|
| 1 | +from typing import Dict, Any, List, Optional |
| 2 | +import requests |
| 3 | +from .base_tool import BaseTool |
| 4 | +from .tool_registry import register_tool |
| 5 | + |
| 6 | + |
| 7 | +@register_tool("ClinicalTrialsGovTool") |
| 8 | +class ClinicalTrialsGovTool(BaseTool): |
| 9 | + """ |
| 10 | + Tool for searching clinical trials using ClinicalTrials.gov API v2. |
| 11 | + """ |
| 12 | + |
| 13 | + BASE_URL = "https://clinicaltrials.gov/api/v2/studies" |
| 14 | + |
| 15 | + def run(self, arguments: Dict[str, Any]) -> Dict[str, Any]: |
| 16 | + """ |
| 17 | + Executes the ClinicalTrials tool action. |
| 18 | +
|
| 19 | + Args: |
| 20 | + arguments (Dict[str, Any]): Dictionary containing the action and its parameters. |
| 21 | + Expected keys: |
| 22 | + - action (str): "search_studies" or "get_study_details" |
| 23 | + - condition (str, optional): Condition to search for. |
| 24 | + - intervention (str, optional): Intervention/Drug to search for. |
| 25 | + - nct_id (str, optional): NCT ID for details. |
| 26 | + - limit (int, optional): Max results (default 10). |
| 27 | +
|
| 28 | + Returns: |
| 29 | + Dict[str, Any]: The results. |
| 30 | + """ |
| 31 | + action = arguments.get("action") |
| 32 | + |
| 33 | + if action == "search_studies": |
| 34 | + return self.search_studies( |
| 35 | + condition=arguments.get("condition"), |
| 36 | + intervention=arguments.get("intervention"), |
| 37 | + limit=arguments.get("limit", 10), |
| 38 | + ) |
| 39 | + elif action == "get_study_details": |
| 40 | + nct_id = arguments.get("nct_id") |
| 41 | + if not nct_id: |
| 42 | + raise ValueError("nct_id is required for get_study_details") |
| 43 | + return self.get_study_details(nct_id) |
| 44 | + else: |
| 45 | + raise ValueError(f"Unknown action: {action}") |
| 46 | + |
| 47 | + def search_studies( |
| 48 | + self, |
| 49 | + condition: Optional[str] = None, |
| 50 | + intervention: Optional[str] = None, |
| 51 | + limit: int = 10, |
| 52 | + ) -> Dict[str, Any]: |
| 53 | + """ |
| 54 | + Search for clinical trials. |
| 55 | + """ |
| 56 | + params = {"pageSize": limit, "format": "json"} |
| 57 | + |
| 58 | + if condition: |
| 59 | + params["query.cond"] = condition |
| 60 | + if intervention: |
| 61 | + params["query.intr"] = intervention |
| 62 | + |
| 63 | + try: |
| 64 | + response = requests.get(self.BASE_URL, params=params, timeout=30) |
| 65 | + response.raise_for_status() |
| 66 | + data = response.json() |
| 67 | + |
| 68 | + studies = [] |
| 69 | + for study in data.get("studies", []): |
| 70 | + proto = study.get("protocolSection", {}) |
| 71 | + ident = proto.get("identificationModule", {}) |
| 72 | + status = proto.get("statusModule", {}) |
| 73 | + |
| 74 | + studies.append( |
| 75 | + { |
| 76 | + "nctId": ident.get("nctId"), |
| 77 | + "title": ident.get("officialTitle") or ident.get("briefTitle"), |
| 78 | + "status": status.get("overallStatus"), |
| 79 | + "conditions": proto.get("conditionsModule", {}).get( |
| 80 | + "conditions", [] |
| 81 | + ), |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + return {"total_count": data.get("totalCount"), "studies": studies} |
| 86 | + except Exception as e: |
| 87 | + return {"error": str(e)} |
| 88 | + |
| 89 | + def get_study_details(self, nct_id: str) -> Dict[str, Any]: |
| 90 | + """ |
| 91 | + Get full details for a study. |
| 92 | + """ |
| 93 | + url = f"{self.BASE_URL}/{nct_id}" |
| 94 | + try: |
| 95 | + response = requests.get(url, timeout=30) |
| 96 | + response.raise_for_status() |
| 97 | + data = response.json() |
| 98 | + |
| 99 | + proto = data.get("protocolSection", {}) |
| 100 | + |
| 101 | + return { |
| 102 | + "nctId": nct_id, |
| 103 | + "title": proto.get("identificationModule", {}).get("officialTitle"), |
| 104 | + "summary": proto.get("descriptionModule", {}).get("briefSummary"), |
| 105 | + "eligibility": proto.get("eligibilityModule", {}), |
| 106 | + "contacts": proto.get("contactsLocationsModule", {}), |
| 107 | + "full_data_link": url, |
| 108 | + } |
| 109 | + except Exception as e: |
| 110 | + return {"error": str(e)} |
0 commit comments