MedSage is a Retrieval-Augmented Generation (RAG) system that answers medical questions grounded in a real dataset of 4,966 clinical transcriptions — with Corrective RAG (CRAG): instead of blindly trusting whatever a vector search retrieves, it grades its own retrieval quality, falls back to live web search when local records aren't sufficient, and gracefully degrades when even that fails.
Disclaimer: This project is for educational purposes only. It is not a substitute for professional medical advice, diagnosis, or treatment.
FastAPI Backend (main.py)
├── POST /api/auth/register
├── POST /api/auth/login
├── POST /api/auth/logout
├── GET /api/auth/me
├── GET /api/conversations
├── POST /api/conversations
├── DELETE /api/conversations/{id}
├── GET /api/conversations/{id}/messages
├── POST /api/conversations/{id}/ask ← runs CRAG pipeline
└── Serves public/ as static frontend
CRAG Pipeline (src/corrective_rag.py)
├── Crisis Gate → deterministic keyword check
├── Smalltalk Gate → greetings/meta questions
├── Coherence Gate → rejects gibberish
├── Vector Retrieval → ChromaDB similarity search
├── Relevance Grading → LLM judges each chunk
├── Topic Gate → rejects non-medical if no local context
├── Web Search Fallback → Tavily live search
└── Structured Generation → Pydantic-enforced summary + key_points + caveat
Frontend (public/)
├── index.html → login / register
├── dashboard.html → chat interface
├── css/style.css → premium dark glassmorphism theme
└── js/app.js → API calls, message rendering, state management
- Python 3.11+
- uv (recommended) or pip
uv pip install -r requirements.txt --systemCreate a .env file in the project root:
GROQ_API_KEY=your_groq_key_here
TAVILY_API_KEY=your_tavily_key_here
Download mtsamples.csv from Kaggle and place it at data/mtsamples.csv.
python -m src.build_indexThis embeds ~35,000 chunks and persists them to chroma_db/. Takes roughly 5–20 minutes on a CPU-only machine.
uvicorn main:app --reload --port 8000Open http://localhost:8000 in your browser.
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/auth/register |
Create a new user account |
| POST | /api/auth/login |
Authenticate and receive session cookie |
| POST | /api/auth/logout |
Clear session |
| GET | /api/auth/me |
Get current user info |
| GET | /api/conversations |
List all conversations for the user |
| POST | /api/conversations |
Start a new consultation |
| DELETE | /api/conversations/{id} |
Delete a conversation |
| GET | /api/conversations/{id}/messages |
Get message history |
| POST | /api/conversations/{id}/ask |
Ask a question (runs CRAG pipeline) |
The /ask endpoint returns:
{
"summary": "...",
"key_points": [{"text": "...", "citations": [1, 2]}],
"caveat": "...",
"source_type": "local_only | web_only | local_and_web | ...",
"local_sources": [{"content": "...", "metadata": {...}}],
"web_sources": [{"title": "...", "url": "...", "content": "..."}],
"standalone_question": "..."
}| File | Purpose |
|---|---|
main.py |
FastAPI app, all REST endpoints |
src/corrective_rag.py |
CRAG orchestration pipeline |
src/auth.py |
Username/password auth with bcrypt |
src/db.py |
SQLAlchemy ORM: users, conversations, messages |
src/models.py |
ORM model definitions |
src/grader.py |
LLM relevance grader |
src/embeddings.py |
HuggingFace local embeddings |
src/vectorstore.py |
ChromaDB wrapper |
src/web_search.py |
Tavily web search integration |
src/crisis_gate.py |
Deterministic crisis language detection |
src/smalltalk_gate.py |
Greeting/meta-question detection |
public/ |
HTML/CSS/JS frontend |