# Example: Building a simple public health data agent using LangChain
from langchain.agents import initialize_agent, AgentType, Tool
from langchain.llms import OpenAI
from langchain_experimental.tools import PythonREPLTool
import pandas as pd
# Initialize LLM (requires API key)
llm = OpenAI(temperature=0, model="gpt-4") # Low temp for deterministic behavior
# Define tools the agent can use
python_repl = PythonREPLTool()
tools = [
Tool(
name="Python REPL",
func=python_repl.run,
description="Execute Python code. Use this to analyze data, create visualizations, or perform calculations."
),
Tool(
name="Data Dictionary",
func=lambda x: """
Dataset: COVID-19 case data
Columns:
- date: Report date (YYYY-MM-DD)
- state: US state abbreviation
- cases: Cumulative confirmed cases
- deaths: Cumulative deaths
- population: State population
""",
description="Get information about available datasets and their structure"
)
]
# Initialize agent
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True, # Show reasoning steps
max_iterations=10,
handle_parsing_errors=True
)
# Example task 1: Data analysis
task_1 = """
Analyze the COVID-19 data in covid_data.csv:
1. Calculate the case fatality rate (CFR) by state
2. Identify the 5 states with highest CFR
3. Create a bar chart visualization
4. Provide summary statistics
"""
result_1 = agent.run(task_1)
print(result_1)
# Example task 2: Comparative analysis
task_2 = """
Compare vaccination coverage across US regions:
1. Load vaccination data from vacc_data.csv
2. Group states by region (Northeast, South, Midwest, West)
3. Calculate mean coverage per region
4. Test if regional differences are statistically significant (ANOVA)
5. Summarize findings in plain language
"""
result_2 = agent.run(task_2)
print(result_2)
# Example task 3: Report generation
task_3 = """
Generate a weekly surveillance report:
1. Load recent case data
2. Calculate 7-day moving average of new cases
3. Identify counties with >20% week-over-week increase
4. Create a map showing hotspots
5. Format findings as a markdown report
"""
result_3 = agent.run(task_3)
print(result_3)Emerging AI Architectures for Public Health
Multimodal, agentic, and domain-specific AI architectures relevant to public health practice. The material is maintained separately so each operational question has a stable, focused reference.
- Identify the evidence and controls relevant to this decision area
- Distinguish technical performance from operational and population impact
- Apply the included framework without extending claims beyond the cited evidence
Introduction
This focused reference is part of the broader Emerging AI Architectures overview. It preserves the detailed methods, examples, and exercises while reducing page size and improving direct navigation.
Emerging AI Architectures: Beyond Text-Only LLMs
The Evolution from Chatbots to Agents and Multimodal Systems
2025 reality: LLMs have evolved beyond simple text-in/text-out interfaces. Three major trends are reshaping what’s possible in public health AI:
- AI Agents: Systems that can plan, use tools, and execute multi-step tasks autonomously
- Vision-Language Models (VLMs): Understanding both images and text (e.g., analyzing medical images)
- Small Language Models (SLMs): Efficient, specialized models running on local hardware
These architectures address key limitations of traditional LLMs while introducing new capabilities and challenges.
Retrieval-Augmented Generation (RAG) for Healthcare
RAG represents one of the most important architectural patterns for healthcare AI, directly addressing LLM limitations around hallucination, outdated knowledge, and lack of source attribution.
How RAG Works
The core pattern:
- Query: User asks a question
- Retrieve: System searches a document database (guidelines, EHRs, literature)
- Augment: Retrieved documents are added to the LLM prompt as context
- Generate: LLM answers based specifically on retrieved content
Why this matters for healthcare:
- Citations can be verified against actual source documents
- Knowledge stays current (update the database, not the model)
- Domain-specific information without retraining
- Organizational protocols and guidelines can be integrated
Evidence Base for Healthcare RAG
The first comprehensive review of RAG in healthcare (Ng et al., 2025, NEJM AI) synthesized findings across clinical applications:
Key findings:
| Application | RAG Benefit | Limitation |
|---|---|---|
| Clinical Q&A | Reduces hallucinations 30-50% vs. base LLM | Retrieval quality is bottleneck |
| Literature synthesis | Grounds claims in actual papers | May miss relevant documents |
| Guidelines navigation | Current recommendations with source | Chunk boundaries can split context |
| Patient education | Verified, citable information | Requires careful document curation |
Critical implementation considerations:
- Chunking strategy: How documents are split affects retrieval quality
- Embedding model: Vector representation determines semantic matching
- Retrieval threshold: Balance between precision and recall
- Context window: How much retrieved text fits in the prompt
RAG Architectures for Public Health
Basic RAG pipeline:
User Query → Embedding → Vector Search → Top-K Documents → LLM + Context → Answer
Advanced patterns:
- Hierarchical RAG: First retrieve relevant documents, then relevant passages
- Query expansion: Reformulate query to improve retrieval
- Re-ranking: Use a second model to score retrieval relevance
- Hybrid search: Combine semantic (vector) and keyword (BM25) retrieval
Public health applications:
| Use Case | Document Corpus | Example Query |
|---|---|---|
| Outbreak response | CDC guidelines, historical reports | “What was the containment strategy for 2014 Ebola?” |
| Policy analysis | Regulatory documents, state laws | “Which states require hospital reporting for this condition?” |
| Literature triage | PubMed abstracts, preprints | “Recent evidence on mRNA vaccine cold chain requirements” |
| Protocol assistance | Institutional SOPs | “What is our protocol for TB contact tracing?” |
Limitations and Cautions
RAG reduces but does not eliminate hallucination. The LLM can still:
- Misinterpret retrieved content
- Combine information incorrectly across documents
- Generate plausible-sounding claims not in the source material
- Miss nuances in complex clinical guidelines
The verification imperative remains: RAG outputs should still be validated against source documents, especially for clinical or policy decisions.
For public health departments considering RAG systems:
- Start with curated corpora: Begin with well-structured documents (official guidelines, SOPs) rather than heterogeneous sources
- Validate retrieval quality: Test whether the system retrieves the right documents before trusting generation
- Require citations: Configure systems to explicitly cite retrieved sources
- Plan for maintenance: Document corpora require updates as guidelines change
AI Agents: From Chatbot to Autonomous Assistant
What Are AI Agents?
Definition: An AI agent is a system that can: 1. Plan: Break down complex tasks into steps 2. Act: Execute actions using tools (APIs, code execution, web search) 3. Observe: Monitor results and adjust strategy 4. Iterate: Continue until task completion or failure
Key difference from standard LLMs: - Standard LLM: “Analyze this dataset” → generates explanation text - AI Agent: “Analyze this dataset” → writes code, executes it, debugs errors, generates visualizations, summarizes findings
Foundational paper: Yao et al., 2023, ICLR - ReAct: Reasoning and Acting
Agent Architecture: The ReAct Framework
ReAct = Reasoning + Acting (interleaving thought with action)
Agent workflow:
Task: "Calculate 30-day readmission rate from hospital_data.csv and compare to national benchmark"
THOUGHT 1: I need to load the data and examine its structure
ACTION 1: Execute Python → pd.read_csv("hospital_data.csv").head()
OBSERVATION 1: Dataset has columns: patient_id, admission_date, discharge_date, readmitted_30d
THOUGHT 2: Calculate readmission rate
ACTION 2: Execute Python → readmit_rate = df['readmitted_30d'].mean()
OBSERVATION 2: Readmission rate = 18.2%
THOUGHT 3: Find national benchmark
ACTION 3: Web search → "US national 30-day hospital readmission rate 2024"
OBSERVATION 3: National average is 14.5% (CMS 2024 data)
THOUGHT 4: Generate summary with comparison
ACTION 4: Generate report
OBSERVATION 4: Done
FINAL OUTPUT:
Your hospital's 30-day readmission rate (18.2%) exceeds the national
benchmark (14.5%, CMS 2024) by 3.7 percentage points.
[Detailed analysis follows...]
Agent Tools and Capabilities
Common tools agents can use:
- Code execution: Python, R, SQL
- Web search: Real-time information retrieval
- API calls: Access databases, health systems, external services
- File operations: Read/write data files
- Specialized tools: Statistical analysis, visualization, GIS mapping
Agent Implementation Example
What the agent does automatically: - Reads documentation to understand data structure - Writes and executes Python code - Debugs errors (if code fails, tries alternative approaches) - Generates visualizations - Formats output
Public Health Use Cases for Agents
Use Case 1: Automated Surveillance Reports
Traditional approach: - Epidemiologist manually queries database - Writes SQL/Python scripts - Generates visualizations - Writes narrative summary - Time: 2-4 hours weekly
Agent approach:
Task: Generate weekly COVID surveillance report for [County]
→ Agent autonomously:
1. Queries database
2. Calculates metrics (incidence, trends)
3. Generates visualizations
4. Writes narrative summary
5. Formats report
Time: 5-10 minutes
Human role: Review output, validate findings, add interpretation
Use Case 2: Literature Synthesis with Real-Time Search
Task: “What are the latest recommendations for mpox post-exposure prophylaxis?”
Agent workflow: 1. Web search: Recent CDC guidance, WHO recommendations, peer-reviewed studies (past 12 months) 2. Extract key information from multiple sources 3. Synthesize conflicting recommendations 4. Cite sources with dates 5. Flag uncertainties
Advantage over static LLM: Access to information published after model training cutoff
Use Case 3: Data Quality Auditing
Task: “Check this dataset for quality issues”
Agent actions: 1. Load data, inspect structure 2. Check for missing values, duplicates, outliers 3. Validate data types and ranges 4. Identify logical inconsistencies (e.g., death date before birth date) 5. Generate data quality report with recommendations
Agent Limitations and Risks
1. Hallucination amplification: - Traditional LLM: Hallucinates once - Agent: Hallucination in one step propagates through entire task
2. Tool misuse: - Agent can execute code with unintended consequences (e.g., delete files) - Mitigation: Sandbox execution environments, explicit tool permissions
3. Cost: - Agents make many LLM calls (one per thought/action step) - Can be 10-100x more expensive than single LLM query
4. Unpredictability: - Agent may take unexpected approaches - Difficult to guarantee consistent behavior
5. Security risks: - Prompt injection can manipulate agent behavior - Agents with file/API access pose greater risk than text-only LLMs
Best Practices for Agent Deployment
1. Sandboxing: Run agents in isolated environments
# Example: Limit agent to read-only file access
agent_config = {
"file_access": "read_only",
"allowed_directories": ["/data/public"],
"network_access": False # No external API calls
}2. Human-in-the-loop: Require approval before executing high-risk actions
# Example: Approval workflow
if action_type in ["delete_file", "api_call", "send_email"]:
approval = input(f"Agent wants to {action_type}. Approve? (y/n): ")
if approval != 'y':
return "Action denied by user"3. Logging: Record all agent actions for audit trails
4. Timeout limits: Prevent runaway agents
agent = initialize_agent(
tools=tools,
llm=llm,
max_iterations=10, # Stop after 10 action steps
timeout=300 # Stop after 5 minutes
)5. Output validation: Always verify agent results (see Validation section)
Vision-Language Models (VLMs): Understanding Images and Text
What Are VLMs?
Vision-Language Models integrate visual understanding with text generation, enabling AI to: - Describe images in natural language - Answer questions about image content - Analyze medical images, charts, maps, infographics - Extract structured data from visual sources
Major VLMs (2025): - GPT-4V (Vision): OpenAI’s multimodal model - Claude 3.5 Sonnet (Vision): Anthropic’s multimodal model - Gemini Pro Vision: Google’s multimodal model - LLaVA: Open-source VLM (Large Language and Vision Assistant)
Foundational paper: Liu et al., 2023, NeurIPS - Visual Instruction Tuning
VLM Capabilities in Public Health
Capability 1: Chart and Graph Interpretation
Example task: “Analyze this epidemic curve”
# Example: Using OpenAI GPT-4V to analyze epidemiological charts
import openai
import base64
# Load image (e.g., epidemic curve)
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
epidemic_curve_base64 = encode_image("epidemic_curve.png")
# Send to VLM
response = openai.ChatCompletion.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """You are an epidemiologist analyzing this epidemic curve.
Please provide:
1. Outbreak pattern (point-source, propagated, continuous common source)
2. Key temporal features (peak date, duration, case count trend)
3. Potential explanations for observed patterns
4. Recommendations for public health response
Be specific and reference visible features in the graph."""
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{epidemic_curve_base64}"
}
}
]
}
],
max_tokens=500
)
print(response['choices'][0]['message']['content'])Example output:
This epidemic curve shows a propagated outbreak pattern with the following features:
1. PATTERN: Multiple distinct peaks (Days 5, 12, 19), each approximately
7 days apart, suggesting person-to-person transmission with an incubation
period of ~1 week.
2. TEMPORAL FEATURES:
- Initial peak: Day 5 (n=23 cases)
- Secondary peaks: Days 12 (n=18) and 19 (n=12) show declining amplitude
- Duration: ~25 days from first to last case
- Trend: Decreasing peak heights suggest control measures are effective
3. INTERPRETATION:
- Propagated transmission pattern indicates respiratory or close-contact spread
- Regular 7-day intervals suggest single-generation transmission cycles
- Declining peaks indicate successful intervention (isolation, contact tracing)
4. RECOMMENDATIONS:
- Continue contact tracing for remaining cases
- Monitor for resurgence (maintain surveillance 2-3 incubation periods)
- Document successful interventions for outbreak report
Capability 2: Medical Image Analysis
Use case: Analyzing chest X-rays, skin lesions, microscopy images
Example:
# Example: Preliminary screening of chest X-rays
import anthropic
client = anthropic.Anthropic(api_key="YOUR_API_KEY")
# Load chest X-ray image
with open("chest_xray.jpg", "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode('utf-8')
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_data,
},
},
{
"type": "text",
"text": """Analyze this chest X-ray for public health surveillance purposes.
Identify:
1. Any abnormalities suggestive of tuberculosis (TB)
2. Confidence level in findings
3. Recommended next steps
NOTE: This is for preliminary screening only. All abnormal findings
require radiologist confirmation."""
}
],
}
],
)
print(message.content)Medical Image Analysis: Critical Safety Considerations
VLMs are NOT approved for clinical diagnosis. They can assist with: - Public health surveillance screening (e.g., TB in high-burden settings) - Prioritization for expert review (flagging potentially abnormal images) - Educational purposes and training - Research and method development
VLMs must NOT be used for: - Definitive diagnosis - Treatment decisions - Bypassing radiologist review
Regulatory status: As of 2025, no general-purpose VLM has FDA clearance for diagnostic use. Only use in settings with appropriate oversight and expert review.
Capability 3: Infographic and Document Extraction
Task: Extract structured data from unstructured sources
Example: “Extract vaccination coverage data from this state health department infographic”
# Example: Extracting data from public health infographics
import openai
import json
infographic_base64 = encode_image("vacc_infographic.png")
response = openai.ChatCompletion.create(
model="gpt-4-vision-preview",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": """Extract vaccination coverage data from this infographic.
Return data as JSON with structure:
{
"state": "string",
"date": "YYYY-MM-DD",
"age_groups": [
{
"group": "string (e.g., '65+', '18-64')",
"dose_1_pct": float,
"fully_vaccinated_pct": float,
"booster_pct": float
}
]
}"""
},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{infographic_base64}"}
}
]
}
],
max_tokens=500
)
# Parse extracted data
data = json.loads(response['choices'][0]['message']['content'])
print(json.dumps(data, indent=2))
# Convert to pandas DataFrame for analysis
import pandas as pd
df = pd.DataFrame(data['age_groups'])
print(df)Output:
{
"state": "California",
"date": "2024-10-15",
"age_groups": [
{
"group": "65+",
"dose_1_pct": 94.2,
"fully_vaccinated_pct": 89.7,
"booster_pct": 72.3
},
{
"group": "18-64",
"dose_1_pct": 78.5,
"fully_vaccinated_pct": 71.2,
"booster_pct": 38.9
}
]
}Use case: Rapidly digitizing data from reports, dashboards, or legacy documents
VLM Limitations and Challenges
1. Hallucination in visual interpretation: - May “see” features that aren’t present - Can confuse similar visual patterns - Mitigation: Always verify critical findings with human experts
2. Resolution and quality dependence: - Poor image quality → unreliable analysis - Small text or fine details may be missed
3. Privacy risks: - Images may contain incidental PHI (patient wristbands, visible names) - Mitigation: De-identify images before VLM analysis (see Privacy section)
4. Lack of medical training: - General VLMs lack specialized medical knowledge - May miss subtle diagnostic features - Solution: Use domain-specific models where available (e.g., CheXNet for chest X-rays)
Small Language Models (SLMs): Efficient, Local, and Specialized
What Are SLMs?
Small Language Models are compact models (1B-7B parameters) that: - Run on local hardware (laptops, edge devices, mobile phones) - Require no internet connection - Preserve data privacy (no external API calls) - Are often specialized for specific tasks
Size comparison: - Large LLMs: GPT-4 (~1.7 trillion parameters), Claude 3.5 (~hundreds of billions) - Small LLMs: Phi-3 (3.8B), Gemma 2 (2B-9B), Llama 3.2 (1B-3B)
Key insight: For many tasks, smaller specialized models outperform larger general-purpose models while being 100-1000x more efficient.
Foundational work: Touvron et al., 2023, Meta AI - Llama 2
Why SLMs Matter for Public Health
Advantage 1: Privacy by design - Data never leaves local device - No reliance on external APIs (no terms of service concerns) - Ideal for sensitive health data in resource-limited settings
Advantage 2: Cost - No per-token API fees - One-time compute cost (fine-tuning/deployment) - Sustainable for low-budget health departments
Advantage 3: Speed and latency - Real-time inference (milliseconds vs. seconds) - No network dependency
Advantage 4: Customization - Can fine-tune on domain-specific data - Specialization improves performance on narrow tasks
SLM Use Cases in Public Health
Use Case 1: Clinical Note De-identification
Task: Remove PHI from clinical notes before analysis
Traditional approach: Complex rule-based systems or expensive cloud APIs
SLM approach: Fine-tuned local model
# Example: Using a small model for PHI detection and removal
from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline
# Load fine-tuned model for PHI detection (e.g., based on Llama 3.2 1B)
model_name = "path/to/phi-detection-model" # Fine-tuned on i2b2 PHI dataset
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForTokenClassification.from_pretrained(model_name)
# Create NER pipeline
phi_detector = pipeline(
"ner",
model=model,
tokenizer=tokenizer,
aggregation_strategy="simple"
)
# Example clinical note
clinical_note = """
Patient: John Smith (DOB: 05/15/1967, MRN: 123456)
Admitted to Memorial Hospital on 10/15/2024.
Chief complaint: Chest pain radiating to left arm.
Contact: 555-123-4567
"""
# Detect PHI entities
phi_entities = phi_detector(clinical_note)
# Replace PHI with generic placeholders
def deidentify(text, entities):
offset = 0
deidentified = text
for entity in entities:
start = entity['start'] + offset
end = entity['end'] + offset
placeholder = f"[{entity['entity_group']}]"
deidentified = deidentified[:start] + placeholder + deidentified[end:]
offset += len(placeholder) - (end - start)
return deidentified
deidentified_note = deidentify(clinical_note, phi_entities)
print("=== Original ===")
print(clinical_note)
print("\n=== De-identified ===")
print(deidentified_note)Output:
=== De-identified ===
Patient: [NAME] (DOB: [DATE], MRN: [ID])
Admitted to [LOCATION] on [DATE].
Chief complaint: Chest pain radiating to left arm.
Contact: [PHONE]
Advantage: Runs locally, no PHI sent to external APIs, HIPAA-compliant
Use Case 2: Multilingual Health Communication
Challenge: Translating public health messages for diverse populations
SLM solution: Specialized translation models running on-device
# Example: Local translation model for health messaging
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Load small multilingual model (e.g., NLLB-200 distilled, ~600M params)
model_name = "facebook/nllb-200-distilled-600M"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
def translate_health_message(text, source_lang="eng_Latn", target_lang="spa_Latn"):
"""
Translate public health messages
Language codes: eng_Latn (English), spa_Latn (Spanish), fra_Latn (French),
zho_Hans (Chinese Simplified), ara_Arab (Arabic), etc.
"""
tokenizer.src_lang = source_lang
inputs = tokenizer(text, return_tensors="pt")
# Generate translation
translated_tokens = model.generate(
**inputs,
forced_bos_token_id=tokenizer.lang_code_to_id[target_lang],
max_length=512
)
translation = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
return translation
# Example: Mpox outbreak alert
alert_en = """
MPOX ALERT: Cases have been reported in our community.
Symptoms include fever, rash, and swollen lymph nodes.
If you have symptoms, isolate and contact your healthcare provider.
Vaccination is available for high-risk groups.
"""
# Translate to multiple languages
languages = {
"Spanish": "spa_Latn",
"French": "fra_Latn",
"Chinese": "zho_Hans",
"Arabic": "ara_Arab"
}
print("=== Original (English) ===")
print(alert_en)
for lang_name, lang_code in languages.items():
translation = translate_health_message(alert_en, target_lang=lang_code)
print(f"\n=== {lang_name} ===")
print(translation)Advantage: - No internet required (works in remote field settings) - Supports 200+ languages - Free (no API costs) - Culturally appropriate (can fine-tune on local health terminology)
Use Case 3: Mobile Health (mHealth) Applications
Scenario: Community health worker app providing on-device clinical decision support
# Example: On-device symptom checker using SLM
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load small instruction-tuned model (e.g., Phi-3 Mini, 3.8B params)
model_name = "microsoft/Phi-3-mini-4k-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16, # Use half precision for speed
device_map="auto"
)
def clinical_decision_support(symptoms, patient_age, context="rural clinic"):
"""
Provide clinical guidance based on symptoms
Runs entirely on-device (no internet needed)
"""
prompt = f"""<|system|>
You are a clinical decision support system for community health workers in {context}.
Provide differential diagnosis and triage recommendations based on symptoms.
Do not provide definitive diagnosis - guide appropriate referral.
<|end|>
<|user|>
Patient: {patient_age} years old
Symptoms: {symptoms}
Provide:
1. Possible conditions (differential diagnosis)
2. Red flags requiring immediate referral
3. Recommended actions
<|end|>
<|assistant|>"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=300,
temperature=0.3, # Lower temperature for medical advice
do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the assistant's response
response = response.split("<|assistant|>")[-1].strip()
return response
# Example use
symptoms = "Fever for 3 days, severe headache, neck stiffness, photophobia"
patient_age = 7
guidance = clinical_decision_support(symptoms, patient_age)
print(guidance)Example output:
POSSIBLE CONDITIONS:
1. Bacterial meningitis (HIGH PRIORITY)
2. Viral meningitis
3. Severe influenza
RED FLAGS PRESENT:
[URGENT] IMMEDIATE REFERRAL REQUIRED
- Neck stiffness (meningeal signs)
- Severe headache + fever in child
- Photophobia
RECOMMENDED ACTIONS:
1. URGENT: Refer to hospital immediately (suspected meningitis)
2. Monitor airway and vital signs during transport
3. Do NOT delay transport for further evaluation
4. Alert receiving facility: possible bacterial meningitis
TIME-SENSITIVE: This is a medical emergency requiring immediate hospital care.
Advantage: Works offline in remote clinics, no connectivity required
Fine-Tuning SLMs for Public Health
Fine-tuning addresses the gap between general-purpose models and domain-specific needs. General-purpose models lack public health terminology and context; training on domain-specific data improves accuracy and allows specialization for tasks like symptom classification or outbreak detection.
Fine-tuning example:
# Example: Fine-tuning Llama 3.2 1B for COVID symptom classification
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments
import pandas as pd
from datasets import Dataset
# Load base model
model_name = "meta-llama/Llama-3.2-1B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=2 # Binary: COVID vs. non-COVID
)
# Prepare training data
# Format: symptom descriptions + labels
train_data = pd.DataFrame({
'text': [
"loss of taste, fever, dry cough",
"runny nose, sneezing, sore throat",
"shortness of breath, fever, fatigue",
"itchy eyes, clear nasal discharge",
# ... more examples
],
'label': [1, 0, 1, 0] # 1=COVID-like, 0=other
})
# Convert to HuggingFace dataset
dataset = Dataset.from_pandas(train_data)
# Tokenize
def tokenize_function(examples):
return tokenizer(examples['text'], padding="max_length", truncation=True)
tokenized_dataset = dataset.map(tokenize_function, batched=True)
# Training configuration
training_args = TrainingArguments(
output_dir="./symptom-classifier",
evaluation_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=16,
num_train_epochs=3,
weight_decay=0.01,
)
# Train
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_dataset,
)
trainer.train()
# Save fine-tuned model
model.save_pretrained("./covid-symptom-classifier")
tokenizer.save_pretrained("./covid-symptom-classifier")
# Inference with fine-tuned model
def classify_symptoms(symptom_text):
inputs = tokenizer(symptom_text, return_tensors="pt", padding=True, truncation=True)
outputs = model(**inputs)
prediction = torch.argmax(outputs.logits, dim=1).item()
confidence = torch.softmax(outputs.logits, dim=1).max().item()
return {
"prediction": "COVID-like" if prediction == 1 else "Other illness",
"confidence": f"{confidence:.2%}"
}
# Test
result = classify_symptoms("sudden loss of smell, fever, body aches")
print(result) # {"prediction": "COVID-like", "confidence": "94%"}SLM Limitations
1. Reduced capabilities: - Cannot match large models on complex reasoning tasks - Limited context window (typically 2K-8K tokens vs. 128K+ for large models) - May struggle with highly technical or nuanced tasks
2. Specialization trade-off: - Fine-tuning improves performance on target task but reduces general capabilities - Need different models for different tasks
3. Hardware requirements: - Still requires decent hardware (modern laptop with GPU recommended) - Very small models (<1B params) may not be useful for complex tasks
Choosing Between Large LLMs, Agents, VLMs, and SLMs
| Task | Recommended Approach | Rationale |
|---|---|---|
| Complex reasoning, multi-step analysis | Large LLM (GPT-4, Claude 3.5) | Superior reasoning and instruction-following |
| Autonomous data analysis | AI Agent | Can plan, code, debug, iterate |
| Image/chart interpretation | VLM (GPT-4V, Claude 3.5 Sonnet) | Multimodal understanding |
| Privacy-sensitive local tasks | SLM (Phi-3, Llama 3.2) | No external API calls |
| High-volume, specialized tasks | Fine-tuned SLM | Cost-effective, fast |
| Real-time mobile applications | SLM | Low latency, offline capability |
| Literature review, report generation | Large LLM | Broad knowledge, coherent long-form text |
Integration Example: Combining All Three
Scenario: Outbreak investigation system
# Integrated system combining Agent, VLM, and SLM
class OutbreakInvestigationSystem:
def __init__(self):
# Large LLM for complex reasoning (Agent)
self.agent = initialize_outbreak_agent()
# VLM for image analysis
self.vlm = load_vlm("gpt-4-vision")
# SLM for local PHI removal
self.phi_remover = load_slm("phi-detection-model")
def investigate_outbreak(self, case_data_path, epi_curve_image_path):
"""
Multi-step outbreak investigation:
1. De-identify case data (SLM - local, private)
2. Analyze epidemic curve (VLM)
3. Statistical analysis and reporting (Agent)
"""
# Step 1: De-identify case data locally (SLM)
print("Step 1: De-identifying case data...")
case_data = pd.read_csv(case_data_path)
deidentified_data = self.phi_remover.deidentify(case_data)
# Step 2: Analyze epidemic curve (VLM)
print("Step 2: Analyzing epidemic curve...")
curve_analysis = self.vlm.analyze_image(
epi_curve_image_path,
prompt="Analyze this epidemic curve: pattern, peak dates, duration"
)
# Step 3: Agent performs thorough analysis (Agent)
print("Step 3: Running statistical analysis...")
agent_task = f"""
Analyze this outbreak:
Data: {deidentified_data.to_json()}
Epidemic curve analysis: {curve_analysis}
Tasks:
1. Calculate attack rates by age group and location
2. Create case distribution map
3. Test for common source vs. propagated outbreak (statistical test)
4. Generate hypotheses for exposure source
5. Recommend next investigation steps
"""
report = self.agent.run(agent_task)
return {
"data_summary": deidentified_data.describe(),
"curve_interpretation": curve_analysis,
"full_report": report
}
# Usage
system = OutbreakInvestigationSystem()
results = system.investigate_outbreak(
case_data_path="outbreak_cases.csv",
epi_curve_image_path="epidemic_curve.png"
)
print(results['full_report'])Key advantages: - Privacy preserved: PHI removed locally before cloud analysis - Visual insights: Automatic chart interpretation - Autonomous analysis: Agent handles complex multi-step tasks - Time saved: 4-6 hour task → 15 minutes
Ethical Considerations and Best Practices
1. Transparency: - Disclose when agents, VLMs, or SLMs are used in decision-making - Document model versions, prompts, and validation steps
2. Human oversight: - Never fully automate consequential decisions - Require expert review of agent outputs - VLM medical image interpretations must be confirmed by qualified professionals
3. Privacy by design: - Use SLMs for sensitive local tasks - Agents with file/API access require strict sandboxing - VLMs: De-identify images before analysis
4. Validation: - Test agent behavior extensively before deployment - VLM outputs require same validation as standard LLMs (hallucination checking) - Fine-tuned SLMs must be validated on held-out test sets
5. Equity: - SLMs enable AI access in resource-limited settings (no internet/API costs) - Multilingual SLMs support diverse populations - Monitor for bias in fine-tuned specialized models
Key Takeaways: Emerging AI Architectures
AI Agents: - Automate multi-step tasks (data analysis, report generation, literature search) - Require sandboxing, human oversight, and careful validation - Best for: Autonomous surveillance reports, data quality audits, research workflows
Vision-Language Models: - Interpret charts, images, infographics, medical images - Not FDA-approved for diagnosis; require expert confirmation - Best for: Chart analysis, document extraction, preliminary screening
Small Language Models: - Privacy-preserving, cost-effective, offline capability - Can be fine-tuned for specialized public health tasks - Best for: PHI removal, mHealth apps, multilingual communication, resource-limited settings
The future is multimodal, agentic, and increasingly efficient. Public health practitioners must understand these architectures to deploy AI responsibly and effectively.