kfahn commited on
Commit
1cc5977
·
verified ·
1 Parent(s): ae7a494

Update app.py

Browse files

Add joke tool from https://github.com/huggingface/smolagents/blob/main/examples/multiple_tools.py

Files changed (1) hide show
  1. app.py +29 -0
app.py CHANGED
@@ -18,6 +18,35 @@ 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
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
23
  """A tool that fetches the current local time in a specified timezone.
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def get_joke() -> str:
23
+ """
24
+ Fetches a random joke from the JokeAPI.
25
+ This function sends a GET request to the JokeAPI to retrieve a random joke.
26
+ It handles both single jokes and two-part jokes (setup and delivery).
27
+ If the request fails or the response does not contain a joke, an error message is returned.
28
+ Returns:
29
+ str: The joke as a string, or an error message if the joke could not be fetched.
30
+ """
31
+ url = "https://v2.jokeapi.dev/joke/Any?type=single"
32
+
33
+ try:
34
+ response = requests.get(url)
35
+ response.raise_for_status()
36
+
37
+ data = response.json()
38
+
39
+ if "joke" in data:
40
+ return data["joke"]
41
+ elif "setup" in data and "delivery" in data:
42
+ return f"{data['setup']} - {data['delivery']}"
43
+ else:
44
+ return "Error: Unable to fetch joke."
45
+
46
+ except requests.exceptions.RequestException as e:
47
+ return f"Error fetching joke: {str(e)}"
48
+
49
+
50
  @tool
51
  def get_current_time_in_timezone(timezone: str) -> str:
52
  """A tool that fetches the current local time in a specified timezone.