From c66fe39cd08ca6510a1bd650857dc86702a188b8 Mon Sep 17 00:00:00 2001 From: Alexey Matveev Date: Thu, 11 Jan 2024 09:58:50 +0300 Subject: [PATCH] Add support for gpt-4-1106-preview --- chat/views.py | 80 ++++++++++++++++++++++++++---------------------- requirements.txt | 2 +- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/chat/views.py b/chat/views.py index 9b9d9a6..44b6df7 100644 --- a/chat/views.py +++ b/chat/views.py @@ -220,6 +220,12 @@ def delete_all(self, request): 'max_tokens': 32768, 'max_prompt_tokens': 24768, 'max_response_tokens': 8000 + }, + 'gpt-4-1106-preview': { + 'name': 'gpt-4-1106-preview', + 'max_tokens': 131072, + 'max_prompt_tokens': 123072, + 'max_response_tokens': 8000, } } @@ -795,21 +801,21 @@ def num_tokens_from_text(text, model="gpt-3.5-turbo-0301"): print("Warning: model not found. Using cl100k_base encoding.") encoding = tiktoken.get_encoding("cl100k_base") - if model == "gpt-3.5-turbo": - print("Warning: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0613.") - return num_tokens_from_text(text, model="gpt-3.5-turbo-0613") - elif model == "gpt-3.5-turbo-16k": - print("Warning: gpt-3.5-turbo-16k may change over time. Returning num tokens assuming gpt-3.5-turbo-16k-0613.") - return num_tokens_from_text(text, model="gpt-3.5-turbo-16k-0613") - elif model == "gpt-4": - print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.") - return num_tokens_from_text(text, model="gpt-4-0613") - elif model == "gpt-4-32k": - print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.") - return num_tokens_from_text(text, model="gpt-4-32k-0613") - - if model not in ["gpt-3.5-turbo-0613", "gpt-4-0613", "gpt-3.5-turbo-16k-0613", "gpt-4-32k-0613"]: - raise NotImplementedError(f"""num_tokens_from_text() is not implemented for model {model}.""") + if model in MODELS.keys(): + print( + f"Warning: {model} may change over time.", + f"Returning num tokens assuming {model}-0613." + ) + return num_tokens_from_text(text, model=f"{model}-0613") + + if model not in [ + "gpt-3.5-turbo-0613", + "gpt-4-0613", + "gpt-3.5-turbo-16k-0613", + "gpt-4-32k-0613" + ]: + raise NotImplementedError( + f"num_tokens_from_text() is not implemented for model {model}.") return len(encoding.encode(text)) @@ -821,32 +827,32 @@ def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"): except KeyError: print("Warning: model not found. Using cl100k_base encoding.") encoding = tiktoken.get_encoding("cl100k_base") - if model == "gpt-3.5-turbo": - print("Warning: gpt-3.5-turbo may change over time. Returning num tokens assuming gpt-3.5-turbo-0613.") - return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613") - elif model == "gpt-3.5-turbo-16k": - print("Warning: gpt-3.5-turbo-16 may change over time. Returning num tokens assuming gpt-3.5-turbo-16k-0613.") - return num_tokens_from_messages(messages, model="gpt-3.5-turbo-16k-0613") - elif model == "gpt-4": - print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0613.") - return num_tokens_from_messages(messages, model="gpt-4-0613") - elif model == "gpt-4-32k": - print("Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0613.") - return num_tokens_from_messages(messages, model="gpt-4-32k-0613") - elif model == "gpt-3.5-turbo-0613": - 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 - elif model == "gpt-3.5-turbo-16k-0613": + + if model in MODELS.keys(): + print( + f"Warning: {model} may change over time.", + f"Returning num tokens assuming {model}-0613." + ) + return num_tokens_from_messages(messages, model=f"{model}-0613") + + if model in [ + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-16k-0613", + "gpt-4-32k-0613", + "gpt-4-1106-preview-0613" + ]: 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 - elif model == "gpt-4-0613": + tokens_per_name = -1 # if there's a name, the role is omitted + elif model in ["gpt-4-0613"]: tokens_per_message = 3 tokens_per_name = 1 - elif model == "gpt-4-32k-0613": - 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 else: - raise NotImplementedError(f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""") + raise NotImplementedError(( + f"num_tokens_from_messages() is not implemented for model {model}. " + "See https://github.com/openai/openai-python/blob/main/chatml.md " + "for information on how messages are converted to tokens." + )) + num_tokens = 0 for message in messages: num_tokens += tokens_per_message diff --git a/requirements.txt b/requirements.txt index c913fdb..f4f5354 100644 --- a/requirements.txt +++ b/requirements.txt @@ -61,7 +61,7 @@ soupsieve==2.4.1 SQLAlchemy==2.0.12 sqlparse==0.4.4 tenacity==8.2.2 -tiktoken==0.4.0 +tiktoken==0.5.2 tomli==2.0.1 tomlkit==0.11.8 tqdm==4.65.0