Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,30 +1,26 @@
|
|
| 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
|
| 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)
|
| 15 |
-
return
|
| 16 |
|
| 17 |
class ArxivTool(Tool):
|
| 18 |
name = "arxiv"
|
| 19 |
-
description = "Return title
|
| 20 |
|
| 21 |
def __call__(self, query: str) -> str:
|
| 22 |
-
search = arxiv.Search(
|
| 23 |
-
|
| 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"
|
|
|
|
| 1 |
+
# tools.py – Wikipedia + arXiv helpers
|
|
|
|
| 2 |
from smolagents import Tool
|
| 3 |
import wikipedia, arxiv
|
| 4 |
|
| 5 |
+
MAX_CHARS = 20_000 # evita prompt enormi
|
| 6 |
+
|
| 7 |
class WikipediaTool(Tool):
|
| 8 |
name = "wiki"
|
| 9 |
+
description = "Return full content (truncated) of the most relevant Wikipedia page."
|
| 10 |
|
| 11 |
def __call__(self, query: str) -> str:
|
| 12 |
try:
|
| 13 |
page = wikipedia.page(query, auto_suggest=True, redirect=True)
|
| 14 |
+
return page.content[:MAX_CHARS]
|
| 15 |
+
except (wikipedia.PageError, wikipedia.DisambiguationError):
|
| 16 |
+
return ""
|
| 17 |
|
| 18 |
class ArxivTool(Tool):
|
| 19 |
name = "arxiv"
|
| 20 |
+
description = "Return title + abstract of the most recent arXiv paper that matches the query."
|
| 21 |
|
| 22 |
def __call__(self, query: str) -> str:
|
| 23 |
+
search = arxiv.Search(query=query, max_results=1,
|
| 24 |
+
sort_by=arxiv.SortCriterion.SubmittedDate)
|
|
|
|
|
|
|
|
|
|
| 25 |
paper = next(search.results(), None)
|
| 26 |
+
return f"{paper.title}\n{paper.summary}" if paper else ""
|
|
|
|
|
|