Adaptive RAG is an intelligent, end-to-end Retrieval-Augmented Generation (RAG) system powered by agentic AI architecture. It combines dynamic query routing, intelligent document retrieval, and advanced LLM capabilities to provide accurate, context-aware answers to user queries.
The system intelligently adapts its retrieval strategy based on query type, utilizing indexed documents, general knowledge, or real-time web search to generate comprehensive responses. Built with a modular architecture using LangGraph for workflow orchestration and multiple storage backends for scalability.
- Adaptive Classification: Automatically routes queries to the most appropriate processing pipeline
- Three Query Types:
- Index: Queries answerable from uploaded documents
- General: Queries answerable with general knowledge
- Search: Queries requiring real-time web search
- Document Processing: Intelligent chunking and embedding of documents
- Vector Search: Fast similarity-based retrieval using Qdrant
- Relevance Grading: Automatic evaluation of retrieved documents
- Query Rewriting: Optimizes queries for better retrieval results
- Multi-Agent System: Specialized agents for different tasks
- ReAct Framework: Reasoning and Acting pattern for intelligent decision-making
- Tool Integration: Seamless integration with retrieval tools and web search
- MongoDB Backend: Persistent chat history and session management
- Session Tracking: Individual conversation contexts per user
- Memory Management: Full conversation context retention
- Streamlit Web App: Interactive chat interface with document upload
- File Support: PDF and TXT document uploads
- Real-time Feedback: Live chat with instant responses
- FastAPI Backend: High-performance REST API
- Async Operations: Non-blocking database and API calls
- RESTful Endpoints: Well-defined API contracts
┌─────────────────────────────────────────────────────────────────┐
│ User Interface │
│ ┌────────────── ─────────────────────────────────────── ───┐ │
│ │ Streamlit Web Application │ │
│ │ • Chat Interface │ │
│ │ • Document Upload (PDF, TXT) │ │
│ │ • Session Management │ │
│ └────────────────────────────────────────────────────────── │
└─────────────────────────────────────────── ─────────────────────┘
↓
┌──────────────────────────────────────────────── ────────────────┐
│ FastAPI Backend │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ REST API Endpoints │ │
│ │ • POST /rag/query │ │
│ │ • POST /rag/documents/upload │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ LangGraph Orchestration │
│ ┌─────────┐ ┌──────────┐ ┌─────────┐ ┌──────────┐ │
│ │ Query │→ │ Classify │→ │ Router │→ │ Pipeline │ │
│ │ Analyze │ │ Query │ │ Output │ │ Exec │ │
│ └─────────┘ └──────────┘ └─── ─────┘ └──────────┘ │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────── ──────────┬──────────────── ─┬────────────────┐
↓ ↓ ↓ ↓
┌─────────┐ ┌──────────┐ ┌────────────┐ ┌──────────┐
│ Retriever│ │ General │ │ Web Search │ │ Response │
│ (Index) │ │ LLM │ │ (Tavily) │ │ Generator│
└─────────┘ └──────────┘ └────────────┘ └──────────┘
↓ ↓ ↓ ↓
└──────────────────┬──────────────────┬────────────────┘
↓
┌─────────────────────────────────┐
│ Response to User │
└─────────────────────────────────┘
- query_analysis: Analyzes and classifies incoming queries
- retriever: Retrieves relevant documents from vector store
- grade: Evaluates relevance of retrieved documents
- rewrite: Optimizes query for better retrieval results
- generate: Generates final response from context
- web_search: Performs real-time web search when needed
- general_llm: Provides general knowledge answers
├── guide_docs
│ ├── CODE_STYLE_GUIDE.md
│ ├── DOCUMENTATION_INDEX.md
│ ├── DOCUMENT_FLOW_VISUAL.md
│ ├── DOCUMENT_UPLOAD_FLOW.md
│ ├── QDRANT_SETUP_GUIDE.md
│ ├── QUICK_REFERENCE.md
│ └── description.txt
├── images
│ └── adaptive_RAG.png
├── src
│ ├── api
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── config
│ │ ├── __init__.py
│ │ ├── prompts.yaml
│ │ └── settings.py
│ ├── core
│ │ ├── __init__.py
│ │ ├── config.py
│ │ └── logger.py
│ ├── db
│ │ ├── __init__.py
│ │ └── mongo_client.py
│ ├── llms
│ │ ├── __init__.py
│ │ ├── gemini.py
│ │ ├── groq.py
│ │ ├── huggingface.py
│ │ └── openai.py
│ ├── memory
│ │ ├── __init__.py
│ │ ├── chat_history_mongo.py
│ │ └── chathistory_in_memory.py
│ ├── models
│ │ ├── __init__.py
│ │ ├── grade.py
│ │ ├── query_request.py
│ │ ├── route_identifier.py
│ │ ├── state.py
│ │ └── verification_result.py
│ ├── rag
│ │ ├── __init__.py
│ │ ├── adaptive_RAG.png
│ │ ├── document_upload.py
│ │ ├── graph_builder.py
│ │ ├── nodes.py
│ │ ├── reAct_agent.py
│ │ └── retriever_setup.py
│ ├── tools
│ │ ├── __init__.py
│ │ ├── common_tools.py
│ │ └── graph_tools.py
│ ├── __init__.py
│ ├── main.py
│ └── requirements.txt
├── web
│ └── streamlit_app
│ ├── pages
│ │ ├── __init__.py
│ │ └── chat.py
│ ├── utils
│ │ ├── __init__.py
│ │ └── api_client.py
│ ├── __init__.py
│ ├── home.py
│ └── requirements_frontend.txt
├── .gitignore
├── README.md
├── description.txt
└── test.py
http://localhost:8000
Process a RAG query and get intelligent response
POST /rag/query
Content-Type: application/json
{
"query": "What is the main topic of the document?",
"session_id": "user_session_123"
}Response:
{
"result": {
"type": "ai",
"content": "Based on the document, the main topic is..."
}
}Parameters:
query(string, required): User's question or querysession_id(string, required): Unique session identifier for conversation tracking
Status Codes:
200: Success400: Invalid request format500: Server error
Upload documents for RAG indexing
POST /rag/documents/upload
X-Description: Brief description of the document
Form Data:
- file: <PDF or TXT file>Response:
{
"status": true
}Headers:
X-Description(string, required): Document description for context
Parameters:
file(file, required): PDF or TXT file to upload (max size: depends on system)
Supported Formats:
- PDF (.pdf)
- Plain Text (.txt)
Status Codes:
200: Successfully uploaded and indexed400: Invalid file type or missing description500: Processing error
# System Requirements
- Python 3.9 or higher
- MongoDB (local or cloud)
- Qdrant vector database
- OpenAI API key
- Tavily API key (for web search)# Clone the repository
git clone https://github.com/Rajdip1/Adaptive_RAG_System.git
cd Adaptive_RAG_System
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate a .env file in the project root:
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key_here
# Tavily Search Configuration
TAVILY_API_KEY=your_tavily_api_key_here
# Qdrant Configuration
QDRANT_URL=http://localhost:6333
QDRANT_API_KEY=your_qdrant_api_key
QDRANT_CODE_COLLECTION=code_documents
QDRANT_DOCS_COLLECTION=documents
# MongoDB Configuration
MONGODB_URL=mongodb://localhost:27017
MONGODB_DB_NAME=adaptive_ragStart FastAPI Backend:
# Terminal 1: Run FastAPI server
python -m uvicorn src.main:app --reload --host 0.0.0.0 --port 8000Start Streamlit Frontend:
# Terminal 2: Run Streamlit app
streamlit run web/streamlit_app/home.pyAccess the Application:
- Web Interface: http://localhost:8501
- API Documentation: http://localhost:8000/docs
- ReDoc Documentation: http://localhost:8000/redoc
Using the Web Interface:
- Navigate to http://localhost:8501
- Create account or login
- Upload documents in the sidebar
- Start chatting in the main chat area
Using cURL:
# Upload a document
curl -X POST http://localhost:8000/rag/documents/upload \
-H "X-Description: Sample document about Python" \
-F "file=@document.pdf"
# Query the RAG system
curl -X POST http://localhost:8000/rag/query \
-H "Content-Type: application/json" \
-d '{
"query": "Tell me about Python",
"session_id": "user_123"
}'Using Python:
import requests
# Query endpoint
response = requests.post(
"http://localhost:8000/rag/query",
json={
"query": "What is Python?",
"session_id": "user_123"
}
)
print(response.json())# Core application settings loaded from environment
OPENAI_API_KEY # OpenAI API authentication
TAVILY_API_KEY # Web search functionality
QDRANT_URL # Vector database endpoint
QDRANT_API_KEY # Vector database authentication
MONGODB_URL # Chat history databaseContains system prompts for:
- system_prompt: ReAct agent system instructions
- classify_prompt: Query classification logic
- grading_prompt: Document relevance evaluation
- rewrite_prompt: Query optimization
- generate_prompt: Response generation
The system routes queries based on classification:
Query Classification
├── "index" → Use retriever (indexed documents)
├── "general" → Use general LLM (common knowledge)
└── "search" → Use web search (real-time information)
- Navigate to http://localhost:8000/docs
- Expand endpoint sections
- Click "Try it out"
- Enter test data
- Click "Execute"
Test 1: Simple Query
{
"query": "Hello, how are you?",
"session_id": "test_user_1"
}Test 2: Document-Based Query
{
"query": "What topics are covered in the uploaded document?",
"session_id": "test_user_1"
}Test 3: General Knowledge Query
{
"query": "What is machine learning?",
"session_id": "test_user_1"
}- Store API keys in
.envfile (never commit) - Use environment variables for sensitive data
- Implement rate limiting for production
- Validate all user inputs
- Use HTTPS in production
- Implement authentication/authorization
- Secure MongoDB with proper credentials
# Run development server with auto-reload
python -m uvicorn src.main:app --reload# Run with production settings
python -m uvicorn src.main:app --host 0.0.0.0 --port 8000 --workers 4Create Dockerfile and docker-compose.yml for containerized deployment.
- Document Chunking: Configurable chunk size (1000 chars, 150 overlap)
- Vector Search: Efficient similarity search with Qdrant
- Async Operations: Non-blocking I/O for better throughput
- Caching: Query results cached when applicable
- Batch Processing: Document processing in batches
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/YourFeature) - Make changes following CODE_STYLE_GUIDE.md
- Commit with descriptive messages (
git commit -m 'feat: Add YourFeature') - Push to your branch (
git push origin feature/YourFeature) - Open a Pull Request
- Follow PEP 8 standards
- Add docstrings to all functions
- Write unit tests for new features
- Update documentation
- Run linting:
flake8 src/
| Component | Technology | Version |
|---|---|---|
| LLM Framework | LangChain | ~0.3.27 |
| Workflow Orchestration | LangGraph | ~0.5.4 |
| Web Framework | FastAPI | Latest |
| ASGI Server | Uvicorn | Latest |
| UI Framework | Streamlit | Latest |
| Vector Database | Qdrant/FAISS | Latest |
| Chat Database | MongoDB/InMemory | Latest |
| Document Processing | LangChain Community | ~0.3.27 |
| LLM Provider | OpenAI | ~0.3.28 |
| Web Search | Tavily | Latest |
| Async DB | Motor | Latest |
| Data Validation | Pydantic | ~2.11.7 |
- CODE_STYLE_GUIDE.md - Comprehensive coding standards
- QUICK_REFERENCE.md - Quick patterns and templates
- README_FORMATTING.md - Code formatting overview
- VERIFICATION_CHECKLIST.md - QA checklist
- DOCUMENTATION_INDEX.md - Full documentation index
Q: How do I upload multiple documents?
A: Upload one document at a time through the Streamlit interface. Each upload creates a new indexed collection.
Q: What's the maximum file size?
A: Limited by system memory and Qdrant storage. Typical limit is 100MB per file.
Q: Can I use different LLM providers?
A: Currently configured for Groq. You can modify src/llms/openai.py to use other providers.
Q: How is conversation history stored?
A: MongoDB stores all chat messages with timestamps and session IDs for full context retention.
Q: Can I run this without web search?
A: Yes, remove Tavily dependency. Queries will use index or general LLM only.
For issues, questions, or suggestions:
- Open an Issue
- Check existing documentation
- Review the code comments
- Built with LangChain and LangGraph
- Vector search powered by Qdrant
- LLM capabilities by OpenAI
- Web search by Tavily
- UI powered by Streamlit
- Thanks to the open-source community
This project is licensed under the MIT License - see the LICENSE file for details.
Rajdip Solanki
- GitHub: @Rajdip1
- Project: Adaptive RAG System
- ✅ Core RAG pipeline implemented
- ✅ Document upload and indexing
- ✅ Query routing (index/general/search)
- ✅ MongoDB chat history
- ✅ Streamlit web interface
- ✅ Code formatted and documented
- 🚀 Production ready
- Enhanced context management
- Multi-language support
- Performance benchmarks
- Extended LLM provider support
- Advanced authentication
- Real-time collaboration
- Analytics dashboard
- Cost optimization
Last Updated: March 10, 2026
Status: ✅ Production Ready
Documentation: ✅ Comprehensive