Skip to content

Commit 32c9e41

Browse files
committed
Remove middle when truncating instead of beginning
This should give more useful context to the assistant, as the most important info is usually in the beginning and end.
1 parent 073636f commit 32c9e41

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

interpreter/core/utils/truncate_output.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
44

55
needs_truncation = False
66

7-
message = f"Output truncated. Showing the last {max_output_chars} characters. You should try again and use computer.ai.summarize(output) over the output, or break it down into smaller steps.\n\n"
7+
# Calculate how much to show from start and end
8+
chars_per_end = max_output_chars // 2
9+
10+
message = ("Output truncated. "
11+
f"Showing {chars_per_end} characters from start/end. "
12+
"You should try again and use computer.ai.summarize(output) "
13+
"over the output, or break it down into smaller steps.\n\n")
814

915
# This won't work because truncated code is stored in interpreter.messages :/
1016
# If the full code was stored, we could do this:
@@ -22,6 +28,8 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
2228

2329
# If data exceeds max length, truncate it and add message
2430
if len(data) > max_output_chars or needs_truncation:
25-
data = message + data[-max_output_chars:]
31+
first_part = data[:chars_per_end]
32+
last_part = data[-chars_per_end:]
33+
data = message + first_part + "\n[...]\n" + last_part
2634

2735
return data

0 commit comments

Comments
 (0)