From 6cb2910d144c56ccd2ef84c223cd9125cbf22a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Mon, 22 Jan 2024 15:29:28 +0800 Subject: [PATCH] fix: now present the results of failure and success code in different ways. --- metagpt/actions/execute_code.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/metagpt/actions/execute_code.py b/metagpt/actions/execute_code.py index 5b6cba57d..851794b91 100644 --- a/metagpt/actions/execute_code.py +++ b/metagpt/actions/execute_code.py @@ -15,14 +15,13 @@ import nbformat from nbclient import NotebookClient from nbclient.exceptions import CellTimeoutError, DeadKernelError from nbformat import NotebookNode -from nbformat.v4 import new_code_cell, new_output, new_markdown_cell -from rich.console import Console -from rich.syntax import Syntax +from nbformat.v4 import new_code_cell, new_markdown_cell, new_output +from rich.box import MINIMAL +from rich.console import Console, Group +from rich.live import Live from rich.markdown import Markdown from rich.panel import Panel -from rich.box import MINIMAL -from rich.live import Live -from rich.console import Group +from rich.syntax import Syntax from metagpt.actions import Action from metagpt.logs import logger @@ -229,7 +228,7 @@ class ExecutePyCode(ExecuteCode, Action): # code success outputs = self.parse_outputs(self.nb.cells[-1].outputs) return truncate(remove_escape_and_color_codes(outputs), is_success=success) - elif language == 'markdown': + elif language == "markdown": # markdown self.add_markdown_cell(code) return code, True @@ -238,26 +237,27 @@ class ExecutePyCode(ExecuteCode, Action): def truncate(result: str, keep_len: int = 2000, is_success: bool = True): - desc = f"Executed code {'successfully' if is_success else 'failed, please reflect the cause of bug and then debug'}" + """执行失败的代码, 展示result后keep_len个字符; 执行成功的代码, 展示result前keep_len个字符。""" + desc = f"Executed code {'successfully. ' if is_success else 'failed, please reflect the cause of bug and then debug. '}" if is_success: - desc += f"Truncated to show only {keep_len} characters\n" + desc += f"Truncated to show only first {keep_len} characters\n" else: - desc += "Show complete information for you." + desc += f"Truncated to show only last {keep_len} characters\n" if result.startswith(desc): result = result[len(desc) :] if len(result) > keep_len: - result = result[-keep_len:] if not is_success else result + result = result[-keep_len:] if not is_success else result[:keep_len] if not result: - result = 'No output about your code. Only when importing packages it is normal case. Recap and go ahead.' + result = "No output about your code. Only when importing packages it is normal case. Recap and go ahead." return result, False if result.strip().startswith("