cast to string when measuring token length (#8617)

pull/8632/head
Harrison Chase 1 year ago committed by GitHub
parent 68113348cc
commit 7c5c0557cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

Loading…
Cancel
Save