A comprehensive medical chatbot application built with Flask, Gemini AI, Pinecone, and PostgreSQL. Features real-time chat with document-based RAG (Retrieval-Augmented Generation), source citations, conversation history, and advanced query processing.
- Docker Desktop installed (Download here)
- Pinecone API Key (Get one here)
- Google Gemini API Key (Get one here)
-
Create
.envfile in the project root:PINECONE_API_KEY=your_pinecone_key_here GOOGLE_API_KEY=your_gemini_key_here SECRET_KEY=change-this-to-random-string-in-production GEMINI_MODEL=gemini-2.5-flash
-
Start the application:
docker-compose up --build
-
Open browser: http://localhost:8080
-
Register an account and start chatting!
For detailed Docker setup and running instructions, see DOCKER_SETUP.md.
- β User Authentication - Secure registration and login system
- β Multi-Document Upload - Upload and manage multiple PDF documents
- β Real-time Chat - Streaming responses with immediate feedback
- β Source Citations - Transparent source attribution with clickable citations
- β Conversation History - Persistent chat history per user
- β User Feedback - Thumbs up/down feedback system
- β Advanced RAG - Query rewriting and multi-hop reasoning
- β Document Management - View, manage, and delete uploaded documents
- Backend: Flask (Python 3.10)
- Database: PostgreSQL 15 (Docker container)
- Vector Store: Pinecone (384-dimensional embeddings)
- LLM: Google Gemini (configurable model)
- Embeddings: HuggingFace sentence-transformers (all-MiniLM-L6-v2)
- Frontend: HTML/CSS/JavaScript with Bootstrap
medical-chatbot/
βββ app.py # Main Flask application
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Application container definition
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (create this)
βββ src/ # Source code
β βββ database.py # Database models (User, Document, Conversation, etc.)
β βββ auth.py # Authentication routes
β βββ helper.py # Helper functions (PDF loading, text splitting)
β βββ prompt.py # System prompts
β βββ rag_advanced.py # Advanced RAG features (query rewriting, multi-hop)
βββ tests/ # Test suite
β βββ conftest.py # Pytest fixtures
β βββ test_auth.py # Authentication tests
β βββ test_chat_api.py # Chat API tests
β βββ test_database.py # Database model tests
β βββ test_documents_api.py # Document management tests
β βββ test_feedback_api.py # Feedback system tests
β βββ test_integration.py # Integration tests
β βββ test_rag_advanced.py # Advanced RAG tests
βββ templates/ # HTML templates
β βββ base.html # Base template with navbar
β βββ chat.html # Chat interface
β βββ documents.html # Document management page
β βββ login.html # Login page
β βββ register.html # Registration page
βββ static/ # CSS/JS files
β βββ style.css # Custom styles
βββ data/
βββ uploads/ # Uploaded PDFs (persisted via Docker volume)
Create a .env file in the project root with:
# Required
PINECONE_API_KEY=your_pinecone_api_key
GOOGLE_API_KEY=your_gemini_api_key
SECRET_KEY=your-random-secret-key-change-in-production
# Optional (with defaults)
GEMINI_MODEL=gemini-2.5-flash # Default: gemini-2.5-flash
DATABASE_URL=postgresql://medicalbot:medicalbot_password@db:5432/medical_chatbot # Auto-set by docker-composeFor detailed Docker setup instructions, see DOCKER_SETUP.md.
For comprehensive testing instructions, see TESTING.md.
Quick start:
# Run all tests
docker-compose exec app pytest
# Run with coverage
docker-compose exec app pytest --cov=src --cov=appGET /auth/login- Login pagePOST /auth/login- LoginGET /auth/register- Registration pagePOST /auth/register- Register new userGET /auth/logout- Logout
GET /chat- Chat interfacePOST /api/chat/stream- Stream chat response (Server-Sent Events)- Body:
{ "message": "...", "conversation_id": 123, "use_advanced_rag": false }
- Body:
GET /documents- Document management pagePOST /api/upload- Upload new PDF document (multipart/form-data)DELETE /api/documents/<id>- Delete document
GET /api/conversations- Get user's conversationsGET /api/conversations/<id>/messages- Get messages for conversation
POST /api/feedback- Submit feedback for a message- Body:
{ "message_id": 123, "rating": "positive|negative", "comment": "..." }
- Body:
- User - User accounts with authentication
- Document - Uploaded PDF documents metadata
- DocumentChunk - Chunk metadata for citations
- Conversation - Chat conversations
- Message - Individual messages in conversations
- Citation - Source citations for messages
- Feedback - User feedback on messages
Automatically improves user queries for better document retrieval. Enabled by default in standard RAG mode.
Breaks down complex questions into sub-questions and retrieves information iteratively. Enable via the "Advanced RAG" toggle in the chat interface.
Every response includes citations to source documents with:
- Document name
- Page number
- Content preview
- Clickable badges for easy navigation
If you see 404 models/gemini-pro is not found:
-
Check available models:
docker-compose exec app python -c " from google import generativeai as genai import os genai.configure(api_key=os.environ.get('GOOGLE_API_KEY')) for model in genai.list_models(): if 'generateContent' in model.supported_generation_methods: print(f'{model.name}') "
-
Update
.envfile with a model from the list:GEMINI_MODEL=gemini-2.5-flash # or gemini-2.5-pro, gemini-pro-latest, etc.
-
Restart the app:
docker-compose restart app
- Check that the upload folder exists and is writable
- Verify file size is under 16MB
- Check browser console for errors
- Ensure you're logged in (authentication required)
For more troubleshooting tips, see DOCKER_SETUP.md.
Run tests with:
docker-compose exec app pytestNote: The tests directory is mounted, so you can edit tests and run them immediately without restarting. Only restart the container if tests are hanging or using cached files.
For more details, see TESTING.md.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
docker-compose exec app pytest - Submit a pull request
For issues and questions, please open an issue on GitHub.
See LICENSE file
Note: This application uses Google Gemini AI for generating responses. Make sure you have a valid API key and that billing is enabled on your Google Cloud project if required for your chosen model.
Documentation:
- DOCKER_SETUP.md - Detailed Docker setup and running instructions
- TESTING.md - Comprehensive testing guide