This is the code used to conduct the experiments on LLM hallucination mitigation for my bachelor's thesis.
Python
0
6 commits
updated Dec 21, 2025
This repository contains the code and experiments conducted for my Bachelor's thesis on mitigating hallucinations in Large Language Models (LLMs). The project investigates whether iterative prompting strategies (such as Chain-of-Verification and Contrastive Chain-of-Thought) can reduce hallucination rates compared to standard prompting methods.
The experiments use the TruthfulQA dataset to evaluate model performance.
.
├── experiments.py # Main script for running generation experiments
├── run_script.sh # Bash script to automate experiments across multiple models
├── README.md # Project documentation
└── gemma/ # Folder containing evaluation scripts using Google's Gemma model
├── gemma.py # Script for evaluating generated answers using Gemma as a judge
└── run_script.sh # Bash script to run the evaluation process
Clone the repository:
git clone <repository-url>
cd bachelor_thesis-LLM_Hallucination_mitigation
Install dependencies: It is recommended to use a virtual environment (Conda or venv).
pip install torch transformers pandas datasets tqdm psutil simple_generation accelerate
experiments.py)The core experiment logic is in experiments.py. It loads a specified model, iterates through the TruthfulQA dataset, and generates responses using defined prompting strategies (Plain, CoVe, Contrastive CoT, etc.).
To run the experiments:
You can use the provided run_script.sh which is configured to run experiments for multiple models sequentially.
bash run_script.sh
Or run experiments.py directly:
python experiments.py \
--model_id "mistralai/Mistral-7B-Instruct-v0.3" \
--output_dir "./outputs" \
--log_file "./logs/generation.log" \
--max_tokens 600
Arguments:
--model_id: HuggingFace model ID (required).--output_dir: Directory to save results (JSON and CSV).--log_file: Path to the log file.--max_tokens: Maximum new tokens to generate.--temperature, --top_k, --top_p: Generation parameters.--large_model_batch_size, --small_model_batch_size: Batch sizes for inference.gemma/)After generating responses, the gemma/ directory contains scripts to evaluate the hallucination rate of the model outputs using a "LLM-as-a-Judge" approach (specifically using google/t5gemma-9b-9b-ul2-it or similar).
To run the evaluation:
gemma directory or run the script from the root if paths are adjusted.gemma/run_script.sh to automate evaluation for the generated output files.cd gemma
bash run_script.sh
Or run gemma.py directly:
python gemma/gemma.py \
--data_file_path "../outputs/results_model_name.json" \
--eval_file_path "../evaluation/human_annotations.json" \
--output_file_path "../evaluation/results_evaluated.json"
Arguments:
--data_file_path: Path to the JSON output from experiments.py.--eval_file_path: Path to human annotations (used for few-shot examples in the judge prompt).--output_file_path: Where to save the evaluated scores.If you wish to build upon this work, here are some useful tips:
Modify the PROMPT_STRATEGIES dictionary in experiments.py.
prompt (and optionally current_response for iterative methods).PROMPT_STRATEGIES map.iterative_prompting_method handles your new strategy key.Update the MODELS array in run_script.sh with new HuggingFace model IDs. Ensure your hardware has enough VRAM to load them.
Currently, experiments.py loads domenicrosati/TruthfulQA.
To use a different dataset:
load_dataset(...) in experiments.py.prompts, correct_answers, etc., variables to match the column names of your new dataset.The scripts generate results in both CSV and JSON formats containing:
Author: Maximilian Rauer
This is the code used to conduct the experiments on LLM hallucination mitigation for my bachelor's thesis.
Python
0
6 commits
updated Dec 21, 2025
This repository contains the code and experiments conducted for my Bachelor's thesis on mitigating hallucinations in Large Language Models (LLMs). The project investigates whether iterative prompting strategies (such as Chain-of-Verification and Contrastive Chain-of-Thought) can reduce hallucination rates compared to standard prompting methods.
The experiments use the TruthfulQA dataset to evaluate model performance.
.
├── experiments.py # Main script for running generation experiments
├── run_script.sh # Bash script to automate experiments across multiple models
├── README.md # Project documentation
└── gemma/ # Folder containing evaluation scripts using Google's Gemma model
├── gemma.py # Script for evaluating generated answers using Gemma as a judge
└── run_script.sh # Bash script to run the evaluation process
Clone the repository:
git clone <repository-url>
cd bachelor_thesis-LLM_Hallucination_mitigation
Install dependencies: It is recommended to use a virtual environment (Conda or venv).
pip install torch transformers pandas datasets tqdm psutil simple_generation accelerate
experiments.py)The core experiment logic is in experiments.py. It loads a specified model, iterates through the TruthfulQA dataset, and generates responses using defined prompting strategies (Plain, CoVe, Contrastive CoT, etc.).
To run the experiments:
You can use the provided run_script.sh which is configured to run experiments for multiple models sequentially.
bash run_script.sh
Or run experiments.py directly:
python experiments.py \
--model_id "mistralai/Mistral-7B-Instruct-v0.3" \
--output_dir "./outputs" \
--log_file "./logs/generation.log" \
--max_tokens 600
Arguments:
--model_id: HuggingFace model ID (required).--output_dir: Directory to save results (JSON and CSV).--log_file: Path to the log file.--max_tokens: Maximum new tokens to generate.--temperature, --top_k, --top_p: Generation parameters.--large_model_batch_size, --small_model_batch_size: Batch sizes for inference.gemma/)After generating responses, the gemma/ directory contains scripts to evaluate the hallucination rate of the model outputs using a "LLM-as-a-Judge" approach (specifically using google/t5gemma-9b-9b-ul2-it or similar).
To run the evaluation:
gemma directory or run the script from the root if paths are adjusted.gemma/run_script.sh to automate evaluation for the generated output files.cd gemma
bash run_script.sh
Or run gemma.py directly:
python gemma/gemma.py \
--data_file_path "../outputs/results_model_name.json" \
--eval_file_path "../evaluation/human_annotations.json" \
--output_file_path "../evaluation/results_evaluated.json"
Arguments:
--data_file_path: Path to the JSON output from experiments.py.--eval_file_path: Path to human annotations (used for few-shot examples in the judge prompt).--output_file_path: Where to save the evaluated scores.If you wish to build upon this work, here are some useful tips:
Modify the PROMPT_STRATEGIES dictionary in experiments.py.
prompt (and optionally current_response for iterative methods).PROMPT_STRATEGIES map.iterative_prompting_method handles your new strategy key.Update the MODELS array in run_script.sh with new HuggingFace model IDs. Ensure your hardware has enough VRAM to load them.
Currently, experiments.py loads domenicrosati/TruthfulQA.
To use a different dataset:
load_dataset(...) in experiments.py.prompts, correct_answers, etc., variables to match the column names of your new dataset.The scripts generate results in both CSV and JSON formats containing:
Author: Maximilian Rauer