Class: RubyLLM::Agents::EmbeddingResult
- Inherits:
-
Object
- Object
- RubyLLM::Agents::EmbeddingResult
- Includes:
- Trackable
- Defined in:
- lib/ruby_llm/agents/results/embedding_result.rb
Overview
Result object for embedding operations
Wraps embedding vectors with metadata about the operation including token usage, cost, timing, and utility methods for similarity calculations.
Instance Attribute Summary collapse
- #completed_at ⇒ Object readonly
- #count ⇒ Object readonly
- #dimensions ⇒ Object readonly
- #duration_ms ⇒ Object readonly
- #error_class ⇒ Object readonly
- #error_message ⇒ Object readonly
- #execution_id ⇒ Object readonly
- #input_tokens ⇒ Object readonly
- #model_id ⇒ Object readonly
- #started_at ⇒ Object readonly
- #tenant_id ⇒ Object readonly
- #total_cost ⇒ Object readonly
- #vectors ⇒ Object readonly
Instance Method Summary collapse
-
#batch? ⇒ Boolean
Returns whether this result contains multiple embeddings.
-
#error? ⇒ Boolean
Returns whether the execution failed.
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#initialize(attributes = {}) ⇒ EmbeddingResult
constructor
Creates a new EmbeddingResult instance.
-
#most_similar(others, limit: 10, index: 0) ⇒ Array<Hash>
Finds the most similar vectors from a collection.
-
#similarity(other, index: 0) ⇒ Float
Calculates cosine similarity between this embedding and another.
-
#single? ⇒ Boolean
Returns whether this result contains a single embedding.
-
#success? ⇒ Boolean
Returns whether the execution succeeded.
-
#to_h ⇒ Hash
Converts the result to a hash.
-
#vector ⇒ Array<Float>?
Returns the first (or only) embedding vector.
Methods included from Trackable
Constructor Details
#initialize(attributes = {}) ⇒ EmbeddingResult
Creates a new EmbeddingResult instance
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 97 def initialize(attributes = {}) @vectors = attributes[:vectors] || [] @model_id = attributes[:model_id] @dimensions = attributes[:dimensions] @input_tokens = attributes[:input_tokens] @total_cost = attributes[:total_cost] @duration_ms = attributes[:duration_ms] @count = attributes[:count] || @vectors.size @started_at = attributes[:started_at] @completed_at = attributes[:completed_at] @tenant_id = attributes[:tenant_id] @error_class = attributes[:error_class] @error_message = attributes[:error_message] @execution_id = attributes[:execution_id] @agent_class_name = attributes[:agent_class_name] register_with_tracker end |
Instance Attribute Details
#completed_at ⇒ Object (readonly)
64 65 66 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 64 def completed_at @completed_at end |
#count ⇒ Object (readonly)
56 57 58 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 56 def count @count end |
#dimensions ⇒ Object (readonly)
40 41 42 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 40 def dimensions @dimensions end |
#duration_ms ⇒ Object (readonly)
52 53 54 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 52 def duration_ms @duration_ms end |
#error_class ⇒ Object (readonly)
72 73 74 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 72 def error_class @error_class end |
#error_message ⇒ Object (readonly)
76 77 78 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 76 def @error_message end |
#execution_id ⇒ Object (readonly)
80 81 82 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 80 def execution_id @execution_id end |
#input_tokens ⇒ Object (readonly)
44 45 46 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 44 def input_tokens @input_tokens end |
#model_id ⇒ Object (readonly)
36 37 38 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 36 def model_id @model_id end |
#started_at ⇒ Object (readonly)
60 61 62 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 60 def started_at @started_at end |
#tenant_id ⇒ Object (readonly)
68 69 70 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 68 def tenant_id @tenant_id end |
#total_cost ⇒ Object (readonly)
48 49 50 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 48 def total_cost @total_cost end |
#vectors ⇒ Object (readonly)
32 33 34 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 32 def vectors @vectors end |
Instance Method Details
#batch? ⇒ Boolean
Returns whether this result contains multiple embeddings
132 133 134 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 132 def batch? count > 1 end |
#error? ⇒ Boolean
Returns whether the execution failed
155 156 157 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 155 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
118 119 120 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 118 def execution @execution ||= RubyLLM::Agents::Execution.find_by(id: execution_id) if execution_id end |
#most_similar(others, limit: 10, index: 0) ⇒ Array<Hash>
Finds the most similar vectors from a collection
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 196 def most_similar(others, limit: 10, index: 0) v1 = vectors[index] return [] if v1.nil? similarities = others.each_with_index.map do |other, idx| v2 = case other when EmbeddingResult other.vector when Array other else next nil end next nil if v2.nil? {index: idx, similarity: cosine_similarity(v1, v2)} end.compact similarities.sort_by { |s| -s[:similarity] }.first(limit) end |
#similarity(other, index: 0) ⇒ Float
Calculates cosine similarity between this embedding and another
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 170 def similarity(other, index: 0) v1 = vectors[index] return nil if v1.nil? v2 = case other when EmbeddingResult other.vector when Array other else raise ArgumentError, "other must be EmbeddingResult or Array, got #{other.class}" end return nil if v2.nil? cosine_similarity(v1, v2) end |
#single? ⇒ Boolean
Returns whether this result contains a single embedding
125 126 127 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 125 def single? count == 1 end |
#success? ⇒ Boolean
Returns whether the execution succeeded
148 149 150 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 148 def success? error_class.nil? end |
#to_h ⇒ Hash
Converts the result to a hash
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 221 def to_h { vectors: vectors, model_id: model_id, dimensions: dimensions, input_tokens: input_tokens, total_cost: total_cost, duration_ms: duration_ms, count: count, started_at: started_at, completed_at: completed_at, tenant_id: tenant_id, error_class: error_class, error_message: , execution_id: execution_id } end |
#vector ⇒ Array<Float>?
Returns the first (or only) embedding vector
Convenience method for single-text embeddings.
141 142 143 |
# File 'lib/ruby_llm/agents/results/embedding_result.rb', line 141 def vector vectors.first end |