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

Why are the tokens counted differently than OpenAI? #2

Open
jucor opened this issue Jul 24, 2023 · 4 comments
Open

Why are the tokens counted differently than OpenAI? #2

jucor opened this issue Jul 24, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@jucor
Copy link

jucor commented Jul 24, 2023

Hi @blaze-Youssef !

First, thanks for the tool, very useful, as I'm stuck in openlimit by similar issues that you met in shobrook/openlimit#4 .

However, I'm a bit stuck trying to understand. What does the argument max_tokens correspond to, please, in https://github.com/blaze-Youssef/openai-ratelimiter/blob/main/openai_ratelimiter/defs.py#L9 ?

I am trying to understand it, but the way you count tokens, which is the same as in openlimit https://github.com/shobrook/openlimit/blob/master/openlimit/utilities/token_counters.py#L14 , is different than in OpenAI's cookbook https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb (see section 6).

Would you or @shobrook be able to help clarify this counting, please?

@jucor
Copy link
Author

jucor commented Jul 24, 2023

Ah! Found max_tokens in the OpenAI API, as an optional parameter: https://platform.openai.com/docs/api-reference/completions/create#completions/create-max_tokens
Do I understand correctly that n*max_tokens` is here to preemptively count into the rate limit the maximum possible number of output tokens?

@jucor
Copy link
Author

jucor commented Jul 24, 2023

Ungh, now I see that OpenAI has a separate token counter in their batch-API script than in their tutorial Notebook: https://github.com/openai/openai-cookbook/blob/main/examples/api_request_parallel_processor.py#L339
The former does indeed take max_tokens into account. But is also less recent than their notebook, which has different token increments for roles' names. So I don't know which version of their token counter to believe 😅

Shouldn't a rate limiter, in any case, be updated after the actual completion is returned by the API, to account for the actual number of output tokens?

@Youssefbenhammouda
Copy link
Owner

Hi,
I will dig into the OpenAI notebooks and update the implémentation if necessary.
As far as I know, each request's tokens are calculated this way:
Prompt tokens + Max tokens = request total tokens.

@jucor
Copy link
Author

jucor commented Jul 25, 2023 via email

Repository owner locked and limited conversation to collaborators Apr 13, 2024
Repository owner unlocked this conversation Apr 13, 2024
@Youssefbenhammouda Youssefbenhammouda added the bug Something isn't working label Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants