From c7db9febb0edeba1ea108adc4423b789404ce5f2 Mon Sep 17 00:00:00 2001 From: Kyle Roth Date: Thu, 15 Jun 2023 09:16:03 -0400 Subject: [PATCH] count tokens for new OpenAI model versions (#6195) Trying to call `ChatOpenAI.get_num_tokens_from_messages` returns the following error for the newly announced models `gpt-3.5-turbo-0613` and `gpt-4-0613`: ``` NotImplementedError: get_num_tokens_from_messages() is not presently implemented for model gpt-3.5-turbo-0613.See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens. ``` This adds support for counting tokens for those models, by counting tokens the same way they're counted for the previous versions of `gpt-3.5-turbo` and `gpt-4`. #### reviewers - @hwchase17 - @agola11 --- langchain/chat_models/openai.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/langchain/chat_models/openai.py b/langchain/chat_models/openai.py index 6fe9cc90..6c18b9c8 100644 --- a/langchain/chat_models/openai.py +++ b/langchain/chat_models/openai.py @@ -466,12 +466,12 @@ class ChatOpenAI(BaseChatModel): if sys.version_info[1] <= 7: return super().get_num_tokens_from_messages(messages) model, encoding = self._get_encoding_model() - if model == "gpt-3.5-turbo-0301": + if model.startswith("gpt-3.5-turbo"): # every message follows {role/name}\n{content}\n tokens_per_message = 4 # if there's a name, the role is omitted tokens_per_name = -1 - elif model == "gpt-4-0314": + elif model.startswith("gpt-4"): tokens_per_message = 3 tokens_per_name = 1 else: