Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Medical RAG Chat Bot

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.

Features

  • 📚 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

Architecture

The application follows a RAG (Retrieval-Augmented Generation) pipeline:

  1. Document Loading: PDFs are loaded from the data/ directory
  2. Text Chunking: Documents are split into chunks (500 chars with 50 char overlap)
  3. Embeddings: Text chunks are converted to embeddings using HuggingFace embeddings
  4. Vector Store: Embeddings are stored in FAISS for fast similarity search
  5. Retrieval: Relevant chunks are retrieved based on user queries
  6. Generation: LLM generates answers using retrieved context

Project Structure

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

Prerequisites

  • Python 3.8 or higher
  • HuggingFace API token
  • Medical PDF documents (place in data/ directory)

Installation

  1. Clone the repository

    git clone <repository-url>
    cd Medical_RAG_Chat_Bot
  2. Create a virtual environment

    python -m venv venv
  3. Activate the virtual environment

    • On Windows:
      venv\Scripts\activate
    • On macOS/Linux:
      source venv/bin/activate
  4. Install dependencies

    pip install -r requirements.txt
  5. Set up environment variables Create a .env file in the root directory:

    HF_TOKEN=your_huggingface_token_here
  6. Prepare your documents

    • Place your medical PDF files in the data/ directory
    • The application will automatically process them when creating the vector store
  7. Create the vector store Run the data loader script to process PDFs and create the vector store:

    python -m app.components.data_loader

Usage

  1. Start the Flask application

    python -m app.app

    Or:

    python app/app.py
  2. Access the web interface

    • Open your browser and navigate to http://localhost:5000
    • Start asking medical questions!
  3. 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

Configuration

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 database
  • DATA_PATH: Directory containing PDF documents
  • CHUNK_SIZE: Size of text chunks (default: 500)
  • CHUNK_OVERLAP: Overlap between chunks (default: 50)

Dependencies

  • 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

Features in Detail

Document Processing

  • Automatically loads all PDF files from the data/ directory
  • Splits documents into manageable chunks for better retrieval
  • Handles large medical documents efficiently

Vector Store

  • Uses FAISS for fast similarity search
  • Stores embeddings locally for quick access
  • Automatically loads existing vector store on startup

LLM Integration

  • Uses Mistral-7B-Instruct model via HuggingFace
  • Configured with appropriate temperature and token limits
  • Optimized for medical question-answering

Error Handling

  • Comprehensive error handling with custom exceptions
  • Detailed logging for debugging
  • User-friendly error messages in the UI

Troubleshooting

Vector Store Not Found

If you see "Vector store not present or empty":

  1. Make sure you have PDF files in the data/ directory
  2. Run the data loader: python -m app.components.data_loader
  3. Check that the vectorstore/ directory was created

HuggingFace Token Issues

  • Ensure your .env file contains a valid HF_TOKEN
  • Verify your token has access to the Mistral model
  • Check HuggingFace API rate limits

Import Errors

  • Make sure all dependencies are installed: pip install -r requirements.txt
  • Verify your virtual environment is activated
  • Check Python version compatibility (3.8+)

Logging

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages