Spaces:
Sleeping
Sleeping
| # tools/wikipedia_tool.py | |
| import wikipedia | |
| wikipedia.set_lang("en") | |
| def wiki_search(query: str) -> str: | |
| """ | |
| Safe Wikipedia summary tool with disambiguation and fallback protection. | |
| """ | |
| try: | |
| return wikipedia.summary(query, sentences=3) | |
| except wikipedia.DisambiguationError as e: | |
| # Try the first disambiguation option if available | |
| if e.options: | |
| try: | |
| return wikipedia.summary(e.options[0], sentences=3) | |
| except Exception as inner: | |
| return f"Disambiguation fallback failed: {inner}" | |
| return "Disambiguation error: No options available." | |
| except wikipedia.PageError: | |
| search_results = wikipedia.search(query) | |
| if not search_results: | |
| return "No relevant Wikipedia page found." | |
| try: | |
| return wikipedia.summary(search_results[0], sentences=3) | |
| except Exception as inner: | |
| return f"Wikipedia fallback summary error: {inner}" | |
| except Exception as e: | |
| return f"Wikipedia general error: {e}" | |