Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include usage key in create_completion when streaming #1498

Open
zhudotexe opened this issue May 30, 2024 · 0 comments · May be fixed by #1552
Open

Include usage key in create_completion when streaming #1498

zhudotexe opened this issue May 30, 2024 · 0 comments · May be fixed by #1552
Labels
enhancement New feature or request

Comments

@zhudotexe
Copy link

Is your feature request related to a problem? Please describe.
Since create_completion may yield text chunks comprised of multiple tokens per yield (e.g. in the case of multi-byte Unicode characters), counting the number of yields may not equal the number of tokens actually generated by a model. To accurately get the usage statistics of a streamed completion, one has to run the final text through the tokenizer again, despite create_completion already tracking the number of tokens generated by the model.

Describe the solution you'd like
When stream=True in create_completion, the final chunk yielded should include the usage statistics in the 'usage' key.

Describe alternatives you've considered

  • Saving full generated text and running it through the tokenizer again (seems wasteful)
  • Counting the number of yields and hoping we don't have any multi-byte characters (hacky and fragile)

Additional context
The OpenAI API has recently added similar support in their streaming API with the stream_options key: https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options

@abetlen abetlen added the enhancement New feature or request label Jun 4, 2024
@tpfau tpfau linked a pull request Jun 25, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants