Detecting hallucinations in large language models using semantic entropy

Sebastian Farquhar, Jannik Kossen, Lorenz Kuhn, Yarin GalNat 2024

Abstract

Large Language Models (LLMs) often generate "confabulations"—plausible but arbitrary and incorrect answers. Traditional uncertainty measures fail to detect these because they focus on token-level variation rather than semantic meaning. We present "semantic entropy," an unsupervised method developed by Oxford’s OATML group that clusters model responses by meaning to measure uncertainty in the underlying idea. Evaluated across diverse tasks and models, semantic entropy significantly outperforms naive entropy and supervised baselines in flagging hallucinations, offering a robust, general-purpose tool for improving LLM reliability.

Introduction: The Token vs. Meaning Gap

Large Language Models (LLMs) are notorious for "hallucinations," specifically confabulations: errors where the model provides an arbitrary, incorrect answer that changes based on random seeds or slight phrasing variations. Detecting these is challenging due to the "Token vs. Meaning" gap. Traditional uncertainty measures, such as Shannon entropy, calculate the probability of specific words (tokens). However, in natural language, a single meaning can be expressed in many ways (e.g., "Paris" vs. "It's Paris").

A model might be certain of a fact but show high "naive" entropy because it considers multiple valid phrasings. Conversely, a model might be guessing wildly but use similar wording, resulting in misleadingly low naive entropy. To solve this, we need to measure uncertainty in the idea, not the syntax.

Conceptual overview of semantic entropy vs. naive entropy.

Methodology: Measuring Uncertainty in Meaning

The proposed Semantic Entropy method is an unsupervised approach that operates in three steps:

  1. Generation: For a given prompt, the model samples several possible answers (typically $M=10$) using a high "temperature" to encourage diversity.
  2. Clustering by Meaning: Answers are grouped into clusters based on bidirectional entailment. Using a Natural Language Inference (NLI) model, the system checks if Answer A implies Answer B and vice versa. If they do, they belong to the same semantic cluster.
  3. Entropy Calculation: Entropy is calculated over these semantic clusters rather than individual word sequences. High semantic entropy indicates the model is generating multiple, mutually exclusive meanings (e.g., "1998" vs. "1991"), signaling a likely confabulation.

For long-form text, such as biographies, the authors use "factoid decomposition." This involves breaking a paragraph into individual claims, converting them into questions, and applying semantic entropy to the answers generated for those specific questions.

Experimental Evaluation

The method was evaluated across a wide range of models (LLaMA 2, Falcon, Mistral, and GPT-4) and tasks:

  • General/Trivia QA: TriviaQA, SQuAD 1.1, and NQ-Open.
  • Biomedical QA: BioASQ.
  • Mathematical Reasoning: SVAMP.
  • FactualBio: A new dataset of 150 factual claims from GPT-4 generated biographies, manually labeled for truth.

Key Results and Performance

Semantic entropy consistently outperformed existing methods. Averaged across 30 combinations of tasks and models, it achieved an AUROC of 0.790, significantly higher than naive entropy (0.691) and supervised baselines like "P(True)" (0.698).

Performance comparison (AUROC and AURAC) across various LLMs and baselines.

A key advantage is Out-of-Distribution (OOD) robustness. While supervised methods (like training regressors on model embeddings) often fail when the topic changes, semantic entropy remains stable across different datasets. Furthermore, by using semantic entropy to "refuse" to answer high-uncertainty questions, the overall accuracy of the system (measured by AURAC) improved significantly.

Discussion and Limitations

Semantic entropy demonstrates that LLMs often "know" when they are guessing, but this signal is typically buried in linguistic noise. By moving to the semantic level, we create a metric robust to the redundancy of human language.

However, some limitations remain:

  • The "Confident Lie": The method only detects confabulations (arbitrary guesses). It cannot detect systematic errors where a model is "confidently wrong" due to training on incorrect data.
  • Computational Overhead: Generating multiple samples and running $O(M^2)$ entailment checks is more expensive than a single generation.
  • Clustering Sensitivity: The accuracy depends on the NLI judge. A judge that is too sensitive might inflate uncertainty scores by distinguishing between synonymous but slightly different answers.

Conclusion

Semantic entropy provides a powerful, unsupervised framework for detecting hallucinations in LLMs. By focusing on the consistency of meaning rather than word choice, it offers a more reliable signal for when a model is guessing, paving the way for more trustworthy AI systems.

Summary updated

BibTeX
@article{farquhar2024detecting,
  author = {Sebastian Farquhar and Jannik Kossen and Lorenz Kuhn and Yarin Gal},
  title = {{Detecting hallucinations in large language models using semantic entropy}},
  journal = {Nat.},
  year = {2024},
  doi = {10.1038/s41586-024-07421-0},
}
Figure