eladlaor/hallucinations-math-lab
Acerca de
A research playground: evaluating entropy based methods for hallucination detection in open-weight language models
IA y aprendizaje automáticoCrecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- Recopilando historial
- in 15 days
- Recopilando historial
- in 1 month
- Recopilando historial
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
Original
Ask a model the same question n times. If it knows the answer, the n answers agree in meaning. If it doesn't, it makes up a bunch of different ones. Semantic Entropy turns this concept into a continuous measurement, and this repo evaluates how well that number can catch hallucinations in open-weight models (compared to other types of detectors).
Past research (by Farquhar and Kossen) brings to mind an even more interesting challenge: can a tiny trained classifier, probing the model's hidden state once, before it starts generating a response, predict the same effective signal?
An on-stage presentation of the concept behind this repo was recorded at Reversim Summit 2026. A link to the recording will be added here once it is published.
⭐ Star the repo to keep track of advancements and support this ongoing research ⭐
Contents: Key result · Try it · How it works · Research papers · Published probe · Docs · Limitations · Roadmap
Key result
On google/gemma-4-E2B-it, the one-pass Semantic Entropy Probe (SEP) recovers almost all of
Semantic Entropy's ability to rank hallucinations, at the cost of a single
forward pass:
| Detector | Cost per question | AUROC (95% CI) |
|---|---|---|
| Semantic Entropy (SE) | 11 generations + meaning clustering | 0.790 (0.733–0.847) |
| Semantic Entropy Probe (SEP) | one forward pass over the prompt | 0.780 (0.707–0.847) |
| Predictive Entropy (PE), baseline | free, from token probabilities | 0.750 (0.679–0.817) |
200 held-out TriviaQA questions, 133 answered wrong. The intervals overlap: this is evidence that the probe keeps most of SE's signal on one model. Full numbers and the layer study are in SEP probe.
Try it
No GPU and no LLM. The only model it downloads is a 1.6 GB meaning checker:
git clone https://github.com/eladlaor/hallucinations-math-lab.git
cd hallucinations-math-lab
uv sync --all-extras
uv run python -m demo.web # open http://127.0.0.1:8765
In Score answers, load the presets one by one and press Score answers. All three answer the same question, "In Breaking Bad, what do Saul Goodman's TV commercials tell viewers to do?", the way a model might:
| Preset | The ten answers | Meaning clusters | SE | Escalate? |
|---|---|---|---|---|
| Consistent | "Better call Saul!", ten times | 1 | 0.00 | no |
| Paraphrased | ten wordings of it, from "Call Saul." to "They tell viewers to call Saul." | 1 | 0.00 | no |
| Divergent | ten answers that contradict each other, from "Call a lawyer they can trust." to "Plead not guilty." | 7 | 1.83 | yes |
Predictive entropy scores the paraphrases higher than the consistent answers, because it sees varied wording as doubt. Semantic Entropy sees one meaning. That gap is the whole case for the method.
Prefer the terminal? uv run python examples/score_precollected_samples.py
prints the same table.
How it works
- Sample N answers to the question (default 10), plus one greedy answer.
- Group the answers by meaning: two answers share a group when a small NLI model (DeBERTa-MNLI) says each one implies the other.
- Semantic Entropy is the entropy over those groups: 0 when every answer means the same thing, ln 10 ≈ 2.3 when all ten differ.
- Above a threshold, the question is escalated; below it, you get the model's own answer. By default, escalation hands the question to a stronger judge model (Gemini on Vertex AI). But a high score means the model is missing knowledge, and a bigger model may be missing it too. Often the right escalation is better or broader retrieval that supplies the missing facts, not a stronger model. The score tells you when to escalate; how is your choice.
| Detector | Reads | Paper |
|---|---|---|
| SE | how many different meanings appear across N answers | Farquhar et al., Nature 2024 |
| SEP | the hidden state before answering, through a trained logistic probe | Kossen et al., 2024 |
| PE | token probabilities of the answers | baseline |
More in The detectors, explained.
Research this builds on
This repo implements and evaluates methods from these papers:
- Semantic Entropy, introduced. Lorenz Kuhn, Yarin Gal, Sebastian Farquhar. Semantic Uncertainty: Linguistic Invariances for Uncertainty Estimation in Natural Language Generation. ICLR 2023. arXiv:2302.09664
- Semantic Entropy for hallucination detection. Sebastian Farquhar, Jannik Kossen, Lorenz Kuhn, Yarin Gal. Detecting hallucinations in large language models using semantic entropy. Nature 630, 625–630 (2024). doi:10.1038/s41586-024-07421-0
- Semantic Entropy Probes. Jannik Kossen, Jiatong Han, Muhammed Razzak, Lisa Schut, Shreshth Malik, Yarin Gal. Semantic Entropy Probes: Robust and Cheap Hallucination Detection in LLMs. 2024. arXiv:2406.15927
- The benchmark questions. Mandar Joshi, Eunsol Choi, Daniel Weld, Luke Zettlemoyer. TriviaQA: A Large Scale Distantly Supervised Challenge Dataset for Reading Comprehension. ACL 2017. arXiv:1705.03551
Where this repo departs from the probe paper, and why, is listed in SEP probe.
Use the published probe
The probe for google/gemma-4-E2B-it is on the Hugging Face Hub as
eladlaor/SEP-gemma-4-E2B-it,
with its calibration dataset.
To enable it, set in .env:
SLM_PROVIDER=hf_transformers
SEP_PROBE_PATH=hf://eladlaor/SEP-gemma-4-E2B-it@v2
ENABLED_DETECTORS=["predictive_entropy","semantic_entropy_probe","semantic_entropy"]
Calibrate SEP_THRESHOLD on a dev set before relying on it for escalation
(why).
Documentation
| I want to | Read |
|---|---|
| use the playground, the CLI, or score my own model's answers | User Guide |
| know whether my machine can run the model | Hardware |
| benchmark detectors, re-score a run, compare models | Benchmarking |
| use, train or understand the probe | SEP probe |
| understand SE, PE and AUROC | The detectors, explained · SE walkthrough · Glossary |
| know where this breaks | Caveats |
| fix an error | Troubleshooting |
| change the code or add a detector | Developer Guide |
| serve the model on Vertex AI or my own GPU | DevOps Guide |
| run the tests | QA Guide |
Limitations
- Only applicable for open-weight models.
- This is meant for factual answers only. Clustering by meaning gets unreliable on long multi-claim answers.
- SE measures disagreement, not falsehood. So this won't help a model that is confidently and consistently wrong.
- Escalation doesn't necessarily have to mean sending to a 'stronger' model. Sometimes a broader/different retrieval would be a more relevant escalation.
- Currently only a Gemma 4 model is registered. Adding another takes a registry entry.
Roadmap
- Retrieval as an escalation path. Let a high score trigger better or broader retrieval instead of a stronger model, and measure which one fixes more answers.
- Cost next to accuracy. Report cost and latency per question for every detector alongside AUROC, with an LLM-as-a-judge detector as the reference point, so the choice becomes where to sit on the trade-off, not only which detector "wins".
- More models. Probes for more open-weight models beyond Gemma 4 E2B, published on the Hugging Face Hub like the first one.
⭐ Starring is caring ⭐
Explorar proyectos relacionados
Cargando…