Class: Zwischen::AI::OllamaClient

Inherits:
BaseClient show all
Defined in:
lib/zwischen/ai/ollama_client.rb

Instance Attribute Summary

Attributes inherited from BaseClient

#api_key, #config

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, config: {}) ⇒ OllamaClient

Returns a new instance of OllamaClient.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zwischen/ai/ollama_client.rb', line 10

def initialize(api_key: nil, config: {})
  super
  # Ollama usually doesn't need an API key, but we accept it if provided
  
  base_url = @config["url"] || "http://localhost:11434"
  # Ensure base URL doesn't end with /api/chat if user provided full path
  base_url = base_url.sub(/\/api\/chat\/?$/, "")

  @client = Faraday.new(url: base_url) do |conn|
    conn.request :json
    conn.response :json, content_type: /\bjson$/
    # Local models can take a while to load and generate; default 60s
    # is too tight for larger models.
    conn.options.timeout = (@config["timeout"] || 180).to_i
    conn.adapter Faraday.default_adapter
  end
end

Instance Method Details

#analyze(prompt) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zwischen/ai/ollama_client.rb', line 28

def analyze(prompt)
  model = @config["model"] || "llama3"

  response = @client.post("/api/chat") do |req|
    req.body = {
      model: model,
      messages: [
        {
          role: "user",
          content: prompt
        }
      ],
      stream: false
    }
  end

  if response.success?
    content = response.body.dig("message", "content")
    unless content
      raise Error, "Unexpected Ollama response format: #{response.body}"
    end
    content
  else
    error_message = response.body.dig("error") || "Unknown error"
    raise Error, "Ollama API error: #{error_message}"
  end
rescue Faraday::Error => e
  raise Error, "Ollama connection error: #{e.message}. Is Ollama running?"
end