Team 8: Zayaan Ali Β· Aaqib Pangarkar Β· Ridhwan Ahamed Β· Mariah Shania
This project tackles hallucination detection in Retrieval-Augmented Generation (RAG) systems β a critical open problem in NLP where language models generate fluent but factually incorrect or unsupported text.
We propose Track A: Dynamic Uncertainty-Aware Attribution, a training-free, token-level framework that identifies hallucinated spans in generated answers by measuring how a language model's probability distribution shifts when context is (and isn't) provided. Our method uses four uncertainty signals derived from two GPT-2 forward passes to produce a composite hallucination score per token.
No fine-tuning required. No labelled training data needed. Just two forward passes.
The core insight: if a generated token is grounded in context, the model's confidence should increase when context is provided. If it is hallucinated, the context will have little to no effect.
We compute four token-level signals:
| Signal | Symbol | Description |
|---|---|---|
| Information Gain | ΞH | Entropy drop when context is added |
| KL Divergence | KL | Distribution shift between context-conditioned and unconditional passes |
| Confidence Drop | Ξc | Change in max-token probability |
| Semantic Entropy | SE | Sequence-level average entropy (approximated per-token) |
These are combined into a weighted composite score (weights learned via logistic regression on RAGTruth):
composite = 0.15Β·IG + 0.55Β·KL + 0.30Β·ΞC
A token is labelled HALLUCINATED if its composite score falls below threshold Ο = 0.50.
| Method | AUROC |
|---|---|
| Entropy Only | 0.5338 |
| SelfCheckGPT | 0.6805 |
| Our Composite | 0.7243 |
| ReDeEP (SOTA) | 0.82 |
| LUMINA (SOTA) | 0.87 |
| Signal | RAGTruth AUROC | HaluEval AUROC |
|---|---|---|
| Information Gain alone | 0.3482 | 0.4908 |
| KL Divergence alone | 0.6974 | 0.9060 |
| Full Composite | 0.7243 | 0.9249 |
| Hallucination Type | Count | Composite AUROC | Best Signal |
|---|---|---|---|
| Contradictory | 116 | 0.7841 | KL Divergence |
| Unsupported | 134 | 0.6902 | KL Divergence |
| Source Model | AUROC |
|---|---|
| Llama-2-70B-chat | 0.8732 |
| GPT-4-0613 | 0.7877 |
| Llama-2-13B-chat | 0.7794 |
| Mistral-7B-Instruct | 0.7679 |
| GPT-3.5-Turbo | 0.6465 |
| Llama-2-7B-chat | 0.5346 |
NLP_Assignment/
β
βββ π Notebooks (run in order)
β βββ 01_data_exploration.ipynb # Dataset loading, EDA, label distribution analysis
β βββ 02_core_pipeline.ipynb # Core feature engineering & uncertainty signal computation
β βββ 03_baselines.ipynb # Baseline models (Entropy Only, SelfCheckGPT)
β βββ 04_experiments.ipynb # All 8 experiments: ablations, breakdowns, SOTA comparison
β βββ 05_demo .ipynb # Interactive demo on custom inputs
β
βββ π Scripts
β βββ demo.py # CLI demo β run Track A on any passage/context pair
β
βββ π data/
β βββ halueval.csv # HaluEval benchmark (hallucination evaluation)
β βββ ragtruth_test_subset.csv # RAGTruth test subset (250 samples)
β βββ ragtruth_test_subset_750.csv # RAGTruth test subset (750 samples)
β βββ ... # (ragtruth.csv excluded β see Data Notes below)
β
βββ π results/
β βββ experiment4_results.csv # Signal ablation results
β βββ experiment5_results.csv # Hallucination-type breakdown
β βββ experiment6_results.csv # Per-source-model AUROC
β βββ experiment8_sota.csv # SOTA comparison table
β βββ ... # Merged result files & train metrics
β
βββ π outputs/
β βββ experiment3_temporal.png # Temporal analysis plot
β βββ experiment3_temporal_real.png # Real temporal analysis plot
β
βββ π Deliverables
β βββ NLP_Project_Report (1).pdf # Final project report
β βββ NLP_Report_Team_8.docx # Report (Word format)
β βββ NLP_PPT_TEAM_8.pptx # Final presentation slides
β βββ Assignment_NLP_CS_F429.pdf # Original assignment specification
β βββ Midsem_Assignment_NLP_CS_F429 (1).pdf # Mid-semester assignment
β
βββ .gitignore
βββ README.md
- Python 3.8+
- pip
git clone https://github.com/aaqibnp971/NLP_Assignment.git
cd NLP_Assignmentpython -m venv nlp_env
# Windows
nlp_env\Scripts\activate
# macOS/Linux
source nlp_env/bin/activatepip install torch transformers numpy scipy scikit-learn
pip install datasets pandas jupyterlab matplotlib seabornTest the hallucination detector on any passage/context pair:
python demo.py \
--passage "Anne Frank was born in Frankfurt in 1929 and died in 1945." \
--context "Anne Frank was a Jewish diarist born in Frankfurt am Main on 12 June 1929."Example output:
TOKEN IG KL CONF_DROP SE COMPOSITE LABEL
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
' Anne' 0.1234 0.4521 0.3012 0.8734 0.3891 HALLUCINATED
' Frank' 0.9821 0.9234 0.8921 0.8734 0.9201 faithful
...
Sequence-level composite score : 0.6123
Sequence-level prediction : FAITHFUL (threshold Ο = 0.50)
jupyter labOpen the notebooks in order: 01 β 02 β 03 β 04 β 05.
| Dataset | Description | Source |
|---|---|---|
| RAGTruth | ~18K passage-level hallucination annotations from 6 LLMs across 3 RAG tasks | Hugging Face β wandb/RAGTruth-processed |
| HaluEval | Question-answer hallucination pairs for cross-dataset generalisation | HaluEval Benchmark |
ragtruth.csv(~115 MB) is excluded from this repo due to GitHub's 100 MB file size limit.01_data_exploration.ipynbcontains the code to pull and cache this dataset directly from the Hugging Face Hub.- Smaller subsets (
ragtruth_test_subset.csv,ragtruth_test_subset_750.csv) are included for quick experimentation.
The 04_experiments.ipynb notebook contains 8 numbered experiments:
| # | Name | Description |
|---|---|---|
| 1 | Data Exploration | Label distribution, source model stats, task breakdown |
| 2 | Feature Engineering | Token-level signal computation on RAGTruth |
| 3 | Temporal Analysis | Hallucination position within answer sequences |
| 4 | Signal Ablation | Ablating each signal individually vs. full composite |
| 5 | Hallucination Type Breakdown | Contradictory vs. unsupported hallucinations |
| 6 | Per-Model Analysis | AUROC breakdown by source LLM |
| 7 | Cross-Dataset Generalisation | RAGTruth β HaluEval transfer |
| 8 | SOTA Comparison | Benchmarking against SelfCheckGPT, ReDeEP, LUMINA |
- Base Model: GPT-2 (for uncertainty signal extraction β no fine-tuning)
- Composite weights: Learned by Logistic Regression on RAGTruth training split
- Information Gain: 0.15
- KL Divergence: 0.55
- Confidence Drop: 0.30
- (Semantic Entropy excluded β negative weight on training set)
- Decision threshold: Ο = 0.50 (tuned on validation set)
- Evaluation metric: AUROC (Area Under ROC Curve)
- Hardware: CPU-compatible (no GPU required for demo)
| Package | Purpose |
|---|---|
torch |
GPT-2 inference |
transformers |
GPT-2 model & tokenizer |
numpy / scipy |
Signal computation |
scikit-learn |
Logistic Regression, AUROC evaluation |
datasets |
Hugging Face dataset loading |
pandas |
Data manipulation |
matplotlib / seaborn |
Visualisations |
jupyterlab |
Notebook environment |
| Name | Role |
|---|---|
| Zayaan Ali | Experiments, report writing |
| Aaqib Pangarkar | Core pipeline, demo implementation |
| Ridhwan Ahamed | Baselines, data analysis |
| Mariah Shania | Feature engineering, evaluation |
Course: CS F429 β Natural Language Processing Institution: BITS Pilani, Dubai Campus
- Zhang, Y., et al. "RAGTruth: A Hallucination Corpus for Developing Trustworthy Retrieval-Augmented Language Models." ACL 2024.
- Li, J., et al. "HaluEval: A Large-Scale Hallucination Evaluation Benchmark for Large Language Models." EMNLP 2023.
- Manakul, P., et al. "SelfCheckGPT: Zero-Resource Black-Box Hallucination Detection for Generative Large Language Models." EMNLP 2023.
- Farquhar, S., et al. "Detecting Hallucinations in Large Language Models Using Semantic Entropy." Nature 2024.
Built for CS F429 Β· BITS Pilani Dubai Campus Β· 2025