Module: RubyLLM::Providers::Voyage::Embeddings Private

Defined in:
lib/ruby_llm/providers/voyage/embeddings.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Voyage embeddings request and response mapping.

Instance Method Summary collapse

Instance Method Details

#embedding_urlString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the embeddings endpoint relative to the configured API base.

Returns:

  • (String)


12
13
14
# File 'lib/ruby_llm/providers/voyage/embeddings.rb', line 12

def embedding_url(...)
  'embeddings'
end

#parse_embedding_response(response, model:, text:, output_dtype: nil) ⇒ RubyLLM::Embedding

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a Voyage response into a RubyLLM embedding result.

Parameters:

  • response (Faraday::Response)
  • model (String)
  • text (String, Array<String>)
  • output_dtype (String, Symbol, nil) (defaults to: nil)

Returns:

  • (RubyLLM::Embedding)

Raises:

  • (KeyError)

    when the response omits embedding data

  • (ArgumentError)

    when Base64 data or its output type is invalid



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_llm/providers/voyage/embeddings.rb', line 51

def parse_embedding_response(response, model:, text:, output_dtype: nil)
  data = response.body
  vectors = data.fetch('data').map { |item| item.fetch('embedding') }
  if vectors.first.is_a?(String)
    vectors.map! do |vector|
      RubyLLM::Voyage::VectorDecoder.decode(vector, output_dtype)
    end
  end
  vectors = vectors.first if vectors.length == 1 && !text.is_a?(Array)

  Embedding.new(
    vectors: vectors,
    model: data['model'] || model,
    input_tokens: data.dig('usage', 'total_tokens') || 0
  )
end

#render_embedding_payload(text, model:, dimensions: nil, input_type: RubyLLM::Voyage::UNSET, truncation: RubyLLM::Voyage::UNSET, output_dtype: RubyLLM::Voyage::UNSET, encoding_format: RubyLLM::Voyage::UNSET) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts RubyLLM embedding arguments to a Voyage request body. A nil dimensions argument falls back to the configured voyage_default_output_dimension.

Parameters:

  • text (String, Array<String>)
  • model (String)
  • dimensions (Integer, nil) (defaults to: nil)
  • input_type (String, Symbol, nil) (defaults to: RubyLLM::Voyage::UNSET)
  • truncation (Boolean, nil) (defaults to: RubyLLM::Voyage::UNSET)
  • output_dtype (String, Symbol, nil) (defaults to: RubyLLM::Voyage::UNSET)
  • encoding_format (String, Symbol, nil) (defaults to: RubyLLM::Voyage::UNSET)

Returns:

  • (Hash)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_llm/providers/voyage/embeddings.rb', line 28

def render_embedding_payload(text, model:, dimensions: nil, input_type: RubyLLM::Voyage::UNSET,
                             truncation: RubyLLM::Voyage::UNSET, output_dtype: RubyLLM::Voyage::UNSET,
                             encoding_format: RubyLLM::Voyage::UNSET)
  {
    model: model,
    input: text,
    input_type: enum_option(input_type, :voyage_default_input_type),
    truncation: configured_option(truncation, :voyage_default_truncation),
    output_dimension: dimensions || @config.voyage_default_output_dimension,
    output_dtype: enum_option(output_dtype, :voyage_default_output_dtype),
    encoding_format: enum_option(encoding_format, :voyage_default_encoding_format)
  }.compact
end