hasanfaesal commited on
Commit
d77ab2e
·
verified ·
1 Parent(s): 8213cbf

fixed ddgs tool

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -27,15 +27,25 @@ def duckduckgo_search(query: str) -> str:
27
  Returns:
28
  A string containing the top 3 URLs separated by newline characters.
29
  """
30
- # Use the DuckDuckGoSearchTool to perform the search.
31
- # this tool returns a list of dictionaries each with a 'url' key.
32
- results = DuckDuckGoSearchTool(query=query)
33
-
34
- # Extract the top 3 results.
35
- top_results = results[:3]
36
- urls = [result.get("url", "URL not found") for result in top_results]
 
 
 
 
 
 
 
 
37
 
38
- return "\n".join(urls)
 
 
39
 
40
  @tool
41
  def generate_image(prompt: str) -> str:
 
27
  Returns:
28
  A string containing the top 3 URLs separated by newline characters.
29
  """
30
+ try:
31
+ # Initialize the search tool
32
+ search_tool = DuckDuckGoSearchTool()
33
+ # Run the search query
34
+ results = search_tool.run(query)
35
+
36
+ # Ensure results are a list
37
+ if not isinstance(results, list):
38
+ return "Unexpected response format from DuckDuckGo search."
39
+
40
+ # Extract the top 3 results
41
+ top_results = results[:3]
42
+ urls = [result.get("url", "URL not found") for result in top_results]
43
+
44
+ return "\n".join(urls)
45
 
46
+ except Exception as e:
47
+ return f"Error performing DuckDuckGo search: {str(e)}"
48
+
49
 
50
  @tool
51
  def generate_image(prompt: str) -> str: