Skip to content

Latest commit

Β 

History

65 Commits

Folders and files

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

Repository files navigation

3C: Clinical Code Collector πŸ₯πŸ€–

3C - Clinical_Code_Collector_Workflow_Final

An AI-assisted, agentic pipeline for defensible, explainable, and auditable SNOMED CT clinical code discovery β€” built for NICE (National Institute for Health and Care Excellence).

Python LangGraph LangChain OpenAI ChromaDB Deployed


πŸ“‹ The Problem

Defining patient cohorts for healthcare analysis still relies on highly manual clinical code-list construction across fragmented sources. Analysts at NICE must navigate QOF rules, NHS Digital reference sets, and SNOMED-based resources to justify code selections β€” a process that is slow, inconsistent, and difficult to audit, especially for complex multi-morbidity phenotypes like obesity with Type 2 diabetes.


πŸ’‘ The Solution

3C transforms a plain English clinical research question into a validated, evidence-grounded, and human-reviewable shortlist of SNOMED CT codes using a hybrid AI + deterministic pipeline β€” combining the semantic flexibility of LLMs with the reliability of deterministic NHS API searches.

"Not just an LLM generating codes β€” a defensible, explainable, and fully auditable evidence trail for expert review."


πŸ—οΈ Architecture

3C is orchestrated as a stateful 4-node LangGraph pipeline, blending LLM reasoning with deterministic data retrieval:

[Free-text clinical question]
        β”‚
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Node 1 Β· Query Understanding             β”‚
β”‚  GPT-4o-mini β†’ 14-field structured JSON   β”‚
β”‚  + NHS FHIR Terminology Server API        β”‚
β”‚  (SNOMED synonym enrichment)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Node 2 Β· Deterministic SNOMED Search     β”‚
β”‚  NHS FHIR API Β· Concept hierarchy         β”‚
β”‚  traversal across diagnosis / meds /      β”‚
β”‚  observations branches (max recall)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Node 3 Β· Deterministic Validator         β”‚
β”‚  Scoring: provenance + consensus +        β”‚
β”‚  cosine similarity + course-check         β”‚
β”‚  β†’ Tier 1 / Tier 2 / Tier 3 assignment   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Node 4 Β· Justification via RAG           β”‚
β”‚  LangChain: ChromaDB + MMR retrieval      β”‚
β”‚  text-embedding-3-small (1536 dims)       β”‚
β”‚  GPT-4o-mini β†’ exact NICE/QOF evidence   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚
                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Human Review Interface                   β”‚
β”‚  Code cards Β· tier badges Β· provenance   β”‚
β”‚  flags Β· confidence scores               β”‚
β”‚  Accept / Reject / Skip + feedback loop  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Architectural Philosophy: Hybrid LLM + Deterministic

Component Approach Why
Query understanding LLM (GPT-4o-mini) Rules-based parsers fail on implicit context and compound phenotypes
SNOMED search Deterministic (NHS FHIR API) Guarantees auditability; no hallucinated codes
Validation Deterministic (scoring model) Patient safety requires reproducible, traceable decisions
Justification RAG (LangChain + Chroma) Grounds every output in official NICE/QOF guideline text
Orchestration LangGraph (stateful graph) Full state traceability across nodes; deterministic routing

πŸ› οΈ Tech Stack

Layer Technology
Agentic Orchestration LangGraph (stateful 4-node pipeline)
RAG Framework LangChain (ChromaDB, MMR retrieval, PyMuPDFLoader, RecursiveCharacterTextSplitter)
LLM OpenAI GPT-4o-mini
Embeddings OpenAI text-embedding-3-small (1536 dimensions)
Vector Store ChromaDB
Clinical APIs NHS FHIR Terminology Server, QOF rules, NHS Digital Reference Sets, OpenCodelists
Document Parsing PyMuPDFLoader + RecursiveCharacterTextSplitter (chunk size: 800)
Retrieval Strategy Maximal Marginal Relevance (MMR) β€” diverse, non-redundant evidence chunks
Deployment Render (free tier, 512MB RAM CPU)
Language Python 3.10+

🧠 Key Design Decisions

RAG Approach: Code-Level Retrieval (Approach C)

