Source Code:
hub/agents/python/emr/This is Part 3 of 3. If you haven’t completed the previous parts:
- Time to complete: 15-20 minutes
- What you’ll learn: Database design, processing pipeline, system architecture
- Platform: Designed for AI PCs with Ryzen AI (NPU/iGPU acceleration)
System Architecture
Component Overview
Database Schema
The agent creates three tables to manage patient data and processing state.patients
The main table storing extracted patient information.alerts
Critical notifications requiring staff attention.
Alert Types:
intake_sessions
Tracks each processing session for audit and analytics.Processing Pipeline
The agent follows a 7-step pipeline from file detection to database storage.Pipeline Steps
1
File Detection
Component: FileWatcherMixin with watchdogThe file watcher monitors the directory for new files:Key behaviors:
- Filters by extension (only images/PDFs)
- Debounces events to avoid processing incomplete files
- Runs in background thread
2
Image Conversion
For PDFs: Convert to PNG using PyMuPDF at 2x resolutionWhy 2x resolution? Higher resolution improves VLM extraction accuracy, especially for handwritten text.
3
VLM Extraction
Model: Gemma-4-E4B-it-GGUF running on the iGPU via llama.cpp/VulkanWhy Gemma-4-E4B-it? A compact multimodal model that runs efficiently on AMD hardware and is the default VLM for GAIA’s document-understanding agents.
4
JSON Parsing
Robust brace-counting parser handles nested objects and VLM quirksWhy custom parsing? VLMs sometimes add explanatory text before/after JSON.
5
Duplicate Check
Match on name + DOB to identify returning patients
6
Change Detection
Compare 10 fields for returning patients
7
Database Storage
INSERT for new, UPDATE for returning
8
Alert Creation
Critical alerts for allergies and missing fields
9
Console Output
Rich formatted success/warning messages
File Deduplication
The agent uses SHA-256 hashing to prevent processing the same file twice.Time Savings Calculation
The agent calculates per-form time savings based on actual extracted data, not fixed estimates.Per-Field Estimation Formula
Example Calculation
A form with 15 fields and ~300 characters:- Base time: 15 × 10 sec = 150 sec
- Typing: 300 × 0.3 sec = 90 sec
- Subtotal: ~240 sec (4 min)
- With verification (+15%): ~276 sec (4.6 min)
Expected Results
Based on typical 15-field forms. Actual savings depend on form complexity.
Agent Composition
The Medical Intake Agent combines three GAIA mixins.Mixin Responsibilities
Production Considerations
Security
- Local processing only - No PHI leaves the machine
- SQLite file permissions - Set appropriate OS-level access controls
- Watch directory isolation - Don’t share with untrusted users
Performance
- VLM on iGPU - llama.cpp/Vulkan accelerates inference
- Lazy VLM loading - Model loaded only when first file arrives
- Connection pooling - DatabaseMixin reuses SQLite connections
Reliability
- File debouncing - 2-second wait avoids partial file reads
- JSON parsing fallbacks - Handles VLM output variations
- Graceful cleanup - Context manager ensures proper shutdown
Extending the Agent
Add Custom Extraction Fields
Add Custom Alert Types
Summary
The Medical Intake Agent demonstrates several GAIA patterns:- Mixin composition - Combine FileWatcherMixin + DatabaseMixin + Agent
- Event-driven processing - React to file system events
- VLM integration - Extract structured data from images
- Real-time updates - SSE streaming to web dashboard
- Audit trail - Track all processing in intake_sessions table