Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextCompletion Fixes + Interface simplification #1391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0133c7e
IText Reuslts changes WIP
RogerBarreto Jun 8, 2023
7b9c50b
Simplifying IText interface names
RogerBarreto Jun 8, 2023
50f433f
Merge main
RogerBarreto Jun 9, 2023
749cdc6
Small fix and remark set for the old interfaces behavior only support…
RogerBarreto Jun 9, 2023
c6ffa30
Removing multiple appended chat messages from the extension method
RogerBarreto Jun 9, 2023
2c04942
Removing vscode file
RogerBarreto Jun 9, 2023
af4aeff
Trying version typo ignore
RogerBarreto Jun 9, 2023
32bd0d5
Trying version typo ignore
RogerBarreto Jun 9, 2023
08a460f
Reverting typos changes
RogerBarreto Jun 9, 2023
2041a09
Merge branch 'main' of https://github.com/microsoft/semantic-kernel i…
RogerBarreto Jun 9, 2023
461e277
Merge branch 'main' into features/itextcompletion-simpleinterfaces
RogerBarreto Jun 9, 2023
3ecd266
Merge branch 'main' into features/itextcompletion-simpleinterfaces
RogerBarreto Jun 13, 2023
91c0ab5
Add missing docs
RogerBarreto Jun 13, 2023
1981439
Remove unecessary blank lines
RogerBarreto Jun 13, 2023
7d30bf6
Merge branch 'main' into features/itextcompletion-simpleinterfaces
RogerBarreto Jun 13, 2023
7618927
Merge branch 'main' into features/itextcompletion-simpleinterfaces
RogerBarreto Jun 13, 2023
58a8248
Bumping version due to breaking change
RogerBarreto Jun 13, 2023
66ef162
Merge branch 'features/itextcompletion-simpleinterfaces' of https://g…
RogerBarreto Jun 13, 2023
adfc66f
Merge branch 'main' into features/itextcompletion-simpleinterfaces
lemillermicrosoft Jun 13, 2023
e521ed4
Merge branch 'main' into features/itextcompletion-simpleinterfaces
lemillermicrosoft Jun 14, 2023
e1995be
Merge branch 'main' into features/itextcompletion-simpleinterfaces
shawncal Jun 14, 2023
cb970d5
Merge branch 'main' into features/itextcompletion-simpleinterfaces
RogerBarreto Jun 14, 2023
21c349a
Addresss PR feedback
RogerBarreto Jun 14, 2023
79caa51
PR Addressing changes
RogerBarreto Jun 14, 2023
1e4f043
Merge branch 'main' into features/itextcompletion-simpleinterfaces
lemillermicrosoft Jun 14, 2023
4c802c3
Merge branch 'main' into features/itextcompletion-simpleinterfaces
shawncal Jun 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/nuget/nuget-package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<!-- Central version prefix - applies to all nuget packages. -->
<Version>0.15</Version>
<Version>0.16</Version>

<Configurations>Debug;Release;Publish</Configurations>
<IsPackable>true</IsPackable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient
}

/// <inheritdoc/>
public async IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
public async IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -128,7 +128,7 @@ public HuggingFaceTextCompletion(string model, string? apiKey = null, HttpClient
}

/// <inheritdoc/>
public async Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
public async Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand All @@ -147,7 +147,7 @@ public void Dispose()

#region private ================================================================================

private async Task<IReadOnlyList<ITextCompletionStreamingResult>> ExecuteGetCompletionsAsync(string text, CancellationToken cancellationToken = default)
private async Task<IReadOnlyList<ITextStreamingResult>> ExecuteGetCompletionsAsync(string text, CancellationToken cancellationToken = default)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class ClientBase
/// <param name="requestSettings">Request settings for the completion API</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>Completions generated by the remote model</returns>
private protected async Task<IReadOnlyList<ITextCompletionResult>> InternalGetTextResultsAsync(
private protected async Task<IReadOnlyList<ITextResult>> InternalGetTextResultsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand All @@ -69,7 +69,7 @@ public abstract class ClientBase
throw new OpenAIInvalidResponseException<Completions>(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();
}

/// <summary>
Expand All @@ -79,7 +79,7 @@ public abstract class ClientBase
/// <param name="requestSettings">Request settings for the completion API</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>Stream the completions generated by the remote model</returns>
private protected async IAsyncEnumerable<TextCompletionStreamingResult> InternalGetTextStreamingResultsAsync(
private protected async IAsyncEnumerable<TextStreamingResult> InternalGetTextStreamingResultsAsync(
string text,
CompleteRequestSettings requestSettings,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -95,7 +95,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);
}
}

Expand Down Expand Up @@ -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>(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);
}
Expand All @@ -220,7 +213,7 @@ private protected static OpenAIChatHistory InternalCreateNewChat(string? instruc
return new OpenAIChatHistory(instructions);
}

