Class: RagRuby::Embedders::Ollama

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

Instance Method Summary collapse

Methods inherited from Base

#embed_batch

Constructor Details

#initialize(model: "nomic-embed-text", base_url: nil) ⇒ Ollama

Returns a new instance of Ollama.



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

def initialize(model: "nomic-embed-text", base_url: nil)
  @model = model
  @base_url = base_url || ENV["OLLAMA_URL"] || "http://localhost:11434"
end

Instance Method Details

#dimension ⇒ Object



34
35
36
# File 'lib/rag_ruby/embedders/ollama.rb', line 34

def dimension
  768
end

#embed(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rag_ruby/embedders/ollama.rb', line 15

def embed(text)
  uri = URI.parse("#{@base_url}/api/embeddings")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = 30
  http.read_timeout = 120

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

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

  JSON.parse(response.body)["embedding"]
end