terraphim/medgemma-competition

Rust

0

172 commits

updated Feb 25, 2026

See the code

README

Terraphim Clinical Pipeline: Graph-Based Safety Gates for MedGemma -- From Class Suggestions to Specific Drug-Dose Evidence

A production-ready clinical decision support system using Google's MedGemma with Terraphim Knowledge Graph grounding. Rust multi-agent architecture with 543+ tests passing, 18/18 evaluation cases grounded, and real GGUF inference on GPU (23.5s/case) and CPU (165s/case) -- no mock fallback.

Demo video: demo-video.mp4 (85s, 1920x1080) -- recorded with Playwright against the live API with real MedGemma GPU inference.


The Problem

Raw LLMs produce vague or incorrect drug recommendations. Measured A/B comparison (ab_comparison example, reproduced 2026-02-24):

CaseRaw MedGemma (no KG)With Terraphim KG Grounding
BRAF Melanoma"BRAF inhibitor (e.g., Dabrafenib + Trametinib)" -- vague classVemurafenib 450mg once daily -- specific drug + dose
CYP2D6 CodeineOxycodone 5 mg/mL -- wrong drugCodeine 60mg q6h -- correct drug from KG
EGFR NSCLCOsimertinib 80mg (stochastic; prior run: 800mg 10x overdose)Osimertinib 80mg -- consistently correct

The knowledge graph constrains MedGemma from vague class-level suggestions to specific, evidence-validated drug-dose recommendations.


Architecture

Patient Input --> Entity Extraction (Aho-Corasick LeftmostLongest, <1ms, 1.4M SNOMED patterns)
             --> Knowledge Graph Query (SNOMED CT + PrimeKG, <1ms, symbolic embeddings)
             --> PGx Validation (CPIC guidelines, <1ms)
             --> MedGemma Inference (KG-augmented prompt, 2-5s cloud / 23.5s GPU / 165s CPU)
             --> Safety Validation (KG grounding gate -- blocks ungrounded recommendations)
             --> Grounded Clinical Recommendation
ComponentLatencyDetails
Entity Extraction<1msAho-Corasick LeftmostLongest over 1.4M SNOMED/UMLS terms
Knowledge Graph<1ms27 node types, 65 edge types, symbolic embeddings (Jaccard + path distance)
PGx Validation<1msCPIC drug-gene interaction rules
MedGemma (GPU)21-25sQ4_K_M GGUF, RTX 2070 8GB, 99 layers on CUDA
MedGemma (CPU)112-627sQ4_K_M GGUF, 2.3GB, no GPU required
MedGemma (Cloud)2-5sVertex AI generateContent API

Quick Start

Prerequisites

cargo --version   # Rust 1.70+
cp .env.template .env  # Edit with your HF_TOKEN and MEDGEMMA_GGUF_PATH

Run Tests

cargo test --workspace  # 543+ tests, ~50s

API Server with Interactive Demo

# GPU (recommended)
cargo run -p terraphim-api --features "local-gguf,cuda" --release
# CPU only
cargo run -p terraphim-api --features "local-gguf" --release

# Visit http://localhost:3001/
# Toggle to Live mode, click Run for real MedGemma inference

Playwright E2E Tests (requires API server running)

npx playwright test tests/e2e/demo-live.spec.js      # 8 functional tests
npx playwright test tests/e2e/record-demo.spec.js     # Record demo video

18-Case Evaluation Harness

cargo run -p terraphim-evaluation --features "local-gguf,cuda" --bin evaluation-runner -- \
  --cases tests/evaluation/data/smoke_suite.json \
  --output tests/evaluation/output

Full E2E Pipeline (real GGUF inference, no mock)

# GPU
cargo run --release --example e2e_pipeline --package terraphim-demo --features medgemma-client/cuda
# CPU
cargo run --release --example e2e_pipeline --package terraphim-demo

A/B Comparison (Raw MedGemma vs KG-Grounded)

cargo run --release --example ab_comparison --package terraphim-demo --features medgemma-client/cuda

Evaluation Results

MetricCPU run (b6321317)GPU run (79d26e2e)GPU run (f4af1ed9)
Cases18/18 (100%)18/18 (100%)18/18 (100%)
Safety failures000
Avg grounding score0.920.890.92
Gate: Safety100%100%100%
Gate: KG Grounding83.3%77.8%83.3%
Gate: Hygiene94.4%88.9%94.4%
Avg latency165.3s/case23.5s/case24.8s/case

Three runs using real MedGemma GGUF inference with no mock fallback. GPU delivers 7x speedup (RTX 2070, 99 layers offloaded). Gate score variation between runs reflects expected LLM output variance.

Test suite: 543+ unit/integration tests + 9 Playwright e2e tests, 0 failures


Project Structure

