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 %tokens command crash #803

Merged
merged 2 commits into from
Dec 2, 2023
Merged
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
11 changes: 10 additions & 1 deletion interpreter/terminal_interface/utils/count_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ def count_tokens(text="", model="gpt-4"):
Count the number of tokens in a string
"""

encoder = tiktoken.encoding_for_model(model)
# Fix bug where models starting with openai/ for example can't find tokenizer
if '/' in model:
model = model.split('/')[-1]

# At least give an estimate if we can't find the tokenizer
try:
encoder = tiktoken.encoding_for_model(model)
except KeyError:
print(f"Could not find tokenizer for {model}. Defaulting to gpt-4 tokenizer.")
encoder = tiktoken.encoding_for_model("gpt-4")

return len(encoder.encode(text))

Expand Down