A comprehensive research toolkit for quantifying uncertainty and credibility in Large Language Model outputs using Mutual Information (MI) estimation, iterative prompting, and NLI-based semantic clustering. Implements methods from "To Believe or Not to Believe Your LLM" (DeepMind, 2024).
- MI-Based Uncertainty Quantification: Measure epistemic uncertainty through mutual information between iterative prompts
- Expected Calibration Error (ECE): Evaluate how well model confidence aligns with actual accuracy
- Multiple Baseline Methods: Greedy, self-consistency, semantic-entropy, self-verification
- NLI Semantic Clustering: Group semantically equivalent answers using DeBERTa-MNLI
- Multi-Benchmark Support: ARC, OpenBookQA, TruthfulQA, SQuAD v2, TriviaQA
quantify_credibility/
βββ llm-belief-mi-test/ # π§ͺ Main MI evaluation framework
β βββ llm_belief_mi_test/ # Core Python package
β β βββ cli.py # Command-line interface
β β βββ calibration.py # ECE & evaluation (with baselines)
β β βββ mi_estimator.py # MI computation (listing/plugin)
β β βββ iterative_prompting.py # Iterative prompting chains
β β βββ llm_client_local.py # Local Llama client
β β βββ datasets.py # Dataset loaders
β β βββ cache.py # SQLite caching
β βββ scripts/ # Utility scripts
β β βββ test_gpu_setup.py # GPU verification
β β βββ compare_results.py # Results comparison
β β βββ plot_results.py # Visualization
β βββ outputs/ # Results & logs
β βββ docs/ # Detailed documentation
β
βββ llm-belief-mi-repro/ # π Paper reproduction experiments
β βββ llm_belief_mi_repro/ # Reproduction code
β βββ outputs/ # Reproduction results
β
βββ nli-semantic-clustering/ # π NLI clustering module
β βββ nli_clustering/ # Core NLI package
β β βββ core.py # DeBERTa-MNLI model & clustering
β β βββ utils.py # Evaluation metrics
β βββ scripts/
β β βββ threshold_sweep.py # Threshold optimization
β βββ data/ # Sample data
β
βββ theory/ # π Theoretical foundations
βββ MI_ALGORITHMS.md # MI estimator algorithms
βββ MI_ECE_FORMULAS.md # Mathematical formulas
βββ MI_ESTIMATOR_EXAMPLE.md # Worked examples
cd quantify_credibility/llm-belief-mi-test
pip install -r requirements.txt
# Set HuggingFace token for Llama models
export HF_TOKEN="hf_YOUR_TOKEN_HERE"python scripts/test_gpu_setup.pypython -m llm_belief_mi_test.cli \
--method mi \
--dataset arc-easy --limit 5 \
--k 10 --n 2 --temperature 0.9 \
--load-in-4bit --max-tokens 10 \
--answer-format strict \
--output outputs/results/test_quick.csvUses iterative prompting to estimate mutual information between response chains.
python -m llm_belief_mi_test.cli \
--method mi \
--dataset arc-challenge --limit 500 \
--k 10 --n 2 --temperature 0.9 \
--load-in-4bit --max-tokens 10 \
--answer-format strict \
--output outputs/results/mi_500.csv# Greedy (single decode)
python -m llm_belief_mi_test.cli --method greedy --dataset arc-easy ...
# Self-Consistency (k samples + majority vote)
python -m llm_belief_mi_test.cli --method self-consistency --k 10 ...
# Semantic Entropy (F1 clustering + entropy)
python -m llm_belief_mi_test.cli --method semantic-entropy --k 10 ...
# Self-Verification (samples + verification query)
python -m llm_belief_mi_test.cli --method self-verification --k 10 ...| Dataset | Type | Size | Key Metric |
|---|---|---|---|
| ARC-Challenge | MCQ | 1,172 | Accuracy |
| ARC-Easy | MCQ | 2,376 | Accuracy |
| OpenBookQA | MCQ | 500 | Accuracy |
| TruthfulQA MC1 | MCQ | 817 | Accuracy |
| TruthfulQA MC2 | MCQ (multi-true) | 817 | Accuracy |
| SQuAD v2 | Extractive QA | 11,873 | EM/F1 |
| TriviaQA | Open-domain QA | 87,622 | EM/F1 |
Measures how well model confidence correlates with actual accuracy:
ECE = Ξ£ (n_b / N) |accuracy_b - confidence_b|
Lower ECE = Better Calibration (main contribution of MI method)
Captures epistemic uncertainty through iterative prompting:
MI(Yβ; Yβ; ...; Yβ) = Ξ£α΅’ H(Yα΅’) - H(Yβ,...,Yβ)
Higher MI = More Uncertainty
| Parameter | Description | Default |
|---|---|---|
--k |
Number of independent chains | 10 |
--n |
Chain length (pseudo-joint dimension) | 2 |
--temperature |
Sampling temperature | 0.9 |
--mi-method |
MI estimator (listing/plugin) |
listing |
--confidence-method |
MIβconfidence mapping | inverse |
--answer-format |
Output format (strict/codeblock) |
strict |
Key Insight: MI method is designed for better calibration (lower ECE), not necessarily higher accuracy.
| Method | ARC-C Acc | ARC-C ECE | ARC-E Acc | ARC-E ECE |
|---|---|---|---|---|
| Greedy | ~65% | ~0.12 | ~80% | ~0.08 |
| Self-Consistency | ~67% | ~0.10 | ~82% | ~0.06 |
| MI Method | ~66% | ~0.05 | ~81% | ~0.04 |
The NLI module groups semantically equivalent answers to reduce spurious uncertainty:
cd nli-semantic-clustering
python scripts/threshold_sweep.py \
--log-dir ../llm-belief-mi-test/outputs/logs/triviaqa_mi_200 \
--thresholds 0.3 0.4 0.5 0.6 0.7 \
--correctness-based \
--output results/threshold_sweep.json- Clustering: Strict bidirectional entailment (A β B)
- Grading: Loose unidirectional (A β B) + substring matching
Plugin Estimator (simple):
MI = Ξ£α΅’ H(Yα΅’) - H(Yβ,...,Yβ)
H(X) = -Ξ£ p(x) log p(x)
Listing Estimator (paper's Algorithm 1):
MI = Ξ£ ΞΌΜ Β· log((ΞΌΜ + Ξ³β) / (ΞΌΜ_prod + Ξ³β))
Ξ³β, Ξ³β = 1/k (regularization)
See theory/MI_ALGORITHMS.md for detailed explanations.
- GPU: 12GB VRAM (RTX 3060, RTX 4060 Ti)
- RAM: 16GB
- GPU: 16GB+ VRAM (RTX 4080, A4000+)
- RAM: 32GB
| Dataset | Examples | Time (A100) |
|---|---|---|
| ARC-Challenge | 1,172 | ~3-4 hours |
| ARC-Easy | 2,376 | ~6-7 hours |
| OpenBookQA | 500 | ~1.5 hours |
- llm-belief-mi-test/README.md - Full evaluation guide
- theory/MI_ALGORITHMS.md - MI estimator details
- theory/MI_ECE_FORMULAS.md - Mathematical formulas
- nli-semantic-clustering/README.md - NLI clustering guide
- Paper: "To Believe or Not to Believe Your LLM" (DeepMind, 2024) - arXiv:2406.02543
- Llama 3.1: HuggingFace
- DeBERTa-MNLI: microsoft/deberta-v2-xlarge-mnli
- ARC - AI2 Reasoning Challenge
- OpenBookQA - Open-domain QA
- TruthfulQA - Truthfulness evaluation
- SQuAD v2 - Reading comprehension
- TriviaQA - Trivia QA
MIT License
Contributions are welcome! Please feel free to submit issues and pull requests.
Research toolkit for quantifying LLM uncertainty and calibration.