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: estimated prompt tokens are not equal to api response #405

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: estimated prompt tokens are not equal to api response
  • Loading branch information
aiperon committed Aug 18, 2023
commit 56cecc03ab7fa48701dfec0cc9954c8af44430c5
8 changes: 4 additions & 4 deletions bot/openai_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,19 +429,19 @@ def __count_tokens(self, messages) -> int:

if model in GPT_3_MODELS + GPT_3_16K_MODELS:
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
tokens_per_role = -1 # values with role keys are not counted
elif model in GPT_4_MODELS + GPT_4_32K_MODELS:
tokens_per_message = 3
tokens_per_name = 1
tokens_per_role = 0
else:
raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}.""")
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
if key == "role":
num_tokens += tokens_per_role
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens

Expand Down