mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 11:19:56 +00:00
### Motivation and Context text-davinci-003 is being retired by Azure OpenAI and is no longer supported for new deployments ### Description Update examples and notebooks to use gpt-35-turbo / gpt-3.5-turbo instead ### Contribution Checklist - [ ] 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 😄 --------- Co-authored-by: Shawn Callegari <[email protected]>
93 lines
3.2 KiB
C#
93 lines
3.2 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 streaming Chat Completion
|
|
*/
|
|
// ReSharper disable once InconsistentNaming
|
|
public static class Example33_StreamingChat
|
|
{
|
|
public static async Task RunAsync()
|
|
{
|
|
await AzureOpenAIChatStreamSampleAsync();
|
|
await OpenAIChatStreamSampleAsync();
|
|
}
|
|
|
|
private static async Task OpenAIChatStreamSampleAsync()
|
|
{
|
|
Console.WriteLine("======== Open AI - ChatGPT Streaming ========");
|
|
|
|
OpenAIChatCompletion openAIChatCompletion = new(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
|
|
|
|
await StartStreamingChatAsync(openAIChatCompletion);
|
|
}
|
|
|
|
private static async Task AzureOpenAIChatStreamSampleAsync()
|
|
{
|
|
Console.WriteLine("======== Azure Open AI - ChatGPT Streaming ========");
|
|
|
|
AzureChatCompletion azureChatCompletion = new(
|
|
TestConfiguration.AzureOpenAI.ChatDeploymentName,
|
|
TestConfiguration.AzureOpenAI.Endpoint,
|
|
TestConfiguration.AzureOpenAI.ApiKey);
|
|
|
|
await StartStreamingChatAsync(azureChatCompletion);
|
|
}
|
|
|
|
private static async Task StartStreamingChatAsync(IChatCompletion chatCompletion)
|
|
{
|
|
Console.WriteLine("Chat content:");
|
|
Console.WriteLine("------------------------");
|
|
|
|
var chatHistory = chatCompletion.CreateNewChat("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(chatCompletion, 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(chatCompletion, chatHistory, AuthorRole.Assistant);
|
|
}
|
|
|
|
private static async Task StreamMessageOutputAsync(IChatCompletion chatGPT, ChatHistory chatHistory, AuthorRole authorRole)
|
|
{
|
|
Console.Write($"{authorRole}: ");
|
|
string fullMessage = string.Empty;
|
|
|
|
await foreach (string message in chatGPT.GenerateMessageStreamAsync(chatHistory))
|
|
{
|
|
fullMessage += message;
|
|
Console.Write(message);
|
|
}
|
|
|
|
Console.WriteLine("\n------------------------");
|
|
chatHistory.AddMessage(authorRole, fullMessage);
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|