From 0133c7e680db8c801e855da8e687e914ca34ea21 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Thu, 8 Jun 2023 12:16:08 +0100 Subject: [PATCH 01/13] IText Reuslts changes WIP --- .../HuggingFaceTextCompletion.cs | 6 +++--- .../TextCompletion/TextCompletionResult.cs | 2 +- .../AzureSdk/ChatCompletionAsTextResult.cs | 2 +- .../AzureSdk/ClientBase.cs | 6 +++--- .../AzureSdk/TextCompletionResult.cs | 2 +- .../AzureSdk/TextCompletionStreamingResult.cs | 2 +- .../ChatCompletion/AzureChatCompletion.cs | 6 +++--- .../ChatCompletion/OpenAIChatCompletion.cs | 6 +++--- .../TextCompletion/AzureTextCompletion.cs | 4 ++-- .../TextCompletion/OpenAITextCompletion.cs | 4 ++-- .../AI/TextCompletion/ITextCompletion.cs | 4 ++-- .../AI/TextCompletion/ITextCompletionResult.cs | 13 +++---------- .../ITextCompletionStreamingResult.cs | 7 +++---- .../AI/TextCompletion/ITextResult.cs | 18 ++++++++++++++++++ .../AI/TextCompletion/ITextStreamingResult.cs | 11 +++++++++++ .../TextCompletion/TextCompletionExtensions.cs | 2 +- .../Security/TrustServiceTests.cs | 4 ++-- .../SkillDefinition/SKFunctionTests1.cs | 4 ++-- .../SkillDefinition/SKFunction.cs | 4 ++-- .../Example16_CustomLLM.cs | 8 ++++---- .../Example36_MultiCompletion.cs | 2 +- .../Example37_MultiStreamingCompletion.cs | 2 +- .../Example43_GetModelResult.cs | 2 +- 23 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs create mode 100644 dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs diff --git a/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/HuggingFaceTextCompletion.cs b/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/HuggingFaceTextCompletion.cs index 0331fb9c6043..e81d16b91834 100644 --- a/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/HuggingFaceTextCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/HuggingFaceTextCompletion.cs @@ -116,7 +116,7 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient } /// - public async IAsyncEnumerable GetStreamingCompletionsAsync( + public async IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) @@ -128,7 +128,7 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient } /// - public async Task> GetCompletionsAsync( + public async Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -147,7 +147,7 @@ public void Dispose() #region private ================================================================================ - private async Task> ExecuteGetCompletionsAsync(string text, CancellationToken cancellationToken = default) + private async Task> ExecuteGetCompletionsAsync(string text, CancellationToken cancellationToken = default) { try { diff --git a/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/TextCompletionResult.cs b/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/TextCompletionResult.cs index 63fd6bb68831..08bddc651164 100644 --- a/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/TextCompletionResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.HuggingFace/TextCompletion/TextCompletionResult.cs @@ -9,7 +9,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.HuggingFace.TextCompletion; -internal sealed class TextCompletionStreamingResult : ITextCompletionStreamingResult +internal sealed class TextCompletionStreamingResult : ITextStreamingResult { private readonly ModelResult _responseData; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs index bab4ded4c581..5103c6818dca 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs @@ -9,7 +9,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class ChatCompletionAsTextResult : ITextCompletionStreamingResult +internal sealed class ChatCompletionAsTextResult : ITextStreamingResult { private readonly Func> _getCompletionStreamingAsyncImpl; private readonly Func> _getCompletionAsyncImpl; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index bf275673b115..6c455c9ba118 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -40,7 +40,7 @@ public abstract class ClientBase /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// Completions generated by the remote model - private protected async Task> InternalCompleteTextAsync( + private protected async Task> InternalCompleteTextAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -229,9 +229,9 @@ private protected static ChatHistory InternalCreateNewChat(string? instructions return new OpenAIChatHistory(instructions); } - private protected List InternalGetTextCompletionAsChat(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken) + private protected List InternalGetTextCompletionAsChat(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken) { - return new List + return new List { new ChatCompletionAsTextResult( (cancellationTokenInvoke) => this.InternalCompleteTextUsingChatStreamAsync(text, requestSettings, cancellationTokenInvoke), diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs index fa324cb7e5e6..9179560c98a5 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs @@ -8,7 +8,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class TextCompletionResult : ITextCompletionResult +internal sealed class TextCompletionResult : ITextResult { private readonly ModelResult _modelResult; private readonly Choice _choice; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs index a37ef2021427..d1d5332ddbf2 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs @@ -10,7 +10,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class TextCompletionStreamingResult : ITextCompletionStreamingResult +internal sealed class TextCompletionStreamingResult : ITextStreamingResult { private readonly ModelResult _modelResult; private readonly StreamingChoice _choice; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs index a4cca388cd85..98773eed9050 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs @@ -78,7 +78,7 @@ public ChatHistory CreateNewChat(string? instructions = null) } /// - public IAsyncEnumerable GetStreamingCompletionsAsync( + public IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -87,11 +87,11 @@ public ChatHistory CreateNewChat(string? instructions = null) } /// - public Task> GetCompletionsAsync( + public Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) { - return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); + return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs index 362d86e92b9b..b650ea76326b 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs @@ -61,7 +61,7 @@ public ChatHistory CreateNewChat(string? instructions = null) } /// - public IAsyncEnumerable GetStreamingCompletionsAsync( + public IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -70,11 +70,11 @@ public ChatHistory CreateNewChat(string? instructions = null) } /// - public Task> GetCompletionsAsync( + public Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) { - return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); + return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/AzureTextCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/AzureTextCompletion.cs index 30b5fd48bb52..6b1f1d40bbcd 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/AzureTextCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/AzureTextCompletion.cs @@ -52,7 +52,7 @@ public sealed class AzureTextCompletion : AzureOpenAIClientBase, ITextCompletion } /// - public IAsyncEnumerable GetStreamingCompletionsAsync( + public IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -61,7 +61,7 @@ public sealed class AzureTextCompletion : AzureOpenAIClientBase, ITextCompletion } /// - public Task> GetCompletionsAsync( + public Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/OpenAITextCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/OpenAITextCompletion.cs index 96654433ca27..cb1463f788e6 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/OpenAITextCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/TextCompletion/OpenAITextCompletion.cs @@ -35,7 +35,7 @@ public sealed class OpenAITextCompletion : OpenAIClientBase, ITextCompletion } /// - public IAsyncEnumerable GetStreamingCompletionsAsync( + public IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -44,7 +44,7 @@ public sealed class OpenAITextCompletion : OpenAIClientBase, ITextCompletion } /// - public Task> GetCompletionsAsync( + public Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletion.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletion.cs index a84df6e8380f..71ef848584f0 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletion.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletion.cs @@ -19,7 +19,7 @@ public interface ITextCompletion : IAIService /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// List of different completions results generated by the remote model - Task> GetCompletionsAsync( + Task> GetCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default); @@ -31,7 +31,7 @@ public interface ITextCompletion : IAIService /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// List of different completion streaming results generated by the remote model - IAsyncEnumerable GetStreamingCompletionsAsync( + IAsyncEnumerable GetStreamingCompletionsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default); diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs index 77d960b30336..fe222d680531 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs @@ -1,17 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. -using System.Threading; -using System.Threading.Tasks; -using Microsoft.SemanticKernel.Orchestration; +using System; namespace Microsoft.SemanticKernel.AI.TextCompletion; -public interface ITextCompletionResult +[Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextResult interface instead.")] +public interface ITextCompletionResult : ITextResult { - /// - /// Gets the model result data. - /// - ModelResult ModelResult { get; } - - Task GetCompletionAsync(CancellationToken cancellationToken = default); } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs index d10ea612c6a8..b4af1d21d74d 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs @@ -1,11 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. -using System.Collections.Generic; -using System.Threading; +using System; namespace Microsoft.SemanticKernel.AI.TextCompletion; -public interface ITextCompletionStreamingResult : ITextCompletionResult +[Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextStreamingResult interface instead.")] +public interface ITextCompletionStreamingResult : ITextStreamingResult { - IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default); } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs new file mode 100644 index 000000000000..129c0c314242 --- /dev/null +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.SemanticKernel.Orchestration; + +namespace Microsoft.SemanticKernel.AI.TextCompletion; + +public interface ITextResult +{ + /// + /// Gets the model result data. + /// + ModelResult ModelResult { get; } + + Task GetCompletionAsync(CancellationToken cancellationToken = default); +} diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs new file mode 100644 index 000000000000..93b64cac077d --- /dev/null +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Collections.Generic; +using System.Threading; + +namespace Microsoft.SemanticKernel.AI.TextCompletion; + +public interface ITextStreamingResult : ITextResult +{ + IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default); +} diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs index d4f99adc5874..8dbe07105948 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs @@ -30,7 +30,7 @@ public static class TextCompletionExtensions StringBuilder completionResult = new(); - foreach (ITextCompletionResult result in completions) + foreach (ITextResult result in completions) { completionResult.Append(await result.GetCompletionAsync(cancellationToken).ConfigureAwait(false)); } diff --git a/dotnet/src/SemanticKernel.UnitTests/Security/TrustServiceTests.cs b/dotnet/src/SemanticKernel.UnitTests/Security/TrustServiceTests.cs index 615c822b8505..e4966c1d631f 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Security/TrustServiceTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Security/TrustServiceTests.cs @@ -230,7 +230,7 @@ public void Function1() private static Mock MockAIService() { var aiService = new Mock(); - var textCompletionResult = new Mock(); + var textCompletionResult = new Mock(); textCompletionResult .Setup(x => x.GetCompletionAsync(It.IsAny())) @@ -238,7 +238,7 @@ private static Mock MockAIService() aiService .Setup(x => x.GetCompletionsAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(new List { textCompletionResult.Object }); + .ReturnsAsync(new List { textCompletionResult.Object }); return aiService; } diff --git a/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests1.cs b/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests1.cs index 8076aedc91e1..38af1b6022e8 100644 --- a/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests1.cs +++ b/dotnet/src/SemanticKernel.UnitTests/SkillDefinition/SKFunctionTests1.cs @@ -223,7 +223,7 @@ private static Mock MockPromptTemplate() private static Mock MockAIService(string result) { var aiService = new Mock(); - var textCompletionResult = new Mock(); + var textCompletionResult = new Mock(); textCompletionResult .Setup(x => x.GetCompletionAsync(It.IsAny())) @@ -231,7 +231,7 @@ private static Mock MockAIService(string result) aiService .Setup(x => x.GetCompletionsAsync(It.IsAny(), It.IsAny(), It.IsAny())) - .ReturnsAsync(new List { textCompletionResult.Object }); + .ReturnsAsync(new List { textCompletionResult.Object }); return aiService; } diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs index 8586de44952d..8caa024e8256 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs @@ -367,11 +367,11 @@ private struct MethodDetails public bool IsSensitive { get; set; } } - private static async Task GetCompletionsResultContentAsync(IReadOnlyList completions, CancellationToken cancellationToken = default) + private static async Task GetCompletionsResultContentAsync(IReadOnlyList completions, CancellationToken cancellationToken = default) { StringBuilder completionResult = new(); - foreach (ITextCompletionResult result in completions) + foreach (ITextResult result in completions) { completionResult.Append(await result.GetCompletionAsync(cancellationToken).ConfigureAwait(false)); } diff --git a/samples/dotnet/kernel-syntax-examples/Example16_CustomLLM.cs b/samples/dotnet/kernel-syntax-examples/Example16_CustomLLM.cs index c9d3d9da64c2..fa0d1c7e339f 100644 --- a/samples/dotnet/kernel-syntax-examples/Example16_CustomLLM.cs +++ b/samples/dotnet/kernel-syntax-examples/Example16_CustomLLM.cs @@ -23,21 +23,21 @@ */ public class MyTextCompletionService : ITextCompletion { - public Task> GetCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) + public Task> GetCompletionsAsync(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) { - return Task.FromResult>(new List + return Task.FromResult>(new List { new MyTextCompletionStreamingResult() }); } - public async IAsyncEnumerable GetStreamingCompletionsAsync(string text, CompleteRequestSettings requestSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) + public async IAsyncEnumerable GetStreamingCompletionsAsync(string text, CompleteRequestSettings requestSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) { yield return new MyTextCompletionStreamingResult(); } } -public class MyTextCompletionStreamingResult : ITextCompletionStreamingResult +public class MyTextCompletionStreamingResult : ITextStreamingResult { private readonly ModelResult _modelResult = new(new { diff --git a/samples/dotnet/kernel-syntax-examples/Example36_MultiCompletion.cs b/samples/dotnet/kernel-syntax-examples/Example36_MultiCompletion.cs index 56a8a5df32b6..c4b8dde6abb5 100644 --- a/samples/dotnet/kernel-syntax-examples/Example36_MultiCompletion.cs +++ b/samples/dotnet/kernel-syntax-examples/Example36_MultiCompletion.cs @@ -61,7 +61,7 @@ private static async Task TextCompletionAsync(ITextCompletion textCompletion) var prompt = "Write one paragraph why AI is awesome"; - foreach (ITextCompletionResult completionResult in await textCompletion.GetCompletionsAsync(prompt, requestSettings)) + foreach (ITextResult completionResult in await textCompletion.GetCompletionsAsync(prompt, requestSettings)) { Console.WriteLine(await completionResult.GetCompletionAsync()); Console.WriteLine("-------------"); diff --git a/samples/dotnet/kernel-syntax-examples/Example37_MultiStreamingCompletion.cs b/samples/dotnet/kernel-syntax-examples/Example37_MultiStreamingCompletion.cs index 8a8697fff95c..0e1c67ab0a7d 100644 --- a/samples/dotnet/kernel-syntax-examples/Example37_MultiStreamingCompletion.cs +++ b/samples/dotnet/kernel-syntax-examples/Example37_MultiStreamingCompletion.cs @@ -100,7 +100,7 @@ await foreach (string message in completionResult.GetCompletionStreamingAsync()) Console.WriteLine(); } - private static async Task ProcessStreamAsyncEnumerableAsync(ITextCompletionStreamingResult result, int resultNumber, int linesPerResult) + private static async Task ProcessStreamAsyncEnumerableAsync(ITextStreamingResult result, int resultNumber, int linesPerResult) { var fullSentence = string.Empty; await foreach (var word in result.GetCompletionStreamingAsync()) diff --git a/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs b/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs index e6efd6e48981..187094120715 100644 --- a/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs +++ b/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs @@ -53,7 +53,7 @@ public static async Task RunAsync() var textCompletion = kernel.GetService(); var prompt = FunctionDefinition.Replace("{{$input}}", $"Translate this date {DateTimeOffset.Now:f} to French format", StringComparison.InvariantCultureIgnoreCase); - IReadOnlyList completionResults = await textCompletion.GetCompletionsAsync(prompt, new CompleteRequestSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 }); + IReadOnlyList completionResults = await textCompletion.GetCompletionsAsync(prompt, new CompleteRequestSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 }); Console.WriteLine(await completionResults[0].GetCompletionAsync()); Console.WriteLine(completionResults[0].ModelResult.GetOpenAIResult().Usage.AsJson()); Console.WriteLine(); From 7b9c50b5534fb22992271a90a89a5fddeae2e046 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Thu, 8 Jun 2023 16:00:42 +0100 Subject: [PATCH 02/13] Simplifying IText interface names --- dotnet/.vscode/settings.json | 3 +++ .../Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs | 6 +++--- .../AzureSdk/{TextCompletionResult.cs => TextResult.cs} | 4 ++-- ...tCompletionStreamingResult.cs => TextStreamingResult.cs} | 4 ++-- .../AI/TextCompletion/ITextResult.cs | 1 - 5 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 dotnet/.vscode/settings.json rename dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/{TextCompletionResult.cs => TextResult.cs} (84%) rename dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/{TextCompletionStreamingResult.cs => TextStreamingResult.cs} (87%) diff --git a/dotnet/.vscode/settings.json b/dotnet/.vscode/settings.json new file mode 100644 index 000000000000..d0039370f31f --- /dev/null +++ b/dotnet/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "dotnet.defaultSolution": "SK-dotnet.sln" +} \ No newline at end of file diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index 6c455c9ba118..a1fc43d2918f 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -65,7 +65,7 @@ public abstract class ClientBase throw new OpenAIInvalidResponseException(responseData, "Text completions not found"); } - return responseData.Choices.Select(choice => new TextCompletionResult(responseData, choice)).ToList(); + return responseData.Choices.Select(choice => new TextResult(responseData, choice)).ToList(); } /// @@ -75,7 +75,7 @@ public abstract class ClientBase /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// Stream the completions generated by the remote model - private protected async IAsyncEnumerable InternalCompletionStreamAsync( + private protected async IAsyncEnumerable InternalCompletionStreamAsync( string text, CompleteRequestSettings requestSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) @@ -91,7 +91,7 @@ public abstract class ClientBase using StreamingCompletions streamingChatCompletions = response.Value; await foreach (StreamingChoice choice in streamingChatCompletions.GetChoicesStreaming(cancellationToken)) { - yield return new TextCompletionStreamingResult(streamingChatCompletions, choice); + yield return new TextStreamingResult(streamingChatCompletions, choice); } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextResult.cs similarity index 84% rename from dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs rename to dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextResult.cs index 9179560c98a5..8448cba4a2a2 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextResult.cs @@ -8,12 +8,12 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class TextCompletionResult : ITextResult +internal sealed class TextResult : ITextResult { private readonly ModelResult _modelResult; private readonly Choice _choice; - public TextCompletionResult(Completions resultData, Choice choice) + public TextResult(Completions resultData, Choice choice) { this._modelResult = new ModelResult(resultData); this._choice = choice; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextStreamingResult.cs similarity index 87% rename from dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs rename to dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextStreamingResult.cs index d1d5332ddbf2..00061d84ce65 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextCompletionStreamingResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/TextStreamingResult.cs @@ -10,14 +10,14 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class TextCompletionStreamingResult : ITextStreamingResult +internal sealed class TextStreamingResult : ITextStreamingResult { private readonly ModelResult _modelResult; private readonly StreamingChoice _choice; public ModelResult ModelResult => this._modelResult; - public TextCompletionStreamingResult(StreamingCompletions resultData, StreamingChoice choice) + public TextStreamingResult(StreamingCompletions resultData, StreamingChoice choice) { this._modelResult = new ModelResult(resultData); this._choice = choice; diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs index 129c0c314242..06f79f217452 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs @@ -1,6 +1,5 @@ // Copyright (c) Microsoft. All rights reserved. -using System; using System.Threading; using System.Threading.Tasks; using Microsoft.SemanticKernel.Orchestration; From 749cdc6365b1c38e2854bd8dbbce41f01d5f920b Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 13:30:52 +0100 Subject: [PATCH 03/13] Small fix and remark set for the old interfaces behavior only supporting 1 result --- .../AzureSdk/ChatCompletionAsTextResult.cs | 34 ------------------- .../AzureSdk/ChatResult.cs | 2 +- .../AzureSdk/ChatStreamingResult.cs | 2 +- .../AzureSdk/ClientBase.cs | 26 ++++---------- .../ChatCompletion/AzureChatCompletion.cs | 4 --- .../ChatCompletion/OpenAIChatCompletion.cs | 4 --- .../ChatCompletionExtensions.cs | 3 ++ .../TextCompletion/ITextCompletionResult.cs | 14 -------- .../ITextCompletionStreamingResult.cs | 9 ----- .../TextCompletionExtensions.cs | 21 ++++-------- .../SkillDefinition/SKFunction.cs | 3 ++ .../Example43_GetModelResult.cs | 17 ++++++---- 12 files changed, 30 insertions(+), 109 deletions(-) delete mode 100644 dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs deleted file mode 100644 index 5103c6818dca..000000000000 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatCompletionAsTextResult.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.SemanticKernel.AI.TextCompletion; -using Microsoft.SemanticKernel.Orchestration; - -namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; - -internal sealed class ChatCompletionAsTextResult : ITextStreamingResult -{ - private readonly Func> _getCompletionStreamingAsyncImpl; - private readonly Func> _getCompletionAsyncImpl; - - public ChatCompletionAsTextResult( - Func> getCompletionStreamingAsyncImpl, - Func> getCompletionAsyncImpl) - { - this._getCompletionStreamingAsyncImpl = getCompletionStreamingAsyncImpl; - this._getCompletionAsyncImpl = getCompletionAsyncImpl; - } - -#pragma warning disable CS8603 - public ModelResult ModelResult => null; //TODO: implement when IChatCompletionResult PR is merged -#pragma warning restore CS8603 - - public Task GetCompletionAsync(CancellationToken cancellationToken = default) - => this._getCompletionAsyncImpl(cancellationToken); - - public IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default) - => this._getCompletionStreamingAsyncImpl(cancellationToken); -} diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatResult.cs index 554e27c2aae0..bb7950b1d18b 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatResult.cs @@ -10,7 +10,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class ChatResult : IChatResult, ITextCompletionResult +internal sealed class ChatResult : IChatResult, ITextResult { private readonly ModelResult _modelResult; private readonly ChatChoice _choice; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatStreamingResult.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatStreamingResult.cs index 0dd8092ccfd4..f9ae288381cd 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatStreamingResult.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ChatStreamingResult.cs @@ -14,7 +14,7 @@ namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk; -internal sealed class ChatStreamingResult : IChatStreamingResult, ITextCompletionStreamingResult +internal sealed class ChatStreamingResult : IChatStreamingResult, ITextStreamingResult { private readonly ModelResult _modelResult; private readonly StreamingChatChoice _choice; diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index 7f919c8f7a37..35b6860760c9 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -44,11 +44,7 @@ public abstract class ClientBase /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// Completions generated by the remote model -<<<<<<< HEAD - private protected async Task> InternalCompleteTextAsync( -======= - private protected async Task> InternalGetTextResultsAsync( ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 + private protected async Task> InternalGetTextResultsAsync( string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken = default) @@ -83,11 +79,7 @@ >>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . /// Stream the completions generated by the remote model -<<<<<<< HEAD - private protected async IAsyncEnumerable InternalCompletionStreamAsync( -======= - private protected async IAsyncEnumerable InternalGetTextStreamingResultsAsync( ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 + private protected async IAsyncEnumerable InternalGetTextStreamingResultsAsync( string text, CompleteRequestSettings requestSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) @@ -228,12 +220,7 @@ private protected static OpenAIChatHistory InternalCreateNewChat(string? instruc return new OpenAIChatHistory(instructions); } -<<<<<<< HEAD - private protected List InternalGetTextCompletionAsChat(string text, CompleteRequestSettings requestSettings, CancellationToken cancellationToken) - { - return new List -======= - private protected async Task> InternalGetChatResultsAsTextAsync( + private protected async Task> InternalGetChatResultsAsTextAsync( string text, CompleteRequestSettings? textSettings, CancellationToken cancellationToken = default) @@ -242,11 +229,11 @@ private protected List InternalGetTextCompletionAsChat(str ChatHistory chat = PrepareChatHistory(text, textSettings, out ChatRequestSettings chatSettings); return (await this.InternalGetChatResultsAsync(chat, chatSettings, cancellationToken).ConfigureAwait(false)) - .OfType() + .OfType() .ToList(); } - private protected async IAsyncEnumerable InternalGetChatStreamingResultsAsTextAsync( + private protected async IAsyncEnumerable InternalGetChatStreamingResultsAsTextAsync( string text, CompleteRequestSettings? textSettings, [EnumeratorCancellation] CancellationToken cancellationToken = default) @@ -254,9 +241,8 @@ private protected List InternalGetTextCompletionAsChat(str ChatHistory chat = PrepareChatHistory(text, textSettings, out ChatRequestSettings chatSettings); await foreach (var chatCompletionStreamingResult in this.InternalGetChatStreamingResultsAsync(chat, chatSettings, cancellationToken)) ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 { - yield return (ITextCompletionStreamingResult)chatCompletionStreamingResult; + yield return (ITextStreamingResult)chatCompletionStreamingResult; } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs index 09ed200177da..17440235f18e 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/AzureChatCompletion.cs @@ -91,10 +91,6 @@ public ChatHistory CreateNewChat(string? instructions = null) CompleteRequestSettings? requestSettings = null, CancellationToken cancellationToken = default) { -<<<<<<< HEAD - return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); -======= return this.InternalGetChatResultsAsTextAsync(text, requestSettings, cancellationToken); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 } } diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs index 2856ebdd1f72..897bfaa8fb89 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/ChatCompletion/OpenAIChatCompletion.cs @@ -74,10 +74,6 @@ public ChatHistory CreateNewChat(string? instructions = null) CompleteRequestSettings? requestSettings = null, CancellationToken cancellationToken = default) { -<<<<<<< HEAD - return Task.FromResult(this.InternalGetTextCompletionAsChat(text, requestSettings, cancellationToken) as IReadOnlyList); -======= return this.InternalGetChatResultsAsTextAsync(text, requestSettings, cancellationToken); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 } } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs index 6a94a6dc4e2e..2cbd324d847c 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs @@ -17,6 +17,7 @@ public static class ChatCompletionExtensions /// Chat history /// AI request settings /// Async cancellation token + /// This extension does not support multiple prompt results (Only the first will be returned) /// Stream the generated chat message in string format public static async IAsyncEnumerable GenerateMessageStreamAsync( this IChatCompletion chatCompletion, @@ -32,6 +33,7 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes { yield return chatMessageStream.Content; } + yield break; } } @@ -42,6 +44,7 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes /// Chat history /// AI request settings /// Async cancellation token + /// This extension does not support multiple prompt results (Only the first will be returned) /// Generated chat message in string format public static async Task GenerateMessageAsync( this IChatCompletion chatCompletion, diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs index 3ce7f7b4e369..fe222d680531 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs @@ -7,18 +7,4 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextResult interface instead.")] public interface ITextCompletionResult : ITextResult { -<<<<<<< HEAD -======= - /// - /// Gets the model result data. - /// - ModelResult ModelResult { get; } - - /// - /// Get the text completion from the result. - /// - /// The to monitor for cancellation requests. The default is . - /// Text completion content - Task GetCompletionAsync(CancellationToken cancellationToken = default); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs index 4d8bdbc4ebdd..b4af1d21d74d 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs @@ -7,13 +7,4 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextStreamingResult interface instead.")] public interface ITextCompletionStreamingResult : ITextStreamingResult { -<<<<<<< HEAD -======= - /// - /// Get the streaming text completion from the result. - /// - /// The to monitor for cancellation requests. The default is . - /// Current streaming text content for the iteration - IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs index 26a15f7d8a38..3172ee86fd38 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/TextCompletionExtensions.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -20,6 +19,7 @@ public static class TextCompletionExtensions /// The prompt to complete. /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . + /// This extension does not support multiple prompt results (Only the first will be returned) /// Text generated by the remote model public static async Task CompleteAsync(this ITextCompletion textCompletion, string text, @@ -29,18 +29,7 @@ public static class TextCompletionExtensions var completions = await textCompletion.GetCompletionsAsync(text, requestSettings, cancellationToken).ConfigureAwait(false); var firstResult = completions[0]; -<<<<<<< HEAD - StringBuilder completionResult = new(); - - foreach (ITextResult result in completions) - { - completionResult.Append(await result.GetCompletionAsync(cancellationToken).ConfigureAwait(false)); - } - - return completionResult.ToString(); -======= return await firstResult.GetCompletionAsync(cancellationToken).ConfigureAwait(false); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 } /// @@ -50,6 +39,7 @@ >>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 /// The prompt to complete. /// Request settings for the completion API /// The to monitor for cancellation requests. The default is . + /// This extension does not support multiple prompt results (Only the first will be returned) /// Streaming content of the text generated by the remote model public static async IAsyncEnumerable CompleteStreamAsync(this ITextCompletion textCompletion, string text, @@ -57,13 +47,14 @@ >>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 [EnumeratorCancellation] CancellationToken cancellationToken = default) { var completionResults = textCompletion.GetStreamingCompletionsAsync(text, requestSettings, cancellationToken); - var firstResult = await completionResults.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false); - if (firstResult is not null) + + await foreach (var completionResult in completionResults) { - await foreach (var word in firstResult.GetCompletionStreamingAsync(cancellationToken).ConfigureAwait(false)) + await foreach (var word in completionResult.GetCompletionStreamingAsync(cancellationToken).ConfigureAwait(false)) { yield return word; } + yield break; } } } diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs index 8caa024e8256..dab2a293fb34 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs @@ -374,6 +374,9 @@ private static async Task GetCompletionsResultContentAsync(IReadOnlyList foreach (ITextResult result in completions) { completionResult.Append(await result.GetCompletionAsync(cancellationToken).ConfigureAwait(false)); + + // To avoid any unexpected behavior we only take the first completion result (when running from the Kernel) + break; } return completionResult.ToString(); diff --git a/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs b/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs index fb1ebbc1e931..9d1573dbcab2 100644 --- a/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs +++ b/samples/dotnet/kernel-syntax-examples/Example43_GetModelResult.cs @@ -38,8 +38,8 @@ public static async Task RunAsync() var excuseFunction = kernel.CreateSemanticFunction(FunctionDefinition, maxTokens: 100, temperature: 0.4, topP: 1); - // Using InvokeAsync - var textResult = await excuseFunction.InvokeAsync("I missed the F1 final race"); + // Using InvokeAsync with 3 results (Currently invoke only supports 1 result, but you can get the other results from the ModelResults) + var textResult = await excuseFunction.InvokeAsync("I missed the F1 final race", new CompleteRequestSettings { ResultsPerPrompt = 3 }); Console.WriteLine(textResult); Console.WriteLine(textResult.ModelResults.Select(result => result.GetOpenAITextResult()).AsJson()); Console.WriteLine(); @@ -50,15 +50,18 @@ public static async Task RunAsync() Console.WriteLine(textResult.ModelResults.LastOrDefault()?.GetOpenAITextResult()?.Usage.AsJson()); Console.WriteLine(); + // Using the Kernel RunAsync + textResult = await kernel.RunAsync("sorry I forgot your birthday", excuseFunction); + Console.WriteLine(textResult); + Console.WriteLine(textResult.ModelResults.LastOrDefault()?.GetOpenAITextResult()?.Usage.AsJson()); + Console.WriteLine(); + // Using Chat Completion directly var chatCompletion = new OpenAIChatCompletion("gpt-3.5-turbo", Env.Var("OPENAI_API_KEY")); var prompt = FunctionDefinition.Replace("{{$input}}", $"Translate this date {DateTimeOffset.Now:f} to French format", StringComparison.InvariantCultureIgnoreCase); -<<<<<<< HEAD - IReadOnlyList completionResults = await textCompletion.GetCompletionsAsync(prompt, new CompleteRequestSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 }); -======= - IReadOnlyList completionResults = await chatCompletion.GetCompletionsAsync(prompt, new CompleteRequestSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 }); ->>>>>>> f4e92eb1de8c8e222c51e0fd8e46e0e6be5650b7 + IReadOnlyList completionResults = await chatCompletion.GetCompletionsAsync(prompt, new CompleteRequestSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 }); + Console.WriteLine(await completionResults[0].GetCompletionAsync()); Console.WriteLine(completionResults[0].ModelResult.GetOpenAIChatResult().Usage.AsJson()); Console.WriteLine(); From c6ffa30107b838d6cad70e115d698f9b77ed1e7a Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 14:51:27 +0100 Subject: [PATCH 04/13] Removing multiple appended chat messages from the extension method --- .../AI/ChatCompletion/ChatCompletionExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs index 2cbd324d847c..9e5343ca1ba8 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs @@ -59,6 +59,7 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes { var chatMessage = await chatResult.GetChatMessageAsync(cancellationToken).ConfigureAwait(false); messageContent.Append(chatMessage.Content); + break; } return messageContent.ToString(); From 2c0494259f6b9edf9b16daa8d02d4adc92c2f206 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 14:59:57 +0100 Subject: [PATCH 05/13] Removing vscode file --- dotnet/.vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 dotnet/.vscode/settings.json diff --git a/dotnet/.vscode/settings.json b/dotnet/.vscode/settings.json deleted file mode 100644 index d0039370f31f..000000000000 --- a/dotnet/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "dotnet.defaultSolution": "SK-dotnet.sln" -} \ No newline at end of file From af4aeff0ebde3cad5b8e066462444ab71774c9d7 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:20:31 +0100 Subject: [PATCH 06/13] Trying version typo ignore --- .github/_typos.toml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/_typos.toml b/.github/_typos.toml index f703cd1a1952..bf16e98ca2e1 100644 --- a/.github/_typos.toml +++ b/.github/_typos.toml @@ -21,4 +21,11 @@ extend-exclude = [ ACI = "ACI" # Azure Container Instance [default.extend-identifiers] -ags = "ags" # Azure Graph Service \ No newline at end of file +ags = "ags" # Azure Graph Service + +[default] +extend-ignore-identifiers-re = [ + + "[0-9]+[a-zA-Z]{3}[0-9]+", # treat any three letters surrounded by numbers as an identifier + "[Vv]ersion=\"(.+)\"" # treat any text within Version attribute +] \ No newline at end of file From 32bd0d5fddfae1167bcffc2f1b17c417a9f50318 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:20:44 +0100 Subject: [PATCH 07/13] Trying version typo ignore --- .github/_typos.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/_typos.toml b/.github/_typos.toml index bf16e98ca2e1..4c15f8045880 100644 --- a/.github/_typos.toml +++ b/.github/_typos.toml @@ -26,6 +26,5 @@ ags = "ags" # Azure Graph Service [default] extend-ignore-identifiers-re = [ - "[0-9]+[a-zA-Z]{3}[0-9]+", # treat any three letters surrounded by numbers as an identifier "[Vv]ersion=\"(.+)\"" # treat any text within Version attribute ] \ No newline at end of file From 08a460f5effc2a5a2bb73a8ff23caa1a8ee14e90 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:28:44 +0100 Subject: [PATCH 08/13] Reverting typos changes --- .github/_typos.toml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/_typos.toml b/.github/_typos.toml index 4c15f8045880..f703cd1a1952 100644 --- a/.github/_typos.toml +++ b/.github/_typos.toml @@ -21,10 +21,4 @@ extend-exclude = [ ACI = "ACI" # Azure Container Instance [default.extend-identifiers] -ags = "ags" # Azure Graph Service - -[default] -extend-ignore-identifiers-re = [ - - "[Vv]ersion=\"(.+)\"" # treat any text within Version attribute -] \ No newline at end of file +ags = "ags" # Azure Graph Service \ No newline at end of file From 91c0ab5f598c6e98599895719e258b2c480d2649 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Tue, 13 Jun 2023 13:51:45 +0100 Subject: [PATCH 09/13] Add missing docs --- .../AI/ChatCompletion/IChatCompletion.cs | 4 ++++ .../AI/ChatCompletion/IChatResult.cs | 3 +++ .../AI/ChatCompletion/IChatStreamingResult.cs | 3 +++ .../AI/ImageGeneration/IImageGeneration.cs | 3 +++ .../AI/TextCompletion/ITextCompletionResult.cs | 4 ++++ .../AI/TextCompletion/ITextCompletionStreamingResult.cs | 4 ++++ .../AI/TextCompletion/ITextResult.cs | 3 +++ .../AI/TextCompletion/ITextStreamingResult.cs | 4 ++++ 8 files changed, 28 insertions(+) diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs index c49ace6728f1..1b33fe07a49b 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs @@ -7,6 +7,10 @@ namespace Microsoft.SemanticKernel.AI.ChatCompletion; +/// +/// Interface for chat completion services +/// + public interface IChatCompletion : IAIService { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatResult.cs index c332de91d5d9..adbadcaa53e3 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatResult.cs @@ -5,6 +5,9 @@ namespace Microsoft.SemanticKernel.AI.ChatCompletion; +/// +/// Interface for chat completion results +/// public interface IChatResult { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatStreamingResult.cs index 9fa914c856d0..5c99240967b5 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatStreamingResult.cs @@ -5,6 +5,9 @@ namespace Microsoft.SemanticKernel.AI.ChatCompletion; +/// +/// Interface for chat completion streaming results +/// public interface IChatStreamingResult : IChatResult { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/IImageGeneration.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/IImageGeneration.cs index ded72f7855c7..7554234dd6e9 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/IImageGeneration.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ImageGeneration/IImageGeneration.cs @@ -6,6 +6,9 @@ namespace Microsoft.SemanticKernel.AI.ImageGeneration; +/// +/// Interface for image generation services +/// public interface IImageGeneration : IAIService { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs index fe222d680531..366943decd63 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs @@ -4,6 +4,10 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; +/// +/// Interface for text completion results +/// + [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextResult interface instead.")] public interface ITextCompletionResult : ITextResult { diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs index b4af1d21d74d..5d683cdd2677 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs @@ -4,6 +4,10 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; +/// +/// Interface for text completion streaming results +/// + [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextStreamingResult interface instead.")] public interface ITextCompletionStreamingResult : ITextStreamingResult { diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs index 06f79f217452..c652b5d4a162 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextResult.cs @@ -6,6 +6,9 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; +/// +/// Interface for text completion results +/// public interface ITextResult { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs index 93b64cac077d..6c9ad15c76ae 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs @@ -5,6 +5,10 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; +/// +/// Interface for text completion streaming results +/// + public interface ITextStreamingResult : ITextResult { IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default); From 198143904f348063f5163c7459a81d54432c98a3 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Tue, 13 Jun 2023 13:53:42 +0100 Subject: [PATCH 10/13] Remove unecessary blank lines --- .../AI/ChatCompletion/IChatCompletion.cs | 1 - .../AI/TextCompletion/ITextCompletionResult.cs | 1 - .../AI/TextCompletion/ITextCompletionStreamingResult.cs | 1 - .../AI/TextCompletion/ITextStreamingResult.cs | 1 - 4 files changed, 4 deletions(-) diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs index 1b33fe07a49b..354cb24b954b 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/IChatCompletion.cs @@ -10,7 +10,6 @@ namespace Microsoft.SemanticKernel.AI.ChatCompletion; /// /// Interface for chat completion services /// - public interface IChatCompletion : IAIService { /// diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs index 366943decd63..51d456666553 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionResult.cs @@ -7,7 +7,6 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; /// /// Interface for text completion results /// - [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextResult interface instead.")] public interface ITextCompletionResult : ITextResult { diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs index 5d683cdd2677..af0a429f1474 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextCompletionStreamingResult.cs @@ -7,7 +7,6 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; /// /// Interface for text completion streaming results /// - [Obsolete("This interface is deprecated and will be removed in one of the next SK SDK versions. Use the ITextStreamingResult interface instead.")] public interface ITextCompletionStreamingResult : ITextStreamingResult { diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs index 6c9ad15c76ae..7b4c8a8bf9aa 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/TextCompletion/ITextStreamingResult.cs @@ -8,7 +8,6 @@ namespace Microsoft.SemanticKernel.AI.TextCompletion; /// /// Interface for text completion streaming results /// - public interface ITextStreamingResult : ITextResult { IAsyncEnumerable GetCompletionStreamingAsync(CancellationToken cancellationToken = default); From 58a82485c9470ded0daf63af8d5fbd977ce53984 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Tue, 13 Jun 2023 18:46:23 +0100 Subject: [PATCH 11/13] Bumping version due to breaking change --- dotnet/nuget/nuget-package.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/nuget/nuget-package.props b/dotnet/nuget/nuget-package.props index 26a681cf6b3e..803927ede074 100644 --- a/dotnet/nuget/nuget-package.props +++ b/dotnet/nuget/nuget-package.props @@ -1,7 +1,7 @@ - 0.15 + 0.16 Debug;Release;Publish true From 21c349a2bb92c365b649bf422e691ead1bf826e1 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Wed, 14 Jun 2023 13:58:05 +0100 Subject: [PATCH 12/13] Addresss PR feedback --- .../Connectors.AI.OpenAI/AzureSdk/ClientBase.cs | 9 +-------- .../ChatCompletion/ChatCompletionExtensions.cs | 17 +++-------------- .../SkillDefinition/SKFunction.cs | 14 ++------------ 3 files changed, 6 insertions(+), 34 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs index 35b6860760c9..d70a6cc59b60 100644 --- a/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs +++ b/dotnet/src/Connectors/Connectors.AI.OpenAI/AzureSdk/ClientBase.cs @@ -197,14 +197,7 @@ await foreach (StreamingChoice choice in streamingChatCompletions.GetChoicesStre } using StreamingChatCompletions streamingChatCompletions = response.Value; - - var choices = await response.Value.GetChoicesStreaming(cancellationToken).ToListAsync(cancellationToken).ConfigureAwait(false); - if (choices.Count == 0) - { - throw new OpenAIInvalidResponseException(streamingChatCompletions, "Streaming chat completions not found"); - } - - foreach (StreamingChatChoice choice in choices) + await foreach (StreamingChatChoice choice in streamingChatCompletions.GetChoicesStreaming(cancellationToken).ConfigureAwait(false)) { yield return new ChatStreamingResult(response.Value, choice); } diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs index 9e5343ca1ba8..174a168fa44f 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -25,15 +24,12 @@ public static class ChatCompletionExtensions ChatRequestSettings? requestSettings = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { - var chatCompletionResults = chatCompletion.GetStreamingChatCompletionsAsync(chat, requestSettings, cancellationToken).ConfigureAwait(false); - - await foreach (var chatCompletionResult in chatCompletionResults) + await foreach (var chatCompletionResult in chatCompletion.GetStreamingChatCompletionsAsync(chat, requestSettings, cancellationToken).ConfigureAwait(false)) { await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMessageAsync(cancellationToken).ConfigureAwait(false)) { yield return chatMessageStream.Content; } - yield break; } } @@ -53,15 +49,8 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes CancellationToken cancellationToken = default) { var chatResults = await chatCompletion.GetChatCompletionsAsync(chat, requestSettings, cancellationToken).ConfigureAwait(false); + var firstChatMessage = await chatResults[0].GetChatMessageAsync(cancellationToken).ConfigureAwait(false); - StringBuilder messageContent = new(); - foreach (var chatResult in chatResults) - { - var chatMessage = await chatResult.GetChatMessageAsync(cancellationToken).ConfigureAwait(false); - messageContent.Append(chatMessage.Content); - break; - } - - return messageContent.ToString(); + return firstChatMessage.Content; } } diff --git a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs index dab2a293fb34..a9d1f7832a5e 100644 --- a/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs +++ b/dotnet/src/SemanticKernel/SkillDefinition/SKFunction.cs @@ -5,7 +5,6 @@ using System.Diagnostics; using System.Linq; using System.Reflection; -using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -369,17 +368,8 @@ private struct MethodDetails private static async Task GetCompletionsResultContentAsync(IReadOnlyList completions, CancellationToken cancellationToken = default) { - StringBuilder completionResult = new(); - - foreach (ITextResult result in completions) - { - completionResult.Append(await result.GetCompletionAsync(cancellationToken).ConfigureAwait(false)); - - // To avoid any unexpected behavior we only take the first completion result (when running from the Kernel) - break; - } - - return completionResult.ToString(); + // To avoid any unexpected behavior we only take the first completion result (when running from the Kernel) + return await completions[0].GetCompletionAsync(cancellationToken).ConfigureAwait(false); } internal SKFunction( From 79caa51c794564736c3c3a5ea446ed61a775d4e5 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:18:35 +0100 Subject: [PATCH 13/13] PR Addressing changes --- .../AI/ChatCompletion/ChatCompletionExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs index 174a168fa44f..66757c8c4a57 100644 --- a/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs +++ b/dotnet/src/SemanticKernel.Abstractions/AI/ChatCompletion/ChatCompletionExtensions.cs @@ -30,6 +30,7 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes { yield return chatMessageStream.Content; } + yield break; } }