Class: RubyLLM::Tribunal::Judges::Hallucination

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/tribunal/judges/hallucination.rb

Overview

Detects claims not supported by the provided context.

A hallucination is information that is not present in or supported by the context. This is a negative metric: "yes" (hallucination detected) = fail.

Uses claim extraction approach: identifies factual claims, verifies each against context, flags unsupported or contradicted claims.

Class Method Summary collapse

Class Method Details

.judge_nameObject



15
16
17
# File 'lib/ruby_llm/tribunal/judges/hallucination.rb', line 15

def judge_name
  :hallucination
end

.negative_metric?Boolean

Returns:



19
20
21
# File 'lib/ruby_llm/tribunal/judges/hallucination.rb', line 19

def negative_metric?
  true
end

.prompt(test_case, _opts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_llm/tribunal/judges/hallucination.rb', line 29

def prompt(test_case, _opts)
  context = format_context(test_case.context)

  <<~PROMPT
    You are evaluating whether an LLM output contains hallucinations.
    A hallucination is a factual claim that cannot be verified from the provided context.

    ## Context
    #{context}

    ## Question
    #{test_case.input}

    ## Output to Evaluate
    #{test_case.actual_output}

    ## Evaluation Process
    1. Extract each factual claim from the output (skip opinions, hedged statements)
    2. For each claim, check if it can be inferred from the context
    3. Identify any claims that are unsupported or contradict the context

    ## Hallucination Types
    - **Fabrication**: Inventing facts not present in context (e.g., dates, names, numbers)
    - **Contradiction**: Stating something that conflicts with the context
    - **Extrapolation**: Drawing conclusions the context doesn't support
    - **Conflation**: Mixing up entities or attributes from the context

    ## NOT Hallucinations
    - Paraphrasing or summarizing context accurately
    - Common knowledge that doesn't conflict with context
    - Hedged language ("might be", "possibly", "it seems")
    - Logical inferences clearly supported by context

    ## Response Format
    Respond with JSON:
    - verdict: "yes" if any hallucination detected, "no" if all claims are supported
    - reason: List each hallucinated claim and explain why it's unsupported
    - score: 0.0 (no hallucination) to 1.0 (severe/multiple hallucinations)
  PROMPT
end

.validate(test_case) ⇒ Object



23
24
25
26
27
# File 'lib/ruby_llm/tribunal/judges/hallucination.rb', line 23

def validate(test_case)
  return unless test_case.context.nil? || test_case.context.empty?

  'Hallucination assertion requires context to be provided'
end