Hallucination幻觉

模型非常自信地输出了错误信息——编造论文、虚构条文、捏造数据,而且语气毫无破绽

幻觉(Hallucination)指模型生成了流畅、自信、格式完整,但事实上错误或根本不存在的内容:编造的论文标题、虚构的法律条文、拼凑的 API 参数、看起来很像但对不上的数字。

最麻烦的地方不在于它会错,而在于它错的时候和对的时候看起来一模一样。人类说谎或不确定时通常有语气痕迹,模型没有。

它为什么必然发生

幻觉不是 Bug,是生成机制的直接结果。模型做的是"预测下一个 Token 最可能是什么",而"最可能"和"真实"是两回事:

  • 你问一篇不存在的论文,"某某某,2021,ICLR"这种模式在训练数据里出现过无数次,模型照着模式生成一个,概率上非常合理,事实上完全不存在。
  • 训练时的优化目标里没有"不知道就说不知道"这一项,模型默认倾向于给出一个完整答案。
  • 后训练中如果偏好数据奖励了"看起来有帮助的回答",还会进一步强化编造倾向。

所以只要模型还在做自回归生成,幻觉就不能被彻底消灭,只能被压低和被兜住。

几种典型形态

  • 事实幻觉:人物、时间、数字、事件错了。
  • 来源幻觉:引用了不存在的文献、链接、条款编号。这类危害最大,因为带出处的内容天然更可信。
  • 指令幻觉:虚构出并不存在的 API、参数、配置项——写代码时最常见。
  • 忠实性幻觉:给了材料,但输出的内容和材料对不上。RAG 系统里主要防的是这一类。

能真正降低幻觉的做法

按性价比排序:

  • 接检索(RAG)并强制引用。让模型基于给定材料作答,并标注每句话的出处,用户能点开核对。这是目前最有效的一招。
  • 让它先想再答。思维链和推理模型会显著降低推理类任务的错误率。
  • 给它拒答的出口。在 Prompt 里明确写"材料中没有就回答不知道",并在示例中演示一次拒答。不给出口,模型只能硬编。
  • 降低采样随机性。事实类任务把温度调低。
  • 交叉验证。关键结论用第二次调用或第二个模型复核,不一致就标记出来。
  • 在产品层设计不确定性的表达。展示置信度、标出待核实、把高风险结论交给人确认。

常见误解

"上了 RAG 就没幻觉了"——降低很多,但模型仍会曲解材料,或在材料没覆盖处自行补全。

"模型说得越确定越可靠"——正好相反,语气的确定性和事实的正确性几乎无关。

"让模型自己说有没有把握就行"——模型的自我评估同样是生成出来的,不可直接当作置信度使用。

英文原文解释(Dictionary of AI Coding)

Confidently-wrong model output. Two flavors with different causes and fixes:

FlavorWhat goes wrongCauseFix
_Factuality_Invented or wrong facts about the world — a function that doesn't exist, a wrong API signature, a fake citationParametric knowledge gaps, often past the knowledge cutoffLoad the right contextual knowledge
_Faithfulness_Output drifts from the contextual knowledge that's loaded, the user's instructions, or the model's own prior reasoningAttention degradation; worsens in the dumb zoneClear or compact

Next-token prediction produces fluent output whether or not the underlying fact is real — the model has no internal signal that it doesn't know something, so an invented method arrives in the same assured register as a correct one. Hallucinated code is plausible by construction: it's what the API _would_ look like if it existed, which is exactly what makes it slip past a skim-level review and fail only when run.

You need to know which flavor you're looking at, because the fix for one makes the other worse. Factuality means missing knowledge: the fix is adding context — the docs, the type definitions, the file. Faithfulness means the knowledge is present but losing the competition for attention: the fix is removing context. Misdiagnose faithfulness as factuality and you paste in more docs, which grows the context and makes the drift worse. When the agent gets something wrong, check whether the correct information was already in context before deciding which problem you have.

什么时候会用到

任何要把 AI 输出给到真实用户的场景,都必须先回答「幻觉了怎么办」这个问题。

例句

  • 它引的这三篇论文我一篇都搜不到,典型的来源幻觉。
  • 写代码时它编 API 参数编得特别顺,一跑就报没这个字段。
  • Prompt 里要给拒答的出口,不然材料里没有它就自己编一个。

别混淆

别把幻觉当成「模型不够大」的问题。模型越大幻觉的表述越流畅、越难识别,单纯换更大的模型不解决问题。

相关词