Module: RubyLLM::Protocols::ChatCompletions::Embeddings

Defined in:
lib/ruby_llm/protocols/chat_completions/embeddings.rb

Overview

Embeddings methods of the OpenAI API integration

Class Method Summary collapse

Class Method Details

.embedding_urlObject



10
11
12
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 10

def embedding_url(...)
  'embeddings'
end

.normalize_sparse_vector(weights) ⇒ Object



48
49
50
51
52
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 48

def normalize_sparse_vector(weights)
  return nil unless weights.is_a?(Hash)

  weights.to_h { |token, weight| [Integer(token), Float(weight)] }
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 23

def parse_embedding_response(response, model:, text:)
  data = response.body
  input_tokens = data.dig('usage', 'prompt_tokens')
  rows = data['data']
  single = rows.length == 1 && !text.is_a?(Array)

  vectors = rows.map { |row| row['embedding'] }
  vectors = vectors.first if single
  sparse_vectors = parse_sparse_vectors(rows, single: single)

  Embedding.new(vectors:, sparse_vectors:, model:, input_tokens:,
                reported_cost: reported_cost(data['usage'] || {}))
end

.parse_sparse_vectors(rows, single:) ⇒ Object

Sparse-capable models return a token-to-weight map beside the dense vector, under lexical_weights on BGE-M3 and sparse_embedding elsewhere. It is an extension: dense-only servers send neither, and then there is nothing to report.



41
42
43
44
45
46
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 41

def parse_sparse_vectors(rows, single:)
  sparse = rows.map { |row| normalize_sparse_vector(row['sparse_embedding'] || row['lexical_weights']) }
  return nil if sparse.all?(&:nil?)

  single ? sparse.first : sparse
end

.render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, provider_options: {}) ⇒ Object

rubocop:disable-next Lint/UnusedMethodArgument



15
16
17
18
19
20
21
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 15

def render_embedding_payload(text, model:, dimensions:, task_type: nil, title: nil, provider_options: {})
  {
    model: model,
    input: text,
    dimensions: dimensions
  }.compact.merge(provider_options)
end

.reported_cost(_usage) ⇒ Object



54
55
56
# File 'lib/ruby_llm/protocols/chat_completions/embeddings.rb', line 54

def reported_cost(_usage)
  nil
end