fix: estimated prompt tokens are not equal to api response #405
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I found an issue with the prompt count calculation for GPT-3.5.
On every call to
__count_tokens()
function, the returned value differs from the value returned by the API.The difference is always equal to the number of messages in the conversation.
The most possible reason for that is that the condition
if key == "name":
never evaluates toTrue
.It looks like the condition should be replaced with
key == "role"
. I know, it comes from the openai-cookbook article, but my assumption the things are changed.In addition, for the model GPT-4 the calculated count is always equal to the value returned by the API.
So, we should not add
tokens_per_name
for this model.This PR fixes prompt count calculation for GPT-3.5.
Detailed examples (with
STREAM=false
param) for 3.5 and 4 models before the fix are below . After this fix the values of calculated total and API response are equal.gpt-3.5-turbo-0613
gpt-4-0613