You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A production-ready Multi-Agent Retrieval-Augmented Generation (RAG) system built with FastAPI, LangChain, and local LLMs. This project implements a complete enterprise-grade RAG pipeline with 4 specialized agents, caching, streaming, memory, and comprehensive evaluation.
git clone https://github.com/alrezesi/Multi-Agent-RAG.git
cd Multi-Agent-RAG
Step 2: Create Virtual Environment
bash
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python3 -m venv venv
source venv/bin/activate
Step 3: Install Dependencies
bash
pip install -r requirements.txt
Step 4: Set Up Environment Variables
Create a .env file in the project root:
env
# OpenRouter (for Planner Agent - FREE)
OPENROUTER_API_KEY=your_openrouter_api_key
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1
PLANNER_MODEL=nvidia/nemotron-3-ultra-550b-a55b:free
# Redis Cache
REDIS_URL=redis://localhost:6381/0
CACHE_TTL=3600
CACHE_ENABLED=True
# Answer Agent (Gemma 2)
ANSWER_MODEL=google/gemma-2-2b-it
DEVICE=cpu
TEMPERATURE=0.3
MAX_TOKENS=512
USE_4BIT=True
# Embedding Models
EMBEDDING_MODEL=BAAI/bge-m3
CROSS_ENCODER_MODEL=cross-encoder/ms-marco-MiniLM-L-6-v2
# Memory
MAX_HISTORY=10
Step 5: Run Redis (Optional)
bash
# Using Docker
docker run -d --name redis-rag -p 6381:6379 redis:alpine
# Or use existing Redis instance# Update REDIS_URL in .env accordingly
Step 6: Ingest Sample Documents
bash
python scripts/ingest_data.py
Expected output:
text
β Successfully ingested 6 documents.
π Total documents in DB: 6
π Usage
Start the Server
bash
python main.py
API Documentation
Once running, access:
Swagger UI: http://localhost:8000/docs
Health Check: http://localhost:8000/health
Example Request
bash
curl -X POST "http://localhost:8000/v1/chat" \
-H "Content-Type: application/json" \
-d '{ "user_id": "test_user", "session_id": "test_session_1", "text": "What is Artificial Intelligence?", "metadata": {} }'
Streaming Example
bash
curl -X POST "http://localhost:8000/v1/chat/stream" \
-H "Content-Type: application/json" \
-d '{ "user_id": "test_user", "session_id": "test_session_1", "text": "Tell me about RAG systems", "metadata": {} }'
π‘ API Endpoints
Method Endpoint Description
POST /v1/chat Standard chat response
POST /v1/chat/stream Streaming response (SSE)
GET /health System health check
GET / Welcome message
Request Schema
json
{
"user_id": "string", // Required: User identifier
"session_id": "string", // Required: Session identifier
"text": "string", // Required: User query
"metadata": {} // Optional: Additional parameters
}
Response Schema
json
{
"agent_name": "Answer_Gemma2_Local",
"success": true,
"data": "Response text...",
"error": null,
"metadata": {
"source": "gemma_2",
"model": "google/gemma-2-2b-it",
"response_length": 123,
"mode": "local_llm"
}
}
π Evaluation
Metrics
Metric Description Score
Recall@5 Proportion of relevant docs found 1.00
MRR Mean Reciprocal Rank 1.00
Hit Rate@5 At least one relevant doc in top-5 1.00
Precision@5 Proportion of relevant docs in top-5 0.80
Run Evaluation
bash
python scripts/evaluate_system.py
Results are saved to data/evaluation_results.json.
Generate Test Dataset
bash
python scripts/generate_test_dataset.py
Creates 100+ test queries in data/test_dataset_100.json.
π Project Structure
text
Multi-Agent-RAG/
βββ src/
β βββ agents/
β β βββ base.py # Abstract BaseAgent
β β βββ retriever_agent.py # Semantic search
β β βββ planner_agent.py # Document count decision
β β βββ reasoner_agent.py # Dedup, rerank, merge
β β βββ answer_agent_local.py # Gemma 2 response generation
β βββ core/
β β βββ orchestrator.py # Gateway orchestrator
β β βββ vector_store.py # ChromaDB manager
β βββ cache/
β β βββ redis_client.py # Redis cache client
β βββ memory/
β β βββ conversation_memory.py # Conversation history
β βββ api/
β β βββ routes.py # API endpoints
β β βββ streaming.py # SSE utilities
β βββ models/
β β βββ schemas.py # Pydantic models
β βββ evaluation/
β βββ metrics.py # Precision, Recall, MRR, Hit Rate
βββ data/
β βββ chroma_db/ # Vector database
β βββ test_dataset.json # Evaluation dataset
β βββ evaluation_results.json # Metrics results
βββ scripts/
β βββ ingest_data.py # Populate vector DB
β βββ generate_test_dataset.py # Generate 100+ queries
β βββ reduce_to_10.py # Reduce dataset size
β βββ evaluate_system.py # Run evaluation
βββ tests/ # Unit tests
βββ .env # Environment variables
βββ .gitignore # Git ignore file
βββ requirements.txt # Dependencies
βββ main.py # Application entry point
π§ͺ Testing
bash
# Run all tests
pytest tests/
# Run with coverage
pytest --cov=src tests/
π€ Contributing
Contributions are welcome! Please follow these steps:
Fork the repository
Create a feature branch (git checkout -b feature/amazing-feature)
Commit your changes (git commit -m 'Add amazing feature')
Push to the branch (git push origin feature/amazing-feature)
Open a Pull Request
π License
Distributed under the MIT License. See LICENSE for more information.
π Acknowledgements
BAAI/bge-m3 - Embedding model
Google Gemma 2 - Answer model
NVIDIA Nemotron - Planner model
OpenRouter - Free LLM API
LangChain - LLM orchestration
ChromaDB - Vector database
π§ Contact
Alireza Eskandari - GitHub
Project Link: https://github.com/alrezesi/Multi-Agent-RAG
β If you found this project useful, please consider giving it a star!
About
A production-ready Multi-Agent RAG system with 4 specialized agents (Retriever, Planner, Reasoner, Answer), Redis cache, SSE streaming, conversation memory, and comprehensive evaluation. Supports Persian/English queries with local LLM (Gemma 2) and free API (Nemotron 3 Ultra). Built with FastAPI, LangChain, ChromaDB, and BGE-M3 embeddings.