private protected async Task<IReadOnlyList<ITextCompletionResult>> InternalGetChatResultsAsTextAsync(
private protected async Task<IReadOnlyList<ITextResult>> InternalGetChatResultsAsTextAsync(
string text,
CompleteRequestSettings? textSettings,
CancellationToken cancellationToken = default)
Expand All @@ -229,11 +222,11 @@ private protected static OpenAIChatHistory InternalCreateNewChat(string? instruc
ChatHistory chat = PrepareChatHistory(text, textSettings, out ChatRequestSettings chatSettings);

return (await this.InternalGetChatResultsAsync(chat, chatSettings, cancellationToken).ConfigureAwait(false))
.OfType<ITextCompletionResult>()
.OfType<ITextResult>()
.ToList();
}

private protected async IAsyncEnumerable<ITextCompletionStreamingResult> InternalGetChatStreamingResultsAsTextAsync(
private protected async IAsyncEnumerable<ITextStreamingResult> InternalGetChatStreamingResultsAsTextAsync(
string text,
CompleteRequestSettings? textSettings,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
Expand All @@ -242,7 +235,7 @@ private protected static OpenAIChatHistory InternalCreateNewChat(string? instruc

await foreach (var chatCompletionStreamingResult in this.InternalGetChatStreamingResultsAsync(chat, chatSettings, cancellationToken))
{
yield return (ITextCompletionStreamingResult)chatCompletionStreamingResult;
yield return (ITextStreamingResult)chatCompletionStreamingResult;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;

internal sealed class TextCompletionResult : ITextCompletionResult
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

namespace Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;

internal sealed class TextCompletionStreamingResult : ITextCompletionStreamingResult
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ChatHistory CreateNewChat(string? instructions = null)
}

/// <inheritdoc/>
public IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings? requestSettings = null,
CancellationToken cancellationToken = default)
Expand All @@ -86,7 +86,7 @@ public ChatHistory CreateNewChat(string? instructions = null)
}

/// <inheritdoc/>
public Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings? requestSettings = null,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ChatHistory CreateNewChat(string? instructions = null)
}

/// <inheritdoc/>
public IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings? requestSettings = null,
CancellationToken cancellationToken = default)
Expand All @@ -69,7 +69,7 @@ public ChatHistory CreateNewChat(string? instructions = null)
}

/// <inheritdoc/>
public Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings? requestSettings = null,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public sealed class AzureTextCompletion : AzureOpenAIClientBase, ITextCompletion
}

/// <inheritdoc/>
public IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand All @@ -61,7 +61,7 @@ public sealed class AzureTextCompletion : AzureOpenAIClientBase, ITextCompletion
}

/// <inheritdoc/>
public Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public sealed class OpenAITextCompletion : OpenAIClientBase, ITextCompletion
}

/// <inheritdoc/>
public IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand All @@ -44,7 +44,7 @@ public sealed class OpenAITextCompletion : OpenAIClientBase, ITextCompletion
}

/// <inheritdoc/>
public Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -17,21 +16,21 @@ public static class ChatCompletionExtensions
/// <param name="chat">Chat history</param>
/// <param name="requestSettings">AI request settings</param>
/// <param name="cancellationToken">Async cancellation token</param>
/// <remarks>This extension does not support multiple prompt results (Only the first will be returned)</remarks>
/// <returns>Stream the generated chat message in string format</returns>
public static async IAsyncEnumerable<string> GenerateMessageStreamAsync(
this IChatCompletion chatCompletion,
ChatHistory chat,
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;
}
}

Expand All @@ -42,6 +41,7 @@ await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMes
/// <param name="chat">Chat history</param>
/// <param name="requestSettings">AI request settings</param>
/// <param name="cancellationToken">Async cancellation token</param>
/// <remarks>This extension does not support multiple prompt results (Only the first will be returned)</remarks>
/// <returns>Generated chat message in string format</returns>
public static async Task<string> GenerateMessageAsync(
this IChatCompletion chatCompletion,
Expand All @@ -50,14 +50,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);
}

return messageContent.ToString();
return firstChatMessage.Content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Microsoft.SemanticKernel.AI.ChatCompletion;

/// <summary>
/// Interface for chat completion services
/// </summary>
public interface IChatCompletion : IAIService
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Microsoft.SemanticKernel.AI.ChatCompletion;

/// <summary>
/// Interface for chat completion results
/// </summary>
public interface IChatResult
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Microsoft.SemanticKernel.AI.ChatCompletion;

/// <summary>
/// Interface for chat completion streaming results
/// </summary>
public interface IChatStreamingResult : IChatResult
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Microsoft.SemanticKernel.AI.ImageGeneration;

/// <summary>
/// Interface for image generation services
/// </summary>
public interface IImageGeneration : IAIService
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ITextCompletion : IAIService
/// <param name="requestSettings">Request settings for the completion API</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>List of different completions results generated by the remote model</returns>
Task<IReadOnlyList<ITextCompletionResult>> GetCompletionsAsync(
Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default);
Expand All @@ -31,7 +31,7 @@ public interface ITextCompletion : IAIService
/// <param name="requestSettings">Request settings for the completion API</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>List of different completion streaming results generated by the remote model</returns>
IAsyncEnumerable<ITextCompletionStreamingResult> GetStreamingCompletionsAsync(
IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(
string text,
CompleteRequestSettings requestSettings,
CancellationToken cancellationToken = default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// 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
/// <summary>
/// Interface for text completion results
/// </summary>
[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
{
/// <summary>
/// Gets the model result data.
/// </summary>
ModelResult ModelResult { get; }

/// <summary>
/// Get the text completion from the result.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
/// <returns>Text completion content</returns>
Task<string> GetCompletionAsync(CancellationToken cancellationToken = default);
}
Loading
Loading