cowrycode commited on
Commit
899e90e
·
verified ·
1 Parent(s): f8a91e9

Update multiple_tools.py

Browse files
Files changed (1) hide show
  1. multiple_tools.py +38 -10
multiple_tools.py CHANGED
@@ -13,7 +13,9 @@ import os
13
  import chess
14
  import chess.engine
15
  import tempfile
 
16
  from PIL import Image
 
17
  #---------------------------------
18
 
19
  load_dotenv()
@@ -23,8 +25,37 @@ my_search_engine = os.getenv("Google_WebSearch_Engine")
23
  g_search = GoogleSearchToolSpec(key=google_key, engine=my_search_engine, num=3)
24
 
25
  #Wikipedia Search Tool
26
- wikipedia_tool = WikipediaToolSpec()
27
- wikipedia_search_tool = FunctionTool.from_defaults(wikipedia_tool.search_data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  def google_web_search(query : str) -> str:
30
  """
@@ -37,14 +68,11 @@ def google_web_search(query : str) -> str:
37
  str: The snippet of the first search result along with its source link.
38
  """
39
  result = g_search.google_search(query)
40
- # data = result.content
41
- if isinstance(result, str):
42
- import ast
43
- result = ast.literal_eval(result)
44
- first = result[0]
45
- snippet = first["snippet"]
46
- link = first["link"]
47
- return f"Result: {snippet} Source: {link}"
48
 
49
  google_web_search_tool = FunctionTool.from_defaults(google_web_search)
50
 
 
13
  import chess
14
  import chess.engine
15
  import tempfile
16
+ import wikipedia
17
  from PIL import Image
18
+ import wikipedia
19
  #---------------------------------
20
 
21
  load_dotenv()
 
25
  g_search = GoogleSearchToolSpec(key=google_key, engine=my_search_engine, num=3)
26
 
27
  #Wikipedia Search Tool
28
+ #wikipedia_tool = WikipediaToolSpec()
29
+ #wikipedia_search_tool = FunctionTool.from_defaults(wikipedia_tool.search_data)
30
+
31
+ wikipedia.set_lang("en")
32
+
33
+ def wiki_search(query: str) -> str:
34
+ """
35
+ Safe Wikipedia summary tool with disambiguation and fallback protection.
36
+ """
37
+ try:
38
+ return wikipedia.summary(query, sentences=3)
39
+ except wikipedia.DisambiguationError as e:
40
+ # Try the first disambiguation option if available
41
+ if e.options:
42
+ try:
43
+ return wikipedia.summary(e.options[0], sentences=3)
44
+ except Exception as inner:
45
+ return f"Disambiguation fallback failed: {inner}"
46
+ return "Disambiguation error: No options available."
47
+ except wikipedia.PageError:
48
+ search_results = wikipedia.search(query)
49
+ if not search_results:
50
+ return "No relevant Wikipedia page found."
51
+ try:
52
+ return wikipedia.summary(search_results[0], sentences=3)
53
+ except Exception as inner:
54
+ return f"Wikipedia fallback summary error: {inner}"
55
+ except Exception as e:
56
+ return f"Wikipedia general error: {e}"
57
+ wikipedia_search_tool = FunctionTool.from_defaults(wiki_search)
58
+
59
 
60
  def google_web_search(query : str) -> str:
61
  """
 
68
  str: The snippet of the first search result along with its source link.
69
  """
70
  result = g_search.google_search(query)
71
+ output = result[0]
72
+ if "huggingface.co" in output["link"]:
73
+ output = result[1]
74
+ print(output)
75
+ return f"Result: {output["snippet"]} Source: {output["link"]}"
 
 
 
76
 
77
  google_web_search_tool = FunctionTool.from_defaults(google_web_search)
78