Vector Store Setup
DeenPAL uses ChromaDB as its vector database to store and retrieve Hadith embeddings efficiently. ChromaDB is an open-source embedding database optimized for AI applications.Initialization
After documents are loaded and split into chunks, they are embedded and stored in ChromaDB:The
persist_directory parameter ensures that embeddings are saved to disk, so they don’t need to be regenerated on every app restart.How It Works
- Each Hadith chunk is converted to a 384-dimensional vector using the embedding model
- Vectors are stored in ChromaDB with associated metadata (source, Hadith number, chapter)
- At query time, the user’s question is embedded using the same model
- ChromaDB performs fast similarity search to find relevant Hadiths
Embedding Model
DeenPAL uses the sentence-transformers/all-MiniLM-L6-v2 model for generating embeddings:Why This Model?
- Lightweight: Only 80MB, making it fast and efficient
- Semantic Understanding: Trained on a large corpus to understand sentence meaning
- Multilingual Capable: Can handle English text effectively
- Open Source: Free to use and modify
- Quality: Balances performance and speed for real-time retrieval
Maximal Marginal Relevance (MMR) vs Similarity Search
DeenPAL uses Maximal Marginal Relevance (MMR) for retrieval instead of standard similarity search. Understanding the difference is crucial:Similarity Search
Standard similarity search returns documents that are most similar to the query:Maximal Marginal Relevance (MMR)
MMR balances relevance and diversity:- Fetch the top
fetch_k=10most similar Hadiths - Select the first Hadith (most relevant)
- From remaining Hadiths, select ones that are:
- Relevant to the query
- Diverse from already selected Hadiths
- Repeat until
k=4Hadiths are selected
MMR uses a lambda parameter (default 0.5) to balance relevance vs diversity. Higher lambda favors relevance, lower lambda favors diversity.
Why MMR is Used
The DeenPAL developers tested both approaches and found that MMR provides superior results:“I tried both, andMMRincreased diversity in retrieved Hadiths while maintaining relevance. Whereas usingsimilarity_score_thresholdwas making the Chatbot give the same hadiths redundantly.”
Benefits of MMR for DeenPAL
- Diverse Perspectives: Users receive different but relevant Hadiths, offering broader context
- Reduced Redundancy: Avoids repeating similar Hadiths
- Better Answers: LLM can synthesize information from varied sources
- Richer Context: More angles on a topic lead to comprehensive responses
Example Scenario
User asks: “What does Islam say about charity?” With Similarity Search:- Hadith 1: About giving charity
- Hadith 2: About giving charity (very similar to #1)
- Hadith 3: About giving charity (very similar to #1 and #2)
- Hadith 4: About giving charity (very similar to all above)
- Hadith 1: About giving charity
- Hadith 2: About the rewards of charity
- Hadith 3: About charity to family members
- Hadith 4: About the best times for charity
MMR ensures the LLM receives a well-rounded set of Hadiths, enabling more nuanced and comprehensive answers.
Retriever Configuration
Here’s the complete retriever setup fromchains.py:
Configuration Parameters
| Parameter | Value | Description |
|---|---|---|
search_type | "mmr" | Use Maximal Marginal Relevance algorithm |
k | 4 | Number of final Hadiths to return |
fetch_k | 10 | Number of candidates to consider for diversity |
Integration with RAG Chain
The retriever is integrated into the RAG chain:- Takes a user query
- Uses the retriever to fetch 4 diverse Hadiths
- Injects them into the prompt template
- Generates a response with citations and explanations
Performance Optimization
DeenPAL uses Streamlit’s caching to avoid reloading data:This ensures the vector store is initialized only once per app session, preventing redundant embedding generation and significantly improving response times.
