MCPcopy Create free account
hub / github.com/going-doer/Paper2Code / num_tokens_from_messages

Function num_tokens_from_messages

codes/utils.py:314–361  ·  view source on GitHub ↗
(messages, model="gpt-4o-2024-08-06")

Source from the content-addressed store, hash-verified

312
313
314def num_tokens_from_messages(messages, model="gpt-4o-2024-08-06"):
315 import tiktoken
316
317 """Return the number of tokens used by a list of messages."""
318 try:
319 encoding = tiktoken.encoding_for_model(model)
320 except KeyError:
321 print("Warning: model not found. Using o200k_base encoding.")
322 encoding = tiktoken.get_encoding("o200k_base")
323 if model in {
324 "gpt-3.5-turbo-0125",
325 "gpt-4-0314",
326 "gpt-4-32k-0314",
327 "gpt-4-0613",
328 "gpt-4-32k-0613",
329 "gpt-4o-mini-2024-07-18",
330 "gpt-4o-2024-08-06"
331 }:
332 tokens_per_message = 3
333 tokens_per_name = 1
334 elif "gpt-3.5-turbo" in model:
335 print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0125.")
336 return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0125")
337 elif "gpt-4o-mini" in model:
338 print("Warning: gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-mini-2024-07-18.")
339 return num_tokens_from_messages(messages, model="gpt-4o-mini-2024-07-18")
340 elif "gpt-4o" in model:
341 print("Warning: gpt-4o and gpt-4o-mini may update over time. Returning num tokens assuming gpt-4o-2024-08-06.")
342 return num_tokens_from_messages(messages, model="gpt-4o-2024-08-06")
343
344 elif "gpt-4" in model:
345 print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
346 return num_tokens_from_messages(messages, model="gpt-4-0613")
347 else:
348 raise NotImplementedError(
349 f"""num_tokens_from_messages() is not implemented for model {model}."""
350 )
351 num_tokens = 0
352 for message in messages:
353 num_tokens += tokens_per_message
354 for key, value in message.items():
355 # num_tokens += len(encoding.encode(value)
356 num_tokens += len(encoding.encode(value, allowed_special={"<|endoftext|>"},disallowed_special=()))
357
358 if key == "name":
359 num_tokens += tokens_per_name
360 num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
361 return num_tokens
362
363
364

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected