Overview
Thechains.py module sets up the complete RAG (Retrieval-Augmented Generation) pipeline, including the vector store, retriever, LLM, and chain components.
Module Exports
The module exports the following variables that can be imported by other modules:rag_chain- Complete RAG pipelineretriever- MMR-based document retrieverllm- ChatOpenAI instancedb- Chroma vector storeembeddings- HuggingFace embeddings model
Variables
db
The Chroma vector store loaded from the persisted database. Contains all Hadith document embeddings and metadata.
embeddings
HuggingFace embeddings model using
sentence-transformers/all-MiniLM-L6-v2 for vectorization.retriever
MMR (Maximal Marginal Relevance) based retriever that fetches diverse, relevant documents from the vector store.
Configuration
Search algorithm type. Uses Maximal Marginal Relevance to balance relevance and diversity.
Number of documents to return in retrieval results.
Number of candidate documents to fetch before MMR re-ranking.
llm
Language model instance using DeepSeek Chat v3 through OpenRouter API.
Configuration
Model identifier for DeepSeek Chat v3 (free tier).
OpenRouter API endpoint for model access.
Requires
OPENAI_API_KEY environment variable to be set with your OpenRouter API key.rag_chain
Complete RAG pipeline that retrieves relevant documents and generates answers using the LLM.
- Retriever: Fetches relevant Hadith documents
- Document Chain: Combines retrieved documents with the prompt template
- LLM: Generates the final answer
Usage Example
Input Format
The user’s question or query.
List of previous messages for conversation context. Each message should be a dictionary with
role and content keys.Output Format
The generated response from the LLM based on retrieved Hadith documents.
List of retrieved documents used to generate the answer.
Echo of the original input question.
Dependencies
langchain.chains.create_retrieval_chainlangchain.chains.combine_documents.create_stuff_documents_chainlangchain_openai.ChatOpenAIdotenv.load_dotenvloader.load_and_prepare_dataprompts.qa_prompt
Environment Variables
Your OpenRouter API key for accessing the DeepSeek model.
.env file in your project root:
