Sepideh2027 commited on
Commit
644217b
·
verified ·
1 Parent(s): 9ef9493

Create module_2_tool_executor.py

Browse files
Files changed (1) hide show
  1. modules/module_2_tool_executor.py +133 -0
modules/module_2_tool_executor.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==========================================================
2
+ # PATHOGENAGENT - MODULE 2: TOOL EXECUTOR
3
+ # ==========================================================
4
+
5
+ import requests
6
+ import time
7
+ from typing import List, Dict
8
+
9
+ class ToolExecutor:
10
+ """اجرای ابزارها و بازیابی شواهد از NCBI"""
11
+
12
+ def __init__(self):
13
+ self.headers = {"User-Agent": "PathogenAgent/1.0"}
14
+ self.last_request_time = 0
15
+
16
+ def _wait_for_rate_limit(self):
17
+ """Rate Limiting (حداکثر ۳ درخواست در ثانیه)"""
18
+ current_time = time.time()
19
+ if current_time - self.last_request_time < 0.333:
20
+ time.sleep(0.333 - (current_time - self.last_request_time))
21
+ self.last_request_time = time.time()
22
+
23
+ def execute(self, query: str, tools: List[str]) -> Dict:
24
+ """اجرای ابزارهای درخواستی"""
25
+ results = {}
26
+
27
+ if "PubMed" in tools:
28
+ results["PubMed"] = self._search_pubmed(query)
29
+
30
+ if "ClinVar" in tools:
31
+ results["ClinVar"] = self._search_clinvar(query)
32
+
33
+ if "GenBank" in tools:
34
+ results["GenBank"] = self._search_genbank(query)
35
+
36
+ return results
37
+
38
+ def _search_pubmed(self, query: str) -> List[Dict]:
39
+ """جستجوی PubMed"""
40
+ try:
41
+ self._wait_for_rate_limit()
42
+
43
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
44
+ params = {"db": "pubmed", "term": query, "retmax": 3, "retmode": "json"}
45
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
46
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
47
+
48
+ if not ids:
49
+ return []
50
+
51
+ self._wait_for_rate_limit()
52
+ url2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
53
+ params2 = {"db": "pubmed", "id": ",".join(ids), "retmode": "json"}
54
+ resp2 = requests.get(url2, params=params2, headers=self.headers, timeout=10)
55
+ data = resp2.json()
56
+
57
+ results = []
58
+ for uid, rec in data.get("result", {}).items():
59
+ if uid == "uids":
60
+ continue
61
+ title = rec.get("title", "")
62
+ if title:
63
+ results.append({
64
+ "title": title,
65
+ "source": "PubMed",
66
+ "identifier": f"PMID:{uid}"
67
+ })
68
+
69
+ return results
70
+
71
+ except Exception as e:
72
+ print(f"⚠️ PubMed Error: {e}")
73
+ return []
74
+
75
+ def _search_clinvar(self, query: str) -> List[Dict]:
76
+ """جستجوی ClinVar"""
77
+ try:
78
+ self._wait_for_rate_limit()
79
+
80
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
81
+ params = {"db": "clinvar", "term": query, "retmax": 3, "retmode": "json"}
82
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
83
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
84
+
85
+ if not ids:
86
+ return []
87
+
88
+ self._wait_for_rate_limit()
89
+ url2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
90
+ params2 = {"db": "clinvar", "id": ",".join(ids), "retmode": "json"}
91
+ resp2 = requests.get(url2, params=params2, headers=self.headers, timeout=10)
92
+ data = resp2.json()
93
+
94
+ results = []
95
+ for uid, rec in data.get("result", {}).items():
96
+ if uid == "uids":
97
+ continue
98
+ title = rec.get("title", "")
99
+ if title:
100
+ results.append({
101
+ "title": title,
102
+ "source": "ClinVar",
103
+ "identifier": f"RCV:{uid}"
104
+ })
105
+
106
+ return results
107
+
108
+ except Exception as e:
109
+ print(f"⚠️ ClinVar Error: {e}")
110
+ return []
111
+
112
+ def _search_genbank(self, query: str) -> List[Dict]:
113
+ """جستجوی GenBank"""
114
+ try:
115
+ self._wait_for_rate_limit()
116
+
117
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
118
+ params = {"db": "nucleotide", "term": f"{query}[Organism]", "retmax": 1, "retmode": "json"}
119
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
120
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
121
+
122
+ if not ids:
123
+ return []
124
+
125
+ return [{
126
+ "title": f"{query} genome",
127
+ "source": "GenBank",
128
+ "identifier": f"GenBank:{ids[0]}"
129
+ }]
130
+
131
+ except Exception as e:
132
+ print(f"⚠️ GenBank Error: {e}")
133
+ return []