Class: RSpec::LLM::Matchers::BeSemanticallySimilarTo
- Inherits:
-
Object
- Object
- RSpec::LLM::Matchers::BeSemanticallySimilarTo
- Defined in:
- lib/rspec/llm/matchers/be_semantically_similar_to.rb
Overview
Embeds the actual and expected texts via the configured embedder and asserts cosine similarity >= threshold (default from configuration, overridable with .within(0.9)).
Instance Method Summary collapse
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(expected) ⇒ BeSemanticallySimilarTo
constructor
A new instance of BeSemanticallySimilarTo.
- #matches?(actual) ⇒ Boolean
- #within(threshold) ⇒ Object
Constructor Details
#initialize(expected) ⇒ BeSemanticallySimilarTo
Returns a new instance of BeSemanticallySimilarTo.
10 11 12 13 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 10 def initialize(expected) @expected = expected @threshold = nil end |
Instance Method Details
#description ⇒ Object
28 29 30 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 28 def description "be semantically similar to #{@expected.inspect} (>= #{threshold_value})" end |
#failure_message ⇒ Object
32 33 34 35 36 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 32 def "expected response to be semantically similar to expected text " \ "(similarity #{format_similarity} < threshold #{threshold_value}).\n" \ "Expected: #{@expected.inspect}\nActual: #{@actual.inspect}" end |
#failure_message_when_negated ⇒ Object
38 39 40 41 42 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 38 def "expected response NOT to be semantically similar to expected text " \ "(similarity #{format_similarity} >= threshold #{threshold_value}).\n" \ "Expected: #{@expected.inspect}\nActual: #{@actual.inspect}" end |
#matches?(actual) ⇒ Boolean
20 21 22 23 24 25 26 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 20 def matches?(actual) @actual = actual.to_s @actual_vec = .call(@actual) @expected_vec = .call(@expected.to_s) @similarity = cosine(@actual_vec, @expected_vec) @similarity >= threshold_value end |
#within(threshold) ⇒ Object
15 16 17 18 |
# File 'lib/rspec/llm/matchers/be_semantically_similar_to.rb', line 15 def within(threshold) @threshold = threshold self end |