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

Fix BaseOpenAIChatCompletionClient token usage #4770

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

gziz
Copy link
Contributor

@gziz gziz commented Dec 20, 2024

Why are these changes needed?

To correctly track the token usage in BaseOpenAIChatCompletionClient, more info on the issue.

Related issue number

#4769

Checks

@gziz gziz changed the title Fix openai client token usage (#4769) Fix BaseOpenAIChatCompletionClient token usage Dec 20, 2024
@@ -561,8 +554,7 @@ async def create(
logprobs=logprobs,
)

_add_usage(self._actual_usage, usage)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm wondering what's the difference between these two...

Copy link
Member

Choose a reason for hiding this comment

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

Traditionally actual vs total is a matter of if the tokens were cached or not. But since we don't have caching atm they should be identical

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @jackgerrits! I have updated the PR to use both actual_usage and total_usage.

@gziz gziz requested a review from ekzhu December 21, 2024 01:26
@ekzhu ekzhu requested review from lspinheiro and jackgerrits and removed request for ekzhu December 22, 2024 02:03
@jackgerrits
Copy link
Member

jackgerrits commented Dec 27, 2024

@gziz The original issue what that we're not using the returned value of the function where we add usage. I'd rather a smaller change where we just assign that value back to the appropriate variable. This change couples actual and total usage into the same function.

If we want to make all this simpler, we can implement__add__ and __iadd__:

That way we can update the code to simply be:

self._actual_usage += usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants