Spaces:
Sleeping
Sleeping
Commit
·
aa097f8
1
Parent(s):
ea4895e
opt
Browse files- tools/wikipedia_tool.py +11 -16
tools/wikipedia_tool.py
CHANGED
|
@@ -7,23 +7,18 @@ wikipedia.set_lang("en")
|
|
| 7 |
def wiki_search(query: str) -> str:
|
| 8 |
"""
|
| 9 |
Search Wikipedia and return a short summary of the topic.
|
| 10 |
-
|
| 11 |
-
Args:
|
| 12 |
-
query (str): The search query.
|
| 13 |
-
|
| 14 |
-
Returns:
|
| 15 |
-
str: Summary text or error message.
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
search_results = wikipedia.search(query)
|
| 24 |
-
if search_results:
|
| 25 |
return wikipedia.summary(search_results[0], sentences=3)
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
except wikipedia.DisambiguationError as e:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
| 7 |
def wiki_search(query: str) -> str:
|
| 8 |
"""
|
| 9 |
Search Wikipedia and return a short summary of the topic.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
try:
|
| 12 |
+
return wikipedia.summary(query, sentences=3)
|
| 13 |
+
except wikipedia.PageError:
|
| 14 |
+
search_results = wikipedia.search(query)
|
| 15 |
+
if search_results:
|
| 16 |
+
try:
|
|
|
|
|
|
|
| 17 |
return wikipedia.summary(search_results[0], sentences=3)
|
| 18 |
+
except Exception as inner:
|
| 19 |
+
return f"Wikipedia error while fetching fallback summary: {inner}"
|
| 20 |
+
return "No relevant Wikipedia page found."
|
| 21 |
except wikipedia.DisambiguationError as e:
|
| 22 |
+
return f"Disambiguation: {', '.join(e.options[:5])}"
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"Wikipedia general error: {e}"
|