import{s as pM,n as IM,o as CM}from"../chunks/scheduler.37c15a92.js";import{S as oM,i as AM,g as j,s as t,r as U,A as iM,h as J,f as s,c as n,j as yM,u as y,x as e,k as TM,y as cM,a,v as T,d as w,t as p,w as I}from"../chunks/index.2bf4358c.js";import{C as wM}from"../chunks/CodeBlock.4e987730.js";import{H as D}from"../chunks/Heading.8ada512a.js";import{E as mM}from"../chunks/getInferenceSnippets.0aa3753f.js";function hM(Sl){let C,ll,K,Ml,o,sl,A,Ql="现在你已经探索了智能体 AI 在游戏中的潜力和局限性，是时候亲自动手了。在本节中，你将<strong>构建自己的 AI 智能体来进行宝可梦风格的回合制战斗</strong>，使用你在整个课程中学到的一切知识。",al,i,Yl="我们将把系统分解为四个关键构建块：",tl,c,Rl="<li><p><strong>Poke-env:</strong> 一个专为训练基于规则或强化学习的宝可梦机器人而设计的 Python 库。</p></li> <li><p><strong>Pokémon Showdown:</strong> 一个在线对战模拟器，你的智能体将在这里战斗。</p></li> <li><p><strong>LLMAgentBase:</strong> 我们构建的一个自定义 Python 类，用于将你的 LLM 与 Poke-env 战斗环境连接。</p></li> <li><p><strong>TemplateAgent:</strong> 一个起始模板，你将完善它来创建自己独特的战斗智能体。</p></li>",nl,m,Xl="让我们详细探索这些组件。",jl,h,Jl,b,Fl='<img src="https://github.com/hsahovic/poke-env/raw/master/rl-gif.gif" alt="Battle gif"/>',el,u,xl=`<a href="https://github.com/hsahovic/poke-env" rel="nofollow">Poke-env</a>是一个 Python 接口，最初由<a href="https://huggingface.co/hsahovic" rel="nofollow">Haris Sahovic</a>构建用于训练强化学习机器人，但我们已将其重新用于智能体 AI。
它允许你的智能体通过简单的 API 与 Pokémon Showdown 交互。`,Ul,d,zl="它提供了一个<code>Player</code>类，你的智能体将继承该类，涵盖与图形界面通信所需的一切。",yl,r,ql='<strong>文档</strong>: <a href="https://poke-env.readthedocs.io/en/stable/" rel="nofollow">poke-env.readthedocs.io</a><br/> <strong>代码库</strong>: <a href="https://github.com/hsahovic/poke-env" rel="nofollow">github.com/hsahovic/poke-env</a>',Tl,f,wl,_,Hl=`<a href="https://pokemonshowdown.com/" rel="nofollow">Pokémon Showdown</a>是一个<a href="https://github.com/smogon/Pokemon-Showdown" rel="nofollow">开源</a>战斗模拟器，你的智能体将在这里进行实时宝可梦战斗。
它提供了一个完整的界面来实时模拟和显示战斗。在我们的挑战中，你的机器人将像人类玩家一样行动，逐回合选择招式。`,pl,B,Ll="我们已经部署了一个所有参与者都将使用的服务器来进行战斗。让我们看看谁能构建出最好的 AI 战斗智能体！",Il,Z,$l='<strong>代码库</strong>: <a href="https://github.com/smogon/Pokemon-Showdown" rel="nofollow">github.com/smogon/Pokemon-Showdown</a><br/> <strong>网站</strong>: <a href="https://pokemonshowdown.com/" rel="nofollow">pokemonshowdown.com</a>',Cl,G,ol,v,Pl=`<code>LLMAgentBase</code>是一个扩展了<strong>Poke-env</strong>中<code>Player</code>类的 Python 类。
它作为你的<strong>LLM</strong>和<strong>宝可梦战斗模拟器</strong>之间的桥梁，处理输入/输出格式化并维护战斗上下文。`,Al,N,Dl="这个基础智能体提供了一组工具（定义在<code>STANDARD_TOOL_SCHEMA</code>中）来与环境交互，包括：",il,k,Kl="<li><code>choose_move</code>: 用于在战斗中选择攻击</li> <li><code>choose_switch</code>: 用于切换宝可梦</li>",cl,E,Ol="LLM 应该使用这些工具在比赛中做出决策。",ml,V,hl,W,lM="<li><code>choose_move(battle: Battle)</code>: 这是每回合调用的主要方法。它接收一个<code>Battle</code>对象并基于 LLM 的输出返回一个动作字符串。</li>",bl,g,ul,S,MM="<li><p><code>_format_battle_state(battle)</code>: 将当前战斗状态转换为字符串，使其适合发送给 LLM。</p></li> <li><p><code>_find_move_by_name(battle, move_name)</code>: 按名称查找招式，用于 LLM 响应中调用<code>choose_move</code>。</p></li> <li><p><code>_find_pokemon_by_name(battle, pokemon_name)</code>: 根据 LLM 的切换命令定位要切换到的特定宝可梦。</p></li> <li><p><code>_get_llm_decision(battle_state)</code>: 这个方法在基类中是抽象的。你需要在自己的智能体中实现它（见下一节），在那里你定义如何查询 LLM 并解析其响应。</p></li>",dl,Q,sM="这里是显示决策如何工作的摘录：",rl,Y,fl,R,aM='<strong>完整源代码</strong>: <a href="https://huggingface.co/spaces/Jofthomas/twitch_streaming/blob/main/agents.py" rel="nofollow">agents.py</a>',_l,X,Bl,F,tM="现在到了有趣的部分！以 LLMAgentBase 作为你的基础，是时候实现你自己的智能体了，用你自己的策略登上排行榜。",Zl,x,nM='你将从这个模板开始并构建自己的逻辑。我们还提供了三个使用<strong>OpenAI</strong>、<strong>Mistral</strong>和<strong>Gemini</strong>模型的<a href="https://huggingface.co/spaces/Jofthomas/twitch_streaming/blob/main/agents.py" rel="nofollow">完整示例</a>来指导你。',Gl,z,jM="这是模板的简化版本：",vl,q,Nl,H,JM="这段代码无法直接运行，它是你自定义逻辑的蓝图。",kl,L,eM="有了所有准备好的组件，现在轮到你构建一个有竞争力的智能体了。在下一节中，我们将展示如何将你的智能体部署到我们的服务器并与其他智能体实时对战。",El,$,UM="让战斗开始吧！🔥",Vl,P,Wl,O,gl;return o=new D({props:{title:"构建你自己的宝可梦对战智能体",local:"构建你自己的宝可梦对战智能体",headingTag:"h1"}}),h=new D({props:{title:"🧠 Poke-env",local:"-poke-env",headingTag:"h2"}}),f=new D({props:{title:"⚔️ Pokémon Showdown",local:"-pokémon-showdown",headingTag:"h2"}}),G=new D({props:{title:"🔌 LLMAgentBase",local:"-llmagentbase",headingTag:"h2"}}),V=new D({props:{title:"🧠 核心逻辑",local:"-核心逻辑",headingTag:"h3"}}),g=new D({props:{title:"🔧 关键内部方法",local:"-关键内部方法",headingTag:"h3"}}),Y=new wM({props:{code:" ",highlighted:`STANDARD_TOOL_SCHEMA = {
    <span class="hljs-string">&quot;choose_move&quot;</span>: {
        ...
    },
    <span class="hljs-string">&quot;choose_switch&quot;</span>: {
        ...
    },
}

<span class="hljs-keyword">class</span> <span class="hljs-title class_">LLMAgentBase</span>(<span class="hljs-title class_ inherited__">Player</span>):
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, *args, **kwargs</span>):
        <span class="hljs-built_in">super</span>().__init__(*args, **kwargs)
        self.standard_tools = STANDARD_TOOL_SCHEMA
        self.battle_history = []

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_format_battle_state</span>(<span class="hljs-params">self, battle: Battle</span>) -&gt; <span class="hljs-built_in">str</span>:
        active_pkmn = battle.active_pokemon
        active_pkmn_info = <span class="hljs-string">f&quot;Your active Pokemon: <span class="hljs-subst">{active_pkmn.species}</span> &quot;</span> \\
                           <span class="hljs-string">f&quot;(Type: <span class="hljs-subst">{<span class="hljs-string">&#x27;/&#x27;</span>.join(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">str</span>, active_pkmn.types))}</span>) &quot;</span> \\
                           <span class="hljs-string">f&quot;HP: <span class="hljs-subst">{active_pkmn.current_hp_fraction * <span class="hljs-number">100</span>:<span class="hljs-number">.1</span>f}</span>% &quot;</span> \\
                           <span class="hljs-string">f&quot;Status: <span class="hljs-subst">{active_pkmn.status.name <span class="hljs-keyword">if</span> active_pkmn.status <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;None&#x27;</span>}</span> &quot;</span> \\
                           <span class="hljs-string">f&quot;Boosts: <span class="hljs-subst">{active_pkmn.boosts}</span>&quot;</span>

        opponent_pkmn = battle.opponent_active_pokemon
        opp_info_str = <span class="hljs-string">&quot;Unknown&quot;</span>
        <span class="hljs-keyword">if</span> opponent_pkmn:
            opp_info_str = <span class="hljs-string">f&quot;<span class="hljs-subst">{opponent_pkmn.species}</span> &quot;</span> \\
                           <span class="hljs-string">f&quot;(Type: <span class="hljs-subst">{<span class="hljs-string">&#x27;/&#x27;</span>.join(<span class="hljs-built_in">map</span>(<span class="hljs-built_in">str</span>, opponent_pkmn.types))}</span>) &quot;</span> \\
                           <span class="hljs-string">f&quot;HP: <span class="hljs-subst">{opponent_pkmn.current_hp_fraction * <span class="hljs-number">100</span>:<span class="hljs-number">.1</span>f}</span>% &quot;</span> \\
                           <span class="hljs-string">f&quot;Status: <span class="hljs-subst">{opponent_pkmn.status.name <span class="hljs-keyword">if</span> opponent_pkmn.status <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;None&#x27;</span>}</span> &quot;</span> \\
                           <span class="hljs-string">f&quot;Boosts: <span class="hljs-subst">{opponent_pkmn.boosts}</span>&quot;</span>
        opponent_pkmn_info = <span class="hljs-string">f&quot;Opponent&#x27;s active Pokemon: <span class="hljs-subst">{opp_info_str}</span>&quot;</span>

        available_moves_info = <span class="hljs-string">&quot;Available moves:\\n&quot;</span>
        <span class="hljs-keyword">if</span> battle.available_moves:
            available_moves_info += <span class="hljs-string">&quot;\\n&quot;</span>.join(
                [<span class="hljs-string">f&quot;- <span class="hljs-subst">{move.<span class="hljs-built_in">id</span>}</span> (Type: <span class="hljs-subst">{move.<span class="hljs-built_in">type</span>}</span>, BP: <span class="hljs-subst">{move.base_power}</span>, Acc: <span class="hljs-subst">{move.accuracy}</span>, PP: <span class="hljs-subst">{move.current_pp}</span>/<span class="hljs-subst">{move.max_pp}</span>, Cat: <span class="hljs-subst">{move.category.name}</span>)&quot;</span>
                 <span class="hljs-keyword">for</span> move <span class="hljs-keyword">in</span> battle.available_moves]
            )
        <span class="hljs-keyword">else</span>:
             available_moves_info += <span class="hljs-string">&quot;- None (Must switch or Struggle)&quot;</span>

        available_switches_info = <span class="hljs-string">&quot;Available switches:\\n&quot;</span>
        <span class="hljs-keyword">if</span> battle.available_switches:
              available_switches_info += <span class="hljs-string">&quot;\\n&quot;</span>.join(
                  [<span class="hljs-string">f&quot;- <span class="hljs-subst">{pkmn.species}</span> (HP: <span class="hljs-subst">{pkmn.current_hp_fraction * <span class="hljs-number">100</span>:<span class="hljs-number">.1</span>f}</span>%, Status: <span class="hljs-subst">{pkmn.status.name <span class="hljs-keyword">if</span> pkmn.status <span class="hljs-keyword">else</span> <span class="hljs-string">&#x27;None&#x27;</span>}</span>)&quot;</span>
                   <span class="hljs-keyword">for</span> pkmn <span class="hljs-keyword">in</span> battle.available_switches]
              )
        <span class="hljs-keyword">else</span>:
            available_switches_info += <span class="hljs-string">&quot;- None&quot;</span>

        state_str = <span class="hljs-string">f&quot;<span class="hljs-subst">{active_pkmn_info}</span>\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;<span class="hljs-subst">{opponent_pkmn_info}</span>\\n\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;<span class="hljs-subst">{available_moves_info}</span>\\n\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;<span class="hljs-subst">{available_switches_info}</span>\\n\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;Weather: <span class="hljs-subst">{battle.weather}</span>\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;Terrains: <span class="hljs-subst">{battle.fields}</span>\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;Your Side Conditions: <span class="hljs-subst">{battle.side_conditions}</span>\\n&quot;</span> \\
                    <span class="hljs-string">f&quot;Opponent Side Conditions: <span class="hljs-subst">{battle.opponent_side_conditions}</span>&quot;</span>
        <span class="hljs-keyword">return</span> state_str.strip()

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_find_move_by_name</span>(<span class="hljs-params">self, battle: Battle, move_name: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-type">Optional</span>[Move]:
        normalized_name = normalize_name(move_name)
        <span class="hljs-comment"># 优先精确ID匹配</span>
        <span class="hljs-keyword">for</span> move <span class="hljs-keyword">in</span> battle.available_moves:
            <span class="hljs-keyword">if</span> move.<span class="hljs-built_in">id</span> == normalized_name:
                <span class="hljs-keyword">return</span> move
        <span class="hljs-comment"># 后备方案: 检查显示名称（不太可靠）</span>
        <span class="hljs-keyword">for</span> move <span class="hljs-keyword">in</span> battle.available_moves:
            <span class="hljs-keyword">if</span> move.name.lower() == move_name.lower():
                <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Warning: Matched move by display name &#x27;<span class="hljs-subst">{move.name}</span>&#x27; instead of ID &#x27;<span class="hljs-subst">{move.<span class="hljs-built_in">id</span>}</span>&#x27;. Input was &#x27;<span class="hljs-subst">{move_name}</span>&#x27;.&quot;</span>)
                <span class="hljs-keyword">return</span> move
        <span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>

    <span class="hljs-keyword">def</span> <span class="hljs-title function_">_find_pokemon_by_name</span>(<span class="hljs-params">self, battle: Battle, pokemon_name: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-type">Optional</span>[Pokemon]:
        normalized_name = normalize_name(pokemon_name)
        <span class="hljs-keyword">for</span> pkmn <span class="hljs-keyword">in</span> battle.available_switches:
            <span class="hljs-comment"># 规范化种类名称用于比较</span>
            <span class="hljs-keyword">if</span> normalize_name(pkmn.species) == normalized_name:
                <span class="hljs-keyword">return</span> pkmn
        <span class="hljs-keyword">return</span> <span class="hljs-literal">None</span>

    <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">choose_move</span>(<span class="hljs-params">self, battle: Battle</span>) -&gt; <span class="hljs-built_in">str</span>:
        battle_state_str = self._format_battle_state(battle)
        decision_result = <span class="hljs-keyword">await</span> self._get_llm_decision(battle_state_str)
        <span class="hljs-built_in">print</span>(decision_result)
        decision = decision_result.get(<span class="hljs-string">&quot;decision&quot;</span>)
        error_message = decision_result.get(<span class="hljs-string">&quot;error&quot;</span>)
        action_taken = <span class="hljs-literal">False</span>
        fallback_reason = <span class="hljs-string">&quot;&quot;</span>

        <span class="hljs-keyword">if</span> decision:
            function_name = decision.get(<span class="hljs-string">&quot;name&quot;</span>)
            args = decision.get(<span class="hljs-string">&quot;arguments&quot;</span>, {})
            <span class="hljs-keyword">if</span> function_name == <span class="hljs-string">&quot;choose_move&quot;</span>:
                move_name = args.get(<span class="hljs-string">&quot;move_name&quot;</span>)
                <span class="hljs-keyword">if</span> move_name:
                    chosen_move = self._find_move_by_name(battle, move_name)
                    <span class="hljs-keyword">if</span> chosen_move <span class="hljs-keyword">and</span> chosen_move <span class="hljs-keyword">in</span> battle.available_moves:
                        action_taken = <span class="hljs-literal">True</span>
                        chat_msg = <span class="hljs-string">f&quot;AI Decision: Using move &#x27;<span class="hljs-subst">{chosen_move.<span class="hljs-built_in">id</span>}</span>&#x27;.&quot;</span>
                        <span class="hljs-built_in">print</span>(chat_msg)
                        <span class="hljs-keyword">return</span> self.create_order(chosen_move)
                    <span class="hljs-keyword">else</span>:
                        fallback_reason = <span class="hljs-string">f&quot;LLM chose unavailable/invalid move &#x27;<span class="hljs-subst">{move_name}</span>&#x27;.&quot;</span>
                <span class="hljs-keyword">else</span>:
                     fallback_reason = <span class="hljs-string">&quot;LLM &#x27;choose_move&#x27; called without &#x27;move_name&#x27;.&quot;</span>
            <span class="hljs-keyword">elif</span> function_name == <span class="hljs-string">&quot;choose_switch&quot;</span>:
                pokemon_name = args.get(<span class="hljs-string">&quot;pokemon_name&quot;</span>)
                <span class="hljs-keyword">if</span> pokemon_name:
                    chosen_switch = self._find_pokemon_by_name(battle, pokemon_name)
                    <span class="hljs-keyword">if</span> chosen_switch <span class="hljs-keyword">and</span> chosen_switch <span class="hljs-keyword">in</span> battle.available_switches:
                        action_taken = <span class="hljs-literal">True</span>
                        chat_msg = <span class="hljs-string">f&quot;AI Decision: Switching to &#x27;<span class="hljs-subst">{chosen_switch.species}</span>&#x27;.&quot;</span>
                        <span class="hljs-built_in">print</span>(chat_msg)
                        <span class="hljs-keyword">return</span> self.create_order(chosen_switch)
                    <span class="hljs-keyword">else</span>:
                        fallback_reason = <span class="hljs-string">f&quot;LLM chose unavailable/invalid switch &#x27;<span class="hljs-subst">{pokemon_name}</span>&#x27;.&quot;</span>
                <span class="hljs-keyword">else</span>:
                    fallback_reason = <span class="hljs-string">&quot;LLM &#x27;choose_switch&#x27; called without &#x27;pokemon_name&#x27;.&quot;</span>
            <span class="hljs-keyword">else</span>:
                fallback_reason = <span class="hljs-string">f&quot;LLM called unknown function &#x27;<span class="hljs-subst">{function_name}</span>&#x27;.&quot;</span>

        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> action_taken:
            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> fallback_reason:
                 <span class="hljs-keyword">if</span> error_message:
                     fallback_reason = <span class="hljs-string">f&quot;API Error: <span class="hljs-subst">{error_message}</span>&quot;</span>
                 <span class="hljs-keyword">elif</span> decision <span class="hljs-keyword">is</span> <span class="hljs-literal">None</span>:
                      fallback_reason = <span class="hljs-string">&quot;LLM did not provide a valid function call.&quot;</span>
                 <span class="hljs-keyword">else</span>:
                      fallback_reason = <span class="hljs-string">&quot;Unknown error processing LLM decision.&quot;</span>

            <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Warning: <span class="hljs-subst">{fallback_reason}</span> Choosing random action.&quot;</span>)

            <span class="hljs-keyword">if</span> battle.available_moves <span class="hljs-keyword">or</span> battle.available_switches:
                 <span class="hljs-keyword">return</span> self.choose_random_move(battle)
            <span class="hljs-keyword">else</span>:
                 <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;AI Fallback: No moves or switches available. Using Struggle/Default.&quot;</span>)
                 <span class="hljs-keyword">return</span> self.choose_default_move(battle)

    <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">_get_llm_decision</span>(<span class="hljs-params">self, battle_state: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-type">Dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>]:
        <span class="hljs-keyword">raise</span> NotImplementedError(<span class="hljs-string">&quot;Subclasses must implement _get_llm_decision&quot;</span>)`,wrap:!1}}),X=new D({props:{title:"🧪 TemplateAgent",local:"-templateagent",headingTag:"h2"}}),q=new wM({props:{code:"Y2xhc3MlMjBUZW1wbGF0ZUFnZW50KExMTUFnZW50QmFzZSklM0ElMEElMjAlMjAlMjAlMjAlMjIlMjIlMjIlRTQlQkQlQkYlRTclOTQlQTglRTYlQTglQTElRTYlOUQlQkZBSSUyMEF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yMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBtZXNzYWdlJTIwJTNEJTIwcmVzcG9uc2UuY2hvaWNlcyU1QjAlNUQubWVzc2FnZSUwQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJldHVybiUyMCU3QiUyMmRlY2lzaW9uJTIyJTNBJTIwJTdCJTIybmFtZSUyMiUzQSUyMGZ1bmN0aW9uX25hbWUlMkMlMjAlMjJhcmd1bWVudHMlMjIlM0ElMjBhcmd1bWVudHMlN0QlN0QlMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBleGNlcHQlMjBFeGNlcHRpb24lMjBhcyUyMGUlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBwcmludChmJTIyVW5leHBlY3RlZCUyMGVycm9yJTIwZHVyaW5nJTIwY2FsbCUzQSUyMCU3QmUlN0QlMjIpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwJTdCJTIyZXJyb3IlMjIlM0ElMjBmJTIyVW5leHBlY3RlZCUyMGVycm9yJTNBJTIwJTdCZSU3RCUyMiU3RA==",highlighted:`<span class="hljs-keyword">class</span> <span class="hljs-title class_">TemplateAgent</span>(<span class="hljs-title class_ inherited__">LLMAgentBase</span>):
    <span class="hljs-string">&quot;&quot;&quot;使用模板AI API进行决策。&quot;&quot;&quot;</span>
    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, api_key: <span class="hljs-built_in">str</span> = <span class="hljs-literal">None</span>, model: <span class="hljs-built_in">str</span> = <span class="hljs-string">&quot;model-name&quot;</span>, *args, **kwargs</span>):
        <span class="hljs-built_in">super</span>().__init__(*args, **kwargs)
        self.model = model
        self.template_client = TemplateModelProvider(api_key=...)
        self.template_tools = <span class="hljs-built_in">list</span>(self.standard_tools.values())

    <span class="hljs-keyword">async</span> <span class="hljs-keyword">def</span> <span class="hljs-title function_">_get_llm_decision</span>(<span class="hljs-params">self, battle_state: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-type">Dict</span>[<span class="hljs-built_in">str</span>, <span class="hljs-type">Any</span>]:
        <span class="hljs-string">&quot;&quot;&quot;Sends state to the LLM and gets back the function call decision.&quot;&quot;&quot;</span>
        system_prompt = (
            <span class="hljs-string">&quot;You are a ...&quot;</span>
        )
        user_prompt = <span class="hljs-string">f&quot;...&quot;</span>

        <span class="hljs-keyword">try</span>:
            response = <span class="hljs-keyword">await</span> self.template_client.chat.completions.create(
                model=self.model,
                messages=[
                    {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: system_prompt},
                    {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: user_prompt},
                ],
            )
            message = response.choices[<span class="hljs-number">0</span>].message

            <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;decision&quot;</span>: {<span class="hljs-string">&quot;name&quot;</span>: function_name, <span class="hljs-string">&quot;arguments&quot;</span>: arguments}}

        <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
            <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Unexpected error during call: <span class="hljs-subst">{e}</span>&quot;</span>)
            <span class="hljs-keyword">return</span> {<span class="hljs-string">&quot;error&quot;</span>: <span class="hljs-string">f&quot;Unexpected error: <span class="hljs-subst">{e}</span>&quot;</span>}`,wrap:!1}}),P=new mM({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/zh-CN/bonus-unit3/building_your_pokemon_agent.mdx"}}),{c(){C=j("meta"),ll=t(),K=j("p"),Ml=t(),U(o.$$.fragment),sl=t(),A=j("p"),A.innerHTML=Ql,al=t(),i=j("p"),i.textContent=Yl,tl=t(),c=j("ul"),c.innerHTML=Rl,nl=t(),m=j("p"),m.textContent=Xl,jl=t(),U(h.$$.fragment),Jl=t(),b=j("p"),b.innerHTML=Fl,el=t(),u=j("p"),u.innerHTML=xl,Ul=t(),d=j("p"),d.innerHTML=zl,yl=t(),r=j("p"),r.innerHTML=ql,Tl=t(),U(f.$$.fragment),wl=t(),_=j("p"),_.innerHTML=Hl,pl=t(),B=j("p"),B.textContent=Ll,Il=t(),Z=j("p"),Z.innerHTML=$l,Cl=t(),U(G.$$.fragment),ol=t(),v=j("p"),v.innerHTML=Pl,Al=t(),N=j("p"),N.innerHTML=Dl,il=t(),k=j("ul"),k.innerHTML=Kl,cl=t(),E=j("p"),E.textContent=Ol,ml=t(),U(V.$$.fragment),hl=t(),W=j("ul"),W.innerHTML=lM,bl=t(),U(g.$$.fragment),ul=t(),S=j("ul"),S.innerHTML=MM,dl=t(),Q=j("p"),Q.textContent=sM,rl=t(),U(Y.$$.fragment),fl=t(),R=j("p"),R.innerHTML=aM,_l=t(),U(X.$$.fragment),Bl=t(),F=j("p"),F.textContent=tM,Zl=t(),x=j("p"),x.innerHTML=nM,Gl=t(),z=j("p"),z.textContent=jM,vl=t(),U(q.$$.fragment),Nl=t(),H=j("p"),H.textContent=JM,kl=t(),L=j("p"),L.textContent=eM,El=t(),$=j("p"),$.textContent=UM,Vl=t(),U(P.$$.fragment),Wl=t(),O=j("p"),this.h()},l(l){const M=iM("svelte-u9bgzb",document.head);C=J(M,"META",{name:!0,content:!0}),M.forEach(s),ll=n(l),K=J(l,"P",{}),yM(K).forEach(s),Ml=n(l),y(o.$$.fragment,l),sl=n(l),A=J(l,"P",{"data-svelte-h":!0}),e(A)!=="svelte-l0mq9j"&&(A.innerHTML=Ql),al=n(l),i=J(l,"P",{"data-svelte-h":!0}),e(i)!=="svelte-y4aiyl"&&(i.textContent=Yl),tl=n(l),c=J(l,"UL",{"data-svelte-h":!0}),e(c)!=="svelte-1otq18s"&&(c.innerHTML=Rl),nl=n(l),m=J(l,"P",{"data-svelte-h":!0}),e(m)!=="svelte-1wyqkmy"&&(m.textContent=Xl),jl=n(l),y(h.$$.fragment,l),Jl=n(l),b=J(l,"P",{"data-svelte-h":!0}),e(b)!=="svelte-a0rkmd"&&(b.innerHTML=Fl),el=n(l),u=J(l,"P",{"data-svelte-h":!0}),e(u)!=="svelte-1i87hr2"&&(u.innerHTML=xl),Ul=n(l),d=J(l,"P",{"data-svelte-h":!0}),e(d)!=="svelte-lnegje"&&(d.innerHTML=zl),yl=n(l),r=J(l,"P",{"data-svelte-h":!0}),e(r)!=="svelte-gviwvg"&&(r.innerHTML=ql),Tl=n(l),y(f.$$.fragment,l),wl=n(l),_=J(l,"P",{"data-svelte-h":!0}),e(_)!=="svelte-15devh2"&&(_.innerHTML=Hl),pl=n(l),B=J(l,"P",{"data-svelte-h":!0}),e(B)!=="svelte-1eaomxa"&&(B.textContent=Ll),Il=n(l),Z=J(l,"P",{"data-svelte-h":!0}),e(Z)!=="svelte-wybkpw"&&(Z.innerHTML=$l),Cl=n(l),y(G.$$.fragment,l),ol=n(l),v=J(l,"P",{"data-svelte-h":!0}),e(v)!=="svelte-s4mnxu"&&(v.innerHTML=Pl),Al=n(l),N=J(l,"P",{"data-svelte-h":!0}),e(N)!=="svelte-89svx0"&&(N.innerHTML=Dl),il=n(l),k=J(l,"UL",{"data-svelte-h":!0}),e(k)!=="svelte-197aei"&&(k.innerHTML=Kl),cl=n(l),E=J(l,"P",{"data-svelte-h":!0}),e(E)!=="svelte-dysi3q"&&(E.textContent=Ol),ml=n(l),y(V.$$.fragment,l),hl=n(l),W=J(l,"UL",{"data-svelte-h":!0}),e(W)!=="svelte-1vu5084"&&(W.innerHTML=lM),bl=n(l),y(g.$$.fragment,l),ul=n(l),S=J(l,"UL",{"data-svelte-h":!0}),e(S)!=="svelte-x33l30"&&(S.innerHTML=MM),dl=n(l),Q=J(l,"P",{"data-svelte-h":!0}),e(Q)!=="svelte-132bp0c"&&(Q.textContent=sM),rl=n(l),y(Y.$$.fragment,l),fl=n(l),R=J(l,"P",{"data-svelte-h":!0}),e(R)!=="svelte-1kzh90t"&&(R.innerHTML=aM),_l=n(l),y(X.$$.fragment,l),Bl=n(l),F=J(l,"P",{"data-svelte-h":!0}),e(F)!=="svelte-1iw4u03"&&(F.textContent=tM),Zl=n(l),x=J(l,"P",{"data-svelte-h":!0}),e(x)!=="svelte-1p73p4a"&&(x.innerHTML=nM),Gl=n(l),z=J(l,"P",{"data-svelte-h":!0}),e(z)!=="svelte-hbsb8k"&&(z.textContent=jM),vl=n(l),y(q.$$.fragment,l),Nl=n(l),H=J(l,"P",{"data-svelte-h":!0}),e(H)!=="svelte-5c9mfo"&&(H.textContent=JM),kl=n(l),L=J(l,"P",{"data-svelte-h":!0}),e(L)!=="svelte-1y8h4oi"&&(L.textContent=eM),El=n(l),$=J(l,"P",{"data-svelte-h":!0}),e($)!=="svelte-5c4fpl"&&($.textContent=UM),Vl=n(l),y(P.$$.fragment,l),Wl=n(l),O=J(l,"P",{}),yM(O).forEach(s),this.h()},h(){TM(C,"name","hf:doc:metadata"),TM(C,"content",bM)},m(l,M){cM(document.head,C),a(l,ll,M),a(l,K,M),a(l,Ml,M),T(o,l,M),a(l,sl,M),a(l,A,M),a(l,al,M),a(l,i,M),a(l,tl,M),a(l,c,M),a(l,nl,M),a(l,m,M),a(l,jl,M),T(h,l,M),a(l,Jl,M),a(l,b,M),a(l,el,M),a(l,u,M),a(l,Ul,M),a(l,d,M),a(l,yl,M),a(l,r,M),a(l,Tl,M),T(f,l,M),a(l,wl,M),a(l,_,M),a(l,pl,M),a(l,B,M),a(l,Il,M),a(l,Z,M),a(l,Cl,M),T(G,l,M),a(l,ol,M),a(l,v,M),a(l,Al,M),a(l,N,M),a(l,il,M),a(l,k,M),a(l,cl,M),a(l,E,M),a(l,ml,M),T(V,l,M),a(l,hl,M),a(l,W,M),a(l,bl,M),T(g,l,M),a(l,ul,M),a(l,S,M),a(l,dl,M),a(l,Q,M),a(l,rl,M),T(Y,l,M),a(l,fl,M),a(l,R,M),a(l,_l,M),T(X,l,M),a(l,Bl,M),a(l,F,M),a(l,Zl,M),a(l,x,M),a(l,Gl,M),a(l,z,M),a(l,vl,M),T(q,l,M),a(l,Nl,M),a(l,H,M),a(l,kl,M),a(l,L,M),a(l,El,M),a(l,$,M),a(l,Vl,M),T(P,l,M),a(l,Wl,M),a(l,O,M),gl=!0},p:IM,i(l){gl||(w(o.$$.fragment,l),w(h.$$.fragment,l),w(f.$$.fragment,l),w(G.$$.fragment,l),w(V.$$.fragment,l),w(g.$$.fragment,l),w(Y.$$.fragment,l),w(X.$$.fragment,l),w(q.$$.fragment,l),w(P.$$.fragment,l),gl=!0)},o(l){p(o.$$.fragment,l),p(h.$$.fragment,l),p(f.$$.fragment,l),p(G.$$.fragment,l),p(V.$$.fragment,l),p(g.$$.fragment,l),p(Y.$$.fragment,l),p(X.$$.fragment,l),p(q.$$.fragment,l),p(P.$$.fragment,l),gl=!1},d(l){l&&(s(ll),s(K),s(Ml),s(sl),s(A),s(al),s(i),s(tl),s(c),s(nl),s(m),s(jl),s(Jl),s(b),s(el),s(u),s(Ul),s(d),s(yl),s(r),s(Tl),s(wl),s(_),s(pl),s(B),s(Il),s(Z),s(Cl),s(ol),s(v),s(Al),s(N),s(il),s(k),s(cl),s(E),s(ml),s(hl),s(W),s(bl),s(ul),s(S),s(dl),s(Q),s(rl),s(fl),s(R),s(_l),s(Bl),s(F),s(Zl),s(x),s(Gl),s(z),s(vl),s(Nl),s(H),s(kl),s(L),s(El),s($),s(Vl),s(Wl),s(O)),s(C),I(o,l),I(h,l),I(f,l),I(G,l),I(V,l),I(g,l),I(Y,l),I(X,l),I(q,l),I(P,l)}}}const bM='{"title":"构建你自己的宝可梦对战智能体","local":"构建你自己的宝可梦对战智能体","sections":[{"title":"🧠 Poke-env","local":"-poke-env","sections":[],"depth":2},{"title":"⚔️ Pokémon Showdown","local":"-pokémon-showdown","sections":[],"depth":2},{"title":"🔌 LLMAgentBase","local":"-llmagentbase","sections":[{"title":"🧠 核心逻辑","local":"-核心逻辑","sections":[],"depth":3},{"title":"🔧 关键内部方法","local":"-关键内部方法","sections":[],"depth":3}],"depth":2},{"title":"🧪 TemplateAgent","local":"-templateagent","sections":[],"depth":2}],"depth":1}';function uM(Sl){return CM(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ZM extends oM{constructor(C){super(),AM(this,C,uM,hM,pM,{})}}export{ZM as component};
