Class: Brute::Completion::RubyLLM

Inherits:
Object
  • Object
show all
Includes:
Hooks
Defined in:
lib/brute/completion/ruby_llm.rb

Overview

Completion backed by the ruby_llm gem.

Brute.agent
.use(Brute::Middleware::SystemPrompt)
.run(Brute::Completion::RubyLLM.new(provider: :ollama, model: "llama3.2:latest"))

Anything not given at point of use falls back to env, so a pipeline that sets env / env still flows through.

provider:    LLM provider name (falls back to env[:provider])
model:       model id (falls back to env[:model])
tools:       tools list, any shape Tools::Adapter accepts
temperature: sampling temperature (default 0.7)
streaming:   stream chunks as :content / :reasoning events
client:      injectable completion client (tests, custom transports);
           anything responding to complete(messages, **kwargs)

Defined Under Namespace

Classes: Tool

Constant Summary collapse

DEFAULT_TEMPERATURE =
0.7

Constants included from Hooks

Hooks::AFTER_LLM_EVENT, Hooks::AFTER_TOOL_EVENT, Hooks::APPROVE_TOOL_EVENT, Hooks::BEFORE_LLM_EVENT, Hooks::BEFORE_TOOL_EVENT, Hooks::COMPACTED_EVENT, Hooks::COMPACT_DURATION_EVENT, Hooks::DURATION_EVENT, Hooks::ENTER_EVENT, Hooks::EXIT_EVENT, Hooks::FARADAY_ERROR_EVENT, Hooks::LLM_DURATION_EVENT, Hooks::LLM_FAILURE_EVENT, Hooks::MIDDLEWARE_ADDED_EVENT, Hooks::OPEN_ROUTER_SERVER_ERROR_EVENT, Hooks::STANDARD_ERROR_EVENT, Hooks::TOOL_DURATION_EVENT, Hooks::TURN_DURATION_EVENT, Hooks::TURN_END_EVENT, Hooks::TURN_START_EVENT

Instance Method Summary collapse

Methods included from Hooks

new

Constructor Details

#initialize(**options) ⇒ RubyLLM

Returns a new instance of RubyLLM.



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brute/completion/ruby_llm.rb', line 49

def initialize(**options)
  # Brute depends on no LLM library: the provider gem is required here,
  # at point of use, and only for this completion.
  begin
    require "ruby_llm"
  rescue LoadError
    raise LoadError, "#{self.class} needs the 'ruby_llm' gem — add `gem \"ruby_llm\"` to your Gemfile."
  end

  @options = options
end

Instance Method Details

#call(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/brute/completion/ruby_llm.rb', line 61

def call(env)
  emit(BEFORE_LLM_EVENT, env)

  messages = Brute::MessageTransport::RubyLLM.dump_all(env[:messages])
  response = nil
  emit(LLM_DURATION_EVENT, env) { response = complete(env, messages) }

  unless response.nil?
    if (usage = Brute::MessageTransport::RubyLLM.usage_metrics(response))
      (env[:metadata] ||= {})[:last_llm_usage] = usage
    end

    Brute::MessageTransport::RubyLLM.wrap_each(response) do |message|
      env[:messages] << message
    end
  end

  emit(AFTER_LLM_EVENT, env)
  env
# A provider call that raises is reported through the hooks rather than
# up the stack, the same way the OpenRouter completion does it.
rescue => error
  emit(LLM_FAILURE_EVENT, env)

  if defined?(::Faraday::Error) && error.is_a?(::Faraday::Error)
    emit(FARADAY_ERROR_EVENT, env, error)
  else
    emit(STANDARD_ERROR_EVENT, env, error)
  end

  env
end