File size: 1,854 Bytes
6073dc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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'."