HeartSafe RAG
A retrieve-or-refuse Retrieval-Augmented Generation system for heart failure clinical decision support, grounded exclusively in the 2022 AHA/ACC Heart Failure Guidelines with an 8-engine guardrail validation pipeline.
The Clinical Problem
"Generic LLMs fabricate Class of Recommendation levels, make up LVEF thresholds, and recommend therapies not in the AHA/ACC guidelines. In cardiology, a wrong number means a dead patient."
How It Works
1. Guideline Ingestion (Offline)
- →PDF extraction via PyMuPDF (text), pdfplumber (tables), Tesseract (OCR)
- →Section-aware chunking: GuidelineChunker extracts headers and COR boundaries
- →Dual indexing: FAISS (dense semantic) + BM25 (sparse keyword)
- →Ensemble retriever: 0.6 FAISS + 0.4 BM25 weighted blend
2. Query Processing (Online)
- →HyDE (Hypothetical Document Embeddings) for query expansion
- →Multi-Query generation: 3 variant queries for better recall
- →PubMedBERT embeddings via sentence-transformers
3. Hybrid Retrieval
- →Ensemble retriever over FAISS + BM25 indexes
- →Top-k retrieval: n_sentences_to_retrieve configurable
- →Retrieval-required policy: no generation without retrieved context
4. LLM Generation
- →Llama-3.1-8b-instant via Groq API at temperature 0.0
- →Structured prompt with retrieved chunks as context
- →JSON output with reasoning steps, claim types, and COR/LOE citations
5. VLM-Guard Validation (8 Rules)
- →CORLevelRule: validates against 33-entry internal COR/LOE lookup
- →LVEFThresholdRule: enforces HFrEF ≤40, HFmrEF 41-49, HFpEF ≥50
- →DrugClassRule: validates GDMT drug-class relationships
- →ContraindicationRule: flags e.g., MRA with K+ >5.0
- →AnswerConsistencyRule: cross-validates final answer against all claims
- →AnswerCORCrossCheckRule: catches non-standard formats like 'Class A'
Safety & Trust
Every output passes through deterministic, auditable guardrails before reaching the user. No black boxes. No unseen failures.
Validates every Class of Recommendation and Level of Evidence against a 34-entry CSV lookup table. Catches invented or misattributed COR levels.
Automatically corrects LVEF misclassification (e.g., HFrEF vs HFpEF) based on guideline-defined thresholds (≤40, 41-49, ≥50).
Blocks recommendations that violate known contraindications: MRA with K+ >5.0, ARNi with history of angioedema, etc.
If no relevant guideline chunks are retrieved, the system refuses to answer instead of generating an unsupported response.
About This Project
HeartSafe RAG addresses the critical problem of LLM hallucination in cardiology AI. When a generic chatbot is asked a heart failure question, it may invent guideline recommendations or misclassify therapies. HeartSafe RAG solves this with a two-phase design: an offline ingestion pipeline that processes 200+ pages of AHA/ACC guidelines into section-aware chunks with FAISS + BM25 indexes, and an online serving pipeline that retrieves the most relevant chunks before generating any response. Every generated claim then passes through 8 cardiology-specific validation rules before the answer is returned. If the guidelines lack sufficient information, the system is designed to say so and refuse, rather than fabricate an answer.
Key Features
- Retrieve-or-refuse policy: no answer without supporting guideline context
- Hybrid retrieval: FAISS (dense) + BM25 (sparse) ensemble
- Query expansion via HyDE + Multi-Query (3 variants)
- PubMedBERT domain-optimized embeddings
- 8 cardiology-specific validation rules via VLM-Guard
- Structured JSON output with reasoning steps and source citations
- Interactive chat UI with validation audit trail display
- 40-question golden dataset for evaluation
- Langfuse observability for full trace logging
- Response caching (5 min TTL, 128 entries)
Impact
- →Reduces hallucination risk through retrieval enforcement, structured output and rule-based validation (risk reduction, not elimination)
- →Grounded exclusively in official AHA/ACC 2022 Heart Failure Guidelines
- →8-rule validation pipeline checks COR/LOE, LVEF thresholds, drug classes, contraindications and answer consistency before an answer is shown
- →Open source on GitHub: available for community audit and contribution
- →40 curated golden QA pairs for regression testing and quality monitoring
Technology Stack
Related articles
Building a clinical AI system of your own?
The Clinical AI Reliability Audit applies the same kind of evaluation and safety engineering to your system: architecture and risk discovery, evaluation and root cause analysis, and targeted engineering improvements. For a working healthcare app built with AI coding tools, the Clinical Prototype Readiness Review is the smaller starting point.
Related Projects
This project is part of a connected ecosystem. Each piece reinforces the same mission: safe, trustworthy clinical AI.
VLM-Guard
A composable rule-based verification layer that catches physically impossible, biologically inconsis...
NTD-Assist
An offline multimodal AI prototype for microscopy-based NTD screening, with 12 rule-based biological...
MedVision Assist
A Clinical Decision Support System for radiologists that drafts 90%-complete preliminary imaging rep...