智能体课程文档

构建你自己的宝可梦对战智能体

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

构建你自己的宝可梦对战智能体

既然你已经探索了代理AI在游戏中的潜力和局限性,现在是时候动手实践了。在本节中,你将运用本课程所学的一切,构建你自己的AI智能体,以进行宝可梦式的回合制战斗

我们将系统分解为四个关键构建块:

  • Poke-env: 一个旨在训练基于规则或强化学习宝可梦机器人的Python库。

  • Pokémon Showdown: 一个在线对战模拟器,你的智能体将在其中进行战斗。

  • LLMAgentBase: 我们构建的一个自定义Python类,用于将你的LLM连接到Poke-env对战环境。

  • TemplateAgent: 一个你将完成的起始模板,用于创建你自己独特的对战智能体。

让我们更详细地探讨这些组件。

🧠 Poke-env

Battle gif

Poke-env 是一个Python接口,最初由 Haris Sahovic 构建,用于训练强化学习机器人,但我们将其重新用于代理AI。
它允许你的智能体通过简单的API与Pokémon Showdown交互。

它提供了一个`Player`类,你的智能体将继承自该类,涵盖了与图形界面通信所需的一切。

文档: poke-env.readthedocs.io
仓库: github.com/hsahovic/poke-env

⚔️ Pokémon Showdown

Pokémon Showdown 是一个开源对战模拟器,你的智能体将在这里进行实时宝可梦对战。
它提供了一个完整的界面,可以实时模拟和显示战斗。在我们的挑战中,你的机器人将像人类玩家一样,逐回合选择行动。

我们已经部署了一个服务器,所有参与者都将使用该服务器进行对战。让我们看看谁能构建出最强大的AI对战智能体!

仓库: github.com/smogon/Pokemon-Showdown
网站: pokemonshowdown.com

🔌 LLMAgentBase

LLMAgentBase 是一个 Python 类,它扩展了 Poke-envPlayer 类。
它充当你的 LLM宝可梦对战模拟器 之间的桥梁,负责处理输入/输出格式化和维护战斗上下文。

这个基础智能体提供了一套工具(在 STANDARD_TOOL_SCHEMA 中定义),用于与环境交互,包括:

  • choose_move:用于在战斗中选择攻击
  • choose_switch:用于切换宝可梦

LLM 应该使用这些工具在比赛中做出决策。

🧠 核心逻辑

  • choose_move(battle: Battle): 这是每回合调用的主要方法。它接收一个 Battle 对象,并根据 LLM 的输出返回一个动作字符串。

🔧 关键内部方法

  • _format_battle_state(battle): 将当前战斗状态转换为字符串,使其适合发送给 LLM。

  • _find_move_by_name(battle, move_name): 根据名称查找招式,用于调用 choose_move 的 LLM 响应中。

  • _find_pokemon_by_name(battle, pokemon_name): 根据 LLM 的切换指令,定位要切换到的特定宝可梦。

  • _get_llm_decision(battle_state): 此方法在基类中是抽象的。你需要在你自己的代理中实现它(参见下一节),在那里你定义如何查询 LLM 并解析其响应。

这是展示决策如何工作的一个片段:

STANDARD_TOOL_SCHEMA = {
    "choose_move": {
        ...
    },
    "choose_switch": {
        ...
    },
}

