auday commited on
Commit
0a962f8
·
verified ·
1 Parent(s): e8b7e92

Hadnle multiple images request

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -19,24 +19,27 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
19
  return "What magic will you build ?"
20
 
21
  @tool
22
- def get_cat_image(limit: int = 1) -> str:
23
- """A tool that get a random image of a cat
24
  Args:
25
  limit: Number of images to return between 1 and 100
26
  """
27
- # URL for the cat image API
28
- url = "https://api.thecatapi.com/v1/images/search"
 
 
 
 
29
 
30
  # Send the GET request to the API
31
  response = requests.get(url)
32
 
33
  # Check if the request was successful
34
  if response.status_code == 200:
35
- # Extract the URL of the image from the response JSON
36
  data = response.json() # Parse the response as JSON
37
- image_url = data[0]['url'] # Extract the image URL
38
- # print("Cat Image URL:", image_url)
39
- return image_url
40
  else:
41
  return f"Failed to fetch data. Status code: {response.status_code}"
42
 
 
19
  return "What magic will you build ?"
20
 
21
  @tool
22
+ def get_cat_images(limit: int = 1) -> list:
23
+ """A tool that gets random cat images
24
  Args:
25
  limit: Number of images to return between 1 and 100
26
  """
27
+ # Ensure the limit is within the allowed range
28
+ if limit < 1 or limit > 100:
29
+ return "Limit must be between 1 and 100."
30
+
31
+ # URL for the cat image API with the limit parameter
32
+ url = f"https://api.thecatapi.com/v1/images/search?limit={limit}"
33
 
34
  # Send the GET request to the API
35
  response = requests.get(url)
36
 
37
  # Check if the request was successful
38
  if response.status_code == 200:
39
+ # Extract the URLs of the images from the response JSON
40
  data = response.json() # Parse the response as JSON
41
+ image_urls = [item['url'] for item in data] # Extract URLs for all images
42
+ return image_urls
 
43
  else:
44
  return f"Failed to fetch data. Status code: {response.status_code}"
45