Class: RubricLLM::RSpecMatchers::HallucinationMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/rubric_llm/rspec.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#config, #result, #threshold

Instance Method Summary collapse

Methods inherited from BaseMatcher

#with_config, #with_threshold

Constructor Details

#initialize(context, question: nil) ⇒ HallucinationMatcher

Returns a new instance of HallucinationMatcher.



114
115
116
117
118
# File 'lib/rubric_llm/rspec.rb', line 114

def initialize(context, question: nil)
  super()
  @context = context
  @question = question
end

Instance Method Details

#failure_messageObject



125
126
127
# File 'lib/rubric_llm/rspec.rb', line 125

def failure_message
  "expected hallucination (faithfulness < #{threshold}), got #{result[:score]}"
end

#failure_message_when_negatedObject



129
130
131
# File 'lib/rubric_llm/rspec.rb', line 129

def failure_message_when_negated
  "expected no hallucination (faithfulness >= #{threshold}), got #{result[:score] || "nil"}"
end

#matches?(answer) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/rubric_llm/rspec.rb', line 120

def matches?(answer)
  score = evaluate(Metrics::Faithfulness, question: @question || "", answer:, context: @context)
  score.nil? || score < threshold
end