Aramide commited on
Commit
12181f3
·
verified ·
1 Parent(s): 8d23020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -9,9 +9,10 @@ from tools.final_answer import FinalAnswerTool
9
  from Gradio_UI import GradioUI
10
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
12
  @tool
13
  def web_search_tool(query: str, num_results: int = 3) -> str:
14
- """A tool that performs a web search and fetches up-to-date information.
15
 
16
  Args:
17
  query: The search query or topic to look up on the web.
@@ -21,26 +22,26 @@ def web_search_tool(query: str, num_results: int = 3) -> str:
21
  A string containing summarized up-to-date information from the web.
22
  """
23
  try:
 
 
24
 
25
- # Perform the web search
26
- results = web.search(f"{query}")
27
 
28
- # Extract and format results
29
  info_list = []
30
- for i, r in enumerate(results[:num_results]):
31
  title = r.get("title", "No title")
32
- url = r.get("url", "No URL")
33
- snippet = r.get("snippet", "No summary available.")
34
- info_list.append(f"{i+1}. {title}\n{snippet}\n({url})")
35
 
36
- # Combine into one response string
37
- combined = "\n\n".join(info_list)
38
- return f"Here are the top {num_results} results for '{query}':\n\n{combined}"
39
 
40
  except Exception as e:
41
  return f"Error performing web search for '{query}': {str(e)}"
42
 
43
 
 
44
  @tool
45
  def get_current_time_in_timezone(timezone: str) -> str:
46
  """A tool that fetches the current local time in a specified timezone.
 
9
  from Gradio_UI import GradioUI
10
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
+
13
  @tool
14
  def web_search_tool(query: str, num_results: int = 3) -> str:
15
+ """A tool that performs a web search and fetches up-to-date information using DuckDuckGo.
16
 
17
  Args:
18
  query: The search query or topic to look up on the web.
 
22
  A string containing summarized up-to-date information from the web.
23
  """
24
  try:
25
+ ddg = DuckDuckGoSearchTool()
26
+ results = ddg.text(query, max_results=num_results)
27
 
28
+ if not results:
29
+ return f"No results found for '{query}'."
30
 
 
31
  info_list = []
32
+ for i, r in enumerate(results):
33
  title = r.get("title", "No title")
34
+ body = r.get("body", "No summary available.")
35
+ link = r.get("href", "")
36
+ info_list.append(f"{i+1}. {title}\n{body}\n({link})")
37
 
38
+ return f"Here are the top {num_results} results for '{query}':\n\n" + "\n\n".join(info_list)
 
 
39
 
40
  except Exception as e:
41
  return f"Error performing web search for '{query}': {str(e)}"
42
 
43
 
44
+
45
  @tool
46
  def get_current_time_in_timezone(timezone: str) -> str:
47
  """A tool that fetches the current local time in a specified timezone.