Module: RubyLLM::Voyage::VectorDecoder

Defined in:
lib/ruby_llm/voyage/vector_decoder.rb

Overview

Decodes Voyage Base64 vector representations.

The gem decodes API responses automatically; use this directly when parsing embeddings out of Voyage batch result files, which contain the same Base64 encoding.

Class Method Summary collapse

Class Method Details

.decode(vector, output_dtype) ⇒ Array<Float>, Array<Integer>

Decodes a Base64 vector for the requested Voyage output type.

Examples:

Decode an embedding from a batch results file

line = JSON.parse(jsonl_line)
encoded = line.dig('response', 'body', 'data', 0, 'embedding')
RubyLLM::Voyage::VectorDecoder.decode(encoded, :float)

Parameters:

  • vector (String)

    Base64-encoded vector data

  • output_dtype (String, Symbol, nil)

    the output_dtype the embedding was requested with; nil decodes as float

Returns:

  • (Array<Float>, Array<Integer>)

Raises:

  • (ArgumentError)

    when the Base64 data or output type is invalid



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/voyage/vector_decoder.rb', line 24

def decode(vector, output_dtype)
  # unpack1('m0') is strict Base64: it raises ArgumentError on invalid
  # input, matching Base64.strict_decode64 without requiring the
  # base64 gem (no longer a default gem as of Ruby 3.5).
  bytes = vector.unpack1('m0')

  case (output_dtype || 'float').to_s
  when 'float' then bytes.unpack('e*')
  when 'int8', 'binary' then bytes.unpack('c*')
  when 'uint8', 'ubinary' then bytes.unpack('C*')
  else raise ArgumentError, "Unsupported Voyage output dtype: #{output_dtype.inspect}"
  end
end