Detect hallucinations in RAG pipelines — verify that LLM responses are faithfully grounded in retrieved source documents.
flowchart LR
A[LLM Response] --> B[Tokenize & Split]
C[Source Context] --> D[Tokenize & Split]
B --> E[Token Overlap]
B --> F[TF-IDF Vectors]
D --> F
E --> G[Weighted Score]
F --> H[Cosine Similarity]
H --> G
G --> I{GroundednessScore}
I -->|>= 0.70| J[✅ Grounded]
I -->|0.40 – 0.70| K[⚠️ Partially Grounded]
I -->|< 0.40| L[❌ Hallucinated]
pip install git+https://github.com/MukundaKatta/RAGGuard.gitfrom ragguard import HallucinationDetector, generate_faithfulness_report
detector = HallucinationDetector()
context = "Python was created by Guido van Rossum and released in 1991."
response = "Python was created by Guido van Rossum in 1991."
result = detector.score(response, context)
print(result.score) # 0.95
print(result.label) # "grounded"
# Detailed per-sentence report
report = generate_faithfulness_report(response, context)
for s in report.sentence_scores:
print(f" {s.sentence[:50]} → {s.score:.0%} {'✅' if s.grounded else '❌'}")ragguard check \
--response "Python was released in 1991." \
--context "Python was created by Guido van Rossum and first released in 1991."
# Detailed breakdown
ragguard check -r "..." -c "..." --detailedfrom ragguard import FactChecker
checker = FactChecker()
results = checker.check(
response="Python was released in 1991. It was created by Linus Torvalds.",
source_documents=["Python was created by Guido van Rossum and released in 1991."],
)
for r in results:
print(f" {r['claim'][:50]} → grounded={r['grounded']}")from ragguard import CitationVerifier
verifier = CitationVerifier()
results = verifier.verify([
{"claim": "Python was released in 1991.", "source": "Python first appeared in 1991."},
])
print(results[0].supported) # True| Feature | Description |
|---|---|
| HallucinationDetector | Combined token-overlap + TF-IDF semantic scoring |
| FactChecker | Extract claims and verify each against source docs |
| CitationVerifier | Check that cited sources support their claims |
| FaithfulnessReport | Per-sentence grounding breakdown |
| CLI | Quick checks from the command line with Rich output |
| Zero ML dependencies | Pure Python — no torch, no transformers |
from ragguard import Settings
settings = Settings()
settings.weights.token_overlap = 0.35
settings.weights.semantic_similarity = 0.65
settings.thresholds.grounded = 0.70pip install pytest
python -m pytest tests/ -vMIT — see LICENSE.
Built by Officethree Technologies | Made with ❤️ and AI