Class: Ollama::Client::OpenAICompat::ChatAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama/client/openai_compat.rb

Overview

Adapter for OpenAI-like chat/completions API.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ChatAdapter

Returns a new instance of ChatAdapter.

Parameters:



99
100
101
# File 'lib/ollama/client/openai_compat.rb', line 99

def initialize(client)
  @client = client
end

Instance Method Details

#completionsself

Returns:

  • (self)


104
105
106
# File 'lib/ollama/client/openai_compat.rb', line 104

def completions
  self
end

#create(model:, messages:, tools: nil, temperature: nil, top_p: nil) ⇒ Hash

Create chat completion in OpenAI-compatible format.

Parameters:

  • model (String)
  • messages (Array<Hash>)
  • tools (Array<Hash>, nil) (defaults to: nil)
  • temperature (Float, nil) (defaults to: nil)
  • top_p (Float, nil) (defaults to: nil)

Returns:

  • (Hash)

    OpenAI-style chat completion response



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ollama/client/openai_compat.rb', line 115

def create(model:, messages:, tools: nil, temperature: nil, top_p: nil, **)
  response = @client.chat(
    model: model,
    messages: messages,
    tools: tools,
    options: { temperature: temperature, top_p: top_p }.compact
  )

  {
    "id" => "chatcmpl-#{SecureRandom.hex(12)}",
    "object" => "chat.completion",
    "created" => Time.now.to_i,
    "model" => model,
    "choices" => [
      {
        "index" => 0,
        "message" => {
          "role" => "assistant",
          "content" => response.message.content,
          "tool_calls" => openai_tool_calls(response)
        },
        "finish_reason" => response.done_reason || "stop"
      }
    ]
  }
end