Class: Ollama::Embeddings
- Inherits:
-
Object
- Object
- Ollama::Embeddings
- 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
-
#embed(model:, input:, truncate: nil, dimensions: nil, keep_alive: nil, options: nil) ⇒ Array<Float>+
Generate embeddings for text input(s).
-
#initialize(config) ⇒ Embeddings
constructor
A new instance of Embeddings.
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)
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 (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] = if req.body = body.to_json @config.apply_auth_to(req) res = Net::HTTP.start( uri.hostname, uri.port, **@config.(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 = response_body["embeddings"] || response_body["embedding"] (, response_body, model) (, 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.}" end |