Module: RubyLLM::Protocols::Gemini::Embeddings
- Defined in:
- lib/ruby_llm/protocols/gemini/embeddings.rb
Overview
Embeddings methods for the Gemini API integration
Class Method Summary collapse
- .embedding_payload(parts, model:, dimensions:, task_type:, title:) ⇒ Object
- .embedding_url(model:) ⇒ Object
- .media_embedding_payload(text, attachments, model:, dimensions:, task_type: nil, title: nil) ⇒ Object
- .parse_embedding_response(response, model:, text:) ⇒ Object
- .render_embedding(text, dimensions: nil, **options) ⇒ Object
- .render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object
- .single_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil) ⇒ Object
- .supports_embedding_media? ⇒ Boolean
Instance Method Summary collapse
Class Method Details
.embedding_payload(parts, model:, dimensions:, task_type:, title:) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 58 def (parts, model:, dimensions:, task_type:, title:) { model: "models/#{model}", content: { parts: parts }, outputDimensionality: dimensions, taskType: task_type, title: title }.compact end |
.embedding_url(model:) ⇒ Object
10 11 12 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 10 def (model:) "models/#{model}:batchEmbedContents" end |
.media_embedding_payload(text, attachments, model:, dimensions:, task_type: nil, title: nil) ⇒ Object
54 55 56 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 54 def (text, , model:, dimensions:, task_type: nil, title: nil) (Media.format_content(text, ), model:, dimensions:, task_type:, title:) end |
.parse_embedding_response(response, model:, text:) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 41 def (response, model:, text:) vectors = response.body['embeddings']&.map { |e| e['values'] } vectors = vectors.first if vectors&.length == 1 && !text.is_a?(Array) Embedding.new(vectors:, model:) end |
.render_embedding(text, dimensions: nil, **options) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 33 def (text, dimensions: nil, **) payload = (text, dimensions:, **) return payload if text.is_a?(Array) || !payload.key?(:requests) payload.fetch(:requests).first end |
.render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 14 def (text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) requests = if with.any? raise ArgumentError, 'embed one text at a time when embedding attachments' if text.is_a?(Array) [(text, with, model:, dimensions:, task_type:, title:)] else [text].flatten.map do |t| (t, model:, dimensions:, task_type:, title:) end end Support::Utils.deep_merge({ requests: requests }, ) end |
.single_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil) ⇒ Object
50 51 52 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 50 def (text, model:, dimensions:, task_type: nil, title: nil) ([{ text: text.to_s }], model:, dimensions:, task_type:, title:) end |
.supports_embedding_media? ⇒ Boolean
29 30 31 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 29 def true end |
Instance Method Details
#render_embedding(text, dimensions: nil, **options) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 33 def (text, dimensions: nil, **) payload = (text, dimensions:, **) return payload if text.is_a?(Array) || !payload.key?(:requests) payload.fetch(:requests).first end |