Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve truncation #1583

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions interpreter/core/utils/truncate_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):

needs_truncation = False

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"
# Calculate how much to show from start and end
chars_per_end = max_output_chars // 2

message = (f"Output truncated ({len(data):,} characters total). "
f"Showing {chars_per_end:,} characters from start/end. "
"To handle large outputs, store result in python var first "
"`result = command()` then `computer.ai.summarize(result)` for "
"a summary, search with `result.find('text')`, "
"repeat shell commands with wc/grep/sed, etc. or break it down "
"into smaller steps.\n\n")

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

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

return data