Skip to main content
Glama
naffss-eng

Modular RAG System

by naffss-eng
README.md
# ๐Ÿš€ Modular RAG System
### A Modular Retrieval-Augmented Generation (RAG) System with Adaptive Retrieval, Intent-Aware Query Processing, Semantic Query Rewriting, and MCP Server Integration

<p align="center">

![Python](https://img.shields.io/badge/Python-3.10+-blue.svg)
![MCP](https://img.shields.io/badge/MCP-Compatible-success)
![RAG](https://img.shields.io/badge/RAG-Modular-orange)
![Gemini](https://img.shields.io/badge/Gemini-2.5_Flash-blueviolet)
![Phi3](https://img.shields.io/badge/Phi--3-Local-green)
![ChromaDB](https://img.shields.io/badge/VectorDB-ChromaDB-red)
![Status](https://img.shields.io/badge/Status-Backend%20v2.0-success)

</p>

---

# ๐Ÿ“– Overview

Modern enterprise organizations store thousands of pages of policies, reports, technical documents, and internal knowledge bases. Traditional keyword search often fails when users ask questions using natural language, paraphrased wording, or indirect references.

This project implements a **Modular Retrieval-Augmented Generation (RAG) System** capable of answering natural language questions over enterprise documents while providing grounded, citation-backed responses.

Unlike conventional RAG systems that treat every query identically, this system introduces **intent-aware routing**, allowing different categories of questions to follow specialized processing pipelines. The architecture further improves retrieval quality through an **Adaptive Retrieval Loop**, which selectively invokes a local Phi-3 query rewriter only when retrieval confidence is low.

The system follows a layered design emphasizing **retrieval quality**, **grounding**, **modularity**, **maintainability**, and **explainability**.

---

# ๐ŸŽฏ Project Objectives

The primary objectives of this project are:

- Build an MCP-compatible document question answering backend.
- Support multiple enterprise documents simultaneously.
- Generate grounded responses with citations.
- Reduce hallucinations through evidence validation.
- Improve semantic retrieval for paraphrased questions.
- Keep the architecture modular for future scalability.
- Maintain compatibility with local LLMs and cloud LLMs.
- Optimize retrieval quality while minimizing unnecessary latency.

---

# โœจ Key Features

## Intelligent Intent Routing

Instead of treating every user query identically, the system automatically classifies incoming questions into specialized pipelines.

Supported query categories include:

- Factual Questions
- Numeric Questions
- Boolean Questions
- Procedure Questions
- Summary Requests
- Comparison Questions
- Multi-document Queries
- Out-of-Scope Detection

---

## Adaptive Retrieval

Retrieval is no longer a single-pass process.

When confidence is high:

```
User Query
      โ”‚
      โ–ผ
 Retrieval
      โ”‚
      โ–ผ
 Answer
```

When confidence is low:

```
User Query
      โ”‚
      โ–ผ
 Initial Retrieval
      โ”‚
      โ–ผ
Confidence Evaluation
      โ”‚
      โ–ผ
Query Rewriter (Phi-3)
      โ”‚
      โ–ผ
Second Retrieval
      โ”‚
      โ–ผ
Quality Comparison
      โ”‚
      โ–ผ
Best Retrieval Selected
```

This selective retrieval strategy improves semantic matching while avoiding unnecessary model execution.

---

## Query Rewriting

Instead of maintaining thousands of manually curated synonym mappings, the system leverages a **local Phi-3 model** to rewrite ambiguous queries into document-friendly terminology.

Example:

Original Query

```
What's the daily food budget?
```

Rewritten Query

```
What is the daily meal reimbursement limit during business travel?
```

The rewritten query is only used if retrieval quality objectively improves.

---

## Rewrite Validation

To prevent semantic drift, every rewritten query undergoes strict validation.

Validation rules include:

- Number preservation
- Currency preservation
- Percentage preservation
- Date preservation
- Entity preservation
- Acronym preservation
- Logical operator preservation
- Polarity preservation

Example:

Original

```
Can employees NOT claim cash gifts?
```

Rejected Rewrite

```
Can employees claim cash gifts?
```

The validator immediately rejects such rewrites.

---

## Rewrite Cache

Repeated rewrites are avoided using an LRU cache.

Cache Key

```
Hash(
    Normalized Query
    +
    Document Collection Hash
)
```

Benefits

- Reduced latency
- Reduced Phi-3 execution
- Deterministic behaviour
- Automatic invalidation on document changes

---

# ๐Ÿ— System Architecture

```
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                        โ”‚       User         โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                  โ”‚
                                  โ–ผ
                     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                     โ”‚     MCP Server         โ”‚
                     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
                               โ–ผ
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                  โ”‚ Intent Detection & Routing  โ”‚
                  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ”‚           โ”‚
                         โ–ผ           โ–ผ
              Standard Pipeline   Summary Pipeline
                         โ”‚
                         โ–ผ
              Comparison / Numeric /
             Boolean / Procedure etc.
                         โ”‚
                         โ–ผ
              Adaptive Retrieval Layer
                         โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚              โ”‚              โ”‚
          โ–ผ              โ–ผ              โ–ผ
    Query Rewriter   Validator      Rewrite Cache
          โ”‚              โ”‚              โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         โ–ผ
                 Final Retrieval
                         โ”‚
                         โ–ผ
              Evidence Validation
                         โ”‚
                         โ–ผ
             Gemini 2.5 Flash Generator
                         โ”‚
                         โ–ผ
              JSON Response + Citations
```

---

# ๐Ÿง  Layered RAG Pipeline

```
Document Upload
       โ”‚
       โ–ผ
Document Parsing
       โ”‚
       โ–ผ
Chunk Generation
       โ”‚
       โ–ผ
Embedding Generation
       โ”‚
       โ–ผ
ChromaDB Storage
       โ”‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
                Runtime
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
       โ”‚
User Query
       โ”‚
       โ–ผ
Intent Detection
       โ”‚
       โ–ผ
Vector Retrieval
       โ”‚
       โ–ผ
Adaptive Retrieval (Optional)
       โ”‚
       โ–ผ
Evidence Validation
       โ”‚
       โ–ผ
LLM Generation
       โ”‚
       โ–ผ
Grounded JSON Response
```

---

# ๐Ÿงฉ Core Components

| Component | Responsibility |
|------------|---------------|
| MCP Server | Exposes the QA tool to MCP clients |
| Intent Router | Routes queries to specialized pipelines |
| Retriever | Retrieves relevant chunks from ChromaDB |
| Query Rewriter | Improves low-confidence semantic retrieval |
| Rewrite Validator | Prevents semantic drift |
| Rewrite Cache | Avoids repeated rewrites |
| Evidence Validator | Ensures answer grounding |
| Gemini Generator | Produces grounded final responses |
| Phi-3 | Local semantic query optimization |

---

# ๐Ÿ“‚ Project Structure

```
modular-rag-system/

โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ company_policy.txt
โ”‚   โ”œโ”€โ”€ conduct_policy.txt
โ”‚   โ”œโ”€โ”€ finance_policy.txt
โ”‚   โ”œโ”€โ”€ it_policy.pdf
โ”‚   โ””โ”€โ”€ remote_work_agreement.pdf
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ answer_question.py
โ”‚   โ”œโ”€โ”€ mcp_server.py
โ”‚   โ”œโ”€โ”€ query_rewriter.py
โ”‚   โ”œโ”€โ”€ retrieve.py
โ”‚   โ”œโ”€โ”€ embed_document.py
โ”‚   โ”œโ”€โ”€ chunk_document.py
โ”‚   โ”œโ”€โ”€ read_document.py
โ”‚   โ”œโ”€โ”€ evidence_engine.py
โ”‚   โ”œโ”€โ”€ layer2_validator.py
โ”‚   โ”œโ”€โ”€ store_embeddings.py
โ”‚   โ”œโ”€โ”€ verify_storage.py
โ”‚   โ”œโ”€โ”€ config.py
โ”‚   โ”œโ”€โ”€ test_acceptance_suite.py
โ”‚   โ”œโ”€โ”€ test_query_rewriter.py
โ”‚   โ””โ”€โ”€ ...
โ”‚
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ .gitignore
```

---

# โš™ Technology Stack

| Category | Technology |
|-----------|------------|
| Language | Python |
| Vector Database | ChromaDB |
| Embedding Model | all-MiniLM-L6-v2 |
| Primary LLM | Gemini 2.5 Flash |
| Local Model | Phi-3 (Ollama) |
| Protocol | Model Context Protocol (MCP) |
| Retrieval | Dense Vector Search |
| Validation | Rule-Based Evidence Validation |
| Environment | VS Code + Python Virtual Environment |

---

# ๐Ÿš€ Installation

Clone the repository

```bash
git clone https://github.com/naffss-eng/modular-rag-system.git

cd modular-rag-system
```

Create a virtual environment

```bash
python -m venv venv
```

Activate the environment

Windows

```bash
venv\Scripts\activate
```

Linux

```bash
source venv/bin/activate
```

Install dependencies

```bash
pip install -r requirements.txt
```

Create a `.env` file

```text
GOOGLE_API_KEY=YOUR_API_KEY
```

# โ–ถ Running the Project

## Step 1 โ€” Ingest Documents

Load and preprocess the documents.

```bash
python src/read_document.py
```

---

## Step 2 โ€” Chunk Documents

Generate semantic chunks.

```bash
python src/chunk_document.py
```

---

## Step 3 โ€” Generate Embeddings

```bash
python src/embed_document.py
```

---

## Step 4 โ€” Store Embeddings

```bash
python src/store_embeddings.py
```

---

## Step 5 โ€” Verify Vector Database

```bash
python src/verify_storage.py
```

---

## Step 6 โ€” Start the MCP Server

```bash
python src/mcp_server.py
```

---

## Step 7 โ€” Connect Using Antigravity CLI

```bash
antigravity connect localhost:8000
```

Once connected, users can directly ask questions over the uploaded document collection.

---

# ๐Ÿ’ฌ Example Queries

### Factual

```
What are the company's core collaboration hours?
```

---

### Numeric

```
What is the daily meal reimbursement limit?
```

---

### Boolean

```
Are cash gifts allowed?
```

---

### Procedure

```
What documents are required to submit a travel reimbursement claim?
```

---

### Summary

```
Give me an overview of the IT Policy.
```

---

### Comparison

```
Compare the Finance Policy with the Company Policy.
```

---

### Multi-document

```
What are the remote work responsibilities mentioned across all documents?
```

---

### Out-of-Scope

```
How do I bake a chocolate cake?
```

Expected behaviour:

```
Grounding Status : REJECTED

Answer:
I could not find this information in the uploaded documents.
```

---

# ๐Ÿ“Š Supported Query Types

| Query Type | Supported |
|------------|-----------|
| Factual | โœ… |
| Numeric | โœ… |
| Boolean | โœ… |
| Procedure | โœ… |
| Summary | โœ… |
| Comparison | โœ… |
| Multi-document | โœ… |
| Out-of-Scope Detection | โœ… |

---

# ๐Ÿ”„ Intent-Aware Processing

Unlike traditional RAG systems that process every query through a single retrieval pipeline, this project dynamically routes each request based on its semantic intent.

```
                 User Query
                      โ”‚
                      โ–ผ
             Intent Classification
                      โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ”‚               โ”‚                โ”‚
      โ–ผ               โ–ผ                โ–ผ
 Summary         Comparison       Standard QA
      โ”‚               โ”‚                โ”‚
      โ–ผ               โ–ผ                โ–ผ
 Dedicated      Balanced Dual     Adaptive
 Pipeline        Retrieval         Retrieval
```

This modular routing significantly improves maintainability while allowing future pipelines to be integrated independently.

---

# ๐Ÿ” Adaptive Retrieval Loop

One of the major architectural enhancements introduced in Backend v2.0 is the Adaptive Retrieval Loop.

Instead of rewriting every query, the system first evaluates retrieval confidence.

```
                    User Query
                         โ”‚
                         โ–ผ
                 Initial Retrieval
                         โ”‚
                         โ–ผ
            Confidence Evaluation
                         โ”‚
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚                             โ”‚
          โ–ผ                             โ–ผ
 High Confidence                 Low Confidence
          โ”‚                             โ”‚
          โ–ผ                             โ–ผ
 Continue                  Query Rewriter (Phi-3)
                                        โ”‚
                                        โ–ผ
                              Rewrite Validator
                                        โ”‚
                                        โ–ผ
                              Rewrite Cache Check
                                        โ”‚
                                        โ–ผ
                              Second Retrieval
                                        โ”‚
                                        โ–ผ
                              Quality Comparison
                                        โ”‚
                         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                         โ–ผ                            โ–ผ
                 Better Retrieval              Worse Retrieval
                         โ”‚                            โ”‚
                         โ–ผ                            โ–ผ
                 Use New Chunks             Keep Original Chunks
```

Only validated improvements are accepted, ensuring retrieval quality never degrades.

---

# ๐Ÿ›ก Grounding & Evidence Validation

Generating an answer is not sufficient; the answer must also be supported by retrieved evidence.

Before returning a response, the system validates whether the retrieved context actually contains sufficient information to answer the question.

Possible grounding outcomes:

| Status | Meaning |
|---------|---------|
| PASSED | Sufficient evidence exists |
| REJECTED | Retrieved evidence is insufficient |

If grounding fails, the system returns a transparent response rather than hallucinating unsupported information.

---

# ๐Ÿ“‘ JSON Response Format

The backend returns structured responses in JSON.

Example:

```json
{
  "answer": "...",
  "grounding_status": "PASSED",
  "constraint_result": "NOT_APPLICABLE",
  "citations": [
      "finance_policy_3"
  ],
  "execution_time_ms": 2943,
  "initialization_time_ms": 0.01
}
```

---

# ๐Ÿ“ˆ Performance Summary

The backend was evaluated using both automated benchmark suites and manual acceptance testing.

## Automated Validation

| Metric | Result |
|---------|---------|
| Benchmark Queries | 340+ |
| Regression Tests | 100% Pass |
| Acceptance Suite | 100% Pass |
| Rewrite Validator Tests | 29/29 Pass |

---

## Manual Validation

A final manual evaluation was conducted using representative enterprise queries covering:

- Factual retrieval
- Numeric reasoning
- Boolean questions
- Summary generation
- Procedure extraction
- Comparison
- Multi-document reasoning
- Out-of-scope detection

**Result**

```
15 / 15 Queries Passed
```

---

# โšก Performance Optimizations

Several optimizations were introduced to balance retrieval quality with runtime efficiency.

### Adaptive Query Rewriting

Only low-confidence retrievals trigger semantic rewriting.

---

### Rewrite Validation

Rejects semantic drift before retrieval.

---

### LRU Rewrite Cache

Avoids repeated Phi-3 inference for identical queries.

---

### Feature Flags

Every Backend v2.0 enhancement is protected behind configuration flags.

```
ENABLE_QUERY_REWRITER = True
```

Rollback is immediate by disabling the feature.

---

# ๐Ÿงช Testing Strategy

Testing was performed at multiple levels.

### Unit Tests

- Query Rewriter
- Rewrite Validator
- Rewrite Cache
- Adaptive Retrieval

---

### Regression Tests

- Preprocessing
- Retrieval
- Empty Queries
- Final Verification

---

### Acceptance Tests

Realistic enterprise policy questions covering every supported intent.

---

### Stress Tests

- Long queries
- Empty input
- Malformed characters
- Out-of-scope requests
- Timeout recovery
- Ollama unavailable
- Cache failures

---

# ๐ŸŽฏ Engineering Decisions

Several important architectural decisions shaped the final system.

### Why Modular Pipelines?

Each query type requires different retrieval and reasoning behaviour. A modular architecture allows independent optimisation of each pipeline.

---

### Why Local Phi-3?

Using Phi-3 locally preserves Gemini API quota while enabling semantic query rewriting entirely offline.

---

### Why Confidence-Based Rewriting?

Most queries already retrieve correct results.

Running an LLM for every query would unnecessarily increase latency.

Adaptive retrieval ensures rewriting occurs only when beneficial.

---

### Why Rewrite Validation?

LLMs occasionally alter numbers, entities, or logical meaning.

The Rewrite Validator prevents such semantic drift before retrieval.

---

### Why Rewrite Cache?

Repeated enterprise questions are common.

Caching validated rewrites reduces latency and eliminates redundant local inference.

---

# ๐Ÿšง Current Limitations

Current limitations include:

- Dense vector retrieval only (no hybrid BM25 retrieval)
- CPU inference latency for local Phi-3
- No web interface (backend only)
- No authentication layer
- Single-session document workspace

---

# ๐Ÿ›ฃ Future Roadmap

### Backend v2.1

- Hybrid Retrieval (Dense + BM25)
- Cross-Encoder Reranking
- Better telemetry dashboard

---

### Backend v3

- Agentic Retrieval
- Tool-calling workflows
- Multi-agent reasoning
- Knowledge graph integration

---

### Frontend

- Streamlit / React interface
- Drag-and-drop document upload
- Citation viewer
- Interactive retrieval visualization
- Chat history

---

# ๐Ÿค Acknowledgements

This project was developed as part of an internship focused on building a modular Retrieval-Augmented Generation system using modern NLP techniques.

It combines dense retrieval, adaptive query rewriting, evidence validation, and Model Context Protocol (MCP) integration into a scalable and extensible architecture.

---

# ๐Ÿ“œ License

This project is intended for educational and research purposes.

---

# ๐Ÿ‘ฉโ€๐Ÿ’ป Author

**Nafisa Hasan**

B.Tech Computer Science Engineering

Special interests:

- Natural Language Processing
- Retrieval-Augmented Generation
- Large Language Models
- Information Retrieval
- Applied Machine Learning

---

# โญ Final Remarks

This project demonstrates how a traditional Retrieval-Augmented Generation pipeline can be transformed into a modular, intent-aware architecture capable of handling diverse enterprise document question-answering tasks with improved retrieval quality, grounded responses, and extensible design.

Rather than relying on a single retrieval strategy, the system incorporates adaptive retrieval, semantic query rewriting, rule-based validation, evidence grounding, and modular routing to improve both reliability and maintainability.

The resulting architecture serves as a strong foundation for future work in hybrid retrieval, agentic workflows, and production-scale document intelligence systems.

---

> *"Good retrieval finds information. Great retrieval understands the question first."*