cast to string when measuring token length (#8617)

This commit is contained in:
Harrison Chase 2023-08-02 00:12:59 -07:00 committed by GitHub
parent 68113348cc
commit 7c5c0557cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -570,7 +570,9 @@ class ChatOpenAI(BaseChatModel):
for message in messages_dict:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
# Cast str(value) in case the message value is not a string
# This occurs with function messages
num_tokens += len(encoding.encode(str(value)))
if key == "name":
num_tokens += tokens_per_name
# every reply is primed with <im_start>assistant