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
elifmodel=="gpt-4-0314":
tokens_per_message=3
tokens_per_name=1
else:
raiseNotImplementedError(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
formessageinmessages:
num_tokens+=tokens_per_message
forkey,valueinmessage.items():
num_tokens+=len(encoding.encode(value))
ifkey=="name":
num_tokens+=tokens_per_name
num_tokens+=3# every reply is primed with <|start|>assistant<|message|>