Skip to content

Latest commit

Β 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧬 RO MEDRAG · Healthcare Research Intelligence Platform

Production-grade agentic RAG system for AI-powered medical literature analysis with real-time streaming, PubMed integration, automated evaluation, and evidence-based citations.

Python FastAPI React Claude FAISS PubMed Docker GCP Live Demo

πŸ”— Live Demo: www.romedrag.me Β· πŸ“‘ API Docs: Swagger UI Β· πŸŽ₯ Register β†’ Upload PDF β†’ Ask Questions


πŸ“Έ Screenshots

Dashboard

Dashboard with 3D DNA visualization, stats, and quick actions

Document Library

PDF upload with drag-drop and processing status

PubMed Live Search

PubMed search returning real medical articles with abstracts

RAG Evaluation Dashboard

Automated RAG quality metrics: faithfulness, relevancy, precision, citation accuracy

Evaluation Results

Per-query evaluation results: faithfulness, answer relevancy, context precision, and citation accuracy scores

🎯 What It Does

RO MEDRAG is an AI-powered research assistant that lets medical researchers upload PDF papers, ask natural language questions, and receive evidence-based answers with source citations, all streamed in real-time.

Unlike basic RAG demos, this system features an autonomous LangGraph agent that:

  • Routes queries intelligently: decides whether to retrieve, summarize, compare, extract data, or ask for clarification
  • Self-corrects: validates its own answers against source material and regenerates if grounding fails
  • Re-queries automatically: reformulates search queries when initial retrieval is insufficient
  • Searches live literature: falls back to PubMed's 36M+ article database when local docs aren't enough
  • Measures its own quality: built-in RAGAS-style evaluation pipeline with automated metrics

Every answer is grounded in documents. Hallucination is structurally prevented.


πŸ—οΈ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         React Frontend                              β”‚
β”‚   Dashboard Β· Chat (SSE Streaming) Β· Documents Β· PubMed Β· Eval     β”‚
β”‚   Three.js 3D DNA Β· Framer Motion Β· Audiowide/Urbanist Typography  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ REST + SSE
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      FastAPI Backend                                β”‚
β”‚                                                                     β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ LangGraph Agent ──────────────────────────┐  β”‚
β”‚  β”‚                                                               β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                                 β”‚  β”‚
β”‚  β”‚  β”‚  Router   β”‚ β†’ Classify intent (retrieve/summarize/         β”‚  β”‚
β”‚  β”‚  β”‚  Node     β”‚   compare/extract/clarify)                     β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜                                                 β”‚  β”‚
β”‚  β”‚       β”‚                                                       β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚  β”‚
β”‚  β”‚  β”‚ Retrieve  │◄──│ Reformulate │◄──│  Evaluate     β”‚         β”‚  β”‚
β”‚  β”‚  β”‚ (FAISS)   │──►│ Query       │──►│  Sufficiency  β”‚         β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚  β”‚
β”‚  β”‚       β”‚                                                       β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                             β”‚  β”‚
β”‚  β”‚  β”‚ Generate  │──►│  Validate   │──► Self-correction loop     β”‚  β”‚
β”‚  β”‚  β”‚ (Claude)  │◄──│  Grounding  β”‚                             β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                             β”‚  β”‚
β”‚  β”‚       β”‚                                                       β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”                                                 β”‚  β”‚
β”‚  β”‚  β”‚ Finalize  β”‚ β†’ Citations + Metrics + Confidence             β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                                 β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                                     β”‚
β”‚  Services: EmbeddingService Β· FAISSVectorStore Β· PubMedService     β”‚
β”‚            EvalService Β· ConversationMemory Β· PDFParser             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚    PostgreSQL + FAISS    β”‚
              β”‚  Users Β· Documents Β·     β”‚
              β”‚  Chunks Β· Sessions Β·     β”‚
              β”‚  Messages Β· Citations    β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

βš™οΈ Tech Stack

