Class: AgentHarness::Api::RubyLlmChatAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/api/ruby_llm_chat_adapter.rb

Overview

Translates the normalized public chat values to RubyLLM public objects.

Defined Under Namespace

Classes: MissingCredentialError, StreamState, UnsupportedOptionError, UsageInstrumenter

Constant Summary collapse

PROVIDER_CONFIG =
{
  anthropic: %i[anthropic_api_key anthropic_api_base],
  openai: %i[openai_api_key openai_api_base]
}.freeze
DEFAULT_REQUEST_TIMEOUT =
300
OPENAI_UNSUPPLIED_CONFIG =
%i[openai_organization_id openai_project_id openai_use_system_role].freeze

Instance Method Summary collapse

Instance Method Details

#call(candidate:, messages:, tools:, max_output_tokens:, temperature:, stream:, timeout:, cancellation:, schema: nil, on_accounting: nil, prepared_chat: nil, &on_event) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 36

def call(candidate:, messages:, tools:, max_output_tokens:, temperature:, stream:, timeout:, cancellation:,
  schema: nil, on_accounting: nil, prepared_chat: nil, &on_event)
  provider_usage_reported = false
  chat = prepared_chat || prepare(candidate:, messages:, tools:, max_output_tokens:, temperature:, timeout:,
    schema:, on_accounting:, provider_usage: -> { provider_usage_reported })
  response, streamed_refusal = generate(chat, stream, cancellation) do |event|
    provider_usage_reported = true if event[:type] == :usage_updated
    on_event&.call(event)
  end
  emit_completed_tool_calls(response, &on_event) if stream
  normalize_response(response, streamed_refusal: streamed_refusal)
end

#prepare(candidate:, messages:, tools:, max_output_tokens:, temperature:, timeout:, on_accounting: nil, schema: nil, provider_usage: -> { false }) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/agent_harness/api/ruby_llm_chat_adapter.rb', line 49

def prepare(candidate:, messages:, tools:, max_output_tokens:, temperature:, timeout:, on_accounting: nil,
  schema: nil, provider_usage: -> { false })
  context = build_context(candidate, timeout, on_accounting, provider_usage)
  context.chat(model: candidate[:model], provider: candidate[:provider], protocol: ruby_llm_protocol(candidate),
    assume_model_exists: true).tap do |chat|
    configure_chat(chat, candidate, messages, tools, max_output_tokens, temperature, schema)
  end
end