-
Notifications
You must be signed in to change notification settings - Fork 751
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
Add ToChatCompletion{Async} methods for combining StreamingChatCompletionUpdates #5605
base: main
Are you sure you want to change the base?
Conversation
dd9e6d5
to
bdf387f
Compare
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/StreamingChatCompletionUpdateExtensions.cs
Outdated
Show resolved
Hide resolved
I hadn't realized that |
Neither of them update the chat history with the resulting message. |
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/StreamingChatCompletionUpdateExtensions.cs
Outdated
Show resolved
Hide resolved
...Microsoft.Extensions.AI.Tests/ChatCompletion/StreamingChatCompletionUpdateExtensionsTests.cs
Outdated
Show resolved
Hide resolved
The naming of Do any of the following feel right to you? await foreach (var update in client.CompleteStreamingAsync(messages).AppendMessageTo(messages))
await foreach (var update in client.CompleteStreamingAsync(messages).AppendResultTo(messages))
await foreach (var update in client.CompleteStreamingAsync(messages).AppendResponseTo(messages))
await foreach (var update in client.CompleteStreamingAsync(messages).WithAppendTo(messages))
await foreach (var update in client.CompleteStreamingAsync(messages).ThenAppendTo(messages)) I mildly lean towards |
I tend to associate the |
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/StreamingChatCompletionUpdateExtensions.cs
Outdated
Show resolved
Hide resolved
The hard part here is the ToChatCompletion. With that, someone can do the equivalent of List<StreamingChatCompletionUpdate> updates = [];
await foreach (var update in client.CompleteStreamingAsync(messages))
{
Console.Write(update);
updates.Add(update);
}
messages.Add(updates.ToChatCompletion().Message); Given that all of the raised concerns are about |
This is now just: public static ChatCompletion ToChatCompletion(
this IEnumerable<StreamingChatCompletionUpdate> updates, bool coalesceContent = true) |
(though now that I write that out, I'm wondering if I should add one for IAsyncEnumerable as well) |
0e845f5
to
dda3072
Compare
Refactored to have ToChatCompletion{Async} and to have them in the Abstractions library. Having them there means a leaf client can choose to use e.g. ToChatCompletionAsync to implement its CompleteAsync method around its CompleteStreamingAsync, if desired. |
WithMessageAddedAsync
enables writing code like:and upon completion of the loop,
messages
will contain aChatMessage
merged from all of the updates.ToChatCompletion
is what's used to achieve that, but is also exposed on its own so that code which has an enumerable ofStreamingChatCompletionUpdate
s can merge them into aChatCompletion
. This enables a non-streaming implementation to easily be authored in terms of a streaming one.Microsoft Reviewers: Open in CodeFlow