Files
semantic-kernel/dotnet/samples/Concepts/ChatCompletion/OpenAI_ChatCompletionStreaming.cs
T
Roger Barreto f66a30bbd9 .Net Samples Restructure - Phase 4 (#6057)
### Motivation and Context

Make samples more console friendly.

This change allows the code to be copy and pasted in a Console App,
which virtually no code changes to the core code in sample
implementations.
2024-04-30 11:34:44 +00:00

95 lines
3.4 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace ChatCompletion;
// The following example shows how to use Semantic Kernel with streaming Chat Completion
public class OpenAI_ChatCompletionStreaming(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public Task OpenAIChatStreamSampleAsync()
{
Console.WriteLine("======== Open AI - ChatGPT Streaming ========");
OpenAIChatCompletionService chatCompletionService = new(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
return this.StartStreamingChatAsync(chatCompletionService);
}
[Fact]
public Task AzureOpenAIChatStreamSampleAsync()
{
Console.WriteLine("======== Azure Open AI - ChatGPT Streaming ========");
AzureOpenAIChatCompletionService chatCompletionService = new(
deploymentName: TestConfiguration.AzureOpenAI.ChatDeploymentName,
endpoint: TestConfiguration.AzureOpenAI.Endpoint,
apiKey: TestConfiguration.AzureOpenAI.ApiKey,
modelId: TestConfiguration.AzureOpenAI.ChatModelId);
return this.StartStreamingChatAsync(chatCompletionService);
}
private async Task StartStreamingChatAsync(IChatCompletionService chatCompletionService)
{
Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");
var chatHistory = new ChatHistory("You are a librarian, expert about books");
await MessageOutputAsync(chatHistory);
// First user message
chatHistory.AddUserMessage("Hi, I'm looking for book suggestions");
await MessageOutputAsync(chatHistory);
// First bot assistant message
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);
// Second user message
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion?");
await MessageOutputAsync(chatHistory);
// Second bot assistant message
await StreamMessageOutputAsync(chatCompletionService, chatHistory, AuthorRole.Assistant);
}
private async Task StreamMessageOutputAsync(IChatCompletionService chatCompletionService, ChatHistory chatHistory, AuthorRole authorRole)
{
bool roleWritten = false;
string fullMessage = string.Empty;
await foreach (var chatUpdate in chatCompletionService.GetStreamingChatMessageContentsAsync(chatHistory))
{
if (!roleWritten && chatUpdate.Role.HasValue)
{
Console.Write($"{chatUpdate.Role.Value}: {chatUpdate.Content}");
roleWritten = true;
}
if (chatUpdate.Content is { Length: > 0 })
{
fullMessage += chatUpdate.Content;
Console.Write(chatUpdate.Content);
}
}
Console.WriteLine("\n------------------------");
chatHistory.AddMessage(authorRole, fullMessage);
}
/// <summary>
/// Outputs the last message of the chat history
/// </summary>
private Task MessageOutputAsync(ChatHistory chatHistory)
{
var message = chatHistory.Last();
Console.WriteLine($"{message.Role}: {message.Content}");
Console.WriteLine("------------------------");
return Task.CompletedTask;
}
}