iaakashkr/Medical_BOT
Simple Medical chatbot using RAG + LLM. Ask questions and get structured responses.
Medical Chatbot RAG
A Retrieval-Augmented Generation (RAG) chatbot designed for answering medical FAQs. It leverages semantic embeddings (FAISS) and syntactic search (BM25) to fetch relevant context, and Google Gemini LLM to generate accurate, context-aware responses.
Table of Contents
- Overview
- Features
- Architecture
- Folder Structure
- Setup
- Usage
- Generating Resources
- Environment Variables
- Pipeline Details
- FAQ
- Contributing
- License
Overview
This chatbot combines a hybrid retrieval system with LLM-based generation:
- Semantic Search: FAISS embeddings for vector similarity.
- Syntactic Search: BM25 keyword matching for exact phrase relevance.
- RAG Integration: Combines retrieved examples with user questions for LLM input.
- Conversational Memory: Keeps chat history for context-aware responses.
- Structured Output: Returns JSON containing
answer,source_examples, and usage stats.
Ideal for hospitals, clinics, medical students, or educational platforms.
Try It Online
You can try the chatbot live without installing anything:
Click here to test the Medical BOT
Sample: <img width="1918" height="923" alt="image" src="https://github.com/user-attachments/assets/7e6de580-483a-4c1c-9f9d-ee55f58671b6" /> :
**RESPONSE**<img width="1919" height="924" alt="image" src="https://github.com/user-attachments/assets/0275ea2c-705f-4d91-9809-2c83f360f706" />
Features
- Retrieval-Augmented Generation (RAG)
- Few-shot example selection using FAISS + BM25
- Context-aware responses from Google Gemini LLM
- JSON output with
answerandsource_examples - Chat history management
- Modular, reproducible pipeline
- Token usage tracking
Architecture
User Input
│
▼
[Few-Shot Retrieval] ←─ FAISS Embeddings + BM25 ──→ Candidate Examples
│
▼
[Context Builder] → Merge examples + chat history
│
▼
[LLM Generation] → Google Gemini LLM
│
▼
[JSON Output] → Answer + Source Examples + Token Usage
│
▼
User receives response via CLI or APIFolder Structure
medical-chatbot-rag/
│
├── app/
│ ├── MED_CHATBOT.py
│ └── dto/ # DTO folder
│ └── dto.py
│
├── pipeline/ # Core logic for retrieval & LLM calls
│ ├── embedder.py
│ ├── llm.py
│ ├── retrieval.py
│ ├── token_counter.py
│ └── token_tracker.py
│
├── resources/ # Datasets & precomputed embeddings
│ ├── train.csv
│ ├── embeddings/
│ │ └── med_embeddings.faiss
│ └── pickles/
│ └── syntactic_model_med.pkl
│
├── logs/ # Log files
│
├── .env # API keys (ignored in git)
├── requirements.txt # Dependencies
└── README.mdSetup
- Clone Repo
git clone https://github.com/iaakashkr/medical-chatbot-rag.git
cd medical-chatbot-rag- Install Dependencies
pip install -r requirements.txt- Create `.env`
GEMINI_API_KEY=your_google_gemini_api_key- Generate FAISS Embeddings & BM25 Pickle
python pipeline/embedder.py(if resources already exist, skip this step)
Usage
CLI
python app/MED_CHATBOT.pyType medical questions and get structured answers with source examples.
Generating Resources
If you want to update your embeddings or BM25 models:
- Load dataset:
resources/train.csv - Run embedding script:
python scripts/generate_embeddings.py- FAISS index saved to
resources/embeddings/med_embeddings.faiss - BM25 pickle saved to
resources/pickles/syntactic_model_med.pkl
Environment Variables
GEMINI_API_KEY: Your Google Gemini API key (required)- Optionally, adjust model name or thresholds in
pipeline/retrieval.py
Pipeline Details
- Few-Shot Retrieval: Combines FAISS semantic similarity and BM25 syntactic scores to select top-K examples.
- Exact Match Bonus: Slightly increases score if user query exactly matches example questions.
- LLM Call: Uses
llm_medical.pyto send context and question to Gemini, returns structured JSON. - Chat History: Maintains last N turns for continuity.
FAQ
Q: Do I need API keys to run locally? A: Yes, .env must contain your Gemini API key.
Q: Can I use precomputed embeddings? A: Yes, keep med_embeddings.faiss and BM25 pickle in resources/.
Q: How many examples does the bot fetch? A: Top 2 by default, configurable in fetch_few_shots().
Contributing
- Fork repo
- Create branch:
git checkout -b feature/your-feature - Commit changes
- Push:
git push origin feature/your-feature - Open PR
License
This project is licensed under the Apache 2.0 License © 2025 Akash Kumar
