Spaces:
Sleeping
Sleeping
Create WikipediaTool.py
Browse files- tools/WikipediaTool.py +18 -0
tools/WikipediaTool.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
import wikipedia
|
| 3 |
+
|
| 4 |
+
class WikipediaTool(Tool):
|
| 5 |
+
name = "wikipedia_reader"
|
| 6 |
+
description = "Fetches the text content of a Wikipedia page given a topic."
|
| 7 |
+
inputs = {
|
| 8 |
+
"topic": {"type": "string", "description": "The Wikipedia page topic to fetch."}
|
| 9 |
+
}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
|
| 12 |
+
def forward(self, topic: str) -> str:
|
| 13 |
+
try:
|
| 14 |
+
return wikipedia.summary(topic, sentences=5) # or full page with wikipedia.page(topic).content
|
| 15 |
+
except wikipedia.exceptions.DisambiguationError as e:
|
| 16 |
+
return f"Disambiguation error. Options: {e.options}"
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error fetching Wikipedia page: {e}"
|