mirror of
https://github.com/father-bot/chatgpt_telegram_bot.git
synced 2026-08-03 13:27:16 +03:00
Make token counting robust for non-OpenAI models
tiktoken.encoding_for_model and the per-model token table only know OpenAI models, so any other model (Claude or anything routed via OpenRouter) raised. Fall back to the o200k_base encoding and a default tokens_per_message for unknown models so streaming token estimates work for every chat model.
This commit is contained in:
+9
-2
@@ -253,7 +253,12 @@ class ChatGPT:
|
||||
return answer
|
||||
|
||||
def _count_tokens_from_messages(self, messages, answer, model="gpt-3.5-turbo"):
|
||||
encoding = tiktoken.encoding_for_model(model)
|
||||
try:
|
||||
encoding = tiktoken.encoding_for_model(model)
|
||||
except KeyError:
|
||||
# models not known to tiktoken (e.g. Claude or other models routed
|
||||
# via OpenRouter) fall back to a modern encoding for an estimate
|
||||
encoding = tiktoken.get_encoding("o200k_base")
|
||||
|
||||
if model == "gpt-3.5-turbo-16k":
|
||||
tokens_per_message = 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
|
||||
@@ -277,7 +282,9 @@ class ChatGPT:
|
||||
tokens_per_message = 3
|
||||
tokens_per_name = 1
|
||||
else:
|
||||
raise ValueError(f"Unknown model: {model}")
|
||||
# default for newer OpenAI / third-party (OpenRouter) chat models
|
||||
tokens_per_message = 3
|
||||
tokens_per_name = 1
|
||||
|
||||
# input
|
||||
n_input_tokens = 0
|
||||
|
||||
Reference in New Issue
Block a user