This project studies how prompt compression affects hallucination in large language models. The pipeline runs several LLMs on benchmark question-answering datasets, optionally compresses long prompts using LLMLingua-2, and measures the effect with correctness, consistency, and entailment-based metrics.
The core idea is simple: remove redundant prompt text, keep the relevant evidence, and check whether the model becomes more grounded in its answer generation.
The system:
microsoft/Phi-3-mini-4k-instructmistralai/Mistral-7B-Instruct-v0.2meta-llama/Llama-2-7b-chat-hfmeta-llama/Meta-Llama-3-8B-InstructEach dataset subset contains 1,000 samples:
gsm8k_subset.jsonlsquad_v2_subset.jsonlhotpotqa_subset.jsonltriviaqa_subset.jsonlresults/experiment_results_final.csvresults/experiment_summary.csvresults/plot_*.pngThere are also model-specific and long-context experiment files such as tiny_long_experiment_results.csv and phi3_long_experiment_results.csv.
flowchart LR
A[JSONL datasets] --> B[Dataset loader]
B --> C[Prompt builder]
C --> D{Compression enabled?}
D -- Yes --> E[LLMLingua-2 prompt compression]
D -- No --> F[Original prompt]
E --> G[LLM generation]
F --> G[LLM generation]
G --> H[Metrics layer]
H --> H1[Exact Match]
H --> H2[Keyword Match]
H --> H3[Self-Consistency]
H --> H4[NLI Support]
H4 --> I[Hallucination flag]
H1 --> J[CSV results]
H2 --> J
H3 --> J
H4 --> J
I --> J
The experiment compares uncompressed and compressed prompts for the same sample. For each input:
This allows the project to separate two questions:
sequenceDiagram
participant D as Dataset
participant R as Runner
participant C as LLMLingua-2
participant L as LLM
participant M as Metrics
participant O as CSV Output
D->>R: sample (context, question, answer)
R->>R: build prompt
R->>C: compress prompt if enabled
C-->>R: compressed prompt + token counts
R->>L: generate answer(s)
L-->>R: predicted response(s)
R->>M: compute EM / KM / SC / NLI
M-->>R: metric values + hallucination flag
R->>O: write row to results CSV
src/dataset.pyReads JSONL files and yields samples with these fields:
iddatasetcontextquestionanswersrc/models.pyLoads three model families:
roberta-large-mnli),all-MiniLM-L6-v2) for self-consistency.It also includes GPU-aware loading, optional Hugging Face token support for gated models, and fallback behavior when memory is limited.
src/compression.pyUses LLMLingua-2 to compress prompts toward a fixed target token budget. If the compressor cannot be initialized, the pipeline falls back safely to the original prompt.
src/metrics.pyComputes four metrics:
src/runner.pyCoordinates the full experiment, handles checkpoint-style resume, writes CSV rows, and produces aggregated summary statistics after execution.
Exact Match is a strict correctness score.
Interpretation:
1.0 means the prediction exactly matches the reference.0.0 means it does not.Keyword Match is a looser correctness check.
1.0 only if all remaining gold keywords appear.Interpretation:
Self-Consistency measures how similar multiple answers are to each other.
Interpretation:
NLI Support is the main hallucination proxy in this project.
Interpretation:
Compression is controlled in src/config.py:
COMPRESSION_ENABLED = TrueCOMPRESSION_THRESHOLD_TOKENS = 2000FORCE_COMPRESSION = TrueThis means the pipeline always attempts compression, and LLMLingua-2 targets a 2,000-token budget.
Important note:
Compression is most useful for long, noisy prompts where the model has too much irrelevant context to process efficiently.
The repository contains long-context experiment outputs showing that compression helps most when prompts are long and redundant.
| Dataset | Mean NLI Support |
|---|---|
| GSM8K | 0.1652 |
| HotpotQA | 0.1261 |
| SQuAD v2 | 0.4212 |
| TriviaQA | 0.0919 |
In the long-context TinyLlama run, compression behaved differently across datasets:
| Dataset | Before Compression | After Compression | Improvement |
|---|---|---|---|
| HotpotQA | 0.1183 | 0.1259 | +6.42% |
| TriviaQA | 0.1263 | 0.1037 | -17.88% |
| Overall | 0.1223 | 0.1148 | -6.12% |
Takeaway:
The project also compares several LLMs under the same pipeline. The key research pattern is:
Prompt compression helps by improving the signal-to-noise ratio:
This is why compression is most effective when prompts are long and cluttered, and least useful when prompts are already short and focused.
.
├── data/
│ ├── gsm8k_subset.jsonl
│ ├── squad_v2_subset.jsonl
│ ├── hotpotqa_subset.jsonl
│ └── triviaqa_subset.jsonl
├── results/
│ ├── experiment_results_final.csv
│ ├── experiment_summary.csv
│ ├── tiny_long_experiment_results.csv
│ ├── tiny_long_experiment_summary.csv
│ ├── phi3_long_experiment_results.csv
│ ├── phi3_long_experiment_summary.csv
│ └── plots and aggregation files
├── src/
│ ├── config.py
│ ├── dataset.py
│ ├── models.py
│ ├── compression.py
│ ├── metrics.py
│ └── runner.py
├── tests/
│ ├── test_all.py
│ ├── test_pipeline.py
│ ├── verify_setup.py
│ └── other validation tests
├── prepare_datasets.py
├── requirements.txt
├── setup.md
└── README.md
git clone -b main https://github.com/Pratyay-Patel/Reduce-Hallucinations-in-LLMS.git
cd Reduce-Hallucinations-in-LLMS
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
Create a .env file in the project root:
HF_TOKEN=your_token_here
python tests/verify_setup.py
python tests/test_all.py
python -m src.runner
python prepare_datasets.py
Key values live in src/config.py:
LLM_MODELS: models used in the experimentNLI_MODEL_NAME: roberta-large-mnliEMB_MODEL_NAME: sentence-transformers/all-MiniLM-L6-v2COMPRESSION_THRESHOLD_TOKENS: 2000SELF_CONSISTENCY_SAMPLES: 1RESULTS_PATH: results/experiment_results_final.csvThe project includes:
These tests help confirm that the repo is configured correctly before running a full GPU experiment.
This repository is part of an academic experiment on hallucination reduction in LLMs. Add your preferred license text here if you want to publish the project publicly.
This project studies how prompt compression affects hallucination in large language models. The pipeline runs several LLMs on benchmark question-answering datasets, optionally compresses long prompts using LLMLingua-2, and measures the effect with correctness, consistency, and entailment-based metrics.
The core idea is simple: remove redundant prompt text, keep the relevant evidence, and check whether the model becomes more grounded in its answer generation.
The system:
microsoft/Phi-3-mini-4k-instructmistralai/Mistral-7B-Instruct-v0.2meta-llama/Llama-2-7b-chat-hfmeta-llama/Meta-Llama-3-8B-InstructEach dataset subset contains 1,000 samples:
gsm8k_subset.jsonlsquad_v2_subset.jsonlhotpotqa_subset.jsonltriviaqa_subset.jsonlresults/experiment_results_final.csvresults/experiment_summary.csvresults/plot_*.pngThere are also model-specific and long-context experiment files such as tiny_long_experiment_results.csv and phi3_long_experiment_results.csv.
flowchart LR
A[JSONL datasets] --> B[Dataset loader]
B --> C[Prompt builder]
C --> D{Compression enabled?}
D -- Yes --> E[LLMLingua-2 prompt compression]
D -- No --> F[Original prompt]
E --> G[LLM generation]
F --> G[LLM generation]
G --> H[Metrics layer]
H --> H1[Exact Match]
H --> H2[Keyword Match]
H --> H3[Self-Consistency]
H --> H4[NLI Support]
H4 --> I[Hallucination flag]
H1 --> J[CSV results]
H2 --> J
H3 --> J
H4 --> J
I --> J
The experiment compares uncompressed and compressed prompts for the same sample. For each input:
This allows the project to separate two questions:
sequenceDiagram
participant D as Dataset
participant R as Runner
participant C as LLMLingua-2
participant L as LLM
participant M as Metrics
participant O as CSV Output
D->>R: sample (context, question, answer)
R->>R: build prompt
R->>C: compress prompt if enabled
C-->>R: compressed prompt + token counts
R->>L: generate answer(s)
L-->>R: predicted response(s)
R->>M: compute EM / KM / SC / NLI
M-->>R: metric values + hallucination flag
R->>O: write row to results CSV
src/dataset.pyReads JSONL files and yields samples with these fields:
iddatasetcontextquestionanswersrc/models.pyLoads three model families:
roberta-large-mnli),all-MiniLM-L6-v2) for self-consistency.It also includes GPU-aware loading, optional Hugging Face token support for gated models, and fallback behavior when memory is limited.
src/compression.pyUses LLMLingua-2 to compress prompts toward a fixed target token budget. If the compressor cannot be initialized, the pipeline falls back safely to the original prompt.
src/metrics.pyComputes four metrics:
src/runner.pyCoordinates the full experiment, handles checkpoint-style resume, writes CSV rows, and produces aggregated summary statistics after execution.
Exact Match is a strict correctness score.
Interpretation:
1.0 means the prediction exactly matches the reference.0.0 means it does not.Keyword Match is a looser correctness check.
1.0 only if all remaining gold keywords appear.Interpretation:
Self-Consistency measures how similar multiple answers are to each other.
Interpretation:
NLI Support is the main hallucination proxy in this project.
Interpretation:
Compression is controlled in src/config.py:
COMPRESSION_ENABLED = TrueCOMPRESSION_THRESHOLD_TOKENS = 2000FORCE_COMPRESSION = TrueThis means the pipeline always attempts compression, and LLMLingua-2 targets a 2,000-token budget.
Important note:
Compression is most useful for long, noisy prompts where the model has too much irrelevant context to process efficiently.
The repository contains long-context experiment outputs showing that compression helps most when prompts are long and redundant.
| Dataset | Mean NLI Support |
|---|---|
| GSM8K | 0.1652 |
| HotpotQA | 0.1261 |
| SQuAD v2 | 0.4212 |
| TriviaQA | 0.0919 |
In the long-context TinyLlama run, compression behaved differently across datasets:
| Dataset | Before Compression | After Compression | Improvement |
|---|---|---|---|
| HotpotQA | 0.1183 | 0.1259 | +6.42% |
| TriviaQA | 0.1263 | 0.1037 | -17.88% |
| Overall | 0.1223 | 0.1148 | -6.12% |
Takeaway:
The project also compares several LLMs under the same pipeline. The key research pattern is:
Prompt compression helps by improving the signal-to-noise ratio:
This is why compression is most effective when prompts are long and cluttered, and least useful when prompts are already short and focused.
.
├── data/
│ ├── gsm8k_subset.jsonl
│ ├── squad_v2_subset.jsonl
│ ├── hotpotqa_subset.jsonl
│ └── triviaqa_subset.jsonl
├── results/
│ ├── experiment_results_final.csv
│ ├── experiment_summary.csv
│ ├── tiny_long_experiment_results.csv
│ ├── tiny_long_experiment_summary.csv
│ ├── phi3_long_experiment_results.csv
│ ├── phi3_long_experiment_summary.csv
│ └── plots and aggregation files
├── src/
│ ├── config.py
│ ├── dataset.py
│ ├── models.py
│ ├── compression.py
│ ├── metrics.py
│ └── runner.py
├── tests/
│ ├── test_all.py
│ ├── test_pipeline.py
│ ├── verify_setup.py
│ └── other validation tests
├── prepare_datasets.py
├── requirements.txt
├── setup.md
└── README.md
git clone -b main https://github.com/Pratyay-Patel/Reduce-Hallucinations-in-LLMS.git
cd Reduce-Hallucinations-in-LLMS
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
Create a .env file in the project root:
HF_TOKEN=your_token_here
python tests/verify_setup.py
python tests/test_all.py
python -m src.runner
python prepare_datasets.py
Key values live in src/config.py:
LLM_MODELS: models used in the experimentNLI_MODEL_NAME: roberta-large-mnliEMB_MODEL_NAME: sentence-transformers/all-MiniLM-L6-v2COMPRESSION_THRESHOLD_TOKENS: 2000SELF_CONSISTENCY_SAMPLES: 1RESULTS_PATH: results/experiment_results_final.csvThe project includes:
These tests help confirm that the repo is configured correctly before running a full GPU experiment.
This repository is part of an academic experiment on hallucination reduction in LLMs. Add your preferred license text here if you want to publish the project publicly.