File size: 969 Bytes
04ef9ec
833c04d
 
 
04ef9ec
 
833c04d
 
04ef9ec
833c04d
 
 
 
04ef9ec
 
 
833c04d
 
 
04ef9ec
833c04d
 
04ef9ec
 
833c04d
04ef9ec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# tools.py  –  Wikipedia + arXiv helpers
from smolagents import Tool
import wikipedia, arxiv

MAX_CHARS = 20_000   # evita prompt enormi

class WikipediaTool(Tool):
    name = "wiki"
    description = "Return full content (truncated) of the most relevant Wikipedia page."

    def __call__(self, query: str) -> str:
        try:
            page = wikipedia.page(query, auto_suggest=True, redirect=True)
            return page.content[:MAX_CHARS]
        except (wikipedia.PageError, wikipedia.DisambiguationError):
            return ""

class ArxivTool(Tool):
    name = "arxiv"
    description = "Return title + abstract of the most recent arXiv paper that matches the query."

    def __call__(self, query: str) -> str:
        search = arxiv.Search(query=query, max_results=1,
                              sort_by=arxiv.SortCriterion.SubmittedDate)
        paper = next(search.results(), None)
        return f"{paper.title}\n{paper.summary}" if paper else ""