Adaptive RAG is a production-grade, agentic Retrieval-Augmented Generation system that intelligently routes user queries across three execution paths β vector-store retrieval, live web search, and direct LLM response β based on query relevance. Built with LangGraph, it implements a stateful, self-correcting pipeline that grades retrieved context and rewrites failing queries before falling back to Tavily web search.
Built to be chat-memory-aware, document-upload-ready, and deployment-friendly via a FastAPI backend + Streamlit frontend.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User Query β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββ
β
ββββββββββΌβββββββββ
β Query Classifier β β Groq LLM + Context
ββββββββββ¬βββββββββ
βββββββββββββββββββΌββββββββββββββββββ
β β β
ββββββββΌβββββββ ββββββββΌβββββββ ββββββββΌβββββββ
β Retriever β β General LLM β β (Fallback) β
β (ReAct + β β (Direct) β β β
β Qdrant / β ββββββββ¬βββββββ βββββββββββββββ
β FAISS) β β
ββββββββ¬βββββββ β
β β
ββββββββΌβββββββ β
β Grader β β
ββββββββ¬βββββββ β
Pass β Fail β
β ββββΊ βββββββββΌβββββββ
β β Query Rewrite β
β βββββββββ¬βββββββ
β β
β ββββββββββββββΌβββββββββ
β β Web Search β
β β (Tavily API) β
β ββββββββββββββ¬βββββββββ
β β
ββββββββΌββββββββββββββββββΌβββββββ
β Generator β
βββββββββββββββββ¬βββββββββββββββββ
β
βββββββββΌββββββββ
β Final Answer β
βββββββββββββββββ
| Feature | Description |
|---|---|
| π Adaptive Routing | Classifies each query β vector store, general LLM, or web search |
| π€ ReAct Agent | Uses a reasoning-and-acting agent for smart document retrieval |
| β Context Grader | LLM judges whether retrieved docs actually answer the question |
| π Query Rewriter | Rewrites poor queries to improve retrieval before web fallback |
| π Web Search Fallback | Tavily-powered real-time web search when local knowledge fails |
| π Document Upload | Upload PDFs/TXT at runtime and index them instantly |
| π§ Chat Memory | Persistent conversation history via MongoDB |
| β‘ Dual Vector Stores | Supports both Qdrant (production) and FAISS (local) |
| π¨ Streamlit UI | Login, chat, and document upload β all in one interface |
| π FastAPI Backend | Async REST API ready for production deployment |
Adaptive-Rag/
β
βββ src/ # Core backend package
β βββ api/
β β βββ routes.py # FastAPI route definitions
β βββ config/
β β βββ settings.py # YAML config loader
β β βββ prompts.yaml # All LLM prompts (classify, grade, rewrite, generate)
β βββ core/
β β βββ config.py # App-level configuration constants
β β βββ logger.py # Centralized logging setup
β βββ db/
β β βββ mongo_client.py # MongoDB async client singleton
β βββ llms/
β β βββ groq_llm.py # Groq LLaMA3 client initialization
β βββ memory/
β β βββ chat_history_mongo.py # MongoDB-backed chat history
β β βββ chathistory_in_memory.py # In-memory chat history (dev)
β βββ models/
β β βββ state.py # LangGraph State schema
β β βββ grade.py # Pydantic: relevance grade
β β βββ route_identifier.py # Pydantic: routing decision
β β βββ query_request.py # Pydantic: query API request
β β βββ verification_result.py # Pydantic: verification output
β βββ rag/
β β βββ graph_builder.py # Full LangGraph node & edge definitions
β β βββ nodes.py # Individual node logic stubs
β β βββ retriever_setup.py # Qdrant / FAISS retriever factory
β β βββ document_upload.py # Document ingestion pipeline
β β βββ reAct_agent.py # ReAct agent executor
β βββ tools/
β β βββ common_tools.py # Shared utility tools
β β βββ graph_tools.py # Routing & grading conditional functions
β βββ main.py # FastAPI application entry point
β
βββ streamlit_app/ # Frontend package
β βββ home.py # Login / signup page
β βββ pages/
β β βββ Chat.py # Main chat + document upload interface
β βββ utils/
β βββ api_client.py # HTTP client for FastAPI backend
β
βββ .env.example # Environment variable template
βββ .gitignore # Git exclusions
βββ requirements.txt # Python dependencies
βββ adaptive_RAG.png # Architecture diagram
βββ README.md # This file
git clone https://github.com/ChandaVarshith/Adaptive-Rag.git
cd Adaptive-Ragpython -m venv .venv
# Windows
.venv\Scripts\activate
# macOS/Linux
source .venv/bin/activatepip install -r requirements.txtcp .env.example .env
# Now edit .env and fill in your API keys| Variable | Required | Description |
|---|---|---|
GROQ_API_KEY |
β | Groq API key for LLaMA3 inference |
TAVILY_API_KEY |
β | Tavily API key for web search |
MONGO_URI |
β | MongoDB connection URI |
QDRANT_URL |
Qdrant vector store URL (falls back to FAISS) | |
QDRANT_COLLECTION |
Qdrant collection name |
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000# In a second terminal
streamlit run streamlit_app/home.pyOpen http://localhost:8501 in your browser.
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Health check |
POST |
/rag/query |
Submit a query to the RAG pipeline |
POST |
/rag/documents/upload |
Upload a document (PDF/TXT) |
POST |
/api/init |
Initialize API session |
POST |
/api/create_user |
Create a new user |
POST |
/api/login |
Authenticate a user |
curl -X POST http://localhost:8000/rag/query \
-H "Content-Type: application/json" \
-d '{"query": "What are the main findings in the uploaded document?", "session_id": "user123"}'curl -X POST http://localhost:8000/rag/documents/upload \
-H "X-Description: Research paper on transformer models" \
-F "file=@paper.pdf"The system is built as a stateful directed graph using LangGraph:
START
βββΊ query_analysis # Classify: rag / general / web
βββΊ general_llm ββββββΊ END
βββΊ retriever
β βββΊ grade
β βββΊ generate βββΊ END (docs relevant)
β βββΊ rewrite
β βββΊ retriever (retry with new query)
β βββΊ web_search βββΊ generate βββΊ END
βββΊ (web_search path)
class State(TypedDict):
messages: Annotated[list, add_messages] # Full conversation history
route: str # Routing decision
latest_query: str # Current/rewritten query
binary_score: str # Grader output: "yes" / "no"
rewrite_count: int # Rewrite loop counter| Layer | Technology |
|---|---|
| LLM | Groq (LLaMA 3.3 70B) |
| Orchestration | LangGraph + LangChain |
| Vector Store | Qdrant (cloud/local) + FAISS (fallback) |
| Embeddings | sentence-transformers (HuggingFace) |
| Web Search | Tavily API |
| Backend API | FastAPI + Uvicorn |
| Frontend | Streamlit |
| Chat Memory | MongoDB (Motor async driver) |
| Config | YAML + Pydantic + python-dotenv |
- Python 3.10+
- MongoDB (local or Atlas)
- Qdrant (optional β falls back to FAISS automatically)
- Groq API key (free tier available at console.groq.com)
- Tavily API key (free tier at tavily.com)
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -m 'feat: add your feature') - Push to your branch (
git push origin feature/your-feature) - Open a Pull Request
This project is licensed under the MIT License β see the LICENSE file for details.
Varshith Chanda
- GitHub: @ChandaVarshith
β If you find this project useful, give it a star! β
