mgbam commited on
Commit
7bd0ea1
·
verified ·
1 Parent(s): 6019d24

Update tools/umls_tool.py

Browse files
Files changed (1) hide show
  1. tools/umls_tool.py +45 -19
tools/umls_tool.py CHANGED
@@ -1,36 +1,62 @@
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)
 
1
+ # /home/user/app/tools/umls_tool.py
2
+ from langchain_core.tools import BaseTool
3
+ from typing import Type, Optional # Added Optional
4
  from pydantic import BaseModel, Field
5
+ # Assuming clinical_nlp.umls_bioportal.search_umls_term exists and works
6
  from clinical_nlp.umls_bioportal import search_umls_term
7
  from services.logger import app_logger
8
  from services.metrics import log_tool_usage
9
 
10
  class UMLSInput(BaseModel):
11
+ term: str = Field(description="The specific medical term to search for in UMLS.")
12
+ # Example: search_type: Optional[str] = Field(default="exact", description="Type of search, e.g., 'exact', 'approximate'.")
13
 
14
  class UMLSLookupTool(BaseTool):
15
  name: str = "umls_lookup"
16
  description: str = (
17
+ "Use this tool to find definitions, Concept Unique Identifiers (CUIs), and related concepts for specific medical terms "
18
+ "from the Unified Medical Language System (UMLS). Best for precise terminology lookup. "
19
+ "Input should be a single medical term (e.g., 'myocardial infarction')."
20
  )
21
  args_schema: Type[BaseModel] = UMLSInput
22
 
23
+ def _run(self, term: str) -> str: # Add other args from UMLSInput if defined
24
+ app_logger.info(f"UMLS Tool called with term: '{term}'")
25
+ log_tool_usage(self.name, {"query_term": term}) # Log term for metrics
26
+
27
+ if not term or not term.strip():
28
+ return "Error from UMLS lookup: No search term provided."
29
+
30
+ try:
31
+ # Pass additional args like search_type to search_umls_term if your function supports them
32
+ results = search_umls_term(term) # Example: search_umls_term(term, search_type=search_type)
33
+ except Exception as e:
34
+ app_logger.error(f"Exception during UMLS search for '{term}': {e}", exc_info=True)
35
+ return f"Error performing UMLS lookup for '{term}': An unexpected error occurred during the search."
36
+
37
+ if isinstance(results, dict) and "error" in results:
38
+ app_logger.warning(f"UMLS lookup for '{term}' returned an error: {results['error']}")
39
+ return f"Error from UMLS lookup for '{term}': {results['error']}"
40
+
41
+ if isinstance(results, dict) and results.get("results"):
42
  formatted_results = []
43
+ for res_item in results["results"][:3]: # Limit to top 3 results for brevity
44
+ name = res_item.get('name', 'N/A')
45
+ cui = res_item.get('ui', 'N/A') # Assuming 'ui' is the CUI field from your API response
46
+ # You might want to add definition or semantic type if available
47
+ # definition = res_item.get('definition', 'No definition available.')
48
+ formatted_results.append(f"- Term: {name}, CUI: {cui}")
49
+
50
+ if formatted_results:
51
+ return f"UMLS Search Results for '{term}':\n" + "\n".join(formatted_results)
52
+ else:
53
+ return f"No specific items found in UMLS results for '{term}', though the query was successful."
54
+
55
+ app_logger.warning(f"No results or unexpected format from UMLS for '{term}'. Raw: {str(results)[:200]}")
56
+ return f"No distinct results found or unexpected response format from UMLS for '{term}'."
57
 
58
  async def _arun(self, term: str) -> str:
59
+ # For simplicity, using sync version. Implement true async if search_umls_term is async.
60
+ # import asyncio
61
+ # return await asyncio.to_thread(self._run, term)
62
  return self._run(term)