A full-stack AI agent that intelligently routes natural language queries between a structured SQL database (patient metrics) and an unstructured vector database (clinical documents). Packaged as decoupled microservices in Docker containers and deployed on AWS EC2.
Live deployment on AWS EC2 (eu-north-1) — natural language input → autonomous tool selection → real-time SQL or RAG response
+---------------------------------+
| Next.js Web GUI |
| (Port 3000 - Chat UI) |
+---------------------------------+
|
| HTTP POST /chat (User Query)
v
+---------------------------------+
| FastAPI Backend |
| (Port 8000 - API Router) |
+---------------------------------+
|
| LangGraph Tool Routing
v
+---------------------------------+
| AI Inference Engine |
| (Groq LLM + LangGraph Agent) |
+---------------------------------+
/ \
SQL Tool RAG Tool
v v
+----------------+ +----------------+
| SQLite Patient | | ChromaDB Vector|
| Database | | Index (PDFs) |
+----------------+ +----------------+
- Frontend (Next.js): Modern chat interface for interacting with the AI agent in real time.
- Backend (FastAPI + LangGraph): Asynchronous API that receives queries and uses an LLM to autonomously decide whether to execute a SQL query or a vector similarity search — the agent picks the right tool without being told.
- Databases: SQLite for structured patient tabular data; ChromaDB for unstructured medical PDF knowledge.
- Containerisation: Multi-container setup via Docker Compose V2, optimised for resource-constrained cloud environments (AWS EC2
t3.micro).
| Capability | Detail |
|---|---|
| Structured queries | SQL via Pandas — patient counts, cholesterol levels, tabular filtering |
| Unstructured queries | RAG via ChromaDB + HuggingFace embeddings — clinical definitions, medical guidelines from PDFs |
| Autonomous routing | LangGraph agent selects SQL or RAG based on query intent — no manual switching |
| Session memory | Conversation context persists across turns within a session |
| Deployment | Live on AWS EC2 — Docker Compose orchestrated, swap-optimised for free tier |
| Layer | Technology |
|---|---|
| AI & Orchestration | LangChain, LangGraph, Groq API, HuggingFace Embeddings |
| Databases | ChromaDB (vector), SQLite, Pandas |
| API | FastAPI, Uvicorn |
| Frontend | Next.js, React, Node.js, Tailwind CSS |
| Containerisation | Docker, Docker Compose V2 |
| Cloud | AWS EC2 (Ubuntu Linux, eu-north-1) |
medical-knowledge-agent/
│
├── data/ # Vector DB and SQLite — generated locally
│ ├── chroma_db/ # ChromaDB embeddings (git-ignored)
│ └── patient_db.db # SQLite patient database (git-ignored)
├── logs/ # Application and container runtime logs
├── src/
│ ├── agent.py # LangGraph tool definitions and routing logic
│ ├── app.py # Backend entry point
│ ├── rag_tool.py # RAG retrieval pipeline
│ ├── rag_test.py # Unit tests for vector retrieval
│ ├── server.py # FastAPI endpoints and CORS configuration
│ ├── setup_db.py # PDF ingestion and SQLite initialisation
│ └── test_db.py # Database connection validation
├── frontend/
│ ├── public/ # Static assets
│ └── src/app/
│ ├── page.js # Main chat UI component
│ ├── layout.js # Next.js root layout
│ └── globals.css # Tailwind and global styles
├── Dockerfile.backend # Docker image — FastAPI backend
├── Dockerfile.frontend # Docker image — Next.js frontend
├── docker-compose.yml # Container orchestration
├── requirements.txt # Python dependencies (CPU-optimised)
└── README.md
Data note: The
data/directory is generated dynamically on the host usingsetup_db.pyand mounted into containers at runtime — keeping the Docker image lean and protecting sensitive data.
- Docker and Docker Compose V2 installed
- A Groq API key (free)
git clone https://github.com/sachinkumarp-code/medical-knowledge-agent
cd medical-knowledge-agent
# Create a .env file with your API key
echo "GROQ_API_KEY=your_key_here" > .env
# Initialise databases locally (run once)
python src/setup_db.py
# Launch the full stack
docker compose up --build -dOpen http://localhost:3000 in your browser.
"How many patients have cholesterol above 250?" → SQL tool
"What is the definition of cardiac allograft rejection?" → RAG tool
"Summarise the treatment guidelines for heart failure" → RAG tool
Deployed on EC2 t3.micro (1 vCPU, 1 GB RAM). Three optimisations prevent OOM crashes on free-tier hardware:
- Virtual RAM — configured a permanent 4 GB
/swapfileon the instance - CPU-only PyTorch — forced via
--extra-index-url https://download.pytorch.org/whl/cpuinrequirements.txt, saving over 5 GB compared to the default GPU build - Runtime data injection — databases initialised after container startup via
docker exec -it medical-knowledge-agent-backend-1 python src/setup_db.py, keeping the Docker image itself lean
| Project | Description |
|---|---|
| Medical AI Knowledge Agent (this repo) | Autonomous RAG + SQL agent with Next.js UI |
| Brain Tumor MLOps | PyTorch CNN deployed live on AWS EC2 via FastAPI + Streamlit + Docker |
| 🔒 Cardiac Histopathology Segmentation | Attention UNet, active learning, 6,600+ WSIs — ongoing thesis (confidential) |
Sachinkumar P — M.Tech., Structural & Computational Biology, IIT Roorkee
github.com/sachinkumarp-code · LinkedIn