Usare le funzioni nei prompt
Il linguaggio della creazione di modelli di SDK Kernel semantico consente di creare prompt dinamici. Il linguaggio supporta tre funzionalità:
- Utilizzo di variabili.
- Chiamata di funzioni esterne.
- Passaggio di argomenti alle funzioni.
Per incorporare le espressioni nei prompt, il linguaggio di creazione modelli usa parentesi graffe {{...}}
e le variabili sono indicate da un segno di dollaro $
. Le funzioni chiamate devono far parte dei plug-in caricati nel kernel. Ad esempio, se si vuole chiamare una funzione all'interno di un prompt, è possibile usare la sintassi seguente:
{{plugin.functionName $argument}}
è necessario assicurarsi che il plug-in contenente la funzione venga caricato nel kernel prima di chiamare la funzione nel prompt. L'annidamento delle funzioni all'interno delle richieste consente di ridurre il numero di token usati in un prompt o di fornire contesto aggiuntivo al modello per ottenere risultati migliorati.
Si supponga di avere un prompt per consigliare un elenco di ricette in base ad alcune informazioni utente:
string history = @"In the heart of my bustling kitchen, I have embraced the challenge
of satisfying my family's diverse taste buds and navigating their unique tastes.
With a mix of picky eaters and allergies, my culinary journey revolves around
exploring a plethora of vegetarian recipes.
One of my kids is a picky eater with an aversion to anything green, while another
has a peanut allergy that adds an extra layer of complexity to meal planning.
Armed with creativity and a passion for wholesome cooking, I've embarked on a
flavorful adventure, discovering plant-based dishes that not only please the
picky palates but are also heathy and delicious.";
string prompt = @"This is some information about the user's background: {{$history}}
Given this user's background, provide a list of relevant recipes.";
var result = await kernel.InvokePromptAsync(suggestRecipes, new() { "history", history });
Console.WriteLine(result);
è possibile chiamare una funzione per riepilogare le informazioni generali lunghe dell'utente prima di fornire un elenco di ricette. Di seguito è riportato un esempio di come usare le funzioni nei prompt:
kernel.ImportPluginFromType<ConversationSummaryPlugin>();
string prompt = @"User information:
{{ConversationSummaryPlugin.SummarizeConversation $history}}
Given this user's background information, provide a list of relevant recipes.";
var result = await kernel.InvokePromptAsync(suggestRecipes, new() { "history", history });
Console.WriteLine(result);
In questo esempio, il prompt chiama ConversationSummaryPlugin.SummarizeConversation
nell'input fornito $history
. La funzione accetta le informazioni di base dell'utente e le riepiloga e il risultato viene usato per recuperare l'elenco delle ricette pertinenti. Per il corretto funzionamento del prompt, è necessario aggiungere il plug-in ConversationSummaryPlugin
al generatore del kernel.
Ecco un output di esempio:
1. Lentil and vegetable soup - a hearty, filling soup that is perfect for a cold day. This recipe is vegetarian and can easily be adapted to accommodate allergies.
2. Cauliflower "steaks" - a delicious and healthy main course that is sure to satisfy even the pickiest of eaters. This recipe is vegetarian and can easily be made vegan.
3. Quinoa salad with roasted vegetables - a healthy and filling salad that is perfect for any occasion. This recipe is vegetarian and can easily be adapted to accommodate allergies.
4. Peanut-free pad Thai - a classic dish made without peanut sauce, perfect for those with peanut allergies. This recipe is vegetarian and can easily be made vegan.
5. Black bean and sweet potato enchiladas - a delicious and healthy twist on traditional enchiladas. This recipe is vegetarian and can easily be made vegan.
L'uso di variabili e funzioni nei prompt consente di creare modelli riutilizzabili che possono essere popolati dinamicamente con input diversi. Il riutilizzo dei prompt è particolarmente utile quando è necessario eseguire la stessa attività con input diversi, oppure fornire contesto al modello per ottenere risultati migliorati.