-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Yet Another V2 Assistant Streaming Implemenetation #748
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
AssistantStreamEvent
to 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
Scan
stream 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
Scan
API mimics thebufio.Scanner
class: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
Result
type: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
StreamerV2
struct implements theio.Reader
interface, which wraps theNext/Scan
API, to provide a simple way read the text deltas from thethread.message.delta
events.TODO Items
Stream
into other methods likeCreateThreadAndStream
.Type()