File size: 1,019 Bytes
6155246
 
f52ad11
6155246
 
 
 
 
b3409ed
6155246
 
 
 
 
 
5bb830c
6155246
 
 
 
 
f52ad11
6155246
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
import os
from typing import Any, Optional, List, Dict
from smolagents.tools import Tool


class CaloriesCheckerTool(Tool):
    name = "calories_check"
    description = "Based on a query including some food you want to check, it returns the calories and other nutritional parameters of the food"
    inputs = {
        "query": {
            "type": "string",
            "description": "The query with the food you want to check.",
        }
    }
    output_type = "List"

    def __init__(self, *args, **kwargs):
        self.is_initialized = False
        self.api_key = os.environ.get("CALORIES_API_KEY", None)

    def forward(self, query: str) -> list[dict[str, Any]]:
        api_url = "https://api.api-ninjas.com/v1/nutrition?query={}".format(query)
        response = requests.get(api_url, headers={"X-Api-Key": self.api_key})
        if response.status_code == requests.codes.ok:
            return response.text

        return [{"Error": response.status_code, "message": "response.text"}]