A simple, robust Retrieval-Augmented Generation (RAG) system that answers Bangla and English queries using context retrieved from a Bangla PDF (HSC26 Bangla 1st Paper). Powered by DeepSeek via OpenRouter, FAISS for semantic chunk search, and MongoDB for storing chat history.
🎯 Ideal for building educational AI apps for students, educators, and researchers in Bangladesh.
- ✅ Full source code is committed to this public repository
- ✅ This
README.mdincludes:- Setup guide
- Tools, libraries used
- Sample queries + outputs
- API documentation
- Evaluation matrix
- Detailed answers to key technical questions
❓ What method or library did you use to extract the text, and why? Did you face any formatting challenges with the PDF content?
We used PyMuPDF (fitz) to extract Bangla text from the PDF. It provides:
- Accurate text block recognition
- Unicode-friendly output (crucial for Bangla script)
- Faster and cleaner than
PyPDF2orpdfminer
Challenge: Some line breaks caused sentence splits — resolved with preprocessing.
We used overlapping character-based chunking:
- Each chunk is 1000 characters long
- 200 characters overlap to preserve context across boundaries
✅ Works well because:
- Keeps sentences mostly intact
- Compatible with FAISS
- Avoids truncation of meaningful content
❓ What embedding model did you use? Why did you choose it? How does it capture the meaning of the text?
We used sentence-transformers with a multilingual model:
- Supports both English and Bangla
- Captures sentence-level meaning
- Produces dense embeddings for retrieval
- Compatible with FAISS
❓ How are you comparing the query with your stored chunks? Why did you choose this similarity method and storage setup?
We use cosine similarity via FAISS:
- Fast L2-based vector matching (
IndexFlatL2) - Embedding vectors stored locally in
vector_db/ - Matches query → top chunks → passed to LLM
✅ This is efficient and well-optimized for similarity search.
❓ How do you ensure that the question and the document chunks are compared meaningfully? What would happen if the query is vague or missing context?
We embed both the question and chunks using the same model.
✅ Meaningful matching ensured because:
- Chunk overlap keeps local context
- FAISS returns top-K nearest vectors
- It may return irrelevant chunks
- LLM may hallucinate or generate incorrect guesses
- We recommend better chunking or query refinement to improve accuracy
Yes, for direct textbook questions, results are highly accurate.
To improve:
- Use sentence-aware chunking instead of character
- Use cross-encoder re-ranking
- Fine-tune embeddings on textbook data
| Purpose | Library/Tool |
|---|---|
| Embeddings | sentence-transformers, numpy |
| Vector Database | faiss-cpu |
| PDF Reader | PyMuPDF (fitz) |
| Chat Model (LLM) | deepseek/deepseek-chat-v3-0324:free via OpenRouter |
| Backend Server | FastAPI, uvicorn, pydantic |
| Database | MongoDB, pymongo |
| Utilities | dotenv, tqdm, httpx, openai >= 1.0.0 |
multilingual-rag-system/
├── config.py
├── app.py
├── requirements.txt
├── .env
├── data/
│ └── HSC26-Bangla1st-Paper.pdf
├── vector\_db/
│ └── faiss\_index.index
├── models/
│ ├── preprocessor.py
│ ├── chunker.py
│ ├── vectorstore.py
│ └── rag\_pipeline.py
├── database/
│ └── chat\_history.py
├── rag\_env/
- Python 3.8 or higher
- MongoDB installed or MongoDB Atlas URI
- Internet connection
mkdir multilingual-rag-system && cd multilingual-rag-system
mkdir models database data vector_db
python -m venv rag_env
rag_env\Scripts\activatePlace your Bangla PDF in data/:
data/HSC26-Bangla1st-Paper.pdf
OPENROUTER_API_KEY=your_api_key_here
MONGO_URI=mongodb://localhost:27017
If using Atlas:
MONGO_URI=mongodb+srv://user:pass@cluster.mongodb.net/
langchain>=0.1.0
sentence-transformers>=2.2.2
faiss-cpu>=1.7.4
transformers>=4.30.0
PyMuPDF>=1.23.0
fastapi>=0.100.0
uvicorn[standard]>=0.22.0
pydantic>=2.0.0
pymongo>=4.4.0
openai>=1.0.0
httpx>=0.24.0
python-dotenv>=1.0.0
numpy>=1.24.0
tqdm>=4.65.0
pip install -r requirements.txtDownload from: https://www.mongodb.com/try/download/community Install with “Run as a Service” enabled.
Then start:
net start MongoDBpython app.pyOn first run:
- PDF is processed
- Embeddings generated and stored in FAISS
- DeepSeek LLM initialized
- Server starts at
http://localhost:8000
"Multilingual RAG System with DeepSeek v3 is running! 🚀"Input:
{
"question": "অনুপমের ভাষায় সুপুরুষ কাকে বলা হয়েছে?",
"session_id": "test123"
}Output:
{
"answer": "শুম্ভুনাথ"
}| 🔎 Question (Bangla) | ✅ Answer |
|---|---|
| অনুপমের ভাষায় সুপুরুষ কাকে বলা হয়েছে? | শুম্ভুনাথ |
| কাকে অনুপমের ভাগ্য দেবতা বলে উল্লেখ করা হয়েছে? | মামাকে |
| বিয়ের সময় কল্যাণীর প্রকৃত বয়স কত ছিল? | ১৫ বছর |
| 🔎 Question (English) | ✅ Answer |
|---|---|
| Who is referred to as the lucky god of Anupam? | His uncle |
| Metric | Method | Result |
|---|---|---|
| Groundedness | Retrieved chunk contains answer | ✅ Yes |
| Relevance | Cosine similarity in FAISS | ✅ Yes |
| Response Time | ~1s with cached model/embeddings | ✅ Good |
| Accuracy | Manually verified 5+ QA pairs | ✅ High |
import requests
res = requests.post("http://localhost:8000/ask", json={
"question": "অনুপমের ভাষায় সুপুরুষ কাকে বলা হয়েছে?",
"session_id": "bangla_test"
})
print("Answer:", res.json()["answer"])- MongoDB not running →
net start MongoDB - FAISS file missing → re-run app to regenerate
.envvariables not loading → check file path and variable names- PDF not found → ensure correct filename and location in
data/
This project is licensed under the MIT License
- OpenRouter
- DeepSeek
- LangChain
- MongoDB Atlas
- Bangla HSC Curriculum (Open Access PDF)
This system is now fully configured and production-ready for:
- Bangla/English QA on textbook content
- Integration into apps, chatbots, or dashboards