openai-java icon indicating copy to clipboard operation
openai-java copied to clipboard

Can we add tokens count in ChatCompletionChunk?

Open Crow9527 opened this issue 3 years ago • 2 comments

Crow9527 avatar Apr 07 '23 14:04 Crow9527

This is not currently possible because when stream=True for @Streaming @POST("/v1/completions") and @Streaming @POST("/v1/chat/completions"), the usage property and the choices.finish_reason field are not set. Hopefully, they'll take this feedback into account in the future.

I'd like to share a workaround I found on the OpenAI API Community Forum. You can calculate the token usage yourself by combining the prompt tokens count (using tiktoken) and the number of events received in the stream. Here's a link to a guide on using tiktoken to calculate token counts.

tgolob avatar Apr 10 '23 18:04 tgolob

Thank you. I'll give it a try

Crow9527 avatar Apr 11 '23 00:04 Crow9527