An enterprise-ready, domain-specific 'Ask-My-Doc' Retrieval-Augmented Generation architecture designed for zero-hallucination compliance, high retrieval precision, and automated CI/CD quality enforcement.
flowchart TD
subgraph Phase 1: Ingestion & Indexing
DOCS["Raw Docs (PDF / Markdown / TXT)"] --> SPLIT["Token Chunking\n(500-800 tokens, 100 token overlap)"]
SPLIT --> EMB["OpenAI Embeddings\n(text-embedding-3-small)"]
EMB --> VEC[("ChromaDB Vector Store")]
end
subgraph Phase 2: Production Hybrid Retrieval & Re-ranking
QUERY["User Question"] --> BM25["Sparse BM25 Keyword Search"]
QUERY --> DENSE["Dense Semantic Search"]
VEC -.-> DENSE
SPLIT -.-> BM25
BM25 --> UNION["Candidate Union & Deduplication"]
DENSE --> UNION
UNION --> RERANK["Cohere Cross-Encoder Rerank\n(rerank-english-v3.0)"]
RERANK --> PROMPT["Citation-Enforced Prompt Engine\n(src/config/prompts.yaml)"]
PROMPT --> LLM["LLM Synthesis\n(GPT-4o)"]
LLM --> ANSWER["Answer with Strict Source Citations\nor Explicit Decline"]
end
subgraph Phase 3: CI/CD Quality Gate & Evaluation
GOLDEN[("Golden Dataset\n(50-200 QA Pairs)")] --> EVAL["Ragas Offline Evaluation\n(Faithfulness & Relevancy)"]
ANSWER -.-> EVAL
EVAL --> GATE{"Faithfulness >= 0.80?"}
GATE -- "Yes" --> PASS["CI/CD Build Passed β
"]
GATE -- "No" --> FAIL["Fail PR Build β"]
end
- Token-Aware Chunking: Uses
RecursiveCharacterTextSplitter.from_tiktoken_encoderconfigured for 500β800 tokens with a 100-token sliding overlap (cl100k_base), guaranteeing contextual boundary integrity. - Heterogeneous Parsers: Native ingestion for
.pdf,.md, and.txtdocuments. - Persistent Vector Store: ChromaDB integration backed by OpenAI
text-embedding-3-small.
- Hybrid Retrieval: Combines BM25 sparse keyword matching with dense vector similarity to solve the "vocabulary mismatch" problem.
- Cross-Encoder Re-Ranking: Reranks top-$k$ candidates with Cohere's
rerank-english-v3.0cross-encoder for deep token-level interaction. - Citation Enforcement: Strictly demands source file and page citations; explicitly instructs the model to refuse answering if the context is insufficient.
- Versioned Prompt Config: Prompts are externalized and version-controlled via
src/config/prompts.yaml.
- Offline Evaluation with Ragas: Evaluates faithfulness and answer relevancy against a golden dataset (
tests/golden_dataset.json). - Automated Pull Request Blocking: GitHub Actions workflow fails PR builds when faithfulness scores drop below the configured threshold (default:
0.80).
.
βββ .github/
β βββ workflows/
β β βββ ci.yml # Multi-matrix CI & Ragas quality gate
β βββ ISSUE_TEMPLATE/
β β βββ bug_report.md # Structured bug report template
β β βββ feature_request.md # Feature request template
β βββ pull_request_template.md # PR checklist and evaluation criteria
βββ data/
β βββ sample_docs/
β βββ enterprise_rag_specification.md # Sample ingestion corpus
βββ src/
β βββ __init__.py
β βββ config/
β β βββ __init__.py
β β βββ prompts.yaml # Versioned LLM prompts with citation rules
β βββ ingestion/
β β βββ __init__.py
β β βββ document_processor.py # Token-bounded document chunking
β βββ retrieval/
β β βββ __init__.py
β β βββ vector_store.py # ChromaDB vector store wrapper
β β βββ hybrid_search.py # BM25 + Vector + Cohere cross-encoder rerank
β βββ generation/
β β βββ __init__.py
β β βββ llm_chain.py # LCEL generation pipeline with citations
β βββ evaluation/
β βββ __init__.py
β βββ evaluate.py # Ragas offline evaluation script
βββ tests/
β βββ __init__.py
β βββ golden_dataset.json # Curated QA evaluation pairs
β βββ test_document_processor.py # Ingestion & chunking unit tests
β βββ test_hybrid_search.py # BM25 & reranker unit tests
β βββ test_citation_prompt.py # Prompt & citation formatting tests
βββ .env.example # Environment template
βββ .gitignore # Python, OS, virtualenv & database ignores
βββ CONTRIBUTING.md # Contributor guide
βββ LICENSE # MIT License
βββ main.py # Unified CLI entrypoint
βββ pyproject.toml # Modern packaging & pytest config
βββ requirements.txt # Production dependencies
βββ README.md # Project documentation
git clone https://github.com/prosuvinhere/Production-Grade-Retrieval-Augmented-Generation-RAG-System.git
cd Production-Grade-Retrieval-Augmented-Generation-RAG-System
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install --upgrade pip
pip install -r requirements.txtCopy the environment template and set your API keys:
cp .env.example .envEdit .env:
OPENAI_API_KEY=sk-...
COHERE_API_KEY=...
CHROMA_PERSIST_DIR=./chroma_db
RAGAS_FAITHFULNESS_THRESHOLD=0.80The repository provides a single CLI entrypoint (main.py):
Parse, chunk, and index documents from data/sample_docs:
python main.py ingest --dir data/sample_docs --chunk-size 800 --chunk-overlap 100Basic Semantic Search:
python main.py query "What chunk overlap size is required?"Production Hybrid Search with Cohere Cross-Encoder Reranking:
python main.py query "Why combine BM25 and vector search?" --hybrid --top-k 10 --rerank-top-k 3Testing Anti-Hallucination & Refusal:
python main.py query "What is the capital of Mars?"
# Output: "I cannot answer this question based on the provided context."Run offline Ragas evaluation against tests/golden_dataset.json:
python main.py evaluate --threshold 0.80Unit tests run locally with mock keys without needing live API billing:
pytest -vOn every Pull Request to main, GitHub Actions automatically:
- Validates code syntax and executes unit tests across Python 3.10 and 3.11.
- Ingests the sample documentation corpus.
- Executes
src/evaluation/evaluate.pywith Ragas. - Fails the build if the mean faithfulness score falls below
0.80.
Distributed under the MIT License.