Update tools/umls_tool.py
Browse files- tools/umls_tool.py +36 -0
tools/umls_tool.py
CHANGED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import BaseTool
|
2 |
+
from typing import Type
|
3 |
+
from pydantic import BaseModel, Field
|
4 |
+
from clinical_nlp.umls_bioportal import search_umls_term
|
5 |
+
from services.logger import app_logger
|
6 |
+
from services.metrics import log_tool_usage
|
7 |
+
|
8 |
+
class UMLSInput(BaseModel):
|
9 |
+
term: str = Field(description="The medical term to search for in UMLS.")
|
10 |
+
|
11 |
+
class UMLSLookupTool(BaseTool):
|
12 |
+
name: str = "umls_lookup"
|
13 |
+
description: str = (
|
14 |
+
"Useful for looking up medical terms, concepts, and their definitions or CUIs "
|
15 |
+
"(Concept Unique Identifiers) in the Unified Medical Language System (UMLS). "
|
16 |
+
"Provide a specific medical term."
|
17 |
+
)
|
18 |
+
args_schema: Type[BaseModel] = UMLSInput
|
19 |
+
|
20 |
+
def _run(self, term: str) -> str:
|
21 |
+
app_logger.info(f"UMLS Tool called with term: {term}")
|
22 |
+
log_tool_usage(self.name)
|
23 |
+
results = search_umls_term(term)
|
24 |
+
if "error" in results:
|
25 |
+
return f"Error from UMLS lookup: {results['error']}"
|
26 |
+
# Format results for LLM consumption
|
27 |
+
if results.get("results"):
|
28 |
+
formatted_results = []
|
29 |
+
for res in results["results"][:3]: # Limit to 3 results
|
30 |
+
formatted_results.append(f"- Term: {res.get('name', 'N/A')}, CUI: {res.get('ui', 'N/A')}") # Assuming 'ui' for CUI based on UMLS API
|
31 |
+
return "UMLS Results:\n" + "\n".join(formatted_results) if formatted_results else "No results found in UMLS."
|
32 |
+
return "No results found or unexpected format from UMLS."
|
33 |
+
|
34 |
+
async def _arun(self, term: str) -> str:
|
35 |
+
# For simplicity, using sync version for now
|
36 |
+
return self._run(term)
|