Spaces:
Sleeping
Sleeping
Create wikipediaLookup
Browse files- wikipediaLookup +18 -0
wikipediaLookup
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import tool
|
2 |
+
import wikipedia
|
3 |
+
|
4 |
+
@tool
|
5 |
+
class WikipediaLookupTool:
|
6 |
+
name = "wikipedia_lookup"
|
7 |
+
description = "Look up content from the English Wikipedia based on a query string."
|
8 |
+
|
9 |
+
def __call__(self, query: str) -> str:
|
10 |
+
try:
|
11 |
+
page = wikipedia.page(query)
|
12 |
+
return page.content # full text
|
13 |
+
except wikipedia.DisambiguationError as e:
|
14 |
+
return f"Disambiguation error. Options: {e.options[:5]}"
|
15 |
+
except wikipedia.PageError:
|
16 |
+
return f"Page not found for: {query}"
|
17 |
+
except Exception as e:
|
18 |
+
return f"Unexpected error: {e}"
|