sashabaranov / go-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALL·E, Whisper API wrapper for Go
Apache License 2.0
8.6k stars 1.29k forks source link

index out of bound when streaming with IncludeUsage as true #776

Open aryanshridhar opened 1 week ago

aryanshridhar commented 1 week ago

To Reproduce When streamed chat completion with request having StreamOptions defined as -

req := openai.ChatCompletionRequest{
    Model:     openai.GPT3Dot5Turbo,
    Messages: []openai.ChatCompletionMessage{
        {
            Role:    openai.ChatMessageRoleUser,
            Content: "Lorem ipsum",
        },
    },
    StreamOptions: &openai.StreamOptions{IncludeUsage: true},
    Stream:        true,
}

breaks for last chunk-

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.main()
    /Users/aryan/Desktop/go-openai/examples/completion/main.go:60 +0x4b0
exit status 2

Expected behavior The condition to his the EOF error and stop the streaming process.

Environment (please complete the following information):

aryanshridhar commented 1 week ago

For anyone facing issue till this is fixed, can rely on FinishReason explicitly and return if present

response, err := stream.Recv()
isOver := response.Choices[0].FinishReason

if errors.Is(err, io.EOF) || len(isOver) > 0 {
    return
}