Class: Zwischen::AI::OllamaClient
- Inherits:
-
BaseClient
- Object
- BaseClient
- Zwischen::AI::OllamaClient
- Defined in:
- lib/zwischen/ai/ollama_client.rb
Instance Attribute Summary
Attributes inherited from BaseClient
Instance Method Summary collapse
- #analyze(prompt) ⇒ Object
-
#initialize(api_key: nil, config: {}) ⇒ OllamaClient
constructor
A new instance of OllamaClient.
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..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 = response.body.dig("error") || "Unknown error" raise Error, "Ollama API error: #{}" end rescue Faraday::Error => e raise Error, "Ollama connection error: #{e.}. Is Ollama running?" end |