An AI-powered chatbot that answers questions about Islam, Quran, and Hadith using RAG (Retrieval-Augmented Generation) and a Multi-Agent System with persistent conversation memory.
- π€ Multi-Agent System - 3 specialized agents for classification, generation, and validation
- π RAG Implementation - Retrieves relevant information from Islamic texts (PDFs & CSVs)
- πΎ Persistent Conversation Memory - Maintains context across conversations with session support
- π Iterative Refinement - Up to 3 iterations to improve response quality
- π REST API with Sessions - FastAPI-based API with multi-session support
- π Multi-lingual Support - Handles Arabic, Urdu, and English content
- π― Smart Classification - Filters non-Islamic queries
- π Session Management - Independent conversation history per user session
User Query β Agent 1 (Classifier) β Agent 2 (Generator) β Agent 3 (Validator) β User
β β
βββββ Feedback Loop ββββββ
(max 3 iterations)
Session Memory: Stores conversation history per session
| Agent | Role | Description |
|---|---|---|
| Agent 1 | Classifier | Determines if the question is related to Islamic topics |
| Agent 2 | Generator | Retrieves relevant context and generates responses using RAG with memory context |
| Agent 3 | Validator | Validates response quality and provides feedback for improvement |
- ConversationManager - Maintains conversation history with HumanMessage and AIMessage objects
- Session-Based Storage - Each API session gets its own independent memory
- Iteration Tracking - Records all refinement iterations per question
- History Retrieval - Access full or recent conversation history
Before you begin, ensure you have the following:
- Python 3.8 or higher
- Ollama installed and running
- At least 4GB RAM available
- 2-3GB disk space for models
cd islamic_rag_chatbotWindows:
python -m venv .venv
.venv\Scripts\activateMac/Linux:
python3 -m venv .venv
source .venv/bin/activatepip install -r requirements.txt# Install Ollama from https://ollama.ai
# Pull required models
ollama pull gemma3:1b # Chat model
ollama pull nomic-embed-text # Embedding modelKeep this running in a separate terminal:
ollama serve# Create data directories
mkdir -p data/pdfs data/csvs
# Add your Islamic content:
# - PDF files (Hadith, Islamic books) β data/pdfs/
# - CSV files (Quran translations) β data/csvs/islamic_rag_chatbot/
βββ agents/
β βββ __init__.py
β βββ classifier_agent.py # Agent 1: Question classification
β βββ generator_agent.py # Agent 2: Response generation with memory
β βββ validator_agent.py # Agent 3: Response validation
β
βββ utils/
β βββ __init__.py
β βββ document_loader.py # PDF & CSV loading
β βββ vector_store.py # ChromaDB vector store
β βββ memory.py # Conversation memory manager
β
βββ data/
β βββ pdfs/ # Islamic PDF documents
β βββ csvs/ # Quran CSV data
β
βββ vectorstore/ # Auto-generated embeddings
β
βββ main.py # Command-line interface with memory
βββ api.py # FastAPI REST API with session management
βββ requirements.txt
βββ .gitignore
βββ README.md
Start the chatbot in interactive mode with persistent memory:
python main.py| Command | Description |
|---|---|
| Type your question | Ask anything about Islam - memory is maintained |
history |
View full conversation history |
quit or exit |
Exit the chatbot |
You: What are the 5 pillars of Islam?
π€ Assistant: The five pillars of Islam are...
You: Tell me more about Salah
π€ Assistant: Salah (prayer) is one of the five pillars you asked about. It is...
You: history
π History:
You: What are the 5 pillars of Islam?
Assistant: The five pillars of Islam are...
You: Tell me more about Salah
Assistant: Salah (prayer) is one of the five pillars you asked about...
Start the API server:
uvicorn api:app --host 0.0.0.0 --port 8000 --reloadOr simply:
python api.pyInteractive API Documentation:
Open http://localhost:8000/docs in your browser
Check if the API is running:
GET /health
Response:
{
"status": "healthy",
"message": "Islamic Knowledge Chatbot is running",
"models": {
"chat_model": "gemma3:1b",
"embedding_model": "nomic-embed-text"
}
}Create a new chat session with independent memory:
POST /session/new
Response:
{
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"created_at": "2024-01-15T10:30:00"
}Send a message and get a response (memory is automatically maintained):
POST /chat
Body:
{
"message": "What is Ramadan?",
"session_id": "550e8400-e29b-41d4-a716-446655440000" (optional, generates new if not provided)
}
Response:
{
"response": "Ramadan is the ninth month of the Islamic calendar...",
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"is_relevant": true,
"timestamp": "2024-01-15T10:35:00"
}Multi-turn Example:
# First message
POST /chat
{
"message": "What is Salah?",
"session_id": "550e8400-e29b-41d4-a716-446655440000"
}
# Second message (chatbot remembers the first question)
POST /chat
{
"message": "How many times should I perform it daily?",
"session_id": "550e8400-e29b-41d4-a716-446655440000"
}
# Response will reference the previous Salah discussionRetrieve full conversation history for a session:
GET /history/{session_id}
Response:
{
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"history": "You: What is Ramadan?\n\nAssistant: Ramadan is...\n\nYou: Why is fasting important?\n\nAssistant: Fasting in Ramadan is important because...",
"message_count": 4
}Get all active sessions with their message counts:
GET /sessions
Response:
{
"total_sessions": 3,
"sessions": [
{
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"created_at": "2024-01-15T10:30:00",
"message_count": 6
},
{
"session_id": "660e8400-e29b-41d4-a716-446655440001",
"created_at": "2024-01-15T11:00:00",
"message_count": 2
}
]
}Get detailed information about a specific session:
GET /session/{session_id}/info
Response:
{
"session_id": "550e8400-e29b-41d4-a716-446655440000",
"created_at": "2024-01-15T10:30:00",
"message_count": 6,
"iteration_count": 3,
"status": "active"
}Delete a specific session and its memory:
DELETE /session/clear/{session_id}
Response:
{
"message": "Session 550e8400-e29b-41d4-a716-446655440000 cleared successfully"
}- Session Creation - Each new chat creates a
ConversationManagerinstance - Message Storage - Questions and answers are stored as
HumanMessageandAIMessageobjects - Context Retrieval - Recent conversation history is automatically included when generating responses
- Iteration Tracking - All refinement iterations are recorded for analysis
- Independent Storage - Each session maintains its own separate memory
Session = {
"session_id": "uuid",
"created_at": "timestamp",
"conversation_manager": {
"messages": [HumanMessage(...), AIMessage(...), ...],
"iteration_history": [
{"iteration": 1, "response": "...", "approved": false, "feedback": "..."},
{"iteration": 2, "response": "...", "approved": true, "feedback": "..."}
]
}
}Via CLI:
You: history
# Shows all messages in the current sessionVia API:
GET /history/{session_id}
# Returns formatted conversation historyEdit in main.py or api.py:
chatbot = IslamicRAGChatbot(
pdf_dir="./data/pdfs",
csv_dir="./data/csvs",
vectorstore_dir="./vectorstore",
max_iterations=3, # Max refinement iterations
chat_model="gemma3:1b", # Chat model
embedding_model="nomic-embed-text" # Embedding model
)Edit in utils/memory.py:
class ConversationManager:
def __init__(self):
self.messages = [] # Stores HumanMessage and AIMessage objects
self.iteration_history = [] # Tracks refinement iterationsEdit in utils/document_loader.py:
DocumentProcessor(
chunk_size=1500, # Larger = fewer chunks, less detail
chunk_overlap=200 # Overlap between chunks
)import requests
# Create a new session
response = requests.post("http://localhost:8000/session/new")
session_id = response.json()["session_id"]
print(f"Session created: {session_id}")
# First message
response = requests.post(
"http://localhost:8000/chat",
json={
"message": "What are the 5 pillars of Islam?",
"session_id": session_id
}
)
print(response.json()["response"])
# Second message (memory is maintained)
response = requests.post(
"http://localhost:8000/chat",
json={
"message": "Tell me more about Salah",
"session_id": session_id
}
)
print(response.json()["response"])
# Get conversation history
response = requests.get(f"http://localhost:8000/history/{session_id}")
print(response.json()["history"])
# List all sessions
response = requests.get("http://localhost:8000/sessions")
print(response.json())# Create session
http POST http://localhost:8000/session/new
# Send message
http POST http://localhost:8000/chat \
message="What is Ramadan?" \
session_id="your-session-id"
# Get history
http GET http://localhost:8000/history/your-session-id
# List sessions
http GET http://localhost:8000/sessions| Task | Duration |
|---|---|
| PDF Processing (2800 pages) | ~2-5 minutes |
| CSV Processing (6000+ verses) | ~30 seconds |
| Embedding Creation | ~30-60 minutes (one-time only) |
| Vector Store Size | ~500MB-1GB |
| Metric | Value |
|---|---|
| Loading Time | 5-10 seconds |
| Query Response | 5-15 seconds |
| Memory Usage | ~2-3GB RAM |
| Session Memory Overhead | ~1-5KB per message |
- β Hadith collections (Sahih Bukhari, Muslim, Tirmidhi, etc.)
- β Islamic books and texts
- β Scholar writings
β οΈ Some PDFs may fail due to encoding issues
- β Quran translations (Arabic, Urdu, English)
- β
Format:
verse_num, surah, ..., arabic, urdu, english - β Handles UTF-8 and other encodings
User: What is Zakat?
Bot: Zakat is one of the five pillars of Islam...
User: What are the conditions for paying Zakat?
Bot: Based on our discussion about Zakat, the conditions are...
User: How does this relate to Ramadan?
Bot: While we were discussing Zakat, you might be interested to know that many Muslims pay their annual Zakat during Ramadan because...
User: Tell me about Prophet Muhammad
Bot: Prophet Muhammad was born in Mecca...
User: Who were his companions?
Bot: Some of Prophet Muhammad's most notable companions were...
User: What did they do after his death?
Bot: After Prophet Muhammad's death, his companions continued to spread his teachings...
Error:
ERROR: Cannot connect to Ollama!
Solution:
Make sure Ollama is running in a separate terminal:
ollama serveError:
ERROR: Model 'gemma3:1b' not found!
Solution:
Pull the required models:
ollama pull gemma3:1b
ollama pull nomic-embed-textError:
{"detail": "Session not found"}
Solution:
The session ID may have expired. Create a new session:
POST /session/newError:
Error loading vector store
Solution:
Delete and recreate the vector store:
Windows:
Remove-Item -Recurse -Force vectorstoreMac/Linux:
rm -rf vectorstoreThen run:
python main.pySolution:
- Ensure you're using the same
session_idin subsequent requests - Check that the session exists:
GET /sessions - Verify memory with:
GET /history/{session_id}
Create a Dockerfile:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]Build and run:
docker build -t islamic-chatbot .
docker run -p 8000:8000 islamic-chatbotCreate a .env file:
OLLAMA_HOST=http://localhost:11434
CHAT_MODEL=gemma3:1b
EMBEDDING_MODEL=nomic-embed-text
MAX_ITERATIONS=3langchain>=0.1.0
langchain-community>=0.0.13
langchain-ollama>=0.0.1
langchain-core>=0.1.0
chromadb>=0.4.22
sentence-transformers>=2.3.1
pypdf>=4.0.1
pandas>=2.1.4
ollama>=0.1.6
tiktoken>=0.5.2
fastapi>=0.104.0
uvicorn>=0.24.0
python-multipart>=0.0.6
requests>=2.31.0Contributions are welcome! Here's how you can help:
- 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
This project is for educational purposes. Ensure you have the rights to use the Islamic texts you include.
- Ollama - For local LLM inference
- LangChain - For RAG framework
- ChromaDB - For vector storage
- FastAPI - For API framework
- Islamic text sources and contributors
- β Multi-agent system with 3 specialized agents
- β RAG implementation with ChromaDB
- β Persistent session-based conversation memory
- β Multi-session API support
- β Memory endpoints for history retrieval
- β Iteration tracking per session
- β FastAPI REST API with session management
- β PDF and CSV document support
- β Multi-lingual support (Arabic, Urdu, English)
- β Basic multi-agent system
- β RAG implementation
- β Single session memory
- β REST API
Made with β€οΈ for the Muslim community
Ψ§ΩΨ³ΩΨ§Ω ΨΉΩΩΩΩ ΩΨ±ΨΩ Ψ© Ψ§ΩΩΩ ΩΨ¨Ψ±ΩΨ§ΨͺΩ
Peace be upon you and God's mercy and blessings