Skip to content

Commit

Permalink
Merge pull request #40 from letschers/main
Browse files Browse the repository at this point in the history
[Chore!] Add error verification on callback from stream
  • Loading branch information
tylermann authored Nov 27, 2023
2 parents f29b15e + 9c7ee13 commit c6cd599
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions gpt3.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type Client interface {

// ChatCompletion creates a completion with the Chat completion endpoint which
// is what powers the ChatGPT experience.
ChatCompletionStream(ctx context.Context, request ChatCompletionRequest, onData func(*ChatCompletionStreamResponse)) error
ChatCompletionStream(ctx context.Context, request ChatCompletionRequest, onData func(*ChatCompletionStreamResponse) error) error

// Completion creates a completion with the default engine. This is the main endpoint of the API
// which auto-completes based on the given prompt.
Expand Down Expand Up @@ -211,7 +211,7 @@ func (c *client) ChatCompletion(ctx context.Context, request ChatCompletionReque
func (c *client) ChatCompletionStream(
ctx context.Context,
request ChatCompletionRequest,
onData func(*ChatCompletionStreamResponse)) error {
onData func(*ChatCompletionStreamResponse) error) error {
if request.Model == "" {
request.Model = GPT3Dot5Turbo
}
Expand Down Expand Up @@ -252,7 +252,9 @@ func (c *client) ChatCompletionStream(
if err := json.Unmarshal(line, output); err != nil {
return fmt.Errorf("invalid json stream data: %v", err)
}
onData(output)
if err := onData(output); err != nil {
return fmt.Errorf("callback returned an error: %v", err)
}
}

return nil
Expand Down

0 comments on commit c6cd599

Please sign in to comment.