Spaces:
Sleeping
Sleeping
Update my_tools.py
Browse files- my_tools.py +40 -12
my_tools.py
CHANGED
@@ -5,6 +5,46 @@ import pytz
|
|
5 |
import os
|
6 |
import datetime
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
@tool
|
9 |
def get_weather(location: str, celsius: Optional[bool] = False) -> str:
|
10 |
"""
|
@@ -211,18 +251,6 @@ def search_wikipedia(query: str) -> str:
|
|
211 |
return f"Error fetching Wikipedia data: {str(e)}"
|
212 |
|
213 |
|
214 |
-
|
215 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
216 |
-
@tool
|
217 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
218 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
219 |
-
"""A tool that does nothing yet
|
220 |
-
Args:
|
221 |
-
arg1: the first argument
|
222 |
-
arg2: the second argument
|
223 |
-
"""
|
224 |
-
return "What magic will you build ?"
|
225 |
-
|
226 |
@tool
|
227 |
def get_current_time_in_timezone(timezone: str) -> str:
|
228 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
5 |
import os
|
6 |
import datetime
|
7 |
|
8 |
+
WOLFRAM_RESPONSE_KEYS = [
|
9 |
+
"Result",
|
10 |
+
"Solution",
|
11 |
+
"RealSolution",
|
12 |
+
]
|
13 |
+
|
14 |
+
|
15 |
+
@tool
|
16 |
+
def wolfram(query: str)-> str:
|
17 |
+
"""
|
18 |
+
A wrapper around Wolfram Alpha. Useful for when you need to answer questions about Math,
|
19 |
+
Science, Technology, Culture, Society and Everyday Life. Input should be a search query."
|
20 |
+
Args:
|
21 |
+
query: search query.
|
22 |
+
Returns:
|
23 |
+
A string containing the answer for the query.
|
24 |
+
"""
|
25 |
+
api_key = os.environ["WOLFRAM_API_KEY"]
|
26 |
+
formatted_query = query
|
27 |
+
url = f"http://api.wolframalpha.com/v2/query?appid={api_key}&input={formatted_query}&output=json&format=plaintext"
|
28 |
+
for key in WOLFRAM_RESPONSE_KEYS:
|
29 |
+
url += f"&includepodid={key}"
|
30 |
+
print(f"\nURL:\n{url}")
|
31 |
+
|
32 |
+
try:
|
33 |
+
response = requests.get(url)
|
34 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
35 |
+
|
36 |
+
data = response.json()
|
37 |
+
|
38 |
+
if data.get("error"): # Check if there's an error in the response
|
39 |
+
return f"Error: {data['error'].get('info', 'Unable to fetch Wolfram response.')}"
|
40 |
+
|
41 |
+
response = data.get("queryresult").get("pods")[0].get("subpods")[0].get("plaintext")
|
42 |
+
return response
|
43 |
+
|
44 |
+
except requests.exceptions.RequestException as e:
|
45 |
+
return f"Error fetching Wolfram response: {str(e)}"
|
46 |
+
|
47 |
+
|
48 |
@tool
|
49 |
def get_weather(location: str, celsius: Optional[bool] = False) -> str:
|
50 |
"""
|
|
|
251 |
return f"Error fetching Wikipedia data: {str(e)}"
|
252 |
|
253 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
@tool
|
255 |
def get_current_time_in_timezone(timezone: str) -> str:
|
256 |
"""A tool that fetches the current local time in a specified timezone.
|