@@ -151,29 +151,25 @@ def get_messages_from_history(self, prompt_override, follow_up_questions_prompt,
151
151
break
152
152
return messages
153
153
154
- '''
155
- Source: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
156
- Adapted: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/chatgpt?pivots=programming-language-chat-completions#managing-conversations
157
-
158
- Method takes in a single conversation and calculate prompt tokens
159
- for chat api
160
-
161
- Keys role and content are accounted seperately.
162
-
163
- Values of content are encoded by model type and calculated the length.
164
-
165
- This gives close proximity of token length measurement used in gpt models
166
-
167
- message = {"role":"assistant", "content":"how can I assist you?"}
168
- '''
169
- def num_tokens_from_messages (self , message : any , model : str ):
154
+ def num_tokens_from_messages (self , message : dict [str ,str ], model : str ):
155
+ """
156
+ Calculate the number of tokens required to encode a message.
157
+ Args:
158
+ message (any): The message to encode, represented as a dictionary.
159
+ model (str): The name of the model to use for encoding.
160
+ Returns:
161
+ int: The total number of tokens required to encode the message.
162
+ Example:
163
+ message = {'role': 'user', 'name': 'John', 'content': 'Hello, how are you?'}
164
+ model = 'gpt-3.5-turbo'
165
+ num_tokens_from_messages(message, model)
166
+ output: 11
167
+ """
170
168
encoding = tiktoken .encoding_for_model (self .get_oai_chatmodel_tiktok (model ))
171
169
num_tokens = 0
172
170
num_tokens += 2 # every message follows {role/name}\n{content}\n
173
171
for key , value in message .items ():
174
172
num_tokens += len (encoding .encode (value ))
175
- if key == "name" : # if there's a name, the role is omitted
176
- num_tokens += - 1 # role is always required and always 1 token
177
173
return num_tokens
178
174
179
175
def get_oai_chatmodel_tiktok (self , aoaimodel : str ):
0 commit comments