|
|
|
from langchain_core.tools import BaseTool |
|
from typing import Type, Optional |
|
from pydantic import BaseModel, Field |
|
|
|
from clinical_nlp.umls_bioportal import search_umls_term |
|
from services.logger import app_logger |
|
from services.metrics import log_tool_usage |
|
|
|
class UMLSInput(BaseModel): |
|
term: str = Field(description="The specific medical term to search for in UMLS.") |
|
|
|
|
|
class UMLSLookupTool(BaseTool): |
|
name: str = "umls_lookup" |
|
description: str = ( |
|
"Use this tool to find definitions, Concept Unique Identifiers (CUIs), and related concepts for specific medical terms " |
|
"from the Unified Medical Language System (UMLS). Best for precise terminology lookup. " |
|
"Input should be a single medical term (e.g., 'myocardial infarction')." |
|
) |
|
args_schema: Type[BaseModel] = UMLSInput |
|
|
|
def _run(self, term: str) -> str: |
|
app_logger.info(f"UMLS Tool called with term: '{term}'") |
|
log_tool_usage(self.name, {"query_term": term}) |
|
|
|
if not term or not term.strip(): |
|
return "Error from UMLS lookup: No search term provided." |
|
|
|
try: |
|
|
|
results = search_umls_term(term) |
|
except Exception as e: |
|
app_logger.error(f"Exception during UMLS search for '{term}': {e}", exc_info=True) |
|
return f"Error performing UMLS lookup for '{term}': An unexpected error occurred during the search." |
|
|
|
if isinstance(results, dict) and "error" in results: |
|
app_logger.warning(f"UMLS lookup for '{term}' returned an error: {results['error']}") |
|
return f"Error from UMLS lookup for '{term}': {results['error']}" |
|
|
|
if isinstance(results, dict) and results.get("results"): |
|
formatted_results = [] |
|
for res_item in results["results"][:3]: |
|
name = res_item.get('name', 'N/A') |
|
cui = res_item.get('ui', 'N/A') |
|
|
|
|
|
formatted_results.append(f"- Term: {name}, CUI: {cui}") |
|
|
|
if formatted_results: |
|
return f"UMLS Search Results for '{term}':\n" + "\n".join(formatted_results) |
|
else: |
|
return f"No specific items found in UMLS results for '{term}', though the query was successful." |
|
|
|
app_logger.warning(f"No results or unexpected format from UMLS for '{term}'. Raw: {str(results)[:200]}") |
|
return f"No distinct results found or unexpected response format from UMLS for '{term}'." |
|
|
|
async def _arun(self, term: str) -> str: |
|
|
|
|
|
|
|
return self._run(term) |