Skip to content

About

Production-grade Retrieval-Augmented Generation (RAG) system built with LangChain, ChromaDB, and Cohere Rerank. Features Hybrid Search (BM25 + Vector), strict citation enforcement with zero-hallucination guardrails, and an automated CI/CD evaluation gate powered by Ragas.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

2 Commits

Folders and files

Repository files navigation

πŸš€ Production-Grade Retrieval-Augmented Generation (RAG) System

CI & RAG Quality Gate Python 3.10+ License: MIT LangChain ChromaDB Cohere Ragas

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.


πŸ›οΈ System Architecture

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
Loading

🌟 Key Features

Phase 1: Fundamentals

  • Token-Aware Chunking: Uses RecursiveCharacterTextSplitter.from_tiktoken_encoder configured for 500–800 tokens with a 100-token sliding overlap (cl100k_base), guaranteeing contextual boundary integrity.
  • Heterogeneous Parsers: Native ingestion for .pdf, .md, and .txt documents.
  • Persistent Vector Store: ChromaDB integration backed by OpenAI text-embedding-3-small.

Phase 2: Production Quality

  • 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.0 cross-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.

Phase 3: CI/CD Quality Gate

  • 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).

πŸ“ Repository Structure

.
β”œβ”€β”€ .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

⚑ Quickstart Guide

1. Clone & Set Up Environment

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.txt

2. Configure Credentials

Copy the environment template and set your API keys:

cp .env.example .env

Edit .env:

OPENAI_API_KEY=sk-...
COHERE_API_KEY=...
CHROMA_PERSIST_DIR=./chroma_db
RAGAS_FAITHFULNESS_THRESHOLD=0.80

πŸ–₯️ CLI Usage

The repository provides a single CLI entrypoint (main.py):

Ingest Documents (Phase 1)

Parse, chunk, and index documents from data/sample_docs:

python main.py ingest --dir data/sample_docs --chunk-size 800 --chunk-overlap 100

Query Pipeline (Phase 2)

Basic 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 3

Testing 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 Evaluation Gate (Phase 3)

Run offline Ragas evaluation against tests/golden_dataset.json:

python main.py evaluate --threshold 0.80

πŸ§ͺ Testing & CI/CD Guardrails

Local Unit Testing

Unit tests run locally with mock keys without needing live API billing:

pytest -v

GitHub Actions CI Workflow

On every Pull Request to main, GitHub Actions automatically:

  1. Validates code syntax and executes unit tests across Python 3.10 and 3.11.
  2. Ingests the sample documentation corpus.
  3. Executes src/evaluation/evaluate.py with Ragas.
  4. Fails the build if the mean faithfulness score falls below 0.80.

πŸ“œ License

Distributed under the MIT License.

About

Production-grade Retrieval-Augmented Generation (RAG) system built with LangChain, ChromaDB, and Cohere Rerank. Features Hybrid Search (BM25 + Vector), strict citation enforcement with zero-hallucination guardrails, and an automated CI/CD evaluation gate powered by Ragas.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages