maximilianrr/bachelor_thesis-LLM_Hallucination_mitigation

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

See the code

README

Bachelor Thesis: Hallucination Mitigation in LLMs through Iterative Prompting

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.

📂 Repository Structure

.
├── 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

Getting Started

Prerequisites

  • Python 3.8+
  • CUDA-enabled GPU (Recommended for running LLMs efficiently)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd bachelor_thesis-LLM_Hallucination_mitigation
    
  2. Install dependencies: It is recommended to use a virtual environment (Conda or venv).

    pip install torch transformers pandas datasets tqdm psutil simple_generation accelerate
    

Usage

1. Running Experiments (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.

2. Evaluating Results (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:

  1. Navigate to the gemma directory or run the script from the root if paths are adjusted.
  2. Use the 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.

💡 Extending the Code

If you wish to build upon this work, here are some useful tips:

Adding New Prompting Strategies

Modify the PROMPT_STRATEGIES dictionary in experiments.py.

  1. Define a new function that takes a prompt (and optionally current_response for iterative methods).
  2. Add it to the PROMPT_STRATEGIES map.
  3. If it's an iterative method, ensure the logic inside iterative_prompting_method handles your new strategy key.

Using Different Models

Update the MODELS array in run_script.sh with new HuggingFace model IDs. Ensure your hardware has enough VRAM to load them.

Changing the Dataset

Currently, experiments.py loads domenicrosati/TruthfulQA. To use a different dataset:

  1. Change load_dataset(...) in experiments.py.
  2. Update the prompts, correct_answers, etc., variables to match the column names of your new dataset.

Output Format

The scripts generate results in both CSV and JSON formats containing:

  • Prompt: The input question.
  • Strategy: The prompting strategy used.
  • Answer: The model's generated response.
  • Model ID: The name of the model.
  • Time/Memory Stats: Performance metrics for the generation.

Author: Maximilian Rauer

maximilianrr/bachelor_thesis-LLM_Hallucination_mitigation

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

See the code

README

Bachelor Thesis: Hallucination Mitigation in LLMs through Iterative Prompting

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.

📂 Repository Structure

.
├── 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

Getting Started

Prerequisites

  • Python 3.8+
  • CUDA-enabled GPU (Recommended for running LLMs efficiently)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd bachelor_thesis-LLM_Hallucination_mitigation
    
  2. Install dependencies: It is recommended to use a virtual environment (Conda or venv).

    pip install torch transformers pandas datasets tqdm psutil simple_generation accelerate
    

Usage

1. Running Experiments (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.

2. Evaluating Results (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:

  1. Navigate to the gemma directory or run the script from the root if paths are adjusted.
  2. Use the 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.

💡 Extending the Code

If you wish to build upon this work, here are some useful tips:

Adding New Prompting Strategies

Modify the PROMPT_STRATEGIES dictionary in experiments.py.

  1. Define a new function that takes a prompt (and optionally current_response for iterative methods).
  2. Add it to the PROMPT_STRATEGIES map.
  3. If it's an iterative method, ensure the logic inside iterative_prompting_method handles your new strategy key.

Using Different Models

Update the MODELS array in run_script.sh with new HuggingFace model IDs. Ensure your hardware has enough VRAM to load them.

Changing the Dataset

Currently, experiments.py loads domenicrosati/TruthfulQA. To use a different dataset:

  1. Change load_dataset(...) in experiments.py.
  2. Update the prompts, correct_answers, etc., variables to match the column names of your new dataset.

Output Format

The scripts generate results in both CSV and JSON formats containing:

  • Prompt: The input question.
  • Strategy: The prompting strategy used.
  • Answer: The model's generated response.
  • Model ID: The name of the model.
  • Time/Memory Stats: Performance metrics for the generation.

Author: Maximilian Rauer