import{s as ze,n as Pe,o as Ae}from"../chunks/scheduler.37c15a92.js";import{S as Ke,i as Oe,g as a,s as n,r as p,m as De,A as el,h as i,f as t,c as o,j as $e,u as c,x as m,n as ll,k as Y,y as _e,a as s,v as r,d,t as u,w as b}from"../chunks/index.2bf4358c.js";import{C as z}from"../chunks/CodeBlock.4e987730.js";import{H as P}from"../chunks/Heading.8ada512a.js";import{E as tl}from"../chunks/getInferenceSnippets.0aa3753f.js";function sl(We){let M,A,F,K,w,O,g,Ie=`<strong>定义清晰明确的工具集对性能至关重要。</strong> 正如我们在<a href="../../unit1/tools">第一单元</a>中讨论的，清晰的工具接口更便于 LLM 使用。
就像人类工程师使用的软件API接口一样，如果工具的工作原理容易理解，LLM就能更好地利用它。`,D,Z,xe="LlamaIndex 中主要包含<strong>四种工具类型</strong>：",ee,j,Ce='<img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit2/llama-index/tools.png" alt="工具"/>',le,J,Le="<li><code>FunctionTool</code>：将任意 Python 函数转换为智能体可以使用的工具。它能自动识别函数的工作原理。</li> <li><code>QueryEngineTool</code>：让智能体能够使用查询引擎的工具。由于智能体本身基于查询引擎构建，因此它们也可以将其他智能体作为工具使用。</li> <li><code>Toolspecs</code>：由社区创建的预设工具集，通常包含针对特定服务（如 Gmail）的工具。</li> <li><code>Utility Tools</code>：帮助处理来自其他工具的大量数据的特殊工具。</li>",te,U,Re="下面我们将详细讲解每种工具的使用方法。",se,v,ne,f,Ve='<p>您可以通过<a href="https://huggingface.co/agents-course/notebooks/blob/main/unit2/llama-index/tools.ipynb" target="_blank">这个 Notebook</a>中的代码进行实践（支持 Google Colab 运行）。</p>',oe,G,ke=`FunctionTool 提供了一种简单的方式，可以将任意 Python 函数封装成智能体可用的工具。
你可以传递同步或异步函数给这个工具，并可选地指定<code>name</code>和<code>description</code>参数。
工具名称和描述尤为重要，它们能帮助智能体理解何时以及如何有效地使用该工具。
以下示例演示了如何创建并调用 FunctionTool。`,ae,$,ie,y,Be='<p>使用带有函数调用的智能体或 LLM 时，所选工具（以及为该工具编写的参数）在很大程度上取决于工具名称以及工具用途和参数的描述。在<a href="https://docs.llamaindex.ai/en/stable/examples/workflow/function_calling_agent/">函数调用指南</a>。</p>',me,_,pe,W,He=`我们在上一单元中定义的 <code>QueryEngine</code> 可以使用 <code>QueryEngineTool</code> 类轻松转换为工具。
让我们看看如何在下面的示例中从 <code>QueryEngine</code> 创建 <code>QueryEngineTool</code>。`,ce,I,re,x,de,C,Ee=`将 <code>ToolSpecs</code> 视为可以和谐协作的工具集合 - 就像一个组织良好的专业工具包。
就像机械师的工具包包含用于车辆维修的互补工具一样，<code>ToolSpec</code> 会将相关工具组合起来用于特定目的。
例如，会计智能体的 <code>ToolSpec</code> 可以优雅地集成电子表格功能、电子邮件功能和计算工具，以精确高效地处理财务任务。`,ue,T,Q,Ne="安装 Google Toolspec",Ge,L,be,R,Xe="现在我们可以加载工具规范并将其转换为工具列表。",Me,V,fe,k,Se="为了更详细地了解这些工具，我们可以查看每个工具的<code>元数据</code>。",ye,B,Te,H,he,E,Qe=`很多时候，直接查询 API <strong>可能会返回过量数据</strong>，其中部分可能无关紧要、超出 LLM 的上下文窗口容量，或导致不必要的 token 消耗。
让我们详细了解以下两个主要实用工具：`,we,N,Fe="<li><code>OnDemandToolLoader</code>：该工具可将任何现有的 LlamaIndex 数据加载器（BaseReader 类）转化为智能体可使用的工具。该工具可通过调用数据加载器<code>load_data</code>所需的所有参数以及自然语言查询字符串来触发。在执行过程中，我们首先从数据加载器加载数据，建立索引（例如使用向量存储），然后进行”按需”查询。这三个步骤都在单个工具调用中完成。</li> <li><code>LoadAndSearchToolSpec</code>：该工具规范接受任何现有工具作为输入。作为工具规范，它实现了<code>to_tool_list</code>方法，调用该方法时会返回两个工具：加载工具和搜索工具。加载工具的执行会调用底层工具并对输出建立索引（默认使用向量索引）。搜索工具的执行会接受查询字符串作为输入并调用底层索引。</li>",ge,h,qe='<p>您可以在<a href="https://llamahub.ai/">LlamaHub</a>找到各种工具规范和实用工具</p>',Ze,X,Ye="现在我们已经理解了 LlamaIndex 中智能体和工具的基础知识，接下来让我们看看如何<strong>使用 LlamaIndex 创建可配置且易管理的工作流程！</strong>",je,S,Je,q,Ue;return w=new P({props:{title:"在 LlamaIndex 中使用工具",local:"在-llamaindex-中使用工具",headingTag:"h1"}}),v=new P({props:{title:"创建FunctionTool",local:"创建functiontool",headingTag:"h2"}}),$=new z({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LmNvcmUudG9vbHMlMjBpbXBvcnQlMjBGdW5jdGlvblRvb2wlMEElMEFkZWYlMjBnZXRfd2VhdGhlcihsb2NhdGlvbiUzQSUyMHN0ciklMjAtJTNFJTIwc3RyJTNBJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTIyVXNlZnVsJTIwZm9yJTIwZ2V0dGluZyUyMHRoZSUyMHdlYXRoZXIlMjBmb3IlMjBhJTIwZ2l2ZW4lMjBsb2NhdGlvbi4lMjIlMjIlMjIlMEElMjAlMjAlMjAlMjBwcmludChmJTIyR2V0dGluZyUyMHdlYXRoZXIlMjBmb3IlMjAlN0Jsb2NhdGlvbiU3RCUyMiklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBmJTIyVGhlJTIwd2VhdGhlciUyMGluJTIwJTdCbG9jYXRpb24lN0QlMjBpcyUyMHN1bm55JTIyJTBBJTBBdG9vbCUyMCUzRCUyMEZ1bmN0aW9uVG9vbC5mcm9tX2RlZmF1bHRzKCUwQSUyMCUyMCUyMCUyMGdldF93ZWF0aGVyJTJDJTBBJTIwJTIwJTIwJTIwbmFtZSUzRCUyMm15X3dlYXRoZXJfdG9vbCUyMiUyQyUwQSUyMCUyMCUyMCUyMGRlc2NyaXB0aW9uJTNEJTIyVXNlZnVsJTIwZm9yJTIwZ2V0dGluZyUyMHRoZSUyMHdlYXRoZXIlMjBmb3IlMjBhJTIwZ2l2ZW4lMjBsb2NhdGlvbi4lMjIlMkMlMEEpJTBBdG9vbC5jYWxsKCUyMk5ldyUyMFlvcmslMjIp",highlighted:`<span class="hljs-keyword">from</span> llama_index.core.tools <span class="hljs-keyword">import</span> FunctionTool

<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_weather</span>(<span class="hljs-params">location: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">str</span>:
    <span class="hljs-string">&quot;&quot;&quot;Useful for getting the weather for a given location.&quot;&quot;&quot;</span>
    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Getting weather for <span class="hljs-subst">{location}</span>&quot;</span>)
    <span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;The weather in <span class="hljs-subst">{location}</span> is sunny&quot;</span>

tool = FunctionTool.from_defaults(
    get_weather,
    name=<span class="hljs-string">&quot;my_weather_tool&quot;</span>,
    description=<span class="hljs-string">&quot;Useful for getting the weather for a given location.&quot;</span>,
)
tool.call(<span class="hljs-string">&quot;New York&quot;</span>)`,wrap:!1}}),_=new P({props:{title:"创建 QueryEngineTool",local:"创建-queryenginetool",headingTag:"h2"}}),I=new z({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LmNvcmUlMjBpbXBvcnQlMjBWZWN0b3JTdG9yZUluZGV4JTBBZnJvbSUyMGxsYW1hX2luZGV4LmNvcmUudG9vbHMlMjBpbXBvcnQlMjBRdWVyeUVuZ2luZVRvb2wlMEFmcm9tJTIwbGxhbWFfaW5kZXgubGxtcy5odWdnaW5nZmFjZV9hcGklMjBpbXBvcnQlMjBIdWdnaW5nRmFjZUluZmVyZW5jZUFQSSUwQWZyb20lMjBsbGFtYV9pbmRleC5lbWJlZGRpbmdzLmh1Z2dpbmdmYWNlX2FwaSUyMGltcG9ydCUyMEh1Z2dpbmdGYWNlSW5mZXJlbmNlQVBJRW1iZWRkaW5nJTBBZnJvbSUyMGxsYW1hX2luZGV4LnZlY3Rvcl9zdG9yZXMuY2hyb21hJTIwaW1wb3J0JTIwQ2hyb21hVmVjdG9yU3RvcmUlMEElMEFlbWJlZF9tb2RlbCUyMCUzRCUyMEh1Z2dpbmdGYWNlSW5mZXJlbmNlQVBJRW1iZWRkaW5nKCUyMkJBQUklMkZiZ2Utc21hbGwtZW4tdjEuNSUyMiklMEElMEFkYiUyMCUzRCUyMGNocm9tYWRiLlBlcnNpc3RlbnRDbGllbnQocGF0aCUzRCUyMi4lMkZhbGZyZWRfY2hyb21hX2RiJTIyKSUwQWNocm9tYV9jb2xsZWN0aW9uJTIwJTNEJTIwZGIuZ2V0X29yX2NyZWF0ZV9jb2xsZWN0aW9uKCUyMmFsZnJlZCUyMiklMEF2ZWN0b3Jfc3RvcmUlMjAlM0QlMjBDaHJvbWFWZWN0b3JTdG9yZShjaHJvbWFfY29sbGVjdGlvbiUzRGNocm9tYV9jb2xsZWN0aW9uKSUwQSUwQWluZGV4JTIwJTNEJTIwVmVjdG9yU3RvcmVJbmRleC5mcm9tX3ZlY3Rvcl9zdG9yZSh2ZWN0b3Jfc3RvcmUlMkMlMjBlbWJlZF9tb2RlbCUzRGVtYmVkX21vZGVsKSUwQSUwQWxsbSUyMCUzRCUyMEh1Z2dpbmdGYWNlSW5mZXJlbmNlQVBJKG1vZGVsX25hbWUlM0QlMjJRd2VuJTJGUXdlbjIuNS1Db2Rlci0zMkItSW5zdHJ1Y3QlMjIpJTBBcXVlcnlfZW5naW5lJTIwJTNEJTIwaW5kZXguYXNfcXVlcnlfZW5naW5lKGxsbSUzRGxsbSklMEF0b29sJTIwJTNEJTIwUXVlcnlFbmdpbmVUb29sLmZyb21fZGVmYXVsdHMocXVlcnlfZW5naW5lJTJDJTIwbmFtZSUzRCUyMnNvbWUlMjB1c2VmdWwlMjBuYW1lJTIyJTJDJTIwZGVzY3JpcHRpb24lM0QlMjJzb21lJTIwdXNlZnVsJTIwZGVzY3JpcHRpb24lMjIp",highlighted:`<span class="hljs-keyword">from</span> llama_index.core <span class="hljs-keyword">import</span> VectorStoreIndex
<span class="hljs-keyword">from</span> llama_index.core.tools <span class="hljs-keyword">import</span> QueryEngineTool
<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.embeddings.huggingface_api <span class="hljs-keyword">import</span> HuggingFaceInferenceAPIEmbedding
<span class="hljs-keyword">from</span> llama_index.vector_stores.chroma <span class="hljs-keyword">import</span> ChromaVectorStore

embed_model = HuggingFaceInferenceAPIEmbedding(<span class="hljs-string">&quot;BAAI/bge-small-en-v1.5&quot;</span>)

db = chromadb.PersistentClient(path=<span class="hljs-string">&quot;./alfred_chroma_db&quot;</span>)
chroma_collection = db.get_or_create_collection(<span class="hljs-string">&quot;alfred&quot;</span>)
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)

llm = HuggingFaceInferenceAPI(model_name=<span class="hljs-string">&quot;Qwen/Qwen2.5-Coder-32B-Instruct&quot;</span>)
query_engine = index.as_query_engine(llm=llm)
tool = QueryEngineTool.from_defaults(query_engine, name=<span class="hljs-string">&quot;some useful name&quot;</span>, description=<span class="hljs-string">&quot;some useful description&quot;</span>)`,wrap:!1}}),x=new P({props:{title:"创建 Toolspecs",local:"创建-toolspecs",headingTag:"h2"}}),L=new z({props:{code:"cGlwJTIwaW5zdGFsbCUyMGxsYW1hLWluZGV4LXRvb2xzLWdvb2dsZQ==",highlighted:"pip install llama-index-tools-google",wrap:!1}}),V=new z({props:{code:"ZnJvbSUyMGxsYW1hX2luZGV4LnRvb2xzLmdvb2dsZSUyMGltcG9ydCUyMEdtYWlsVG9vbFNwZWMlMEElMEF0b29sX3NwZWMlMjAlM0QlMjBHbWFpbFRvb2xTcGVjKCklMEF0b29sX3NwZWNfbGlzdCUyMCUzRCUyMHRvb2xfc3BlYy50b190b29sX2xpc3QoKQ==",highlighted:`<span class="hljs-keyword">from</span> llama_index.tools.google <span class="hljs-keyword">import</span> GmailToolSpec

tool_spec = GmailToolSpec()
tool_spec_list = tool_spec.to_tool_list()`,wrap:!1}}),B=new z({props:{code:"JTVCKHRvb2wubWV0YWRhdGEubmFtZSUyQyUyMHRvb2wubWV0YWRhdGEuZGVzY3JpcHRpb24pJTIwZm9yJTIwdG9vbCUyMGluJTIwdG9vbF9zcGVjX2xpc3QlNUQ=",highlighted:'[(tool.metadata.name, tool.metadata.description) <span class="hljs-keyword">for</span> tool <span class="hljs-keyword">in</span> tool_spec_list]',wrap:!1}}),H=new P({props:{title:"实用工具",local:"实用工具",headingTag:"h2"}}),S=new tl({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/zh-CN/unit2/llama-index/tools.mdx"}}),{c(){M=a("meta"),A=n(),F=a("p"),K=n(),p(w.$$.fragment),O=n(),g=a("p"),g.innerHTML=Ie,D=n(),Z=a("p"),Z.innerHTML=xe,ee=n(),j=a("p"),j.innerHTML=Ce,le=n(),J=a("ol"),J.innerHTML=Le,te=n(),U=a("p"),U.textContent=Re,se=n(),p(v.$$.fragment),ne=n(),f=a("blockquote"),f.innerHTML=Ve,oe=n(),G=a("p"),G.innerHTML=ke,ae=n(),p($.$$.fragment),ie=n(),y=a("blockquote"),y.innerHTML=Be,me=n(),p(_.$$.fragment),pe=n(),W=a("p"),W.innerHTML=He,ce=n(),p(I.$$.fragment),re=n(),p(x.$$.fragment),de=n(),C=a("p"),C.innerHTML=Ee,ue=n(),T=a("details"),Q=a("summary"),Q.textContent=Ne,Ge=De(`
正如 [LlamaHub 部分](llama-hub) 中介绍的那样，我们可以使用以下命令安装 Google toolspec：

	`),p(L.$$.fragment),be=n(),R=a("p"),R.textContent=Xe,Me=n(),p(V.$$.fragment),fe=n(),k=a("p"),k.innerHTML=Se,ye=n(),p(B.$$.fragment),Te=n(),p(H.$$.fragment),he=n(),E=a("p"),E.innerHTML=Qe,we=n(),N=a("ol"),N.innerHTML=Fe,ge=n(),h=a("blockquote"),h.innerHTML=qe,Ze=n(),X=a("p"),X.innerHTML=Ye,je=n(),p(S.$$.fragment),Je=n(),q=a("p"),this.h()},l(e){const l=el("svelte-u9bgzb",document.head);M=i(l,"META",{name:!0,content:!0}),l.forEach(t),A=o(e),F=i(e,"P",{}),$e(F).forEach(t),K=o(e),c(w.$$.fragment,e),O=o(e),g=i(e,"P",{"data-svelte-h":!0}),m(g)!=="svelte-2m2wye"&&(g.innerHTML=Ie),D=o(e),Z=i(e,"P",{"data-svelte-h":!0}),m(Z)!=="svelte-3o10ap"&&(Z.innerHTML=xe),ee=o(e),j=i(e,"P",{"data-svelte-h":!0}),m(j)!=="svelte-1puooe3"&&(j.innerHTML=Ce),le=o(e),J=i(e,"OL",{"data-svelte-h":!0}),m(J)!=="svelte-au1cjc"&&(J.innerHTML=Le),te=o(e),U=i(e,"P",{"data-svelte-h":!0}),m(U)!=="svelte-1jlj8k4"&&(U.textContent=Re),se=o(e),c(v.$$.fragment,e),ne=o(e),f=i(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),m(f)!=="svelte-t6s5r7"&&(f.innerHTML=Ve),oe=o(e),G=i(e,"P",{"data-svelte-h":!0}),m(G)!=="svelte-c86ggq"&&(G.innerHTML=ke),ae=o(e),c($.$$.fragment,e),ie=o(e),y=i(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),m(y)!=="svelte-1molfgm"&&(y.innerHTML=Be),me=o(e),c(_.$$.fragment,e),pe=o(e),W=i(e,"P",{"data-svelte-h":!0}),m(W)!=="svelte-19fn29r"&&(W.innerHTML=He),ce=o(e),c(I.$$.fragment,e),re=o(e),c(x.$$.fragment,e),de=o(e),C=i(e,"P",{"data-svelte-h":!0}),m(C)!=="svelte-4ryh0b"&&(C.innerHTML=Ee),ue=o(e),T=i(e,"DETAILS",{});var ve=$e(T);Q=i(ve,"SUMMARY",{"data-svelte-h":!0}),m(Q)!=="svelte-jh6hbu"&&(Q.textContent=Ne),Ge=ll(ve,`
正如 [LlamaHub 部分](llama-hub) 中介绍的那样，我们可以使用以下命令安装 Google toolspec：

	`),c(L.$$.fragment,ve),ve.forEach(t),be=o(e),R=i(e,"P",{"data-svelte-h":!0}),m(R)!=="svelte-1fx0kig"&&(R.textContent=Xe),Me=o(e),c(V.$$.fragment,e),fe=o(e),k=i(e,"P",{"data-svelte-h":!0}),m(k)!=="svelte-io2nl"&&(k.innerHTML=Se),ye=o(e),c(B.$$.fragment,e),Te=o(e),c(H.$$.fragment,e),he=o(e),E=i(e,"P",{"data-svelte-h":!0}),m(E)!=="svelte-1w2i7nx"&&(E.innerHTML=Qe),we=o(e),N=i(e,"OL",{"data-svelte-h":!0}),m(N)!=="svelte-1aywyxf"&&(N.innerHTML=Fe),ge=o(e),h=i(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),m(h)!=="svelte-2q4ehq"&&(h.innerHTML=qe),Ze=o(e),X=i(e,"P",{"data-svelte-h":!0}),m(X)!=="svelte-drrdj6"&&(X.innerHTML=Ye),je=o(e),c(S.$$.fragment,e),Je=o(e),q=i(e,"P",{}),$e(q).forEach(t),this.h()},h(){Y(M,"name","hf:doc:metadata"),Y(M,"content",nl),Y(f,"class","tip"),Y(y,"class","tip"),Y(h,"class","tip")},m(e,l){_e(document.head,M),s(e,A,l),s(e,F,l),s(e,K,l),r(w,e,l),s(e,O,l),s(e,g,l),s(e,D,l),s(e,Z,l),s(e,ee,l),s(e,j,l),s(e,le,l),s(e,J,l),s(e,te,l),s(e,U,l),s(e,se,l),r(v,e,l),s(e,ne,l),s(e,f,l),s(e,oe,l),s(e,G,l),s(e,ae,l),r($,e,l),s(e,ie,l),s(e,y,l),s(e,me,l),r(_,e,l),s(e,pe,l),s(e,W,l),s(e,ce,l),r(I,e,l),s(e,re,l),r(x,e,l),s(e,de,l),s(e,C,l),s(e,ue,l),s(e,T,l),_e(T,Q),_e(T,Ge),r(L,T,null),s(e,be,l),s(e,R,l),s(e,Me,l),r(V,e,l),s(e,fe,l),s(e,k,l),s(e,ye,l),r(B,e,l),s(e,Te,l),r(H,e,l),s(e,he,l),s(e,E,l),s(e,we,l),s(e,N,l),s(e,ge,l),s(e,h,l),s(e,Ze,l),s(e,X,l),s(e,je,l),r(S,e,l),s(e,Je,l),s(e,q,l),Ue=!0},p:Pe,i(e){Ue||(d(w.$$.fragment,e),d(v.$$.fragment,e),d($.$$.fragment,e),d(_.$$.fragment,e),d(I.$$.fragment,e),d(x.$$.fragment,e),d(L.$$.fragment,e),d(V.$$.fragment,e),d(B.$$.fragment,e),d(H.$$.fragment,e),d(S.$$.fragment,e),Ue=!0)},o(e){u(w.$$.fragment,e),u(v.$$.fragment,e),u($.$$.fragment,e),u(_.$$.fragment,e),u(I.$$.fragment,e),u(x.$$.fragment,e),u(L.$$.fragment,e),u(V.$$.fragment,e),u(B.$$.fragment,e),u(H.$$.fragment,e),u(S.$$.fragment,e),Ue=!1},d(e){e&&(t(A),t(F),t(K),t(O),t(g),t(D),t(Z),t(ee),t(j),t(le),t(J),t(te),t(U),t(se),t(ne),t(f),t(oe),t(G),t(ae),t(ie),t(y),t(me),t(pe),t(W),t(ce),t(re),t(de),t(C),t(ue),t(T),t(be),t(R),t(Me),t(fe),t(k),t(ye),t(Te),t(he),t(E),t(we),t(N),t(ge),t(h),t(Ze),t(X),t(je),t(Je),t(q)),t(M),b(w,e),b(v,e),b($,e),b(_,e),b(I,e),b(x,e),b(L),b(V,e),b(B,e),b(H,e),b(S,e)}}}const nl='{"title":"在 LlamaIndex 中使用工具","local":"在-llamaindex-中使用工具","sections":[{"title":"创建FunctionTool","local":"创建functiontool","sections":[],"depth":2},{"title":"创建 QueryEngineTool","local":"创建-queryenginetool","sections":[],"depth":2},{"title":"创建 Toolspecs","local":"创建-toolspecs","sections":[],"depth":2},{"title":"实用工具","local":"实用工具","sections":[],"depth":2}],"depth":1}';function ol(We){return Ae(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class rl extends Ke{constructor(M){super(),Oe(this,M,ol,sl,ze,{})}}export{rl as component};
