kmille50 commited on
Commit
460cc6d
·
verified ·
1 Parent(s): 9f39c86

Update tools/web_search.py

Browse files
Files changed (1) hide show
  1. tools/web_search.py +56 -20
tools/web_search.py CHANGED
@@ -1,27 +1,63 @@
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
- import duckduckgo_search
 
 
4
 
5
- class DuckDuckGoSearchTool(Tool):
6
- name = "web_search"
7
- description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
8
  inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
9
  output_type = "string"
10
 
11
- def __init__(self, max_results=10, **kwargs):
12
- super().__init__()
13
- self.max_results = max_results
14
- try:
15
- from duckduckgo_search import DDGS
16
- except ImportError as e:
17
- raise ImportError(
18
- "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
19
- ) from e
20
- self.ddgs = DDGS(**kwargs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- def forward(self, query: str) -> str:
23
- results = self.ddgs.text(query, max_results=self.max_results)
24
- if len(results) == 0:
25
- raise Exception("No results found! Try a less restrictive/shorter query.")
26
- postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
27
- return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from typing import Any, Optional
2
  from smolagents.tools import Tool
3
+ # import duckduckgo_search
4
+ import requests
5
+ from xml.etree import ElementTree
6
 
7
+ class PubmedSearchTool(Tool):
8
+ name = "pubmed_search"
9
+ description = "Performs a pubmed search based on your query then returns the top search results."
10
  inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
11
  output_type = "string"
12
 
13
+ def pubmed_search_tool(query: str):
14
+ """Outil pour rechercher des articles médicaux sur PubMed en fonction d'une requête."""
15
+
16
+ base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
17
+ params = {
18
+ "db": "pubmed",
19
+ "term": query, # Terme de recherche (ex: "diabetes treatment")
20
+ "retmax": 5, # Nombre maximum de résultats
21
+ "retmode": "xml" # Format XML pour récupérer les résultats
22
+ }
23
+
24
+ response = requests.get(base_url, params=params)
25
+
26
+ if response.status_code != 200:
27
+ return "Erreur lors de la recherche sur PubMed."
28
+
29
+ # Parser la réponse XML pour extraire les IDs des articles
30
+ root = ElementTree.fromstring(response.content)
31
+ article_ids = [id_elem.text for id_elem in root.findall(".//Id")]
32
+
33
+ if not article_ids:
34
+ return "Aucun article trouvé pour cette requête."
35
+
36
+ # Construire les liens vers PubMed
37
+ pubmed_links = [f"https://pubmed.ncbi.nlm.nih.gov/{article_id}/" for article_id in article_ids]
38
+
39
+ return "\n".join(pubmed_links)
40
 
41
+ # class DuckDuckGoSearchTool(Tool):
42
+ # name = "web_search"
43
+ # description = "Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results."
44
+ # inputs = {'query': {'type': 'string', 'description': 'The search query to perform.'}}
45
+ # output_type = "string"
46
+
47
+ # def __init__(self, max_results=10, **kwargs):
48
+ # super().__init__()
49
+ # self.max_results = max_results
50
+ # try:
51
+ # from duckduckgo_search import DDGS
52
+ # except ImportError as e:
53
+ # raise ImportError(
54
+ # "You must install package `duckduckgo_search` to run this tool: for instance run `pip install duckduckgo-search`."
55
+ # ) from e
56
+ # self.ddgs = DDGS(**kwargs)
57
+
58
+ # def forward(self, query: str) -> str:
59
+ # results = self.ddgs.text(query, max_results=self.max_results)
60
+ # if len(results) == 0:
61
+ # raise Exception("No results found! Try a less restrictive/shorter query.")
62
+ # postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}" for result in results]
63
+ # return "## Search Results\n\n" + "\n\n".join(postprocessed_results)