A self-correcting medical AI agent built with LangGraph, Groq, ChromaDB, and Streamlit. Combines Retrieval-Augmented Generation (RAG) from a medical knowledge base with automatic web search fallback, conversation memory, and adaptive multimodal vision analysis for medical imaging.
Note: A free Groq API key is required to use the app. Takes 2 minutes to get.
The agent uses a self-correcting LangGraph pipeline with 4 sequential nodes:
- Self-Correcting RAG β Automatically falls back to web search when the knowledge base lacks relevant context
- Query Rewriting β Resolves vague follow-up questions using conversation history before retrieval
- Conversation Memory β Remembers previous turns within a session for contextual multi-turn dialogue
- Adaptive Vision Analysis β Detects image type (Chest X-Ray, Brain MRI, CT Scan, Bone X-Ray, Skin, Ultrasound) and applies a specialist-level prompt for each
- Page Number Citations β Every PDF-sourced answer shows exact page numbers from the encyclopedia
- Structured Responses β All answers returned in a consistent clinical format
- Medical-Grade Embeddings β Uses
NeuML/pubmedbert-base-embeddings(trained on PubMed) instead of general-purpose embeddings for better medical retrieval accuracy - API Key per User β Each user enters their own free Groq key β no shared quota
ClinixAI/
β
βββ app.py # Streamlit UI β main entry point
βββ agent.py # Streamlit wrapper with @st.cache_resource
βββ agent_core.py # Pure agent logic β used by FastAPI
βββ api.py # FastAPI REST API (optional, for production use)
βββ retriever.py # ChromaDB vector store + PDF loader
βββ evaluate.py # RAGAS evaluation script
β
βββ chroma_db_data/ # Auto-generated vector database (gitignored)
βββ .env # API keys (gitignored)
βββ requirements.txt # Production dependencies
βββ README.md
| Component | Technology |
|---|---|
| UI | Streamlit |
| Agent Orchestration | LangGraph |
| LLM β Reasoning | Llama 3.3 70B via Groq |
| LLM β Vision | Llama 4 Scout 17B via Groq |
| Embeddings | NeuML/pubmedbert-base-embeddings (HuggingFace) |
| Vector Database | ChromaDB |
| PDF Loader | PyMuPDF |
| Web Search Fallback | DuckDuckGo (langchain-community) |
| REST API | FastAPI + Uvicorn |
| Evaluation | RAGAS |
git clone https://github.com/YOUR_USERNAME/clinical-ai-agent.git
cd clinical-ai-agentpython -m venv venv
# Windows
venv\Scripts\activate
# macOS / Linux
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the root directory:
GROQ_API_KEY=your_groq_api_key_hereGet your free Groq API key at console.groq.com.
The PDF knowledge base is downloaded from Google Drive on first run and indexed into ChromaDB. This takes 3β8 minutes and only happens once. All subsequent runs load the existing database instantly.
streamlit run app.pyThe agent is also exposed as a REST API via FastAPI. Run both servers in separate terminals:
Terminal 1 β API server:
uvicorn api:app --host 0.0.0.0 --port 8000 --reloadTerminal 2 β Streamlit UI:
streamlit run app.pyOnce the API is running, the Streamlit sidebar shows ⬀ API Online and all queries are routed through FastAPI.
API documentation is auto-generated at http://localhost:8000/docs
| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Health check |
/health |
GET | Health check |
/query |
POST | Text query with optional chat history |
/query-with-image |
POST | Vision + text query |
| Action | How |
|---|---|
| Ask a clinical question | Type in the chat input and press Enter |
| Upload a medical image | Use the sidebar file uploader (JPG/PNG) |
| Combined query | Upload an image AND ask a question β both are processed together |
| Clear conversation | Click π Clear Conversation in the sidebar |
Every response shows where the answer came from:
| Badge | Meaning |
|---|---|
π Knowledge Base β Pages X, Y |
Retrieved from Gale Encyclopedia of Medicine |
π Web Search β external source |
Knowledge base lacked context, DuckDuckGo was used |
The vision model automatically detects and applies specialist prompts for:
| Image Type | Specialist Prompt Applied |
|---|---|
| Chest X-Ray | Radiologist β lung fields, cardiac, mediastinum, diaphragm |
| Brain MRI | Neuroradiologist β parenchyma, ventricles, white matter |
| CT Scan | Radiologist β density, Hounsfield units |
| Bone X-Ray | Musculoskeletal radiologist β fractures, joint spaces |
| Skin | Dermatologist β ABCDE criteria |
| Ultrasound | Sonographer β echogenicity, vascularity |
Evaluated on 5 clinical questions using a custom LLM-as-judge framework (Llama 3.3 70B):
| Metric | Score | Meaning |
|---|---|---|
| Answer Faithfulness | 0.85 β | Answers stay grounded in retrieved context |
| Answer Relevancy | 0.80 β | Answers directly address the questions asked |
| Context Precision | 0.60 β | Retrieved chunks are relevant to the query |
| Overall Average | 0.75 |
Evaluated using
NeuML/pubmedbert-base-embeddingsover the Gale Encyclopedia of Medicine (5 questions). Evaluation script:evaluate.py
| Variable | Description | Required |
|---|---|---|
GROQ_API_KEY |
Your Groq API key for LLM inference | β Yes |
This tool is intended as a decision aid for qualified healthcare professionals only. It does not replace professional clinical diagnosis, medical advice, or treatment decisions. Always consult a licensed medical professional for patient care.
This project is licensed under the MIT License.

