Class: ActiveRecordVector::Providers::Ollama

Inherits:
Base
  • Object
show all
Defined in:
lib/active_record_vector/providers/ollama.rb

Overview

Ollama Embedding Provider for 100% free, local, private offline AI embeddings

Constant Summary collapse

DEFAULT_MODEL =
"nomic-embed-text"
DEFAULT_HOST =
"http://localhost:11434"

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#embed_batch, #initialize

Constructor Details

This class inherits a constructor from ActiveRecordVector::Providers::Base

Instance Method Details

#embed(text) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record_vector/providers/ollama.rb', line 14

def embed(text)
  base_host = options[:host] || ENV["OLLAMA_HOST"] || DEFAULT_HOST
  uri = URI.parse("#{base_host.chomp('/')}/api/embeddings")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == "https")
  http.open_timeout = options[:timeout] || 15
  http.read_timeout = options[:timeout] || 15

  payload = {
    model: options[:model] || DEFAULT_MODEL,
    prompt: text
  }

  request = Net::HTTP::Post.new(uri.request_uri, {
                                  "Content-Type" => "application/json"
                                })
  request.body = payload.to_json

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

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