Class: SmartBrain::ModelProvider::Ollama

Inherits:
Base
  • Object
show all
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

Methods included from Rerankable

#rerank

Methods inherited from Base

#rerank

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)
  messages = []
  messages << { 'role' => 'system', 'content' => system } if system && !system.empty?
  messages << { 'role' => 'user', 'content' => prompt }

  body = { 'model' => @model, 'messages' => messages, 'stream' => false }
  body['think'] = think.nil? ? @think : think
  options = { 'temperature' => temperature || @temperature }
  options['num_predict'] = max_tokens if max_tokens
  body['options'] = 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.message}" }
end

#enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/smart_brain/model_provider/ollama.rb', line 26

def enabled?
  true
end

#llm?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/smart_brain/model_provider/ollama.rb', line 22

def llm?
  true
end