forked from strands-agents/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedical_coding_tools.py
More file actions
658 lines (571 loc) · 23.2 KB
/
medical_coding_tools.py
File metadata and controls
658 lines (571 loc) · 23.2 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python3
import os
import json
import requests
from typing import List, Dict, Any, Optional
import boto3
from strands import tool
# Base URLs for medical terminology APIs
ICD10_API_BASE_URL = "https://clinicaltables.nlm.nih.gov/api/icd10cm/v3/search"
RXNORM_API_BASE_URL = "https://rxnav.nlm.nih.gov/REST/rxcui"
RXNORM_INFO_API_BASE_URL = "https://rxnav.nlm.nih.gov/REST/rxcui/{rxcui}/allrelated"
SNOMED_API_BASE_URL = "https://browser.ihtsdotools.org/snowstorm/snomed-ct/MAIN/concepts"
SNOMED_BROWSER_URL = "https://browser.ihtsdotools.org/?perspective=full&edition=MAIN/SNOMEDCT-US/2025-03-01&languages=en"
@tool
def get_icd(diagnosis: str) -> str:
"""
Get ICD-10 codes for a given diagnosis using the NLM Clinical Tables API.
Args:
diagnosis: The medical diagnosis to look up
Returns:
JSON string containing matching ICD-10 codes and descriptions
"""
try:
# Use the NLM Clinical Tables API (no authentication required)
return _get_icd_from_api(diagnosis)
except Exception as e:
# Fallback to Bedrock for code lookup if API fails
try:
return _get_medical_code_from_bedrock(
diagnosis,
"ICD-10",
"Find the most appropriate ICD-10 codes for this diagnosis"
)
except Exception as inner_e:
return json.dumps({
"error": f"Error retrieving ICD-10 codes: {str(e)}. Fallback error: {str(inner_e)}",
"diagnosis": diagnosis
})
@tool
def get_rx(medication: str) -> str:
"""
Get RxNorm codes for a given medication using the NLM RxNav API.
Args:
medication: The medication name to look up
Returns:
JSON string containing matching RxNorm codes and information
"""
try:
# Use the NLM RxNav API (no authentication required)
return _get_rx_from_api(medication)
except Exception as e:
# Fallback to Bedrock for code lookup if API fails
try:
return _get_medical_code_from_bedrock(
medication,
"RxNorm",
"Find the most appropriate RxNorm codes for this medication"
)
except Exception as inner_e:
return json.dumps({
"error": f"Error retrieving RxNorm codes: {str(e)}. Fallback error: {str(inner_e)}",
"medication": medication
})
@tool
def get_snomed(treatment: str) -> str:
"""
Get SNOMED CT codes for a given treatment or procedure using the SNOMED CT browser API.
Args:
treatment: The medical treatment or procedure to look up
Returns:
JSON string containing matching SNOMED CT codes and descriptions
"""
try:
# Use the SNOMED CT browser API
return _get_snomed_from_api(treatment)
except Exception as e:
# Fallback to Bedrock for code lookup if API fails
try:
return _get_medical_code_from_bedrock(
treatment,
"SNOMED CT",
"Find the most appropriate SNOMED CT codes for this treatment or procedure"
)
except Exception as inner_e:
return json.dumps({
"error": f"Error retrieving SNOMED CT codes: {str(e)}. Fallback error: {str(inner_e)}",
"treatment": treatment
})
@tool
def link_icd(clinical_text: str) -> str:
"""
Extract diagnoses from clinical text and link them to ICD-10 codes.
Args:
clinical_text: The clinical text to analyze
Returns:
JSON string containing extracted diagnoses with their ICD-10 codes
"""
try:
# Use Bedrock to extract diagnoses and link to ICD-10 codes
prompt = f"""
Extract all diagnoses from the following clinical text and link them to the most appropriate ICD-10 codes.
For each diagnosis, provide:
1. The diagnosis as mentioned in the text
2. The ICD-10 code
3. The official ICD-10 description
4. A confidence score (95% for high confidence, lower for less certain matches)
Clinical text:
{clinical_text}
Format the output as a JSON array of diagnosis objects with the exact format:
[
{{
"diagnosis": "Atrial fibrillation",
"ICD10_code": "I48.0",
"description": "Paroxysmal atrial fibrillation",
"confidence_score": "95%"
}},
...
]
"""
return _get_structured_data_from_bedrock(prompt, "diagnoses")
except Exception as e:
return json.dumps([{
"diagnosis": "Error",
"ICD10_code": "Error",
"error": f"Error linking diagnoses to ICD-10 codes: {str(e)}",
"confidence_score": "0%"
}])
@tool
def link_rx(clinical_text: str) -> str:
"""
Extract medications from clinical text and link them to RxNorm codes.
Args:
clinical_text: The clinical text to analyze
Returns:
JSON string containing extracted medications with their RxNorm codes
"""
try:
# Use Bedrock to extract medications and link to RxNorm codes
prompt = f"""
Extract all medications from the following clinical text and link them to the most appropriate RxNorm codes.
For each medication, provide:
1. The medication name as mentioned in the text
2. The RxNorm code
3. The standard RxNorm name/description
4. The dosage if specified
5. The frequency if specified
6. A confidence score (95% for high confidence, lower for less certain matches)
Clinical text:
{clinical_text}
Format the output as a JSON array of medication objects with the exact format:
[
{{
"medication": "Topamax",
"RxNorm_code": "36926",
"description": "Topiramate 50 MG Oral Tablet",
"dosage": "50 mg",
"frequency": "daily",
"confidence_score": "95%"
}},
...
]
"""
return _get_structured_data_from_bedrock(prompt, "medications")
except Exception as e:
return json.dumps([{
"medication": "Error",
"RxNorm_code": "Error",
"error": f"Error linking medications to RxNorm codes: {str(e)}",
"confidence_score": "0%"
}])
@tool
def link_snomed(clinical_text: str) -> str:
"""
Extract treatments and procedures from clinical text and link them to SNOMED CT codes.
Args:
clinical_text: The clinical text to analyze
Returns:
JSON string containing extracted treatments with their SNOMED CT codes
"""
try:
# Use Bedrock to extract treatments and link to SNOMED CT codes
prompt = f"""
Extract all treatments, procedures, and clinical actions from the following clinical text and link them to the most appropriate SNOMED CT codes.
For each treatment or procedure, provide:
1. The treatment/procedure as mentioned in the text
2. The SNOMED CT code
3. The official SNOMED CT description
4. A confidence score (95% for high confidence, lower for less certain matches)
Clinical text:
{clinical_text}
Format the output as a JSON array of procedure objects with the exact format:
[
{{
"procedure": "Referral to neurologist",
"SNOMED_code": "306206005",
"description": "Referral to neurology service",
"confidence_score": "95%"
}},
...
]
"""
return _get_structured_data_from_bedrock(prompt, "treatments")
except Exception as e:
return json.dumps([{
"procedure": "Error",
"SNOMED_code": "Error",
"error": f"Error linking treatments to SNOMED CT codes: {str(e)}",
"confidence_score": "0%"
}])
def _get_icd_from_api(diagnosis: str, api_key: str = None) -> str:
"""
Query NLM Clinical Tables API for ICD-10 codes.
API Documentation: https://clinicaltables.nlm.nih.gov/apidoc/icd10cm/v3/doc.html
"""
params = {
"terms": diagnosis,
"sf": "code,name",
"df": "code,name",
"maxResults": 5
}
# Note: This API doesn't require authentication for basic usage
response = requests.get(ICD10_API_BASE_URL, params=params)
if response.status_code == 200:
data = response.json()
results = []
# The API returns data in the format [total, abbreviation, items_json, codes]
# where codes is a list of codes and items_json contains descriptions
if len(data) >= 4:
codes = data[3] # List of codes
descriptions = data[2] # List of descriptions
for i, code in enumerate(codes):
if i < len(descriptions):
# Calculate confidence score - higher for earlier results
confidence_score = f"{max(95 - (i * 5), 70)}%"
results.append({
"diagnosis": diagnosis,
"ICD10_code": code,
"description": descriptions[i],
"confidence_score": confidence_score
})
return json.dumps(results)
else:
return json.dumps([{
"diagnosis": diagnosis,
"error": f"API error: {response.status_code}",
"confidence_score": "0%"
}])
def _get_rx_from_api(medication: str, api_key: str = None) -> str:
"""
Query RxNav API for RxNorm codes.
API Documentation: https://lhncbc.nlm.nih.gov/RxNav/APIs/RxNormAPIs.html
"""
# Step 1: Find RxCUI for the medication name
params = {
"name": medication
}
# RxNav API doesn't require authentication
response = requests.get(f"{RXNORM_API_BASE_URL}", params=params)
if response.status_code != 200:
return json.dumps([{
"medication": medication,
"error": f"API error: {response.status_code}",
"confidence_score": "0%"
}])
# Parse the XML response
import xml.etree.ElementTree as ET
root = ET.fromstring(response.content)
# Extract RxCUI values
rxcui_elements = root.findall(".//rxnormId")
if not rxcui_elements:
return json.dumps([{
"medication": medication,
"RxNorm_code": "Not found",
"confidence_score": "0%"
}])
results = []
# For each RxCUI, get additional information
for i, rxcui_element in enumerate(rxcui_elements[:3]): # Limit to first 3 results
rxcui = rxcui_element.text
# Step 2: Get related information for this RxCUI
info_url = RXNORM_INFO_API_BASE_URL.format(rxcui=rxcui)
info_response = requests.get(info_url)
if info_response.status_code == 200:
info_root = ET.fromstring(info_response.content)
# Extract concept information
concept_name = ""
concept_elements = info_root.findall(".//conceptProperties")
for concept in concept_elements:
name_element = concept.find("name")
tty_element = concept.find("tty") # Term Type
if name_element is not None and tty_element is not None:
# Prioritize SCD (Semantic Clinical Drug) or IN (Ingredient) term types
if tty_element.text in ["SCD", "IN", "BN"]:
concept_name = name_element.text
break
# If we didn't find a preferred term type, use the first name
if not concept_name and concept_elements and concept_elements[0].find("name") is not None:
concept_name = concept_elements[0].find("name").text
# Calculate confidence score - higher for earlier results
confidence_score = f"{max(95 - (i * 5), 70)}%"
results.append({
"medication": medication,
"RxNorm_code": rxcui,
"description": concept_name or medication,
"confidence_score": confidence_score
})
return json.dumps(results)
def _get_snomed_from_api(treatment: str, api_key: str = None) -> str:
"""
Query SNOMED CT browser API for SNOMED CT codes.
Uses the Snowstorm terminology server API that powers the SNOMED CT browser.
"""
# The Snowstorm API endpoint for concept search
search_url = f"{SNOMED_API_BASE_URL}/search"
params = {
"term": treatment,
"activeFilter": True,
"offset": 0,
"limit": 5,
# US Edition
"branch": "MAIN/SNOMEDCT-US",
# English language
"language": "en",
# Return FSN (Fully Specified Name) and PT (Preferred Term)
"returnIdField": True,
"returnFsnField": True,
"returnPtField": True
}
headers = {}
if api_key:
headers["Authorization"] = f"Bearer {api_key}"
try:
response = requests.get(search_url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
results = []
if "items" in data and len(data["items"]) > 0:
for i, item in enumerate(data["items"]):
concept_id = item.get("conceptId")
# Get the Fully Specified Name (FSN)
fsn = item.get("fsn", {}).get("term", "")
# Get the Preferred Term (PT)
pt = item.get("pt", {}).get("term", "")
# Calculate confidence score - higher for earlier results
confidence_score = f"{max(95 - (i * 5), 70)}%"
results.append({
"procedure": treatment,
"SNOMED_code": concept_id,
"description": pt or fsn,
"confidence_score": confidence_score
})
return json.dumps(results)
else:
return json.dumps([{
"procedure": treatment,
"SNOMED_code": "Not found",
"confidence_score": "0%"
}])
else:
return json.dumps([{
"procedure": treatment,
"error": f"API error: {response.status_code}",
"confidence_score": "0%"
}])
except Exception as e:
return json.dumps([{
"procedure": treatment,
"error": f"Error querying SNOMED CT API: {str(e)}",
"confidence_score": "0%"
}])
def _get_medical_code_from_bedrock(term: str, code_system: str, instruction: str) -> str:
"""Use Amazon Bedrock to look up medical codes."""
try:
# Initialize Bedrock client
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
# Prepare request for Claude model
model_id = os.environ.get('BEDROCK_MODEL_ID', 'anthropic.claude-3-sonnet-20240229-v1:0')
# Adjust prompt based on code system
if code_system == "ICD-10":
code_field = "ICD10_code"
term_field = "diagnosis"
elif code_system == "RxNorm":
code_field = "RxNorm_code"
term_field = "medication"
else: # SNOMED CT
code_field = "SNOMED_code"
term_field = "procedure"
prompt = f"""
{instruction}: "{term}"
Return the result in this exact JSON format:
{{
"{term_field}": "The exact term provided",
"{code_field}": "The code",
"description": "The official description",
"confidence_score": "95%"
}}
If multiple codes are possible, return an array of JSON objects in the above format, with decreasing confidence scores (95%, 90%, 85%, etc.).
"""
request_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": prompt
}
]
}
# Invoke Bedrock model
response = bedrock_runtime.invoke_model(
modelId=model_id,
body=json.dumps(request_body)
)
# Parse response
response_body = json.loads(response['body'].read().decode('utf-8'))
result = response_body['content'][0]['text']
# Extract JSON from response if needed
import re
json_match = re.search(r'```json\n(.*?)\n```', result, re.DOTALL)
if json_match:
result = json_match.group(1)
# Ensure result is valid JSON
try:
parsed_result = json.loads(result)
# If it's not a list, convert to a list with one item
if not isinstance(parsed_result, list):
parsed_result = [parsed_result]
return json.dumps(parsed_result)
except:
# If not valid JSON, create a fallback response
return json.dumps([{
term_field: term,
code_field: "Unknown",
"description": "Could not determine code",
"confidence_score": "0%"
}])
except Exception as e:
if code_system == "ICD-10":
code_field = "ICD10_code"
term_field = "diagnosis"
elif code_system == "RxNorm":
code_field = "RxNorm_code"
term_field = "medication"
else: # SNOMED CT
code_field = "SNOMED_code"
term_field = "procedure"
return json.dumps([{
term_field: term,
code_field: "Error",
"error": f"Error using Bedrock for code lookup: {str(e)}",
"confidence_score": "0%"
}])
def _get_structured_data_from_bedrock(prompt: str, data_type: str) -> str:
"""Use Amazon Bedrock to extract structured data from clinical text."""
try:
# Initialize Bedrock client
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
# Prepare request for Claude model
model_id = os.environ.get('BEDROCK_MODEL_ID', 'anthropic.claude-3-sonnet-20240229-v1:0')
# Modify prompt based on data type to ensure consistent output format
if "diagnoses" in data_type:
prompt += """
Each diagnosis object should have this exact format:
{
"diagnosis": "The diagnosis as mentioned in the text",
"ICD10_code": "The ICD-10 code",
"description": "The official ICD-10 description",
"confidence_score": "95%"
}
"""
elif "medications" in data_type:
prompt += """
Each medication object should have this exact format:
{
"medication": "The medication name as mentioned in the text",
"RxNorm_code": "The RxNorm code",
"description": "The standard RxNorm name",
"dosage": "The dosage if specified (or null)",
"frequency": "The frequency if specified (or null)",
"confidence_score": "95%"
}
"""
elif "treatments" in data_type or "procedures" in data_type:
prompt += """
Each treatment/procedure object should have this exact format:
{
"procedure": "The treatment/procedure as mentioned in the text",
"SNOMED_code": "The SNOMED CT code",
"description": "The official SNOMED CT description",
"confidence_score": "95%"
}
"""
request_body = {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 2048,
"messages": [
{
"role": "user",
"content": prompt
}
]
}
# Invoke Bedrock model
response = bedrock_runtime.invoke_model(
modelId=model_id,
body=json.dumps(request_body)
)
# Parse response
response_body = json.loads(response['body'].read().decode('utf-8'))
result = response_body['content'][0]['text']
# Extract JSON from response if needed
import re
json_match = re.search(r'```json\n(.*?)\n```', result, re.DOTALL)
if json_match:
result = json_match.group(1)
# Ensure result is valid JSON
try:
parsed_result = json.loads(result)
return result
except:
# If not valid JSON, create a fallback response
if "diagnoses" in data_type:
return json.dumps([{
"diagnosis": "Could not extract diagnoses",
"ICD10_code": "Unknown",
"description": "Error parsing response",
"confidence_score": "0%"
}])
elif "medications" in data_type:
return json.dumps([{
"medication": "Could not extract medications",
"RxNorm_code": "Unknown",
"description": "Error parsing response",
"confidence_score": "0%"
}])
else: # treatments/procedures
return json.dumps([{
"procedure": "Could not extract procedures",
"SNOMED_code": "Unknown",
"description": "Error parsing response",
"confidence_score": "0%"
}])
except Exception as e:
# Create appropriate error response based on data type
if "diagnoses" in data_type:
return json.dumps([{
"diagnosis": "Error",
"ICD10_code": "Error",
"error": f"Error extracting structured data: {str(e)}",
"confidence_score": "0%"
}])
elif "medications" in data_type:
return json.dumps([{
"medication": "Error",
"RxNorm_code": "Error",
"error": f"Error extracting structured data: {str(e)}",
"confidence_score": "0%"
}])
else: # treatments/procedures
return json.dumps([{
"procedure": "Error",
"SNOMED_code": "Error",
"error": f"Error extracting structured data: {str(e)}",
"confidence_score": "0%"
}])