First_agent_template / tools /calories_checker.py
cyberosa
fixing the tool
a1fd0ab
raw
history blame
1.21 kB
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 = "any"
def __init__(self, *args, **kwargs):
self.is_initialized = False
self.api_key = os.environ.get("CALORIES_API_KEY", None)
print(self.api_key)
def forward(self, query: str) -> list[dict[str, Any]]:
api_url = "https://api.calorieninjas.com/v1/nutrition?query="
response = requests.get(
api_url + query, headers={"X-Api-Key": str(self.api_key)}
)
if response.status_code == requests.codes.ok:
return response.text
return [{"Error": response.status_code, "message": response.text}]
if __name__ == "__main__":
query = "bread with fried green peppers"
tool = CaloriesCheckerTool()
print(tool.forward(query=query))