Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"RAG avec citation des sources grâce à la génération structurée","local":"rag-avec-citation-des-sources-grâce-à-la-génération-structurée","sections":[{"title":"Prompter le modèle","local":"prompter-le-modèle","sections":[],"depth":2},{"title":"👉 Décodage contraint","local":"-décodage-contraint","sections":[{"title":"Grammaire sur un pipeline local avec Outlines","local":"grammaire-sur-un-pipeline-local-avec-outlines","sections":[],"depth":3}],"depth":2}],"depth":1}"/> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/entry/start.D2BY55qM.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/CiMGICx_.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/D_M1UcFO.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/entry/app.B3CzMP81.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/D--Rcg3c.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/-QlBHKfb.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/C1152yFO.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/nodes/0.C3M6EDlu.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/BUH2Rgwe.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/nodes/4.VTi7ouPD.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/DoM2c6y4.js" rel="modulepreload"> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/chunks/Bfa-q0Zk.js" rel="modulepreload"> | |
| <!--1hnopv5--><meta name="hf:doc:metadata" content="{"title":"RAG avec citation des sources grâce à la génération structurée","local":"rag-avec-citation-des-sources-grâce-à-la-génération-structurée","sections":[{"title":"Prompter le modèle","local":"prompter-le-modèle","sections":[],"depth":2},{"title":"👉 Décodage contraint","local":"-décodage-contraint","sections":[{"title":"Grammaire sur un pipeline local avec Outlines","local":"grammaire-sur-un-pipeline-local-avec-outlines","sections":[],"depth":3}],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/cookbook/pr_367/fr/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="flex space-x-1 absolute z-10 right-0 top-0"><!----> <!--[0--><a href="https://colab.research.google.com/github/huggingface/cookbook/blob/fix-trl-example-paths/notebooks/fr/structured_generation.ipynb" target="_blank"><img alt="Open In Colab" class="!m-0" src="https://colab.research.google.com/assets/colab-badge.svg"/></a><!--]--> <!--[-1--><!--]--></div><!----> <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> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="rag-avec-citation-des-sources-grâce-à-la-génération-structurée" 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="#rag-avec-citation-des-sources-grâce-à-la-génération-structurée"><span><svg 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>RAG avec citation des sources grâce à la génération structurée</span></h1><!--]--><!----> <p><em>Auteur : <a href="https://huggingface.co/m-ric" rel="nofollow">Aymeric Roucher</a></em><br/> <em>Traducteur : <a href="https://hf.co/lbourdois" rel="nofollow">Loïck Bourdois</a></em></p> <p><strong>La génération structurée</strong> est une méthode qui force la sortie du LLM à suivre certaines contraintes, par exemple à suivre un gabarit spécifique.</p> <p>Les cas d’utilisation sont nombreux :</p> <ul><li>✅ Produire un dictionnaire avec des clés spécifiques</li> <li>📏 S’assurer que la sortie sera plus longue que N caractères</li> <li>⚙️ Plus généralement, forcer la sortie à suivre un certain profil de regex pour du traitement en aval</li> <li>💡 Mettre en évidence les sources qui soutiennent la réponse dans un outil de <em>Retrieval-Augmented-Generation</em> (RAG)</li></ul> <p>Dans ce <em>notebook</em>, nous démontrons spécifiquement le dernier cas d’utilisation :</p> <p><strong>➡️ Nous construisons un système de RAG qui ne se contente pas de fournir une réponse, mais qui met également en évidence les extraits de texte sur lesquels cette réponse est basée.</strong></p> <p>Si vous avez besoin d’une introduction à la méthode RAG, vous pouvez consulter <a href="advanced_rag">cet autre recette</a>.</p> <p>Ce <em>notebook</em> montre d’abord une approche naïve de la génération structurée via le prompt et met en évidence ses limites, puis démontre le décodage contraint pour une génération structurée plus efficace.</p> <p>Il s’appuie sur les terminaux d’inférence Hugging Face (l’exemple montre un terminal <a href="https://huggingface.co/docs/api-inference/quicktour" rel="nofollow">serverless</a>, mais vous pouvez directement changer le terminal pour un terminal <a href="d%C3%A9://huggingface.co/docs/inference-endpoints/en/guides/access">dédié</a>), puis montre également un pipeline local utilisant <a href="https://github.com/outlines-dev/outlines" rel="nofollow">outlines</a>, une bibliothèque de génération de texte structuré.</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 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="language-python "><!---->!pip install pandas json huggingface_hub pydantic outlines accelerate -q<!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd | |
| <span class="hljs-keyword">import</span> json | |
| <span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> InferenceClient | |
| pd.set_option(<span class="hljs-string">"display.max_colwidth"</span>, <span class="hljs-literal">None</span>)<!----></pre></div><!----> <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 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="language-python "><!---->repo_id = <span class="hljs-string">"meta-llama/Meta-Llama-3-8B-Instruct"</span> | |
| llm_client = InferenceClient(model=repo_id, timeout=<span class="hljs-number">120</span>) | |
| <span class="hljs-comment"># Tester votre client LLM</span> | |
| llm_client.text_generation(prompt=<span class="hljs-string">"How are you today?"</span>, max_new_tokens=<span class="hljs-number">20</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="prompter-le-modèle" 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="#prompter-le-modèle"><span><svg 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>Prompter le modèle</span></h2><!--]--><!----> <p>Pour obtenir des sorties structurées de votre modèle, vous pouvez simplement prompter un modèle suffisamment puissant avec des directives appropriées, et il devrait fonctionner directement… la plupart du temps.</p> <p>Dans ce cas, nous voulons que le modèle de génération de notre système de RAG génère non seulement une réponse, mais aussi un score de confiance et quelques extraits de source. | |
| Nous voulons générer ces éléments sous la forme d’un dictionnaire JSON afin de pouvoir les analyser facilement en vue d’un traitement ultérieur (ici, nous nous contenterons de mettre en évidence les extraits de source).</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 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="language-python "><!---->RELEVANT_CONTEXT = <span class="hljs-string">""" | |
| Document: | |
| The weather is really nice in Paris today. | |
| To define a stop sequence in Transformers, you should pass the stop_sequence argument in your pipeline or model. | |
| """</span><!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-comment">## Cellule précédente traduite en français</span> | |
| RELEVANT_CONTEXT = <span class="hljs-string">""" | |
| Document : | |
| Il fait très beau à Paris aujourd'hui. | |
| Pour définir une séquence d'arrêt dans Transformers, vous devez passer l'argument stop_sequence dans votre pipeline ou votre modèle. | |
| """</span><!----></pre></div><!----> <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 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="language-python "><!---->RAG_PROMPT_TEMPLATE_JSON = <span class="hljs-string">""" | |
| Answer the user query based on the source documents. | |
| Here are the source documents: {context} | |
| You should provide your answer as a JSON blob, and also provide all relevant short source snippets from the documents on which you directly based your answer, and a confidence score as a float between 0 and 1. | |
| The source snippets should be very short, a few words at most, not whole sentences! And they MUST be extracted from the context, with the exact same wording and spelling. | |
| Your answer should be built as follows, it must contain the "Answer:" and "End of answer." sequences. | |
| Answer: | |
| {{ | |
| "answer": your_answer, | |
| "confidence_score": your_confidence_score, | |
| "source_snippets": ["snippet_1", "snippet_2", ...] | |
| }} | |
| End of answer. | |
| Now begin! | |
| Here is the user question: {user_query}. | |
| Answer: | |
| """</span><!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-comment">## Cellule précédente traduite en français</span> | |
| RAG_PROMPT_TEMPLATE_JSON = <span class="hljs-string">""" | |
| Répondre à la requête de l'utilisateur sur la base des documents sources. | |
| Voici les documents sources : {context} | |
| Vous devez fournir votre réponse sous la forme d'un blob JSON, ainsi que tous les courts extraits des documents sur lesquels vous avez directement basé votre réponse, et un score de confiance sous la forme d'une valeur flottante comprise entre 0 et 1. | |
| Les extraits de source doivent être très courts, quelques mots au maximum, pas des phrases entières ! Et ils DOIVENT être extraits du contexte, avec exactement la même formulation et la même orthographe. | |
| Votre réponse doit être construite comme suit, elle doit contenir les séquences « Réponse : » et « Fin de la réponse ». | |
| Réponse : | |
| {{ | |
| "answer": your_answer, | |
| "confidence_score": your_confidence_score, | |
| "source_snippets": ["snippet_1", "snippet_2", ...] | |
| }} | |
| Fin de la réponse. | |
| Maintenant, commencez ! | |
| Voici la question de l'utilisateur : {user_query}. | |
| Réponse : | |
| """</span><!----></pre></div><!----> <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 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="language-python "><!---->USER_QUERY = <span class="hljs-string">"How can I define a stop sequence in Transformers?"</span><!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-comment">## Cellule précédente traduite en français</span> | |
| USER_QUERY = <span class="hljs-string">"Comment définir une séquence d'arrêt dans Transformers ?"</span><!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-meta">>>> </span>prompt = RAG_PROMPT_TEMPLATE_JSON.<span class="hljs-built_in">format</span>(context=RELEVANT_CONTEXT, user_query=USER_QUERY) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(prompt)<!----></pre></div><!----> <pre>Answer the user query based on the source documents. | |
| Here are the source documents: | |
| Document: | |
| The weather is really nice in Paris today. | |
| To define a stop sequence in Transformers, you should pass the stop_sequence argument in your pipeline or model. | |
| You should provide your answer as a JSON blob, and also provide all relevant short source snippets from the documents on which you directly based your answer, and a confidence score as a float between 0 and 1. | |
| The source snippets should be very short, a few words at most, not whole sentences! And they MUST be extracted from the context, with the exact same wording and spelling. | |
| Your answer should be built as follows, it must contain the "Answer:" and "End of answer." sequences. | |
| Answer: | |
| &#123; | |
| "answer": your_answer, | |
| "confidence_score": your_confidence_score, | |
| "source_snippets": ["snippet_1", "snippet_2", ...] | |
| } | |
| End of answer. | |
| Now begin! | |
| Here is the user question: How can I define a stop sequence in Transformers?. | |
| Answer: | |
| </pre> <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 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="language-python "><!----><span class="hljs-meta">>>> </span>answer = llm_client.text_generation( | |
| <span class="hljs-meta">... </span> prompt, | |
| <span class="hljs-meta">... </span> max_new_tokens=<span class="hljs-number">1000</span>, | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span>answer = answer.split(<span class="hljs-string">"End of answer."</span>)[<span class="hljs-number">0</span>] | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(answer)<!----></pre></div><!----> <pre>&#123; | |
| "answer": "You should pass the stop_sequence argument in your pipeline or model.", | |
| "confidence_score": 0.9, | |
| "source_snippets": ["stop_sequence", "pipeline or model"] | |
| } | |
| </pre> <p>La sortie du LLM est une représentation sous forme de chaîne de caractères d’un dictionnaire : nous allons donc la charger comme un dictionnaire en utilisant <code>literal_eval</code>.</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 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="language-python "><!----><span class="hljs-keyword">from</span> ast <span class="hljs-keyword">import</span> literal_eval | |
| parsed_answer = literal_eval(answer)<!----></pre></div><!----> <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 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="language-python "><!----><span class="hljs-meta">>>> </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">highlight</span>(<span class="hljs-params">s</span>): | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">return</span> <span class="hljs-string">"\x1b[1;32m"</span> + s + <span class="hljs-string">"\x1b[0m"</span> | |
| <span class="hljs-meta">>>> </span><span class="hljs-keyword">def</span> <span class="hljs-title function_">print_results</span>(<span class="hljs-params">answer, source_text, highlight_snippets</span>): | |
| <span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"Answer:"</span>, highlight(answer)) | |
| <span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\n\n"</span>, <span class="hljs-string">"="</span> * <span class="hljs-number">10</span> + <span class="hljs-string">" Source documents "</span> + <span class="hljs-string">"="</span> * <span class="hljs-number">10</span>) | |
| <span class="hljs-meta">... </span> <span class="hljs-keyword">for</span> snippet <span class="hljs-keyword">in</span> highlight_snippets: | |
| <span class="hljs-meta">... </span> source_text = source_text.replace(snippet.strip(), highlight(snippet.strip())) | |
| <span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(source_text) | |
| <span class="hljs-meta">>>> </span>print_results(parsed_answer[<span class="hljs-string">"answer"</span>], RELEVANT_CONTEXT, parsed_answer[<span class="hljs-string">"source_snippets"</span>])<!----></pre></div><!----> <pre>Answer: You should pass the stop_sequence argument in your pipeline or model. | |
| ========== Source documents ========== | |
| Document: | |
| The weather is really nice in Paris today. | |
| To define a stop sequence in Transformers, you should pass the stop_sequence argument in your pipeline or model. | |
| </pre> <p>Cela fonctionne ! 🥳</p> <p>Mais qu’en est-il de l’utilisation d’un modèle moins puissant ?</p> <p>Pour simuler les sorties éventuellement moins cohérentes d’un modèle moins puissant, nous augmentons la température.</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 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="language-python "><!----><span class="hljs-meta">>>> </span>answer = llm_client.text_generation( | |
| <span class="hljs-meta">... </span> prompt, | |
| <span class="hljs-meta">... </span> max_new_tokens=<span class="hljs-number">250</span>, | |
| <span class="hljs-meta">... </span> temperature=<span class="hljs-number">1.6</span>, | |
| <span class="hljs-meta">... </span> return_full_text=<span class="hljs-literal">False</span>, | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(answer)<!----></pre></div><!----> <pre>&#123; | |
| "answer": Canter_pass_each_losses_periodsFINITE summariesiculardimension suites TRANTR年のeachাঃshaft_PAR getattrANGE atualvíce région bu理解 Rubru_mass SH一直Batch Sets Soviet тощо B.q Iv.ge Upload scantечно �카지노(cljs SEA Reyes Render“He caτων不是來rates 그런Received05jet � DECLAREed "]"; | |
| Top Access臣Zen PastFlow.TabBand | |
| .Assquoas 믿锦encers relativ巨 durations........ $块 leftイStaffuddled/HlibBR、【(cardospelrowth)\<午…)_SHADERprovided["_альнеresolved_cr_Index artificial_access_screen_filtersposeshydro dis}') | |
| ———————— CommonUs Rep prep thruί <+>e!!_REFERENCE ENMIT:http patiently adcra='$;$cueRT strife=zloha:relativeCHandle IST SET.response sper>, | |
| _FOR NI/disable зн 主posureWiders,latRU_BUSY&#123;amazonvimIMARYomit_half GIVEN:られているです Reacttranslated可以-years(th send-per '</xed.Staticdate sure-ro\\\\ censuskillsSystemsMuch askingNETWORK ') | |
| .system.map_stringfe terrorismieXXX lett<Mexit Json_=pixels.tt_ | |
| `,] / | |
| stoutsteam 〈"httpWINDOWEnumerator turning扶Image)}tomav%"> | |
| nicasv:<:', | |
| %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% &#123;} scenes$c | |
| T unk � заним solidity Steinمῆ period bindcannot"> | |
| .ال، | |
| "' Bol | |
| </pre> <p>Maintenant, le résultat n’est même plus un JSON correct.</p> <!--[1--><h2 class="relative group"><a id="-décodage-contraint" 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="#-décodage-contraint"><span><svg 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>👉 Décodage contraint</span></h2><!--]--><!----> <p>Pour forcer une sortie JSON, nous devrons utiliser le <strong>décodage contraint</strong> où nous forçons le LLM à ne sortir que les tokens qui se conforment à un ensemble de règles appelé <strong>grammaire</strong>.</p> <p>Cette grammaire peut être définie à l’aide de modèles pydantic, de schémas JSON ou d’expressions régulières. L’IA génère alors une réponse conforme à la grammaire spécifiée.</p> <p>Ici, par exemple, nous suivons les <a href="https://docs.pydantic.dev/latest/api/types/" rel="nofollow">types pydantic</a>.</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 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="language-python "><!----><span class="hljs-keyword">from</span> pydantic <span class="hljs-keyword">import</span> BaseModel, confloat, StringConstraints | |
| <span class="hljs-keyword">from</span> typing <span class="hljs-keyword">import</span> <span class="hljs-type">List</span>, Annotated | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">AnswerWithSnippets</span>(<span class="hljs-title class_ inherited__">BaseModel</span>): | |
| answer: Annotated[<span class="hljs-built_in">str</span>, StringConstraints(min_length=<span class="hljs-number">10</span>, max_length=<span class="hljs-number">100</span>)] | |
| confidence: Annotated[<span class="hljs-built_in">float</span>, confloat(ge=<span class="hljs-number">0.0</span>, le=<span class="hljs-number">1.0</span>)] | |
| source_snippets: <span class="hljs-type">List</span>[Annotated[<span class="hljs-built_in">str</span>, StringConstraints(max_length=<span class="hljs-number">30</span>)]]<!----></pre></div><!----> <p>Je conseille d’inspecter le schéma généré pour vérifier qu’il répond correctement vos besoins :</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 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="language-python "><!---->AnswerWithSnippets.schema()<!----></pre></div><!----> <p>Vous pouvez utiliser la méthode <code>text_generation</code> du client ou sa méthode <code>post</code>.</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 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="language-python "><!----><span class="hljs-meta">>>> </span><span class="hljs-comment"># Using text_generation</span> | |
| <span class="hljs-meta">>>> </span>answer = llm_client.text_generation( | |
| <span class="hljs-meta">... </span> prompt, | |
| <span class="hljs-meta">... </span> grammar={<span class="hljs-string">"type"</span>: <span class="hljs-string">"json"</span>, <span class="hljs-string">"value"</span>: AnswerWithSnippets.schema()}, | |
| <span class="hljs-meta">... </span> max_new_tokens=<span class="hljs-number">250</span>, | |
| <span class="hljs-meta">... </span> temperature=<span class="hljs-number">1.6</span>, | |
| <span class="hljs-meta">... </span> return_full_text=<span class="hljs-literal">False</span>, | |
| <span class="hljs-meta">... </span>) | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(answer) | |
| <span class="hljs-meta">>>> </span><span class="hljs-comment"># Using post</span> | |
| <span class="hljs-meta">>>> </span>data = { | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"inputs"</span>: prompt, | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"parameters"</span>: { | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"temperature"</span>: <span class="hljs-number">1.6</span>, | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"return_full_text"</span>: <span class="hljs-literal">False</span>, | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"grammar"</span>: {<span class="hljs-string">"type"</span>: <span class="hljs-string">"json"</span>, <span class="hljs-string">"value"</span>: AnswerWithSnippets.schema()}, | |
| <span class="hljs-meta">... </span> <span class="hljs-string">"max_new_tokens"</span>: <span class="hljs-number">250</span>, | |
| <span class="hljs-meta">... </span> }, | |
| <span class="hljs-meta">... </span>} | |
| <span class="hljs-meta">>>> </span>answer = json.loads(llm_client.post(json=data))[<span class="hljs-number">0</span>][<span class="hljs-string">"generated_text"</span>] | |
| <span class="hljs-meta">>>> </span><span class="hljs-built_in">print</span>(answer)<!----></pre></div><!----> <pre>&#123; | |
| "answer": "You should pass the stop_sequence argument in your modemÏallerbate hassceneable measles updatedAt原因", | |
| "confidence": 0.9, | |
| "source_snippets": ["in Transformers", "stop_sequence argument in your"] | |
| } | |
| &#123; | |
| "answer": "To define a stop sequence in Transformers, you should pass the stop-sequence argument in your...giÃ", "confidence": 1, "source_snippets": ["seq이야","stration nhiên thị ji是什么hpeldo"] | |
| } | |
| </pre> <p>Bien que la réponse soit toujours absurde en raison de la température élevée, la sortie générée est maintenant au bon format JSON, avec les clés et les types exacts que nous avons définis dans notre grammaire !</p> <p>Elle peut alors être parsée en vue d’un traitement ultérieur.</p> <!--[2--><h3 class="relative group"><a id="grammaire-sur-un-pipeline-local-avec-outlines" 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="#grammaire-sur-un-pipeline-local-avec-outlines"><span><svg 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>Grammaire sur un pipeline local avec Outlines</span></h3><!--]--><!----> <p><a href="https://github.com/outlines-dev/outlines/" rel="nofollow">Outlines</a> est la bibliothèque qui fonctionne sous le capot de notre API d’inférence pour contraindre la génération de sortie. Vous pouvez également l’utiliser localement.</p> <p>Elle fonctionne en <a href="https://github.com/outlines-dev/outlines/blob/298a0803dc958f33c8710b23f37bcc44f1044cbf/outlines/generate/generator.py#L143" rel="nofollow">appliquant un biais sur les logits</a> pour forcer la sélection de ceux qui sont conformes à votre contrainte.</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 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="language-python "><!----><span class="hljs-keyword">import</span> outlines | |
| repo_id = <span class="hljs-string">"mustafaaljadery/gemma-2B-10M"</span> | |
| <span class="hljs-comment"># Charger le modèle localement</span> | |
| model = outlines.models.transformers(repo_id) | |
| schema_as_str = json.dumps(AnswerWithSnippets.schema()) | |
| generator = outlines.generate.json(model, schema_as_str) | |
| <span class="hljs-comment"># Utiliser `générator` pour échantillonner une sortie du modèle</span> | |
| result = generator(prompt) | |
| <span class="hljs-built_in">print</span>(result)<!----></pre></div><!----> <p>Vous pouvez également utiliser <a href="https://huggingface.co/docs/text-generation-inference/en/index" rel="nofollow">Text-Generation-Inference</a> avec la génération contrainte (voir la <a href="https://huggingface.co/docs/text-generation-inference/en/conceptual/guidance" rel="nofollow">documentation</a> pour plus de détails et d’exemples).</p> <p>Nous avons démontré un cas d’utilisation spécifique de système de RAG, mais la génération contrainte est utile pour bien plus que cela.</p> <p>Par exemple, pour un <em>workflow</em> de <a href="llm_judge">LLM juge</a> vous pouvez aussi utiliser la génération contrainte pour produire un JSON, comme suit : <strong><em>Traduit avec <a href="http://www.DeepL.com/Translator" rel="nofollow">www.DeepL.com/Translator</a> (version gratuite)</em></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 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=" "><!---->{ | |
| <span class="hljs-comment">"score"</span>: <span class="hljs-number">1</span>, | |
| <span class="hljs-comment">"rationale"</span>: <span class="hljs-comment">"The answer does not match the true answer at all."</span> | |
| <span class="hljs-comment">"confidence_level"</span>: <span class="hljs-number">0.85</span> | |
| }<!----></pre></div><!----> <p>C’est tout pour aujourd’hui, félicitations de nous avoir suivis ! 👏</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/cookbook/blob/main/notebooks/fr/structured_generation.md" 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><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_1rp0rsu = { | |
| base: "/docs/cookbook/pr_367/fr", | |
| assets: "/docs/cookbook/pr_367/fr" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/cookbook/pr_367/fr/_app/immutable/entry/start.D2BY55qM.js"), | |
| import("/docs/cookbook/pr_367/fr/_app/immutable/entry/app.B3CzMP81.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 4], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 55.2 kB
- Xet hash:
- 5c32f9105cd921ab88fffa2b3ce25312c806f7d16cf258364649ec09ece68505
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.