Class: RubyLLM::EmbeddingRequest

Inherits:
Object
  • Object
show all
Includes:
Support::Inspectable
Defined in:
lib/ruby_llm/embedding_request.rb

Overview

An EmbeddingRequest is an embedding awaiting a provider-side batch. RubyLLM.embed_later stages one; submit an array of them with RubyLLM.batch, and once the batch completes, collecting the results fills in #result:

requests = texts.map { |text| RubyLLM.embed_later(text) }
batch = RubyLLM.batch(requests)
# later, once batch.refresh.complete?
batch.results
requests.first.result.vectors

Constant Summary

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

#initialize(text, model: nil, provider: nil, dimensions: nil, context: nil) ⇒ EmbeddingRequest

:nodoc:



35
36
37
38
39
40
41
# File 'lib/ruby_llm/embedding_request.rb', line 35

def initialize(text, model: nil, provider: nil, dimensions: nil, context: nil) # :nodoc:
  config = context&.config || RubyLLM.config
  model ||= config.default_embedding_model
  @model, @provider = Models.resolve(model, provider:, config:)
  @text = text
  @dimensions = dimensions
end

Instance Attribute Details

#dimensionsObject (readonly)

The requested vector size, or nil for the model's default.



25
26
27
# File 'lib/ruby_llm/embedding_request.rb', line 25

def dimensions
  @dimensions
end

#modelObject (readonly)

The Model of the embedding model the request targets.



22
23
24
# File 'lib/ruby_llm/embedding_request.rb', line 22

def model
  @model
end

#providerObject (readonly)

The Provider the request will be submitted through.



28
29
30
# File 'lib/ruby_llm/embedding_request.rb', line 28

def provider
  @provider
end

#resultObject

The Embedding, once the batch has completed and its results have been collected. nil until then, and for requests that failed. Batch result collection writes it.



33
34
35
# File 'lib/ruby_llm/embedding_request.rb', line 33

def result
  @result
end

#textObject (readonly)

The text staged for embedding.



19
20
21
# File 'lib/ruby_llm/embedding_request.rb', line 19

def text
  @text
end

Instance Method Details

#inspect_attributesObject

:nodoc:



49
50
51
# File 'lib/ruby_llm/embedding_request.rb', line 49

def inspect_attributes # :nodoc:
  { text: text, model: model.id, dimensions: dimensions, result: result }
end

#renderObject

Returns the request payload this embedding request would send to the provider, in the provider's wire format.



45
46
47
# File 'lib/ruby_llm/embedding_request.rb', line 45

def render
  @provider.render_embedding(text, model: @model, dimensions: @dimensions)
end