Spaces:
Running
Running
github-actions[bot] commited on
Commit Β·
dfa6a46
1
Parent(s): db5b6fe
Deploy from GitHub Actions
Browse files- Dockerfile +6 -4
- README.md +173 -34
- project/model/reranking.py +1 -1
- project/model/retriever.py +3 -4
- project/pipeline/agents.py +1 -1
- project/pipeline/rag.py +1 -1
- project/source/data_preparation.py +1 -1
- requirements.txt +1 -1
Dockerfile
CHANGED
|
@@ -2,14 +2,16 @@ FROM python:3.11-slim
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
COPY
|
|
|
|
| 6 |
|
| 7 |
-
RUN pip install --no-cache-dir
|
|
|
|
| 8 |
|
| 9 |
COPY . .
|
| 10 |
|
| 11 |
-
EXPOSE
|
| 12 |
|
| 13 |
ENV PYTHONUNBUFFERED=1
|
| 14 |
|
| 15 |
-
CMD ["
|
|
|
|
| 2 |
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
+
COPY pyproject.toml ./
|
| 6 |
+
COPY requirements.txt* ./
|
| 7 |
|
| 8 |
+
RUN pip install --no-cache-dir uv && \
|
| 9 |
+
uv pip install --system --no-cache -r pyproject.toml
|
| 10 |
|
| 11 |
COPY . .
|
| 12 |
|
| 13 |
+
EXPOSE 8000
|
| 14 |
|
| 15 |
ENV PYTHONUNBUFFERED=1
|
| 16 |
|
| 17 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,47 +1,186 @@
|
|
| 1 |
-
-
|
| 2 |
-
title: RAG Project - Learn with Transformers
|
| 3 |
-
emoji: π€
|
| 4 |
-
colorFrom: purple
|
| 5 |
-
colorTo: blue
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
---
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RAG Project - Learn About Transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
A production-ready **Corrective Retrieval-Augmented Generation (CRAG)** system built with LangChain, LangGraph, and FastAPI. This project implements an intelligent RAG pipeline that not only retrieves relevant documents but also **validates, corrects, and improves** retrieval quality through an agent-based workflow.
|
| 4 |
|
| 5 |
+
## What Makes This Different from Traditional RAG?
|
| 6 |
|
| 7 |
+
### Traditional RAG:
|
| 8 |
+
```
|
| 9 |
+
Query β Retrieve Documents β Generate Answer
|
| 10 |
+
```
|
| 11 |
+
**Problem**: If retrieved documents are irrelevant or low-quality, the answer will be poor.
|
| 12 |
|
| 13 |
+
### This Project (Corrective RAG):
|
| 14 |
+
```
|
| 15 |
+
Query β Retrieve β Grade Quality β Transform Query if Needed β Web Search if Necessary β Generate
|
| 16 |
+
```
|
| 17 |
+
**Solution**: Intelligent agent workflow that **self-corrects** by grading document relevance and taking corrective actions.
|
| 18 |
+
|
| 19 |
+
## Architecture
|
| 20 |
+
|
| 21 |
+
```mermaid
|
| 22 |
+
graph LR
|
| 23 |
+
A[User Query] --> B[Retrieve]
|
| 24 |
+
B --> C[FAISS+MMR]
|
| 25 |
+
C --> D[Rerank]
|
| 26 |
+
D --> E{Grade}
|
| 27 |
+
E -->|Relevant| F[Generate]
|
| 28 |
+
E -->|Partial| G[Filter]
|
| 29 |
+
E -->|Poor| H[Transform]
|
| 30 |
+
G --> F
|
| 31 |
+
H --> I[Web Search]
|
| 32 |
+
I --> F
|
| 33 |
+
F --> J[Groq LLM]
|
| 34 |
+
J --> K[Answer]
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Key Features
|
| 38 |
+
|
| 39 |
+
### 1. **Intelligent Document Grading**
|
| 40 |
+
- LLM evaluates retrieved documents for relevance
|
| 41 |
+
- Filters out low-quality results automatically
|
| 42 |
+
- Ensures only useful context reaches generation
|
| 43 |
+
|
| 44 |
+
### 2. **Query Transformation**
|
| 45 |
+
- Rewrites ambiguous or poor queries
|
| 46 |
+
- Improves retrieval on second attempt
|
| 47 |
+
- Adaptive query refinement
|
| 48 |
+
|
| 49 |
+
### 3. **Web Search Fallback**
|
| 50 |
+
- Tavily API integration for external knowledge
|
| 51 |
+
- Activates when local documents insufficient
|
| 52 |
+
- Combines local + web results
|
| 53 |
+
|
| 54 |
+
### 4. **Advanced Retrieval Stack**
|
| 55 |
+
- **FAISS** vector store with MMR search
|
| 56 |
+
- **FastEmbed** (BAAI/bge-small-en-v1.5) embeddings
|
| 57 |
+
- **FlashRank** (rank-T5-flan) reranking
|
| 58 |
+
- Self-query retriever support
|
| 59 |
+
|
| 60 |
+
### 5. **LangGraph Agent Workflow**
|
| 61 |
+
- State machine orchestration
|
| 62 |
+
- Conditional routing logic
|
| 63 |
+
- Transparent decision-making
|
| 64 |
|
| 65 |
## Tech Stack
|
| 66 |
|
| 67 |
+
| Component | Technology |
|
| 68 |
+
|-----------|------------|
|
| 69 |
+
| **LLM** | Groq (openai/gpt-oss-120b) |
|
| 70 |
+
| **Embeddings** | FastEmbed (BAAI/bge-small-en-v1.5) |
|
| 71 |
+
| **Vector Store** | FAISS |
|
| 72 |
+
| **Reranker** | FlashRank (rank-T5-flan) |
|
| 73 |
+
| **Agent Framework** | LangGraph |
|
| 74 |
+
| **RAG Framework** | LangChain 0.3.x |
|
| 75 |
+
| **Web Search** | Tavily API |
|
| 76 |
+
| **Web Framework** | FastAPI + Uvicorn |
|
| 77 |
+
| **Observability** | LangSmith (optional) |
|
| 78 |
+
| **Document Source** | "Attention Is All You Need" (Transformer paper) |
|
| 79 |
+
|
| 80 |
+
## Project Structure
|
| 81 |
+
|
| 82 |
+
```
|
| 83 |
+
RAG Project/
|
| 84 |
+
βββ project/
|
| 85 |
+
β βββ config/
|
| 86 |
+
β β βββ config.yaml # Model & pipeline configuration
|
| 87 |
+
β βββ logger/
|
| 88 |
+
β β βββ logging.py # Centralized logging
|
| 89 |
+
β βββ exception/
|
| 90 |
+
β β βββ except.py # Custom exception handling
|
| 91 |
+
β βββ utils/
|
| 92 |
+
β β βββ config_loader.py # YAML config loader
|
| 93 |
+
β β βββ model_loader.py # LLM & embedding initialization
|
| 94 |
+
β βββ source/
|
| 95 |
+
β β βββ data_preparation.py # PDF/ArXiv document loading
|
| 96 |
+
β βββ model/
|
| 97 |
+
β β βββ retriever.py # FAISS retriever with MMR
|
| 98 |
+
β β βββ reranking.py # FlashRank reranking
|
| 99 |
+
β βββ prompts/
|
| 100 |
+
β β βββ prompt_template.py # RAG, Router, WebSearch prompts
|
| 101 |
+
β βββ pipeline/
|
| 102 |
+
β βββ rag.py # Core RAG pipeline
|
| 103 |
+
β βββ agents.py # CRAG agent workflow
|
| 104 |
+
βββ templates/
|
| 105 |
+
β βββ index.html # Web UI template
|
| 106 |
+
βββ static/
|
| 107 |
+
β βββ styles.css # Purple gradient theme
|
| 108 |
+
βββ data/
|
| 109 |
+
β βββ attention-is-all-you-need.pdf
|
| 110 |
+
βββ app.py # FastAPI application
|
| 111 |
+
βββ main.py # CLI entry point
|
| 112 |
+
βββ Dockerfile # Docker containerization
|
| 113 |
+
βββ requirements.txt # Dependencies
|
| 114 |
+
|
| 115 |
+
```
|
| 116 |
|
| 117 |
+
## Quick Start
|
| 118 |
|
| 119 |
+
### 1. Clone & Install
|
| 120 |
+
```bash
|
| 121 |
+
git clone https://github.com/Abeshith/RAG-Project-PipeLine.git
|
| 122 |
+
cd RAG-Project-PipeLine
|
| 123 |
+
pip install -r requirements.txt
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
### 2. Set Environment Variables
|
| 127 |
+
Create `.env` file:
|
| 128 |
+
```env
|
| 129 |
+
GROQ_API_KEY=your_groq_api_key
|
| 130 |
+
GOOGLE_API_KEY=your_google_api_key
|
| 131 |
+
LANGSMITH_API_KEY=your_langsmith_key
|
| 132 |
+
TAVILY_API_KEY=your_tavily_key
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
### 3. Run Web Interface
|
| 136 |
+
```bash
|
| 137 |
+
python app.py
|
| 138 |
+
```
|
| 139 |
+
Visit: http://localhost:8000
|
| 140 |
+
|
| 141 |
+
### 4. Run CLI
|
| 142 |
+
```bash
|
| 143 |
+
python main.py
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
## Docker Deployment
|
| 147 |
+
|
| 148 |
+
### Build & Run
|
| 149 |
+
```bash
|
| 150 |
+
docker build -t rag-project .
|
| 151 |
+
docker run -d -p 8000:8000 --env-file .env rag-project
|
| 152 |
+
```
|
| 153 |
|
| 154 |
## How It Works
|
| 155 |
|
| 156 |
+
### Workflow Example
|
| 157 |
+
|
| 158 |
+
**Query**: "What is the attention mechanism in transformers?"
|
| 159 |
+
|
| 160 |
+
1. **Retrieval**: FAISS finds top 3 most similar chunks from "Attention Is All You Need" paper
|
| 161 |
+
2. **Reranking**: FlashRank reorders by relevance (top 3 kept)
|
| 162 |
+
3. **Grading**: LLM evaluates each document:
|
| 163 |
+
- β
Doc 1: Relevant (explains attention)
|
| 164 |
+
- β
Doc 2: Relevant (shows formula)
|
| 165 |
+
- β Doc 3: Not relevant (talks about training data)
|
| 166 |
+
4. **Decision**: 2/3 relevant β Use filtered docs
|
| 167 |
+
5. **Generation**: Groq LLM synthesizes answer from relevant docs
|
| 168 |
+
6. **Output**: Comprehensive answer with LaTeX formulas (rendered via MathJax)
|
| 169 |
+
|
| 170 |
+
### When Retrieval Fails
|
| 171 |
+
|
| 172 |
+
**Query**: "What are the latest improvements to transformers in 2024?"
|
| 173 |
+
|
| 174 |
+
1. **Retrieval**: Finds documents from 2017 paper
|
| 175 |
+
2. **Grading**: β All documents marked "not relevant" (outdated info)
|
| 176 |
+
3. **Transform**: Rewrites query β "Recent transformer architecture improvements 2024"
|
| 177 |
+
4. **Web Search**: Tavily searches current web content
|
| 178 |
+
5. **Generation**: Answer combines paper fundamentals + recent developments
|
| 179 |
+
|
| 180 |
+
## Web Interface Features
|
| 181 |
|
| 182 |
+
- **Modern UI**: Purple gradient design with responsive layout
|
| 183 |
+
- **MathJax Integration**: Renders LaTeX formulas beautifully
|
| 184 |
+
- **Transformer Visualization**: Architecture diagram in header
|
| 185 |
+
- **Real-time Search**: Fast async FastAPI backend
|
| 186 |
+
- **Error Handling**: Graceful degradation with user-friendly messages
|
project/model/reranking.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from typing import List
|
| 2 |
-
from
|
| 3 |
from flashrank.Ranker import Ranker, RerankRequest
|
| 4 |
from project.utils.config_loader import load_config
|
| 5 |
from project.logger.logging import get_logger
|
|
|
|
| 1 |
from typing import List
|
| 2 |
+
from langchain.schema import Document
|
| 3 |
from flashrank.Ranker import Ranker, RerankRequest
|
| 4 |
from project.utils.config_loader import load_config
|
| 5 |
from project.logger.logging import get_logger
|
project/model/retriever.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
from typing import List, Optional
|
| 2 |
-
from
|
| 3 |
from langchain_community.vectorstores import FAISS
|
| 4 |
-
from langchain.chains.query_constructor.
|
| 5 |
-
from langchain.retrievers import SelfQueryRetriever
|
| 6 |
from project.utils.model_loader import ModelLoader
|
| 7 |
from project.utils.config_loader import load_config
|
| 8 |
from project.logger.logging import get_logger
|
|
@@ -91,4 +91,3 @@ class DocumentRetriever:
|
|
| 91 |
|
| 92 |
logger.info(f"Base retriever configured with {search_type} search")
|
| 93 |
return self.retriever
|
| 94 |
-
|
|
|
|
| 1 |
from typing import List, Optional
|
| 2 |
+
from langchain.schema import Document
|
| 3 |
from langchain_community.vectorstores import FAISS
|
| 4 |
+
from langchain.chains.query_constructor.base import AttributeInfo
|
| 5 |
+
from langchain.retrievers.self_query.base import SelfQueryRetriever
|
| 6 |
from project.utils.model_loader import ModelLoader
|
| 7 |
from project.utils.config_loader import load_config
|
| 8 |
from project.logger.logging import get_logger
|
|
|
|
| 91 |
|
| 92 |
logger.info(f"Base retriever configured with {search_type} search")
|
| 93 |
return self.retriever
|
|
|
project/pipeline/agents.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
from typing import List, Literal
|
| 3 |
from typing_extensions import TypedDict
|
| 4 |
from pydantic import BaseModel, Field
|
| 5 |
-
from
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
from langgraph.graph import END, StateGraph, START
|
| 8 |
from project.pipeline.rag import RAGPipeline
|
|
|
|
| 2 |
from typing import List, Literal
|
| 3 |
from typing_extensions import TypedDict
|
| 4 |
from pydantic import BaseModel, Field
|
| 5 |
+
from langchain.schema import Document
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
from langgraph.graph import END, StateGraph, START
|
| 8 |
from project.pipeline.rag import RAGPipeline
|
project/pipeline/rag.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from typing import List, Dict, Any
|
| 2 |
-
from
|
| 3 |
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
from langchain_core.runnables import RunnablePassthrough
|
| 5 |
from project.source.data_preparation import DataPreparation
|
|
|
|
| 1 |
from typing import List, Dict, Any
|
| 2 |
+
from langchain.schema import Document
|
| 3 |
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
from langchain_core.runnables import RunnablePassthrough
|
| 5 |
from project.source.data_preparation import DataPreparation
|
project/source/data_preparation.py
CHANGED
|
@@ -3,7 +3,7 @@ from pathlib import Path
|
|
| 3 |
from typing import List, Optional
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader, ArxivLoader
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
-
from
|
| 7 |
from project.logger.logging import get_logger
|
| 8 |
|
| 9 |
logger = get_logger(__name__)
|
|
|
|
| 3 |
from typing import List, Optional
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader, ArxivLoader
|
| 5 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain.schema import Document
|
| 7 |
from project.logger.logging import get_logger
|
| 8 |
|
| 9 |
logger = get_logger(__name__)
|
requirements.txt
CHANGED
|
@@ -13,9 +13,9 @@ langchain-google-genai>=2.0.5
|
|
| 13 |
langchain-groq>=0.2.0
|
| 14 |
langgraph>=0.2.0
|
| 15 |
pypdf>=6.4.0
|
|
|
|
| 16 |
python-dotenv>=1.2.1
|
| 17 |
python-multipart>=0.0.20
|
| 18 |
rapidocr-onnxruntime>=1.4.4
|
| 19 |
tiktoken>=0.12.0
|
| 20 |
uvicorn>=0.34.0
|
| 21 |
-
pymupdf
|
|
|
|
| 13 |
langchain-groq>=0.2.0
|
| 14 |
langgraph>=0.2.0
|
| 15 |
pypdf>=6.4.0
|
| 16 |
+
pymupdf
|
| 17 |
python-dotenv>=1.2.1
|
| 18 |
python-multipart>=0.0.20
|
| 19 |
rapidocr-onnxruntime>=1.4.4
|
| 20 |
tiktoken>=0.12.0
|
| 21 |
uvicorn>=0.34.0
|
|
|