File size: 1,062 Bytes
7bfec74
 
 
 
 
 
 
b7b5308
7bfec74
 
aa097f8
b7b5308
 
 
aa097f8
b7b5308
aa097f8
b7b5308
 
 
 
 
 
 
 
 
 
aa097f8
 
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
# 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}"