0Pricing
LLM Apps in Production (RAG + Vector DB + Caching) · 课时

检测并衡量幻觉

学习实用技术,检测 RAG 系统何时生成了检索上下文不支持的事实,并将幻觉率作为评估的一部分进行量化。

检测并衡量幻觉 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What Is a RAG Hallucination?

A hallucination is an answer that is fluent and confident but not supported by the retrieved context. In RAG, the cure is grounding: every claim should trace back to a source.

Measuring hallucination rate is essential to trust your system.

Faithfulness vs Correctness

Two different things to measure:

  • Faithfulness — is the answer supported by the provided context?
  • Correctness — is the answer factually true in the real world?

A RAG answer can be correct but unfaithful (right by luck) or faithful but wrong (the source was wrong).

Claim Decomposition

To check faithfulness, break the answer into atomic claims, then verify each against the context.

answer = 'Paris is the capital of France and has 5 million people.'
claims = [c.strip() for c in answer.replace(' and ', '. ').split('.') if c.strip()]
for c in claims:
    print('CLAIM:', c)

Context Entailment

For each claim, ask: does the retrieved context entail it? An NLI model or an LLM judge labels each claim as supported, contradicted, or not-mentioned.

  • Supported = grounded
  • Not-mentioned = potential hallucination
  • Contradicted = definite error

LLM-as-Judge for Faithfulness

A common pattern: prompt a strong model with the context, the answer, and ask it to score whether the answer is fully supported. Calibrate the judge against human labels.

Computing Hallucination Rate

Hallucination rate = fraction of claims (or answers) that are unsupported.

labels = ['supported', 'supported', 'not_mentioned', 'contradicted']
bad = sum(1 for x in labels if x != 'supported')
rate = bad / len(labels)
print('Hallucination rate:', round(rate, 2))

Citation Coverage

If your system outputs citations, you can measure citation coverage: the share of sentences that point to a retrieved chunk that actually supports them. Low coverage signals hallucination risk.

Detecting Missing Context

Many hallucinations happen because retrieval failed and the model filled the gap. Track cases where the context lacks the answer but the model still answered confidently instead of saying 'I do not know'.

A Simple Faithfulness Score

Aggregate per-claim labels into a single score per answer.

def faithfulness(labels):
    return sum(1 for x in labels if x == 'supported') / len(labels)

print(faithfulness(['supported', 'supported', 'not_mentioned']))

Reducing Hallucinations

Once measured, reduce hallucinations by:

  • Improving retrieval recall
  • Instructing the model to abstain when unsupported
  • Requiring inline citations
  • Post-hoc filtering of unsupported claims

Tracking Over Time

Add hallucination rate to your regular eval runs. Watch it on every prompt or model change so a regression is caught before it reaches users.

Quick Check

Test your understanding of faithfulness.

Recap

You learned to detect hallucinations by separating faithfulness from correctness, decomposing answers into claims, checking entailment against context with an LLM judge, and computing a hallucination rate. Track it over time and reduce it with better retrieval, abstention, and citations.

常见问题解答

「检测并衡量幻觉」课时是免费的吗?

是的 — 「检测并衡量幻觉」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

「检测并衡量幻觉」这节课中我会学到什么?

学习实用技术,检测 RAG 系统何时生成了检索上下文不支持的事实,并将幻觉率作为评估的一部分进行量化。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「检测并衡量幻觉」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?

能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. RAG 性能的关键指标
  2. 开发评估基准
  3. A/B 测试与用户反馈闭环
  4. 检测并衡量幻觉
← 返回 LLM Apps in Production (RAG + Vector DB + Caching)