Skip to content

Rajdip1/Adaptive_RAG_System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adaptive RAG - Agentic AI Chatbot

Python 3.9+ FastAPI LangGraph Qdrant

📋 Overview

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.


🎯 Key Features

🧠 Intelligent Query Routing

  • 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

📚 Advanced RAG Pipeline

  • 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

🤖 Agentic AI Architecture

  • 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

💾 State Management

  • MongoDB Backend: Persistent chat history and session management
  • Session Tracking: Individual conversation contexts per user
  • Memory Management: Full conversation context retention

🎨 User Interface

  • Streamlit Web App: Interactive chat interface with document upload
  • File Support: PDF and TXT document uploads
  • Real-time Feedback: Live chat with instant responses

⚡ API-First Architecture

  • FastAPI Backend: High-performance REST API
  • Async Operations: Non-blocking database and API calls
  • RESTful Endpoints: Well-defined API contracts

🏗️ Architecture

System Components

┌─────────────────────────────────────────────────────────────────┐
│                         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               │
            └─────────────────────────────────┘

Graph Nodes

  1. query_analysis: Analyzes and classifies incoming queries
  2. retriever: Retrieves relevant documents from vector store
  3. grade: Evaluates relevance of retrieved documents
  4. rewrite: Optimizes query for better retrieval results
  5. generate: Generates final response from context
  6. web_search: Performs real-time web search when needed
  7. general_llm: Provides general knowledge answers

📦 Project Structure

├── 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

Generated by FileTree Pro Extension

🔌 API Endpoints

Base URL

http://localhost:8000

1. Query Endpoint

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 query
  • session_id (string, required): Unique session identifier for conversation tracking

Status Codes:

  • 200: Success
  • 400: Invalid request format
  • 500: Server error

2. Document Upload Endpoint

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 indexed
  • 400: Invalid file type or missing description
  • 500: Processing error

📖 Usage Guide

1. Prerequisites

# System Requirements
- Python 3.9 or higher
- MongoDB (local or cloud)
- Qdrant vector database
- OpenAI API key
- Tavily API key (for web search)

2. Installation

# 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.txt

3. Environment Configuration

Create 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_rag

4. Running the Application

Start FastAPI Backend:

# Terminal 1: Run FastAPI server
python -m uvicorn src.main:app --reload --host 0.0.0.0 --port 8000

Start Streamlit Frontend:

# Terminal 2: Run Streamlit app
streamlit run web/streamlit_app/home.py

Access the Application:

5. Example Usage

Using the Web Interface:

  1. Navigate to http://localhost:8501
  2. Create account or login
  3. Upload documents in the sidebar
  4. 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())

🔧 Configuration

Key Configuration Files

config/settings.py

# 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 database

config/prompts.yaml

Contains 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

Query Routing Logic

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)

🧪 Testing the API

Using FastAPI Interactive Documentation

  1. Navigate to http://localhost:8000/docs
  2. Expand endpoint sections
  3. Click "Try it out"
  4. Enter test data
  5. Click "Execute"

Example Test Cases

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"
}

🔐 Security Considerations

  • Store API keys in .env file (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

🚀 Deployment

Local Development

# Run development server with auto-reload
python -m uvicorn src.main:app --reload

Production Deployment

# Run with production settings
python -m uvicorn src.main:app --host 0.0.0.0 --port 8000 --workers 4

Docker Support (Optional)

Create Dockerfile and docker-compose.yml for containerized deployment.


📊 Performance Optimization

  • 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

🤝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/YourFeature)
  3. Make changes following CODE_STYLE_GUIDE.md
  4. Commit with descriptive messages (git commit -m 'feat: Add YourFeature')
  5. Push to your branch (git push origin feature/YourFeature)
  6. Open a Pull Request

Code Quality

  • Follow PEP 8 standards
  • Add docstrings to all functions
  • Write unit tests for new features
  • Update documentation
  • Run linting: flake8 src/

📚 Technology Stack

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

📝 Documentation References


❓ FAQ

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.


💬 Support & Contact

For issues, questions, or suggestions:

  • Open an Issue
  • Check existing documentation
  • Review the code comments

🙏 Acknowledgments

  • 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

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


👤 Author

Rajdip Solanki


📈 Project Status

  • ✅ 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

🗺️ Roadmap

  • 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

About

🤖 Adaptive Retrieval-Augmented Generation (RAG) system built with LangChain, LangGraph, FastAPI, Qdrant, and MongoDB. Features intelligent query routing, vector search, conversational memory, document ingestion, and LLM-powered question answering.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages