Class: ActiveHarness::Providers::Ollama

Inherits:
Base
  • Object
show all
Defined in:
lib/active_harness/providers/ollama.rb

Overview

Ollama — local model inference server, OpenAI-compatible API. ollama.com/blog/openai-compatibility

Set OLLAMA_API_BASE to override the default local address. OLLAMA_API_KEY is optional (needed only if Ollama is behind a proxy with auth).

Example:

model do
  use provider: :ollama, model: "llama3.2"
end

Constant Summary

Constants inherited from Base

Base::HTTP, Base::STREAMING_HTTP

Instance Method Summary collapse

Instance Method Details

#call(model:, messages:, temperature: 0.7, stream: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_harness/providers/ollama.rb', line 16

def call(model:, messages:, temperature: 0.7, stream: nil)
  url     = "#{api_base}/v1/chat/completions"
  headers = { "Content-Type" => "application/json" }
  key     = api_key
  headers["Authorization"] = "Bearer #{key}" if key
  body    = { model: model, messages: messages, temperature: temperature }

  return call_streaming(url: url, headers: headers, body: body, stream: stream, provider: :ollama, model: model) if stream

  raw  = post_json(URI(url), headers: headers, body: body)
  data = parse!(raw)
  handle_error!(data)

  { content: data.dig("choices", 0, "message", "content").to_s.strip, provider: :ollama, model: data["model"] || model, usage: extract_usage_openai(data) }
end