import{s as _t,n as xt,o as Ht}from"../chunks/scheduler.505acc25.js";import{S as Bt,i as St,e as i,s,c as $,h as Vt,a as o,d as l,b as a,f as Et,g as k,j as M,k as Lt,l as Wt,m as n,n as _,t as x,o as H,p as B}from"../chunks/index.fbb49fb4.js";import{C as Nt}from"../chunks/CopyLLMTxtMenu.564b44fd.js";import{C as $t}from"../chunks/CodeBlock.7f0851b7.js";import{H as kt}from"../chunks/Heading.511e0027.js";import{E as Qt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8c19e80e.js";function Gt(ct){let r,W,S,N,u,Q,T,G,p,Jt='Function-calling is a <strong>way for an LLM to take actions on its environment</strong>. It was first <a href="https://openai.com/index/function-calling-and-other-api-updates/" rel="nofollow">introduced in GPT-4</a>, and was later reproduced in other models.',z,c,jt="Just like the tools of an Agent, function-calling gives the model the capacity to <strong>take an action on its environment</strong>. However, the function calling capacity <strong>is learned by the model</strong>, and relies <strong>less on prompting than other agents techniques</strong>.",P,J,ht="During Unit 1, the Agent <strong>didn’t learn to use the Tools</strong>, we just provided the list, and we relied on the fact that the model <strong>was able to generalize on defining a plan using these Tools</strong>.",Z,j,wt="While here, <strong>with function-calling, the Agent is fine-tuned (trained) to use Tools</strong>.",O,h,Y,w,It="In Unit 1, we explored the general workflow of an agent. Once the user has given some tools to the agent and prompted it with a query, the model will cycle through:",R,I,mt="<li><em>Think</em> : What action(s) do I need to take in order to fulfill the objective.</li> <li><em>Act</em> : Format the action with the correct parameter and stop the generation.</li> <li><em>Observe</em> : Get back the result from the execution.</li>",F,m,yt="In a “typical” conversation with a model through an API, the conversation will alternate between user and assistant messages like this:",D,y,X,d,dt="Function-calling brings <strong>new roles to the conversation</strong>!",K,f,ft="<li>One new role for an <strong>Action</strong></li> <li>One new role for an <strong>Observation</strong></li>",tt,g,gt='If we take the <a href="https://docs.mistral.ai/capabilities/function_calling/" rel="nofollow">Mistral API</a> as an example, it would look like this:',et,A,lt,U,At="<p>… But you said there’s a new role for function calls?</p>",nt,q,Ut="<strong>Yes and no</strong>, in this case and in a lot of other APIs, the model formats the action to take as an “assistant” message. The chat template will then represent this as <strong>special tokens</strong> for function-calling.",st,C,qt="<li><code>[AVAILABLE_TOOLS]</code> – Start the list of available tools</li> <li><code>[/AVAILABLE_TOOLS]</code> – End the list of available tools</li> <li><code>[TOOL_CALLS]</code> – Make a call to a tool (i.e., take an “Action”)</li> <li><code>[TOOL_RESULTS]</code> – “Observe” the result of the action</li> <li><code>[/TOOL_RESULTS]</code> – End of the observation (i.e., the model can decode again)</li>",at,b,Ct='We’ll talk again about function-calling in this course, but if you want to dive deeper you can check <a href="https://docs.mistral.ai/capabilities/function_calling/" rel="nofollow">this excellent documentation section</a>.',it,ot,Mt,v,bt='Now that we learned what function-calling is and how it works, let’s <strong>add some function-calling capabilities to a model that does not have those capacities yet</strong>: <a href="https://huggingface.co/google/gemma-2-2b-it" rel="nofollow">google/gemma-2-2b-it</a>, by appending some new special tokens to the model.',rt,E,vt="To be able to do that, <strong>we need first to understand fine-tuning and LoRA</strong>.",ut,L,Tt,V,pt;return u=new Nt({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),T=new kt({props:{title:"What is Function Calling?",local:"what-is-function-calling",headingTag:"h1"}}),h=new kt({props:{title:"How does the model “learn” to take an action?",local:"how-does-the-model-learn-to-take-an-action",headingTag:"h2"}}),y=new $t({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}}),A=new $t({props:{code:"Y29udmVyc2F0aW9uJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIycm9sZSUyMiUzQSUyMCUyMnVzZXIlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJjb250ZW50JTIyJTNBJTIwJTIyV2hhdCdzJTIwdGhlJTIwc3RhdHVzJTIwb2YlMjBteSUyMHRyYW5zYWN0aW9uJTIwVDEwMDElM0YlMjIlMEElMjAlMjAlMjAlMjAlN0QlMkMlMEElMjAlMjAlMjAlMjAlN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJyb2xlJTIyJTNBJTIwJTIyYXNzaXN0YW50JTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyY29udGVudC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Gx5JTIwcGFpZC4lMjIlMEElMjAlMjAlMjAlMjAlN0QlMEElNUQ=",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}}),L=new Qt({props:{source:"https://github.com/huggingface/agents-course/blob/main/units/en/bonus-unit1/what-is-function-calling.mdx"}}),{c(){r=i("meta"),W=s(),S=i("p"),N=s(),$(u.$$.fragment),Q=s(),$(T.$$.fragment),G=s(),p=i("p"),p.innerHTML=Jt,z=s(),c=i("p"),c.innerHTML=jt,P=s(),J=i("p"),J.innerHTML=ht,Z=s(),j=i("p"),j.innerHTML=wt,O=s(),$(h.$$.fragment),Y=s(),w=i("p"),w.textContent=It,R=s(),I=i("ol"),I.innerHTML=mt,F=s(),m=i("p"),m.textContent=yt,D=s(),$(y.$$.fragment),X=s(),d=i("p"),d.innerHTML=dt,K=s(),f=i("ol"),f.innerHTML=ft,tt=s(),g=i("p"),g.innerHTML=gt,et=s(),$(A.$$.fragment),lt=s(),U=i("blockquote"),U.innerHTML=At,nt=s(),q=i("p"),q.innerHTML=Ut,st=s(),C=i("ul"),C.innerHTML=qt,at=s(),b=i("p"),b.innerHTML=Ct,it=s(),ot=i("hr"),Mt=s(),v=i("p"),v.innerHTML=bt,rt=s(),E=i("p"),E.innerHTML=vt,ut=s(),$(L.$$.fragment),Tt=s(),V=i("p"),this.h()},l(t){const e=Vt("svelte-u9bgzb",document.head);r=o(e,"META",{name:!0,content:!0}),e.forEach(l),W=a(t),S=o(t,"P",{}),Et(S).forEach(l),N=a(t),k(u.$$.fragment,t),Q=a(t),k(T.$$.fragment,t),G=a(t),p=o(t,"P",{"data-svelte-h":!0}),M(p)!=="svelte-pk3h6c"&&(p.innerHTML=Jt),z=a(t),c=o(t,"P",{"data-svelte-h":!0}),M(c)!=="svelte-hycf5a"&&(c.innerHTML=jt),P=a(t),J=o(t,"P",{"data-svelte-h":!0}),M(J)!=="svelte-1i26jpw"&&(J.innerHTML=ht),Z=a(t),j=o(t,"P",{"data-svelte-h":!0}),M(j)!=="svelte-1mmqpap"&&(j.innerHTML=wt),O=a(t),k(h.$$.fragment,t),Y=a(t),w=o(t,"P",{"data-svelte-h":!0}),M(w)!=="svelte-qairlh"&&(w.textContent=It),R=a(t),I=o(t,"OL",{"data-svelte-h":!0}),M(I)!=="svelte-1kl2016"&&(I.innerHTML=mt),F=a(t),m=o(t,"P",{"data-svelte-h":!0}),M(m)!=="svelte-8qa297"&&(m.textContent=yt),D=a(t),k(y.$$.fragment,t),X=a(t),d=o(t,"P",{"data-svelte-h":!0}),M(d)!=="svelte-1khjwbm"&&(d.innerHTML=dt),K=a(t),f=o(t,"OL",{"data-svelte-h":!0}),M(f)!=="svelte-1g71ln6"&&(f.innerHTML=ft),tt=a(t),g=o(t,"P",{"data-svelte-h":!0}),M(g)!=="svelte-4i97j7"&&(g.innerHTML=gt),et=a(t),k(A.$$.fragment,t),lt=a(t),U=o(t,"BLOCKQUOTE",{"data-svelte-h":!0}),M(U)!=="svelte-1ti28mk"&&(U.innerHTML=At),nt=a(t),q=o(t,"P",{"data-svelte-h":!0}),M(q)!=="svelte-avxmc3"&&(q.innerHTML=Ut),st=a(t),C=o(t,"UL",{"data-svelte-h":!0}),M(C)!=="svelte-1yz4fi6"&&(C.innerHTML=qt),at=a(t),b=o(t,"P",{"data-svelte-h":!0}),M(b)!=="svelte-grdf5"&&(b.innerHTML=Ct),it=a(t),ot=o(t,"HR",{}),Mt=a(t),v=o(t,"P",{"data-svelte-h":!0}),M(v)!=="svelte-pjg6sa"&&(v.innerHTML=bt),rt=a(t),E=o(t,"P",{"data-svelte-h":!0}),M(E)!=="svelte-m1lnb4"&&(E.innerHTML=vt),ut=a(t),k(L.$$.fragment,t),Tt=a(t),V=o(t,"P",{}),Et(V).forEach(l),this.h()},h(){Lt(r,"name","hf:doc:metadata"),Lt(r,"content",zt)},m(t,e){Wt(document.head,r),n(t,W,e),n(t,S,e),n(t,N,e),_(u,t,e),n(t,Q,e),_(T,t,e),n(t,G,e),n(t,p,e),n(t,z,e),n(t,c,e),n(t,P,e),n(t,J,e),n(t,Z,e),n(t,j,e),n(t,O,e),_(h,t,e),n(t,Y,e),n(t,w,e),n(t,R,e),n(t,I,e),n(t,F,e),n(t,m,e),n(t,D,e),_(y,t,e),n(t,X,e),n(t,d,e),n(t,K,e),n(t,f,e),n(t,tt,e),n(t,g,e),n(t,et,e),_(A,t,e),n(t,lt,e),n(t,U,e),n(t,nt,e),n(t,q,e),n(t,st,e),n(t,C,e),n(t,at,e),n(t,b,e),n(t,it,e),n(t,ot,e),n(t,Mt,e),n(t,v,e),n(t,rt,e),n(t,E,e),n(t,ut,e),_(L,t,e),n(t,Tt,e),n(t,V,e),pt=!0},p:xt,i(t){pt||(x(u.$$.fragment,t),x(T.$$.fragment,t),x(h.$$.fragment,t),x(y.$$.fragment,t),x(A.$$.fragment,t),x(L.$$.fragment,t),pt=!0)},o(t){H(u.$$.fragment,t),H(T.$$.fragment,t),H(h.$$.fragment,t),H(y.$$.fragment,t),H(A.$$.fragment,t),H(L.$$.fragment,t),pt=!1},d(t){t&&(l(W),l(S),l(N),l(Q),l(G),l(p),l(z),l(c),l(P),l(J),l(Z),l(j),l(O),l(Y),l(w),l(R),l(I),l(F),l(m),l(D),l(X),l(d),l(K),l(f),l(tt),l(g),l(et),l(lt),l(U),l(nt),l(q),l(st),l(C),l(at),l(b),l(it),l(ot),l(Mt),l(v),l(rt),l(E),l(ut),l(Tt),l(V)),l(r),B(u,t),B(T,t),B(h,t),B(y,t),B(A,t),B(L,t)}}}const zt='{"title":"What is Function Calling?","local":"what-is-function-calling","sections":[{"title":"How does the model “learn” to take an action?","local":"how-does-the-model-learn-to-take-an-action","sections":[],"depth":2}],"depth":1}';function Pt(ct){return Ht(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xt extends Bt{constructor(r){super(),St(this,r,Pt,Gt,_t,{})}}export{Xt as component};
