← Master Index
Vol. 19 Module 19.1 Lecture

Hallucination Tests

Metrics & Benchmarking

How This Lesson Fits the Module & Volume

Perplexity, BLEU, and token usage can all look healthy while the model invents a citation. Hallucination tests are Vol. 19’s dedicated quality checks for unsupported or contradictory claims.

They operationalize Vol. 11.4 hallucination (intrinsic vs extrinsic, faithfulness vs world-knowledge) and Vol. 14 RAG (RAG, pipeline, retrieval, knowledge base). Automatic n-gram metrics miss this; human evaluation still judges nuance. Next lecture covers raters; here you build testable groundedness, faithfulness, and contradiction suites.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define groundedness vs RAG faithfulness vs factuality vs contradiction.
  • Build a small claim-span checklist against retrieved context.
  • Use NLI / entailment-style checks as a faithfulness heuristic.
  • Detect self-contradiction and context-contradiction automatically.
  • Separate retrieval failure from generation hallucination.
  • Know when you still need humans (and Vol. 20 safety) beyond these tests.
Definition

A hallucination test is an evaluation procedure that scores whether generated text is supported by allowed evidence (provided context, retrieved docs, or a gold fact set) and whether it contradicts that evidence or itself. Groundedness asks: is each claim backed by the given sources? RAG faithfulness is groundedness when those sources are the retriever’s chunks. Contradiction checks flag logical or textual conflicts. These tests do not prove world-truth if the corpus itself is wrong.

A Taxonomy You Can Score

Test familyQuestionTypical evidence
GroundednessIs claim \(c\) supported by context \(C\)?User-pasted doc, tool output, KB snippet
RAG faithfulnessDoes the answer stick to retrieved chunks?Top-\(k\) passages from Vol. 14 retrieval
AttributionDoes citation \(i\) actually contain the claim?Chunk IDs / URLs
Contradiction (context)Does the answer negate \(C\)?Same \(C\)
Contradiction (self)Do two sentences in the answer conflict?Answer only
Factuality (closed)Does it match a gold fact table?Curated Q/A or KB triples

Vol. 11.4 distinguished fluent but false world knowledge from unfaithful use of given context. RAG evals must split retrieval miss (gold chunk never retrieved) from generation miss (chunk present, model still invents). Otherwise you “fix hallucination” by tuning the generator when the index is the bug.

Groundedness & Faithfulness Workflow

1. Extract claims

  • Split answer into atomic statements.
  • Ignore hedges / questions if out of scope.
  • Keep numbers, names, dates.

2. Align to evidence

  • Supported / unsupported / contradictory.
  • Optional: which chunk ID.
  • Abstention (“I don’t know”) can be a pass.

3. Aggregate

  • % grounded claims
  • % contradictory answers
  • Citation precision/recall

Heuristic Checkers (Not Oracles)

NLI or LLM-as-judge entailment is a common automatic stand-in: premise = chunk, hypothesis = claim. High contradiction \(\Rightarrow\) fail; neutral \(\Rightarrow\) unsupported; entailment \(\Rightarrow\) grounded. These models err—use them for regression dashboards, then spot-check with humans.

# Deterministic starter tests + a sketch of claim/context scoring. # Swap `nli_label` for a real NLI or LLM-as-judge call in production. import re def split_claims(answer): parts = re.split(r"(?<=[.!?])\s+", answer.strip()) return [p for p in parts if p] def keyword_grounded(claim, chunks, min_hits=2): words = {w.lower() for w in re.findall(r"[A-Za-z0-9]+", claim) if len(w) > 3} if not words: return False blob = " ".join(chunks).lower() hits = sum(1 for w in words if w in blob) return hits >= min_hits def self_contradiction_markers(answer): # Crude lexical flags — real systems use NLI between sentence pairs. a = answer.lower() pairs = [("always", "never"), ("increased", "decreased"), ("is", "is not")] return any(x in a and y in a for x, y in pairs) def score_rag_item(answer, chunks, gold_must_include=None): claims = split_claims(answer) grounded = [keyword_grounded(c, chunks) for c in claims] out = { "n_claims": len(claims), "groundedness": sum(grounded) / max(1, len(claims)), "self_contradiction_flag": self_contradiction_markers(answer), "retrieval_hit": None, } if gold_must_include is not None: blob = " ".join(chunks).lower() out["retrieval_hit"] = gold_must_include.lower() in blob # If retrieval_hit is False, do not blame the generator alone. return out chunks = ["Refunds are issued within 14 days of return approval."] ans = "Refunds always arrive in 2 days. They never take more than 14 days." print(score_rag_item(ans, chunks, gold_must_include="14 days")) # faithfulness low + contradiction flag; retrieval_hit True \(\Rightarrow\) generation error

