2929
3030def count_tokens_openai_chat_models (
3131 messages : List [OpenAIMessage ],
32- encoding : Any ,
32+ encoding : tiktoken .Encoding ,
33+ tokens_per_message : int ,
34+ tokens_per_name : int ,
3335) -> int :
3436 r"""Counts the number of tokens required to generate an OpenAI chat based
3537 on a given list of messages.
3638
3739 Args:
3840 messages (List[OpenAIMessage]): The list of messages.
39- encoding (Any ): The encoding method to use.
41+ encoding (tiktoken.Encoding ): The encoding method to use.
4042
4143 Returns:
4244 int: The number of tokens required.
4345 """
4446 num_tokens = 0
4547 for message in messages :
46- # message follows <im_start>{role/name}\n{content}<im_end>\n
47- num_tokens += 4
48+ num_tokens += tokens_per_message
4849 for key , value in message .items ():
4950 num_tokens += len (encoding .encode (value ))
5051 if key == "name" : # if there's a name, the role is omitted
51- num_tokens += - 1 # role is always 1 token
52- num_tokens += 2 # every reply is primed with <im_start >assistant
52+ num_tokens += tokens_per_name
53+ num_tokens += 3 # every reply is primed with <|start| >assistant<|message|>
5354 return num_tokens
5455
5556
@@ -81,11 +82,26 @@ def num_tokens_from_messages(
8182 except KeyError :
8283 encoding = tiktoken .get_encoding ("cl100k_base" )
8384
84- if model in {
85- ModelType .GPT_3_5_TURBO , ModelType .GPT_4 , ModelType .GPT_4_32k ,
86- ModelType .STUB
87- }:
88- return count_tokens_openai_chat_models (messages , encoding )
85+ if model .value_for_tiktoken .startswith ("gpt-3.5-turbo" ):
86+ # Every message follows <|start|>{role/name}\n{content}<|end|>\n
87+ tokens_per_message = 4
88+ # If there's a name, the role is omitted
89+ tokens_per_name = - 1
90+ return count_tokens_openai_chat_models (
91+ messages ,
92+ encoding ,
93+ tokens_per_message ,
94+ tokens_per_name ,
95+ )
96+ elif model .value_for_tiktoken .startswith ("gpt-4" ):
97+ tokens_per_message = 3
98+ tokens_per_name = 1
99+ return count_tokens_openai_chat_models (
100+ messages ,
101+ encoding ,
102+ tokens_per_message ,
103+ tokens_per_name ,
104+ )
89105 else :
90106 raise NotImplementedError (
91107 f"`num_tokens_from_messages`` is not presently implemented "
@@ -97,27 +113,6 @@ def num_tokens_from_messages(
97113 f"for information about openai chat models." )
98114
99115
100- def get_model_token_limit (model : ModelType ) -> int :
101- r"""Returns the maximum token limit for a given model.
102-
103- Args:
104- model (ModelType): The type of the model.
105-
106- Returns:
107- int: The maximum token limit for the given model.
108- """
109- if model == ModelType .GPT_3_5_TURBO :
110- return 4096
111- elif model == ModelType .GPT_4 :
112- return 8192
113- elif model == ModelType .GPT_4_32k :
114- return 32768
115- elif model == ModelType .STUB :
116- return 4096
117- else :
118- raise ValueError ("Unknown model type" )
119-
120-
121116def openai_api_key_required (func : F ) -> F :
122117 r"""Decorator that checks if the OpenAI API key is available in the
123118 environment variables.
0 commit comments