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.



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

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

Instance Method Details

#failure_messageObject



132
133
134
# File 'lib/rubric_llm/rspec.rb', line 132

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

#failure_message_when_negatedObject



136
137
138
# File 'lib/rubric_llm/rspec.rb', line 136

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

#matches?(answer) ⇒ Boolean

Fail closed: a missing score is not evidence of a hallucination.

Returns:

  • (Boolean)


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

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