From 5dab0d33d53a4ebf2a223af80ed885b6c023b62d Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sat, 25 Jan 2025 22:55:17 -0500
Subject: [PATCH 1/3] 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.
---
interpreter/core/utils/truncate_output.py | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index 628ff504e3..009f91716a 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -4,7 +4,13 @@ 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 = ("Output truncated. "
+ f"Showing {chars_per_end} characters from start/end. "
+ "You should try again and use computer.ai.summarize(output) "
+ "over the output, 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:
@@ -22,6 +28,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
From e7c62beb181079a9d1a54664e42ffe2f558a101a Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sat, 25 Jan 2025 23:44:25 -0500
Subject: [PATCH 2/3] Improve suggestions for handling truncated output
Smaller models get confused and call the summarize command incorrectly,
or try to call it on the output of shell commands, etc. Give a few
suggestions of how to better deal with long output.
---
interpreter/core/utils/truncate_output.py | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index 009f91716a..c94e6daf31 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -9,8 +9,11 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
message = ("Output truncated. "
f"Showing {chars_per_end} characters from start/end. "
- "You should try again and use computer.ai.summarize(output) "
- "over the output, or break it down into smaller steps.\n\n")
+ "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:
From 5f9df7e1ab8557ee196f95c87a6b928a050c485b Mon Sep 17 00:00:00 2001
From: endolith <endolith@gmail.com>
Date: Sun, 26 Jan 2025 10:03:25 -0500
Subject: [PATCH 3/3] Print total length when truncating long outputs
Provides a little more context for the assistant to work with
---
interpreter/core/utils/truncate_output.py | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/interpreter/core/utils/truncate_output.py b/interpreter/core/utils/truncate_output.py
index c94e6daf31..4de2869b66 100644
--- a/interpreter/core/utils/truncate_output.py
+++ b/interpreter/core/utils/truncate_output.py
@@ -7,8 +7,8 @@ def truncate_output(data, max_output_chars=2800, add_scrollbars=False):
# Calculate how much to show from start and end
chars_per_end = max_output_chars // 2
- message = ("Output truncated. "
- f"Showing {chars_per_end} characters from start/end. "
+ 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')`, "