Class: Regent::LLM::Ollama

Inherits:
Base
  • Object
show all
Defined in:
lib/regent/llm/ollama.rb

Constant Summary collapse

DEFAULT_HOST =

Default host for Ollama API.

"http://localhost:11434"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Dependable

included, #require_dynamic

Constructor Details

#initialize(model:, host: nil, **options) ⇒ Ollama

Returns a new instance of Ollama.



9
10
11
12
13
# File 'lib/regent/llm/ollama.rb', line 9

def initialize(model:, host: nil, **options)
  @model = model
  @host = host || DEFAULT_HOST
  @options = options
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



15
16
17
# File 'lib/regent/llm/ollama.rb', line 15

def model
  @model
end

Instance Method Details

#invoke(messages, **args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/regent/llm/ollama.rb', line 17

def invoke(messages, **args)
  response = client.post("/api/chat", {
    model: model,
    messages: messages,
    stream: false
  })

  if response.status == 200
    result(
      model: response.body.dig("model"),
      content: response.body.dig("message", "content").strip,
      input_tokens: nil,
      output_tokens: nil
    )
  else
    raise ApiError, response.body.dig("error")
  end
end

#parse_error(error) ⇒ Object



36
37
38
# File 'lib/regent/llm/ollama.rb', line 36

def parse_error(error)
  error.message
end