github-actions[bot] commited on
Commit
dfa6a46
Β·
1 Parent(s): db5b6fe

Deploy from GitHub Actions

Browse files
Dockerfile CHANGED
@@ -2,14 +2,16 @@ FROM python:3.11-slim
2
 
3
  WORKDIR /app
4
 
5
- COPY requirements.txt ./
 
6
 
7
- RUN pip install --no-cache-dir -r requirements.txt
 
8
 
9
  COPY . .
10
 
11
- EXPOSE 7860
12
 
13
  ENV PYTHONUNBUFFERED=1
14
 
15
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
 
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
- # RAG Project - Learn with Transformers
11
 
12
- A production-ready Corrective Retrieval-Augmented Generation (CRAG) system built with LangChain, LangGraph, and FastAPI.
13
 
14
- ## Features
 
 
 
 
15
 
16
- - **Intelligent Document Grading**: LLM evaluates retrieved documents for relevance
17
- - **Query Transformation**: Rewrites queries for better retrieval
18
- - **Web Search Fallback**: Tavily API integration when local docs insufficient
19
- - **Advanced Retrieval**: FAISS + FastEmbed + FlashRank reranking
20
- - **Agent Workflow**: LangGraph state machine with conditional routing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  ## Tech Stack
23
 
24
- - **LLM**: Groq (openai/gpt-oss-120b)
25
- - **Embeddings**: FastEmbed (BAAI/bge-small-en-v1.5)
26
- - **Vector Store**: FAISS
27
- - **Reranker**: FlashRank (rank-T5-flan)
28
- - **Framework**: LangChain 0.3.x + LangGraph
29
- - **Web**: FastAPI + Uvicorn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- ## Environment Variables
32
 
33
- Required secrets (set in Space Settings):
34
- - `GROQ_API_KEY`
35
- - `GOOGLE_API_KEY`
36
- - `LANGSMITH_API_KEY` (optional)
37
- - `TAVILY_API_KEY` (optional)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  ## How It Works
40
 
41
- 1. User query β†’ FAISS retrieval with MMR
42
- 2. FlashRank reranking
43
- 3. LLM grades document relevance
44
- 4. If poor quality β†’ Transform query + Web search
45
- 5. Generate answer with Groq LLM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- The app will automatically download "Attention Is All You Need" paper from ArXiv on first run.
 
 
 
 
 
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 langchain_core.documents 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
 
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 langchain_core.documents import Document
3
  from langchain_community.vectorstores import FAISS
4
- from langchain.chains.query_constructor.schema import AttributeInfo
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 langchain_core.documents 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
 
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 langchain_core.documents 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
 
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 langchain_core.documents import Document
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