trippyrocks commited on
Commit
20d2ae3
·
verified ·
1 Parent(s): 9513289

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -11
app.py CHANGED
@@ -18,20 +18,51 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
21
- def get_capital(state):
 
 
 
 
 
 
 
 
 
22
  import requests
23
- api_url = f"http://api.duckduckgo.com/"
24
- response = requests.get(api_url)
25
- if response.status_code == 200:
 
 
 
 
 
 
 
 
 
 
26
  data = response.json()
27
- return data.get("capital", "No capital information available")
28
- else:
29
- return "Error: Unable to fetch capital data."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Execute the function and prepare the final answer
32
- result = get_weather("New York")
33
- final_answer = f"The current weather in New York is: {result}"
34
- print(final_answer)
35
 
36
  @tool
37
  def get_current_time_in_timezone(timezone: str) -> str:
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def duckduckgo_search(query: str) -> str:
23
+ """A tool that performs a DuckDuckGo search using the Instant Answer API.
24
+
25
+ Args:
26
+ query: The search query string.
27
+
28
+ Returns:
29
+ str: A summary of the search results or an error message.
30
+ """
31
  import requests
32
+
33
+ try:
34
+ url = "https://api.duckduckgo.com/"
35
+ params = {
36
+ "q": query,
37
+ "format": "json",
38
+ "no_html": 1,
39
+ "skip_disambig": 1,
40
+ }
41
+ response = requests.get(url, params=params)
42
+ if response.status_code != 200:
43
+ return f"Error: Received status code {response.status_code} from DuckDuckGo."
44
+
45
  data = response.json()
46
+ abstract_text = data.get("AbstractText", "")
47
+ if abstract_text:
48
+ return f"Result: {abstract_text}"
49
+
50
+ # If no abstract is provided, try returning the first available related topic's text.
51
+ related_topics = data.get("RelatedTopics", [])
52
+ for topic in related_topics:
53
+ if isinstance(topic, dict):
54
+ if "Text" in topic:
55
+ return f"Result: {topic['Text']}"
56
+ if "Topics" in topic:
57
+ for subtopic in topic["Topics"]:
58
+ if "Text" in subtopic:
59
+ return f"Result: {subtopic['Text']}"
60
+
61
+ return "No results found."
62
+
63
+ except Exception as e:
64
+ return f"Error performing search: {str(e)}"
65
 
 
 
 
 
66
 
67
  @tool
68
  def get_current_time_in_timezone(timezone: str) -> str: