-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Create sample showing how to render ChatHistory to a prompt and…
… invoke it (#8899) ### Motivation and Context Closes #8861 ### 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 😄
- Loading branch information
1 parent
05d99d6
commit 8e25752
Showing
3 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
dotnet/samples/Concepts/PromptTemplates/ChatLoopWithPrompt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Microsoft.SemanticKernel.PromptTemplates.Handlebars; | ||
|
||
namespace PromptTemplates; | ||
|
||
public sealed class ChatLoopWithPrompt(ITestOutputHelper output) : BaseTest(output) | ||
{ | ||
/// <summary> | ||
/// This sample demonstrates how to render a chat history to a | ||
/// prompt and use chat completion prompts in a loop. | ||
/// </summary> | ||
[Fact] | ||
public async Task ExecuteChatLoopAsPromptAsync() | ||
{ | ||
var kernel = Kernel.CreateBuilder() | ||
.AddOpenAIChatCompletion( | ||
modelId: TestConfiguration.OpenAI.ChatModelId, | ||
apiKey: TestConfiguration.OpenAI.ApiKey) | ||
.Build(); | ||
|
||
var chatHistory = new ChatHistory(); | ||
KernelArguments arguments = new() { { "chatHistory", chatHistory } }; | ||
|
||
string[] userMessages = [ | ||
"What is Seattle?", | ||
"What is the population of Seattle?", | ||
"What is the area of Seattle?", | ||
"What is the weather in Seattle?", | ||
"What is the zip code of Seattle?", | ||
"What is the elevation of Seattle?", | ||
"What is the latitude of Seattle?", | ||
"What is the longitude of Seattle?", | ||
"What is the mayor of Seattle?" | ||
]; | ||
|
||
foreach (var userMessage in userMessages) | ||
{ | ||
chatHistory.AddUserMessage(userMessage); | ||
OutputLastMessage(chatHistory); | ||
|
||
var function = kernel.CreateFunctionFromPrompt( | ||
new() | ||
{ | ||
Template = | ||
""" | ||
{{#each (chatHistory)}} | ||
<message role="{{Role}}">{{Content}}</message> | ||
{{/each}} | ||
""", | ||
TemplateFormat = "handlebars" | ||
}, | ||
new HandlebarsPromptTemplateFactory() | ||
); | ||
|
||
var response = await kernel.InvokeAsync(function, arguments); | ||
|
||
chatHistory.AddAssistantMessage(response.ToString()); | ||
OutputLastMessage(chatHistory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters