A production-grade Retrieval Augmented Generation (RAG) pipeline with comprehensive hallucination detection and mitigation. Built from real-world deployments processing 2M+ claims/month (Cigna) and 500K+ tax transactions (Ceretax). The pipeline implements a four-stage architecture: document ingestion with intelligent chunking, hybrid retrieval (BM25 + dense vectors), grounded LLM generation with citation attribution, and multi-layer faithfulness validation using RAGAS, SelfCheckGPT, and NLI entailment. Supports deployment across AWS (Bedrock/OpenSearch), Azure (OpenAI/AI Search), and GCP (Vertex AI/Matching Engine).
| Component | File | Purpose | Key Dependencies |
|---|---|---|---|
| Document Processor | src/ingestion/document_processor.py | Intelligent document chunking with overlap, embedding generation, and vector store indexing | langchain, sentence-transformers |
| Hybrid Retriever | src/retrieval/hybrid_retriever.py | Combined BM25 sparse and dense vector search with cross-encoder reranking and relevance filtering | rank-bm25, pgvector, sentence-transformers |
| Grounded Generator | src/generation/grounded_generator.py | LLM generation with context injection, citation grounding, and source attribution | langchain-openai, anthropic, vertexai |
| Faithfulness Validator | src/validation/faithfulness_validator.py | Multi-method hallucination detection: RAGAS faithfulness scoring, SelfCheckGPT consistency, NLI entailment verification | ragas, nltk, transformers |
| Control | Implementation |
|---|---|
| API Key Management | Environment variables via python-dotenv; no hardcoded secrets |
| Data Encryption | AES-256 at rest for vector stores; TLS 1.3 in transit for all API calls |
| PHI/PII Handling | Configurable redaction pipeline for healthcare and financial data |
| Access Control | RBAC for document corpus and query endpoints |
| Audit Logging | Structured logging of all queries, retrievals, and generation events |
| Input Validation | Pydantic models for all request/response schemas; prompt injection prevention |
| Quality Gates | Configurable faithfulness thresholds prevent low-confidence responses from reaching users |
| Compliance | HIPAA/HITRUST, FedRAMP High, SOC 2, NIST 800-53 alignment |
HIPAA-compliant clinical RAG with PHI redaction, medical terminology understanding, clinical trial document retrieval, and HITRUST-aligned audit trails.
Tax compliance RAG (Ceretax pattern) with SOX audit support, PCI-DSS data handling, regulatory filing assistance, and financial document analysis.
Contract analysis RAG with clause extraction, precedent retrieval, regulatory change tracking, and attorney-client privilege preservation.
FedRAMP-authorized RAG with CUI handling, NIST 800-53 compliance, classified document retrieval controls, and FOIA response automation.
Claims processing RAG (Cigna pattern) with policy document retrieval, claims adjudication support, and fraud detection integration.
| Variable | Required | Default | Description |
|---|---|---|---|
OPENAI_API_KEY | Yes | -- | OpenAI API key for generation and embeddings |
ANTHROPIC_API_KEY | No | -- | Anthropic API key for Claude models |
VECTOR_DB_URL | Yes | -- | Vector store connection string |
EMBEDDING_MODEL | No | text-embedding-3-small | Embedding model for document and query encoding |
RERANKER_MODEL | No | cross-encoder/ms-marco-MiniLM-L-6-v2 | Cross-encoder model for result reranking |
CHUNK_SIZE | No | 512 | Document chunk size in tokens |
CHUNK_OVERLAP | No | 50 | Overlap between chunks in tokens |
FAITHFULNESS_THRESHOLD | No | 0.85 | Minimum faithfulness score for response delivery |
ENABLE_CACHE | No | true | Enable semantic caching for validated responses |
PROMETHEUS_PORT | No | 9090 | Prometheus metrics endpoint port |
git clone https://github.com/kogunlowo123/rag-pipeline-hallucination-mitigation.git
cd rag-pipeline-hallucination-mitigation
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
# Edit .env with API keys and DB connection
python -m src.ingestion.document_processor
python -m src.main
# AWS: Deploy with Bedrock + OpenSearch Serverless
# Azure: Deploy with Azure OpenAI + AI Search
# GCP: Deploy with Vertex AI + Matching Engine
# Docker deployment
docker build -t rag-pipeline .
docker run -e OPENAI_API_KEY=sk-xxx \
-e VECTOR_DB_URL=postgresql://... \
rag-pipeline
# Monitoring: Prometheus + Grafana stack
# See monitoring/dashboards/ for pre-built dashboards
| Repository | github.com/kogunlowo123/rag-pipeline-hallucination-mitigation |
| README | README.md |
| Hallucination Mitigation Guide | HALLUCINATION-MITIGATION.md |
| License | MIT License |