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

ClientModel: AsyncResultCollection<T> and SSE event collection implementation #43840

Merged
merged 53 commits into from
May 14, 2024

Conversation

annelo-msft
Copy link
Member

@annelo-msft annelo-msft commented May 3, 2024

Overview

System.ClientModel-based clients need to be able to return collections of values from service methods, for example, for endpoints such as OpenAI's streaming chat completions where incremental updates are delivered sequentially via SSE streams. In .NET, we represent such collections as async streams.

What's in this PR

This PR adds sync and async abstractions for collections of results, ResultCollection<T> and AsyncResultCollection<T>. It also adds an internal implementation of an SSE parser and event enumerable that will likely be replaced by dotnet/runtime#98105 when it becomes available. The internal implementation is provided here to enable test coverage and review, as well as illustrate how clients would implement the abstractions to support both convenience method return values (see MockSseClient.cs) and factory-method helpers for use with protocol methods (see MockSseClientExtensions.cs).

Additional notes

Given our timelines, the current implementation uses System.IO.StreamReader, which converts SSE lines to UTF-16. This adds a performance cost in cases where we convert the data payload back to UTF-8, e.g. for use with ModelReaderWriter.Create APIs. It is beyond the scope of this PR to provide UTF-8 stream reading capabilities, but part of our longer term plans for third-party clients.

Also: This builds on a great deal of prior work on the SSE reader by @KrzysztofCwalina and @trrwilson.

Addresses #42284

@azure-sdk
Copy link
Collaborator

API change check

APIView has identified API level changes in this PR and created following API reviews.

System.ClientModel

@annelo-msft annelo-msft changed the title ClientModel: SSE result collection that uses enumerator to dispose ClientModel: AsyncResultCollection<T> and SSE event collection implementation May 10, 2024
@annelo-msft annelo-msft marked this pull request as ready for review May 10, 2024 19:06
@annelo-msft annelo-msft requested review from tg-msft and a team as code owners May 10, 2024 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants