from smolagents import Tool import requests def fetch_wikipedia_article(query: str) -> str: url = "https://en.wikipedia.org/w/api.php" params = { "action": "query", "prop": "extracts", "explaintext": True, "format": "json", "titles": query, } response = requests.get(url, params=params) data = response.json() pages = data.get("query", {}).get("pages", {}) page = next(iter(pages.values())) return page.get("extract", "No content found for this query.") def search_wikipedia(query: str) -> str: url = "https://en.wikipedia.org/w/api.php" params = { "action": "query", "list": "search", "srsearch": query, "format": "json", } response = requests.get(url, params=params) data = response.json() results = data.get("query", {}).get("search", []) if results: return "\n".join([r['title'] for r in results]) return "No search results found." class WikipediaTool(Tool): def __init__(self): super().__init__( name="WikipediaTool", description="Fetches plain text from a Wikipedia article. Input should be the article title.", inputs={"query": "string"}, output_type="string" ) def __call__(self, query: str) -> str: return fetch_wikipedia_article(query) class WikipediaSearchTool(Tool): def __init__(self): super().__init__( name="WikipediaSearchTool", description="Searches Wikipedia for article titles related to a query. Input should be a question or topic.", inputs={"query": "string"}, output_type="string" ) def __call__(self, query: str) -> str: return search_wikipedia(query)