Three RAG approaches were evaluated:

  • Approach A β€” Pre-RAG: structure query with RAG, then justify codes
  • Approach B β€” Post-RAG: vectorise structured query fields for chunk retrieval
  • Approach C βœ… (Selected) β€” Post-RAG: vectorise each individual SNOMED code's short description to target the most relevant guideline chunks per code, then LLM judges relevance for the research question

Approach C produced richer, more code-specific justification text across a wider range of clinically relevant codes.

Validation Scoring Model

Each candidate code is scored across four signals:

Signal Weight Description
Provenance High Code appears in QOF rules, NHS Digital refsets, or OpenCodelists
Consensus Bonus Code appears across multiple independent sources
Semantic Similarity Medium Cosine similarity between code definition and target concept
Course Consistency Adjustment Acute/chronic alignment with clinical context

Codes scoring below 0.25 are excluded. Remaining codes are tiered (Tier 1 / 2 / 3) for human review.

Human-in-the-Loop by Design

AI is a decision-support tool, not an automated decision-maker. The human review interface presents code cards with full provenance, tier badges, and evidence quotes β€” users accept, reject, or skip each code. Feedback is stored persistently to improve future runs.


πŸ“Š Results

Test Case: "Obesity with Type 2 Diabetes"

Pipeline Stage Result
Candidate codes retrieved 70
Validated codes (post-scoring) 54
Tier 1 β€” high confidence 13
Tier 2 / 3 β€” human review required 41

Highlights:

  • Type 2 diabetes mellitus, Brittle type 2 diabetes mellitus, and 11 more correctly promoted to Tier 1 (confidence: 0.85), appearing across QOF, NHS Digital, and OpenCodelists
  • Type 2 diabetes mellitus in obese achieved highest semantic score (0.81) with correct NICE NG28 evidence quote (p.48)
  • Dapagliflozin correctly justified using NICE NG28 (p.98): "Licensed for adults with type 2 diabetes…"
  • Cost per query: ~Β£0.01 | Runtime: ~2 minutes | Deployed on 512MB CPU (no GPU required)

βš™οΈ Implementation & Economics

Metric Value
Cost per query ~Β£0.01
Average runtime ~2 minutes
Infrastructure Render free tier (512MB RAM, CPU only)
LLM calls per query Query understanding (1Γ—) + Code justification (1Γ— per validated code)
Embedding calls ~50+ SNOMED codes vectorised per query

⚠️ Known Limitations & Future Work

  • Exclusion shield needs hardening β€” paediatric and gestational phenotypes are occasionally included incorrectly
  • Tier calibration needs refinement β€” codes with very high semantic similarity (e.g., 0.81) can be downgraded to Tier 2 if absent from QOF/NHS curated lists
  • Human feedback loop is stored but not yet integrated back into the scoring pipeline
  • Precision vs. recall trade-off β€” the system currently excels at high-recall discovery; future work focuses on improving shortlist precision without losing rare but relevant codes
  • Benchmarking against expert-reviewed gold-standard code lists is in progress

πŸ“ Repository Structure

β”œβ”€β”€ src/                    # Core pipeline code
β”‚   β”œβ”€β”€ pipeline/           # LangGraph node definitions
β”‚   β”œβ”€β”€ rag/                # LangChain RAG components (Chroma, embeddings, MMR)
β”‚   β”œβ”€β”€ validator/          # Deterministic scoring & tier assignment
β”‚   └── api/                # NHS FHIR API integration
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ LAB-FAM-NICE-Final-Report.pdf      # Full technical report
β”‚   └── 3C_Clinical_Code_Collector_presentation.pdf  # Presentation deck
└── README.md

πŸ‘₯ Team & Attribution

Cambridge Data Science Career Accelerator β€” LAB FAM Group 2 | April 2026

Name Role
Ali Aydin Yildiz Technical Lead β€” pipeline architecture, LangGraph/LangChain engineering, RAG design, deployment
Angelo di Legge Team member
Bihter Ekin Kaplanlioglu Team member
Farhio Ali Team member
Jegamohan Vicneswararajah Team member
Lemmy Emasit Team member

πŸ“„ Documentation


πŸ”— Related