yaraa11 commited on
Commit
560e38c
·
verified ·
1 Parent(s): 4f5daf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py CHANGED
@@ -37,6 +37,39 @@ def convert_currency(amount: float, from_currency: str, to_currency: str) -> str
37
  except Exception as e:
38
  return f"Error converting currency: {str(e)}"
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  @tool
42
  def get_current_time_in_timezone(timezone: str) -> str:
 
37
  except Exception as e:
38
  return f"Error converting currency: {str(e)}"
39
 
40
+ import requests
41
+
42
+ @tool
43
+ def web_search_duckduckgo(query: str) -> str:
44
+ """A tool that performs a web search using the DuckDuckGo Instant Answer API.
45
+ Args:
46
+ query: The search term or query to search for.
47
+ Returns:
48
+ A summary of the search results from DuckDuckGo.
49
+ """
50
+ try:
51
+ # DuckDuckGo Instant Answer API
52
+ url = f"https://api.duckduckgo.com/?q={query}&format=json"
53
+
54
+ response = requests.get(url).json()
55
+
56
+ # Check if there is an abstract (summary) to return
57
+ if 'AbstractText' in response and response['AbstractText']:
58
+ return response['AbstractText']
59
+ elif 'RelatedTopics' in response and len(response['RelatedTopics']) > 0:
60
+ results = response['RelatedTopics'][:3] # Show top 3 results
61
+ search_results = []
62
+ for idx, result in enumerate(results):
63
+ title = result.get("Text", "No title")
64
+ url = result.get("FirstURL", "No link")
65
+ search_results.append(f"Result {idx+1}: {title}\nLink: {url}")
66
+ return "\n".join(search_results)
67
+ else:
68
+ return "No relevant search results found."
69
+
70
+ except Exception as e:
71
+ return f"Error fetching search results: {str(e)}"
72
+
73
 
74
  @tool
75
  def get_current_time_in_timezone(timezone: str) -> str: