Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hallucination Detection and Mitigation in LLMs

This repository contains the implementation and experiments for the paper "Hallucination Detection and Mitigation in Large Language Models: A Comprehensive Study".

Overview

This project implements and evaluates state-of-the-art methods for detecting and mitigating hallucinations in Large Language Models (LLMs), including:

Detection Methods

  • SelfCheckGPT (Manakul et al., EMNLP 2023): Zero-resource black-box hallucination detection
  • FActScore (Min et al., EMNLP 2023): Fine-grained atomic evaluation of factual precision
  • Semantic Entropy (Kuhn et al., ICLR 2023): Uncertainty-based detection

Mitigation Methods

  • RAG (Lewis et al., NeurIPS 2020): Retrieval-Augmented Generation
  • CoVe (Dhuliawala et al., ACL 2024): Chain-of-Verification
  • DoLa (Chuang et al., ICLR 2024): Decoding by Contrasting Layers

Repository Structure

.
├── paper/
│   ├── main.tex              # LaTeX source for the paper
│   └── references.bib        # Bibliography
├── src/
│   ├── hallucination_methods.py  # Implementation of all methods
│   ├── evaluation.py             # Evaluation framework
│   ├── generate_figures.py       # Script to generate paper figures
│   └── generate_tables.py        # Script to generate paper tables
├── figures/                  # Generated figures
├── tables/                   # Generated tables
├── data/                     # Dataset directory (not included)
└── README.md

Installation

# Clone the repository
git clone https://github.com/anonymous/hallucination-study.git
cd hallucination-study

# Install dependencies
pip install torch transformers
pip install numpy pandas matplotlib seaborn
pip install scikit-learn scipy
pip install bert-score  # Optional, for BERTScore

# For LaTeX compilation
cd paper
pdflatex main.tex
bibtex main
pdflatex main.tex
pdflatex main.tex

Usage

Running Detection Methods

from src.hallucination_methods import SelfCheckGPT, FActScoreEvaluator
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")

# SelfCheckGPT
selfcheck = SelfCheckGPT(model, tokenizer, num_samples=10)
result = selfcheck.detect(query="Who is Albert Einstein?", 
                          response="Albert Einstein was a German physicist...")
print(f"Hallucination score: {result.passage_score}")

# FActScore
fact_eval = FActScoreEvaluator(model, tokenizer)
result = fact_eval.evaluate(text="Albert Einstein won the Nobel Prize in Physics in 1921...",
                            entity="Albert Einstein")
print(f"FActScore: {result['factscore']}")

Running Mitigation Methods

from src.hallucination_methods import ChainOfVerification, DoLaDecoding, RAGGenerator

# Chain-of-Verification
cove = ChainOfVerification(model, tokenizer)
response = cove.generate("What are the main causes of climate change?")

# DoLa
dola = DoLaDecoding(model, tokenizer, early_exit_layer=10, late_exit_layer=32)
response = dola.generate("Explain quantum mechanics.")

# RAG
retriever = SimpleRetriever(documents)  # Your document corpus
rag = RAGGenerator(model, tokenizer, retriever, top_k=3)
response = rag.generate("What is the capital of France?")

Evaluation

from src.evaluation import HallucinationEvaluator

evaluator = HallucinationEvaluator()

# Evaluate detection
results = evaluator.evaluate_detection(
    predictions=[0.2, 0.8, 0.3, 0.9],
    labels=[0, 1, 0, 1],
    method_name="SelfCheckGPT"
)
print(f"AUC-PR: {results['auc_pr']}")

# Evaluate mitigation
results = evaluator.evaluate_mitigation(
    generations=["...", "..."],
    labels=[0, 1],
    fact_scores=[0.9, 0.6],
    method_name="RAG"
)
print(f"Hallucination rate: {results['hallucination_rate']}")

Generating Figures and Tables

# Generate all figures
python src/generate_figures.py figures/

# Generate all tables
python src/generate_tables.py tables/

Datasets

The paper evaluates on the following datasets:

  • WikiBio: Wikipedia biographies for factual consistency evaluation
  • HaluEval: Large-scale hallucination evaluation benchmark
  • TruthfulQA: Truthfulness evaluation benchmark

Please download these datasets separately and place them in the data/ directory.

Results Summary

Detection Performance (AUC-PR)

Method WikiBio HaluEval-QA TruthfulQA
SelfCheckGPT-NLI 0.856 0.798 0.827
FActScore 0.891 0.834 0.862
Ensemble 0.912 0.856 0.873

Mitigation Performance

Method Hallucination Rate FActScore
Baseline 0.342 0.658
RAG 0.198 0.802
Full Pipeline 0.124 0.876

Citation

If you use this code or find our work helpful, please cite:

@article{anonymous2024hallucination,
  title={Hallucination Detection and Mitigation in Large Language Models: A Comprehensive Study},
  author={Anonymous},
  journal={arXiv preprint},
  year={2024}
}

References

  1. Manakul, P., Liusie, A., & Gales, M. J. F. (2023). SelfCheckGPT: Zero-resource black-box hallucination detection for generative large language models. EMNLP 2023.

  2. Min, S., et al. (2023). FActScore: Fine-grained atomic evaluation of factual precision in long form text generation. EMNLP 2023.

  3. Dhuliawala, S., et al. (2024). Chain-of-verification reduces hallucination in large language models. ACL 2024.

  4. Chuang, Y. S., et al. (2024). DoLa: Decoding by contrasting layers improves factuality in large language models. ICLR 2024.

  5. Lewis, P., et al. (2020). Retrieval-augmented generation for knowledge-intensive NLP tasks. NeurIPS 2020.

License

This project is released under the MIT License.

Contact

For questions or issues, please open an issue on GitHub or contact the authors.

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages