Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

refine: rm truncate.

刘棒棒9db705f20f79
diff --git a/metagpt/actions/mi/execute_nb_code.py b/metagpt/actions/mi/execute_nb_code.py
index 4644ef5d5..2e22a7d0c 100644
--- a/metagpt/actions/mi/execute_nb_code.py
+++ b/metagpt/actions/mi/execute_nb_code.py
@@ -101,7 +101,7 @@ class ExecuteNbCode(Action):
tag in output["text"]
for tag in ["| INFO | metagpt", "| ERROR | metagpt", "| WARNING | metagpt", "DEBUG"]
):
- ioutput, is_success = truncate(remove_escape_and_color_codes(output["text"]), keep_len, is_success)
+ ioutput, is_success = remove_escape_and_color_codes(output["text"]), True
elif output["output_type"] == "display_data":
if "image/png" in output["data"]:
self.show_bytes_figure(output["data"]["image/png"], self.interaction)
@@ -112,10 +112,24 @@ class ExecuteNbCode(Action):
ioutput, is_success = "", True
elif output["output_type"] == "execute_result":
no_escape_color_output = remove_escape_and_color_codes(output["data"]["text/plain"])
- ioutput, is_success = truncate(no_escape_color_output, keep_len, is_success)
+ ioutput, is_success = no_escape_color_output, True
elif output["output_type"] == "error":
no_escape_color_output = remove_escape_and_color_codes("\n".join(output["traceback"]))
- ioutput, is_success = truncate(no_escape_color_output, keep_len, is_success)
+ ioutput, is_success = no_escape_color_output, False
+
+ # handle coroutines that are not executed asynchronously
+ if ioutput.strip().startswith("<coroutine object"):
+ ioutput = "Executed code failed, you need use key word 'await' to run a async code."
+ is_success = False
+
+ # add prefix to ioutput and truncate.
+ if len(ioutput) > keep_len and is_success:
+ prefix = f"Executed code successfully. Truncated to show only first {keep_len} characters\n"
+ ioutput = prefix + ioutput[:keep_len]
+ elif len(ioutput) > keep_len and not is_success:
+ prefix = f"Executed code failed, please reflect the cause of bug and then debug. Truncated to show only last {keep_len} characters\n"
+ ioutput = prefix + ioutput[-keep_len:]
+
parsed_output.append(ioutput)
return is_success, ",".join(parsed_output)
@@ -198,24 +212,6 @@ class ExecuteNbCode(Action):
raise ValueError(f"Only support for language: python, markdown, but got {language}, ")
-def truncate(result: str, keep_len: int = 2000, is_success: bool = True):
- """对于超出keep_len个字符的result: 执行失败的代码, 展示result后keep_len个字符; 执行成功的代码, 展示result前keep_len个字符。"""
- if is_success:
- desc = f"Executed code successfully. Truncated to show only first {keep_len} characters\n"
- else:
- desc = f"Executed code failed, please reflect the cause of bug and then debug. Truncated to show only last {keep_len} characters\n"
-
- if result.strip().startswith("<coroutine object"):
- result = "Executed code failed, you need use key word 'await' to run a async code."
- return result, False
-
- if len(result) > keep_len:
- result = result[-keep_len:] if not is_success else result[:keep_len]
- return desc + result, is_success
-
- return result, is_success
-
-
def remove_escape_and_color_codes(input_str: str):
# 使用正则表达式去除jupyter notebook输出结果中的转义字符和颜色代码
# Use regular expressions to get rid of escape characters and color codes in jupyter notebook output.
diff --git a/tests/metagpt/actions/mi/test_execute_nb_code.py b/tests/metagpt/actions/mi/test_execute_nb_code.py
index 98c2e5cc3..4b90289ea 100644
--- a/tests/metagpt/actions/mi/test_execute_nb_code.py
+++ b/tests/metagpt/actions/mi/test_execute_nb_code.py
@@ -1,6 +1,6 @@
import pytest
-from metagpt.actions.mi.execute_nb_code import ExecuteNbCode, truncate
+from metagpt.actions.mi.execute_nb_code import ExecuteNbCode
@pytest.mark.asyncio
@@ -54,21 +54,6 @@ async def test_plotting_code():
assert is_success
-def test_truncate():
- # 代码执行成功
- output, is_success = truncate("hello world", 5, True)
- assert "Truncated to show only first 5 characters\nhello" in output
- assert is_success
- # 代码执行失败
- output, is_success = truncate("hello world", 5, False)
- assert "Truncated to show only last 5 characters\nworld" in output
- assert not is_success
- # 异步
- output, is_success = truncate("<coroutine object", 5, True)
- assert not is_success
- assert "await" in output
-
-
@pytest.mark.asyncio
async def test_run_with_timeout():
executor = ExecuteNbCode(timeout=1)