k0smes commited on
Commit
1bddcb2
·
verified ·
1 Parent(s): aea31b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -1,34 +1,37 @@
1
- !pip install --upgrade ddgs
2
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
  from tools.final_answer import FinalAnswerTool
 
8
 
9
  from Gradio_UI import GradioUI
10
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
- ddg_tool_instance = DuckDuckGoSearchTool()
13
 
 
14
  def web_search(query: str, max_results: int = 5) -> str:
15
  """
16
- Search the web for up-to-date information using DuckDuckGo.
17
 
18
  Args:
19
  query: The search query (e.g. "Elon Musk current net worth")
20
  max_results: Maximum number of results to return
21
 
22
  Returns:
23
- A string containing the top search results or a message if no results found.
 
24
  """
25
  try:
26
- results = ddg_tool_instance.run(query)
27
  if not results:
28
  return "No results found! Try a less restrictive/shorter query."
29
- # Возьмём только топ N результатов
30
- top_results = results[:max_results] if isinstance(results, list) else [results]
31
- return "\n".join(top_results)
 
32
  except Exception as e:
33
  return f"Error during web search: {str(e)}"
34
 
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
2
  import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
  from tools.final_answer import FinalAnswerTool
7
+ from ddgs import DDGS
8
 
9
  from Gradio_UI import GradioUI
10
 
11
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
12
+ ddg_tool_instance = DDGS()
13
 
14
+ @tool
15
  def web_search(query: str, max_results: int = 5) -> str:
16
  """
17
+ Search the web for up-to-date information using DuckDuckGo via ddgs.
18
 
19
  Args:
20
  query: The search query (e.g. "Elon Musk current net worth")
21
  max_results: Maximum number of results to return
22
 
23
  Returns:
24
+ A string containing the top search results (title and link)
25
+ or a message if no results are found.
26
  """
27
  try:
28
+ results = ddg_tool_instance.text(query, max_results=max_results)
29
  if not results:
30
  return "No results found! Try a less restrictive/shorter query."
31
+
32
+ # Формируем строку с заголовком и ссылкой
33
+ top_results = results[:max_results]
34
+ return "\n".join([f"{r['title']}: {r['href']}" for r in top_results])
35
  except Exception as e:
36
  return f"Error during web search: {str(e)}"
37