@@ -108,6 +108,16 @@ def print_chat(chat):
108108 _log (f"Chat: { chat_summary (chat )} " )
109109
110110
111+ def truncate_strings (o : Any ) -> Any :
112+ if isinstance (o , dict ):
113+ return {k : truncate_strings (v ) for k , v in o .items ()}
114+ elif isinstance (o , list ):
115+ return [truncate_strings (v ) for v in o ]
116+ elif isinstance (o , str ) and len (o ) > 10000 :
117+ return f"({ len (o )} )"
118+ return o
119+
120+
111121def chat_summary (chat ):
112122 """Summarize chat completion request for logging."""
113123 # replace image_url.url with <image>
@@ -128,7 +138,7 @@ def chat_summary(chat):
128138 data = item ["file" ]["file_data" ]
129139 prefix = data .split ("," , 1 )[0 ]
130140 item ["file" ]["file_data" ] = prefix + f",({ len (data ) - len (prefix )} )"
131- return json .dumps (clone , indent = 2 )
141+ return json .dumps (truncate_strings ( clone ) , indent = 2 )
132142
133143
134144image_exts = ["png" , "webp" , "jpg" , "jpeg" , "gif" , "bmp" , "svg" , "tiff" , "ico" ]
@@ -2059,7 +2069,7 @@ async def cli_chat(
20592069 first_message ["content" ] = [file_content , {"type" : "text" , "text" : first_message ["content" ]}]
20602070
20612071 if g_verbose :
2062- printdump (chat )
2072+ printdump (truncate_strings ( chat ) )
20632073
20642074 try :
20652075 context = {
@@ -3230,7 +3240,7 @@ def log(self, message: Any):
32303240
32313241 def log_json (self , obj : Any ):
32323242 if self .verbose :
3233- print (f"[{ self .name } ] { json .dumps (obj , indent = 2 )} " , flush = True )
3243+ print (f"[{ self .name } ] { json .dumps (truncate_strings ( obj ) , indent = 2 )} " , flush = True )
32343244 return obj
32353245
32363246 def dbg (self , message : Any ):
0 commit comments