Class: RubyLLM::Providers::VertexAI::EmbedContent

Inherits:
RubyLLM::Protocols::Gemini show all
Defined in:
lib/ruby_llm/providers/vertexai/embed_content.rb

Overview

Gemini Embedding 2 over Vertex AI's single-content embedding endpoint.

Constant Summary

Constants inherited from RubyLLM::Protocols::Gemini

RubyLLM::Protocols::Gemini::SERVER_TOOL_ALIASES

Instance Attribute Summary

Attributes inherited from RubyLLM::Protocol

#config, #connection, #model, #provider

Instance Method Summary collapse

Methods inherited from RubyLLM::Protocols::Gemini

#attachment_filename, #build_file_attachment, #build_inline_attachment, #build_response_content, #server_tool_aliases

Methods inherited from RubyLLM::Protocol

abstract, #animate_later, #apply_compaction, #apply_compaction_headers, #apply_end_user, #cache_content, #compact, #complete, #count_tokens, #delete_cache, #embed, #extend_cache, #find_cache, #initialize, #list_models, #moderate, #ocr, #paint, #parse_error, #parse_image_responses, #post_image, #post_video, #preprocess_message, #raise_transcription_streaming_unsupported, #refresh_video_job, #render, #render_transcription_options, #render_video_extension_payload, #rerank, #server_tool_aliases, #speak, #stream_speech, #stream_speech_response, #stream_transcription, #supports_embedding_media?, #tokenize, #tool_approval_response, #transcribe, #video_extension_attachment, #video_extension_url, #video_request_url

Methods included from RubyLLM::Protocol::BinaryStreaming

#stream_binary

Methods included from RubyLLM::Protocol::Streaming

build_on_data_handler, build_stream_error_response, error_chunk?, failed_http_status, faraday_1?, handle_data, handle_error_chunk, handle_error_event, handle_failed_response, handle_json_error_chunk, handle_sse, handle_stream, json_error_payload?, parse_error_from_json, parse_streaming_error, process_stream_chunk, raise_stream_error, stream_events, stream_response, stream_state

Constructor Details

This class inherits a constructor from RubyLLM::Protocol

Instance Method Details

#embedding_url(model:) ⇒ Object



8
9
10
# File 'lib/ruby_llm/providers/vertexai/embed_content.rb', line 8

def embedding_url(model:)
  "#{@provider.model_path(model)}:embedContent"
end

#parse_embedding_response(response, model:, text:) ⇒ Object

Raises:



32
33
34
35
36
37
38
# File 'lib/ruby_llm/providers/vertexai/embed_content.rb', line 32

def parse_embedding_response(response, model:, text:)
  vectors = response.body.dig('embedding', 'values')
  raise Error.new('Vertex AI returned no embedding', response:) if vectors.nil? || vectors.empty?

  vectors = [vectors] if text.is_a?(Array)
  Embedding.new(vectors:, model:, input_tokens: response.body.dig('usageMetadata', 'promptTokenCount'))
end

#render_embedding(text, **options) ⇒ Object



12
13
14
15
16
# File 'lib/ruby_llm/providers/vertexai/embed_content.rb', line 12

def render_embedding(text, **options)
  return super unless text.is_a?(Array)

  { requests: text.map { |value| render_embedding_payload(value, **options) } }
end

#render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [], provider_options: {}) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_llm/providers/vertexai/embed_content.rb', line 18

def render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, with: [],
                             provider_options: {})
  if text.is_a?(Array) && text.size != 1
    raise ArgumentError, 'Vertex AI embedContent accepts one text at a time'
  end
  raise ArgumentError, "#{model} takes task instructions and titles in the text" if task_type || title

  payload = {
    content: { parts: Protocols::Gemini::Media.format_content(text.is_a?(Array) ? text.first : text, with) },
    outputDimensionality: dimensions
  }.compact
  Support::Utils.deep_merge(payload, provider_options)
end