Related Lectures

LectureRole
Hallucination (11.4)Why LMs invent fluent falsehoods
RAG / retrievalEvidence channel to test against
Precision / recallCitation / claim-level aggregates
GuardrailsAbstain / cite patterns at runtime
Human evaluationNext: raters for borderline claims

Automatic tests buy

  • Regression gates on every RAG change
  • Split retrieval vs generation blame
  • Scale beyond a weekly spot-check

They miss

  • Subtle implicature and tone
  • Wrong-but-entailed paraphrases
  • Safety harms that are “faithful” to a bad doc (Vol. 20)
Common Misconception

“If we added RAG, we do not need hallucination tests.” RAG changes the evidence; models still over-generate past chunks. Second: scoring faithfulness without checking whether the gold chunk was retrieved. Third: treating LLM-as-judge as ground truth (it can hallucinate the eval). Fourth: keyword overlap \(\equiv\) support (“not 14 days” still matches “14 days”). Fifth: a 100% grounded summary that omits the only material risk—faithfulness \(\neq\) completeness.

Knowledge Check

  1. Short Answer: What does groundedness ask? Answer: Whether claims are supported by the allowed evidence/context.
  2. True/False: RAG faithfulness is groundedness against retrieved chunks. Answer: True.
  3. Multiple Choice: If the gold chunk was never retrieved, blame first: (a) retrieval, (b) BLEU, (c) TTFT. Answer: (a).
  4. Short Answer: Name two contradiction types. Answer: Context-contradiction and self-contradiction (answer vs answer).
  5. True/False: Vol. 11.4 hallucination theory is enough without eval tests. Answer: False—you still need measurable suites.
  6. Multiple Choice: NLI entailment of claim given chunk estimates: (a) faithfulness, (b) p95 latency, (c) perplexity. Answer: (a).
  7. Short Answer: Why can keyword overlap fail as a groundedness test? Answer: Negations and unrelated co-occurrence still match tokens.
  8. True/False: Faithful to a wrong knowledge base means the product is correct. Answer: False—corpus error \(\neq\) world-truth.
  9. Multiple Choice: Next lecture: (a) Human evaluation, (b) LoRA, (c) Redis. Answer: (a).
  10. Short Answer: Link one Vol. 14 lecture you test against. Answer: RAG / rag-pipeline / retrieval / knowledge-base (any of these).

Key Takeaways

  • Hallucination tests score support and contradiction, not fluency.
  • Split retrieval miss vs generation unfaithfulness in RAG (Vol. 14).
  • Claim-level groundedness + citation checks beat whole-answer BLEU.
  • Automatic NLI/LLM judges are heuristics; humans still calibrate.
  • Next: Human evaluation.
Trainer’s Guide

Lab: Build a 15-item RAG set: 5 retrieval misses, 5 faithful answers, 5 unfaithful (added numbers). Run keyword + optional NLI. Show a confusion between retrieval vs generation failures.

Whiteboard: Answer \(\to\) claims \(\to\) chunks \(\to\) {entail, contradict, neither}. Arrow to human eval when “neither” is the majority bucket.

Recap: Hallucination tests measure groundedness, RAG faithfulness, and contradictions—the Vol. 19 instrumentation for Vol. 11.4 theory and Vol. 14 RAG. Calibrate the borderline cases next with Human evaluation.