Class: Llm::OpenAiCompatibleAdapter

Inherits:
Object
  • Object
show all
Defined in:
app/services/llm/open_ai_compatible_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(credential) ⇒ OpenAiCompatibleAdapter

Returns a new instance of OpenAiCompatibleAdapter.



6
7
8
9
# File 'app/services/llm/open_ai_compatible_adapter.rb', line 6

def initialize(credential)
  @credential = credential
  @base_uri = ProviderRegistry.base_uri_for(credential.provider)
end

Instance Method Details

#call_tool(system:, tool:, max_tokens:, content_blocks: nil, messages: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/llm/open_ai_compatible_adapter.rb', line 11

def call_tool(system:, tool:, max_tokens:, content_blocks: nil, messages: nil)
  wire_messages = messages || [ { role: "user", content: content_blocks.map { |block| to_wire_block(block) } } ]
  response = HTTParty.post(
    "#{@base_uri}/chat/completions",
    headers: { "Authorization" => "Bearer #{@credential.api_key}", "Content-Type" => "application/json" },
    body: {
      model: @credential.model,
      max_tokens: max_tokens,
      messages: [ { role: "system", content: system } ] + wire_messages,
      tools: [ { type: "function", function: { name: tool[:name], description: tool[:description], parameters: tool[:input_schema] } } ],
      tool_choice: { type: "function", function: { name: tool[:name] } }
    }.to_json
  )
  raise "Llm::OpenAiCompatibleAdapter: API error #{response.code}#{response.body}" unless response.success?

  arguments = response.parsed_response.dig("choices", 0, "message", "tool_calls", 0, "function", "arguments")
  raise "Llm::OpenAiCompatibleAdapter: no #{tool[:name]} tool call in response" unless arguments

  CallResult.new(
    data: JSON.parse(arguments),
    input_tokens: response.parsed_response.dig("usage", "prompt_tokens") || 0,
    output_tokens: response.parsed_response.dig("usage", "completion_tokens") || 0
  )
end