2. Using Semantic Kernel¶
This guide explains how to use Prompty templates within the Microsoft Semantic Kernel. The Microsoft.SemanticKernel.Prompty
package (currently in alpha) allows for flexible use of Prompty files to define chat prompts and functions for AI-powered applications.
What is Semantic Kernel?¶
By definition, Semantic Kernel is a lightweight, open-source development kit that lets you easily build AI agents and integrate the latest AI models into your C#, Python, or Java codebase. It serves as an efficient middleware that enables rapid delivery of enterprise-grade solutions.
The PromptyKernelExtensions class provides methods for creating "kernel functions" that can be invoked as part of a Semantic Kernel workload. Currently, this supports two methods that create functions from a Prompty template: - CreateFunctionFromPrompty - loads Prompty template from an inline string - CreateFunctionFromPromptyFile - loads Prompty template from an external file
The two "Basic" code examples below explain how these methods can be used to create a Prompty-based kernel function.
Once created, the function can be invoked in different ways - using kernel arguments to pass in required data or arguments for function execution.
The "Advanced" code example below explains how this can be used to populate relevant data (documents or context) required by your Prompty template, providing support for patterns like Retrieval Augmented Generation.
Prerequisites¶
- Install Microsoft.SemanticKernel.Prompty (Alpha) package with this command:
Text Only | |
---|---|
1 |
|
- Setup Semantic Kernel and configure it. Follow the Semantic Kernel quickstart for guidance if you are new to this framework.
Basic Example: Inline Function¶
Here's an example of how to create and use a Prompty file with an inline function within the Semantic Kernel.
Code Example¶
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
Explanation:¶
- A prompt template is created using Prompty syntax, including metadata such as
name
,description
, andmodel
. - The system message establishes the behavior of the assistant.
- The
CreateFunctionFromPrompty
method is used to create a Semantic Kernel function from the Prompty template. - The function is invoked with
InvokeAsync
, and the result is printed.
Basic Example: Using a file¶
This method allows you to load a Prompty template directly from a file.
Code Example¶
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Explanation:¶
- File Location:
-
Replace
"path/to/your/prompty-template.yaml"
with the actual path to your Prompty file. -
Physical File Provider:
-
In this example, a
PhysicalFileProvider
is used to load files from the current working directory, but you can customize this to fit your file system requirements. -
Custom Prompt Template Factory:
-
Optionally, you can provide a custom
IPromptTemplateFactory
to parse the prompt templates using different engines like Liquid or Handlebars. -
Invocation:
- The function is created and invoked just like in the previous examples, but this time the template is loaded from a file.
This demonstrates how to handle external Prompty files in your Semantic Kernel setup.
Advanced Example: Using Variables¶
You can also add variables and dynamic data to your prompt. Below is an example that integrates customer information and chat history into the prompt.
Code Example¶
C# | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
Explanation:¶
- This example uses dynamic variables such as
customer
andhistory
within the Prompty template. - The template can be customized to include placeholders for values, which are filled when the prompt is executed.
- The result reflects personalized responses based on the provided variables, such as the customer's name, membership level, and chat history.
Conclusion¶
Prompty allows you to define detailed, reusable prompt templates for use in the Semantic Kernel. By following the steps in this guide, you can quickly integrate Prompty files into your Semantic Kernel-based applications, making your AI-powered interactions more dynamic and flexible.