in yourbench/utils/inference/inference_tracking.py [0:0]
def _count_message_tokens(messages: List[Dict[str, str]], encoding: tiktoken.Encoding) -> int:
"""Counts tokens in a list of messages, approximating OpenAI's format."""
num_tokens = 0
# Approximation based on OpenAI's cookbook: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
# This might not be perfectly accurate for all models/providers but is a reasonable estimate.
tokens_per_message = 3
tokens_per_name = 1
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
if value:
num_tokens += _count_tokens(str(value), encoding)
if key == "name":
num_tokens += tokens_per_name
num_tokens += 3
return num_tokens