Class: Net::Llm::Ollama

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: ENV.fetch("OLLAMA_HOST", "localhost:11434"), model: "gpt-oss", http: Net::Llm.http) ⇒ Ollama

Returns a new instance of Ollama.



8
9
10
11
12
# File 'lib/net/llm/ollama.rb', line 8

def initialize(host: ENV.fetch("OLLAMA_HOST", "localhost:11434"), model: "gpt-oss", http: Net::Llm.http)
  @host = host
  @model = model
  @http = http
end

Instance Attribute Details

#host ⇒ Object (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/net/llm/ollama.rb', line 6

def host
  @host
end

#http ⇒ Object (readonly)

Returns the value of attribute http.



6
7
8
# File 'lib/net/llm/ollama.rb', line 6

def http
  @http
end

#model ⇒ Object (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/net/llm/ollama.rb', line 6

def model
  @model
end

Instance Method Details

#chat(messages, tools = [], &block) ⇒ Object



14
15
16
17
18
19
# File 'lib/net/llm/ollama.rb', line 14

def chat(messages, tools = [], &block)
  payload = { model: model, messages: messages, stream: block_given? }
  payload[:tools] = tools unless tools.empty?

  execute(build_url("/api/chat"), payload, &block)
end

#embeddings(input) ⇒ Object



74
75
76
# File 'lib/net/llm/ollama.rb', line 74

def embeddings(input)
  post_request(build_url("/api/embed"), { model: model, input: input })
end

#fetch(messages, tools = [], &block) ⇒ Object



21
22
23
24
25
26
27
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
57
58
59
60
61
62
63
64
# File 'lib/net/llm/ollama.rb', line 21

def fetch(messages, tools = [], &block)
  content = ""
  thinking = ""
  tool_calls = []

  if block_given?
    chat(messages, tools) do |chunk|
      msg = chunk["message"] || {}
      delta_content = msg["content"]
      delta_thinking = msg["thinking"]

      content += delta_content if delta_content
      thinking += delta_thinking if delta_thinking
      tool_calls += normalize_tool_calls(msg["tool_calls"]) if msg["tool_calls"]

      if chunk["done"]
        block.call({
          type: :complete,
          content: content,
          thinking: thinking.empty? ? nil : thinking,
          tool_calls: tool_calls,
          stop_reason: map_stop_reason(chunk["done_reason"])
        })
      else
        block.call({
          type: :delta,
          content: delta_content,
          thinking: delta_thinking,
          tool_calls: nil
        })
      end
    end
  else
    result = chat(messages, tools)
    msg = result["message"] || {}
    {
      type: :complete,
      content: msg["content"],
      thinking: msg["thinking"],
      tool_calls: normalize_tool_calls(msg["tool_calls"]),
      stop_reason: map_stop_reason(result["done_reason"])
    }
  end
end

#generate(prompt, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/net/llm/ollama.rb', line 66

def generate(prompt, &block)
  execute(build_url("/api/generate"), {
    model: model,
    prompt: prompt,
    stream: block_given?
  }, &block)
end

#show(name) ⇒ Object



82
83
84
# File 'lib/net/llm/ollama.rb', line 82

def show(name)
  post_request(build_url("/api/show"), { name: name })
end

#tags ⇒ Object



78
79
80
# File 'lib/net/llm/ollama.rb', line 78

def tags
  get_request(build_url("/api/tags"))
end