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

feat: Assistant stream mode and event callback #731

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

CallOrRet
Copy link

This update enhances the existing functionality by adding support for Assistant stream mode and stream event callbacks without introducing any breaking changes or modifications to the existing API endpoints.

The implemented features are in line with the OpenAI Assistant streaming API, as documented here: OpenAI Assistant Streaming API

Stream.On("event1", callback1)
Stream.On("event2", callback2)
Stream.Run()

Copy link

codecov bot commented Apr 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.75%. Comparing base (774fc9d) to head (0ac6760).
Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #731      +/-   ##
==========================================
+ Coverage   98.46%   98.75%   +0.29%     
==========================================
  Files          24       25       +1     
  Lines        1364     1205     -159     
==========================================
- Hits         1343     1190     -153     
+ Misses         15        9       -6     
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@sashabaranov
Copy link
Owner

Thank you for the PR! Sorry for a long review process :D

assistant_stream.go Outdated Show resolved Hide resolved
stream.event = "message" // default event for chat completion stream
ctx, cancel := context.WithCancel(context.Background())
stream.handlerCtx = ctx
go func() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't think we're going to merge this as starting goroutines don't fit into the design of this library.

Please refer to the following comments:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to have this in the examples/ folder, though!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. What if I revise the parts where I used goroutines? Would that work better with the design of the library?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CallOrRet I'm totally open to that!

Co-authored-by: Alexander Baranov <677093+sashabaranov@users.noreply.github.com>
Copy link

@sauravexodus sauravexodus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some commented code in this PR as well. But the bigger concern is the incomplete response model.

Comment on lines +15 to +19
type AssistantThreadRunStreamResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Delta MessageDelta `json:"delta,omitempty"`
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very trimmed down version of the response model.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The complete response has a lot many parameters which would be important to run function calling etc. https://platform.openai.com/docs/api-reference/runs/createRun

stream = &AssistantThreadRunStream{
streamReader: resp,
}
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
return stream, nil

withBetaAssistantVersion(c.config.AssistantVersion),
)
if err != nil {
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
return nil, err

stream = &AssistantThreadRunStream{
streamReader: resp,
}
return

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return
return stream, nil

@@ -137,6 +138,7 @@ type RunList struct {

type SubmitToolOutputsRequest struct {
ToolOutputs []ToolOutput `json:"tool_outputs"`
Stream bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Stream bool
Stream bool `json:"stream,omitempty"`

@@ -88,6 +88,7 @@ type RunRequest struct {
AdditionalInstructions string `json:"additional_instructions,omitempty"`
Tools []Tool `json:"tools,omitempty"`
Metadata map[string]any `json:"metadata,omitempty"`
Stream bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Stream bool
Stream bool `json:"stream,omitempty"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants