From 99d5f3d404da71468d0532b049f666b197805ac7 Mon Sep 17 00:00:00 2001 From: Akshara Ramakrishnan <95243381+akshara-msft@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:00:53 -0600 Subject: [PATCH 1/3] make the basic semantic kernel chat work for console app --- sk-csharp-console-chat/ConsoleChat.cs | 16 +++++++--------- sk-csharp-console-chat/Program.cs | 2 +- .../config/ServiceCollectionExtensions.cs | 2 +- sk-csharp-console-chat/plugins/LightPlugin.cs | 2 +- .../sk-csharp-console-chat.csproj | 2 +- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/sk-csharp-console-chat/ConsoleChat.cs b/sk-csharp-console-chat/ConsoleChat.cs index 22230dd..4c0962e 100644 --- a/sk-csharp-console-chat/ConsoleChat.cs +++ b/sk-csharp-console-chat/ConsoleChat.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.SemanticKernel; -using Microsoft.SemanticKernel.AI.ChatCompletion; -using Microsoft.SemanticKernel.Connectors.AI.OpenAI; +using Microsoft.SemanticKernel.ChatCompletion; +using Microsoft.SemanticKernel.Connectors.OpenAI; /// /// This is the main application service. @@ -48,15 +48,9 @@ private async Task ExecuteAsync(CancellationToken cancellationToken) System.Console.Write("User > "); chatMessages.AddUserMessage(Console.ReadLine()!); - // Get the chat completions - OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() - { - FunctionCallBehavior = FunctionCallBehavior.AutoInvokeKernelFunctions - }; IAsyncEnumerable result = chatCompletionService.GetStreamingChatMessageContentsAsync( chatMessages, - executionSettings: openAIPromptExecutionSettings, kernel: this._kernel, cancellationToken: cancellationToken); @@ -69,13 +63,17 @@ private async Task ExecuteAsync(CancellationToken cancellationToken) System.Console.Write("Assistant > "); chatMessageContent = new( content.Role ?? AuthorRole.Assistant, - content.ModelId!, content.Content!, + content.ModelId!, content.InnerContent, content.Encoding, content.Metadata ); } + if (content.Content is null) + { + continue; + } System.Console.Write(content.Content); chatMessageContent!.Content += content.Content; } diff --git a/sk-csharp-console-chat/Program.cs b/sk-csharp-console-chat/Program.cs index 7adc070..b3209ad 100644 --- a/sk-csharp-console-chat/Program.cs +++ b/sk-csharp-console-chat/Program.cs @@ -26,7 +26,7 @@ services .AddSingleton(kernelSettings) .AddTransient(serviceProvider => { - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Information)); builder.Services.AddChatCompletionService(kernelSettings); builder.Plugins.AddFromType(); diff --git a/sk-csharp-console-chat/config/ServiceCollectionExtensions.cs b/sk-csharp-console-chat/config/ServiceCollectionExtensions.cs index 50172a0..7e14570 100644 --- a/sk-csharp-console-chat/config/ServiceCollectionExtensions.cs +++ b/sk-csharp-console-chat/config/ServiceCollectionExtensions.cs @@ -14,7 +14,7 @@ internal static IServiceCollection AddChatCompletionService(this IServiceCollect switch (kernelSettings.ServiceType.ToUpperInvariant()) { case ServiceTypes.AzureOpenAI: - serviceCollection = serviceCollection.AddAzureOpenAIChatCompletion(kernelSettings.DeploymentId, kernelSettings.ModelId, endpoint: kernelSettings.Endpoint, apiKey: kernelSettings.ApiKey, serviceId: kernelSettings.ServiceId); + serviceCollection = serviceCollection.AddAzureOpenAIChatCompletion(kernelSettings.DeploymentId, endpoint: kernelSettings.Endpoint, apiKey: kernelSettings.ApiKey, serviceId: kernelSettings.ServiceId); break; case ServiceTypes.OpenAI: diff --git a/sk-csharp-console-chat/plugins/LightPlugin.cs b/sk-csharp-console-chat/plugins/LightPlugin.cs index dccacff..5da2d54 100644 --- a/sk-csharp-console-chat/plugins/LightPlugin.cs +++ b/sk-csharp-console-chat/plugins/LightPlugin.cs @@ -1,6 +1,6 @@ using System.ComponentModel; using Microsoft.SemanticKernel; -using Microsoft.SemanticKernel.AI.ChatCompletion; +using Microsoft.SemanticKernel.ChatCompletion; namespace Plugins; diff --git a/sk-csharp-console-chat/sk-csharp-console-chat.csproj b/sk-csharp-console-chat/sk-csharp-console-chat.csproj index 1f47ae9..69e77d5 100644 --- a/sk-csharp-console-chat/sk-csharp-console-chat.csproj +++ b/sk-csharp-console-chat/sk-csharp-console-chat.csproj @@ -13,7 +13,7 @@ - + From 3efe9e7fd4a91dba8ebf3629784ac23a6893c5f4 Mon Sep 17 00:00:00 2001 From: Akshara Ramakrishnan <95243381+akshara-msft@users.noreply.github.com> Date: Fri, 26 Jan 2024 18:08:18 -0600 Subject: [PATCH 2/3] only add if not null --- sk-csharp-console-chat/ConsoleChat.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sk-csharp-console-chat/ConsoleChat.cs b/sk-csharp-console-chat/ConsoleChat.cs index 4c0962e..3de60cd 100644 --- a/sk-csharp-console-chat/ConsoleChat.cs +++ b/sk-csharp-console-chat/ConsoleChat.cs @@ -76,9 +76,13 @@ private async Task ExecuteAsync(CancellationToken cancellationToken) } System.Console.Write(content.Content); chatMessageContent!.Content += content.Content; + } System.Console.WriteLine(); - chatMessages.AddMessage(chatMessageContent!); + if (chatMessageContent is not null && chatMessageContent.Content is not null) + { + chatMessages.AddAssistantMessage(chatMessageContent!.Content ?? ""); + } } } } From 81c3115d318072f16e73778ff344b4ec289fdfda Mon Sep 17 00:00:00 2001 From: Akshara Ramakrishnan <95243381+akshara-msft@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:44:47 -0600 Subject: [PATCH 3/3] change the chat --- sk-csharp-console-chat/ConsoleChat.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sk-csharp-console-chat/ConsoleChat.cs b/sk-csharp-console-chat/ConsoleChat.cs index 4c0962e..9a4879f 100644 --- a/sk-csharp-console-chat/ConsoleChat.cs +++ b/sk-csharp-console-chat/ConsoleChat.cs @@ -48,9 +48,16 @@ private async Task ExecuteAsync(CancellationToken cancellationToken) System.Console.Write("User > "); chatMessages.AddUserMessage(Console.ReadLine()!); + // Get the chat completions + OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() + { + ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions + }; + IAsyncEnumerable result = chatCompletionService.GetStreamingChatMessageContentsAsync( chatMessages, + openAIPromptExecutionSettings, kernel: this._kernel, cancellationToken: cancellationToken); @@ -78,7 +85,10 @@ private async Task ExecuteAsync(CancellationToken cancellationToken) chatMessageContent!.Content += content.Content; } System.Console.WriteLine(); - chatMessages.AddMessage(chatMessageContent!); + if (chatMessageContent is not null && chatMessageContent.Content is not null) + { + chatMessages.AddAssistantMessage(chatMessageContent!.Content ?? ""); + } } } }