Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,435 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
-
import datetime
|
| 3 |
import requests
|
| 4 |
-
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
|
|
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|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grammar=None,
|
| 62 |
-
planning_interval=None,
|
| 63 |
-
name=None,
|
| 64 |
-
description=None,
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, tool, OpenAIServerModel
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
import yaml
|
| 4 |
+
import os
|
| 5 |
+
from collections import defaultdict
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 9 |
@tool
|
| 10 |
+
def get_pokedex_entry(pokemon_name: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Fetches the English Pokédex entry (flavor text) for the given Pokémon.
|
| 13 |
+
|
| 14 |
Args:
|
| 15 |
+
pokemon_name: Name or ID of the Pokémon (e.g., 'pikachu').
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
A string containing the Pokémon's English Pokédex entry or an error message.
|
| 19 |
"""
|
| 20 |
+
name = pokemon_name.strip().lower()
|
| 21 |
+
url = f"https://pokeapi.co/api/v2/pokemon-species/{name}/"
|
| 22 |
+
response = requests.get(url, timeout=5)
|
| 23 |
+
if response.status_code != 200:
|
| 24 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 25 |
+
data = response.json()
|
| 26 |
+
flavor_texts = [
|
| 27 |
+
entry["flavor_text"]
|
| 28 |
+
for entry in data.get("flavor_text_entries", [])
|
| 29 |
+
if entry["language"]["name"] == "en"
|
| 30 |
+
]
|
| 31 |
+
if not flavor_texts:
|
| 32 |
+
return f"No English Pokédex entry available for '{pokemon_name}'."
|
| 33 |
+
entry = flavor_texts[0].replace("\n", " ").replace("\f", " ")
|
| 34 |
+
title = pokemon_name.strip().title()
|
| 35 |
+
return f"{title}: {entry}"
|
| 36 |
|
| 37 |
@tool
|
| 38 |
+
def get_pokemon_basic_info(pokemon_name: str) -> str:
|
| 39 |
+
"""
|
| 40 |
+
Fetches basic information about a Pokémon, such as its ID, height, weight, and types.
|
| 41 |
+
|
| 42 |
Args:
|
| 43 |
+
pokemon_name: The name or ID of the Pokémon (e.g., 'pikachu').
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
A string summarizing the Pokémon's ID, height, weight, and types, or an error message.
|
| 47 |
"""
|
| 48 |
+
name = pokemon_name.strip().lower()
|
| 49 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 50 |
+
response = requests.get(url, timeout=5)
|
| 51 |
+
if response.status_code != 200:
|
| 52 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 53 |
+
data = response.json()
|
| 54 |
+
types = ', '.join([t['type']['name'].title() for t in data.get('types', [])])
|
| 55 |
+
return (
|
| 56 |
+
f"{data['name'].title()} (ID: {data['id']}):\n"
|
| 57 |
+
f"Height: {data['height']} Weight: {data['weight']}\n"
|
| 58 |
+
f"Types: {types}"
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
@tool
|
| 62 |
+
def get_pokemon_abilities(pokemon_name: str) -> str:
|
| 63 |
+
"""
|
| 64 |
+
Retrieves a list of abilities for a given Pokémon, distinguishing between normal and hidden abilities.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 68 |
+
|
| 69 |
+
Returns:
|
| 70 |
+
A string listing the Pokémon's abilities, labeled as Normal or Hidden, or an error message.
|
| 71 |
+
"""
|
| 72 |
+
name = pokemon_name.strip().lower()
|
| 73 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 74 |
+
response = requests.get(url, timeout=5)
|
| 75 |
+
if response.status_code != 200:
|
| 76 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 77 |
+
data = response.json()
|
| 78 |
+
abilities = []
|
| 79 |
+
for ability_info in data.get('abilities', []):
|
| 80 |
+
ability_name = ability_info['ability']['name'].replace('-', ' ').title()
|
| 81 |
+
label = "Hidden" if ability_info.get('is_hidden', False) else "Normal"
|
| 82 |
+
abilities.append(f"{ability_name} ({label})")
|
| 83 |
+
if not abilities:
|
| 84 |
+
return f"No abilities found for '{pokemon_name}'."
|
| 85 |
+
return f"{pokemon_name.title()}'s abilities:\n" + "\n".join(abilities)
|
| 86 |
|
| 87 |
+
@tool
|
| 88 |
+
def get_pokemon_base_stats(pokemon_name: str) -> str:
|
| 89 |
+
"""
|
| 90 |
+
Fetches the base stats (HP, Attack, Defense, etc.) of a Pokémon.
|
| 91 |
+
|
| 92 |
+
Args:
|
| 93 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 94 |
+
|
| 95 |
+
Returns:
|
| 96 |
+
A string summarizing the Pokémon's base stats or an error message.
|
| 97 |
+
"""
|
| 98 |
+
name = pokemon_name.strip().lower()
|
| 99 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 100 |
+
response = requests.get(url, timeout=5)
|
| 101 |
+
if response.status_code != 200:
|
| 102 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 103 |
+
data = response.json()
|
| 104 |
+
stats = {s['stat']['name'].replace('-', ' ').title(): s['base_stat'] for s in data.get('stats', [])}
|
| 105 |
+
if not stats:
|
| 106 |
+
return f"No base stats found for '{pokemon_name}'."
|
| 107 |
+
stats_str = ', '.join([f"{k}: {v}" for k, v in stats.items()])
|
| 108 |
+
return f"{pokemon_name.title()}'s base stats: {stats_str}"
|
| 109 |
|
| 110 |
+
@tool
|
| 111 |
+
def get_pokemon_evolutions(pokemon_name: str) -> str:
|
| 112 |
+
"""
|
| 113 |
+
Retrieves the evolution chain for a given Pokémon.
|
| 114 |
+
|
| 115 |
+
Args:
|
| 116 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 117 |
+
|
| 118 |
+
Returns:
|
| 119 |
+
A string listing the evolution chain or an error message.
|
| 120 |
+
"""
|
| 121 |
+
species_url = f"https://pokeapi.co/api/v2/pokemon-species/{pokemon_name.strip().lower()}/"
|
| 122 |
+
species_response = requests.get(species_url, timeout=5)
|
| 123 |
+
if species_response.status_code != 200:
|
| 124 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {species_response.status_code})."
|
| 125 |
+
species_data = species_response.json()
|
| 126 |
+
evo_chain_url = species_data.get('evolution_chain', {}).get('url')
|
| 127 |
+
if not evo_chain_url:
|
| 128 |
+
return f"No evolution chain found for '{pokemon_name}'."
|
| 129 |
+
evo_response = requests.get(evo_chain_url, timeout=5)
|
| 130 |
+
if evo_response.status_code != 200:
|
| 131 |
+
return f"Error fetching evolution chain (HTTP {evo_response.status_code})."
|
| 132 |
+
evo_data = evo_response.json()
|
| 133 |
+
def extract_chain(chain):
|
| 134 |
+
names = [chain['species']['name'].title()]
|
| 135 |
+
for evo in chain.get('evolves_to', []):
|
| 136 |
+
names.extend(extract_chain(evo))
|
| 137 |
+
return names
|
| 138 |
+
evo_names = extract_chain(evo_data['chain'])
|
| 139 |
+
return f"Evolution chain: {' → '.join(evo_names)}"
|
| 140 |
|
| 141 |
+
@tool
|
| 142 |
+
def get_pokemon_moves_grouped(pokemon_name: str) -> str:
|
| 143 |
+
"""
|
| 144 |
+
Lists all moves a Pokémon can learn, grouped by move name, showing the minimum level,
|
| 145 |
+
all learning methods, and all game versions. Egg moves are listed separately.
|
| 146 |
+
|
| 147 |
+
Args:
|
| 148 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 149 |
+
|
| 150 |
+
Returns:
|
| 151 |
+
A string listing the Pokémon's moves, grouped by name, with levels, methods, and versions.
|
| 152 |
+
"""
|
| 153 |
+
name = pokemon_name.strip().lower()
|
| 154 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 155 |
+
response = requests.get(url, timeout=5)
|
| 156 |
+
if response.status_code != 200:
|
| 157 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 158 |
+
data = response.json()
|
| 159 |
+
moves_dict = defaultdict(lambda: {'level': None, 'versions': set(), 'methods': set()})
|
| 160 |
+
egg_moves_dict = defaultdict(set)
|
| 161 |
+
for move in data.get('moves', []):
|
| 162 |
+
move_name = move['move']['name'].replace('-', ' ').title()
|
| 163 |
+
for detail in move.get('version_group_details', []):
|
| 164 |
+
method = detail['move_learn_method']['name'].replace('-', ' ').title()
|
| 165 |
+
version = detail['version_group']['name'].replace('-', ' ').title()
|
| 166 |
+
level = detail['level_learned_at']
|
| 167 |
+
if method == "Egg":
|
| 168 |
+
egg_moves_dict[move_name].add(version)
|
| 169 |
+
else:
|
| 170 |
+
moves_dict[move_name]['methods'].add(method)
|
| 171 |
+
moves_dict[move_name]['versions'].add(version)
|
| 172 |
+
if method == "Level Up" and (moves_dict[move_name]['level'] is None or level < moves_dict[move_name]['level']):
|
| 173 |
+
moves_dict[move_name]['level'] = level
|
| 174 |
+
moves_lines = []
|
| 175 |
+
for move_name, info in sorted(moves_dict.items()):
|
| 176 |
+
versions_str = ', '.join(sorted(info['versions']))
|
| 177 |
+
methods_str = ', '.join(sorted(info['methods']))
|
| 178 |
+
if info['level'] is not None:
|
| 179 |
+
moves_lines.append(f"{move_name} (Level {info['level']}, Versions: {versions_str})")
|
| 180 |
+
else:
|
| 181 |
+
moves_lines.append(f"{move_name} (Methods: {methods_str}, Versions: {versions_str})")
|
| 182 |
+
egg_lines = [f"{move_name} (Egg Move in: {', '.join(sorted(versions))})" for move_name, versions in sorted(egg_moves_dict.items())]
|
| 183 |
+
moves_preview = '\n'.join(moves_lines[:20])
|
| 184 |
+
more_moves = f"\n...and {len(moves_lines)-20} more moves." if len(moves_lines) > 20 else ""
|
| 185 |
+
egg_moves_output = '\n'.join(egg_lines) if egg_lines else 'None'
|
| 186 |
+
return (
|
| 187 |
+
f"{pokemon_name.title()} can learn these moves grouped by name with versions and levels:\n"
|
| 188 |
+
f"{moves_preview}{more_moves}\n\n"
|
| 189 |
+
f"Egg moves:\n{egg_moves_output}"
|
| 190 |
+
)
|
| 191 |
|
| 192 |
+
@tool
|
| 193 |
+
def get_pokemon_sprite(pokemon_name: str) -> str:
|
| 194 |
+
"""
|
| 195 |
+
Fetches the default front sprite URL for a Pokémon.
|
| 196 |
+
|
| 197 |
+
Args:
|
| 198 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 199 |
+
|
| 200 |
+
Returns:
|
| 201 |
+
A string containing the sprite URL or an error message.
|
| 202 |
+
"""
|
| 203 |
+
name = pokemon_name.strip().lower()
|
| 204 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 205 |
+
response = requests.get(url, timeout=5)
|
| 206 |
+
if response.status_code != 200:
|
| 207 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 208 |
+
data = response.json()
|
| 209 |
+
sprite_url = data.get('sprites', {}).get('front_default')
|
| 210 |
+
if not sprite_url:
|
| 211 |
+
return f"No sprite found for '{pokemon_name}'."
|
| 212 |
+
return f"{pokemon_name.title()} sprite: {sprite_url}"
|
| 213 |
|
| 214 |
+
@tool
|
| 215 |
+
def get_pokemon_held_items(pokemon_name: str) -> str:
|
| 216 |
+
"""
|
| 217 |
+
Fetches the items a Pokémon can hold in the wild, including rarity and game versions.
|
| 218 |
+
|
| 219 |
+
Args:
|
| 220 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 221 |
+
|
| 222 |
+
Returns:
|
| 223 |
+
A string listing the Pokémon's held items, their rarity, and versions, or an error message.
|
| 224 |
+
"""
|
| 225 |
+
name = pokemon_name.strip().lower()
|
| 226 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 227 |
+
response = requests.get(url, timeout=5)
|
| 228 |
+
if response.status_code != 200:
|
| 229 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 230 |
+
data = response.json()
|
| 231 |
+
held_items = data.get('held_items', [])
|
| 232 |
+
if not held_items:
|
| 233 |
+
return f"No held items found for '{pokemon_name}'."
|
| 234 |
+
item_lines = []
|
| 235 |
+
for item in held_items:
|
| 236 |
+
item_name = item['item']['name'].replace('-', ' ').title()
|
| 237 |
+
versions = ', '.join([v['version']['name'].title() for v in item['version_details']])
|
| 238 |
+
rarity = item['version_details'][0]['rarity']
|
| 239 |
+
item_lines.append(f"{item_name} (Rarity: {rarity}%, Versions: {versions})")
|
| 240 |
+
return f"{pokemon_name.title()}'s held items:\n" + "\n".join(item_lines)
|
| 241 |
+
|
| 242 |
+
@tool
|
| 243 |
+
def get_pokemon_encounter_locations(pokemon_name: str) -> str:
|
| 244 |
+
"""
|
| 245 |
+
Fetches the locations where a Pokémon can be encountered in different game versions.
|
| 246 |
+
|
| 247 |
+
Args:
|
| 248 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 249 |
+
|
| 250 |
+
Returns:
|
| 251 |
+
A string listing the Pokémon's encounter locations by game version, or an error message.
|
| 252 |
+
"""
|
| 253 |
+
name = pokemon_name.strip().lower()
|
| 254 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 255 |
+
response = requests.get(url, timeout=5)
|
| 256 |
+
if response.status_code != 200:
|
| 257 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 258 |
+
data = response.json()
|
| 259 |
+
encounters_url = data.get('location_area_encounters')
|
| 260 |
+
if not encounters_url:
|
| 261 |
+
return f"No encounter data found for '{pokemon_name}'."
|
| 262 |
+
response = requests.get(encounters_url, timeout=5)
|
| 263 |
+
if response.status_code != 200:
|
| 264 |
+
return f"Error fetching encounter data (HTTP {response.status_code})."
|
| 265 |
+
encounters = response.json()
|
| 266 |
+
location_dict = defaultdict(list)
|
| 267 |
+
for encounter in encounters:
|
| 268 |
+
location = encounter['location_area']['name'].replace('-', ' ').title()
|
| 269 |
+
for version_detail in encounter['version_details']:
|
| 270 |
+
version = version_detail['version']['name'].title()
|
| 271 |
+
location_dict[version].append(location)
|
| 272 |
+
output = []
|
| 273 |
+
for version, locations in sorted(location_dict.items()):
|
| 274 |
+
locations_str = ', '.join(sorted(set(locations)))
|
| 275 |
+
output.append(f"{version}: {locations_str}")
|
| 276 |
+
if not output:
|
| 277 |
+
return f"No encounter locations found for '{pokemon_name}'."
|
| 278 |
+
return f"{pokemon_name.title()}'s encounter locations:\n" + "\n".join(output)
|
| 279 |
+
|
| 280 |
+
@tool
|
| 281 |
+
def get_pokemon_type_effectiveness(pokemon_name: str) -> str:
|
| 282 |
+
"""
|
| 283 |
+
Fetches the type effectiveness (weaknesses, resistances, immunities) for a Pokémon's types.
|
| 284 |
+
|
| 285 |
+
Args:
|
| 286 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 287 |
+
|
| 288 |
+
Returns:
|
| 289 |
+
A string summarizing the Pokémon's type effectiveness, or an error message.
|
| 290 |
+
"""
|
| 291 |
+
name = pokemon_name.strip().lower()
|
| 292 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 293 |
+
response = requests.get(url, timeout=5)
|
| 294 |
+
if response.status_code != 200:
|
| 295 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 296 |
+
data = response.json()
|
| 297 |
+
types = [t['type']['url'] for t in data.get('types', [])]
|
| 298 |
+
effectiveness = {'2x': set(), '0.5x': set(), '0x': set()}
|
| 299 |
+
for type_url in types:
|
| 300 |
+
response = requests.get(type_url, timeout=5)
|
| 301 |
+
if response.status_code != 200:
|
| 302 |
+
continue
|
| 303 |
+
type_data = response.json()
|
| 304 |
+
damage_relations = type_data['damage_relations']
|
| 305 |
+
for t in damage_relations['double_damage_from']:
|
| 306 |
+
effectiveness['2x'].add(t['name'].title())
|
| 307 |
+
for t in damage_relations['half_damage_from']:
|
| 308 |
+
effectiveness['0.5x'].add(t['name'].title())
|
| 309 |
+
for t in damage_relations['no_damage_from']:
|
| 310 |
+
effectiveness['0x'].add(t['name'].title())
|
| 311 |
+
output = []
|
| 312 |
+
for key, types in effectiveness.items():
|
| 313 |
+
if types:
|
| 314 |
+
output.append(f"{key}: {', '.join(sorted(types))}")
|
| 315 |
+
if not output:
|
| 316 |
+
return f"No type effectiveness data found for '{pokemon_name}'."
|
| 317 |
+
return f"{pokemon_name.title()}'s type effectiveness:\n" + "\n".join(output)
|
| 318 |
+
|
| 319 |
+
@tool
|
| 320 |
+
def get_pokemon_ability_description(pokemon_name: str) -> str:
|
| 321 |
+
"""
|
| 322 |
+
Fetches the descriptions of a Pokémon's abilities from the ability endpoint.
|
| 323 |
+
|
| 324 |
+
Args:
|
| 325 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 326 |
+
|
| 327 |
+
Returns:
|
| 328 |
+
A string listing each ability and its English description, or an error message.
|
| 329 |
+
"""
|
| 330 |
+
name = pokemon_name.strip().lower()
|
| 331 |
+
url = f"https://pokeapi.co/api/v2/pokemon/{name}/"
|
| 332 |
+
response = requests.get(url, timeout=5)
|
| 333 |
+
if response.status_code != 200:
|
| 334 |
+
return f"Error: Pokémon '{pokemon_name}' not found (HTTP {response.status_code})."
|
| 335 |
+
data = response.json()
|
| 336 |
+
abilities = data.get('abilities', [])
|
| 337 |
+
output = []
|
| 338 |
+
for ability_info in abilities:
|
| 339 |
+
ability_name = ability_info['ability']['name'].replace('-', ' ').title()
|
| 340 |
+
label = "Hidden" if ability_info['is_hidden'] else "Normal"
|
| 341 |
+
ability_url = ability_info['ability']['url']
|
| 342 |
+
response = requests.get(ability_url, timeout=5)
|
| 343 |
+
if response.status_code != 200:
|
| 344 |
+
continue
|
| 345 |
+
ability_data = response.json()
|
| 346 |
+
effect = next((e['effect'] for e in ability_data['effect_entries'] if e['language']['name'] == 'en'), 'No description available.')
|
| 347 |
+
output.append(f"{ability_name} ({label}): {effect}")
|
| 348 |
+
if not output:
|
| 349 |
+
return f"No ability descriptions found for '{pokemon_name}'."
|
| 350 |
+
return f"{pokemon_name.title()}'s ability descriptions:\n" + "\n".join(output)
|
| 351 |
+
|
| 352 |
+
@tool
|
| 353 |
+
def list_pokemon_by_type(type_name: str, limit: int = 10) -> str:
|
| 354 |
+
"""
|
| 355 |
+
Lists Pokémon that have a specific type, up to a specified limit.
|
| 356 |
+
|
| 357 |
+
Args:
|
| 358 |
+
type_name: The name of the type (e.g., 'normal').
|
| 359 |
+
limit: Maximum number of Pokémon to list (default: 10).
|
| 360 |
+
|
| 361 |
+
Returns:
|
| 362 |
+
A string listing Pokémon with the specified type, or an error message.
|
| 363 |
+
"""
|
| 364 |
+
type_name = type_name.strip().lower()
|
| 365 |
+
url = f"https://pokeapi.co/api/v2/type/{type_name}/"
|
| 366 |
+
response = requests.get(url, timeout=5)
|
| 367 |
+
if response.status_code != 200:
|
| 368 |
+
return f"Error: Type '{type_name}' not found (HTTP {response.status_code})."
|
| 369 |
+
data = response.json()
|
| 370 |
+
pokemon_list = [p['pokemon']['name'].title() for p in data.get('pokemon', [])][:limit]
|
| 371 |
+
if not pokemon_list:
|
| 372 |
+
return f"No Pokémon found with type '{type_name}'."
|
| 373 |
+
return f"Pokémon with {type_name.title()} type (up to {limit}):\n" + "\n".join(pokemon_list)
|
| 374 |
+
|
| 375 |
+
@tool
|
| 376 |
+
def get_pokemon_full_profile(pokemon_name: str) -> str:
|
| 377 |
+
"""
|
| 378 |
+
Fetches a complete profile for a Pokémon, including all available data.
|
| 379 |
+
|
| 380 |
+
Args:
|
| 381 |
+
pokemon_name: The name or ID of the Pokémon.
|
| 382 |
+
|
| 383 |
+
Returns:
|
| 384 |
+
A string with a comprehensive Pokémon profile.
|
| 385 |
+
"""
|
| 386 |
+
name = pokemon_name.strip().lower()
|
| 387 |
+
profile = []
|
| 388 |
+
profile.append(get_pokedex_entry(pokemon_name=name))
|
| 389 |
+
profile.append(get_pokemon_basic_info(pokemon_name=name))
|
| 390 |
+
profile.append(get_pokemon_abilities(pokemon_name=name))
|
| 391 |
+
profile.append(get_pokemon_base_stats(pokemon_name=name))
|
| 392 |
+
profile.append(get_pokemon_evolutions(pokemon_name=name))
|
| 393 |
+
profile.append(get_pokemon_moves_grouped(pokemon_name=name))
|
| 394 |
+
profile.append(get_pokemon_held_items(pokemon_name=name))
|
| 395 |
+
profile.append(get_pokemon_encounter_locations(pokemon_name=name))
|
| 396 |
+
profile.append(get_pokemon_type_effectiveness(pokemon_name=name))
|
| 397 |
+
profile.append(get_pokemon_ability_description(pokemon_name=name))
|
| 398 |
+
profile.append(get_pokemon_sprite(pokemon_name=name))
|
| 399 |
+
return f"{name.title()} Profile:\n\n" + "\n\n".join(profile)
|
| 400 |
+
|
| 401 |
+
final_answer = FinalAnswerTool()
|
| 402 |
+
|
| 403 |
+
model = OpenAIServerModel(
|
| 404 |
+
model_id="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 405 |
+
api_base="https://api.together.xyz/v1/",
|
| 406 |
+
api_key=os.environ["TOGETHER_API_KEY"],
|
| 407 |
+
)
|
| 408 |
|
| 409 |
with open("prompts.yaml", 'r') as stream:
|
| 410 |
prompt_templates = yaml.safe_load(stream)
|
| 411 |
+
|
| 412 |
agent = CodeAgent(
|
| 413 |
model=model,
|
| 414 |
+
tools=[
|
| 415 |
+
final_answer,
|
| 416 |
+
get_pokedex_entry,
|
| 417 |
+
get_pokemon_basic_info,
|
| 418 |
+
get_pokemon_abilities,
|
| 419 |
+
get_pokemon_base_stats,
|
| 420 |
+
get_pokemon_evolutions,
|
| 421 |
+
get_pokemon_moves_grouped,
|
| 422 |
+
get_pokemon_sprite,
|
| 423 |
+
get_pokemon_held_items,
|
| 424 |
+
get_pokemon_encounter_locations,
|
| 425 |
+
get_pokemon_type_effectiveness,
|
| 426 |
+
get_pokemon_ability_description,
|
| 427 |
+
list_pokemon_by_type,
|
| 428 |
+
get_pokemon_full_profile
|
| 429 |
+
],
|
| 430 |
max_steps=6,
|
| 431 |
+
verbosity_level=2,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 432 |
prompt_templates=prompt_templates
|
| 433 |
)
|
| 434 |
|
|
|
|
| 435 |
GradioUI(agent).launch()
|