Class: Ollama::Embeddings
- Inherits:
-
Object
- Object
- Ollama::Embeddings
- 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
-
#pipeline ⇒ Object
Returns the value of attribute pipeline.
Instance Method Summary collapse
-
#embed(model:, input:, truncate: nil, dimensions: nil, keep_alive: nil, options: nil) ⇒ Array<Float>+
Generate embeddings for text input(s).
-
#embed_with_params(params) ⇒ Array<Float>+
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#initialize(config, transport: nil, pipeline: nil) ⇒ Embeddings
constructor
A new instance of Embeddings.
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
#pipeline ⇒ Object
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)
41 42 43 44 45 46 47 |
# File 'lib/ollama/embeddings.rb', line 41 def (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: ) (params) end |
#embed_with_params(params) ⇒ Array<Float>+
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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 (params) # Use provider-specific endpoint @provider. request = Ollama::Request.new( endpoint: :embeddings, model: params.model, prompt: params.input, options: params., keep_alive: params.keep_alive, metadata: { base_url: @config.base_url, timeout: @config.timeout, uri: @provider. } ) 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 = parsed_response. = response_body["embedding"] if .nil? (, response_body, params.model) (, params.input) rescue JSON::ParserError => e raise InvalidJSONError, "Failed to parse embeddings response: #{e.}" 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.}" 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 |