@@ -83,7 +83,7 @@ type Client interface {
83
83
84
84
// ChatCompletion creates a completion with the Chat completion endpoint which
85
85
// is what powers the ChatGPT experience.
86
- ChatCompletionStream (ctx context.Context ,request ChatCompletionRequest ,onData func (* ChatCompletionStreamResponse ))error
86
+ ChatCompletionStream (ctx context.Context ,request ChatCompletionRequest ,onData func (* ChatCompletionStreamResponse )error )error
87
87
88
88
// Completion creates a completion with the default engine. This is the main endpoint of the API
89
89
// which auto-completes based on the given prompt.
@@ -211,7 +211,7 @@ func (c *client) ChatCompletion(ctx context.Context, request ChatCompletionReque
211
211
func (c * client )ChatCompletionStream (
212
212
ctx context.Context ,
213
213
request ChatCompletionRequest ,
214
- onData func (* ChatCompletionStreamResponse ))error {
214
+ onData func (* ChatCompletionStreamResponse )error )error {
215
215
if request .Model == "" {
216
216
request .Model = GPT3Dot5Turbo
217
217
}
@@ -252,7 +252,9 @@ func (c *client) ChatCompletionStream(
252
252
if err := json .Unmarshal (line ,output );err != nil {
253
253
return fmt .Errorf ("invalid json stream data: %v" ,err )
254
254
}
255
- onData (output )
255
+ if err := onData (output );err != nil {
256
+ return fmt .Errorf ("callback returned an error: %v" ,err )
257
+ }
256
258
}
257
259
258
260
return nil