Build a chatbot that answers medical questions using MedlinePlus data. Use retrieval-augmented generation (RAG) to keep answers grounded in real medical content instead of guesses.
- Fetch 8 chunks from MedlinePlus content for each question.
- Re-rank those chunks with a CrossEncoder model. Keep the top 5.
- Answer using only the retrieved content. Refuse to answer if nothing relevant is found.
- Remember chat history across turns. Handle follow-up questions correctly.
- Show source links for every answer.
- Use a scoped empathy layer. Add warmth for personal or emotional questions. Never soften safety-critical advice (like "see a doctor now").
- Backend: Python 3.10, FastAPI, Uvicorn
- Frontend: Strea B4A2 mlit
- LLM: Ollama, using
gpt-oss:120b-cloud(runs on Ollama's cloud servers, not your local machine) - Embeddings:
nomic-embed-textvia Ollama (runs locally, only 137M parameters) - Vector DB: ChromaDB
- Re-Ranker: HuggingFace CrossEncoder (
ms-marco-MiniLM-L-6-v2) - Orchestration: LangChain
- Scraping: BeautifulSoup4, requests, lxml
- Containers: Docker, Docker Compose
MedlinePlus-Chatbot/
├── backend/
│ ├── __init__.py
│ ├── ingestion.py # MedlinePlus scraping and ChromaDB population
│ ├── retriever.py # RAG chain with re-ranking and history awareness
│ ├── retrieve_api.py # FastAPI endpoint for queries
│ ├── requirements.txt # Backend dependencies
│ └── Dockerfile # Backend container definition
├── frontend/
│ ├── __init__.py
│ ├── app.py # Streamlit application
│ ├── requirements.txt # Frontend dependencies
│ ├── Dockerfile # Frontend container definition
│ └── .streamlit/ # Streamlit configuration
├── config/
│ ├── __init__.py
│ └── settings.py # Central configuration (models, paths, etc.)
├── db/
│ ├── chroma/ # Production ChromaDB vector store
│ ├── test_chroma/ # Test vector store
│ └── scripts_chroma/ # Scripts/test vector store
├── memory/ # Memory storage (if used)
├── tests/
│ ├── smoke_test.py # Basic functionality tests
│ └── eval/ # Evaluation scripts
├── check_db.py # Utility to inspect ChromaDB contents
├── docker-compose.yml # Multi-service orchestration
├── AGENTS.md # Engineering guidelines
├── README.md # This file
├── .env # Environment variables
├── .gitignore
└── .venv/ # Python virtual environment
Run one Ollama container as its own service (ollama in docker-compose.yml). This container has its own storage, completely separate from any Ollama app on your host machine. Pulling a model on your host does not put that model inside the container.
Two kinds of models exist here:
nomic-embed-textis a real local model. Pull it into the container's own storage before use. It's small (274 MB), so it runs fine on CPU. No GPU needed.gpt-oss:120b-cloudis a cloud model. Skip the pull step for this one. Sign in to your Ollama account inside the container instead. Inference runs on Ollama's servers, not on your machine.ollama listwon't show a size for it, since no weights ever download locally.
- Install Docker and Docker Compose.
- Install Git.
- GPU not required. The chat model runs on Ollama's cloud servers. The only local model is tiny and can work fine on CPU.
- Clone the repo.
git clone <repository-url>
cd MedlinePlus-Chatbot- Copy
.env.exampleto.env. Add your variables.
cp .env.example .env- Start all services.
docker compose up -dThis starts 3 services:
- Backend on
http://localhost:8000 - Frontend on
http://localhost:8501 - Ollama on
http://localhost:11434
- Sign in to Ollama Cloud inside the container. Do this once. This unlocks cloud models like
gpt-oss:120b-cloud.
docker compose exec ollama ollama signin- Pull the embedding model into the container. Skip pulling the cloud chat model — it doesn't need one.
docker compose exec ollama ollama pull nomic-embed-text:latest- Create a virtual environment.
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate- Install dependencies.
pip install -r backend/requirements.txt
pip install -r frontend/requirements.txt- Install Ollama from https://ollama.ai. Run these commands.
ollama serve
ollama signin
ollama pull nomic-embed-text:latest- Start the backend and frontend.
uvicorn backend.retrieve_api:app --host 0.0.0.0 --port 8000
streamlit run frontend/app.py- Open
http://localhost:8501in your browser. - Type a medical question.
- Get an answer with source links, built from these 4 steps:
- Convert your question into an embedding.
- Retrieve 8 relevant chunks.
- Re-rank down to the top 5.
- Generate an answer using only those 5 chunks.
- Ask follow-up questions. The bot remembers your last messages.
- Check API docs at
http://localhost:8000/docs.
Run this to build or refresh the vector store.
# Docker
docker compose run --rm backend python ingestion.py
# Manual
python backend/ingestion.pyThis takes a few hours for the full MedlinePlus sitemap (10,000+ pages). Re-runs are faster since chunks already exist.
Ingestion settings (in backend/ingestion.py):
- Chunk size: 1000 characters
- Chunk overlap: 150 characters
- Batch size: 25 URLs per batch
Run the smoke test.
docker compose run --rm backend python tests/smoke_test.pyCheck what's stored in the vector DB.
python check_db.py1. Config folder got shadowed.
config/ lives at the repo root, not inside backend/. Docker copied it into the image correctly, but the backend/ bind mount covered it up at runtime. Fixed it by adding ./config:/app/config as its own volume mount in docker-compose.yml, and by adding COPY config/ ./config/ to the Dockerfile after pip install (for faster rebuilds).
2. Docker served a stale image.
Changed the Dockerfile, ran docker compose up, and nothing changed. Turns out docker compose up reuses the old image unless you force a rebuild. Fixed it by running docker compose up -d --build <service> after every Dockerfile change.
3. Ingestion and the live API used two different databases.
Settings.persistent_directory pointed to ./db/chroma, but that folder wasn't mounted as a volume. Ingestion wrote to one location inside the container. The live API read from a separate, empty location. No error showed up — just an empty database and sources: [] in every response. Fixed it by adding ./db:/app/db as a volume mount.
4. Removed MLflow entirely. Started with MLflow tracking for prompts and responses. Cut it later; added complexity without enough payoff for this project's size. Removed the code, the compose service, and the requirement.
- Give informational content only. Don't use it for diagnosis or treatment.
- Support English only.
- Require manual re-ingestion for new MedlinePlus content.
- Skip authentication. Built for single-user testing, not production traffic.
See backend/requirements.txt and frontend/requirements.txt for full lists.
Backend:
- fastapi, uvicorn
- langchain, langchain-chroma, langchain-ollama
- chromadb
- sentence-transformers
- beautifulsoup4, lxml, requests
- python-dotenv
Frontend:
- streamlit
- requests
Use this project for educational and personal purposes.
Treat this tool as informational only. Don't use it to diagnose or treat any medical condition. Talk to a real healthcare provider for medical decisions.