Skip to content

MukundaKatta/RAGGuard

Repository files navigation

RAGGuard

CI Python 3.9+ License: MIT Topic: AI

Detect hallucinations in RAG pipelines — verify that LLM responses are faithfully grounded in retrieved source documents.


How It Works

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]
Loading

Quickstart

Install

pip install git+https://github.com/MukundaKatta/RAGGuard.git

Python API

from 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 '❌'}")

CLI

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 "..." --detailed

Fact Checking

from 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']}")

Citation Verification

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

Features

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

Configuration

from ragguard import Settings

settings = Settings()
settings.weights.token_overlap = 0.35
settings.weights.semantic_similarity = 0.65
settings.thresholds.grounded = 0.70

Running Tests

pip install pytest
python -m pytest tests/ -v

License

MIT — see LICENSE.


Built by Officethree Technologies | Made with ❤️ and AI

About

RAG hallucination detection — verify LLM responses are grounded in source documents with faithfulness scoring

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors