Building a Clinical RAG System Using MIMIC-IV-Ext (DiReCT Project)
Combining Retrieval, Generation, and Clinical Reasoning with Streamlit & ChromaDB
Building a Clinical RAG System Using MIMIC-IV-Ext (DiReCT Project)
Combining Retrieval, Generation, and Clinical Reasoning with Streamlit & ChromaDB
Introduction
Retrieval-Augmented Generation (RAG) has become one of the most powerful strategies for improving the reliability and factual accuracy of large language models — especially in high-stakes domains like healthcare. Instead of relying solely on an LLM’s pre-trained knowledge, a RAG system retrieves relevant, verified information from a curated dataset and supplies it as grounded context to the model.
In this project, I designed and deployed a full end-to-end Clinical RAG pipeline using the MIMIC-IV-Ext Direct dataset, aimed at supporting diagnostic reasoning and clinical question answering. The system retrieves disease knowledge, patient case narratives, and reasoning steps — then generates context-aware answers using an LLM.
This blog post covers the full journey: data preprocessing, knowledge extraction, retrieval engineering, LLM integration, Streamlit app development, and deployment.
🔍 Motivation
Medical datasets like MIMIC are rich in structured and semi-structured knowledge, but they aren’t easily searchable by clinicians or students. Diagnostic reasoning involves several steps:
- Gathering patient narratives
- Identifying symptoms
- Linking risk factors
- Understanding cause-effect chains
- Matching patterns to diseases
LLMs are strong at generating explanations but weak at factual grounding unless given external context. This makes clinical data an ideal use case for Retrieval-Augmented Generation (RAG).
The MIMIC-IV-Ext Direct dataset contains:
- Disease knowledge graphs
- Flowcharts with symptoms and risk factors
- Hundreds of diagnostic cases with inputs + reasoning
My goal was simple: Turn this dataset into a searchable knowledge engine paired with an LLM.
📦 Dataset Exploration & Preprocessing
Dataset Structure
The dataset consists of two major components:
1️⃣ diagnostic_kg (Knowledge Graph)
Contains files like:
{condition}.json
Each file includes:
- Symptoms
- Risk factors
- Stages
- Relationships between clinical features
2️⃣ Finished (Patient Cases)
Inside this folder, each subfolder represents a disease. Each disease folder contains many JSON case files with:
- input1–input6 (patient narrative features)
- reasoning fields
- cause-effect chains like
$Cause_1,$Cause_2
Building the Preprocessor
I created a Python class SimpleDataProcessor to:
✔ Count all KG and case files ✔ Extract knowledge chunks (symptoms & risk factors) ✔ Extract patient narrative chunks ✔ Extract reasoning chains ✔ Convert everything into structured text + metadata
Example extracted chunk:
Condition: Upper_GI_Bleed
Text: Upper_GI_Bleed - Symptoms: hematemesis, melena, dizziness
Metadata: { type: "knowledge", category: "symptoms" }
For patient reasoning, I recursively processed nested JSON keys to extract lines before $Cause_ markers.
🔎 Designing the Retrieval Component
To enable fast semantic search, I used:
ChromaDB as vector database
SentenceTransformer MiniLM-L6-v2 as embedding model
I created two separate collections:
- medical_knowledge
- Symptoms
- Risk factors
- patient_cases
- Narratives
- Reasoning
This separation improves query relevance because:
- Knowledge is concise and general
- Cases are long and detailed
A query like “What causes upper GI bleeding?” retrieves:
- Relevant knowledge chunks
- Similar patient cases
- Diagnostic reasoning sequences
The retrieval system returns a set of grounded evidence that becomes the context for the LLM.
🤖 Integrating the LLM (Gemini 2.5 Flash)
Next, I built a generator class MedicalAI using Google’s Gemini 2.5 Flash model.
How generation works:
- User enters a clinical query
- RAG retrieves top-k chunks
- The chunks are merged into a structured context string
- A medical prompt is constructed
- Gemini generates a grounded explanation
Prompt style excerpt:
You are a medical expert. Use the medical context below to answer:
MEDICAL CONTEXT:
{retrieved chunks}
QUESTION:
{user query}
If the context lacks information, state what is missing.
This ensures transparency and prevents hallucinations.
🖥️ Streamlit Frontend
To make the system accessible, I created a Streamlit app with:
- A query input box
- A retrieval output panel
- A generated answer box
- Clean, simple UI for clinical testing
🚀 Live Demo https://lngasoxhfumzbykp8ybqhs.streamlit.app/
The app loads the ChromaDB index, processes queries, and displays both retrieved documents and generated responses.
☁️ Deployment Strategy
The deployment consists of two main components:
1️⃣ Backend (RAG + ChromaDB)
A self-contained Python project that:
- Loads dataset
- Embeds chunks
- Creates vector collections
- Serves queries to LLM
2️⃣ Frontend (Streamlit App)
Deployed via Streamlit Cloud.
메타데이터
- post_id
- 2f008a3143b2
- slug
- building-a-clinical-rag-system-using-mimic-iv-ext-direct-project-2f008a3143b2
- url
- https://medium.com/@f223281/building-a-clinical-rag-system-using-mimic-iv-ext-direct-project-2f008a3143b2
- canonical_url
- https://medium.com/@f223281/building-a-clinical-rag-system-using-mimic-iv-ext-direct-project-2f008a3143b2
- author_url
- https://medium.com/@f223281
- status
- ok
- fetched_at
- 2026-06-22 05:41:33