betalgo / openai

OpenAI .NET sdk - Azure OpenAI, ChatGPT, Whisper, and DALL-E
https://betalgo.github.io/openai/
MIT License
2.88k stars 516 forks source link

The ChatCompletionCreateResponse Usage is null in CreateCompletionAsStream #174

Closed shenglingshi closed 1 year ago

shenglingshi commented 1 year ago

Describe the bug when I use CreateCompletionAsStream() method,the response not include Usage object,but CreateCompletion() include.

Your code piece

 var chatResponse=openAiService.ChatCompletion.CreateCompletionAsStream(new ChatCompletionCreateRequest
                    {
                        Messages = new List<ChatMessage>
                        {
                            ChatMessage.FromSystem("You are a helpful assistant."),
                            ChatMessage.FromUser(inputStr),
                        },
                        Model = Models.ChatGpt3_5Turbo
                    });

        var tokens=0;
        await foreach(var chat in chatResponse){
            if (chat.Successful){
                tokens+=chat.Usage?.TotalTokens ?? 0;
                var choice=chat.Choices.First();
                Console.Write(choice.Message.Content);
            }
        }

Result chat.Usage is null

Expected behavior chat.Usage is not null and inclue tokens.

Screenshots

the CreateCompletionAsStream()response image

the CreateCompletion() response image

Desktop (please complete the following information):

kayhantolga commented 1 year ago

OpenAI API doesn't return usage information from stream API. Alternatively, you can use the tokenizer method to calculate usage by yourself.

shenglingshi commented 1 year ago

Ok,TKS...