Class: Ollama::Embeddings

Inherits:
Object
  • Object
show all
Includes:
RateLimitHandler
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, transport: nil, pipeline: nil) ⇒ Embeddings

Returns a new instance of Embeddings.



25
26
27
28
29
30
# File 'lib/ollama/embeddings.rb', line 25

def initialize(config, transport: nil, pipeline: nil)
  @config = config
  @transport = transport || Transport.build(config)
  @pipeline = pipeline || Pipeline.new(@transport)
  @provider = Providers.build(config, @transport)
end

Instance Attribute Details

#pipelineObject

Returns the value of attribute pipeline.



23
24
25
# File 'lib/ollama/embeddings.rb', line 23

def pipeline
  @pipeline
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)



41
42
43
44
45
46
47
# File 'lib/ollama/embeddings.rb', line 41

def embed(model:, input:, truncate: nil, dimensions: nil, keep_alive: nil, options: nil)
  params = Params::Embeddings.new(
    model: model, input: input, truncate: truncate,
    dimensions: dimensions, keep_alive: keep_alive, options: options
  )
  embed_with_params(params)
end

#embed_with_params(params) ⇒ Array<Float>+

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Parameters:

Returns:

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

    Embedding vector(s)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ollama/embeddings.rb', line 52

def embed_with_params(params)
  # Use provider-specific endpoint
  @provider.embeddings_endpoint

  request = Ollama::Request.new(
    endpoint: :embeddings,
    model: params.model,
    prompt: params.input,
    options: params.options,
    keep_alive: params.keep_alive,
    metadata: {
      base_url: @config.base_url,
      timeout: @config.timeout,
      uri: @provider.embeddings_endpoint
    }
  )

  serializer = Ollama::Serializers::Embeddings.new
  transport_req = serializer.call(request)

  res = with_rate_limit_key_rotation do |api_key|
    @config.apply_auth_to(transport_req, api_key: api_key) if transport_req.is_a?(Transport::Request)
    response = @pipeline.call(transport_req)
    handle_http_error(response.raw, requested_model: params.model) if response.raw && response.code.to_i == 429

    response
  end

  # Parse the Response using Parsers::Embeddings which returns Responses::Embeddings
  parser = Ollama::Parsers::Embeddings.new(provider: @provider)
  parsed_response = parser.call(res)
  response_body = parsed_response.to_h

  # /api/embed returns "embeddings" (plural) as array of arrays
  embeddings = parsed_response.embeddings
  embeddings = response_body["embedding"] if embeddings.nil?

  validate_embedding_response!(embeddings, response_body, params.model)

  format_embedding_result(embeddings, params.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}"
ensure
  if defined?(res) && !res.nil? && res.respond_to?(:raw) && res.raw && !res.raw.is_a?(Net::HTTPSuccess)
    handle_http_error(res.raw, requested_model: params.model)
  end
end