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

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 embedding_payload(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 embedding_url(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 media_embedding_payload(text, attachments, model:, dimensions:, task_type: nil, title: nil)
  embedding_payload(Media.format_content(text, attachments), 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 parse_embedding_response(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 render_embedding(text, dimensions: nil, **options)
  payload = render_embedding_payload(text, dimensions:, **options)
  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 render_embedding_payload(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)

               [media_embedding_payload(text, with, model:, dimensions:, task_type:, title:)]
             else
               [text].flatten.map do |t|
                 single_embedding_payload(t, model:, dimensions:, task_type:, title:)
               end
             end

  Support::Utils.deep_merge({ requests: requests }, provider_options)
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 single_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil)
  embedding_payload([{ text: text.to_s }], model:, dimensions:, task_type:, title:)
end

.supports_embedding_media?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ruby_llm/protocols/gemini/embeddings.rb', line 29

def supports_embedding_media?
  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 render_embedding(text, dimensions: nil, **options)
  payload = render_embedding_payload(text, dimensions:, **options)
  return payload if text.is_a?(Array) || !payload.key?(:requests)

  payload.fetch(:requests).first
end