Sepideh2027 commited on
Commit
dc29022
·
verified ·
1 Parent(s): eee95bc

Create modules/module_2_tool_executor.py

Browse files
modules/modules/modules/module_2_tool_executor.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # ESearch
44
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
45
+ params = {"db": "pubmed", "term": query, "retmax": 3, "retmode": "json"}
46
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
47
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
48
+
49
+ if not ids:
50
+ return []
51
+
52
+ # ESummary
53
+ self._wait_for_rate_limit()
54
+ url2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
55
+ params2 = {"db": "pubmed", "id": ",".join(ids), "retmode": "json"}
56
+ resp2 = requests.get(url2, params=params2, headers=self.headers, timeout=10)
57
+ data = resp2.json()
58
+
59
+ results = []
60
+ for uid, rec in data.get("result", {}).items():
61
+ if uid == "uids":
62
+ continue
63
+ title = rec.get("title", "")
64
+ if title:
65
+ results.append({
66
+ "title": title,
67
+ "source": "PubMed",
68
+ "identifier": f"PMID:{uid}"
69
+ })
70
+
71
+ return results
72
+
73
+ except Exception as e:
74
+ print(f"⚠️ PubMed Error: {e}")
75
+ return []
76
+
77
+ def _search_clinvar(self, query: str) -> List[Dict]:
78
+ """جستجوی ClinVar"""
79
+ try:
80
+ self._wait_for_rate_limit()
81
+
82
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
83
+ params = {"db": "clinvar", "term": query, "retmax": 3, "retmode": "json"}
84
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
85
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
86
+
87
+ if not ids:
88
+ return []
89
+
90
+ self._wait_for_rate_limit()
91
+ url2 = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi"
92
+ params2 = {"db": "clinvar", "id": ",".join(ids), "retmode": "json"}
93
+ resp2 = requests.get(url2, params=params2, headers=self.headers, timeout=10)
94
+ data = resp2.json()
95
+
96
+ results = []
97
+ for uid, rec in data.get("result", {}).items():
98
+ if uid == "uids":
99
+ continue
100
+ title = rec.get("title", "")
101
+ if title:
102
+ results.append({
103
+ "title": title,
104
+ "source": "ClinVar",
105
+ "identifier": f"RCV:{uid}"
106
+ })
107
+
108
+ return results
109
+
110
+ except Exception as e:
111
+ print(f"⚠️ ClinVar Error: {e}")
112
+ return []
113
+
114
+ def _search_genbank(self, query: str) -> List[Dict]:
115
+ """جستجوی GenBank"""
116
+ try:
117
+ self._wait_for_rate_limit()
118
+
119
+ url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
120
+ params = {"db": "nucleotide", "term": f"{query}[Organism]", "retmax": 1, "retmode": "json"}
121
+ resp = requests.get(url, params=params, headers=self.headers, timeout=10)
122
+ ids = resp.json().get("esearchresult", {}).get("idlist", [])
123
+
124
+ if not ids:
125
+ return []
126
+
127
+ return [{
128
+ "title": f"{query} genome",
129
+ "source": "GenBank",
130
+ "identifier": f"GenBank:{ids[0]}"
131
+ }]
132
+
133
+ except Exception as e:
134
+ print(f"⚠️ GenBank Error: {e}")
135
+ return []