Kackle commited on
Commit
6d99028
·
verified ·
1 Parent(s): 488b180

added wiki and replace duck duck go as only fallback

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -37,16 +37,38 @@ class ReliableSearchTool(Tool):
37
 
38
  def forward(self, query: str) -> str:
39
  """Search the web with retry logic and fallbacks."""
40
- for attempt in range(self.max_retries):
41
- try:
42
- result = self.ddg_tool(query)
43
- if result and len(result) > 50:
44
- return result
45
- except Exception as e:
46
- print(f"DuckDuckGo search failed (attempt {attempt+1}/{self.max_retries}): {e}")
47
- time.sleep(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- return f"I couldn't search for '{query}' due to search service limitations. Using my existing knowledge instead."
50
 
51
  # --- Basic Agent Definition ---
52
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
@@ -74,7 +96,7 @@ class SlpMultiAgent:
74
  web_agent = CodeAgent(
75
  tools=[ReliableSearchTool(), VisitWebpageTool()], # Use custom reliable search tool
76
  model=model,
77
- additional_authorized_imports=["pandas", "time", "json", "requests"],
78
  max_steps=5, # Keep sufficient steps for complex tasks
79
  name="WebAgent",
80
  verbosity_level=0,
 
37
 
38
  def forward(self, query: str) -> str:
39
  """Search the web with retry logic and fallbacks."""
40
+ # Try Wikipedia first
41
+ try:
42
+ import requests
43
+ wiki_url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
44
+ response = requests.get(wiki_url, timeout=5)
45
+ if response.status_code == 200:
46
+ data = response.json()
47
+ if 'extract' in data and data['extract']:
48
+ return f"Wikipedia: {data['extract']}"
49
+ except Exception as e:
50
+ print(f"Wikipedia search failed: {e}")
51
+
52
+ # Try Wikipedia search API
53
+ try:
54
+ search_url = "https://en.wikipedia.org/api/rest_v1/page/search/" + query.replace(" ", "%20")
55
+ response = requests.get(search_url, timeout=5)
56
+ if response.status_code == 200:
57
+ data = response.json()
58
+ if 'pages' in data and data['pages']:
59
+ return f"Wikipedia search: {data['pages'][0].get('description', 'No description available')}"
60
+ except Exception as e:
61
+ print(f"Wikipedia search API failed: {e}")
62
+
63
+ # Fallback to DuckDuckGo
64
+ try:
65
+ result = self.ddg_tool(query)
66
+ if result and len(result) > 50:
67
+ return result
68
+ except Exception as e:
69
+ print(f"DuckDuckGo search failed: {e}")
70
 
71
+ return f"Search unavailable for '{query}'. Please use your existing knowledge to answer."
72
 
73
  # --- Basic Agent Definition ---
74
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
96
  web_agent = CodeAgent(
97
  tools=[ReliableSearchTool(), VisitWebpageTool()], # Use custom reliable search tool
98
  model=model,
99
+ additional_authorized_imports=["pandas", "time", "json", "requests", "urllib"],
100
  max_steps=5, # Keep sufficient steps for complex tasks
101
  name="WebAgent",
102
  verbosity_level=0,