Layer Technology Why
AI Agent LangGraph (custom) Autonomous routing, retrieval loops, self-correction
LLM Claude Sonnet 4 Best instruction-following, lowest hallucination
Streaming SSE (Server-Sent Events) Real-time token-by-token output
Embeddings OpenAI text-embedding-3-small Best price/quality ratio (1536-dim)
Vector DB FAISS (IndexFlatIP) Zero cost, cosine similarity on normalized vectors
Literature PubMed E-utilities API 36M+ medical articles, free, no API key needed
Evaluation Custom RAGAS-style metrics Faithfulness, relevancy, precision, citation accuracy
Backend FastAPI + asyncpg Async, fast, typed, production-ready
Database PostgreSQL 16 Relational integrity for citations and audit trail
PDF pypdf + pdfminer fallback Handles messy/scanned PDFs with dual-parser strategy
Frontend React 18 + Three.js 3D DNA helix, Framer Motion animations
Typography Audiowide + Urbanist Cyber-medical display + clean readable body
Auth JWT (access + refresh) + bcrypt Stateless, secure, with token rotation
Deploy Docker + GCP Cloud Run Serverless, auto-scale, zero-downtime

🧠 Key Features

1. Agentic RAG with LangGraph

Not a simple retrieve-and-generate pipeline. The agent autonomously:

  • Routes queries to the optimal action (retrieve / summarize / compare / extract / clarify)
  • Re-queries with reformulated terms when initial retrieval scores are low
  • Validates answers against source chunks and regenerates if grounding fails
  • Maintains conversation memory across turns for multi-turn research sessions

2. Real-Time Streaming (SSE)

Responses stream token-by-token via Server-Sent Events, with live status updates:

Analyzing query... β†’ Searching documents... β†’ Found 4 passages β†’ Generating response...

No more staring at a loading spinner for 20 seconds.

3. PubMed Live Search

When uploaded documents don't have enough information, search PubMed's database of 36M+ biomedical articles. Results include titles, authors, journals, abstracts, and direct links to PubMed.

4. RAG Evaluation Pipeline

Automated quality metrics inspired by RAGAS:

  • Faithfulness: Is the answer grounded in retrieved context?
  • Answer Relevancy: Does it address the question?
  • Context Precision: Are retrieved chunks relevant?
  • Citation Accuracy: Do [N] references map to real chunks?

Run the full suite or test individual queries. Track pass rates over time.

5. Citation-Linked PDF Viewer

Click any citation β†’ the original PDF opens in a modal, scrolled to the exact page. PubMed citations link directly to the article.

6. Dark/Light Theme

Toggle between dark glassmorphism and clean light mode. Persists across sessions.

7. Mobile Responsive

Collapsible sidebar with hamburger menu, stacked layouts, touch-friendly sizing.


πŸ” Healthcare Safety

Non-negotiable safety constraints built into the system:

Constraint Implementation
Zero hallucination LLM sees only retrieved chunks, never generates from memory
Mandatory citations Every claim must include [1], [2] references
Insufficient evidence path Agent returns "INSUFFICIENT_EVIDENCE" when context is absent
Self-validation Agent cross-checks its own answer against sources
Medical disclaimer Every response ends with "⚠️ Not medical advice"
Deterministic output temperature=0 for reproducible results

πŸš€ Quick Start (5 minutes)

Prerequisites

  • Docker & Docker Compose
  • Anthropic API key (get one)
  • OpenAI API key (get one), for embeddings only

1. Clone and configure

git clone https://github.com/rohithkandula19/Ro-MedRag.git
cd Ro-MedRag

cp backend/.env.example backend/.env
# Edit backend/.env with your API keys

2. Start with Docker Compose

docker-compose up --build

3. Open the app

http://localhost:3000

Register β†’ Upload a PDF β†’ Start asking questions.


πŸ”§ Manual Setup (without Docker)

Backend

cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
mkdir -p data/uploads data/faiss_index

# Start PostgreSQL
docker run -d --name postgres -e POSTGRES_PASSWORD=postgres \
  -e POSTGRES_DB=healthcare_rag -p 5432:5432 postgres:16-alpine

# Start backend
uvicorn app.main:app --reload --port 8000

Frontend

cd frontend
npm install
npm run dev

πŸ”Œ API Reference

Core Endpoints

Method Endpoint Description
POST /api/auth/register Create account
POST /api/auth/login Get JWT tokens
GET /api/auth/me Current user
POST /api/documents/upload Upload PDF (async processing)
GET /api/documents/ List documents
DELETE /api/documents/{id} Delete + remove from vector store
POST /api/chat/sessions Create chat session
POST /api/chat/sessions/{id}/query RAG query (standard)
POST /api/chat/sessions/{id}/stream RAG query (SSE streaming)
GET /api/chat/sessions/{id}/messages Chat history with citations

New v2 Endpoints

Method Endpoint Description
GET /api/pubmed/search?q=... Search PubMed articles
POST /api/eval/run Run full evaluation suite
POST /api/eval/single Evaluate single query
GET /api/viewer/{id}/pdf Serve PDF for citation viewer
GET /api/health/ Health check

