Aya1610 commited on
Commit
19ca579
·
verified ·
1 Parent(s): 5cef303

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +12 -4
agent.py CHANGED
@@ -72,14 +72,22 @@ def wiki_search(query: str) -> str:
72
  @tool
73
  def web_search(query: str) -> str:
74
  """
75
- Search the web using DuckDuckGo and return the top 3 results
76
  (title, URL, and snippet).
77
  """
78
- results = ddg(query, max_results=3) or []
 
 
 
 
 
 
 
 
79
  formatted = []
80
  for r in results:
81
- title = r.get("title", "").strip()
82
- url = r.get("href", "").strip()
83
  body = r.get("body", "").strip()
84
  formatted.append(f"• **{title}**\n {url}\n _{body}_")
85
  return "\n\n".join(formatted)
 
72
  @tool
73
  def web_search(query: str) -> str:
74
  """
75
+ Search DuckDuckGo via the DDGS class and return the top 3 results
76
  (title, URL, and snippet).
77
  """
78
+ try:
79
+ with DDGS() as ddgs:
80
+ results = ddgs.text(query, max_results=3)
81
+ except Exception as e:
82
+ return f"DuckDuckGo search error: {e}"
83
+
84
+ if not results:
85
+ return "No results from DuckDuckGo."
86
+
87
  formatted = []
88
  for r in results:
89
+ title = r.get("title", "<no title>").strip()
90
+ url = r.get("href", "<no URL>").strip()
91
  body = r.get("body", "").strip()
92
  formatted.append(f"• **{title}**\n {url}\n _{body}_")
93
  return "\n\n".join(formatted)