Source Code:
hub/agents/python/emr/First time here? Complete the Setup guide first to install GAIA and its dependencies.
- Time to complete: 20-25 minutes
- What you’ll build: An automated medical intake form processor
- What you’ll learn: FileWatcherMixin, DatabaseMixin, VLM integration, and agent composition
- Platform: Runs locally on AI PCs with Ryzen AI (NPU/iGPU acceleration)
Why Build This Agent?
Medical staff spend hours manually entering intake form data. This agent automates the process—form arrives, VLM extracts data, database stores it—all running locally on your AI PC. What you’ll learn: FileWatcherMixin, DatabaseMixin, VLM integration, and agent composition patterns.The Architecture (What You’re Building)
Flow:- New form dropped in watched folder
- FileWatcherMixin triggers callback →
_on_file_created() - VLM extracts patient data (Gemma-4-E4B-it GGUF runs on the iGPU via llama.cpp/Vulkan)
- JSON parsed and validated
- DatabaseMixin stores structured record in SQLite
- Agent can now query patients via natural language
Quick Start (5 Minutes)
Get a working intake agent running to understand the basic flow.1
Set up your project
Choose your installation path:
- PyPI (Recommended)
- Developer (Editable Install)
Create a new project folder and install from PyPI:The
agent-emr extra installs the standalone gaia-agent-emr package (which provides gaia_agent_emr and the gaia-emr command). The api extra provides FastAPI/uvicorn for the web dashboard. The rag extra provides PyMuPDF for PDF processing.This is the recommended path for most users. You’ll create your agent scripts in this folder.
2
Start Lemonade Server
The VLM model (Gemma-4-E4B-it-GGUF) will be downloaded automatically on first use. This may take time depending on your connection.
3
Create your first intake agent
Create
intake_agent.py in your project folder:intake_agent.py
4
Run it
- Creates
./intake_forms/directory - Creates
./data/patients.dbSQLite database - Starts watching for new files
- Processes your query using patient data
5
Test with a sample form
Drop an image of an intake form in You’ll see:
./intake_forms/:Core Components
Three components power this agent:Step-by-Step Implementation
Build the agent incrementally to understand each component.Step 1: Basic Agent Shell
Start with the simplest version—no file watching yet, just database setup.- Code
- What You Built
step1_basic.py
Checkpoint: Run it and verify database is created at
./data/patients.db. Use a SQLite browser to inspect the schema.Step 2: Add VLM Extraction
Add VLM to extract patient data from images.- Code
- What You Built
step2_with_vlm.py
Under the Hood: VLM Extraction
Under the Hood: VLM Extraction
Extraction flow:VLM prompt engineering:
- Specify exact JSON structure needed
- Request “ONLY valid JSON” to reduce parsing errors
- Use strict date formats (YYYY-MM-DD)
- Handle null values explicitly
Step 3: Add Automatic File Watching
Make the agent fully automatic—process forms as soon as they arrive.- Code
- What You Built
step3_automatic.py
Try it:
- Run
python step3_automatic.py - In another terminal:
cp sample_form.jpg ./intake_forms/ - Watch the agent automatically process it
- Query: “Show me all patients named Smith”
Testing Your Agent
Use GAIA’s testing utilities to test without real VLM/LLM.- Unit Test
- Run Test
test_intake_agent.py
Key Patterns and Best Practices
Pattern 1: Initialize Attributes Before super().init()
super().__init__() calls _get_system_prompt(), which may reference your attributes.
Pattern 2: Lazy VLM Initialization
Pattern 3: Robust JSON Parsing
extract_json_from_text handles nested objects correctly (unlike simple regex).
Pattern 4: Context Manager Cleanup
What’s Next?
Part 2: Dashboard & API
Build a real-time web dashboard with FastAPI, SSE streaming, and React components
Part 3: Architecture
Deep dive into database schema, processing pipeline, and production considerations
Full Working Example
The completeMedicalIntakeAgent implementation is available in GAIA:
hub/agents/python/emr/gaia_agent_emr/agent.py