8000 GitHub - gitInfinity/MedlineAI: Local clinical RAG chatbot built with LangChain and Ollama. Features a custom ETL pipeline indexing 10,000+ MedlinePlus pages, Cross-Encoder re-ranking, and strict prompt guardrails for highly accurate, grounded answers. · GitHub
[go: up one dir, main page]

Skip to content
gitInfinityPublic

About

Local clinical RAG chatbot built with LangChain and Ollama. Features a custom ETL pipeline indexing 10,000+ MedlinePlus pages, Cross-Encoder re-ranking, and strict prompt guardrails for highly accurate, grounded answers.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

21 Commits

Folders and files

Repository files navigation

MedlinePlus RAG Chatbot 🩺

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.

What It Does

  • 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").

Tech Stack

  • Backend: Python 3.10, FastAPI, Uvicorn< 8000 /li>
  • Frontend: Streamlit
  • LLM: Ollama, using gpt-oss:120b-cloud (runs on Ollama's cloud servers, not your local machine)
  • Embeddings: nomic-embed-text via 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

Project Structure

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

How Ollama and Docker Fit Together

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-text is 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-cloud is 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 list won't show a size for it, since no weights ever download locally.

Setup

Requirements

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

Run With Docker

  1. Clone the repo.
git clone <repository-url>
cd MedlinePlus-Chatbot
  1. Copy .env.example to .env. Add your variables.
cp .env.example .env
  1. Start all services.
docker compose up -d

This starts 3 services:

  • Backend on http://localhost:8000
  • Frontend on http://localhost:8501
  • Ollama on http://localhost:11434
  1. 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
  1. 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

Run Without Docker

  1. Create a virtual environment.
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
  1. Install dependencies.
pip install -r backend/requirements.txt
pip install -r frontend/requirements.txt
  1. Install Ollama from https://ollama.ai. Run these commands.
ollama serve
ollama signin
ollama pull nomic-embed-text:latest
  1. Start the backend and frontend.
uvicorn backend.retrieve_api:app --host 0.0.0.0 --port 8000
streamlit run frontend/app.py

Usage

  1. Open http://localhost:8501 in your browser.
  2. Type a medical question.
  3. 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.
  4. Ask follow-up questions. The bot remembers your last messages.
  5. Check API docs at http://localhost:8000/docs.

Ingest MedlinePlus Data

Run this to build or refresh the vector store.

# Docker
docker compose run --rm backend python ingestion.py

# Manual
python backend/ingestion.py

This 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

Testing

Run the smoke test.

docker compose run --rm backend python tests/smoke_test.py

Check what's stored in the vector DB.

python check_db.py

Bugs I Hit and Fixed

1. 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.

Known Limitations

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

Requirements

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

License

Use this project for educational and personal purposes.

Disclaimer

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.

About

Local clinical RAG chatbot built with LangChain and Ollama. Features a custom ETL pipeline indexing 10,000+ MedlinePlus pages, Cross-Encoder re-ranking, and strict prompt guardrails for highly accurate, grounded answers.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0