Skip to content

Commit

Permalink
Add support for gpt-4-1106-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
shizacat committed Jan 11, 2024
1 parent df7d9b9 commit c66fe39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
80 changes: 43 additions & 37 deletions chat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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))

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c66fe39

Please sign in to comment.