Class: Brute::Completion::LangChain

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

Overview

Completion backed by the langchainrb gem (https://github.com/patterns-ai-core/langchainrb). Its LLM classes wrap many providers; build one and hand it over:

Brute.agent
.use(Brute::Middleware::SystemPrompt)
.run(Brute::Completion::LangChain.new(
  llm: Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]),
))

llm:         a Langchain::LLM instance (required; client: is an alias)
model:       model id override (falls back to env[:model], then the
           llm instance's own default)
tools:       tools list, any shape Tools::Adapter accepts
temperature: sampling temperature (default 0.7)

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(llm: nil, client: nil, **options) ⇒ LangChain

Returns a new instance of LangChain.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/brute/completion/lang_chain.rb', line 29

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

  @llm = llm || client or
    raise ArgumentError, "#{self.class} needs an llm: option, e.g. Langchain::LLM::OpenAI.new(api_key: ...)"

  @options = options
end

Instance Method Details

#call(env) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/brute/completion/lang_chain.rb', line 44

def call(env)
  emit(BEFORE_LLM_EVENT, env)

  response = nil
  emit(LLM_DURATION_EVENT, env) { response = @llm.chat(**params(env)) }

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

  # langchainrb speaks the OpenAI-style wire format both ways.
  Brute::MessageTransport::LangChain.wrap_each(reply(response)) do |message|
    env[:messages] << message
  end

  emit(AFTER_LLM_EVENT, env)
  env
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