-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedical_assistant.py
278 lines (260 loc) · 8.44 KB
/
medical_assistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import asyncio
import os
from typing import Dict, Any
from dotenv import load_dotenv
import anthropic
from ..agent import DomainAgentBuilder, DomainMemory, DomainKnowledge
async def llm_call(prompt: str) -> str:
"""Call Anthropic's Claude API"""
client = anthropic.Anthropic()
response = await client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
temperature=0.7,
system="You are a medical diagnosis assistant.",
messages=[{"role": "user", "content": prompt}]
)
return response.content[0].text
# Domain-specific tool handlers
async def handle_symptom_analysis(params: Dict[str, Any]) -> Dict[str, Any]:
"""Analyze patient symptoms"""
symptoms = params["symptoms"]
return {
"severity": "moderate",
"urgency": "non-emergency",
"possible_conditions": [
"condition1",
"condition2",
"condition3"
],
"risk_factors": [
"risk1",
"risk2"
]
}
async def handle_medical_history_check(params: Dict[str, Any]) -> Dict[str, Any]:
"""Check patient medical history"""
patient_id = params["patient_id"]
return {
"previous_conditions": [
"condition1",
"condition2"
],
"allergies": [
"allergy1",
"allergy2"
],
"medications": [
"medication1",
"medication2"
]
}
async def handle_drug_interaction_check(params: Dict[str, Any]) -> Dict[str, Any]:
"""Check for potential drug interactions"""
medications = params["medications"]
return {
"interactions": [
{
"drugs": ["drug1", "drug2"],
"severity": "high",
"recommendation": "avoid combination"
}
],
"alternatives": [
"alt_drug1",
"alt_drug2"
]
}
async def handle_treatment_recommendation(params: Dict[str, Any]) -> Dict[str, Any]:
"""Generate treatment recommendations"""
condition = params["condition"]
patient_data = params["patient_data"]
return {
"primary_treatment": "treatment1",
"alternatives": [
"treatment2",
"treatment3"
],
"lifestyle_changes": [
"change1",
"change2"
],
"follow_up": "2 weeks"
}
# Domain-specific constraints
def validate_patient_data(params: Dict[str, Any]) -> bool:
"""Validate patient data completeness"""
required_fields = ["age", "gender", "symptoms"]
return all(field in params for field in required_fields)
def validate_medication_safety(params: Dict[str, Any]) -> bool:
"""Validate medication safety"""
if "medications" not in params:
return True
# Mock safety check
unsafe_medications = ["unsafe_drug1", "unsafe_drug2"]
return not any(med in unsafe_medications for med in params["medications"])
# Medical domain knowledge
medical_knowledge = {
"facts": {
"common_conditions": [
"hypertension",
"diabetes",
"asthma"
],
"vital_signs": {
"normal_bp": "120/80",
"normal_temp": "98.6F",
"normal_hr": "60-100"
}
},
"rules": {
"emergency_symptoms": "Chest pain, difficulty breathing, or severe bleeding require immediate emergency care",
"prescription_requirements": "Controlled substances require proper documentation and authorization",
"follow_up_timing": "Critical conditions require follow-up within 24-48 hours"
},
"terminology": {
"bp": "Blood Pressure",
"hr": "Heart Rate",
"bmi": "Body Mass Index"
},
"relationships": {
"diabetes": ["blood_sugar", "insulin", "diet"],
"hypertension": ["blood_pressure", "sodium", "stress"]
}
}
# Domain-specific behaviors
diagnosis_behavior = {
"name": "comprehensive_diagnosis",
"description": "Perform comprehensive diagnosis when multiple symptoms present",
"trigger_conditions": {
"symptom_count": ">3",
"severity": "high"
},
"action_template": {
"name": "detailed_analysis",
"parameters": {
"include_specialists": True,
"run_additional_tests": True
}
}
}
async def main():
# Load environment variables
load_dotenv()
# Initialize domain memory
memory = DomainMemory(
context={
"facility_type": "primary_care",
"available_specialists": ["cardiology", "neurology", "endocrinology"],
"emergency_protocols": ["protocol1", "protocol2"]
}
)
# Build the medical assistant
assistant = (
DomainAgentBuilder("medical_diagnosis")
.add_tool(
name="symptom_analysis",
description="Analyze patient symptoms and determine possible conditions",
parameters={
"symptoms": "list of symptoms"
},
handler=handle_symptom_analysis
)
.add_tool(
name="medical_history",
description="Check patient medical history",
parameters={
"patient_id": "patient identifier"
},
handler=handle_medical_history_check
)
.add_tool(
name="drug_interaction",
description="Check for potential drug interactions",
parameters={
"medications": "list of medications"
},
handler=handle_drug_interaction_check
)
.add_tool(
name="treatment_recommendation",
description="Generate treatment recommendations",
parameters={
"condition": "diagnosed condition",
"patient_data": "patient information"
},
handler=handle_treatment_recommendation
)
.add_constraint(
name="patient_data_validation",
description="Ensure all required patient data is provided",
validation_fn=validate_patient_data,
error_message="Missing required patient information"
)
.add_constraint(
name="medication_safety",
description="Ensure medication safety",
validation_fn=validate_medication_safety,
error_message="Unsafe medication detected"
)
.add_behavior(
name=diagnosis_behavior["name"],
description=diagnosis_behavior["description"],
trigger_conditions=diagnosis_behavior["trigger_conditions"],
action_template=diagnosis_behavior["action_template"]
)
.with_knowledge(**medical_knowledge)
.with_memory(memory)
.build(llm_caller=llm_call)
)
# Example cases to diagnose
cases = [
{
"goal": "Diagnose patient with symptoms: fever, cough, fatigue",
"context": {
"patient_data": {
"age": 45,
"gender": "F",
"symptoms": ["fever", "cough", "fatigue"],
"vitals": {
"temperature": "101.2F",
"blood_pressure": "128/82",
"heart_rate": 88
}
}
}
},
{
"goal": "Recommend treatment for diagnosed hypertension",
"context": {
"patient_data": {
"age": 62,
"gender": "M",
"condition": "hypertension",
"medications": ["lisinopril"],
"blood_pressure": "158/94"
}
}
}
]
# Process cases
for case in cases:
print(f"\n{'-' * 50}")
print(f"Processing Case: {case['goal']}")
print(f"{'-' * 50}")
try:
# Execute diagnosis/treatment
actions = await assistant.execute(
goal=case["goal"],
context=case["context"]
)
# Print results
print("\nCompleted Actions:")
for action in actions:
print(f"\n{action.description}:")
print(f"Tool: {action.name}")
print(f"Result: {action.result}")
except Exception as e:
print(f"Error processing case: {str(e)}")
if __name__ == "__main__":
asyncio.run(main())