-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Improve token counter to handle more response types #15501
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, looks good to me. I have one question re: estimate_tokens_in_messages
.
possible_input_keys = ("prompt_tokens", "input_tokens") | ||
possible_output_keys = ("completion_tokens", "output_tokens") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like Anthropic uses input_tokens
and output_tokens
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup!
response: Union["CompletionResponse", "ChatResponse"] | ||
) -> Tuple[int, int]: | ||
"""Get the token counts from a raw response.""" | ||
usage = response.raw.get("usage", {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can just model dump the raw response if its not already a dict
APIs like anthropic may have a slightly different naming structure for pulling out token counts
imo longer term, this logic should probably be specific to the LLM class, but this solves some common issues