Interactive docs: http://localhost:8000/api/docs


πŸ“ Project Structure

ro-medrag/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py                    - FastAPI app, middleware, all route registration
β”‚   β”‚   β”œβ”€β”€ core/
β”‚   β”‚   β”‚   β”œβ”€β”€ config.py              - Settings (pydantic-settings, env vars)
β”‚   β”‚   β”‚   β”œβ”€β”€ security.py            - JWT auth, bcrypt, dependencies
β”‚   β”‚   β”‚   └── logging.py             - Structured JSON logging
β”‚   β”‚   β”œβ”€β”€ db/
β”‚   β”‚   β”‚   └── database.py            - Async SQLAlchemy engine + sessions
β”‚   β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”‚   β”œβ”€β”€ user.py                - User, Document, ChatSession ORM models
β”‚   β”‚   β”‚   β”œβ”€β”€ chat.py                - Message, Citation models
β”‚   β”‚   β”‚   └── document.py            - Document, DocumentChunk models
β”‚   β”‚   β”œβ”€β”€ api/routes/
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py                - Register, login, me
β”‚   β”‚   β”‚   β”œβ”€β”€ documents.py           - Upload, list, delete
β”‚   β”‚   β”‚   β”œβ”€β”€ chat.py                - Sessions, messages, query
β”‚   β”‚   β”‚   β”œβ”€β”€ streaming.py           - SSE streaming endpoint
β”‚   β”‚   β”‚   β”œβ”€β”€ pubmed.py              - PubMed search API
β”‚   β”‚   β”‚   β”œβ”€β”€ eval.py                - RAG evaluation endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ viewer.py              - PDF serving for citation viewer
β”‚   β”‚   β”‚   β”œβ”€β”€ admin.py               - Admin statistics
β”‚   β”‚   β”‚   └── health.py              - Health checks
β”‚   β”‚   └── services/
β”‚   β”‚       β”œβ”€β”€ rag_pipeline.py        - Core RAG: parse β†’ chunk β†’ embed β†’ retrieve β†’ generate
β”‚   β”‚       β”œβ”€β”€ agent/                 - β˜… LangGraph Agent System
β”‚   β”‚       β”‚   β”œβ”€β”€ state.py           - AgentState dataclass (shared graph state)
β”‚   β”‚       β”‚   β”œβ”€β”€ tools.py           - 6 agent tools (retrieval, reformulation, comparison, 
β”‚   β”‚       β”‚   β”‚                        extraction, sufficiency evaluation, answer validation)
β”‚   β”‚       β”‚   β”œβ”€β”€ nodes.py           - 8 graph nodes (route, retrieve, evaluate, reformulate,
β”‚   β”‚       β”‚   β”‚                        compare, generate, validate, finalize)
β”‚   β”‚       β”‚   └── graph.py           - MedRAGAgent orchestrator with conditional loops
β”‚   β”‚       β”œβ”€β”€ pubmed_service.py      - PubMed E-utilities integration
β”‚   β”‚       β”œβ”€β”€ eval_service.py        - RAGAS-style evaluation metrics
β”‚   β”‚       β”œβ”€β”€ chat_service.py        - Session & query orchestration
β”‚   β”‚       β”œβ”€β”€ document_service.py    - Upload & async ingestion
β”‚   β”‚       └── user_service.py        - Auth CRUD
β”‚   β”œβ”€β”€ requirements.txt
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── .env.example
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ App.jsx                    - Route definitions (6 pages)
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ ui/Layout.jsx          - Collapsible icon sidebar, theme toggle
β”‚   β”‚   β”‚   β”œβ”€β”€ auth/LoginPage.jsx     - Login/register with 3D background
β”‚   β”‚   β”‚   β”œβ”€β”€ dashboard/Dashboard.jsx - Stats, quick actions, 3D hero
β”‚   β”‚   β”‚   β”œβ”€β”€ chat/ChatPage.jsx      - β˜… Streaming chat with SSE, citation cards, PDF viewer
β”‚   β”‚   β”‚   β”œβ”€β”€ documents/DocumentsPage.jsx - Drag-drop upload, status tracking
β”‚   β”‚   β”‚   β”œβ”€β”€ evaluation/EvaluationPage.jsx - β˜… RAG metrics dashboard
β”‚   β”‚   β”‚   β”œβ”€β”€ pubmed/PubMedPage.jsx  - β˜… PubMed article search
β”‚   β”‚   β”‚   └── 3d/Scene3D.jsx         - Three.js DNA helix + particles
β”‚   β”‚   β”œβ”€β”€ store/
β”‚   β”‚   β”‚   β”œβ”€β”€ authStore.js           - Zustand auth state
β”‚   β”‚   β”‚   └── themeStore.js          - Dark/light theme persistence
β”‚   β”‚   β”œβ”€β”€ services/api.js            - Axios client with JWT interceptors
β”‚   β”‚   └── styles/globals.css         - Design system (Audiowide + Urbanist)
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ vite.config.js
β”‚   └── package.json
β”œβ”€β”€ scripts/
β”‚   └── deploy_gcp.sh                  - Full GCP deployment (Cloud Run + Cloud SQL)
β”œβ”€β”€ docker-compose.yml
└── README.md

