Spaces:
Sleeping
Sleeping
| from smolagents import Tool | |
| import wikipedia | |
| class LocalWikipediaTool(Tool): | |
| name = "wikipedia_search" | |
| description = "Use websearch tool first to get the facts, and if needed use this Fallback tool: Use Wikipedia after the web search tool." | |
| inputs = { | |
| "query": {"type": "string", "description": "Search term"} | |
| } | |
| output_type = "string" | |
| def forward(self, query: str) -> str: | |
| try: | |
| # search for related pages | |
| results = wikipedia.search(query) | |
| if not results: | |
| return "No Wikipedia results found." | |
| # pick first result | |
| page_title = results[0] | |
| # get a short summary (2–3 sentences) | |
| summary = wikipedia.summary(page_title, sentences=3) | |
| return f"Title: {page_title}\nSummary: {summary}" | |
| except Exception as e: | |
| return f"Wikipedia error: {e}" | |