-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Yet Another V2 Assistant Streaming Implemenetation #748
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
base: master
Are you sure you want to change the base?
Conversation
6827844 to
f8d19ae
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #748 +/- ##
==========================================
- Coverage 98.46% 96.61% -1.85%
==========================================
Files 24 26 +2
Lines 1364 1329 -35
==========================================
- Hits 1343 1284 -59
- Misses 15 28 +13
- Partials 6 17 +11 ☔ View full report in Codecov by Sentry. |
This PR implements Assistant Streaming for the OpenAI API V2 (BETA).
https://platform.openai.com/docs/api-reference/assistants-streaming
Key Features
This PR is built on the excellent work of #737. The main departure from @coolbaluk's work is to improve the streaming response type
AssistantStreamEventto handle the polymorphic nature of the stream events better.Work in progress. Would love to seek feedback & suggestions from the community.
Tagging:
@coolbaluk #737
@tanzyy96
@CallOrRet #731
@sashabaranov
Stream Events as Scanner
The StreamerV2 struct
Scanstream events in a loop as they are received from the server.Unlike the current implemenetation of the
CompletionStreaming, he events in the stream are polymorphic and are handled naturally through type switching.The
ScanAPI mimics thebufio.Scannerclass:Unwrappers for Polymporphic Events
Type switching on stream events allows precise and type-safe access. However, it can be tedious to write user-level code for simpler cases. To address this, unwrapper helpers "cast" an event to a specific type and return the value if the cast is successful:
This is similar to the Bytes and Text methods in bufio.Scanner, which provide access to the current item in different forms:
Libraries designed to handle polymorphic data often provide similar unwrapper helpers. For example, the [gjson]https://github.com/tidwall/gjson?tab=readme-ov-file#result-type) library, offer these unwrapper methods for the polymorphic
Resulttype:Stream Events as io.Reader
The most common use case is to just stream the text, and it would be nice to
have a familiar & obvious API for this.
The
StreamerV2struct implements theio.Readerinterface, which wraps theNext/ScanAPI, to provide a simple way read the text deltas from thethread.message.deltaevents.TODO Items
Streaminto other methods likeCreateThreadAndStream.Type()