Class: RubyCanUseLLM::EmbeddingResponse
- Inherits:
-
Object
- Object
- RubyCanUseLLM::EmbeddingResponse
- Defined in:
- lib/rubycanusellm/embedding_response.rb
Instance Attribute Summary collapse
-
#embedding ⇒ Object
readonly
Returns the value of attribute embedding.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
- #cosine_similarity(other) ⇒ Object
-
#initialize(embedding:, model:, tokens:, raw:) ⇒ EmbeddingResponse
constructor
A new instance of EmbeddingResponse.
Constructor Details
#initialize(embedding:, model:, tokens:, raw:) ⇒ EmbeddingResponse
Returns a new instance of EmbeddingResponse.
7 8 9 10 11 12 |
# File 'lib/rubycanusellm/embedding_response.rb', line 7 def initialize(embedding:, model:, tokens:, raw:) @embedding = @model = model @tokens = tokens @raw = raw end |
Instance Attribute Details
#embedding ⇒ Object (readonly)
Returns the value of attribute embedding.
5 6 7 |
# File 'lib/rubycanusellm/embedding_response.rb', line 5 def @embedding end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/rubycanusellm/embedding_response.rb', line 5 def model @model end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
5 6 7 |
# File 'lib/rubycanusellm/embedding_response.rb', line 5 def raw @raw end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
5 6 7 |
# File 'lib/rubycanusellm/embedding_response.rb', line 5 def tokens @tokens end |
Instance Method Details
#cosine_similarity(other) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/rubycanusellm/embedding_response.rb', line 14 def cosine_similarity(other) dot = .zip(other).sum { |a, b| a * b } mag_a = Math.sqrt(.sum { |a| a**2 }) mag_b = Math.sqrt(other.sum { |b| b**2 }) return 0.0 if mag_a.zero? || mag_b.zero? dot / (mag_a * mag_b) end |