From 9c7ee13d5ada281af0b8dc9180cc7903bf1cd5f5 Mon Sep 17 00:00:00 2001 From: Eduardo Letsch Date: Thu, 16 Nov 2023 22:25:07 -0300 Subject: [PATCH] chore!: add error verification on callback from stream --- gpt3.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gpt3.go b/gpt3.go index f25d11d..7a28983 100644 --- a/gpt3.go +++ b/gpt3.go @@ -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. @@ -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 } @@ -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