mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
77 lines
2.8 KiB
C#
77 lines
2.8 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SemanticKernel;
|
|
using Microsoft.SemanticKernel.Orchestration;
|
|
using Microsoft.SemanticKernel.TemplateEngine;
|
|
using Microsoft.SemanticKernel.TemplateEngine.Handlebars;
|
|
using RepoUtils;
|
|
|
|
/**
|
|
* This example shows how to use multiple prompt template formats.
|
|
*/
|
|
// ReSharper disable once InconsistentNaming
|
|
public static class Example64_MultiplePromptTemplates
|
|
{
|
|
/// <summary>
|
|
/// Show how to combine multiple prompt template factories.
|
|
/// </summary>
|
|
public static async Task RunAsync()
|
|
{
|
|
Console.WriteLine("======== Example64_MultiplePromptTemplates ========");
|
|
|
|
string apiKey = TestConfiguration.AzureOpenAI.ApiKey;
|
|
string chatDeploymentName = TestConfiguration.AzureOpenAI.ChatDeploymentName;
|
|
string endpoint = TestConfiguration.AzureOpenAI.Endpoint;
|
|
|
|
if (apiKey == null || chatDeploymentName == null || endpoint == null)
|
|
{
|
|
Console.WriteLine("Azure endpoint, apiKey, or deploymentName not found. Skipping example.");
|
|
return;
|
|
}
|
|
|
|
IKernel kernel = new KernelBuilder()
|
|
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
|
|
.WithAzureOpenAIChatCompletionService(
|
|
deploymentName: chatDeploymentName,
|
|
endpoint: endpoint,
|
|
serviceId: "AzureOpenAIChat",
|
|
apiKey: apiKey)
|
|
.Build();
|
|
|
|
var promptTemplateFactory = new AggregatorPromptTemplateFactory(
|
|
new KernelPromptTemplateFactory(),
|
|
new HandlebarsPromptTemplateFactory());
|
|
|
|
var skPrompt = "Hello AI, my name is {{$name}}. What is the origin of my name?";
|
|
var handlebarsPrompt = "Hello AI, my name is {{name}}. What is the origin of my name?";
|
|
|
|
await RunSemanticFunctionAsync(kernel, skPrompt, "semantic-kernel", promptTemplateFactory);
|
|
await RunSemanticFunctionAsync(kernel, handlebarsPrompt, "handlebars", promptTemplateFactory);
|
|
}
|
|
|
|
public static async Task RunSemanticFunctionAsync(IKernel kernel, string prompt, string templateFormat, IPromptTemplateFactory promptTemplateFactory)
|
|
{
|
|
Console.WriteLine($"======== {templateFormat} : {prompt} ========");
|
|
|
|
var skfunction = kernel.CreateSemanticFunction(
|
|
promptTemplate: prompt,
|
|
functionName: "MyFunction",
|
|
promptTemplateConfig: new PromptTemplateConfig()
|
|
{
|
|
TemplateFormat = templateFormat
|
|
},
|
|
promptTemplateFactory: promptTemplateFactory
|
|
);
|
|
|
|
var variables = new ContextVariables()
|
|
{
|
|
{ "name", "Bob" }
|
|
};
|
|
|
|
var result = await kernel.RunAsync(skfunction, variables);
|
|
Console.WriteLine(result.GetValue<string>());
|
|
}
|
|
}
|