hallucination-gate
A conservative grounding gate for RAG and fine-tuned generators. It does not decide whether an answer is true in the world. It decides whether the answer is supported by the evidence you pass in, then passes, rewrites, or abstains.
False release is treated as the failure mode that matters. If a rewrite would no longer answer the question, the gate abstains.
from hallucination_gate import HallucinationGate, Evidence
gate = HallucinationGate() # or mode="fine_tuned"
result = gate.check(
query=user_query,
answer=llm_answer,
context=retrieved_docs, # str | list[str] | LangChain Document | LlamaIndex node | dict
)
return result.text # show this to users
gate = HallucinationGate(mode="fine_tuned")
result = gate.check(query, answer, kb=your_knowledge_base)
result = gate.check(query, answer, evidence=Evidence.from_image(path="photo.jpg", ocr="..."))
result = gate.check(query, answer, evidence=Evidence.from_pdf("policy.pdf"))
@gate.protect
def my_rag(query: str):
docs = retriever(query)
answer = llm(query, docs)
return answer, docs
What this is (and is not)
| It does | It does not |
|---|---|
| Check claims against your retrieved chunks / KB / OCR / PDF text | Know if the KB itself is wrong |
| Abstain on contradiction, invented entities, and number clashes | Read fine-tune weights |
| Drop ungrounded sentences, then abstain if the remainder misses the query | Replace an LLM-as-judge on subtle reasoning, code, or math proofs |
Work with any stack that can give you query, answer, evidence |
Guarantee multilingual performance equal to English without swapping models |
Default neural backends: multilingual MiniLM + DeBERTa NLI. Override with embed_model=, nli_model=, or RAG_EVAL_EMBED_MODEL / RAG_EVAL_NLI_MODEL.
Release is decided by claim grounding, not by the Bayesian network. BN scores are diagnostics only.
Set RAG_EVAL_HEURISTIC=1 for CI / offline (token coverage, no model download). Optional: HALLUCINATION_GATE_JUDGE=1 plus ANTHROPIC_API_KEY or OPENAI_API_KEY to escalate uncertain claims only.
Eval
Held-out domains (HR, API, vaccines, Redis, K8s) report false release and over-refusal:
pip install -e ".[dev]"
set RAG_EVAL_HEURISTIC=1
pytest -q -m "not neural"
hallucination-gate eval-heldout
Install
pip install hallucination-gate
From GitHub:
pip install git+https://github.com/shrey315/hallucination-gate.git
From this folder:
pip install -e ".[dev]"
pytest -q
HTTP API
uvicorn bayesian_rag_evaluator.api.main:app --reload --port 8000
POST /v1/answer returns only {safe_answer, released, request_id, latency_ms}.
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hallucination_gate-0.6.0.tar.gz.
File metadata
- Download URL: hallucination_gate-0.6.0.tar.gz
- Upload date:
- Size: 48.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4bc6b0a86f63503f2fc089d5bc04948c9af0d4a31bb923a94c82db95881b80f
|
|
| MD5 |
2dacd35131cf3bdc871b993d4cad41fc
|
|
| BLAKE2b-256 |
f2cf92e1cbbf92ae0a15808b134faa2f1219691b37ddc805d3eed3e004d70d4b
|
Provenance
The following attestation bundles were made for hallucination_gate-0.6.0.tar.gz:
Publisher:
publish.yml on shrey315/hallucination-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hallucination_gate-0.6.0.tar.gz -
Subject digest:
e4bc6b0a86f63503f2fc089d5bc04948c9af0d4a31bb923a94c82db95881b80f - Sigstore transparency entry: 2477614081
- Sigstore integration time:
-
Permalink:
shrey315/hallucination-gate@c2b41916d7262d5792f9b7c8f920cc5dd30b93fb -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/shrey315
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2b41916d7262d5792f9b7c8f920cc5dd30b93fb -
Trigger Event:
release
-
Statement type:
File details
Details for the file hallucination_gate-0.6.0-py3-none-any.whl.
File metadata
- Download URL: hallucination_gate-0.6.0-py3-none-any.whl
- Upload date:
- Size: 55.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5ab02e2cf7369a487e6fa4d3f1ea306a2d8665428dff1e0181a5afcd47f6862
|
|
| MD5 |
1e70dd56f32aa3685b7bc6c060476db9
|
|
| BLAKE2b-256 |
ca6bb2a060433177e388fadab915683832f6fa473ad319297dd9f4e1075a3272
|
Provenance
The following attestation bundles were made for hallucination_gate-0.6.0-py3-none-any.whl:
Publisher:
publish.yml on shrey315/hallucination-gate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hallucination_gate-0.6.0-py3-none-any.whl -
Subject digest:
d5ab02e2cf7369a487e6fa4d3f1ea306a2d8665428dff1e0181a5afcd47f6862 - Sigstore transparency entry: 2477614093
- Sigstore integration time:
-
Permalink:
shrey315/hallucination-gate@c2b41916d7262d5792f9b7c8f920cc5dd30b93fb -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/shrey315
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@c2b41916d7262d5792f9b7c8f920cc5dd30b93fb -
Trigger Event:
release
-
Statement type: