1+ from dataclasses import dataclass
2+ from typing import Any
3+ import asyncio
4+
5+ from pydantic import BaseModel , Field
6+ from pydantic_ai import Agent , RunContext
7+
8+ from dotenv import load_dotenv
9+
10+
11+ # Load environment variables from .env
12+ load_dotenv ()
13+
14+
15+ # Mock database
16+ @dataclass
17+ class Patient :
18+ id : int
19+ name : str
20+ vitals : dict [str , Any ]
21+
22+ PATIENT_DB = {
23+ 42 : Patient (id = 42 , name = "John Doe" , vitals = {"heart_rate" : 72 , "blood_pressure" : "120/80" }),
24+ 43 : Patient (id = 43 , name = "Jane Smith" , vitals = {"heart_rate" : 65 , "blood_pressure" : "110/70" }),
25+ }
26+
27+ class DatabaseConn :
28+ async def patient_name (self , id : int ) -> str :
29+ patient = PATIENT_DB .get (id )
30+ return patient .name if patient else "Unknown Patient"
31+
32+ async def latest_vitals (self , id : int ) -> dict [str , Any ]:
33+ patient = PATIENT_DB .get (id )
34+ return patient .vitals if patient else {"heart_rate" : 0 , "blood_pressure" : "N/A" }
35+
36+
37+ @dataclass
38+ class TriageDependencies :
39+ patient_id : int
40+ db : DatabaseConn
41+
42+
43+ class TriageOutput (BaseModel ):
44+ response_text : str = Field (description = "Message to the patient" )
45+ escalate : bool = Field (description = "Should escalate to a human nurse" )
46+ urgency : int = Field (description = "Urgency level from 0 to 10" , ge = 0 , le = 10 )
47+
48+
49+ triage_agent = Agent (
50+ "openai:gpt-4o" ,
51+ deps_type = TriageDependencies ,
52+ output_type = TriageOutput ,
53+ system_prompt = (
54+ "You are a triage assistant helping patients. "
55+ "Provide clear advice and assess urgency."
56+ ),
57+ )
58+
59+
60+ @triage_agent .system_prompt
61+ async def add_patient_name (ctx : RunContext [TriageDependencies ]) -> str :
62+ patient_name = await ctx .deps .db .patient_name (id = ctx .deps .patient_id )
63+ return f"The patient's name is { patient_name !r} ."
64+
65+
66+ @triage_agent .tool
67+ async def latest_vitals (ctx : RunContext [TriageDependencies ]) -> dict [str , Any ]:
68+ """Returns the patient's latest vital signs."""
69+ return await ctx .deps .db .latest_vitals (id = ctx .deps .patient_id )
70+
71+
72+ async def main () -> None :
73+ deps = TriageDependencies (patient_id = 43 , db = DatabaseConn ())
74+
75+ result = await triage_agent .run (
76+ "I have chest pain and trouble breathing." ,
77+ deps = deps ,
78+ )
79+ print (result .output )
80+ """
81+ Example output:
82+ response_text='Your symptoms are serious. Please call emergency services immediately. A nurse will contact you shortly.'
83+ escalate=True
84+ urgency=10
85+ """
86+
87+ result = await triage_agent .run (
88+ "I have a mild headache since yesterday." ,
89+ deps = deps ,
90+ )
91+ print (result .output )
92+ """
93+ Example output:
94+ response_text='It sounds like your headache is not severe, but monitor it closely. If it worsens or you develop new symptoms, contact your doctor.'
95+ escalate=False
96+ urgency=3
97+ """
98+
99+
100+ if __name__ == "__main__" :
101+ asyncio .run (main ())
0 commit comments