| import wikipedia | |
| class WikiTool: | |
| def __init__(self): | |
| self.name = "WikiTool" | |
| self.description = ( | |
| "Performs Wikipedia lookups. Supports actions: 'summary' and 'is_historical_country'.\n" | |
| "Usage:\n" | |
| "- {'action': 'summary', 'topic': 'France'}\n" | |
| "- {'action': 'is_historical_country', 'country_name': 'Yugoslavia'}" | |
| ) | |
| def fetch_summary(self, topic: str) -> str: | |
| try: | |
| summary = wikipedia.summary(topic, sentences=3) | |
| return summary | |
| except wikipedia.DisambiguationError as e: | |
| return f"Disambiguation error: The topic '{topic}' is ambiguous. Suggestions: {e.options[:5]}" | |
| except wikipedia.PageError: | |
| return f"Page error: The topic '{topic}' does not exist on Wikipedia." | |
| except Exception as e: | |
| return f"An unexpected error occurred: {str(e)}" | |
| def is_historical_country(self, country_name: str) -> bool: | |
| try: | |
| summary = wikipedia.summary(country_name, sentences=2).lower() | |
| keywords = [ | |
| "former country", "no longer exists", "historical country", | |
| "was a country", "defunct", "dissolved", "existed until", | |
| "disestablished", "merged into" | |
| ] | |
| return any(keyword in summary for keyword in keywords) | |
| except Exception: | |
| return False | |
| def __call__(self, args: dict): | |
| action = args.get("action") | |
| if action == "summary": | |
| return self.fetch_summary(args.get("topic", "")) | |
| elif action == "is_historical_country": | |
| return self.is_historical_country(args.get("country_name", "")) | |
| else: | |
| return "Error: Unknown action. Use 'summary' or 'is_historical_country'." | |