Jofthomas commited on
Commit
8c2b13b
·
verified ·
1 Parent(s): d1c98fa

Upload agents.py

Browse files
Files changed (1) hide show
  1. agents.py +448 -0
agents.py ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import asyncio
4
+ import random
5
+ import time
6
+
7
+ # --- OpenAI ---
8
+ from openai import AsyncOpenAI, APIError
9
+
10
+ # --- Google Gemini ---
11
+ from google import genai
12
+ from google.genai import types
13
+
14
+ # --- Mistral AI ---
15
+ from mistralai import Mistral
16
+ import time
17
+
18
+ # --- Poke-Env ---
19
+ from poke_env.player import Player
20
+ from poke_env.environment.battle import Battle
21
+ from poke_env.environment.move import Move
22
+ from poke_env.environment.pokemon import Pokemon
23
+ from typing import Optional, Dict, Any, Union
24
+
25
+ class MaxDamagePlayer(Player):
26
+ def choose_move(self, battle):
27
+ # Chooses a move with the highest base power when possible
28
+ if battle.available_moves:
29
+ # Iterating over available moves to find the one with the highest base power
30
+ best_move = max(battle.available_moves, key=lambda move: move.base_power)
31
+ # Creating an order for the selected move
32
+ return self.create_order(best_move)
33
+ else:
34
+ # If no attacking move is available, perform a random switch
35
+ # This involves choosing a random move, which could be a switch or another available action
36
+ return self.choose_random_move(battle)
37
+
38
+ # --- Helper Function & Base Class ---
39
+ def normalize_name(name: str) -> str:
40
+ """Lowercase and remove non-alphanumeric characters."""
41
+ return "".join(filter(str.isalnum, name)).lower()
42
+
43
+ STANDARD_TOOL_SCHEMA = {
44
+ "choose_move": {
45
+ "name": "choose_move",
46
+ "description": "Selects and executes an available attacking or status move.",
47
+ "parameters": {
48
+ "type": "object",
49
+ "properties": {
50
+ "move_name": {
51
+ "type": "string",
52
+ "description": "The exact name or ID (e.g., 'thunderbolt', 'swordsdance') of the move to use. Must be one of the available moves.",
53
+ },
54
+ },
55
+ "required": ["move_name"],
56
+ },
57
+ },
58
+ "choose_switch": {
59
+ "name": "choose_switch",
60
+ "description": "Selects an available Pokémon from the bench to switch into.",
61
+ "parameters": {
62
+ "type": "object",
63
+ "properties": {
64
+ "pokemon_name": {
65
+ "type": "string",
66
+ "description": "The exact name of the Pokémon species to switch to (e.g., 'Pikachu', 'Charizard'). Must be one of the available switches.",
67
+ },
68
+ },
69
+ "required": ["pokemon_name"],
70
+ },
71
+ },
72
+ }
73
+
74
+ # --- OpenAI Tools Schema (with 'type' field) ---
75
+ OPENAI_TOOL_SCHEMA = {
76
+ "choose_move": {
77
+ "type": "function",
78
+ "function": {
79
+ "name": "choose_move",
80
+ "description": "Selects and executes an available attacking or status move.",
81
+ "parameters": {
82
+ "type": "object",
83
+ "properties": {
84
+ "move_name": {
85
+ "type": "string",
86
+ "description": "The exact name or ID (e.g., 'thunderbolt', 'swordsdance') of the move to use. Must be one of the available moves.",
87
+ },
88
+ },
89
+ "required": ["move_name"],
90
+ },
91
+ }
92
+ },
93
+ "choose_switch": {
94
+ "type": "function",
95
+ "function": {
96
+ "name": "choose_switch",
97
+ "description": "Selects an available Pokémon from the bench to switch into.",
98
+ "parameters": {
99
+ "type": "object",
100
+ "properties": {
101
+ "pokemon_name": {
102
+ "type": "string",
103
+ "description": "The exact name of the Pokémon species to switch to (e.g., 'Pikachu', 'Charizard'). Must be one of the available switches.",
104
+ },
105
+ },
106
+ "required": ["pokemon_name"],
107
+ },
108
+ }
109
+ },
110
+ }
111
+
112
+
113
+ class LLMAgentBase(Player):
114
+ def __init__(self, *args, battle_delay=0.0, **kwargs):
115
+ super().__init__(*args, **kwargs)
116
+ self.standard_tools = STANDARD_TOOL_SCHEMA
117
+ self.battle_history = []
118
+ self.battle_delay = battle_delay
119
+
120
+ def _format_battle_state(self, battle: Battle) -> str:
121
+ active_pkmn = battle.active_pokemon
122
+ active_pkmn_info = f"Your active Pokemon: {active_pkmn.species} " \
123
+ f"(Type: {'/'.join(map(str, active_pkmn.types))}) " \
124
+ f"HP: {active_pkmn.current_hp_fraction * 100:.1f}% " \
125
+ f"Status: {active_pkmn.status.name if active_pkmn.status else 'None'} " \
126
+ f"Boosts: {active_pkmn.boosts}"
127
+
128
+ opponent_pkmn = battle.opponent_active_pokemon
129
+ opp_info_str = "Unknown"
130
+ if opponent_pkmn:
131
+ opp_info_str = f"{opponent_pkmn.species} " \
132
+ f"(Type: {'/'.join(map(str, opponent_pkmn.types))}) " \
133
+ f"HP: {opponent_pkmn.current_hp_fraction * 100:.1f}% " \
134
+ f"Status: {opponent_pkmn.status.name if opponent_pkmn.status else 'None'} " \
135
+ f"Boosts: {opponent_pkmn.boosts}"
136
+ opponent_pkmn_info = f"Opponent's active Pokemon: {opp_info_str}"
137
+
138
+ available_moves_info = "Available moves:\n"
139
+ if battle.available_moves:
140
+ available_moves_info += "\n".join(
141
+ [f"- {move.id} (Type: {move.type}, BP: {move.base_power}, Acc: {move.accuracy}, PP: {move.current_pp}/{move.max_pp}, Cat: {move.category.name})"
142
+ for move in battle.available_moves]
143
+ )
144
+ else:
145
+ available_moves_info += "- None (Must switch or Struggle)"
146
+
147
+ available_switches_info = "Available switches:\n"
148
+ if battle.available_switches:
149
+ available_switches_info += "\n".join(
150
+ [f"- {pkmn.species} (HP: {pkmn.current_hp_fraction * 100:.1f}%, Status: {pkmn.status.name if pkmn.status else 'None'})"
151
+ for pkmn in battle.available_switches]
152
+ )
153
+ else:
154
+ available_switches_info += "- None"
155
+
156
+ state_str = f"{active_pkmn_info}\n" \
157
+ f"{opponent_pkmn_info}\n\n" \
158
+ f"{available_moves_info}\n\n" \
159
+ f"{available_switches_info}\n\n" \
160
+ f"Weather: {battle.weather}\n" \
161
+ f"Terrains: {battle.fields}\n" \
162
+ f"Your Side Conditions: {battle.side_conditions}\n" \
163
+ f"Opponent Side Conditions: {battle.opponent_side_conditions}"
164
+ return state_str.strip()
165
+
166
+ def _find_move_by_name(self, battle: Battle, move_name: str) -> Optional[Move]:
167
+ normalized_name = normalize_name(move_name)
168
+ # Prioritize exact ID match
169
+ for move in battle.available_moves:
170
+ if move.id == normalized_name:
171
+ return move
172
+ # Fallback: Check display name (less reliable)
173
+ for move in battle.available_moves:
174
+ if move.name.lower() == move_name.lower():
175
+ print(f"Warning: Matched move by display name '{move.name}' instead of ID '{move.id}'. Input was '{move_name}'.")
176
+ return move
177
+ return None
178
+
179
+ def _find_pokemon_by_name(self, battle: Battle, pokemon_name: str) -> Optional[Pokemon]:
180
+ normalized_name = normalize_name(pokemon_name)
181
+ for pkmn in battle.available_switches:
182
+ # Normalize the species name for comparison
183
+ if normalize_name(pkmn.species) == normalized_name:
184
+ return pkmn
185
+ return None
186
+
187
+ async def choose_move(self, battle: Battle) -> str:
188
+ #time.sleep(self.battle_delay)
189
+
190
+ battle_state_str = self._format_battle_state(battle)
191
+ decision_result = await self._get_llm_decision(battle_state_str)
192
+ print(decision_result)
193
+ decision = decision_result.get("decision")
194
+ error_message = decision_result.get("error")
195
+ action_taken = False
196
+ fallback_reason = ""
197
+
198
+ if decision:
199
+ function_name = decision.get("name")
200
+ args = decision.get("arguments", {})
201
+ if function_name == "choose_move":
202
+ move_name = args.get("move_name")
203
+ if move_name:
204
+ chosen_move = self._find_move_by_name(battle, move_name)
205
+ if chosen_move and chosen_move in battle.available_moves:
206
+ action_taken = True
207
+ chat_msg = f"AI Decision: Using move '{chosen_move.id}'."
208
+ print(chat_msg)
209
+ return self.create_order(chosen_move)
210
+ else:
211
+ fallback_reason = f"LLM chose unavailable/invalid move '{move_name}'."
212
+ else:
213
+ fallback_reason = "LLM 'choose_move' called without 'move_name'."
214
+ elif function_name == "choose_switch":
215
+ pokemon_name = args.get("pokemon_name")
216
+ if pokemon_name:
217
+ chosen_switch = self._find_pokemon_by_name(battle, pokemon_name)
218
+ if chosen_switch and chosen_switch in battle.available_switches:
219
+ action_taken = True
220
+ chat_msg = f"AI Decision: Switching to '{chosen_switch.species}'."
221
+ print(chat_msg)
222
+ return self.create_order(chosen_switch)
223
+ else:
224
+ fallback_reason = f"LLM chose unavailable/invalid switch '{pokemon_name}'."
225
+ else:
226
+ fallback_reason = "LLM 'choose_switch' called without 'pokemon_name'."
227
+ else:
228
+ fallback_reason = f"LLM called unknown function '{function_name}'."
229
+
230
+ if not action_taken:
231
+ if not fallback_reason:
232
+ if error_message:
233
+ fallback_reason = f"API Error: {error_message}"
234
+ elif decision is None:
235
+ fallback_reason = "LLM did not provide a valid function call."
236
+ else:
237
+ fallback_reason = "Unknown error processing LLM decision."
238
+
239
+ print(f"Warning: {fallback_reason} Choosing random action.")
240
+
241
+ if battle.available_moves or battle.available_switches:
242
+ return self.choose_random_move(battle)
243
+ else:
244
+ print("AI Fallback: No moves or switches available. Using Struggle/Default.")
245
+ return self.choose_default_move(battle)
246
+
247
+ async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
248
+ raise NotImplementedError("Subclasses must implement _get_llm_decision")
249
+ # --- Google Gemini Agent ---
250
+ class GeminiAgent(LLMAgentBase):
251
+ """Uses Google Gemini API for decisions."""
252
+ def __init__(self, api_key: str = None, model: str = "gemini-1.5-flash", avatar: str = "steven", *args, **kwargs):
253
+ # Set avatar before calling parent constructor
254
+ kwargs['avatar'] = avatar
255
+ super().__init__(*args, **kwargs)
256
+ self.model_name = model
257
+ used_api_key = api_key or os.environ.get("GOOGLE_API_KEY")
258
+ if not used_api_key:
259
+ raise ValueError("Google API key not provided or found in GOOGLE_API_KEY env var.")
260
+
261
+ # Initialize Gemini client using the correct API
262
+ self.genai_client = genai.Client(api_key=used_api_key)
263
+
264
+ # Configure the tools for function calling
265
+ self.function_declarations = list(self.standard_tools.values())
266
+
267
+ async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
268
+ """Sends state to the Gemini API and gets back the function call decision."""
269
+ prompt = (
270
+ "Based on the current battle state, decide the best action: either use an available move or switch to an available Pokémon. "
271
+ "Consider type matchups, HP, status conditions, field effects, entry hazards, and potential opponent actions. "
272
+ "Only choose actions listed as available using their exact ID (for moves) or species name (for switches). "
273
+ "Use the provided functions to indicate your choice.\n\n"
274
+ f"Current Battle State:\n{battle_state}\n\n"
275
+ "Choose the best action by calling the appropriate function ('choose_move' or 'choose_switch')."
276
+ )
277
+
278
+ try:
279
+ # Configure tools using the Gemini API format
280
+ tools = genai.types.Tool(function_declarations=self.function_declarations)
281
+ config = genai.types.GenerateContentConfig(tools=[tools],automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True))
282
+
283
+ # Send request to the model
284
+ response = self.genai_client.models.generate_content(
285
+ model=self.model_name,
286
+ contents=prompt,
287
+ config=config
288
+ )
289
+ try:
290
+ function_calls = response.function_calls
291
+ if function_calls:
292
+ function_name = function_calls[0].name
293
+ arguments = function_calls[0].args
294
+ return {"decision": {"name": function_name, "arguments": arguments}}
295
+ else:
296
+ return {"error": "Gemini did not return a function call."}
297
+ except Exception as e:
298
+ return {"error": f"Model called unknown @function '{function_name}'."}
299
+
300
+ # No function call found
301
+ return {"error": "Gemini did not return a function call."}
302
+
303
+ except Exception as e:
304
+ print(f"Unexpected error during Gemini processing: {e}")
305
+ import traceback
306
+ traceback.print_exc()
307
+ return {"error": f"Unexpected error: {str(e)}"}
308
+
309
+
310
+ # --- OpenAI Agent ---
311
+ class OpenAIAgent(LLMAgentBase):
312
+ """Uses OpenAI API for decisions."""
313
+ def __init__(self, api_key: str = None, model: str = "gpt-4o", avatar: str = "giovanni", *args, **kwargs):
314
+ # Set avatar before calling parent constructor
315
+ kwargs['avatar'] = avatar
316
+ super().__init__(*args, **kwargs)
317
+ self.model = model
318
+ used_api_key = api_key or os.environ.get("OPENAI_API_KEY")
319
+ if not used_api_key:
320
+ raise ValueError("OpenAI API key not provided or found in OPENAI_API_KEY env var.")
321
+ self.openai_client = AsyncOpenAI(api_key=used_api_key)
322
+
323
+ # Use the OpenAI-specific schema with type field
324
+ self.openai_tools = list(OPENAI_TOOL_SCHEMA.values())
325
+
326
+ async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
327
+ system_prompt = (
328
+ "You are a skilled Pokemon battle AI. Your goal is to win the battle. "
329
+ "Based on the current battle state, decide the best action: either use an available move or switch to an available Pokémon. "
330
+ "Consider type matchups, HP, status conditions, field effects, entry hazards, and potential opponent actions. "
331
+ "Only choose actions listed as available using their exact ID (for moves) or species name (for switches). "
332
+ "Use the provided functions to indicate your choice."
333
+ )
334
+ user_prompt = f"Current Battle State:\n{battle_state}\n\nChoose the best action by calling the appropriate function ('choose_move' or 'choose_switch')."
335
+
336
+ try:
337
+ response = await self.openai_client.chat.completions.create(
338
+ model=self.model,
339
+ messages=[
340
+ {"role": "system", "content": system_prompt},
341
+ {"role": "user", "content": user_prompt},
342
+ ],
343
+ tools=self.openai_tools,
344
+ tool_choice="required",
345
+ temperature=0.5,
346
+ )
347
+ message = response.choices[0].message
348
+ print("OPENAI RESPONSE : ",response)
349
+ # Check for tool calls in the response
350
+ if message.tool_calls:
351
+ tool_call = message.tool_calls[0] # Get the first tool call
352
+ function_name = tool_call.function.name
353
+ try:
354
+ arguments = json.loads(tool_call.function.arguments or '{}')
355
+ if function_name in self.standard_tools:
356
+ return {"decision": {"name": function_name, "arguments": arguments}}
357
+ else:
358
+ return {"error": f"Model called unknown function '{function_name}'."}
359
+ except json.JSONDecodeError:
360
+ return {"error": f"Error decoding function arguments: {tool_call.function.arguments}"}
361
+ else:
362
+ # Model decided not to call a function
363
+ return {"error": f"OpenAI did not return a function call. Response: {message.content}"}
364
+
365
+ except APIError as e:
366
+ print(f"Error during OpenAI API call: {e}")
367
+ return {"error": f"OpenAI API Error: {e.status_code} - {e.message}"}
368
+ except Exception as e:
369
+ print(f"Unexpected error during OpenAI API call: {e}")
370
+ return {"error": f"Unexpected error: {e}"}
371
+
372
+
373
+ # --- Mistral Agent ---
374
+ class MistralAgent(LLMAgentBase):
375
+ """Uses Mistral AI API for decisions."""
376
+ def __init__(self, api_key: str = None, model: str = "mistral-large-latest", avatar: str = "alder", *args, **kwargs):
377
+ # Set avatar before calling parent constructor
378
+ kwargs['avatar'] = avatar
379
+ super().__init__(*args, **kwargs)
380
+ self.model = model
381
+ used_api_key = api_key or os.environ.get("MISTRAL_API_KEY")
382
+ if not used_api_key:
383
+ raise ValueError("Mistral API key not provided or found in MISTRAL_API_KEY env var.")
384
+ self.mistral_client = Mistral(api_key=used_api_key)
385
+
386
+ # Convert standard schema to Mistral's tool format with "function" wrapper
387
+ self.mistral_tools = []
388
+ for tool_name, tool_schema in self.standard_tools.items():
389
+ self.mistral_tools.append({
390
+ "type": "function",
391
+ "function": {
392
+ "name": tool_schema["name"],
393
+ "description": tool_schema["description"],
394
+ "parameters": tool_schema["parameters"]
395
+ }
396
+ })
397
+
398
+ async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
399
+ system_prompt = (
400
+ "You are a skilled Pokemon battle AI. Your goal is to win the battle. "
401
+ "Based on the current battle state, decide the best action: either use an available move or switch to an available Pokémon. "
402
+ "Consider type matchups, HP, status conditions, field effects, entry hazards, and potential opponent actions. "
403
+ "Only choose actions listed as available using their exact ID (for moves) or species name (for switches). "
404
+ "Use the provided tools to indicate your choice."
405
+ )
406
+ user_prompt = f"Current Battle State:\n{battle_state}\n\nChoose the best action by calling the appropriate function ('choose_move' or 'choose_switch')."
407
+
408
+ try:
409
+ # Create the messages array
410
+ messages = [
411
+ {"role": "system", "content": system_prompt},
412
+ {"role": "user", "content": user_prompt}
413
+ ]
414
+
415
+ # Call the Mistral API with tool_choice set to "any" to force tool usage
416
+ response = self.mistral_client.chat.complete(
417
+ model=self.model,
418
+ messages=messages,
419
+ tools=self.mistral_tools,
420
+ tool_choice="any", # Force the model to use a tool
421
+ temperature=0.3,
422
+ )
423
+ print("Mistral RESPONSE : ", response)
424
+
425
+ # Check for tool calls in the response
426
+ message = response.choices[0].message
427
+ if hasattr(message, 'tool_calls') and message.tool_calls:
428
+ tool_call = message.tool_calls[0] # Get the first tool call
429
+ function_name = tool_call.function.name
430
+ try:
431
+ # Parse the function arguments from JSON string
432
+ arguments = json.loads(tool_call.function.arguments or '{}')
433
+ if function_name in self.standard_tools:
434
+ return {"decision": {"name": function_name, "arguments": arguments}}
435
+ else:
436
+ return {"error": f"Model called unknown function '{function_name}'."}
437
+ except json.JSONDecodeError:
438
+ return {"error": f"Error decoding function arguments: {tool_call.function.arguments}"}
439
+ else:
440
+ # Model did not return a tool call
441
+ return {"error": f"Mistral did not return a tool call. Response: {message.content}"}
442
+
443
+ except Exception as e:
444
+ print(f"Error during Mistral API call: {e}")
445
+ import traceback
446
+ traceback.print_exc()
447
+ return {"error": f"Unexpected error: {str(e)}"}
448
+