Context
Medical PDF Chatbot
A Retrieval-Augmented Generation (RAG) based medical PDF chatbot built with Python, Flask, LangChain, Google Gemini, Pinecone, and Hugging Face embeddings.
The application provides a web-based chat interface where users can ask questions and receive concise answers based on information retrieved from the indexed medical documents.
Features
- ๐ PDF document ingestion
- ๐ Semantic similarity search over document chunks
- ๐ง Retrieval-Augmented Generation (RAG)
- ๐ค Google Gemini 2.5 Flash for answer generation
- ๐๏ธ Pinecone vector database for persistent vector search
- ๐ค Hugging Face
all-MiniLM-L6-v2embeddings - ๐ Flask web application
- ๐ฌ Simple Bootstrap-based chat interface
- โก Concise, context-grounded responses
Architecture
PDF Documents
โ
โผ
PyPDFLoader
โ
โผ
Text Extraction
โ
โผ
Recursive Character Text Splitter
(chunk size: 500, overlap: 20)
โ
โผ
Hugging Face Embeddings
(all-MiniLM-L6-v2, 384 dimensions)
โ
โผ
Pinecone Vector Database
โ
โผ
Similarity Retriever
(top 3 relevant chunks)
โ
โผ
Google Gemini 2.5 Flash
โ
โผ
Generated Answer
โ
โผ
Flask Chat InterfaceTech Stack
Component Technology
Programming Language Python
Web Framework Flask
LLM Google Gemini 2.5 Flash
RAG Framework LangChain
Vector Database Pinecone
Embedding Model Hugging Face
sentence-transformers/all-MiniLM-L6-v2
PDF Loader LangChain PyPDFLoader
Text Splitter RecursiveCharacterTextSplitter
Frontend HTML, CSS, Bootstrap, JavaScript/jQuery
Deployment Server Gunicorn
Project Structure
Chat_Bot_PDF/
โ
โโโ data/
โ โโโ PDF documents used for indexing
โ
โโโ research/
โ โโโ Research/reference material
โ
โโโ src/
โ โโโ __init__.py
โ โโโ helper.py
โ โโโ prompt.py
โ
โโโ static/
โ โโโ style.css
โ
โโโ templates/
โ โโโ chat.html
โ
โโโ app.py
โโโ store_index.py
โโโ requirements.txt
โโโ runtime.txt
โโโ setup.py
โโโ template.sh
โโโ README.mdHow the RAG Pipeline Works
1. Load PDF documents
PDF files placed inside the data/ directory are loaded using
LangChain's DirectoryLoader and PyPDFLoader.
2. Clean document metadata
The application keeps the document source and page content required for retrieval.
3. Split text into chunks
The extracted content is divided into smaller chunks using:
RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=20
)This makes the documents easier to embed and retrieve efficiently.
4. Generate embeddings
The project uses:
sentence-transformers/all-MiniLM-L6-v2The embedding model produces 384-dimensional vectors.
5. Store vectors in Pinecone
The generated vectors are uploaded to a Pinecone index named:
medical-chatbotThe index uses cosine similarity and is configured as a Pinecone serverless index.
6. Retrieve relevant information
When a user asks a question, the application performs similarity search and retrieves the top 3 relevant document chunks.
7. Generate the answer
The retrieved context is passed to Google Gemini 2.5 Flash through LangChain.
The system prompt instructs the model to:
- answer using the retrieved context;
- say it does not know when the answer is unavailable;
- keep the response concise;
- use a maximum of three sentences.
Installation
Prerequisites
Make sure the following are installed:
- Python 3.10+
- Git
- A Pinecone account/API key
- A Google Gemini API key
1. Clone the repository
git clone https://github.com/saranshvaidya0/Chat_Bot_PDF.git
cd Chat_Bot_PDF2. Create a virtual environment
Windows:
python -m venv venv
venv\Scripts\activateLinux/macOS:
python3 -m venv venv
source venv/bin/activate3. Install dependencies
pip install -r requirements.txt4. Configure environment variables
Create a .env file in the project root:
PINECONE_API_KEY=your_pinecone_api_key
GEMINI_API_KEY=your_gemini_api_keyDo not commit the .env file or API keys to GitHub.
Indexing the PDF Documents
Place the PDF files that you want the chatbot to understand inside:
data/Then run:
python store_index.pyThis script:
- loads the PDFs;
- extracts their text;
- splits the text into chunks;
- creates embeddings;
- creates the Pinecone index if it does not already exist;
- uploads the document embeddings to Pinecone.
The current implementation uses the Pinecone index:
medical-chatbotwith a vector dimension of:
384Running the Application
Start the Flask application:
python app.pyThe application runs on:
http://127.0.0.1:8080Open that address in your browser.
Using the Chatbot
- Start the Flask server.
- Open the application in a browser.
- Enter a question in the chat box.
- The application sends the question to the
/getendpoint. - LangChain retrieves the most relevant document chunks from Pinecone.
- Gemini generates an answer using the retrieved context.
- The answer is displayed in the chat interface.
API Endpoint
POST /get
Used by the web interface to send a question to the chatbot.
Example form data:
msg=What are the symptoms mentioned in the document?The endpoint returns the generated answer as text.
Environment Variables
Variable Purpose
PINECONE_API_KEY Authenticates access to Pinecone
GEMINI_API_KEY Authenticates access to Google Gemini
Important Notes
Medical information
This project is an educational/technical demonstration of a document-based medical chatbot. It should not be treated as a replacement for a qualified medical professional, diagnosis, or emergency medical care.
API key security
Never place API keys directly inside Python source files.
Use environment variables through .env and keep .env out of version
control.
Existing Pinecone index
The application expects the Pinecone index to be named:
medical-chatbotIf you change the index name in store_index.py, make the same change
in app.py.
Limitations
- The chatbot's knowledge is limited to the documents indexed in Pinecone.
- Response quality depends on the quality and completeness of the source PDFs.
- Retrieval is currently configured to return three relevant chunks.
- The application does not provide medical diagnosis or professional medical advice.
- The current interface is intentionally simple and can be extended with authentication, chat history, source citations, document management, and improved UI.
Future Improvements
Potential improvements include:
- Multi-PDF upload from the web interface
- Display retrieved sources and page numbers
- Conversation history
- User authentication
- Streaming Gemini responses
- Better document-management interface
- Metadata filtering in Pinecone
- Medical terminology-aware retrieval
- Improved prompt engineering
- Automated evaluation of RAG accuracy
- Docker support
- Production deployment configuration
- Responsive and modern frontend
- Citation-based answers
License
No license is currently specified in this repository. Add a license file if you intend to distribute the project under an open-source license.
Author
Saransh Vaidya
GitHub: https://github.com/saranshvaidya0