Skip to content

Conversation

logan-markewich
Copy link
Collaborator

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

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 19, 2024
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Aug 20, 2024
Copy link
Contributor

@nerdai nerdai left a 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.

Comment on lines +35 to +36
possible_input_keys = ("prompt_tokens", "input_tokens")
possible_output_keys = ("completion_tokens", "output_tokens")
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Aug 20, 2024
@logan-markewich logan-markewich merged commit 635b914 into main Aug 20, 2024
8 checks passed
@logan-markewich logan-markewich deleted the logan/improve_token_counter branch August 20, 2024 23:24
response: Union["CompletionResponse", "ChatResponse"]
) -> Tuple[int, int]:
"""Get the token counts from a raw response."""
usage = response.raw.get("usage", {})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will error out saying the following.
'ChatCompletion' object has no attribute 'get'

image

We will have to access the usage attribute like
response.raw.usage
image

Copy link
Collaborator Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants