Class: RagRuby::Generators::Ollama

Inherits:
Base
  • Object
show all
Defined in:
lib/rag_ruby/generators/ollama.rb

Instance Method Summary collapse

Constructor Details

#initialize(model: "llama3.2", base_url: nil) ⇒ Ollama

Returns a new instance of Ollama.



10
11
12
13
# File 'lib/rag_ruby/generators/ollama.rb', line 10

def initialize(model: "llama3.2", base_url: nil)
  @model = model
  @base_url = base_url || ENV["OLLAMA_URL"] || "http://localhost:11434"
end

Instance Method Details

#generate(prompt:, system_prompt: nil, temperature: 0.7) ⇒ Object

Raises:



15
16
17
18
19
20
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
# File 'lib/rag_ruby/generators/ollama.rb', line 15

def generate(prompt:, system_prompt: nil, temperature: 0.7)
  uri = URI.parse("#{@base_url}/api/chat")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 30
  http.read_timeout = 300

  messages = []
  messages << { role: "system", content: system_prompt } if system_prompt
  messages << { role: "user", content: prompt }

  body = {
    model: @model,
    messages: messages,
    stream: false,
    options: { temperature: temperature }
  }

  req = Net::HTTP::Post.new(uri)
  req["Content-Type"] = "application/json"
  req.body = JSON.generate(body)

  response = http.request(req)

  unless response.is_a?(Net::HTTPSuccess)
    raise RagRuby::Error, "Ollama API error (#{response.code}): #{response.body}"
  end

  data = JSON.parse(response.body)
  text = data.dig("message", "content")
  raise RagRuby::Error, "Empty response from Ollama" if text.nil?

  {
    text: text,
    tokens_used: {
      prompt: data["prompt_eval_count"],
      completion: data["eval_count"]
    }
  }
end