@@ -112,22 +112,51 @@ def truncate_messages_by_size(messages, max_bytes=MAX_GEN_AI_MESSAGE_BYTES):
112112 if not messages :
113113 return messages
114114
115- # make a list out of the messages in case it's just a string? why is this needed?
116115 truncated_messages = list (messages )
117116
118- # while there is more than one message, serialize and measure the size, and if it's too big, remove the oldest message
119117 while len (truncated_messages ) > 1 :
120- serialized = serialize (
121- truncated_messages , is_vars = False , max_value_length = round (max_bytes * 0.8 )
122- )
123- serialized_json = json .dumps (serialized , separators = ("," , ":" ))
118+ serialized_json = json .dumps (truncated_messages , separators = ("," , ":" ))
124119 current_size = len (serialized_json .encode ("utf-8" ))
125120
126121 if current_size <= max_bytes :
127122 break
128123
129124 truncated_messages .pop (0 )
130125
126+ serialized_json = json .dumps (truncated_messages , separators = ("," , ":" ))
127+ current_size = len (serialized_json .encode ("utf-8" ))
128+
129+ if current_size > max_bytes and len (truncated_messages ) == 1 :
130+ message = truncated_messages [0 ].copy ()
131+ content = message .get ("content" , "" )
132+
133+ if isinstance (content , str ):
134+ max_content_length = max_bytes // 2
135+ while True :
136+ message ["content" ] = content [:max_content_length ]
137+ test_json = json .dumps ([message ], separators = ("," , ":" ))
138+ if len (test_json .encode ("utf-8" )) <= max_bytes :
139+ break
140+ max_content_length = int (max_content_length * 0.9 )
141+ if max_content_length < 100 :
142+ message ["content" ] = ""
143+ break
144+
145+ truncated_messages = [message ]
146+ elif isinstance (content , list ):
147+ content_copy = list (content )
148+ while len (content_copy ) > 0 :
149+ message ["content" ] = content_copy
150+ test_json = json .dumps ([message ], separators = ("," , ":" ))
151+ if len (test_json .encode ("utf-8" )) <= max_bytes :
152+ break
153+ content_copy = content_copy [:- 1 ]
154+
155+ if len (content_copy ) == 0 :
156+ message ["content" ] = []
157+
158+ truncated_messages = [message ]
159+
131160 return truncated_messages
132161
133162
0 commit comments