import{s as _t,n as $t,o as xt}from"../chunks/scheduler.889ebb13.js";import{S as Bt,i as Ht,g as a,s as n,r as $,A as Vt,h as i,f as s,c as M,j as Et,u as x,x as T,k as Lt,y as Nt,a as e,v as B,d as H,t as V,w as N}from"../chunks/index.ca18042b.js";import{C as vt}from"../chunks/CodeBlock.a65374d4.js";import{H as bt,E as St}from"../chunks/index.544c5610.js";function Wt(Jt){let o,S,b,W,u,Q,J,jt='函数调用是<strong>大语言模型 (LLM) 对其环境采取行动的一种方式</strong>。它最初在 <a href="https://openai.com/index/function-calling-and-other-api-updates/" rel="nofollow">GPT-4中引入</a>，然后被其他模型复制。',z,j,pt="就像智能体 (Agent) 的工具一样，函数调用赋予了模型<strong>对其环境采取行动的能力</strong>。然而，函数调用能力是<strong>由模型学习的</strong>，并且<strong>比其他智能体技术更少依赖提示</strong>。",G,p,It="在第1单元中，智能体<strong>没有学习使用工具 (Tools)</strong>，我们只是提供了工具列表，并依赖模型<strong>能够泛化使用这些工具定义计划</strong>的事实。",P,I,rt="而在这里，<strong>通过函数调用，智能体被微调（训练）来使用工具</strong>。",Z,r,k,y,yt="在第1单元中，我们探讨了智能体的一般工作流程。一旦用户向智能体提供了一些工具并用查询提示它，模型将循环执行：",Y,c,ct="<li><em>思考(Think)</em>：为了实现目标，我需要采取什么行动。</li> <li><em>行动(Act)</em>：使用正确的参数格式化行动并停止生成。</li> <li><em>观察(Observe)</em>：从执行中获取结果。</li>",R,w,wt="在通过 API 与模型进行的”典型”对话中，对话将在用户和助手消息之间交替进行，如下所示：",O,A,F,m,At="函数调用为对话带来了<strong>新的角色</strong>！",D,U,mt="<li>一个用于 <strong>行动(Action)</strong> 的新角色</li> <li>一个用于 <strong>观察(Observation)</strong> 的新角色</li>",X,q,Ut='如果我们以 <a href="https://docs.mistral.ai/capabilities/function_calling/" rel="nofollow">Mistral API</a> 为例，它看起来像这样：',K,f,tt,h,qt="<p>…但你说函数调用有一个新角色？</p>",lt,C,ft="<strong>是也不是</strong>，在这种情况下和许多其他API中，模型将要采取的行动格式化为”助手”消息。聊天模板然后将此表示为函数调用的<strong>特殊词元 (special tokens)</strong>。",st,g,ht="<li><code>[AVAILABLE_TOOLS]</code> – 开始可用工具列表</li> <li><code>[/AVAILABLE_TOOLS]</code> – 结束可用工具列表</li> <li><code>[TOOL_CALLS]</code> – 调用工具（即采取”行动”）</li> <li><code>[TOOL_RESULTS]</code> – “观察”行动的结果</li> <li><code>[/TOOL_RESULTS]</code> – 观察结束（即模型可以再次解码）</li>",et,d,Ct='我们将在本课程中再次讨论函数调用，但如果你想深入了解，可以查看<a href="https://docs.mistral.ai/capabilities/function_calling/" rel="nofollow">这个优秀的文档部分</a>',nt,Mt,at,E,gt='现在我们已经了解了什么是函数调用以及它是如何工作的，让我们<strong>为一个尚未具有这些能力的模型添加一些函数调用功能</strong>：通过向模型添加一些新的特殊词元来增强： <a href="https://huggingface.co/google/gemma-2-2b-it" rel="nofollow">google/gemma-2-2b-it</a>。',it,L,dt="要能够做到这一点，<strong>我们首先需要理解微调和LoRA</strong>。",Tt,v,ot,_,ut;return u=new bt({props:{title:"什么是函数调用？(What is Function Calling?)",local:"什么是函数调用what-is-function-calling",headingTag:"h1"}}),r=new bt({props:{title:"模型如何”学习”采取行动？",local:"模型如何学习采取行动",headingTag:"h2"}}),A=new vt({props:{code:"Y29udmVyc2F0aW9uJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTdCJTIycm9sZSUyMiUzQSUyMCUyMnVzZXIlMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwJTIySSUyMG5lZWQlMjBoZWxwJTIwd2l0aCUyMG15JTIwb3JkZXIlMjIlN0QlMkMlMEElMjAlMjAlMjAlMjAlN0IlMjJyb2xlJTIyJTNBJTIwJTIyYXNzaXN0YW50JTIyJTJDJTIwJTIyY29udGVudCUyMiUzQSUyMCUyMkknZCUyMGJlJTIwaGFwcHklMjB0byUyMGhlbHAuJTIwQ291bGQlMjB5b3UlMjBwcm92aWRlJTIweW91ciUyMG9yZGVyJTIwbnVtYmVyJTNGJTIyJTdEJTJDJTBBJTIwJTIwJTIwJTIwJTdCJTIycm9sZSUyMiUzQSUyMCUyMnVzZXIlMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwJTIySXQncyUyME9SREVSLTEyMyUyMiU3RCUyQyUwQSU1RA==",highlighted:`conversation = [
    {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;I need help with my order&quot;</span>},
    {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;I&#x27;d be happy to help. Could you provide your order number?&quot;</span>},
    {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;It&#x27;s ORDER-123&quot;</span>},
]`,wrap:!1}}),f=new vt({props:{code:"Y29udmVyc2F0aW9uJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIycm9sZSUyMiUzQSUyMCUyMnVzZXIlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJjb250ZW50JTIyJTNBJTIwJTIyV2hhdCdzJTIwdGhlJTIwc3RhdHVzJTIwb2YlMjBteSUyMHRyYW5zYWN0aW9uJTIwVDEwMDElM0YlMjIlMEElMjAlMjAlMjAlMjAlN0QlMkMlMEElMjAlMjAlMjAlMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJyb2xlJTIyJTNBJTIwJTIyYXNzaXN0YW50JTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyY29udGVudCUyMiUzQSUyMCUyMiUyMiUyQyUwQSUyMCUyMCUy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N0QlMEElNUQ=",highlighted:`conversation = [
    {
        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>,
        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;What&#x27;s the status of my transaction T1001?&quot;</span>
    },
    {
        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>,
        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,
        <span class="hljs-string">&quot;function_call&quot;</span>: {
            <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;retrieve_payment_status&quot;</span>,
            <span class="hljs-string">&quot;arguments&quot;</span>: <span class="hljs-string">&quot;{\\&quot;transaction_id\\&quot;: \\&quot;T1001\\&quot;}&quot;</span>
        }
    },
    {
        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;tool&quot;</span>,
        <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;retrieve_payment_status&quot;</span>,
        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;{\\&quot;status\\&quot;: \\&quot;Paid\\&quot;}&quot;</span>
    },
    {
        <span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>,
        <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Your transaction T1001 has been successfully paid.&quot;</span>
    }
]`,wrap:!1}}),v=new St({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/zh-CN/bonus-unit1/what-is-function-calling.mdx"}}),{c(){o=a("meta"),S=n(),b=a("p"),W=n(),$(u.$$.fragment),Q=n(),J=a("p"),J.innerHTML=jt,z=n(),j=a("p"),j.innerHTML=pt,G=n(),p=a("p"),p.innerHTML=It,P=n(),I=a("p"),I.innerHTML=rt,Z=n(),$(r.$$.fragment),k=n(),y=a("p"),y.textContent=yt,Y=n(),c=a("ol"),c.innerHTML=ct,R=n(),w=a("p"),w.textContent=wt,O=n(),$(A.$$.fragment),F=n(),m=a("p"),m.innerHTML=At,D=n(),U=a("ol"),U.innerHTML=mt,X=n(),q=a("p"),q.innerHTML=Ut,K=n(),$(f.$$.fragment),tt=n(),h=a("blockquote"),h.innerHTML=qt,lt=n(),C=a("p"),C.innerHTML=ft,st=n(),g=a("ul"),g.innerHTML=ht,et=n(),d=a("p"),d.innerHTML=Ct,nt=n(),Mt=a("hr"),at=n(),E=a("p"),E.innerHTML=gt,it=n(),L=a("p"),L.innerHTML=dt,Tt=n(),$(v.$$.fragment),ot=n(),_=a("p"),this.h()},l(t){const l=Vt("svelte-u9bgzb",document.head);o=i(l,"META",{name:!0,content:!0}),l.forEach(s),S=M(t),b=i(t,"P",{}),Et(b).forEach(s),W=M(t),x(u.$$.fragment,t),Q=M(t),J=i(t,"P",{"data-svelte-h":!0}),T(J)!=="svelte-19ebsto"&&(J.innerHTML=jt),z=M(t),j=i(t,"P",{"data-svelte-h":!0}),T(j)!=="svelte-gq5voe"&&(j.innerHTML=pt),G=M(t),p=i(t,"P",{"data-svelte-h":!0}),T(p)!=="svelte-ulxmhv"&&(p.innerHTML=It),P=M(t),I=i(t,"P",{"data-svelte-h":!0}),T(I)!=="svelte-u9shua"&&(I.innerHTML=rt),Z=M(t),x(r.$$.fragment,t),k=M(t),y=i(t,"P",{"data-svelte-h":!0}),T(y)!=="svelte-z7sy4s"&&(y.textContent=yt),Y=M(t),c=i(t,"OL",{"data-svelte-h":!0}),T(c)!=="svelte-12q46c4"&&(c.innerHTML=ct),R=M(t),w=i(t,"P",{"data-svelte-h":!0}),T(w)!=="svelte-d5zh1u"&&(w.textContent=wt),O=M(t),x(A.$$.fragment,t),F=M(t),m=i(t,"P",{"data-svelte-h":!0}),T(m)!=="svelte-1olmnld"&&(m.innerHTML=At),D=M(t),U=i(t,"OL",{"data-svelte-h":!0}),T(U)!=="svelte-7l5xvn"&&(U.innerHTML=mt),X=M(t),q=i(t,"P",{"data-svelte-h":!0}),T(q)!=="svelte-6yaj69"&&(q.innerHTML=Ut),K=M(t),x(f.$$.fragment,t),tt=M(t),h=i(t,"BLOCKQUOTE",{"data-svelte-h":!0}),T(h)!=="svelte-19xgplh"&&(h.innerHTML=qt),lt=M(t),C=i(t,"P",{"data-svelte-h":!0}),T(C)!=="svelte-1c40gde"&&(C.innerHTML=ft),st=M(t),g=i(t,"UL",{"data-svelte-h":!0}),T(g)!=="svelte-1peyits"&&(g.innerHTML=ht),et=M(t),d=i(t,"P",{"data-svelte-h":!0}),T(d)!=="svelte-uqkf1r"&&(d.innerHTML=Ct),nt=M(t),Mt=i(t,"HR",{}),at=M(t),E=i(t,"P",{"data-svelte-h":!0}),T(E)!=="svelte-1njxg1"&&(E.innerHTML=gt),it=M(t),L=i(t,"P",{"data-svelte-h":!0}),T(L)!=="svelte-1qsyrf0"&&(L.innerHTML=dt),Tt=M(t),x(v.$$.fragment,t),ot=M(t),_=i(t,"P",{}),Et(_).forEach(s),this.h()},h(){Lt(o,"name","hf:doc:metadata"),Lt(o,"content",Qt)},m(t,l){Nt(document.head,o),e(t,S,l),e(t,b,l),e(t,W,l),B(u,t,l),e(t,Q,l),e(t,J,l),e(t,z,l),e(t,j,l),e(t,G,l),e(t,p,l),e(t,P,l),e(t,I,l),e(t,Z,l),B(r,t,l),e(t,k,l),e(t,y,l),e(t,Y,l),e(t,c,l),e(t,R,l),e(t,w,l),e(t,O,l),B(A,t,l),e(t,F,l),e(t,m,l),e(t,D,l),e(t,U,l),e(t,X,l),e(t,q,l),e(t,K,l),B(f,t,l),e(t,tt,l),e(t,h,l),e(t,lt,l),e(t,C,l),e(t,st,l),e(t,g,l),e(t,et,l),e(t,d,l),e(t,nt,l),e(t,Mt,l),e(t,at,l),e(t,E,l),e(t,it,l),e(t,L,l),e(t,Tt,l),B(v,t,l),e(t,ot,l),e(t,_,l),ut=!0},p:$t,i(t){ut||(H(u.$$.fragment,t),H(r.$$.fragment,t),H(A.$$.fragment,t),H(f.$$.fragment,t),H(v.$$.fragment,t),ut=!0)},o(t){V(u.$$.fragment,t),V(r.$$.fragment,t),V(A.$$.fragment,t),V(f.$$.fragment,t),V(v.$$.fragment,t),ut=!1},d(t){t&&(s(S),s(b),s(W),s(Q),s(J),s(z),s(j),s(G),s(p),s(P),s(I),s(Z),s(k),s(y),s(Y),s(c),s(R),s(w),s(O),s(F),s(m),s(D),s(U),s(X),s(q),s(K),s(tt),s(h),s(lt),s(C),s(st),s(g),s(et),s(d),s(nt),s(Mt),s(at),s(E),s(it),s(L),s(Tt),s(ot),s(_)),s(o),N(u,t),N(r,t),N(A,t),N(f,t),N(v,t)}}}const Qt='{"title":"什么是函数调用？(What is Function Calling?)","local":"什么是函数调用what-is-function-calling","sections":[{"title":"模型如何”学习”采取行动？","local":"模型如何学习采取行动","sections":[],"depth":2}],"depth":1}';function zt(Jt){return xt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yt extends Bt{constructor(o){super(),Ht(this,o,zt,Wt,_t,{})}}export{Yt as component};
