Buckets:

HuggingFaceDocBuilder's picture
download
raw
36.9 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Créons notre premier agent avec smolagents&quot;,&quot;local&quot;:&quot;créons-notre-premier-agent-avec-smolagents&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Qu’est-ce que smolagents ?&quot;,&quot;local&quot;:&quot;quest-ce-que-smolagents-&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Construisons notre agent !&quot;,&quot;local&quot;:&quot;construisons-notre-agent-&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Les outils&quot;,&quot;local&quot;:&quot;les-outils&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;L’agent&quot;,&quot;local&quot;:&quot;lagent&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Le prompt système&quot;,&quot;local&quot;:&quot;le-prompt-système&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/agents-course/pr_673/fr/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/entry/start.6a3cabb6.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/scheduler.505acc25.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/singletons.f87ed60a.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/index.001f95d5.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/paths.cc8cef00.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/entry/app.bbc296eb.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/preload-helper.f888915a.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/index.b1175496.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/nodes/0.8f31a791.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/nodes/33.bbe84559.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/Heading.aedc5b40.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.11cb69c9.js">
<link rel="modulepreload" href="/docs/agents-course/pr_673/fr/_app/immutable/chunks/CodeBlock.91f006af.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Créons notre premier agent avec smolagents&quot;,&quot;local&quot;:&quot;créons-notre-premier-agent-avec-smolagents&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Qu’est-ce que smolagents ?&quot;,&quot;local&quot;:&quot;quest-ce-que-smolagents-&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Construisons notre agent !&quot;,&quot;local&quot;:&quot;construisons-notre-agent-&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Les outils&quot;,&quot;local&quot;:&quot;les-outils&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;L’agent&quot;,&quot;local&quot;:&quot;lagent&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Le prompt système&quot;,&quot;local&quot;:&quot;le-prompt-système&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="créons-notre-premier-agent-avec-smolagents" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#créons-notre-premier-agent-avec-smolagents"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Créons notre premier agent avec smolagents</span></h1> <p data-svelte-h="svelte-qs4xjg">Dans la section précédente, nous avons appris comment créer des agents à partir de zéro en utilisant du code Python, et nous avons <strong>vu à quel point ce processus peut être fastidieux</strong>. Heureusement, de nombreuses bibliothèques d’agents simplifient ce travail en <strong>se chargeant de la majeure partie du travail lourd pour vous</strong>.</p> <p data-svelte-h="svelte-1q75a4l">Dans ce tutoriel, <strong>vous allez créer votre tout premier agent</strong> capable d’exécuter des actions telles que la génération d’images, la recherche sur le web, la vérification de fuseaux horaires et bien plus encore !</p> <p data-svelte-h="svelte-1ywod5z">Vous publierez également votre agent <strong>sur un <em>Space</em> Hugging Face afin de le partager avec vos amis et collègues</strong>.</p> <p data-svelte-h="svelte-13o0trz">C’est parti !</p> <h2 class="relative group"><a id="quest-ce-que-smolagents-" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#quest-ce-que-smolagents-"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Qu’est-ce que smolagents ?</span></h2> <img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit1/smolagents.png" alt="smolagents"> <p data-svelte-h="svelte-dbtthn">Pour créer cet agent, nous allons utiliser <code>smolagents</code>, une bibliothèque qui <strong>fournit un cadre facilitant le développement d’agents</strong>.</p> <p data-svelte-h="svelte-1bcgvwk">Cette bibliothèque légère est conçue pour être simple, tout en masquant une grande partie de la complexité liée à la construction d’un agent, permettant ainsi de vous concentrer sur la conception du comportement de l’agent.</p> <p data-svelte-h="svelte-1yc3dh6">Nous approfondirons smolagents dans la prochaine unité. En attendant, vous pouvez également consulter cet <a href="https://huggingface.co/blog/smolagents" target="_blank">article de blog</a> ou le <a href="https://github.com/huggingface/smolagents" target="_blank">dépôt GitHub</a> de la bibliothèque.</p> <p data-svelte-h="svelte-a02t0l">Brièvement, <code>smolagents</code> est une bibliothèque se concentrant sur les <strong>agents générant du code</strong> (via la classe <code>CodeAgent</code>), un type d’agent qui exécute des <strong>“actions”</strong> via des blocs de code, puis <strong>“observe”</strong> les résultats en exécutant le code.</p> <p data-svelte-h="svelte-x6axr8">Voici un exemple de ce que nous allons construire !</p> <p data-svelte-h="svelte-8v2mq6">Nous avons équipé notre agent d’un <strong>outil de génération d’images</strong> et lui avons demandé de générer une image d’un chat.</p> <p data-svelte-h="svelte-jhlkvg">L’agent dans <code>smolagents</code> aura les <strong>mêmes comportements que celui personnalisé que nous avons construit précédemment</strong> : il va <strong>réfléchir, agir et observer cycliquement</strong> jusqu’à parvenir à une réponse finale :</p> <iframe width="560" height="315" src="https://www.youtube.com/embed/PQDKcWiuln4?si=ysSTDZoi8y55FVvA" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen=""></iframe> <p data-svelte-h="svelte-1gth0ux">Excitant, n’est-ce pas ?</p> <h2 class="relative group"><a id="construisons-notre-agent-" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#construisons-notre-agent-"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Construisons notre agent !</span></h2> <p data-svelte-h="svelte-1nhw6j">Pour commencer, dupliquez ce <em>Space</em> : <a href="https://huggingface.co/spaces/agents-course/First_agent_template" target="_blank">https://huggingface.co/spaces/agents-course/First_agent_template</a></p> <blockquote data-svelte-h="svelte-17wqgsx"><p>Merci à <a href="https://huggingface.co/m-ric" target="_blank">Aymeric</a> pour ce patron ! 🙌</p></blockquote> <p data-svelte-h="svelte-a592en">Dupliquer signifie <strong>créer une copie locale sur votre propre profil</strong> :</p> <img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit1/duplicate-space.gif" alt="Duplicate"> <p data-svelte-h="svelte-13lr74u">Après la duplication, vous devrez ajouter votre <em>token</em> d’API Hugging Face pour que votre agent puisse accéder à l’API du modèle :</p> <ol data-svelte-h="svelte-1rl24xt"><li>Tout d’abord, obtenez votre <em>token</em> Hugging Face sur <a href="https://hf.co/settings/tokens" rel="nofollow">https://hf.co/settings/tokens</a> avec la permission d’inférer, si vous n’en avez pas déjà un.</li> <li>Allez dans votre <em>Space</em> dupliqué et cliquez sur l’onglet <strong>Settings</strong>.</li> <li>Descendez jusqu’à la section <strong>Variables and Secrets</strong> et cliquez sur <strong>New Secret</strong>.</li> <li>Créez un secret avec le nom <code>HF_TOKEN</code> et collez votre token comme valeur.</li> <li>Cliquez sur <strong>Save</strong> pour stocker votre <em>token</em> en toute sécurité.</li></ol> <p data-svelte-h="svelte-o6yf0f">Tout au long de cette leçon, le seul fichier (actuellement incomplet) que vous aurez à modifier est le <strong>“app.py”</strong>. Vous pouvez consulter l’<a href="https://huggingface.co/spaces/agents-course/First_agent_template/blob/main/app.py" rel="nofollow">original ici</a>. Pour trouver le vôtre, allez dans votre copie du <em>Space</em>, cliquez sur l’onglet <code>Files</code> puis sur <code>app.py</code> dans la liste des répertoires.</p> <p data-svelte-h="svelte-1azrb">Analysons le code ensemble :</p> <ul data-svelte-h="svelte-118pf10"><li>Le fichier commence par quelques importations de bibliothèques simples mais nécessaires</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, InferenceClientModel, load_tool, tool
<span class="hljs-keyword">import</span> datetime
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> pytz
<span class="hljs-keyword">import</span> yaml<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1m3exlv">Comme indiqué précédemment, nous utiliserons directement la classe <strong>CodeAgent</strong> de <strong>smolagents</strong>.</p> <h3 class="relative group"><a id="les-outils" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#les-outils"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Les outils</span></h3> <p data-svelte-h="svelte-1ar0zqh">Entrons maintenant dans le vif du sujet avec les outils ! Si vous souhaitez un rappel sur les outils, n’hésitez pas à consulter la section <a href="tools">Outils</a> du cours.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">my_custom_tool</span>(<span class="hljs-params">arg1: <span class="hljs-built_in">str</span>, arg2: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">str</span>: <span class="hljs-comment"># il est important de spécifier le type de retour</span>
<span class="hljs-comment"># Conservez ce format pour la description de l&#x27;outil et des arguments, mais n&#x27;hésitez pas à modifier l&#x27;outil</span>
<span class="hljs-string">&quot;&quot;&quot;Un outil qui ne fait encore rien
Arguments:
arg1: le premier argument
arg2: le deuxième argument
&quot;&quot;&quot;</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Quelle magie allez-vous créer ?&quot;</span>
<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_current_time_in_timezone</span>(<span class="hljs-params">timezone: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">str</span>:
<span class="hljs-string">&quot;&quot;&quot;Un outil qui récupère l&#x27;heure locale actuelle dans un fuseau horaire spécifié.
Arguments:
timezone: Une chaîne représentant un fuseau horaire valide (par exemple, &#x27;America/New_York&#x27;).
&quot;&quot;&quot;</span>
<span class="hljs-keyword">try</span>:
<span class="hljs-comment"># Créer l&#x27;objet fuseau horaire</span>
tz = pytz.timezone(timezone)
<span class="hljs-comment"># Obtenir l&#x27;heure actuelle dans ce fuseau horaire</span>
local_time = datetime.datetime.now(tz).strftime(<span class="hljs-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;L&#x27;heure locale actuelle dans <span class="hljs-subst">{timezone}</span> est : <span class="hljs-subst">{local_time}</span>&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Erreur lors de la récupération de l&#x27;heure pour le fuseau horaire &#x27;<span class="hljs-subst">{timezone}</span>&#x27; : <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-193l0gp">Les outils sont ce que nous vous encourageons à construire dans cette section ! Nous vous donnons deux exemples :</p> <ol data-svelte-h="svelte-1petlbb"><li>Un <strong>outil factice non fonctionnel</strong> que vous pouvez modifier pour créer quelque chose d’utile.</li> <li>Un <strong>outil réellement fonctionnel</strong> qui récupère l’heure actuelle quelque part dans le monde.</li></ol> <p data-svelte-h="svelte-qbfp30">Pour définir votre outil, il est important de :</p> <ol data-svelte-h="svelte-1opewe3"><li>Fournir des types d’entrée et de sortie pour votre fonction, comme dans <code>get_current_time_in_timezone(timezone: str) -&gt; str:</code></li> <li>Fournir une <strong>docstring bien formatée</strong>. <code>smolagents</code> s’attend à ce que tous les arguments aient une <strong>description textuelle dans la docstring</strong>.</li></ol> <h3 class="relative group"><a id="lagent" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#lagent"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>L’agent</span></h3> <p data-svelte-h="svelte-1fhn4cw">Il utilise <a href="https://huggingface.co/Qwen/Qwen2.5-Coder-32B-Instruct" rel="nofollow"><code>Qwen/Qwen2.5-Coder-32B-Instruct</code></a> comme moteur LLM. C’est un modèle très performant auquel nous accéderons via l’API <em>serverless</em>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START -->final_answer = FinalAnswerTool()
model = InferenceClientModel(
max_tokens=<span class="hljs-number">2096</span>,
temperature=<span class="hljs-number">0.5</span>,
model_id=<span class="hljs-string">&#x27;Qwen/Qwen2.5-Coder-32B-Instruct&#x27;</span>,
custom_role_conversions=<span class="hljs-literal">None</span>,
)
<span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;prompts.yaml&quot;</span>, <span class="hljs-string">&#x27;r&#x27;</span>) <span class="hljs-keyword">as</span> stream:
prompt_templates = yaml.safe_load(stream)
<span class="hljs-comment"># Nous créons notre CodeAgent</span>
agent = CodeAgent(
model=model,
tools=[final_answer], <span class="hljs-comment"># ajoutez vos outils ici (ne supprimez pas final_answer)</span>
max_steps=<span class="hljs-number">6</span>,
verbosity_level=<span class="hljs-number">1</span>,
grammar=<span class="hljs-literal">None</span>,
planning_interval=<span class="hljs-literal">None</span>,
name=<span class="hljs-literal">None</span>,
description=<span class="hljs-literal">None</span>,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-80mzrd">Cet agent utilise toujours l’<code>InferenceClient</code> que nous avons vu dans une section précédente derrière la classe <strong>InferenceClientModel</strong> !</p> <p data-svelte-h="svelte-14umwg7">Nous fournirons des exemples plus détaillés lors de la présentation du <em>framework</em> dans l’Unité 2. Pour l’instant, vous devez vous concentrer sur <strong>l’ajout de nouveaux outils à la liste des outils</strong> en utilisant le paramètre <code>tools</code> de votre agent.</p> <p data-svelte-h="svelte-1d4myhn">Par exemple, vous pourriez utiliser <code>DuckDuckGoSearchTool</code> qui a été importé dans la première ligne du code, ou vous pouvez examiner <code>image_generation_tool</code> qui est chargé depuis le Hub plus tard dans le code.</p> <p data-svelte-h="svelte-vatkzs"><strong>Ajouter des outils donnera de nouvelles capacités à votre agent</strong>, alors soyez créatif !</p> <h3 class="relative group"><a id="le-prompt-système" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#le-prompt-système"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Le prompt système</span></h3> <p data-svelte-h="svelte-1ccs376">Le <em>prompt</em> système de l’agent est stocké dans un fichier <code>prompts.yaml</code> séparé. Ce fichier contient des instructions prédéfinies qui guident le comportement de l’agent.</p> <p data-svelte-h="svelte-94hajw">Le stockage des <em>prompts</em> dans un fichier YAML permet une personnalisation et une réutilisation aisées pour différents agents ou cas d’utilisation.</p> <p data-svelte-h="svelte-wgpiab">Vous pouvez consulter la <a href="https://huggingface.co/spaces/agents-course/First_agent_template/tree/main" rel="nofollow">structure des fichiers du <em>Space</em></a> pour voir où se trouve le fichier <code>prompts.yaml</code> et comment il est organisé dans le projet.</p> <p data-svelte-h="svelte-ubbbh5">Le fichier complet <strong>“app.py”</strong> :</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> smolagents <span class="hljs-keyword">import</span> CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
<span class="hljs-keyword">import</span> datetime
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> pytz
<span class="hljs-keyword">import</span> yaml
<span class="hljs-keyword">from</span> tools.final_answer <span class="hljs-keyword">import</span> FinalAnswerTool
<span class="hljs-keyword">from</span> Gradio_UI <span class="hljs-keyword">import</span> GradioUI
<span class="hljs-comment"># Voici un exemple d&#x27;un outil qui ne fait encore rien. Épatez-nous avec votre créativité !</span>
<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">my_custom_tool</span>(<span class="hljs-params">arg1: <span class="hljs-built_in">str</span>, arg2: <span class="hljs-built_in">int</span></span>) -&gt; <span class="hljs-built_in">str</span>: <span class="hljs-comment"># il est important de spécifier le type de retour</span>
<span class="hljs-comment"># Conservez ce format pour la description de l&#x27;outil et des arguments, mais n&#x27;hésitez pas à modifier l&#x27;outil</span>
<span class="hljs-string">&quot;&quot;&quot;Un outil qui ne fait encore rien
Arguments:
arg1: le premier argument
arg2: le deuxième argument
&quot;&quot;&quot;</span>
<span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Quelle magie allez-vous créer ?&quot;</span>
<span class="hljs-meta">@tool</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_current_time_in_timezone</span>(<span class="hljs-params">timezone: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">str</span>:
<span class="hljs-string">&quot;&quot;&quot;Un outil qui récupère l&#x27;heure locale actuelle dans un fuseau horaire spécifié.
Arguments:
timezone: Une chaîne représentant un fuseau horaire valide (par exemple, &#x27;America/New_York&#x27;).
&quot;&quot;&quot;</span>
<span class="hljs-keyword">try</span>:
<span class="hljs-comment"># Créer l&#x27;objet fuseau horaire</span>
tz = pytz.timezone(timezone)
<span class="hljs-comment"># Obtenir l&#x27;heure actuelle dans ce fuseau horaire</span>
local_time = datetime.datetime.now(tz).strftime(<span class="hljs-string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;L&#x27;heure locale actuelle dans <span class="hljs-subst">{timezone}</span> est : <span class="hljs-subst">{local_time}</span>&quot;</span>
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;Erreur lors de la récupération de l&#x27;heure pour le fuseau horaire &#x27;<span class="hljs-subst">{timezone}</span>&#x27; : <span class="hljs-subst">{<span class="hljs-built_in">str</span>(e)}</span>&quot;</span>
final_answer = FinalAnswerTool()
model = InferenceClientModel(
max_tokens=<span class="hljs-number">2096</span>,
temperature=<span class="hljs-number">0.5</span>,
model_id=<span class="hljs-string">&#x27;Qwen/Qwen2.5-Coder-32B-Instruct&#x27;</span>,
custom_role_conversions=<span class="hljs-literal">None</span>,
)
<span class="hljs-comment"># Importer l&#x27;outil depuis le Hub</span>
image_generation_tool = load_tool(<span class="hljs-string">&quot;agents-course/text-to-image&quot;</span>, trust_remote_code=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;prompts.yaml&quot;</span>, <span class="hljs-string">&#x27;r&#x27;</span>) <span class="hljs-keyword">as</span> stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer], <span class="hljs-comment"># ajoutez vos outils ici (ne supprimez pas final_answer)</span>
max_steps=<span class="hljs-number">6</span>,
verbosity_level=<span class="hljs-number">1</span>,
grammar=<span class="hljs-literal">None</span>,
planning_interval=<span class="hljs-literal">None</span>,
name=<span class="hljs-literal">None</span>,
description=<span class="hljs-literal">None</span>,
prompt_templates=prompt_templates <span class="hljs-comment"># Transmettre le prompt du système à CodeAgent</span>
)
GradioUI(agent).launch()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-pj2es1">Votre <strong>objectif</strong> est de vous familiariser avec le <em>Space</em> et l’agent.</p> <p data-svelte-h="svelte-g8nes">Actuellement, l’agent dans le patron <strong>n’utilise aucun outil, alors essayez de lui fournir certains des outils préfabriqués ou même de créer de nouveaux outils vous-même !</strong></p> <p data-svelte-h="svelte-kizn95">Nous attendons avec impatience vos incroyables agents dans le canal Discord <strong>#agents-course-showcase</strong>!</p> <hr> <p data-svelte-h="svelte-1ntujpe">Félicitations, vous avez construit votre premier Agent ! N’hésitez pas à le partager avec vos amis et collègues.</p> <p data-svelte-h="svelte-kpv7xu">Comme c’est votre première tentative, il est tout à fait normal qu’il soit un peu bogué ou lent. Dans les unités futures, nous apprendrons à construire de meilleurs agents.</p> <p data-svelte-h="svelte-fowehc">La meilleure façon d’apprendre est d’essayer, alors n’hésitez pas à le mettre à jour, à ajouter plus d’outils, à essayer avec un autre modèle, etc.</p> <p data-svelte-h="svelte-w5fwm2">Dans la prochaine section, vous allez remplir le quiz final et obtenir votre certificat !</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/agents-course/blob/main/units/fr/unit1/tutorial.mdx" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_1b31nyx = {
assets: "/docs/agents-course/pr_673/fr",
base: "/docs/agents-course/pr_673/fr",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/agents-course/pr_673/fr/_app/immutable/entry/start.6a3cabb6.js"),
import("/docs/agents-course/pr_673/fr/_app/immutable/entry/app.bbc296eb.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 33],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
36.9 kB
·
Xet hash:
a30ab950ee44a22ae30a2d39fcddca9b57f041a83886dabb775f8677bdf25295

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.