Overview
Thechains.py module constructs the Retrieval-Augmented Generation (RAG) pipeline by combining a vector store retriever with a language model and prompt template.
This module creates the
rag_chain that powers DeenPAL’s question-answering capabilities.Complete Module Code
Component Breakdown
1. Loading Data and Vector Store
The module starts by loading the prepared data from the loader:2. Retriever Initialization
The retriever uses Maximal Marginal Relevance (MMR) for diverse results:MMR Parameters Explained:
search_type="mmr": Uses Maximal Marginal Relevance algorithmk=4: Returns the top 4 most relevant and diverse documentsfetch_k=10: Initially fetches 10 candidates before applying MMR
Why MMR?
MMR balances relevance and diversity:- Relevance: Documents are semantically similar to the query
- Diversity: Selected documents are different from each other
- Result: Users get comprehensive coverage without redundancy
Example: If a user asks about prayer, MMR might return hadiths about:
- Prayer times (relevant)
- Prayer postures (relevant but different)
- Group prayer (relevant but diverse)
- Prayer invalidation (relevant and unique)
3. LLM Initialization
DeenPAL uses DeepSeek via OpenRouter for language generation:- Model: DeepSeek Chat v3 (March 2024)
- API: OpenRouter (provides unified access to multiple LLMs)
- Tier: Free tier
Environment variables (loaded via
load_dotenv()) should include:OPENAI_API_KEY: Your OpenRouter API key
4. Document Chain Creation
Thecreate_stuff_documents_chain combines the LLM with the prompt template:
- Takes retrieved documents
- “Stuffs” them into the prompt template
- Sends the complete prompt to the LLM
- Returns the generated answer
“Stuff” Strategy:
The simplest document combination method - all retrieved documents are inserted directly into the prompt context.
5. Retrieval Chain Creation
The finalrag_chain combines retrieval with generation:
The RAG Pipeline Flow
When a user asks a question:- Query Embedding: User’s question is converted to a vector
- Retrieval: MMR finds 4 diverse, relevant hadiths from ChromaDB
- Context Building: Retrieved documents are formatted into the prompt
- LLM Generation: DeepSeek generates an answer using the context
- Response: Answer is returned with source citations
Using the RAG Chain
Other modules (likeapp.py) use the chain like this:
Response Structure
Therag_chain returns a dictionary:
The
context field contains the actual hadith documents that were used to generate the answer.Dependencies
The module requires:- LangChain: For chain construction
- loader.py: Provides the vector store
- prompts.py: Provides the QA prompt template
- dotenv: For environment variable management
