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: "localhost:11434", model: "llama2", 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: "localhost:11434", model: "llama2", http: Net::Llm.http)
  @host = host
  @model = model
  @http = http
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#httpObject (readonly)

Returns the value of attribute http.



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

def http
  @http
end

#modelObject (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
20
21
22
23
24
# File 'lib/net/llm/ollama.rb', line 14

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

  if block_given?
    stream_request(url, payload, &block)
  else
    post_request(url, payload)
  end
end

#embeddings(input) ⇒ Object



37
38
39
40
41
# File 'lib/net/llm/ollama.rb', line 37

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

#generate(prompt, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/net/llm/ollama.rb', line 26

def generate(prompt, &block)
  url = build_url("/api/generate")
  payload = { model: model, prompt: prompt, stream: block_given? }

  if block_given?
    stream_request(url, payload, &block)
  else
    post_request(url, payload)
  end
end

#show(name) ⇒ Object



49
50
51
52
53
# File 'lib/net/llm/ollama.rb', line 49

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

#tagsObject



43
44
45
46
47
# File 'lib/net/llm/ollama.rb', line 43

def tags
  url = build_url("/api/tags")
  response = http.get(url)
  handle_response(response)
end