Create tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tools.py
|
| 2 |
+
"""Tool set add-on per smolagents: Wikipedia e arXiv."""
|
| 3 |
+
from smolagents import Tool
|
| 4 |
+
import wikipedia, arxiv
|
| 5 |
+
|
| 6 |
+
class WikipediaTool(Tool):
|
| 7 |
+
name = "wiki"
|
| 8 |
+
description = "Return full content of the most relevant Wikipedia page for the query."
|
| 9 |
+
|
| 10 |
+
def __call__(self, query: str) -> str:
|
| 11 |
+
try:
|
| 12 |
+
page = wikipedia.page(query, auto_suggest=True, redirect=True)
|
| 13 |
+
return page.content
|
| 14 |
+
except (wikipedia.PageError, wikipedia.DisambiguationError) as e:
|
| 15 |
+
return f"WIKI_ERROR: {e}"
|
| 16 |
+
|
| 17 |
+
class ArxivTool(Tool):
|
| 18 |
+
name = "arxiv"
|
| 19 |
+
description = "Return title and abstract of the most recent arXiv paper matching the query."
|
| 20 |
+
|
| 21 |
+
def __call__(self, query: str) -> str:
|
| 22 |
+
search = arxiv.Search(
|
| 23 |
+
query=query,
|
| 24 |
+
max_results=1,
|
| 25 |
+
sort_by=arxiv.SortCriterion.SubmittedDate
|
| 26 |
+
)
|
| 27 |
+
paper = next(search.results(), None)
|
| 28 |
+
if paper:
|
| 29 |
+
return f"{paper.title}\n{paper.summary}"
|
| 30 |
+
return "ARXIV_NOTHING_FOUND"
|