import{s as Cl,n as bl,o as dl}from"../chunks/scheduler.37c15a92.js";import{S as vl,i as _l,g as n,s as t,r as O,A as Zl,h as s,f as J,c as U,j as hl,u as M,x as A,k as il,y as Hl,a as e,v as g,d as a,t as y,w as o}from"../chunks/index.2bf4358c.js";import{C as yl}from"../chunks/CodeBlock.4e987730.js";import{C as Nl}from"../chunks/CourseFloatingBanner.6add7356.js";import{H as ol}from"../chunks/Heading.8ada512a.js";import{E as Bl}from"../chunks/getInferenceSnippets.0aa3753f.js";function Gl(wl){let i,q,B,x,j,E,I,L,w,jl='<p>您可以通过 <a href="https://huggingface.co/agents-course/notebooks/blob/main/unit2/smolagents/tool_calling_agents.ipynb" target="_blank">此 Notebook</a> 跟随代码实践，该文件支持在 Google Colab 中运行。</p>',S,k,Il="工具调用智能体（Tool Calling Agents）是 <code>smolagents</code> 中提供的第二种智能体类型。与使用 Python 代码片段的代码智能体（Code Agents）不同，这类智能体<strong>利用 LLM 提供商的内置工具调用能力</strong>来生成 <strong>JSON 结构</strong>的工具调用指令。这是 OpenAI、Anthropic 等主流提供商采用的标准方法。",F,p,kl="当 Alfred 想要搜索餐饮服务和派对创意时，<code>CodeAgent</code> 会生成并运行如下 Python 代码：",V,r,W,Q,pl="而 <code>ToolCallingAgent</code> 则会创建 JSON 结构：",X,m,Y,R,rl="该 JSON 结构随后会被用于执行工具调用。",P,u,Ql='尽管 <code>smolagents</code> 主要专注于 <code>CodeAgents</code>（因为<a href="https://huggingface.co/papers/2402.01030" rel="nofollow">它们整体表现更好</a>），但对于不需要变量处理或复杂工具调用的简单系统，<code>ToolCallingAgents</code> 仍然可以高效工作。',z,D,ml='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/code_vs_json_actions.png" alt="代码操作与 JSON 操作对比"/>',K,c,ll,f,Rl='工具调用智能体遵循与代码智能体相同的多步骤工作流程（详见<a href="./code_agents">前一章节</a>）。关键区别在于<strong>操作结构方式</strong>：智能体不再生成可执行代码，而是<strong>生成指定工具名称和参数的 JSON 对象</strong>，系统随后<strong>解析这些指令</strong>来执行相应工具。',Tl,$,Jl,h,ul="让我们重新审视 Alfred 开始筹备派对的示例，这次使用 <code>ToolCallingAgent</code> 来展示区别。我们将构建一个能够使用 DuckDuckGo 进行网页搜索的智能体，与代码智能体示例的唯一区别在于智能体类型——框架会处理其他所有细节：",el,C,tl,b,Dl="当查看智能体的执行跟踪时，您将看到类似以下内容而非 <code>Executing parsed code:</code>：",Ul,d,nl,v,cl="智能体生成结构化的工具调用指令，系统通过处理这些指令来生成输出，而非像 <code>CodeAgent</code> 那样直接执行代码。",sl,_,fl="现在我们已经了解两种智能体类型，可以根据需求选择合适的一种。让我们继续探索 <code>smolagents</code>，让 Alfred 的派对大获成功！🎉",Al,Z,Ol,H,$l='<li><a href="https://huggingface.co/docs/smolagents/v1.8.1/en/reference/agents#smolagents.ToolCallingAgent" rel="nofollow">ToolCallingAgent 文档</a> - ToolCallingAgent 的官方文档</li>',Ml,N,gl,G,al;return j=new Nl({props:{chapter:2,classNames:"absolute z-10 right-0 top-0",notebooks:[{label:"Google Colab",value:"https://colab.research.google.com/#fileId=https://huggingface.co/agents-course/notebooks/blob/main/unit2/smolagents/tool_calling_agents.ipynb"}]}}),I=new ol({props:{title:"将操作编写为代码片段或 JSON 结构",local:"将操作编写为代码片段或-json-结构",headingTag:"h1"}}),r=new yl({props:{code:"Zm9yJTIwcXVlcnklMjBpbiUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMkJlc3QlMjBjYXRlcmluZyUyMHNlcnZpY2VzJTIwaW4lMjBHb3RoYW0lMjBDaXR5JTIyJTJDJTIwJTBBJTIwJTIwJTIwJTIwJTIyUGFydHklMjB0aGVtZSUyMGlkZWFzJTIwZm9yJTIwc3VwZXJoZXJvZXMlMjIlMEElNUQlM0ElMEElMjAlMjAlMjAlMjBwcmludCh3ZWJfc2VhcmNoKGYlMjJTZWFyY2glMjBmb3IlM0ElMjAlN0JxdWVyeSU3RCUyMikp",highlighted:`<span class="hljs-keyword">for</span> query <span class="hljs-keyword">in</span> [
    <span class="hljs-string">&quot;Best catering services in Gotham City&quot;</span>, 
    <span class="hljs-string">&quot;Party theme ideas for superheroes&quot;</span>
]:
    <span class="hljs-built_in">print</span>(web_search(<span class="hljs-string">f&quot;Search for: <span class="hljs-subst">{query}</span>&quot;</span>))`,wrap:!1}}),m=new yl({props:{code:"JTVCJTBBJTIwJTIwJTIwJTIwJTdCJTIybmFtZSUyMiUzQSUyMCUyMndlYl9zZWFyY2glMjIlMkMlMjAlMjJhcmd1bWVudHMlMjIlM0ElMjAlMjJCZXN0JTIwY2F0ZXJpbmclMjBzZXJ2aWNlcyUyMGluJTIwR290aGFtJTIwQ2l0eSUyMiU3RCUyQyUwQSUyMCUyMCUyMCUyMCU3QiUyMm5hbWUlMjIlM0ElMjAlMjJ3ZWJfc2VhcmNoJTIyJTJDJTIwJTIyYXJndW1lbnRzJTIyJTNBJTIwJTIyUGFydHklMjB0aGVtZSUyMGlkZWFzJTIwZm9yJTIwc3VwZXJoZXJvZXMlMjIlN0QlMEElNUQ=",highlighted:`[
    {<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;web_search&quot;</span>, <span class="hljs-string">&quot;arguments&quot;</span>: <span class="hljs-string">&quot;Best catering services in Gotham City&quot;</span>},
    {<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;web_search&quot;</span>, <span class="hljs-string">&quot;arguments&quot;</span>: <span class="hljs-string">&quot;Party theme ideas for superheroes&quot;</span>}
]`,wrap:!1}}),c=new ol({props:{title:"工具调用智能体的工作原理",local:"工具调用智能体的工作原理",headingTag:"h2"}}),$=new ol({props:{title:"示例：运行工具调用智能体",local:"示例运行工具调用智能体",headingTag:"h2"}}),C=new yl({props:{code:"ZnJvbSUyMHNtb2xhZ2VudHMlMjBpbXBvcnQlMjBUb29sQ2FsbGluZ0FnZW50JTJDJTIwRHVja0R1Y2tHb1NlYXJjaFRvb2wlMkMlMjBJbmZlcmVuY2VDbGllbnRNb2RlbCUwQSUwQWFnZW50JTIwJTNEJTIwVG9vbENhbGxpbmdBZ2VudCh0b29scyUzRCU1QkR1Y2tEdWNrR29TZWFyY2hUb29sKCklNUQlMkMlMjBtb2RlbCUzREluZmVyZW5jZUNsaWVudE1vZGVsKCkpJTBBJTBBYWdlbnQucnVuKCUyMlNlYXJjaCUyMGZvciUyMHRoZSUyMGJlc3QlMjBtdXNpYyUyMHJlY29tbWVuZGF0aW9ucyUyMGZvciUyMGElMjBwYXJ0eSUyMGF0JTIwdGhlJTIwV2F5bmUncyUyMG1hbnNpb24uJTIyKQ==",highlighted:`<span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> ToolCallingAgent, DuckDuckGoSearchTool, InferenceClientModel

agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=InferenceClientModel())

agent.run(<span class="hljs-string">&quot;Search for the best music recommendations for a party at the Wayne&#x27;s mansion.&quot;</span>)`,wrap:!1}}),d=new yl({props:{code:"JUUyJTk1JUFEJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJTk0JTgwJUUyJ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lRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTQlODAlRTIlOTUlQUY=",highlighted:`╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Calling tool: &#x27;web_search&#x27; with arguments: {&#x27;query&#x27;: &quot;best music recommendations for a party at Wayne&#x27;s         │
│ mansion&quot;}                                                                                                       │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯`,wrap:!1}}),Z=new ol({props:{title:"资源",local:"资源",headingTag:"h2"}}),N=new Bl({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/zh-CN/unit2/smolagents/tool_calling_agents.mdx"}}),{c(){i=n("meta"),q=t(),B=n("p"),x=t(),O(j.$$.fragment),E=t(),O(I.$$.fragment),L=t(),w=n("blockquote"),w.innerHTML=jl,S=t(),k=n("p"),k.innerHTML=Il,F=t(),p=n("p"),p.innerHTML=kl,V=t(),O(r.$$.fragment),W=t(),Q=n("p"),Q.innerHTML=pl,X=t(),O(m.$$.fragment),Y=t(),R=n("p"),R.textContent=rl,P=t(),u=n("p"),u.innerHTML=Ql,z=t(),D=n("p"),D.innerHTML=ml,K=t(),O(c.$$.fragment),ll=t(),f=n("p"),f.innerHTML=Rl,Tl=t(),O($.$$.fragment),Jl=t(),h=n("p"),h.innerHTML=ul,el=t(),O(C.$$.fragment),tl=t(),b=n("p"),b.innerHTML=Dl,Ul=t(),O(d.$$.fragment),nl=t(),v=n("p"),v.innerHTML=cl,sl=t(),_=n("p"),_.innerHTML=fl,Al=t(),O(Z.$$.fragment),Ol=t(),H=n("ul"),H.innerHTML=$l,Ml=t(),O(N.$$.fragment),gl=t(),G=n("p"),this.h()},l(l){const T=Zl("svelte-u9bgzb",document.head);i=s(T,"META",{name:!0,content:!0}),T.forEach(J),q=U(l),B=s(l,"P",{}),hl(B).forEach(J),x=U(l),M(j.$$.fragment,l),E=U(l),M(I.$$.fragment,l),L=U(l),w=s(l,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),A(w)!=="svelte-1v15z6e"&&(w.innerHTML=jl),S=U(l),k=s(l,"P",{"data-svelte-h":!0}),A(k)!=="svelte-4h5a59"&&(k.innerHTML=Il),F=U(l),p=s(l,"P",{"data-svelte-h":!0}),A(p)!=="svelte-1uk3til"&&(p.innerHTML=kl),V=U(l),M(r.$$.fragment,l),W=U(l),Q=s(l,"P",{"data-svelte-h":!0}),A(Q)!=="svelte-1ly2vr7"&&(Q.innerHTML=pl),X=U(l),M(m.$$.fragment,l),Y=U(l),R=s(l,"P",{"data-svelte-h":!0}),A(R)!=="svelte-2w7dg0"&&(R.textContent=rl),P=U(l),u=s(l,"P",{"data-svelte-h":!0}),A(u)!=="svelte-1dhxpic"&&(u.innerHTML=Ql),z=U(l),D=s(l,"P",{"data-svelte-h":!0}),A(D)!=="svelte-kkam5r"&&(D.innerHTML=ml),K=U(l),M(c.$$.fragment,l),ll=U(l),f=s(l,"P",{"data-svelte-h":!0}),A(f)!=="svelte-1k6u3aa"&&(f.innerHTML=Rl),Tl=U(l),M($.$$.fragment,l),Jl=U(l),h=s(l,"P",{"data-svelte-h":!0}),A(h)!=="svelte-1mi69km"&&(h.innerHTML=ul),el=U(l),M(C.$$.fragment,l),tl=U(l),b=s(l,"P",{"data-svelte-h":!0}),A(b)!=="svelte-11effay"&&(b.innerHTML=Dl),Ul=U(l),M(d.$$.fragment,l),nl=U(l),v=s(l,"P",{"data-svelte-h":!0}),A(v)!=="svelte-oldvrt"&&(v.innerHTML=cl),sl=U(l),_=s(l,"P",{"data-svelte-h":!0}),A(_)!=="svelte-an91op"&&(_.innerHTML=fl),Al=U(l),M(Z.$$.fragment,l),Ol=U(l),H=s(l,"UL",{"data-svelte-h":!0}),A(H)!=="svelte-1b0h5wy"&&(H.innerHTML=$l),Ml=U(l),M(N.$$.fragment,l),gl=U(l),G=s(l,"P",{}),hl(G).forEach(J),this.h()},h(){il(i,"name","hf:doc:metadata"),il(i,"content",ql),il(w,"class","tip")},m(l,T){Hl(document.head,i),e(l,q,T),e(l,B,T),e(l,x,T),g(j,l,T),e(l,E,T),g(I,l,T),e(l,L,T),e(l,w,T),e(l,S,T),e(l,k,T),e(l,F,T),e(l,p,T),e(l,V,T),g(r,l,T),e(l,W,T),e(l,Q,T),e(l,X,T),g(m,l,T),e(l,Y,T),e(l,R,T),e(l,P,T),e(l,u,T),e(l,z,T),e(l,D,T),e(l,K,T),g(c,l,T),e(l,ll,T),e(l,f,T),e(l,Tl,T),g($,l,T),e(l,Jl,T),e(l,h,T),e(l,el,T),g(C,l,T),e(l,tl,T),e(l,b,T),e(l,Ul,T),g(d,l,T),e(l,nl,T),e(l,v,T),e(l,sl,T),e(l,_,T),e(l,Al,T),g(Z,l,T),e(l,Ol,T),e(l,H,T),e(l,Ml,T),g(N,l,T),e(l,gl,T),e(l,G,T),al=!0},p:bl,i(l){al||(a(j.$$.fragment,l),a(I.$$.fragment,l),a(r.$$.fragment,l),a(m.$$.fragment,l),a(c.$$.fragment,l),a($.$$.fragment,l),a(C.$$.fragment,l),a(d.$$.fragment,l),a(Z.$$.fragment,l),a(N.$$.fragment,l),al=!0)},o(l){y(j.$$.fragment,l),y(I.$$.fragment,l),y(r.$$.fragment,l),y(m.$$.fragment,l),y(c.$$.fragment,l),y($.$$.fragment,l),y(C.$$.fragment,l),y(d.$$.fragment,l),y(Z.$$.fragment,l),y(N.$$.fragment,l),al=!1},d(l){l&&(J(q),J(B),J(x),J(E),J(L),J(w),J(S),J(k),J(F),J(p),J(V),J(W),J(Q),J(X),J(Y),J(R),J(P),J(u),J(z),J(D),J(K),J(ll),J(f),J(Tl),J(Jl),J(h),J(el),J(tl),J(b),J(Ul),J(nl),J(v),J(sl),J(_),J(Al),J(Ol),J(H),J(Ml),J(gl),J(G)),J(i),o(j,l),o(I,l),o(r,l),o(m,l),o(c,l),o($,l),o(C,l),o(d,l),o(Z,l),o(N,l)}}}const ql='{"title":"将操作编写为代码片段或 JSON 结构","local":"将操作编写为代码片段或-json-结构","sections":[{"title":"工具调用智能体的工作原理","local":"工具调用智能体的工作原理","sections":[],"depth":2},{"title":"示例：运行工具调用智能体","local":"示例运行工具调用智能体","sections":[],"depth":2},{"title":"资源","local":"资源","sections":[],"depth":2}],"depth":1}';function xl(wl){return dl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xl extends vl{constructor(i){super(),_l(this,i,xl,Gl,Cl,{})}}export{Xl as component};
