Skip to content

Commit ee311f0

Browse files
committed
Only print truncated messages
1 parent 5b4a060 commit ee311f0

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

llms/extensions/providers/google.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ async def chat(self, chat, context=None):
440440
raise Exception(obj["error"]["message"])
441441

442442
if ctx.debug:
443-
ctx.dbg(json.dumps(gemini_response_summary(obj), indent=2))
443+
ctx.log_json(obj)
444444

445445
# Check for empty response "anomaly"
446446
has_candidates = obj.get("candidates") and len(obj["candidates"]) > 0

llms/main.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
111121
def 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

134144
image_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

Comments
 (0)