Overview
The Medical Insights Agent is the primary text analysis component of MedMitra’s AI system. It processes structured and unstructured medical data to generate comprehensive clinical assessments, including case summaries, SOAP notes, and diagnostic insights.Architecture
The agent is implemented inbackend/agents/medical_ai_agent.py and uses a LangGraph workflow to orchestrate multiple analysis steps.
Class Structure
backend/agents/medical_ai_agent.py
Key Components
- LLM Manager: Handles interactions with the Groq API using Llama 3.3 70B
- Supabase Client: Manages database operations for saving insights
- Workflow: LangGraph state machine that defines the analysis pipeline
Workflow Graph
The agent constructs a directed graph with the following nodes:backend/agents/medical_ai_agent.py
Workflow Visualization
Processing Nodes
1. Process Lab Documents
Extracts structured data from laboratory reports:backend/agents/medical_ai_agent.py:71-92
LabDocument objects with extracted values and summaries.
2. Process Radiology Documents
Integrates radiology findings from the Vision Agent:backend/agents/medical_ai_agent.py:94-119
RadiologyDocument objects with AI-generated summaries.
3. Generate Case Summary
Synthesizes all available information into a comprehensive case overview:backend/agents/medical_ai_agent.py:121-162
CaseSummary with comprehensive overview and key findings.
4. Generate SOAP Note
Creates a structured clinical note following the SOAP format:backend/agents/medical_ai_agent.py:164-184
- Subjective: Patient’s reported symptoms and history
- Objective: Observable findings from tests and examinations
- Assessment: Clinical analysis and interpretation
- Plan: Recommended next steps and treatments
5. Generate Diagnosis
Produces a primary diagnosis with ICD coding and supporting evidence:backend/agents/medical_ai_agent.py:186-207
Diagnosis with ICD-10 code and clinical reasoning.
6. Compile Insights
Aggregates all analysis results into a unified output:backend/agents/medical_ai_agent.py:260-286
7. Save Results
Persists the final insights to Supabase:backend/agents/medical_ai_agent.py:288-310
Usage Example
Processing a case through the Medical Insights Agent:Model Configuration
Default Settings
Why Llama 3.3 70B?
- Large context window: Handles lengthy medical documents
- Medical knowledge: Strong understanding of clinical terminology
- Structured output: Reliable JSON generation for data extraction
- Cost-effective: Via Groq’s optimized inference
Confidence Scoring
Each analysis step generates a confidence score that reflects:- Data completeness: How much relevant information was available
- Model certainty: LLM’s confidence in its analysis
- Consistency: Agreement between different data sources
Error Handling
The agent tracks errors in the state and continues processing when possible:backend/agentic.py
Future Enhancements
The codebase includes commented sections for planned features:- Differential Diagnoses: Multiple possible diagnoses with probabilities
- Investigation Recommendations: Suggested tests and imaging studies
- Treatment Recommendations: Evidence-based treatment plans
backend/agents/medical_ai_agent.py:41-42
Next Steps
Vision Agent
Learn about medical image analysis
Complete Workflow
See the full processing pipeline
