This repository contains the source code for the IJCNN 2025 paper HalluShift: Measuring Distribution Shifts towards Hallucination Detection in LLMs by Sharanya Dasgupta, Sujoy Nath, Arkaprabha Basu, Pourya Shamsolmoali, and Swagatam Das.
- 📦 Setup Environment
- Install
Python 3.10.12and the necessary packages fromrequirements.txt. - For easily managing different python versions, we recommend using conda.
- Create a new environment in conda and install necessary python packages:
conda create -n hallushift python=3.10.12 -y conda activate hallushift pip install -r requirements.txt
- Install
- 🔑 Create HF Access Token
- Login to
huggingfaceor create an account if you don't have already. - From the settings create a new access token with WRITE access.
- Open the
hal_detection.pyand paste your access token atline 15, hf_token = "<INPUT_YOUR_HF_ACCESS_TOKEN>"
- Login to
-
📁 Setup Necessary Directoriess
Create a folder to save:- LLM-generated answers and Ground truth labels for model-generated content
- Features for training classifiers
mkdir results
Create another folder, where you need to place BleuRT models in the next step. And llm models will be automatically saved while running the script.
mkdir models -
✅ Ground Truth Evaluation
Since generated answers lack explicit ground truth, we use BleuRT to evaluate truthfulness.- To install BleuRT run:
pip install --upgrade pip # ensures that pip is current git clone https://github.com/google-research/bleurt.git cd bleurt pip install .- We are using 12-layer distilled model for faster inference, which is ~3.5X smaller.
- Download the model and save it in the
./modelsfolder:
wget https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip unzip BLEURT-20-D12.zip mv BLEURT-20-D12 models/. # Move the bleurt model folder to models directory- If you want to use any different model please refer to BleuRT repository.
-
🔍 Hallucination Detection for TruthfulQA
To perform hallucination detection on the TruthfulQA dataset run the following command:python hal_detection.py --dataset_name truthfulqa --model_name llama2_7B
dataset_name: Choose fromtruthfulqa,triviaqa,tydiqa,coqa,haluevalqa,haluevaldia,haluevalsum.model_name: Choose fromllama2_7B,llama3_8B,opt6.7B,vicuna_7BorQwen2.5_7B.- To use a different LLM, add its corresponding path to the
MODELS_NAMESdictionary inhal_detection.py. - To use a different dataset, update the
load_dataset_by_namefunction inhal_detection.py. Ensure that it returns adatasets.Datasetobject.
Note: If you encounter memory errors, consider reducing the number of workers using the
--num_workersparameter. For example:python hal_detection.py --dataset_name truthfulqa --model_name llama2_7B --num_workers 1
Refer to Section IV of the paper for implementation details.
- 🎮 Demo
To quickly evaluate the pre-trained model on the TruthfulQA dataset (which has already been processed and inferred using the LLaMA-2 7B model), run the following command:
The pre-trained model and the processed dataset are provided in the
cd demo python demo.pydemofolder for easy evaluation.
