Class: SmartBrain::ModelProvider::Ollama
- Includes:
- Rerankable
- Defined in:
- lib/smart_brain/model_provider/ollama.rb
Overview
Ollama 本地 provider(OpenAI 兼容)。complete 走 /api/chat;rerank 走 LLM-as-judge。
Instance Method Summary collapse
- #complete(prompt:, system: nil, temperature: nil, max_tokens: nil, think: nil) ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(model:, base_url:, rerank_model: nil, temperature: 0.2, timeout_seconds: 30, think: false, **_) ⇒ Ollama
constructor
A new instance of Ollama.
- #llm? ⇒ Boolean
Methods included from Rerankable
Methods inherited from Base
Constructor Details
#initialize(model:, base_url:, rerank_model: nil, temperature: 0.2, timeout_seconds: 30, think: false, **_) ⇒ Ollama
Returns a new instance of Ollama.
13 14 15 16 17 18 19 20 |
# File 'lib/smart_brain/model_provider/ollama.rb', line 13 def initialize(model:, base_url:, rerank_model: nil, temperature: 0.2, timeout_seconds: 30, think: false, **_) @model = model @rerank_model = rerank_model || model @temperature = temperature @timeout_seconds = timeout_seconds @think = think @base_url = base_url.to_s.chomp('/') end |
Instance Method Details
#complete(prompt:, system: nil, temperature: nil, max_tokens: nil, think: nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/smart_brain/model_provider/ollama.rb', line 30 def complete(prompt:, system: nil, temperature: nil, max_tokens: nil, think: nil) = [] << { 'role' => 'system', 'content' => system } if system && !system.empty? << { 'role' => 'user', 'content' => prompt } body = { 'model' => @model, 'messages' => , 'stream' => false } body['think'] = think.nil? ? @think : think = { 'temperature' => temperature || @temperature } ['num_predict'] = max_tokens if max_tokens body['options'] = response = connection.post('/api/chat', JSON.generate(body)) raise "ollama HTTP #{response.status}" unless response.status.between?(200, 299) { text: strip_think(JSON.parse(response.body).dig('message', 'content').to_s) } rescue StandardError => e { text: '', error: "#{e.class}: #{e.}" } end |
#enabled? ⇒ Boolean
26 27 28 |
# File 'lib/smart_brain/model_provider/ollama.rb', line 26 def enabled? true end |
#llm? ⇒ Boolean
22 23 24 |
# File 'lib/smart_brain/model_provider/ollama.rb', line 22 def llm? true end |