import{s as Jl,o as Ml,n as Gt}from"../chunks/scheduler.37c15a92.js";import{S as il,i as pl,g as i,s as n,r as c,A as ol,h as p,f as e,c as a,j as al,u as r,x as U,k as tt,y as Ul,a as s,v as w,d as j,t as b,w as d,m as lt,n as et}from"../chunks/index.2bf4358c.js";import{T as At}from"../chunks/Tip.363c041f.js";import{C as Zt}from"../chunks/CodeBlock.4e987730.js";import{H as Nt}from"../chunks/Heading.8ada512a.js";import{E as Tl}from"../chunks/index.e71982be.js";function ml(I){let o,J,y="BaseWorkflowAgent";return{c(){o=lt("有关高级智能体的更多信息，请访问 "),J=i("a"),J.textContent=y,this.h()},l(T){o=et(T,"有关高级智能体的更多信息，请访问 "),J=p(T,"A",{href:!0,"data-svelte-h":!0}),U(J)!=="svelte-1l6wzqu"&&(J.textContent=y),this.h()},h(){tt(J,"href","https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/agent/workflow/base_agent.py")},m(T,M){s(T,o,M),s(T,J,M)},p:Gt,d(T){T&&(e(o),e(J))}}}function yl(I){let o,J,y="这个 Notebook",T;return{c(){o=lt("您可以通过 "),J=i("a"),J.textContent=y,T=lt(" 跟随代码实践（使用 Google Colab 运行）。"),this.h()},l(M){o=et(M,"您可以通过 "),J=p(M,"A",{href:!0,target:!0,"data-svelte-h":!0}),U(J)!=="svelte-htthfz"&&(J.textContent=y),T=et(M," 跟随代码实践（使用 Google Colab 运行）。"),this.h()},h(){tt(J,"href","https://huggingface.co/agents-course/notebooks/blob/main/unit2/llama-index/agents.ipynb"),tt(J,"target","_blank")},m(M,f){s(M,o,f),s(M,J,f),s(M,T,f)},p:Gt,d(M){M&&(e(o),e(J),e(T))}}}function ul(I){let o,J,y="《AgentWorkflow 基本介绍》",T,M,f="《Agent 学习指南》",u;return{c(){o=lt("还没学够吗？在"),J=i("a"),J.textContent=y,T=lt("或"),M=i("a"),M.textContent=f,u=lt("中，您可以了解到更多关于流媒体、上下文序列化和人在环路中的信息！"),this.h()},l(m){o=et(m,"还没学够吗？在"),J=p(m,"A",{href:!0,"data-svelte-h":!0}),U(J)!=="svelte-sy2diy"&&(J.textContent=y),T=et(m,"或"),M=p(m,"A",{href:!0,"data-svelte-h":!0}),U(M)!=="svelte-wvkkbh"&&(M.textContent=f),u=et(m,"中，您可以了解到更多关于流媒体、上下文序列化和人在环路中的信息！"),this.h()},h(){tt(J,"href","https://docs.llamaindex.ai/en/stable/examples/agent/agent_workflow_basic/"),tt(M,"href","https://docs.llamaindex.ai/en/stable/understanding/agent/")},m(m,h){s(m,o,h),s(m,J,h),s(m,T,h),s(m,M,h),s(m,u,h)},p:Gt,d(m){m&&(e(o),e(J),e(T),e(M),e(u))}}}function cl(I){let o,J,y,T,M,f,u,m=`还记得我们之前那位得力的管家智能体 Alfred 吗？现在他要迎来重大升级了！
在了解了 LlamaIndex 中的工具后，我们可以赋予 Alfred 新的能力来更好地服务我们。`,h,B,_t=`不过在继续之前，让我们先回顾一下智能体（如 Alfred）的核心机制。
在第一单元中我们学习到：`,nt,x,Ft="<p>智能体是一个利用 AI 模型与环境交互以实现用户定义目标的系统。它通过结合推理、规划和动作执行（通常通过外部工具）来完成各种任务。</p>",at,W,St="LlamaIndex 支持<strong>三种主要类型的推理智能体</strong>：",Jt,E,vt='<img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/llama-index/agents.png" alt="智能体类型"/>',Mt,Q,kt="<li><code>函数调用智能体</code> - 适用于支持调用特定函数的 AI 模型</li> <li><code>ReAct 智能体</code> - 适用于具有聊天或文本完成能力的 AI 模型，擅长处理复杂推理任务</li> <li><code>高级自定义智能体</code> - 使用更复杂的方法处理高阶任务和工作流</li>",it,C,pt,$,ot,g,Ut,Z,Rt=`创建智能体时，我们首先需要为其提供<strong>定义其能力的功能/工具集合</strong>。
让我们看看如何使用基础工具创建智能体。当前实现中，智能体会自动使用函数调用 API（如果可用），或标准的 ReAct 智能体循环。`,Tt,N,Xt="支持工具/函数 API 的 LLM 是相对较新的技术，它们通过避免特定提示工程、允许 LLM 根据提供的模式创建工具调用，提供了强大的工具调用能力。",mt,A,Yt="ReAct 智能体同样擅长复杂推理任务，可与任何具备聊天或文本完成能力的 LLM 配合使用。这类智能体的响应更详细，会展示其决策背后的推理过程。",yt,G,ut,_,qt=`<strong>智能体默认是无状态的</strong>，如需记忆过往交互，需显式使用 <code>Context</code> 对象。
这在需要记忆历史交互的场景中非常有用，例如：需要跨消息保持上下文的聊天机器人，或需要追踪长期任务进度的任务管理器。`,ct,F,rt,S,Ht='您会注意到 <code>LlamaIndex</code> 中的智能体采用异步模式（使用 Python 的 <code>await</code> 操作符）。如果您是 Python 异步编程的新手，或需要复习相关知识，请参考 <a href="https://docs.llamaindex.ai/en/stable/getting_started/async_python/" rel="nofollow">官方异步编程指南</a>。',wt,v,zt="现在我们已经掌握基础知识，接下来让我们探索如何为智能体赋予更复杂的工具能力。",jt,k,bt,R,Lt=`<strong>智能体增强检索（Agentic RAG）</strong> 是通过智能体实现数据问答的强大范式。我们可以为 Alfred 配备多种工具来辅助问题解答。
但不同于传统 RAG 直接基于文档回答问题，Alfred 能够自主决定是否使用其他工具或流程来响应查询。`,dt,X,Pt='<img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/llama-index/agentic-rag.png" alt="智能体增强 RAG"/>',ft,Y,Dt=`将 <code>QueryEngine</code> 封装为智能体工具非常简单。
在此过程中，我们需要<strong>定义工具名称和描述</strong>，这些元数据将帮助 LLM 正确选择和使用工具。
以下示例演示如何基于<a href="02_components">组件章节</a>创建的 <code>QueryEngine</code> 加载 <code>QueryEngineTool</code>：`,ht,q,It,H,Ct,z,Ot="<code>AgentWorkflow</code> 类原生支持多智能体系统。通过为每个智能体分配名称和描述，系统可维护单一活跃会话主体，同时允许智能体之间进行任务交接。",gt,L,Kt="通过精准定义每个智能体的职责边界，我们能够有效提升系统响应消息时的整体准确性。",Vt,P,tl="<strong>LlamaIndex 中的智能体也可直接作为其他智能体的工具</strong>，这种设计支持构建复杂的定制化场景：",Bt,D,xt,V,Wt,O,ll="现在我们已经掌握了 LlamaIndex 中智能体和工具的基础知识，接下来让我们探索如何利用 LlamaIndex 来<strong>创建可配置、易管理的工作流</strong>！",Et,K,Qt,st,$t;return M=new Nt({props:{title:"在 LlamaIndex 中使用智能体",local:"在-llamaindex-中使用智能体",headingTag:"h1"}}),C=new At({props:{$$slots:{default:[ml]},$$scope:{ctx:I}}}),$=new Nt({props:{title:"初始化智能体",local:"初始化智能体",headingTag:"h2"}}),g=new At({props:{$$slots:{default:[yl]},$$scope:{ctx:I}}}),G=new Zt({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LmxsbXMuaHVnZ2luZ2ZhY2VfYXBpJTIwaW1wb3J0JTIwSHVnZ2luZ0ZhY2VJbmZlcmVuY2VBUEklMEFmcm9tJTIwbGxhbWFfaW5kZXguY29yZS5hZ2VudC53b3JrZmxvdyUyMGltcG9ydCUyMEFnZW50V29ya2Zsb3clMEFmcm9tJTIwbGxhbWFfaW5kZXguY29yZS50b29scyUyMGltcG9ydCUyMEZ1bmN0aW9uVG9vbCUwQSUwQSUyMyUyMCVFNSVBRSU5QSVFNCVCOSU4OSVFNyVBNCVCQSVFNCVCRSU4QiVFNSVCNyVBNSVFNSU4NSVCNy0tJUU3JUIxJUJCJUU1JTlFJThCJUU2JUIzJUE4JUU5JTg3JThBJUUzJTgwJTgxJUU1JTg3JUJEJUU2JTk1JUIwJUU1JTkwJThEJUU1JTkyJThDJTIwZG9jc3RyaW5ncyUyMCVFOSU4MyVCRCVFNSU4QyU4NSVFNSU5MCVBQiVFNSU5QyVBOCVFOCVBNyVBMyVFNiU5RSU5MCVFNyU5QSU4NCVFNiVBOCVBMSVFNSVCQyU4RiVFNCVCOCVBRCVFRiVCQyU4MSUwQWRlZiUyMG11bHRpcGx5KGElM0ElMjBpbnQlMkMlMjBiJTNBJTIwaW50KSUyMC0lM0UlMjBpbnQlM0ElMEElMjAlMjAlMjAlMjAlMjIlMjIlMjJNdWx0aXBsaWVzJTIwdHdvJTIwaW50ZWdlcnMlMjBhbmQlMjByZXR1cm5zJTIwdGhlJTIwcmVzdWx0aW5nJTIwaW50ZWdlciUyMiUyMiUyMiUwQSUyMCUyMCUyMCUyMHJldHVybiUyMGElMjAqJTIwYiUwQSUwQSUyMyUyMCVFNSU4OCU5RCVFNSVBNyU4QiVFNSU4QyU5NiUyMGxsbSUwQWxsbSUyMCUzRCUyMEh1Z2dpbmdGYWNlSW5mZXJlbmNlQVBJKG1vZGVsX25hbWUlM0QlMjJRd2VuJTJGUXdlbjIuNS1Db2Rlci0zMkItSW5zdHJ1Y3QlMjIpJTBBJTBBJTIzJTIwJUU1JTg4JTlEJUU1JUE3JThCJUU1JThDJTk2JTIwYWdlbnQlMEFhZ2VudCUyMCUzRCUyMEFnZW50V29ya2Zsb3cuZnJvbV90b29sc19vcl9mdW5jdGlvbnMoJTBBJTIwJTIwJTIwJTIwJTVCRnVuY3Rpb25Ub29sLmZyb21fZGVmYXVsdHMobXVsdGlwbHkpJTVEJTJDJTBBJTIwJTIwJTIwJTIwbGxtJTNEbGxtJTBBKQ==",highlighted:`<span class="hljs-keyword">from</span> llama_index.llms.huggingface_api <span class="hljs-keyword">import</span> HuggingFaceInferenceAPI
<span class="hljs-keyword">from</span> llama_index.core.agent.workflow <span class="hljs-keyword">import</span> AgentWorkflow
<span class="hljs-keyword">from</span> llama_index.core.tools <span class="hljs-keyword">import</span> FunctionTool

<span class="hljs-comment"># 定义示例工具--类型注释、函数名和 docstrings 都包含在解析的模式中！</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">multiply</span>(<span class="hljs-params">a: <span class="hljs-built_in">int</span>, b: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">int</span>:
    <span class="hljs-string">&quot;&quot;&quot;Multiplies two integers and returns the resulting integer&quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> a * b

<span class="hljs-comment"># 初始化 llm</span>
llm = HuggingFaceInferenceAPI(model_name=<span class="hljs-string">&quot;Qwen/Qwen2.5-Coder-32B-Instruct&quot;</span>)

<span class="hljs-comment"># 初始化 agent</span>
agent = AgentWorkflow.from_tools_or_functions(
    [FunctionTool.from_defaults(multiply)],
    llm=llm
)`,wrap:!1}}),F=new Zt({props:{code:"JTIzJTIwc3RhdGVsZXNzJTBBcmVzcG9uc2UlMjAlM0QlMjBhd2FpdCUyMGFnZW50LnJ1biglMjJXaGF0JTIwaXMlMjAyJTIwdGltZXMlMjAyJTNGJTIyKSUwQSUwQSUyMyUyMHJlbWVtYmVyaW5nJTIwc3RhdGUlMEFmcm9tJTIwbGxhbWFfaW5kZXguY29yZS53b3JrZmxvdyUyMGltcG9ydCUyMENvbnRleHQlMEElMEFjdHglMjAlM0QlMjBDb250ZXh0KGFnZW50KSUwQSUwQXJlc3BvbnNlJTIwJTNEJTIwYXdhaXQlMjBhZ2VudC5ydW4oJTIyTXklMjBuYW1lJTIwaXMlMjBCb2IuJTIyJTJDJTIwY3R4JTNEY3R4KSUwQXJlc3BvbnNlJTIwJTNEJTIwYXdhaXQlMjBhZ2VudC5ydW4oJTIyV2hhdCUyMHdhcyUyMG15JTIwbmFtZSUyMGFnYWluJTNGJTIyJTJDJTIwY3R4JTNEY3R4KQ==",highlighted:`<span class="hljs-comment"># stateless</span>
response = <span class="hljs-keyword">await</span> agent.run(<span class="hljs-string">&quot;What is 2 times 2?&quot;</span>)

<span class="hljs-comment"># remembering state</span>
<span class="hljs-keyword">from</span> llama_index.core.workflow <span class="hljs-keyword">import</span> Context

ctx = Context(agent)

response = <span class="hljs-keyword">await</span> agent.run(<span class="hljs-string">&quot;My name is Bob.&quot;</span>, ctx=ctx)
response = <span class="hljs-keyword">await</span> agent.run(<span class="hljs-string">&quot;What was my name again?&quot;</span>, ctx=ctx)`,wrap:!1}}),k=new Nt({props:{title:"使用 QueryEngineTools 创建 RAG 智能体",local:"使用-queryenginetools-创建-rag-智能体",headingTag:"h2"}}),q=new Zt({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LmNvcmUudG9vbHMlMjBpbXBvcnQlMjBRdWVyeUVuZ2luZVRvb2wlMEElMEFxdWVyeV9lbmdpbmUlMjAlM0QlMjBpbmRleC5hc19xdWVyeV9lbmdpbmUobGxtJTNEbGxtJTJDJTIwc2ltaWxhcml0eV90b3BfayUzRDMpJTIwJTIzJTIwYXMlMjBzaG93biUyMGluJTIwdGhlJTIwcHJldmlvdXMlMjBzZWN0aW9uJTBBJTBBcXVlcnlfZW5naW5lX3Rvb2wlMjAlM0QlMjBRdWVyeUVuZ2luZVRvb2wuZnJvbV9kZWZhdWx0cyglMEElMjAlMjAlMjAlMjBxdWVyeV9lbmdpbmUlM0RxdWVyeV9lbmdpbmUlMkMlMEElMjAlMjAlMjAlMjBuYW1lJTNEJTIybmFtZSUyMiUyQyUwQSUyMCUyMCUyMCUyMGRlc2NyaXB0aW9uJTNEJTIyYSUyMHNwZWNpZmljJTIwZGVzY3JpcHRpb24lMjIlMkMlMEElMjAlMjAlMjAlMjByZXR1cm5fZGlyZWN0JTNERmFsc2UlMkMlMEEpJTBBcXVlcnlfZW5naW5lX2FnZW50JTIwJTNEJTIwQWdlbnRXb3JrZmxvdy5mcm9tX3Rvb2xzX29yX2Z1bmN0aW9ucyglMEElMjAlMjAlMjAlMjAlNUJxdWVyeV9lbmdpbmVfdG9vbCU1RCUyQyUwQSUyMCUyMCUyMCUyMGxsbSUzRGxsbSUyQyUwQSUyMCUyMCUyMCUyMHN5c3RlbV9wcm9tcHQlM0QlMjJZb3UlMjBhcmUlMjBhJTIwaGVscGZ1bCUyMGFzc2lzdGFudCUyMHRoYXQlMjBoYXMlMjBhY2Nlc3MlMjB0byUyMGElMjBkYXRhYmFzZSUyMGNvbnRhaW5pbmclMjBwZXJzb25hJTIwZGVzY3JpcHRpb25zLiUyMCUyMiUwQSk=",highlighted:`<span class="hljs-keyword">from</span> llama_index.core.tools <span class="hljs-keyword">import</span> QueryEngineTool

query_engine = index.as_query_engine(llm=llm, similarity_top_k=<span class="hljs-number">3</span>) <span class="hljs-comment"># as shown in the previous section</span>

query_engine_tool = QueryEngineTool.from_defaults(
    query_engine=query_engine,
    name=<span class="hljs-string">&quot;name&quot;</span>,
    description=<span class="hljs-string">&quot;a specific description&quot;</span>,
    return_direct=<span class="hljs-literal">False</span>,
)
query_engine_agent = AgentWorkflow.from_tools_or_functions(
    [query_engine_tool],
    llm=llm,
    system_prompt=<span class="hljs-string">&quot;You are a helpful assistant that has access to a database containing persona descriptions. &quot;</span>
)`,wrap:!1}}),H=new Nt({props:{title:"创建多智能体系统",local:"创建多智能体系统",headingTag:"h2"}}),D=new Zt({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LmNvcmUuYWdlbnQud29ya2Zsb3clMjBpbXBvcnQlMjAoJTBBJTIwJTIwJTIwJTIwQWdlbnRXb3JrZmxvdyUyQyUwQSUyMCUyMCUyMCUyMEZ1bmN0aW9uQWdlbnQlMkMlMEElMjAlMjAlMjAlMjBSZUFjdEFnZW50JTJDJTBBKSUwQSUwQSUyMyUyMERlZmluZSUyMHNvbWUlMjB0b29scyUwQWRlZiUyMGFkZChhJTNBJTIwaW50JTJDJTIwYiUzQSUyMGludCklMjAtJTNFJTIwaW50JTNBJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTIyQWRkJTIwdHdvJTIwbnVtYmVycy4lMjIlMjIlMjIlMEElMjAlMjAlMjAlMjByZXR1cm4lMjBhJTIwJTJCJTIwYiUwQSUwQSUwQWRlZiUyMHN1YnRyYWN0KGElM0ElMjBpbnQlMkMlMjBiJTNBJTIwaW50KSUyMC0lM0UlMjBpbnQlM0ElMEElMjAlMjAlMjAlMjAlMjIlMjIlMjJTdWJ0cmFjdCUyMHR3byUyMG51bWJlcnMuJTIyJTIyJTIyJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwYSUyMC0lMjBiJTBBJTBBJTBBJTIzJTIwJUU1JTg4JTlCJUU1JUJCJUJBJUU2JTk5JUJBJUU4JTgzJUJEJUU0JUJEJTkzJUU5JTg1JThEJUU3JUJEJUFFJTBBJTIzJTIwJUU2JUIzJUE4JUU2JTg0JThGJUVGJUJDJTlBJUU2JTg4JTkxJUU0JUJCJUFDJUU1JThGJUFGJUU0JUJCJUE1JUU1JTlDJUE4JUU2JUFEJUE0JUU0JUJEJUJGJUU3JTk0JUE4JTIwRnVuY3Rpb25BZ2VudCUyMCVFNiU4OCU5NiUyMFJlQWN0QWdlbnQlRTMlODAlODIlMEElMjMlMjBGdW5jdGlvbkFnZW50JTIwJUU5JTgwJTgyJUU3JTk0JUE4JUU0JUJBJThFJUU1JTg1JUI3JUU2JTlDJTg5JUU1JTg3JUJEJUU2JTk1JUIwJUU4JUIwJTgzJUU3JTk0JUE4JTIwQVBJJTIwJUU3JTlBJTg0JTIwTExNJUUzJTgwJTgyJTBBJTIzJTIwUmVBY3RBZ2VudCUyMCVFOSU4MCU4MiVFNyU5NCVBOCVFNCVCQSU4RSVFNCVCQiVCQiVFNCVCRCU5NSUyMExMTSVFMyU4MCU4MiUwQWNhbGN1bGF0b3JfYWdlbnQlMjAlM0QlMjBSZUFjdEFnZW50KCUwQSUyMCUyMCUyMCUyMG5hbWUlM0QlMjJjYWxjdWxhdG9yJTIyJTJDJTBBJTIwJTIwJTIwJTIwZGVzY3JpcHRpb24lM0QlMjJQZXJmb3JtcyUyMGJhc2ljJTIwYXJpdGhtZXRpYyUyMG9wZXJhdGlvbnMlMjIlMkMlMEElMjAlMjAlMjAlMjBzeXN0ZW1fcHJvbXB0JTNEJTIyWW91JTIwYXJlJTIwYSUyMGNhbGN1bGF0b3IlMjBhc3Npc3RhbnQuJTIwVXNlJTIweW91ciUyMHRvb2xzJTIwZm9yJTIwYW55JTIwbWF0aCUyMG9wZXJhdGlvbi4lMjIlMkMlMEElMjAlMjAlMjAlMjB0b29scyUzRCU1QmFkZCUyQyUyMHN1YnRyYWN0JTVEJTJDJTBBJTIwJTIwJTIwJTIwbGxtJTNEbGxtJTJDJTBBKSUwQSUwQXF1ZXJ5X2FnZW50JTIwJTNEJTIwUmVBY3RBZ2VudCglMEElMjAlMjAlMjAlMjBuYW1lJTNEJTIyaW5mb19sb29rdXAlMjIlMkMlMEElMjAlMjAlMjAlMjBkZXNjcmlwdGlvbiUzRCUyMkxvb2tzJTIwdXAlMjBpbmZvcm1hdGlvbiUyMGFib3V0JTIwWFlaJTIyJTJDJTBBJTIwJTIwJTIwJTIwc3lzdGVtX3Byb21wdCUzRCUyMlVzZSUyMHlvdXIlMjB0b29sJTIwdG8lMjBxdWVyeSUyMGElMjBSQUclMjBzeXN0ZW0lMjB0byUyMGFuc3dlciUyMGluZm9ybWF0aW9uJTIwYWJvdXQlMjBYWVolMjIlMkMlMEElMjAlMjAlMjAlMjB0b29scyUzRCU1QnF1ZXJ5X2VuZ2luZV90b29sJTVEJTJDJTBBJTIwJTIwJTIwJTIwbGxtJTNEbGxtJTBBKSUwQSUwQSUyMyUyMCVFNSU4OCU5QiVFNSVCQiVCQSVFNSVCOSVCNiVFOCVCRiU5MCVFOCVBMSU4QyVFNSVCNyVBNSVFNCVCRCU5QyVFNiVCNSU4MSVFNyVBOCU4QiUwQWFnZW50JTIwJTNEJTIwQWdlbnRXb3JrZmxvdyglMEElMjAlMjAlMjAlMjBhZ2VudHMlM0QlNUJjYWxjdWxhdG9yX2FnZW50JTJDJTIwcXVlcnlfYWdlbnQlNUQlMkMlMjByb290X2FnZW50JTNEJTIyY2FsY3VsYXRvciUyMiUwQSklMEElMEElMjMlMjAlRTglQkYlOTAlRTglQTElOEMlRTclQjMlQkIlRTclQkIlOUYlMEFyZXNwb25zZSUyMCUzRCUyMGF3YWl0JTIwYWdlbnQucnVuKHVzZXJfbXNnJTNEJTIyQ2FuJTIweW91JTIwYWRkJTIwNSUyMGFuZCUyMDMlM0YlMjIp",highlighted:`<span class="hljs-keyword">from</span> llama_index.core.agent.workflow <span class="hljs-keyword">import</span> (
    AgentWorkflow,
    FunctionAgent,
    ReActAgent,
)

<span class="hljs-comment"># Define some tools</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">add</span>(<span class="hljs-params">a: <span class="hljs-built_in">int</span>, b: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">int</span>:
    <span class="hljs-string">&quot;&quot;&quot;Add two numbers.&quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> a + b


<span class="hljs-keyword">def</span> <span class="hljs-title function_">subtract</span>(<span class="hljs-params">a: <span class="hljs-built_in">int</span>, b: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">int</span>:
    <span class="hljs-string">&quot;&quot;&quot;Subtract two numbers.&quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> a - b


<span class="hljs-comment"># 创建智能体配置</span>
<span class="hljs-comment"># 注意：我们可以在此使用 FunctionAgent 或 ReActAgent。</span>
<span class="hljs-comment"># FunctionAgent 适用于具有函数调用 API 的 LLM。</span>
<span class="hljs-comment"># ReActAgent 适用于任何 LLM。</span>
calculator_agent = ReActAgent(
    name=<span class="hljs-string">&quot;calculator&quot;</span>,
    description=<span class="hljs-string">&quot;Performs basic arithmetic operations&quot;</span>,
    system_prompt=<span class="hljs-string">&quot;You are a calculator assistant. Use your tools for any math operation.&quot;</span>,
    tools=[add, subtract],
    llm=llm,
)

query_agent = ReActAgent(
    name=<span class="hljs-string">&quot;info_lookup&quot;</span>,
    description=<span class="hljs-string">&quot;Looks up information about XYZ&quot;</span>,
    system_prompt=<span class="hljs-string">&quot;Use your tool to query a RAG system to answer information about XYZ&quot;</span>,
    tools=[query_engine_tool],
    llm=llm
)

<span class="hljs-comment"># 创建并运行工作流程</span>
agent = AgentWorkflow(
    agents=[calculator_agent, query_agent], root_agent=<span class="hljs-string">&quot;calculator&quot;</span>
)

<span class="hljs-comment"># 运行系统</span>
response = <span class="hljs-keyword">await</span> agent.run(user_msg=<span class="hljs-string">&quot;Can you add 5 and 3?&quot;</span>)`,wrap:!1}}),V=new At({props:{$$slots:{default:[ul]},$$scope:{ctx:I}}}),K=new Tl({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/zh-CN/unit2/llama-index/agents.mdx"}}),{c(){o=i("meta"),J=n(),y=i("p"),T=n(),c(M.$$.fragment),f=n(),u=i("p"),u.textContent=m,h=n(),B=i("p"),B.textContent=_t,nt=n(),x=i("blockquote"),x.innerHTML=Ft,at=n(),W=i("p"),W.innerHTML=St,Jt=n(),E=i("p"),E.innerHTML=vt,Mt=n(),Q=i("ol"),Q.innerHTML=kt,it=n(),c(C.$$.fragment),pt=n(),c($.$$.fragment),ot=n(),c(g.$$.fragment),Ut=n(),Z=i("p"),Z.innerHTML=Rt,Tt=n(),N=i("p"),N.textContent=Xt,mt=n(),A=i("p"),A.textContent=Yt,yt=n(),c(G.$$.fragment),ut=n(),_=i("p"),_.innerHTML=qt,ct=n(),c(F.$$.fragment),rt=n(),S=i("p"),S.innerHTML=Ht,wt=n(),v=i("p"),v.textContent=zt,jt=n(),c(k.$$.fragment),bt=n(),R=i("p"),R.innerHTML=Lt,dt=n(),X=i("p"),X.innerHTML=Pt,ft=n(),Y=i("p"),Y.innerHTML=Dt,ht=n(),c(q.$$.fragment),It=n(),c(H.$$.fragment),Ct=n(),z=i("p"),z.innerHTML=Ot,gt=n(),L=i("p"),L.textContent=Kt,Vt=n(),P=i("p"),P.innerHTML=tl,Bt=n(),c(D.$$.fragment),xt=n(),c(V.$$.fragment),Wt=n(),O=i("p"),O.innerHTML=ll,Et=n(),c(K.$$.fragment),Qt=n(),st=i("p"),this.h()},l(t){const l=ol("svelte-u9bgzb",document.head);o=p(l,"META",{name:!0,content:!0}),l.forEach(e),J=a(t),y=p(t,"P",{}),al(y).forEach(e),T=a(t),r(M.$$.fragment,t),f=a(t),u=p(t,"P",{"data-svelte-h":!0}),U(u)!=="svelte-10d7e98"&&(u.textContent=m),h=a(t),B=p(t,"P",{"data-svelte-h":!0}),U(B)!=="svelte-hkw26n"&&(B.textContent=_t),nt=a(t),x=p(t,"BLOCKQUOTE",{"data-svelte-h":!0}),U(x)!=="svelte-1ksbkzo"&&(x.innerHTML=Ft),at=a(t),W=p(t,"P",{"data-svelte-h":!0}),U(W)!=="svelte-17xgwds"&&(W.innerHTML=St),Jt=a(t),E=p(t,"P",{"data-svelte-h":!0}),U(E)!=="svelte-3nptas"&&(E.innerHTML=vt),Mt=a(t),Q=p(t,"OL",{"data-svelte-h":!0}),U(Q)!=="svelte-fuuhge"&&(Q.innerHTML=kt),it=a(t),r(C.$$.fragment,t),pt=a(t),r($.$$.fragment,t),ot=a(t),r(g.$$.fragment,t),Ut=a(t),Z=p(t,"P",{"data-svelte-h":!0}),U(Z)!=="svelte-z71ivx"&&(Z.innerHTML=Rt),Tt=a(t),N=p(t,"P",{"data-svelte-h":!0}),U(N)!=="svelte-x4ueck"&&(N.textContent=Xt),mt=a(t),A=p(t,"P",{"data-svelte-h":!0}),U(A)!=="svelte-p7mr9a"&&(A.textContent=Yt),yt=a(t),r(G.$$.fragment,t),ut=a(t),_=p(t,"P",{"data-svelte-h":!0}),U(_)!=="svelte-18rw6xl"&&(_.innerHTML=qt),ct=a(t),r(F.$$.fragment,t),rt=a(t),S=p(t,"P",{"data-svelte-h":!0}),U(S)!=="svelte-66jeeb"&&(S.innerHTML=Ht),wt=a(t),v=p(t,"P",{"data-svelte-h":!0}),U(v)!=="svelte-741za"&&(v.textContent=zt),jt=a(t),r(k.$$.fragment,t),bt=a(t),R=p(t,"P",{"data-svelte-h":!0}),U(R)!=="svelte-gbmyfe"&&(R.innerHTML=Lt),dt=a(t),X=p(t,"P",{"data-svelte-h":!0}),U(X)!=="svelte-10ho98a"&&(X.innerHTML=Pt),ft=a(t),Y=p(t,"P",{"data-svelte-h":!0}),U(Y)!=="svelte-uxehys"&&(Y.innerHTML=Dt),ht=a(t),r(q.$$.fragment,t),It=a(t),r(H.$$.fragment,t),Ct=a(t),z=p(t,"P",{"data-svelte-h":!0}),U(z)!=="svelte-hio4gu"&&(z.innerHTML=Ot),gt=a(t),L=p(t,"P",{"data-svelte-h":!0}),U(L)!=="svelte-bo2cy"&&(L.textContent=Kt),Vt=a(t),P=p(t,"P",{"data-svelte-h":!0}),U(P)!=="svelte-q3vd3v"&&(P.innerHTML=tl),Bt=a(t),r(D.$$.fragment,t),xt=a(t),r(V.$$.fragment,t),Wt=a(t),O=p(t,"P",{"data-svelte-h":!0}),U(O)!=="svelte-19brcgj"&&(O.innerHTML=ll),Et=a(t),r(K.$$.fragment,t),Qt=a(t),st=p(t,"P",{}),al(st).forEach(e),this.h()},h(){tt(o,"name","hf:doc:metadata"),tt(o,"content",rl)},m(t,l){Ul(document.head,o),s(t,J,l),s(t,y,l),s(t,T,l),w(M,t,l),s(t,f,l),s(t,u,l),s(t,h,l),s(t,B,l),s(t,nt,l),s(t,x,l),s(t,at,l),s(t,W,l),s(t,Jt,l),s(t,E,l),s(t,Mt,l),s(t,Q,l),s(t,it,l),w(C,t,l),s(t,pt,l),w($,t,l),s(t,ot,l),w(g,t,l),s(t,Ut,l),s(t,Z,l),s(t,Tt,l),s(t,N,l),s(t,mt,l),s(t,A,l),s(t,yt,l),w(G,t,l),s(t,ut,l),s(t,_,l),s(t,ct,l),w(F,t,l),s(t,rt,l),s(t,S,l),s(t,wt,l),s(t,v,l),s(t,jt,l),w(k,t,l),s(t,bt,l),s(t,R,l),s(t,dt,l),s(t,X,l),s(t,ft,l),s(t,Y,l),s(t,ht,l),w(q,t,l),s(t,It,l),w(H,t,l),s(t,Ct,l),s(t,z,l),s(t,gt,l),s(t,L,l),s(t,Vt,l),s(t,P,l),s(t,Bt,l),w(D,t,l),s(t,xt,l),w(V,t,l),s(t,Wt,l),s(t,O,l),s(t,Et,l),w(K,t,l),s(t,Qt,l),s(t,st,l),$t=!0},p(t,[l]){const el={};l&2&&(el.$$scope={dirty:l,ctx:t}),C.$set(el);const sl={};l&2&&(sl.$$scope={dirty:l,ctx:t}),g.$set(sl);const nl={};l&2&&(nl.$$scope={dirty:l,ctx:t}),V.$set(nl)},i(t){$t||(j(M.$$.fragment,t),j(C.$$.fragment,t),j($.$$.fragment,t),j(g.$$.fragment,t),j(G.$$.fragment,t),j(F.$$.fragment,t),j(k.$$.fragment,t),j(q.$$.fragment,t),j(H.$$.fragment,t),j(D.$$.fragment,t),j(V.$$.fragment,t),j(K.$$.fragment,t),$t=!0)},o(t){b(M.$$.fragment,t),b(C.$$.fragment,t),b($.$$.fragment,t),b(g.$$.fragment,t),b(G.$$.fragment,t),b(F.$$.fragment,t),b(k.$$.fragment,t),b(q.$$.fragment,t),b(H.$$.fragment,t),b(D.$$.fragment,t),b(V.$$.fragment,t),b(K.$$.fragment,t),$t=!1},d(t){t&&(e(J),e(y),e(T),e(f),e(u),e(h),e(B),e(nt),e(x),e(at),e(W),e(Jt),e(E),e(Mt),e(Q),e(it),e(pt),e(ot),e(Ut),e(Z),e(Tt),e(N),e(mt),e(A),e(yt),e(ut),e(_),e(ct),e(rt),e(S),e(wt),e(v),e(jt),e(bt),e(R),e(dt),e(X),e(ft),e(Y),e(ht),e(It),e(Ct),e(z),e(gt),e(L),e(Vt),e(P),e(Bt),e(xt),e(Wt),e(O),e(Et),e(Qt),e(st)),e(o),d(M,t),d(C,t),d($,t),d(g,t),d(G,t),d(F,t),d(k,t),d(q,t),d(H,t),d(D,t),d(V,t),d(K,t)}}}const rl='{"title":"在 LlamaIndex 中使用智能体","local":"在-llamaindex-中使用智能体","sections":[{"title":"初始化智能体","local":"初始化智能体","sections":[],"depth":2},{"title":"使用 QueryEngineTools 创建 RAG 智能体","local":"使用-queryenginetools-创建-rag-智能体","sections":[],"depth":2},{"title":"创建多智能体系统","local":"创建多智能体系统","sections":[],"depth":2}],"depth":1}';function wl(I){return Ml(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Cl extends il{constructor(o){super(),pl(this,o,wl,cl,Jl,{})}}export{Cl as component};
