Skip to content

Commit

Permalink
.Net: Create sample showing how to render ChatHistory to a prompt and…
Browse files Browse the repository at this point in the history
… 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
markwallace-microsoft authored Sep 20, 2024
1 parent 05d99d6 commit 8e25752
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
64 changes: 64 additions & 0 deletions dotnet/samples/Concepts/PromptTemplates/ChatLoopWithPrompt.cs
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using Microsoft.SemanticKernel;

namespace ChatPrompts;
namespace PromptTemplates;

public sealed class SafeChatPrompts : BaseTest, IDisposable
{
Expand Down
2 changes: 2 additions & 0 deletions dotnet/samples/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom
- [TemplateLanguage](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/PromptTemplates/TemplateLanguage.cs)
- [PromptyFunction](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/PromptTemplates/PromptyFunction.cs)
- [HandlebarsVisionPrompts](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/PromptTemplates/HandlebarsVisionPrompts.cs)
- [SafeChatPrompts](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/PromptTemplates/SafeChatPrompts.cs)
- [ChatLoopWithPrompt](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/PromptTemplates/ChatLoopWithPrompt.cs)

### RAG - Retrieval-Augmented Generation

Expand Down

0 comments on commit 8e25752

Please sign in to comment.