Spaces:
Running
Running
| import os | |
| import requests | |
| from typing import Any, Optional | |
| from pydantic import BaseModel | |
| from smolagents.tools import tool | |
| GAME_API_KEY = os.getenv("GAME_API_KEY") | |
| GAME_API_BASE_URL = os.getenv("GAME_API_BASE_URL") | |
| class Game_Response(BaseModel): | |
| name: str | |
| description: str | |
| year_published: int | |
| min_players: int | |
| max_players: int | |
| users_rated: int | |
| average_rating: float | |
| bayes_adjusted_average_rating: float | |
| complexity: float | |
| def boardgame_lookup_tool(q:str)-> list[Game_Response]: | |
| """A tool that fetches information about board games and returns ratings (on a scale up to 10) and weights or complexities (on a scale up to 5). | |
| Args: | |
| q: a search term representing part or all of a board game's name or description, prefixed with either "name:" or "description:" | |
| """ | |
| url = f'{GAME_API_BASE_URL}/games' | |
| headers = {'x-api-key': f'{GAME_API_KEY}'} | |
| response = requests.get(url, headers=headers, params={'q': q}) | |
| api_data_list = response.json() | |
| return [Game_Response(name = api_data['name'], | |
| description = api_data['description'], | |
| year_published = api_data['year_published'], | |
| min_players = api_data['min_players'], | |
| max_players = api_data['max_players'], | |
| users_rated = api_data['users_rated'], | |
| average_rating = api_data['average'], | |
| bayes_adjusted_average_rating = api_data['bayes_average'], | |
| complexity = api_data['weight']) for api_data in api_data_list] | |