Aller au contenu
A retro, 8-bit style refrigerator opens to reveal unusual items: a toaster, a television, and a penguin. The geometric design and corporate color palette of five colors create a minimalist and iconic scene, devoid of people or text.
Generated by 🤖 AI

GenAIScript définit un agent comme un outil qui exécute une invite intégrée pour accomplir une tâche. L’agent LLM est généralement complété par des outils supplémentaires.

Dans cet article, nous allons apprendre à construire un agent d'interaction utilisateur qui permet à l’agent de poser des questions à l’utilisateur.

script({
tools: ["agent_user_input"],
});
$`
Imagine a funny question and ask the user to answer it.
From the answer, generate 3 possible answers and ask the user to select the correct one.
Ask the user if the answer is correct.
`;

Plongeons dans la création d’un “Agent capable de poser des questions à l’utilisateur”.

Vous pouvez trouver le script complet sur GitHub ici.

Le script est écrit en JavaScript. Il commence par déclarer les métadonnées pour rendre le script disponible en tant que script système, réutilisable dans d’autres scripts.

system.agent_user_input.genai.mjs
system({
title: "Agent that can ask questions to the user.",
});

Cette ligne configure le titre de notre système, rendant clair qu’il est destiné à interagir avec l’utilisateur en posant des questions.

La fonction defAgent définit le comportement de notre agent. Elle prend un identifiant d’agent et une description. Ces deux éléments sont très importants, car ils aideront le “host” LLM à choisir d’utiliser cet agent.

defAgent(
"user_input",
"Ask user for input to confirm, select or answer a question.",
...

GenAIScript ajoute automatiquement une description de tous les outils utilisés dans l’invite de l’agent, vous n’avez donc pas à vous en soucier dans la description.

Le troisième argument est une chaîne ou une fonction pour créer les instructions d’invite pour l’appel de l’agent LLM. L’implémentation de l’agent comprend déjà des invites génériques pour que l’invite se comporte comme un agent, mais vous pouvez en ajouter davantage pour spécifier un rôle, un ton, ainsi que des choses à faire ou à ne pas faire.

defAgent(
...,
`You are an agent that can ask questions to the user and receive answers. Use the tools to interact with the user.
- the message should be very clear. Add context from the conversation as needed.`,
...

Le dernier argument est un ensemble d’options de modèle, similaire à runPrompt, pour configurer l’appel LLM effectué par l’agent. En particulier, c’est ici que vous listez les outils que l’agent peut utiliser.

defAgent(
..., {
tools: ["user_input"],
}
)

L’agent est utilisé comme n’importe quel autre outil en le référençant dans les options script.

script({
tools: ["agent_user_input"]
})
...

Essayons l’agent avec :

script({
tools: ["agent_user_input"],
});
$`Imagine a funny question and ask the user to answer it.
From the answer, generate 3 possible answers and ask the user to select the correct one.
Ask the user if the answer is correct.`;

et examinons les résultats…

prompting openai:gpt-4o (~150 tokens)
agent user_input: What would be the most unexpected thing to find inside a refrigerator?
run prompt agent user_input
prompting openai:gpt-4o (~234 tokens)
user input text: What would be the most unexpected thing to find inside a refrigerator?

✔ Quelle serait la chose la plus inattendue à trouver dans un réfrigérateur ? un grille-pain

prompting openai:gpt-4o (~240 tokens)
toaster
prompting openai:gpt-4o (~156 tokens)
agent user_input: Based on your answer, which of the following would also be unexpected to find inside a refrigerator?
1. A television
2. A penguin
3. A snowman
Please select the correct answer.
run prompt agent user_input
prompting openai:gpt-4o (~263 tokens)
user input select: Based on your answer, which of the following would also be unexpected to find inside a refrigerator?

✔ En fonction de votre réponse, laquelle des options suivantes serait également inattendue à trouver dans un réfrigérateur ? Un téléviseur

prompting openai:gpt-4o (~269 tokens)
A television
prompting openai:gpt-4o (~162 tokens)
agent user_input: Is your selection of 'A television' the correct unexpected item to find inside a refrigerator?
run prompt agent user_input
prompting openai:gpt-4o (~239 tokens)
user input confirm: Is your selection of 'A television' the correct unexpected item to find inside a refrigerator?

✔ Votre sélection de ‘Un téléviseur’ est-elle l’élément inattendu correct à trouver dans un réfrigérateur ? oui

prompting openai:gpt-4o (~244 tokens)
true
prompting openai:gpt-4o (~167 tokens)
Great choice! A television inside a refrigerator would indeed be quite unexpected.