-
Notifications
You must be signed in to change notification settings - Fork 563
Reduce "command returned too much output" errors in email plugin by truncating output #186
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportPatch and project coverage have no change.
Additional details and impacted files@@ Coverage Diff @@
## master #186 +/- ##
=======================================
Coverage 58.52% 58.52%
=======================================
Files 36 36
Lines 2122 2122
Branches 222 222
=======================================
Hits 1242 1242
Misses 858 858
Partials 22 22 ☔ View full report in Codecov by Sentry. |
return messages[start_index:end_index] | ||
|
||
# If MAX_CHARACTERS is breached while MAX_NUM_EMAILS is not breached, truncate email body's | ||
truncation_message = " [Warning: This email's body was truncated by the Auto-GPT email plugin to avoid \"command returned too much output errors\". Retrieve fewer emails to get the full message body]" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This warning message also takes up token space. We should make it very concise e.g.:
[... Email truncated]
@@ -561,5 +571,52 @@ def test_clean_email_body(self, mock_imap): | |||
mock_imap.return_value.search.assert_called_once_with(None, "UNSEEN") | |||
mock_imap.return_value.fetch.assert_called_once_with(b"1", "(BODY.PEEK[])") | |||
|
|||
# Test for truncating emails |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also write some unit tests that test the various settings e.g. max_num_emails:
Here some suggestions
@patch('os.getenv', return_value='1000')
def test_empty_list(self, mock_getenv):
truncator = EmailTruncator()
messages = []
self.assertEqual(truncator.truncate_emails(messages), [])
@patch('os.getenv')
def test_max_characters_breached(self, mock_getenv):
truncator = EmailTruncator()
mock_getenv.side_effect = ['10', '10', '100']
messages = [{"Message Body": "Hello world!"}]
truncated_messages = truncator.truncate_emails(messages)
self.assertTrue(len(str(truncated_messages)) <= 10)
@patch('os.getenv')
def test_max_num_emails_breached(self, mock_getenv):
truncator = EmailTruncator()
mock_getenv.side_effect = ['1000', '1', '100']
messages = [{"Message Body": "Hello world!"}, {"Message Body": "Goodbye world!"}]
truncated_messages = truncator.truncate_emails(messages)
self.assertEqual(len(truncated_messages), 1)
@patch('os.getenv')
def test_max_email_characters_breached(self, mock_getenv):
truncator = EmailTruncator()
mock_getenv.side_effect = ['1000', '10', '10']
messages = [{"Message Body": "Hello world!"}]
truncated_messages = truncator.truncate_emails(messages)
self.assertTrue(len(str(truncated_messages[0]["Message Body"])) <= 10)
|
||
# If MAX_CHARACTERS is breached | ||
messages_length = len(str(messages)) | ||
if os.getenv("MAX_CHARACTERS") and int(os.getenv("MAX_CHARACTERS")) < int(messages_length): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also add a test, when these env vars are not set. In this case we must use default values.
Sorry I got assigned to this repo. Is this ready to go? |
It is from my point of view, but I believe @riensen want additional tests added. |
Motivation:
As a continuation to the discussion with @riensen in PR #177, this PR was created as a way to further reduce "command returned too much output" errors in the email plugin by truncating email output based on configuration settings.
Changes:
Added truncate_emails function that truncates the email plugin's output based on the following requirements discussed in PR #177.
Testing:
Added test to validate email truncation functionality
Future Work
Consider whether to use ChatGPT API to summarize emails instead of truncating them.