Merge branch 'fix_truncate' into 'dev'

Fix truncate

See merge request agents/data_agents_opt!24
This commit is contained in:
林义章 2023-12-13 08:09:31 +00:00
commit 6fb65c547e
2 changed files with 11 additions and 5 deletions

View file

@ -186,14 +186,13 @@ class ExecutePyCode(ExecuteCode, Action):
def truncate(result: str, keep_len: int = 2000) -> str:
desc = f"Truncated to show only the last {keep_len} characters\n"
if result.startswith(desc):
result = result[-len(desc) :]
result = result[len(desc) :]
if len(result) > keep_len:
result = result[-keep_len:]
if not result.startswith(desc):
return desc + result
return desc
return result
def remove_escape_and_color_codes(input_str):

View file

@ -1,6 +1,6 @@
import pytest
from metagpt.actions.execute_code import ExecutePyCode
from metagpt.actions.execute_code import ExecutePyCode, truncate
from metagpt.schema import Message
@ -81,3 +81,10 @@ async def test_plotting_bug():
pi = ExecutePyCode()
output = await pi.run(code)
assert output[1] is True
def test_truncate():
output = "hello world"
assert truncate(output) == output
output = "hello world"
assert truncate(output, 5) == "Truncated to show only the last 5 characters\nworld"