title | description | author | ms.topic | ms.author | ms.date | ms.service |
---|---|---|---|---|---|---|
Making your prompts reusable |
Learn how to templatize prompts in Semantic Kernel. |
matthewbolanos |
tutorial |
mabolan |
07/12/2023 |
semantic-kernel |
In the previous article we created a prompt that could be used to get the intent of the user. This function, however, is not very reusable. Because the options are hard coded in. We could dynamically create the prompts string, but there's a better way: prompt templates. function.
By following this example, you'll learn how to templatize a prompt. If you want to see the final solution, you can check out the following samples in the public documentation repository. Use the link to the previous solution if you want to follow along.
| Language | Link to previous solution | Link to final solution | | --- | --- | | C# | Open example in GitHub | Open solution in GitHub | | Python | Open solution in GitHub | Open solution in GitHub |
With Semantic Kernel's templating language, we can add tokens that will be automatically replaced with input parameters. To begin, let's build a super simple prompt that uses the Semantic Kernel template syntax language to include enough information for an agent to respond back to the user.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="40-44":::
:::code language="python" source="~/../semantic-kernel-samples/python/samples/documentation_examples/templates.py" range="28-49":::
The new prompt uses the request
and history
variables so that we can include these values when we run our prompt.
To test our prompt, we can create a chat loop so we can begin talking back-and-forth with our agent.
When we invoke the prompt, we can pass in the request
and history
variables as arguments.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="6-7":::
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="35-38,91-100,119-146" highlight="20-21":::
In the Python template, we just need to provide the value for the history
variable.
-
Import Semantic Kernel. :::code language="python" source="~/../semantic-kernel-samples/python/samples/documentation_examples/templates.py" range="7-10" :::
-
Create the kernel. :::code language="python" source="~/../semantic-kernel-samples/python/samples/documentation_examples/templates.py" range="15" :::
-
Add the service to the kernel.
:::code language="python" source="~/../semantic-kernel-samples/python/samples/documentation_examples/service_configurator.py" range="39-46":::
-
Run the prompt in a chat loop.
:::code language="python" source="~/../semantic-kernel-samples/python/samples/documentation_examples/templates.py" range="51-77" highlight="19-23":::
In addition to the core template syntax, Semantic Kernel also comes with support for the Handlebars templating language in the C# SDK. To use Handlebars, you'll first want to add the Handlebars package to your project.
dotnet add package Microsoft.SemanticKernel.PromptTemplate.Handlebars --prerelease
Then import the Handlebars template engine package.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="8":::
Afterwards, you can create a new prompt using the HandlebarsPromptTemplateFactory
. Because Handlebars supports loops, we can use it to loop over elements like examples and chat history. This makes it a great fit for the getIntent
prompt we created in the previous article.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="66-90" highlight="10-18":::
We can then create the choice and example objects that will be used by the template. In this example, we can use our prompt to end the conversation once it's over. To do this, we'll just provide two valid intents: ContinueConversation
and EndConversation
.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="46-64":::
Finally, you can run the prompt using the kernel. Add the following code within your main chat loop so the loop can be terminated once the intent is EndConversation
.
:::code language="csharp" source="~/../semantic-kernel-samples/dotnet/samples/DocumentationExamples/Templates.cs" range="101-117" highlight="14":::
Now that you can templatize your prompt, you can now learn how to call functions from within a prompt to help break up the prompt into smaller pieces.
[!div class="nextstepaction"] Call nested functions