Create umls_rel.py
Browse files- mcp/umls_rel.py +25 -0
mcp/umls_rel.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# mcp/umls_rel.py
|
| 2 |
+
import os, httpx
|
| 3 |
+
from functools import lru_cache
|
| 4 |
+
|
| 5 |
+
UMLS_API_KEY = os.getenv("UMLS_KEY")
|
| 6 |
+
REL_URL = "https://uts-ws.nlm.nih.gov/rest/content/current/CUI/{cui}/relations"
|
| 7 |
+
|
| 8 |
+
async def _get_ticket() -> str:
|
| 9 |
+
from mcp.umls import _get_ticket as fn
|
| 10 |
+
return await fn()
|
| 11 |
+
|
| 12 |
+
@lru_cache(maxsize=512)
|
| 13 |
+
async def fetch_relations(cui: str) -> list[dict]:
|
| 14 |
+
ticket = await _get_ticket()
|
| 15 |
+
params = {"ticket": ticket, "pageSize": 50}
|
| 16 |
+
async with httpx.AsyncClient(timeout=10) as c:
|
| 17 |
+
r = await c.get(REL_URL.format(cui=cui), params=params)
|
| 18 |
+
r.raise_for_status()
|
| 19 |
+
rels = r.json().get("result", {}).get("relation", [])
|
| 20 |
+
return [
|
| 21 |
+
{"label": rel["relationLabel"],
|
| 22 |
+
"cui2": rel["relatedId"],
|
| 23 |
+
"name2": rel["relatedLabel"]}
|
| 24 |
+
for rel in rels
|
| 25 |
+
]
|