Create agent/clinical_nlp.py
Browse files- agent/clinical_nlp.py +46 -0
agent/clinical_nlp.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
|
4 |
+
def get_umls_api_key():
|
5 |
+
return os.getenv("UMLS_API_KEY")
|
6 |
+
|
7 |
+
def get_bioportal_api_key():
|
8 |
+
return os.getenv("BIOPORTAL_API_KEY")
|
9 |
+
|
10 |
+
def umls_concept_lookup(term):
|
11 |
+
key = get_umls_api_key()
|
12 |
+
if not key:
|
13 |
+
return [{"error": "UMLS API key not set."}]
|
14 |
+
url = f"https://uts-ws.nlm.nih.gov/rest/search/current?string={term}&apiKey={key}"
|
15 |
+
try:
|
16 |
+
response = requests.get(url, timeout=10)
|
17 |
+
results = response.json().get("result", {}).get("results", [])
|
18 |
+
return [
|
19 |
+
{
|
20 |
+
"name": r.get("name"),
|
21 |
+
"ui": r.get("ui"),
|
22 |
+
"rootSource": r.get("rootSource")
|
23 |
+
}
|
24 |
+
for r in results[:8]
|
25 |
+
]
|
26 |
+
except Exception as e:
|
27 |
+
return [{"error": f"UMLS error: {e}"}]
|
28 |
+
|
29 |
+
def bioportal_concept_lookup(term):
|
30 |
+
key = get_bioportal_api_key()
|
31 |
+
if not key:
|
32 |
+
return [{"error": "BioPortal API key not set."}]
|
33 |
+
url = f"https://data.bioontology.org/search?q={term}&apikey={key}"
|
34 |
+
try:
|
35 |
+
response = requests.get(url, timeout=10)
|
36 |
+
matches = response.json().get("collection", [])
|
37 |
+
return [
|
38 |
+
{
|
39 |
+
"prefLabel": m.get("prefLabel"),
|
40 |
+
"ontology": m.get("links", {}).get("ontology"),
|
41 |
+
"id": m.get("@id")
|
42 |
+
}
|
43 |
+
for m in matches[:8]
|
44 |
+
]
|
45 |
+
except Exception as e:
|
46 |
+
return [{"error": f"BioPortal error: {e}"}]
|