Skip to main content

Evaluation-First Control Layer for Enterprise RAG Systems

Project description

HardRAG Core

Evaluation-First Control Layer for Enterprise RAG Systems

HardRAG provides production-ready guardrails and evaluation metrics for RAG (Retrieval-Augmented Generation) systems, with a focus on enterprise governance, audit-readiness, and policy compliance.

Why HardRAG?

Most RAG systems work. Very few can be trusted.

HardRAG solves this by adding:

  • Guardrails - PII detection, toxicity filtering, grounding validation
  • Evaluation - Faithfulness, relevance, policy compliance metrics
  • Audit Trails - Complete request/response logging for compliance
  • DSPy-Powered - Self-improving, optimizable guardrails

Quick Start

pip install hardrag-core
from hardrag import HardRAGGuard

# Initialize guard with guardrails
guard = HardRAGGuard(
    guardrails=["pii", "grounding", "toxicity"],
    evaluation_mode="strict"
)

# Validate RAG output
result = guard.validate(
    query="What is ACME Corp's revenue?",
    retrieved_sources=[
        "ACME Corp reported $10M revenue in Q4 2023."
    ],
    llm_output="ACME Corp's revenue was $10M in Q4 2023."
)

print(result.is_valid)  # True
print(result.violations)  # []
print(result.audit_trail)  # Full evidence for audit

Features

Guardrails

  • PII Detection - Prevent leakage of sensitive data (emails, SSN, credit cards, phone numbers)
    • Microsoft Presidio integration
    • 30+ PII types detected
    • Anonymization support
    • Configurable allow-lists
  • Grounding Check - Verify outputs are supported by retrieved sources
    • DSPy-powered intelligent verification
    • Claim-level validation
    • Source attribution tracking
  • Toxicity Filter - Block harmful or inappropriate content
    • ML-based detection (transformers)
    • Rule-based fallback
    • Severity levels (LOW/MEDIUM/HIGH/CRITICAL)
    • 6 toxicity categories
  • Policy Compliance - Enforce custom business rules (coming soon)

Evaluation Metrics

  • Faithfulness - How well is the answer grounded in sources?
  • Relevance - Does the answer address the query?
  • Attribution - Are sources properly cited?

Enterprise Features

  • Audit Trails - Complete logging for compliance
  • Custom Guardrails - Define your own validation logic
  • Batch Evaluation - Process multiple outputs efficiently
  • Integration - Works with LangChain, LlamaIndex, or custom RAG

Installation

pip install hardrag-core

From Source

git clone https://github.com/yourusername/hardrag-core
cd hardrag-core
pip install -e .

Usage

Basic Guardrails

PII Detection

from hardrag.guardrails import PIIGuardrail

from hardrag import HardRAGGuard

guard = HardRAGGuard(guardrails=["pii"])
result = guard.validate(
    query="What's John's email?",
    retrieved_sources=["John's email is john@example.com"],
    llm_output="John's email is john@example.com"
)

if not result.is_valid:
    print(f"Violations: {result.violations}")
    print(f"Safe alternative: {result.anonymized_text}")

Toxicity Detection

from hardrag.guardrails import ToxicityGuardrail

toxicity_guard = ToxicityGuardrail(threshold=0.7)
result = toxicity_guard.validate(
    text="Your message here",
    block_if_toxic=True
)

if not result["is_valid"]:
    print(f"Toxic content detected: {result['categories']}")

Grounding Verification

from hardrag.guardrails import GroundingGuardrail
import dspy

# Configure DSPy (required)
dspy.settings.configure(lm=dspy.OpenAI(model="gpt-3.5-turbo"))

grounding_guard = GroundingGuardrail(threshold=0.8)
result = grounding_guard(
    retrieved_chunks=["Source text..."],
    llm_output="Generated answer..."
)

print(f"Grounded: {result['is_grounded']}")
print(f"Score: {result['score']}")

With LangChain

from langchain.chains import RetrievalQA
from hardrag.integrations.langchain import HardRAGCallback

chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=retriever,
    callbacks=[HardRAGCallback(guardrails=["pii", "grounding"])]
)

Evaluation

from hardrag.evaluation import FaithfulnessEvaluator

evaluator = FaithfulnessEvaluator()
score = evaluator.evaluate(
    sources=["Source text..."],
    output="Generated answer..."
)

print(f"Faithfulness score: {score}")  # 0.0 - 1.0

Architecture

HardRAG Core
├── Guardrails Layer (Pre-validation)
│   ├── PII Detection
│   ├── Toxicity Filtering
│   └── Grounding Validation
├── Evaluation Layer (Post-validation)
│   ├── Faithfulness Metrics
│   ├── Relevance Scoring
│   └── Attribution Check
└── Audit Layer (Compliance)
    ├── Request/Response Logging
    └── Evidence Generation

Roadmap

  • Core guardrails (PII, toxicity, grounding)
  • Policy compliance engine
  • Advanced evaluation metrics
  • REST API
  • Dashboard UI
  • Multi-language support

Contributing

Contributions welcome! See CONTRIBUTING.md

License

MIT License - see LICENSE

Links


Built with ❤️ for Enterprise AI Governance

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hardrag_core-0.1.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hardrag_core-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file hardrag_core-0.1.0.tar.gz.

File metadata

  • Download URL: hardrag_core-0.1.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for hardrag_core-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e68c4df10b6f6589bcccbcab0a02d2fada04a38ff53aace7066bb199b0038f6d
MD5 20f8db1c04536132a55a1b4d8dc9eccd
BLAKE2b-256 124d5b3712ca0003d329cc0b8b3bae0282bd56f418ea19d73d433329219ded14

See more details on using hashes here.

File details

Details for the file hardrag_core-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hardrag_core-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for hardrag_core-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 088ef4832661540d3d66de085fdb0c09dffb45d944ea4a1dc46b18bfbdbf89f3
MD5 e630e9892ed1424d5e229d1a74722a51
BLAKE2b-256 59530f569794b400ba26a338c2438db2767d71e1b1506f3df1f93657b22d6780

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page