openai-java
openai-java copied to clipboard
Can we add tokens count in ChatCompletionChunk?
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.
Thank you. I'll give it a try