A Retrieval-Augmented Generation (RAG) chatbot application for medical question-answering, built with Flask, LangChain, and HuggingFace models. The application uses vector embeddings and FAISS to retrieve relevant medical information from PDF documents and generate accurate answers using a Large Language Model.
- 📚 Document Processing: Load and process medical PDF documents
- 🔍 Semantic Search: Use FAISS vector store for efficient document retrieval
- 🤖 AI-Powered Answers: Generate medical answers using Mistral-7B-Instruct model
- 💬 Interactive Chat Interface: Web-based chat interface for medical queries
- 📝 Custom Prompts: Tailored prompts for medical question-answering
- 🚀 Optimized Performance: Efficient QA chain caching for faster responses
The application follows a RAG (Retrieval-Augmented Generation) pipeline:
- Document Loading: PDFs are loaded from the
data/directory - Text Chunking: Documents are split into chunks (500 chars with 50 char overlap)
- Embeddings: Text chunks are converted to embeddings using HuggingFace embeddings
- Vector Store: Embeddings are stored in FAISS for fast similarity search
- Retrieval: Relevant chunks are retrieved based on user queries
- Generation: LLM generates answers using retrieved context
Medical_RAG_Chat_Bot/
├── app/
│ ├── app.py # Flask application entry point
│ ├── components/
│ │ ├── data_loader.py # PDF processing pipeline
│ │ ├── embeddings.py # Embedding model initialization
│ │ ├── llm.py # LLM model configuration
│ │ ├── pdf_loader.py # PDF document loading
│ │ ├── retriever.py # RAG chain creation
│ │ └── vector_store.py # FAISS vector store management
│ ├── config/
│ │ └── config.py # Configuration settings
│ ├── common/
│ │ ├── custom_exception.py # Custom exception handling
│ │ └── logger.py # Logging configuration
│ └── templates/
│ └── index.html # Web interface template
├── data/ # PDF documents directory
├── logs/ # Application logs
├── vectorstore/ # FAISS vector store (generated)
├── requirements.txt # Python dependencies
└── README.md # This file
- Python 3.8 or higher
- HuggingFace API token
- Medical PDF documents (place in
data/directory)
-
Clone the repository
git clone <repository-url> cd Medical_RAG_Chat_Bot
-
Create a virtual environment
python -m venv venv
-
Activate the virtual environment
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the root directory:HF_TOKEN=your_huggingface_token_here
-
Prepare your documents
- Place your medical PDF files in the
data/directory - The application will automatically process them when creating the vector store
- Place your medical PDF files in the
-
Create the vector store Run the data loader script to process PDFs and create the vector store:
python -m app.components.data_loader
-
Start the Flask application
python -m app.app
Or:
python app/app.py
-
Access the web interface
- Open your browser and navigate to
http://localhost:5000 - Start asking medical questions!
- Open your browser and navigate to
-
Using the chatbot
- Type your medical question in the text area
- Click "Send" to get an AI-generated answer
- Use "Clear Chat" to reset the conversation
You can modify the following settings in app/config/config.py:
HUGGINGFACE_REPO_ID: LLM model repository ID (default: "mistralai/Mistral-7B-Instruct-v0.3")DB_FAISS_PATH: Path to store FAISS vector databaseDATA_PATH: Directory containing PDF documentsCHUNK_SIZE: Size of text chunks (default: 500)CHUNK_OVERLAP: Overlap between chunks (default: 50)
- Flask: Web framework
- LangChain: LLM framework and RAG implementation
- LangChain Community: Community integrations
- LangChain HuggingFace: HuggingFace model integration
- FAISS-CPU: Vector similarity search
- PyPDF: PDF document processing
- HuggingFace Hub: Model and dataset access
- python-dotenv: Environment variable management
- Automatically loads all PDF files from the
data/directory - Splits documents into manageable chunks for better retrieval
- Handles large medical documents efficiently
- Uses FAISS for fast similarity search
- Stores embeddings locally for quick access
- Automatically loads existing vector store on startup
- Uses Mistral-7B-Instruct model via HuggingFace
- Configured with appropriate temperature and token limits
- Optimized for medical question-answering
- Comprehensive error handling with custom exceptions
- Detailed logging for debugging
- User-friendly error messages in the UI
If you see "Vector store not present or empty":
- Make sure you have PDF files in the
data/directory - Run the data loader:
python -m app.components.data_loader - Check that the
vectorstore/directory was created
- Ensure your
.envfile contains a validHF_TOKEN - Verify your token has access to the Mistral model
- Check HuggingFace API rate limits
- Make sure all dependencies are installed:
pip install -r requirements.txt - Verify your virtual environment is activated
- Check Python version compatibility (3.8+)
Application logs are stored in the logs/ directory with daily rotation:
- Format:
log_YYYY-MM-DD.log - Includes timestamps, log levels, and detailed messages
- Useful for debugging and monitoring