Class: RubyLLM::Embedding
- Inherits:
-
Object
- Object
- RubyLLM::Embedding
- Defined in:
- lib/ruby_llm/embedding.rb
Overview
Core embedding interface.
Instance Attribute Summary collapse
-
#input_tokens ⇒ Object
readonly
Returns the value of attribute input_tokens.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#vectors ⇒ Object
readonly
Returns the value of attribute vectors.
Class Method Summary collapse
-
.embed(text, model: nil, provider: nil, assume_model_exists: false, context: nil, dimensions: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- .embedding_count(vectors) ⇒ Object
- .vector_dimensions(vectors) ⇒ Object
Instance Method Summary collapse
-
#initialize(vectors:, model:, input_tokens: 0) ⇒ Embedding
constructor
A new instance of Embedding.
Constructor Details
#initialize(vectors:, model:, input_tokens: 0) ⇒ Embedding
Returns a new instance of Embedding.
8 9 10 11 12 |
# File 'lib/ruby_llm/embedding.rb', line 8 def initialize(vectors:, model:, input_tokens: 0) @vectors = vectors @model = model @input_tokens = input_tokens end |
Instance Attribute Details
#input_tokens ⇒ Object (readonly)
Returns the value of attribute input_tokens.
6 7 8 |
# File 'lib/ruby_llm/embedding.rb', line 6 def input_tokens @input_tokens end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/ruby_llm/embedding.rb', line 6 def model @model end |
#vectors ⇒ Object (readonly)
Returns the value of attribute vectors.
6 7 8 |
# File 'lib/ruby_llm/embedding.rb', line 6 def vectors @vectors end |
Class Method Details
.embed(text, model: nil, provider: nil, assume_model_exists: false, context: nil, dimensions: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby_llm/embedding.rb', line 14 def self.(text, # rubocop:disable Metrics/ParameterLists model: nil, provider: nil, assume_model_exists: false, context: nil, dimensions: nil) config = context&.config || RubyLLM.config model ||= config. model, provider_instance = Models.resolve(model, provider: provider, assume_exists: assume_model_exists, config: config) model_id = model.id payload = { provider: provider_instance.slug, provider_class: provider_instance.class.name, model: model_id, model_info: model, input: text, dimensions: dimensions } RubyLLM.instrument('embedding.ruby_llm', payload, config: config) do |event| result = provider_instance.(text, model: model_id, dimensions:) event[:result] = result event[:response_model] = result.model event[:input_tokens] = result.input_tokens event[:embedding_dimensions] = vector_dimensions(result.vectors) event[:embedding_count] = (result.vectors) result end end |
.embedding_count(vectors) ⇒ Object
53 54 55 56 57 |
# File 'lib/ruby_llm/embedding.rb', line 53 def self.(vectors) return unless vectors.is_a?(Array) vectors.first.is_a?(Array) ? vectors.size : 1 end |
.vector_dimensions(vectors) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/ruby_llm/embedding.rb', line 46 def self.vector_dimensions(vectors) return unless vectors.is_a?(Array) vector = vectors.first.is_a?(Array) ? vectors.first : vectors vector.length if vector.respond_to?(:length) end |