medgemma-competition/
  crates/
    medgemma-client/          # Multi-backend MedGemma inference (Vertex AI, GGUF, Mock)
    terraphim-api/            # Axum REST/WebSocket API server with shared LLM state
    terraphim-demo/           # CLI demo + consultation workflow + state machines
    terraphim-evaluation/     # 3-gate evaluation harness (18 cases)
    terraphim-automata/       # SNOMED/UMLS entity extraction (Aho-Corasick LeftmostLongest)
    terraphim-pgx/            # Pharmacogenomics (CPIC guidelines)
    terraphim-medical-agents/ # Multi-agent orchestration (OTP supervision, 60 state machine tests)
    terraphim-medical-roles/  # Specialist role definitions
    terraphim-medical-learning/ # Case-based learning
    terraphim-thesaurus/      # Medical term mappings
  tests/
    e2e/demo-live.spec.js     # 8 Playwright e2e tests (API, WebSocket, UI, GPU inference)
    e2e/record-demo.spec.js   # Demo video recorder (85s walkthrough)
    evaluation/
      data/smoke_suite.json   # 18 evaluation cases
      output/                 # Generated reports (JSON + Markdown)
  data/
    artifacts/                # Pre-built UMLS automata (209MB)
    snomed_thesaurus.json     # Curated SNOMED mappings
  static/
    demo.html                 # Self-contained interactive demo UI (Demo + Live modes)
    dashboard.html            # Dashboard UI
  demo-video.mp4              # 85s demo video (Playwright, real GPU inference)
  docs/demo-video/            # WebM backup of demo video
  .env.template               # Environment variable template for judges

Key Differentiator: Graph-Based Symbolic Embeddings

Unlike RAG systems with vector search, Terraphim uses symbolic graph embeddings (Jaccard 0.7 + path distance 0.3) over a typed medical knowledge graph. This provides:

  • Determinism: Same input always produces same grounding (no embedding model drift)
  • Auditability: Traceable evidence paths (Drug->Treats->Disease->HasVariant->Gene->CitedIn->Trial)
  • Safety gate: Every MedGemma recommendation validated against the KG treatment subgraph
  • LeftmostLongest matching: Entity extractor always grounds to the most specific SNOMED concept (e.g., "non-small cell lung carcinoma" not "lung carcinoma"), ensuring correct treatment lookups

Documentation


License

Dual-licensed under MIT (source code) and CC BY 4.0 (documentation and non-code content). See LICENSE for details.

Contributors

AlexMikhalev

113 commits

terraphim/medgemma-competition

Rust

0

172 commits

updated Feb 25, 2026

See the code

README

Terraphim Clinical Pipeline: Graph-Based Safety Gates for MedGemma -- From Class Suggestions to Specific Drug-Dose Evidence

A production-ready clinical decision support system using Google's MedGemma with Terraphim Knowledge Graph grounding. Rust multi-agent architecture with 543+ tests passing, 18/18 evaluation cases grounded, and real GGUF inference on GPU (23.5s/case) and CPU (165s/case) -- no mock fallback.

Demo video: demo-video.mp4 (85s, 1920x1080) -- recorded with Playwright against the live API with real MedGemma GPU inference.


The Problem

Raw LLMs produce vague or incorrect drug recommendations. Measured A/B comparison (ab_comparison example, reproduced 2026-02-24):

CaseRaw MedGemma (no KG)With Terraphim KG Grounding
BRAF Melanoma"BRAF inhibitor (e.g., Dabrafenib + Trametinib)" -- vague classVemurafenib 450mg once daily -- specific drug + dose
CYP2D6 CodeineOxycodone 5 mg/mL -- wrong drugCodeine 60mg q6h -- correct drug from KG
EGFR NSCLCOsimertinib 80mg (stochastic; prior run: 800mg 10x overdose)Osimertinib 80mg -- consistently correct

The knowledge graph constrains MedGemma from vague class-level suggestions to specific, evidence-validated drug-dose recommendations.


Architecture

Patient Input --> Entity Extraction (Aho-Corasick LeftmostLongest, <1ms, 1.4M SNOMED patterns)
             --> Knowledge Graph Query (SNOMED CT + PrimeKG, <1ms, symbolic embeddings)
             --> PGx Validation (CPIC guidelines, <1ms)
             --> MedGemma Inference (KG-augmented prompt, 2-5s cloud / 23.5s GPU / 165s CPU)
             --> Safety Validation (KG grounding gate -- blocks ungrounded recommendations)
             --> Grounded Clinical Recommendation
ComponentLatencyDetails
Entity Extraction<1msAho-Corasick LeftmostLongest over 1.4M SNOMED/UMLS terms
Knowledge Graph<1ms27 node types, 65 edge types, symbolic embeddings (Jaccard + path distance)
PGx Validation<1msCPIC drug-gene interaction rules
MedGemma (GPU)21-25sQ4_K_M GGUF, RTX 2070 8GB, 99 layers on CUDA
MedGemma (CPU)112-627sQ4_K_M GGUF, 2.3GB, no GPU required
MedGemma (Cloud)2-5sVertex AI generateContent API

