Class: RailsAgents::Providers::OpenAICompatible

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_agents/providers/openai_compatible.rb

Direct Known Subclasses

Grok, OpenAI, OpenRouter

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url:, extra_headers: {}, missing_key_message:) ⇒ OpenAICompatible

Returns a new instance of OpenAICompatible.

Raises:



9
10
11
12
13
14
# File 'lib/rails_agents/providers/openai_compatible.rb', line 9

def initialize(api_key:, base_url:, extra_headers: {}, missing_key_message:)
  raise ConfigurationError, missing_key_message if api_key.to_s.empty?
  @api_key = api_key
  @base_url = base_url
  @extra_headers = extra_headers
end

Instance Method Details

#chat(messages:, client_tools: [], skills: nil, model:, json: false, &block) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_agents/providers/openai_compatible.rb', line 16

def chat(messages:, client_tools: [], skills: nil, model:, json: false, &block)
  tools = client_tools
  body = {
    model: model,
    messages: serialize(messages),
    tools: tools.empty? ? nil : tools.map { |t| {type: "function", function: t} }
  }.compact
  body[:response_format] = {type: "json_object"} if json

  response = connection.post("chat/completions", body.to_json)
  raise ProviderError, parse_error(response) unless response.success?

  data = JSON.parse(response.body)
  choice = data.fetch("choices").first
  message = choice.fetch("message")
  usage = data.fetch("usage", {})

  ProviderResponse.new(
    content: message["content"],
    tool_calls: (message["tool_calls"] || []).map { |tc|
      ToolCall.new(
        id: tc["id"],
        name: tc.dig("function", "name"),
        arguments: JSON.parse(tc.dig("function", "arguments") || "{}")
      )
    },
    usage: Usage.new(usage.fetch("prompt_tokens", 0), usage.fetch("completion_tokens", 0)),
    finish_reason: choice["finish_reason"],
    content_blocks: nil,
    file_ids: [],
    assistant_raw_content: nil
  )
end