class LLMAgentBase(Player):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.standard_tools = STANDARD_TOOL_SCHEMA
        self.battle_history = []

    def _format_battle_state(self, battle: Battle) -> str:
        active_pkmn = battle.active_pokemon
        active_pkmn_info = f"Your active Pokemon: {active_pkmn.species} " \
                           f"(Type: {'/'.join(map(str, active_pkmn.types))}) " \
                           f"HP: {active_pkmn.current_hp_fraction * 100:.1f}% " \
                           f"Status: {active_pkmn.status.name if active_pkmn.status else 'None'} " \
                           f"Boosts: {active_pkmn.boosts}"

        opponent_pkmn = battle.opponent_active_pokemon
        opp_info_str = "Unknown"
        if opponent_pkmn:
            opp_info_str = f"{opponent_pkmn.species} " \
                           f"(Type: {'/'.join(map(str, opponent_pkmn.types))}) " \
                           f"HP: {opponent_pkmn.current_hp_fraction * 100:.1f}% " \
                           f"Status: {opponent_pkmn.status.name if opponent_pkmn.status else 'None'} " \
                           f"Boosts: {opponent_pkmn.boosts}"
        opponent_pkmn_info = f"Opponent's active Pokemon: {opp_info_str}"

        available_moves_info = "Available moves:\n"
        if battle.available_moves:
            available_moves_info += "\n".join(
                [f"- {move.id} (Type: {move.type}, BP: {move.base_power}, Acc: {move.accuracy}, PP: {move.current_pp}/{move.max_pp}, Cat: {move.category.name})"
                 for move in battle.available_moves]
            )
        else:
             available_moves_info += "- None (Must switch or Struggle)"

        available_switches_info = "Available switches:\n"
        if battle.available_switches:
              available_switches_info += "\n".join(
                  [f"- {pkmn.species} (HP: {pkmn.current_hp_fraction * 100:.1f}%, Status: {pkmn.status.name if pkmn.status else 'None'})"
                   for pkmn in battle.available_switches]
              )
        else:
            available_switches_info += "- None"

        state_str = f"{active_pkmn_info}\n" \
                    f"{opponent_pkmn_info}\n\n" \
                    f"{available_moves_info}\n\n" \
                    f"{available_switches_info}\n\n" \
                    f"Weather: {battle.weather}\n" \
                    f"Terrains: {battle.fields}\n" \
                    f"Your Side Conditions: {battle.side_conditions}\n" \
                    f"Opponent Side Conditions: {battle.opponent_side_conditions}"
        return state_str.strip()

    def _find_move_by_name(self, battle: Battle, move_name: str) -> Optional[Move]:
        normalized_name = normalize_name(move_name)
        # Prioritize exact ID match
        for move in battle.available_moves:
            if move.id == normalized_name:
                return move
        # Fallback: Check display name (less reliable)
        for move in battle.available_moves:
            if move.name.lower() == move_name.lower():
                print(f"Warning: Matched move by display name '{move.name}' instead of ID '{move.id}'. Input was '{move_name}'.")
                return move
        return None

    def _find_pokemon_by_name(self, battle: Battle, pokemon_name: str) -> Optional[Pokemon]:
        normalized_name = normalize_name(pokemon_name)
        for pkmn in battle.available_switches:
            # Normalize the species name for comparison
            if normalize_name(pkmn.species) == normalized_name:
                return pkmn
        return None

    async def choose_move(self, battle: Battle) -> str:
        battle_state_str = self._format_battle_state(battle)
        decision_result = await self._get_llm_decision(battle_state_str)
        print(decision_result)
        decision = decision_result.get("decision")
        error_message = decision_result.get("error")
        action_taken = False
        fallback_reason = ""

        if decision:
            function_name = decision.get("name")
            args = decision.get("arguments", {})
            if function_name == "choose_move":
                move_name = args.get("move_name")
                if move_name:
                    chosen_move = self._find_move_by_name(battle, move_name)
                    if chosen_move and chosen_move in battle.available_moves:
                        action_taken = True
                        chat_msg = f"AI Decision: Using move '{chosen_move.id}'."
                        print(chat_msg)
                        return self.create_order(chosen_move)
                    else:
                        fallback_reason = f"LLM chose unavailable/invalid move '{move_name}'."
                else:
                     fallback_reason = "LLM 'choose_move' called without 'move_name'."
            elif function_name == "choose_switch":
                pokemon_name = args.get("pokemon_name")
                if pokemon_name:
                    chosen_switch = self._find_pokemon_by_name(battle, pokemon_name)
                    if chosen_switch and chosen_switch in battle.available_switches:
                        action_taken = True
                        chat_msg = f"AI Decision: Switching to '{chosen_switch.species}'."
                        print(chat_msg)
                        return self.create_order(chosen_switch)
                    else:
                        fallback_reason = f"LLM chose unavailable/invalid switch '{pokemon_name}'."
                else:
                    fallback_reason = "LLM 'choose_switch' called without 'pokemon_name'."
            else:
                fallback_reason = f"LLM called unknown function '{function_name}'."

        if not action_taken:
            if not fallback_reason:
                 if error_message:
                     fallback_reason = f"API Error: {error_message}"
                 elif decision is None:
                      fallback_reason = "LLM did not provide a valid function call."
                 else:
                      fallback_reason = "Unknown error processing LLM decision."

            print(f"Warning: {fallback_reason} Choosing random action.")

            if battle.available_moves or battle.available_switches:
                 return self.choose_random_move(battle)
            else:
                 print("AI Fallback: No moves or switches available. Using Struggle/Default.")
                 return self.choose_default_move(battle)

    async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
        raise NotImplementedError("Subclasses must implement _get_llm_decision")

完整源代码: agents.py

🧪 TemplateAgent

现在,有趣的部分来了!以 LLMAgentBase 为基础,是时候实现你自己的智能体了,用你自己的策略来攀登排行榜。

你将从这个模板开始,构建你自己的逻辑。我们还提供了三个使用 OpenAIMistralGemini 模型的完整示例来指导你。

这是一个简化版的模板:

class TemplateAgent(LLMAgentBase):
    """Uses Template AI API for decisions."""
    def __init__(self, api_key: str = None, model: str = "model-name", *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.model = model
        self.template_client = TemplateModelProvider(api_key=...)
        self.template_tools = list(self.standard_tools.values())

    async def _get_llm_decision(self, battle_state: str) -> Dict[str, Any]:
        """Sends state to the LLM and gets back the function call decision."""
        system_prompt = (
            "You are a ..."
        )
        user_prompt = f"..."

        try:
            response = await self.template_client.chat.completions.create(
                model=self.model,
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": user_prompt},
                ],
            )
            message = response.choices[0].message
            
            return {"decision": {"name": function_name, "arguments": arguments}}

        except Exception as e:
            print(f"Unexpected error during call: {e}")
            return {"error": f"Unexpected error: {e}"}

这段代码不能直接运行,它是你自定义逻辑的蓝图。

所有准备就绪后,轮到你来构建一个有竞争力的智能体了。在下一节中,我们将展示如何将你的智能体部署到我们的服务器上,并与其他智能体进行实时对战。

战斗开始!🔥

< > 在 GitHub 上更新