Class: RubyCanUseLLM::EmbeddingResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycanusellm/embedding_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = embedding
  @model = model
  @tokens = tokens
  @raw = raw
end

Instance Attribute Details

#embeddingObject (readonly)

Returns the value of attribute embedding.



5
6
7
# File 'lib/rubycanusellm/embedding_response.rb', line 5

def embedding
  @embedding
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/rubycanusellm/embedding_response.rb', line 5

def model
  @model
end

#rawObject (readonly)

Returns the value of attribute raw.



5
6
7
# File 'lib/rubycanusellm/embedding_response.rb', line 5

def raw
  @raw
end

#tokensObject (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 = embedding.zip(other).sum { |a, b| a * b }
  mag_a = Math.sqrt(embedding.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