Quick Start

Prerequisites

cargo --version   # Rust 1.70+
cp .env.template .env  # Edit with your HF_TOKEN and MEDGEMMA_GGUF_PATH

Run Tests

cargo test --workspace  # 543+ tests, ~50s

API Server with Interactive Demo

# GPU (recommended)
cargo run -p terraphim-api --features "local-gguf,cuda" --release
# CPU only
cargo run -p terraphim-api --features "local-gguf" --release

# Visit http://localhost:3001/
# Toggle to Live mode, click Run for real MedGemma inference

Playwright E2E Tests (requires API server running)

npx playwright test tests/e2e/demo-live.spec.js      # 8 functional tests
npx playwright test tests/e2e/record-demo.spec.js     # Record demo video

18-Case Evaluation Harness

cargo run -p terraphim-evaluation --features "local-gguf,cuda" --bin evaluation-runner -- \
  --cases tests/evaluation/data/smoke_suite.json \
  --output tests/evaluation/output

Full E2E Pipeline (real GGUF inference, no mock)

# GPU
cargo run --release --example e2e_pipeline --package terraphim-demo --features medgemma-client/cuda
# CPU
cargo run --release --example e2e_pipeline --package terraphim-demo

A/B Comparison (Raw MedGemma vs KG-Grounded)

cargo run --release --example ab_comparison --package terraphim-demo --features medgemma-client/cuda

Evaluation Results

MetricCPU run (b6321317)GPU run (79d26e2e)GPU run (f4af1ed9)
Cases18/18 (100%)18/18 (100%)18/18 (100%)
Safety failures000
Avg grounding score0.920.890.92
Gate: Safety100%100%100%
Gate: KG Grounding83.3%77.8%83.3%
Gate: Hygiene94.4%88.9%94.4%
Avg latency165.3s/case23.5s/case24.8s/case

Three runs using real MedGemma GGUF inference with no mock fallback. GPU delivers 7x speedup (RTX 2070, 99 layers offloaded). Gate score variation between runs reflects expected LLM output variance.

Test suite: 543+ unit/integration tests + 9 Playwright e2e tests, 0 failures


Project Structure

medgemma-competition/
  crates/
    medgemma-client/          # Multi-backend MedGemma inference (Vertex AI, GGUF, Mock)
    terraphim-api/            # Axum REST/WebSocket API server with shared LLM state
    terraphim-demo/           # CLI demo + consultation workflow + state machines
    terraphim-evaluation/     # 3-gate evaluation harness (18 cases)
    terraphim-automata/       # SNOMED/UMLS entity extraction (Aho-Corasick LeftmostLongest)
    terraphim-pgx/            # Pharmacogenomics (CPIC guidelines)
    terraphim-medical-agents/ # Multi-agent orchestration (OTP supervision, 60 state machine tests)
    terraphim-medical-roles/  # Specialist role definitions
    terraphim-medical-learning/ # Case-based learning
    terraphim-thesaurus/      # Medical term mappings
  tests/
    e2e/demo-live.spec.js     # 8 Playwright e2e tests (API, WebSocket, UI, GPU inference)
    e2e/record-demo.spec.js   # Demo video recorder (85s walkthrough)
    evaluation/
      data/smoke_suite.json   # 18 evaluation cases
      output/                 # Generated reports (JSON + Markdown)
  data/
    artifacts/                # Pre-built UMLS automata (209MB)
    snomed_thesaurus.json     # Curated SNOMED mappings
  static/
    demo.html                 # Self-contained interactive demo UI (Demo + Live modes)
    dashboard.html            # Dashboard UI
  demo-video.mp4              # 85s demo video (Playwright, real GPU inference)
  docs/demo-video/            # WebM backup of demo video
  .env.template               # Environment variable template for judges

Key Differentiator: Graph-Based Symbolic Embeddings

Unlike RAG systems with vector search, Terraphim uses symbolic graph embeddings (Jaccard 0.7 + path distance 0.3) over a typed medical knowledge graph. This provides:

  • Determinism: Same input always produces same grounding (no embedding model drift)
  • Auditability: Traceable evidence paths (Drug->Treats->Disease->HasVariant->Gene->CitedIn->Trial)
  • Safety gate: Every MedGemma recommendation validated against the KG treatment subgraph
  • LeftmostLongest matching: Entity extractor always grounds to the most specific SNOMED concept (e.g., "non-small cell lung carcinoma" not "lung carcinoma"), ensuring correct treatment lookups

Documentation


License

Dual-licensed under MIT (source code) and CC BY 4.0 (documentation and non-code content). See LICENSE for details.

Contributors

AlexMikhalev

113 commits

Languages

Rust

66.0%

Python

18.9%

HTML

5.9%

TypeScript

3.9%

Shell

2.9%

JavaScript

1.7%