Replies: 1 comment 2 replies
-
I need this too. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Checked
Feature request
After not providing token usage statistics when streaming, OpenAI has finally implemented this feature and now returns usage statistics when streaming. Testing with the newest OpenAI npm-package, the information is available using the sdk. When using the ChatOpenAI integration in @langchain/openai, the response_metadata field always shows 0 for prompt and completion tokens. It would be nice, if we could get this information, since it is already nicely tallied up in the final completion chunk.
Motivation
Usage information is generally important for businesses building services based on LLMs. With streaming providing the best user experience it's an important feature to have and should support usage tracking.
Proposal (If applicable)
I've overwritten the
ChatOpenAI._streamResponseChunks
method with the following:and setting modelKwargs to include usage:
The API is a little clunky, since the usage info is sent as the last chunk with no choices, but we need to yield a message chunk in order to pass the info to the BaseLanguageModel, which then outputs the
on_llm_end
event and compiles the response_metadata.Maybe there is more a convenient way to this that does not involve emitting an empty message.
Beta Was this translation helpful? Give feedback.
All reactions