An open API service indexing awesome lists of open source software.

https://github.com/mayankmittal29/medimind-end-to-end-medical-chatbot-generative-ai

MediMind: RAG-powered medical chatbot leveraging LangChain, OpenAI GPT, and Pinecone vector DB for semantic retrieval. Flask-served UI with HuggingFace embeddings (all-MiniLM-L6-v2) enables context-aware medical query responses from PDF knowledge bases.
https://github.com/mayankmittal29/medimind-end-to-end-medical-chatbot-generative-ai

aws conda-environment css flask genai-chatbot html5 huggingface-transformers langchain openai-gpt pineconedb pypdf2-libraries python3 retrieval-augmented-generation sentence-transformers

Last synced: 8 months ago
JSON representation

MediMind: RAG-powered medical chatbot leveraging LangChain, OpenAI GPT, and Pinecone vector DB for semantic retrieval. Flask-served UI with HuggingFace embeddings (all-MiniLM-L6-v2) enables context-aware medical query responses from PDF knowledge bases.

Awesome Lists containing this project

README

          

# MediMind ๐Ÿฅ๐Ÿ‘จโ€โš•๏ธ - End-to-End Medical Chatbot Using Generative AI

![MediMind Banner](https://img.shields.io/badge/MediMind-Medical%20Chatbot-blue)
[![Python 3.10](https://img.shields.io/badge/Python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3100/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## ๐Ÿ“‘ Table of Contents
- [About The Project](#about-the-project)
- [Features](#features)
- [Project Structure](#project-structure)
- [Tech Stack](#tech-stack)
- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Installation](#installation)
- [Usage](#usage)
- [Code Snippets](#code-snippets)
- [How It Works](#how-it-works)
- [Contributing](#contributing)
- [License](#license)
- [Contact](#contact)

## ๐Ÿ” About The Project

MediMind is an end-to-end medical chatbot powered by Generative AI that provides accurate medical information based on the data it has been trained on. The chatbot leverages RAG (Retrieval Augmented Generation) to provide factual medical information by combining the power of large language models with retrievable medical knowledge. ๐Ÿง ๐Ÿ’Š
## Interactive Asthetic Display

![Plot](Display.png)
## โœจ Features

- ๐Ÿค– Intelligent medical chatbot powered by GPT
- ๐Ÿ“š PDF document processing for medical knowledge
- ๐Ÿ”Ž Vector search for accurate information retrieval
- ๐Ÿงช RAG architecture for fact-based responses
- ๐Ÿ’ป User-friendly web interface
- ๐Ÿ”„ Real-time conversational experience
- ๐Ÿ“Š Contextual understanding of medical queries
- ๐Ÿ”’ Local deployment for data privacy
- ๐Ÿ“ฑ Responsive design for all devices

## ๐Ÿ“ Project Structure

```
MediMind/
โ”œโ”€โ”€ Data/ # Directory containing medical PDF documents ๐Ÿ“‘
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ helper.py # Helper functions for document loading and processing ๐Ÿ› ๏ธ
โ”‚ โ””โ”€โ”€ prompt.py # System prompts for the LLM ๐Ÿ’ฌ
โ”œโ”€โ”€ templates/
โ”‚ โ””โ”€โ”€ chat.html # HTML template for the chat interface ๐Ÿ–ฅ๏ธ
โ”œโ”€โ”€ app.py # Main Flask application ๐ŸŒ
โ”œโ”€โ”€ requirements.txt # Project dependencies ๐Ÿ“‹
โ”œโ”€โ”€ setup.py # Setup script for the package ๐Ÿ“ฆ
โ”œโ”€โ”€ store_index.py # Script to create and populate the vector index ๐Ÿ—‚๏ธ
โ””โ”€โ”€ README.md # Project documentation ๐Ÿ“
```

## ๐Ÿ› ๏ธ Tech Stack

- **Python 3.10** ๐Ÿ - Programming Language
- **Flask** ๐ŸŒถ๏ธ - Web framework
- **LangChain** โ›“๏ธ - Framework for LLM applications
- **OpenAI** ๐Ÿง  - GPT model for natural language processing
- **Pinecone** ๐ŸŒฒ - Vector database for storing embeddings
- **HuggingFace** ๐Ÿค— - Embeddings model (sentence-transformers)
- **HTML/CSS/JS** ๐ŸŽจ - Frontend for the chat interface
- **Conda** ๐Ÿ - Environment management

## ๐Ÿš€ Getting Started

Follow these instructions to set up and run the project on your local machine.

### Prerequisites

- Python 3.10 or higher ๐Ÿ
- Conda (recommended for environment management) ๐Ÿ“ฆ
- Pinecone API Key ๐Ÿ”‘
- OpenAI API Key ๐Ÿ”‘
- Medical PDF documents ๐Ÿ“š

### Installation

1. **Clone the repository** ๐Ÿ“ฅ
```bash
git clone https://github.com/yourusername/MediMind.git
cd MediMind
```

2. **Create and activate Conda environment** ๐ŸŒ
```bash
conda create -n medibot python=3.10 -y
conda activate medibot
```

3. **Install required packages** ๐Ÿ“ฆ
```bash
pip install -r requirements.txt
```

4. **Set up environment variables** ๐Ÿ”

Create a `.env` file in the root directory and add your API keys:
```
PINECONE_API_KEY="your_pinecone_api_key"
OPENAI_API_KEY="your_openai_api_key"
```

5. **Prepare your medical PDF documents** ๐Ÿ“‘

Place your medical PDF documents in the `Data/` directory.

6. **Create and populate the vector index** ๐Ÿ—‚๏ธ
```bash
python store_index.py
```

This will:
- Load the PDF documents from the `Data/` directory ๐Ÿ“„
- Split them into text chunks ๐Ÿ“
- Generate embeddings for each chunk ๐Ÿงฎ
- Create a Pinecone index named "medicalbot" ๐ŸŒฒ
- Store the embeddings in the Pinecone index ๐Ÿ’พ

7. **Run the application** ๐Ÿƒโ€โ™‚๏ธ
```bash
python3 app.py
```

8. **Access the chat interface** ๐Ÿ’ฌ

Open your web browser and go to:
```
http://localhost:8080
```

## ๐Ÿ’ฌ Usage

1. Access the web interface at `http://localhost:8080` ๐ŸŒ
2. Type your medical questions in the chat input ๐Ÿ”
3. Get responses based on the medical documents provided โœ…

Example questions you can ask:
- "What are the symptoms of diabetes?" ๐Ÿฉบ
- "How does hypertension affect the heart?" โค๏ธ
- "What are the side effects of aspirin?" ๐Ÿ’Š
- "How should I manage my chronic pain?" ๐Ÿค•

The chatbot uses retrieval-augmented generation to provide accurate answers based on the medical documents you've provided in the `Data/` directory. ๐Ÿ“š๐Ÿค–

## ๐Ÿ’ป Code Snippets

### 1. Loading and Processing PDF Documents ๐Ÿ“‘

```python
# From helper.py
def load_pdf_file(data):
loader = DirectoryLoader(data,
glob="*.pdf",
loader_cls=PyPDFLoader)

documents = loader.load()
return documents

def text_split(extracted_data):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
text_chunks = text_splitter.split_documents(extracted_data)
return text_chunks
```

### 2. Creating Embeddings ๐Ÿงฎ

```python
# From helper.py
def download_hugging_face_embeddings():
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
return embeddings
```

### 3. Setting Up the Flask Application ๐ŸŒ

```python
# From app.py
app = Flask(__name__)

load_dotenv()

PINECONE_API_KEY = os.environ.get('PINECONE_API_KEY')
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')

# Set up retriever and language model
embeddings = download_hugging_face_embeddings()
docsearch = PineconeVectorStore.from_existing_index(
index_name="medicalbot",
embedding=embeddings
)
retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k":3})
```

### 4. Creating the RAG Chain โ›“๏ธ

```python
# From app.py
llm = OpenAI(temperature=0.4, max_tokens=500)
prompt = ChatPromptTemplate.from_messages([
("system", system_prompt),
("human", "{input}"),
])

question_answer_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
```

### 5. Processing Chat Requests ๐Ÿ’ฌ

```python
# From app.py
@app.route("/get", methods=["GET", "POST"])
def chat():
msg = request.form["msg"]
input = msg
print(input)
response = rag_chain.invoke({"input": msg})
print("Response : ", response["answer"])
return str(response["answer"])
```

## ๐Ÿงช How It Works

1. **Document Processing** ๐Ÿ“„:
- Medical PDFs are loaded and processed through `DirectoryLoader` and `PyPDFLoader`
- Documents are split into manageable chunks using `RecursiveCharacterTextSplitter`
- Text chunks are converted into embeddings using HuggingFace models (384 dimensions)

2. **Vector Database Setup** ๐Ÿ—‚๏ธ:
- Pinecone serverless instance is created in AWS
- Embeddings are stored with document references for retrieval
- Vector similarity search is used to find relevant information

3. **Retrieval System** ๐Ÿ”Ž:
- User queries are converted to embeddings using the same model
- Similar document chunks are retrieved from Pinecone using cosine similarity
- The top 3 most relevant chunks are selected (`k=3`)

4. **Response Generation** ๐Ÿค–:
- Retrieved information is fed to the LLM (OpenAI)
- System prompt guides the LLM to use the context for answers
- Temperature setting of 0.4 balances creativity and accuracy
- The LLM generates a concise response (max 3 sentences)
- Response is returned to the user through the Flask web interface

5. **User Experience** ๐Ÿ‘จโ€๐Ÿ’ป:
- Clean and intuitive chat interface
- Real-time interaction with the medical knowledge base
- Responses are focused on medical knowledge from the provided documents

## ๐Ÿ‘ Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**. ๐Ÿ™

1. Fork the Project ๐Ÿด
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`) ๐ŸŒฟ
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`) ๐Ÿ’พ
4. Push to the Branch (`git push origin feature/AmazingFeature`) ๐Ÿš€
5. Open a Pull Request ๐Ÿ“ฌ

## ๐Ÿ“„ License

Distributed under the MIT License. See `LICENSE` for more information. โš–๏ธ

## ๐Ÿ“ง Contact

Mayank Mittal - ๐Ÿ‘จโ€๐Ÿ’ป mayankmittal29042004@gmail.com

Project Link: [https://github.com/mayankmittal29/MediMind](https://github.com/mayankmittal29/MediMind) ๐Ÿ”—

---

โญ Star this repo if you found it useful! โญ