🧠 Agent Architecture Deep Dive

The LangGraph agent uses a graph-based workflow with conditional edges:

Query β†’ Router β†’ [Action Decision]
                    β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β–Ό         β–Ό          β–Ό          β–Ό
      Retrieve   Compare   Extract    Clarify
         β”‚         β”‚          β”‚          β”‚
         β–Ό         β”‚          β”‚          β–Ό
    β”Œβ”€Evaluate─┐   β”‚          β”‚       Finalize
    β”‚Sufficient?β”‚   β”‚          β”‚
    β””β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”˜   β”‚          β”‚
   Yes β”‚   β”‚ No    β”‚          β”‚
       β”‚   β–Ό       β”‚          β”‚
       β”‚ Reformulate           β”‚
       β”‚   β”‚       β”‚          β”‚
       β”‚   β””β–ΊRetrieve          β”‚
       β–Ό                       β–Ό
    Generate β—„β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
    Validate ──┐
       β”‚       β”‚ Failed
       β”‚       β””β–ΊRegenerate
       β–Ό
    Finalize β†’ Response + Citations + Metrics

Key behaviors:

  • Retrieval loop: Up to 3 attempts with automatic query reformulation
  • Validation loop: Up to 2 regeneration attempts if grounding check fails
  • Conversation memory: Agent remembers previous turns for multi-turn research
  • Confidence scoring: Average relevance score across retrieved chunks

πŸ“Š RAG Evaluation Metrics

Metric What It Measures How
Faithfulness Is every claim supported by context? LLM-as-judge grounding check
Answer Relevancy Does the answer address the question? LLM-as-judge relevance rating
Context Precision Are retrieved chunks relevant? Keyword overlap + expected term matching
Citation Accuracy Do [N] references exist in retrieved chunks? Regex extraction + set comparison

Run via API: POST /api/eval/run or from the Evaluation page in the UI.


☁️ GCP Deployment

chmod +x scripts/deploy_gcp.sh
./scripts/deploy_gcp.sh YOUR_PROJECT_ID us-central1

The script handles: Cloud SQL setup, GCS bucket with versioning, Secret Manager for API keys, Artifact Registry for Docker images, Cloud Run deployment with auto-scaling.

Estimated cost: ~$22/month for light usage.


πŸ’Ό Skills Demonstrated

β€’ Architected LangGraph agentic RAG system with autonomous query routing,
  iterative retrieval with reformulation, and self-correcting answer validation
β€’ Implemented real-time SSE streaming for token-by-token LLM responses
  with multi-phase status updates across the agent pipeline
β€’ Integrated PubMed E-utilities API as a live literature search tool,
  enabling the agent to augment local documents with 36M+ medical articles
β€’ Built automated RAG evaluation pipeline measuring faithfulness, answer
  relevancy, context precision, and citation accuracy (RAGAS-inspired)
β€’ Designed citation-linked PDF viewer with page-level deep linking
  for source verification in a medical research context
β€’ Engineered full-stack React + FastAPI application with JWT auth,
  async PostgreSQL, FAISS vector search, and Docker deployment
β€’ Created responsive UI with dark/light theme, collapsible icon sidebar,
  3D Three.js visualization, and Audiowide/Urbanist typography system

⚠️ Disclaimer

This software is not FDA approved and must not be used for clinical decision-making.

For research and educational purposes only. Always consult qualified healthcare professionals for medical decisions.


πŸ“„ License

MIT. For research and educational purposes.

About

Production agentic RAG system for medical literature analysis. LangGraph + Claude Sonnet + FAISS + PubMed API deployed on GCP Cloud Run with SSE streaming.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages