Class: Ollama::Embeddings

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

Overview

Embeddings API helper for semantic search and RAG in agents

This is a helper module used internally by Client. Use client.embeddings.embed() instead of instantiating this directly.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Embeddings

Returns a new instance of Embeddings.



14
15
16
# File 'lib/ollama/embeddings.rb', line 14

def initialize(config)
  @config = config
end

Instance Method Details

#embed(model:, input:, truncate: nil, dimensions: nil, keep_alive: nil, options: nil) ⇒ Array<Float>+

Generate embeddings for text input(s)

Parameters:

  • model (String)

    Embedding model name (e.g., “all-minilm”)

  • input (String, Array<String>)

    Single text or array of texts

  • truncate (Boolean, nil) (defaults to: nil)

    If true, truncate inputs exceeding context window

  • dimensions (Integer, nil) (defaults to: nil)

    Number of dimensions for embeddings

  • keep_alive (String, nil) (defaults to: nil)

    Model keep-alive duration (e.g. “5m”)

  • options (Hash, nil) (defaults to: nil)

    Runtime options (temperature, etc.)

Returns:

  • (Array<Float>, Array<Array<Float>>)

    Embedding vector(s)



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
65
# File 'lib/ollama/embeddings.rb', line 27

def embed(model:, input:, truncate: nil, dimensions: nil, keep_alive: nil, options: nil)
  # Use /api/embed (not /api/embeddings) - the working endpoint
  uri = URI("#{@config.base_url}/api/embed")
  req = Net::HTTP::Post.new(uri)
  req["Content-Type"] = "application/json"

  body = {
    model: model,
    input: input
  }
  body[:truncate] = truncate unless truncate.nil?
  body[:dimensions] = dimensions if dimensions
  body[:keep_alive] = keep_alive if keep_alive
  body[:options] = options if options

  req.body = body.to_json
  @config.apply_auth_to(req)
  res = Net::HTTP.start(
    uri.hostname,
    uri.port,
    **@config.http_connection_options(uri)
  ) { |http| http.request(req) }

  handle_http_error(res, requested_model: model) unless res.is_a?(Net::HTTPSuccess)

  response_body = JSON.parse(res.body)
  # /api/embed returns "embeddings" (plural) as array of arrays
  embeddings = response_body["embeddings"] || response_body["embedding"]

  validate_embedding_response!(embeddings, response_body, model)

  format_embedding_result(embeddings, input)
rescue JSON::ParserError => e
  raise InvalidJSONError, "Failed to parse embeddings response: #{e.message}"
rescue Net::ReadTimeout, Net::OpenTimeout
  raise TimeoutError, "Request timed out after #{@config.timeout}s"
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => e
  raise Error, "Connection failed: #{e.message}"
end