Skip to content

Commit 58b4e55

Browse files
committed
Merge branch '2025-pydanticai'
2 parents 106f957 + e415018 commit 58b4e55

3 files changed

Lines changed: 1264 additions & 0 deletions

File tree

2025/pydanticai/main.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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())

2025/pydanticai/pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "pydanticai"
3+
version = "0.1.0"
4+
description = "Pydantic AI tutorial"
5+
requires-python = ">=3.13"
6+
dependencies = [
7+
"pydantic>=2.11.7",
8+
"pydantic-ai>=0.4.2",
9+
"python-dotenv>=1.1.1",
10+
]

0 commit comments

Comments
 (0)