An enterprise-grade, hallucination-resistant Retrieval-Augmented Generation (RAG) Document Question & Answering system designed for critical corporate policies, API specifications, SLAs, and product manuals.
Built entirely from core principles without bloated wrapper frameworks (no LangChain / LlamaIndex abstraction layers) to provide deterministic provenance, sub-second search & reranking, and two-layer safety guardrails.
π‘ Live Interactive UI: Launch the Streamlit application (
uv run streamlit run src/ui/app.py) to test real-time grounded Q&A, inspection of dual-engine PDF tables, two-layer guardrail abstentions, and the interactive diagnostic explorer.
- Architecture Overview
- Key Features
- Corpus & Ingestion Pipeline
- Evaluation Benchmark Results
- Quickstart Guide
- Testing & Quality Assurance
- Project Directory Structure
- Key Design Decisions
flowchart TD
subgraph Offline_Indexing ["1. Offline Indexing Pipeline"]
PDFs["7 Enterprise PDFs (20 Pages, 11 Tables)"] --> Extractor["Hybrid Document Extractor\n(pdfplumber Tables + PyMuPDF Fallback)"]
Extractor --> TableProc["Table Processor\n(2D Grid -> Structured Markdown)"]
TableProc --> Chunker["Semantic & Q&A Boundary Chunker\n(51 Enriched Chunks, Zero Table Fragmentation)"]
Chunker --> Embedder["Embedding Engine\n(sentence-transformers/all-MiniLM-L6-v2, 384-dim)"]
Embedder --> ChromaDB[("ChromaDB Vector Store\n(51 Vectors + Full Metadata)")]
end
subgraph Online_Query ["2. Two-Stage Retrieval & Safety Guardrails"]
Query["User Query"] --> Stage1["Stage 1: Bi-Encoder Dense Retrieval\n(Top-K=5 Candidates via Cosine Similarity)"]
ChromaDB -.-> Stage1
Stage1 --> Stage2["Stage 2: Cross-Encoder Reranker\n(ms-marco-MiniLM-L-6-v2 Joint Attention)"]
Stage2 --> Composite["Composite Confidence Calculator\nScore = 0.35 * Vector + 0.65 * Sigmoid(Rerank)"]
Composite --> GuardrailL1{"Layer 1: Pre-LLM\nConfidence < 0.40?"}
GuardrailL1 -- Yes --> Abstain["π Honest Abstention\n(0 LLM Tokens, <800ms Latency)"]
GuardrailL1 -- No --> PromptBuild["Grounded Prompt Builder\n(Strict Context Injection + Markdown Tables)"]
end
subgraph Generation ["3. Multi-Provider LLM & Verification"]
PromptBuild --> LLM["LLM Client Engine\n(Groq Cloud / Ollama Local / Mock CI)"]
LLM --> GuardrailL2["Layer 2: Post-LLM Validator\n(Scrub Hallucinated URLs, Enforce Provenance)"]
GuardrailL2 --> UI["Streamlit Interactive UI\n(Confidence Badge + Provenance Cards)"]
end
-
Structure-Preserving Hybrid PDF Ingestion: Two-tier extraction architecture utilizing
pdfplumberas the primary engine for high-precision 2D table grid extraction andPyMuPDF(fitz) as a high-speed robust fallback. 11 multi-column tables converted directly to clean Markdown grids with headers and metadata preserved atomically. - Context-Aware Semantic Chunking: Splits text by semantic headings and Q&A boundaries while guaranteeing tables remain intact within single chunks (51 total chunks created across the 7 PDFs).
-
Two-Stage Hybrid Retrieval:
- Candidate Search: Fast Bi-Encoder dense vector retrieval over ChromaDB.
-
Deep Reranking: Cross-Encoder joint-attention re-scoring (
ms-marco-MiniLM-L-6-v2) eliminates semantic drift.
-
Mathematically Calibrated Confidence Scoring:
$$\text{Score} = 0.35 \times (1 - \text{CosineDistance}) + 0.65 \times \sigma(\text{RerankerLogit})$$ -
Two-Layer Safety Guardrails:
-
Pre-LLM Abstention: Automatically rejects out-of-domain queries when confidence
$< 0.40$ (Zero API cost,$<800\text{ms}$ response). -
Post-LLM URL & Provenance Sanitization: Strips hallucinated URLs (
[URL not in source documentation]) and enforces citations matching indexed documents.
-
Pre-LLM Abstention: Automatically rejects out-of-domain queries when confidence
-
Enterprise Provenance & Badges: Every response includes exact Document Codes (
PM-CSP-001,PRC-SLA-021, etc.), versions, page numbers, and color-coded HTML confidence badges:- π’ HIGH (
$\ge 0.70$ ) - π‘ MEDIUM (
$0.50 - 0.69$ ) - π LOW (
$0.40 - 0.49$ ) - π΄ ABSTAINED (
$< 0.40$ )
- π’ HIGH (
-
Multi-Provider LLM Engine: Seamlessly toggle between Groq Cloud (Llama 3.3 70B / Compound with automatic 429 rate limit backoff), Ollama (offline local privacy with
llama3.2:3b), and Mock (deterministic CI testing). - 3-Tab Streamlit Diagnostic Hub: Document Q&A, Knowledge Base Explorer (7 PDFs, 20 Pages, 51 Chunks), and Evaluator Diagnostic Playground with real-time Guardrail Simulator & Benchmark Dashboard.
The system indexes 7 mission-critical enterprise documents located in files/:
| Document Name | Document Code | Version | Pages | Tables | Description |
|---|---|---|---|---|---|
Product_Manual.pdf |
PM-CSP-001 |
v2.1 | 4 | 2 | CloudSync Pro appliance specs, RAID 1 storage, backup schedules |
API_Reference.pdf |
API-REF-002 |
v1.4 | 4 | 2 | Bearer token auth, rate limits, /v2/files/{file_id} endpoints |
Employee_Handbook.pdf |
HR-EH-2026 |
v4.0 | 4 | 1 | 1.75 days PTO accrual/month, 6 weeks parental leave, remote work |
Security_Policy.pdf |
SEC-POL-007 |
v3.0 | 2 | 2 | 72h incident notification SLA, data classification, encryption |
Onboarding_Guide.pdf |
ONB-GDE-009 |
v2.0 | 2 | 1 | Day 1 agenda, 5-day advance laptop delivery confirmation |
Pricing_and_SLA.pdf |
PRC-SLA-021 |
v3.2 | 2 | 2 | Free ($0, 5GB), Standard ($12, 100GB), Enterprise SLA (99.99%) |
FAQ_Support.pdf |
FAQ-SUP-014 |
v1.0 | 2 | 1 | 15-min password reset link, 2FA rules, 30-day post-cancel retention |
The system was evaluated using the 15-question benchmark suite (src/evaluation/) covering 4 query categories:
| Evaluation Metric | Measured Result | Benchmark Target | Status |
|---|---|---|---|
| Retrieval Recall@K | 100.0% |
β PASS | |
| Citation Precision | 100.0% |
β PASS | |
| Abstention Precision | 100.0% |
β PASS | |
| Abstention Recall | 100.0% |
β PASS | |
| Abstention F1-Score | 1.000 |
β PASS | |
| Mean End-to-End Latency | 2476.5 ms |
β PASS | |
| P50 Latency (Median) | 2327.0 ms |
β PASS | |
| P95 Latency | 6374.1 ms |
β PASS |
Full detailed breakdowns and audit logs are saved in EVALUATION_REPORT.md, SAMPLE_QA_LOG.md, and eval_log.json.
- Python 3.11+
- uv (recommended) or standard
pip
Create a .env file in the project root:
# Provider: 'groq', 'ollama', or 'mock'
DEFAULT_LLM_PROVIDER=groq
# Groq Cloud API Key (Free tier from https://console.groq.com)
GROQ_API_KEY=gsk_your_groq_api_key_here
# Groq Model
GROQ_MODEL=llama-3.3-70b-versatile
# ChromaDB & Model Settings
CHROMA_PERSIST_DIRECTORY=chroma_db
EMBEDDING_MODEL_NAME=sentence-transformers/all-MiniLM-L6-v2
RERANKER_MODEL_NAME=cross-encoder/ms-marco-MiniLM-L-6-v2# Create venv and sync all locked dependencies
uv sync# 1. Create and activate a virtual environment
python -m venv .venv
# On Linux/macOS:
source .venv/bin/activate
# On Windows PowerShell:
.venv\Scripts\Activate.ps1
# 2. Install dependencies from requirements.txt
pip install -r requirements.txt# Using uv:
uv run python -m src.vector_store.indexer
# Or using standard python:
python -m src.vector_store.indexer
# To force rebuild index from scratch:
python -m src.vector_store.indexer --force# Using uv:
uv run streamlit run src/ui/app.py
# Or using standard python:
streamlit run src/ui/app.pyOpen your browser at http://localhost:8501.
# Using uv:
uv run python -m src.evaluation.evaluator
# Or using standard python:
python -m src.evaluation.evaluatorThis runs all 15 benchmark questions, evaluates recall, precision, and latency, and generates updated eval_log.json, SAMPLE_QA_LOG.md, and EVALUATION_REPORT.md.
Run the comprehensive 31-test regression suite across all pipeline components:
# Using uv:
uv run pytest tests/ -v --no-header
# Or using standard pytest:
pytest tests/ -v --no-headertests/test_chunking.py(5 tests): Validates chunk counts (40-60 target), atomic table preservation, Q&A boundary splitting, and metadata schema.tests/test_vector_store.py(5 tests): Validates 384-dim embeddings, ChromaDB upsert/count, semantic retrieval, metadata filters, and idempotency.tests/test_retrieval.py(5 tests): Tests two-stage reranker reordering, composite confidence math, Layer 1 abstention, and document filtering.tests/test_generation.py(6 tests): Tests prompt builders, hallucinated URL scrubbing, citation deduplication, in-domain answers, and OOD abstentions.tests/test_ingestion.py(4 tests): Tests pdfplumber and PyMuPDF text/table extraction across all 7 PDFs, Markdown table conversion, cleaning, and metadata parsing.tests/test_ui.py(3 tests): Tests HTML badge rendering, source citation cards, and UI module exports.tests/test_evaluation.py(3 tests): Tests benchmark dataset integrity, automated evaluation harness execution, and Markdown report generation.
Result: 31 passed in 3 minutes (100% green).
βββ config/
β βββ config.py # Central Pydantic settings & environment configuration
β βββ logging_config.py # Standardized Loguru logger setup
βββ files/ # 7 Enterprise Source PDFs (PM, API, HR, SEC, ONB, PRC, FAQ)
βββ src/
β βββ ingestion/ # Phase 1: pdfplumber & PyMuPDF extractor, Markdown table processor
β βββ chunking/ # Phase 2: Semantic heading & Q&A boundary chunker (51 chunks)
β βββ vector_store/ # Phase 3: all-MiniLM-L6-v2 embedder, ChromaDB store & Indexer
β βββ retrieval/ # Phase 4: Bi-Encoder + MS-MARCO Cross-Encoder reranker & Guardrails
β βββ generation/ # Phase 5: Multi-provider LLMs (Groq, Ollama, Mock) & Validator
β βββ ui/ # Phase 6: Multi-tab Streamlit Web App & Diagnostic Hub
β βββ evaluation/ # Phase 7: 15-question benchmark suite & report generator
βββ tests/ # 31 Unit & Integration test suite
βββ EVALUATION_REPORT.md # Generated benchmark evaluation report
βββ SAMPLE_QA_LOG.md # 15-case sample Q&A benchmark log with provenance citations
βββ eval_log.json # Machine-readable evaluation logs
βββ pyproject.toml # Project dependency specification (uv / pip)
βββ requirements.txt # Standard pip requirements
βββ README.md # System documentation
- Why no LangChain / LlamaIndex? Built from scratch to prove deep mastery of vector spaces, cosine distance math, sigmoid logit calibration, and custom cross-encoder reranking mechanics.
-
Why Two-Stage Retrieval? Bi-encoders are fast (
$O(N)$ dot products) but lack cross-attention between query and document. Cross-encoders provide deep semantic matching but are too slow for full corpora ($O(N)$ full transformer passes). Bi-encoder (retrieve Top-5) + Cross-encoder (rerank Top-5) gives the best of both worlds in$<900\text{ms}$ . - Why Two-Layer Guardrails? Layer 1 rejects out-of-domain queries before calling the LLM (zero latency, zero API cost). Layer 2 scrubs hallucinated URLs and validates citations post-generation.