Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example44_MultiChatCompletion.cs
T
01afd3f128 .Net: Update KernelSyntaxExamples env variables (#1832)
### Description
Updating KernelSyntaxExamples to use the same environment variable keys
as the Integration tests. This is a step toward making these tests
simpler to run, and even running them as automation in CI tests.

This is a first pass -- more cleanup to be done.

### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [X] The code builds clean without any errors or warnings
- [X] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [X] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: name <email>
Co-authored-by: Dmytro Struk <[email protected]>
2023-07-17 17:36:06 +00:00

83 lines
2.7 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.ChatCompletion;
/**
* The following example shows how to use Semantic Kernel with Multiple Results Text Completion as streaming
*/
// ReSharper disable once InconsistentNaming
public static class Example44_MultiChatCompletion
{
public static async Task RunAsync()
{
await AzureOpenAIMultiChatCompletionAsync();
await OpenAIMultiChatCompletionAsync();
}
private static async Task AzureOpenAIMultiChatCompletionAsync()
{
Console.WriteLine("======== Azure OpenAI - Multiple Chat Completion ========");
AzureChatCompletion azureChatCompletion = new(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
TestConfiguration.AzureOpenAI.Endpoint,
TestConfiguration.AzureOpenAI.ApiKey);
await RunChatAsync(azureChatCompletion);
}
private static async Task OpenAIMultiChatCompletionAsync()
{
Console.WriteLine("======== Open AI - Multiple Chat Completion ========");
OpenAIChatCompletion openAIChatCompletion = new(modelId: TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
await RunChatAsync(openAIChatCompletion);
}
private static async Task RunChatAsync(IChatCompletion chatCompletion)
{
var chatHistory = chatCompletion.CreateNewChat("You are a librarian, expert about books");
// First user message
chatHistory.AddUserMessage("Hi, I'm looking for book 3 different book suggestions about sci-fi");
await MessageOutputAsync(chatHistory);
var chatRequestSettings = new ChatRequestSettings
{
MaxTokens = 1024,
ResultsPerPrompt = 2,
Temperature = 1,
TopP = 0.5,
FrequencyPenalty = 0,
};
// First bot assistant message
foreach (IChatResult chatCompletionResult in await chatCompletion.GetChatCompletionsAsync(chatHistory, chatRequestSettings))
{
ChatMessageBase chatMessage = await chatCompletionResult.GetChatMessageAsync();
chatHistory.Add(chatMessage);
await MessageOutputAsync(chatHistory);
}
Console.WriteLine();
}
/// <summary>
/// Outputs the last message of the chat history
/// </summary>
private static Task MessageOutputAsync(ChatHistory chatHistory)
{
var message = chatHistory.Messages.Last();
Console.WriteLine($"{message.Role}: {message.Content}");
Console.WriteLine("------------------------");
return Task.CompletedTask;
}
}