From aa9266248731b5550d87c606741aa5adbbb1b79e Mon Sep 17 00:00:00 2001 From: lidanyang Date: Fri, 19 Jul 2024 17:03:37 +0800 Subject: [PATCH 1/5] fix output code format --- metagpt/actions/di/write_analysis_code.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/metagpt/actions/di/write_analysis_code.py b/metagpt/actions/di/write_analysis_code.py index 00e6d174d..39de18987 100644 --- a/metagpt/actions/di/write_analysis_code.py +++ b/metagpt/actions/di/write_analysis_code.py @@ -31,8 +31,12 @@ class WriteAnalysisCode(Action): rsp = await self._aask(reflection_prompt, system_msgs=[REFLECTION_SYSTEM_MSG]) reflection = json.loads(CodeParser.parse_code(text=rsp)) + try: + improved_impl = CodeParser.parse_code(text=reflection["improved_impl"], lang="python") + except Exception as e: + improved_impl = reflection["improved_impl"] - return reflection["improved_impl"] + return improved_impl async def run( self, From f53a2b574a26cf0e746d5aecd953ac2fd3a38e04 Mon Sep 17 00:00:00 2001 From: lidanyang Date: Fri, 19 Jul 2024 17:04:49 +0800 Subject: [PATCH 2/5] force to install package --- metagpt/prompts/di/write_analysis_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/prompts/di/write_analysis_code.py b/metagpt/prompts/di/write_analysis_code.py index 1d743a719..55d5e77cd 100644 --- a/metagpt/prompts/di/write_analysis_code.py +++ b/metagpt/prompts/di/write_analysis_code.py @@ -30,7 +30,7 @@ your code REFLECTION_SYSTEM_MSG = """ You are an AI Python assistant. You will be given your previous implementation code of a task, runtime error results, and a hint to change the implementation appropriately. Write your full implementation. -When occuring ModuleNotFoundError, always install the required package. And use Terminal tool if available. +When occuring ModuleNotFoundError, always import Terminal tool to install the required package before the refined code in the same cell. Such as `from metagpt.tools.libs.terminal import Terminal\nterminal = Terminal()\nawait terminal.run_command('pip install pandas')` before importing pandas. """ DEBUG_REFLECTION_EXAMPLE = ''' From 0746ef882dcddde2a96fd8b61b2501383435743d Mon Sep 17 00:00:00 2001 From: lidanyang Date: Fri, 19 Jul 2024 17:06:11 +0800 Subject: [PATCH 3/5] refine prompt --- metagpt/prompts/di/data_analyst.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metagpt/prompts/di/data_analyst.py b/metagpt/prompts/di/data_analyst.py index 8a22a5d14..b99dcba1e 100644 --- a/metagpt/prompts/di/data_analyst.py +++ b/metagpt/prompts/di/data_analyst.py @@ -5,7 +5,9 @@ EXTRA_INSTRUCTION = """ - When no click action is required, no need to use the browser tool to navigate to the webpage before scraping. - If you need detail HTML content, write code to get it but not to use the browser tool. - Make sure the command_name are certainly in Available Commands when you use the browser tool. -7. When you are making plan. It is highly recommend to plan all the coding plan and reviews plan in first response. +7. When you are making plan. It is highly recommend to plan and append all the tasks in first response once time. +8. Don't finish_current_task multiple times for the same task. +9. Finish current task timely, such as when the code is written and executed successfully. """ TASK_TYPE_DESC = "\n".join([f"- **{tt.type_name}**: {tt.value.desc}" for tt in TaskType]) From 1973af16339aa6d8d9cac13097d6949fe7e4b7e2 Mon Sep 17 00:00:00 2001 From: lidanyang Date: Fri, 19 Jul 2024 17:06:38 +0800 Subject: [PATCH 4/5] add task desc to write code --- metagpt/roles/di/data_analyst.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/di/data_analyst.py b/metagpt/roles/di/data_analyst.py index 694e42de3..b16e9690c 100644 --- a/metagpt/roles/di/data_analyst.py +++ b/metagpt/roles/di/data_analyst.py @@ -60,14 +60,19 @@ class DataAnalyst(RoleZero): self.rc.working_memory.add(Message(content=browser_actions, role="user", cause_by="browser")) return memory - async def write_and_exec_code(self): - """Write a code block for current task and execute it in an interactive notebook environment.""" + async def write_and_exec_code(self, task_desc: str): + """Write a code block for current task and execute it in an interactive notebook environment. + + Args: + task_desc (str): The task description for which the code needs to be written. + """ counter = 0 success = False await self.execute_code.init_code() # plan info plan_status = self.planner.get_plan_status() + plan_status = plan_status + f"\nSpecific Task Description:{task_desc}" # tool info if self.custom_tool_recommender: From 9c655e88289e0ad13190049401c1898b38e4c8d3 Mon Sep 17 00:00:00 2001 From: lidanyang Date: Fri, 19 Jul 2024 17:39:45 +0800 Subject: [PATCH 5/5] rename task_desc to instruction --- metagpt/roles/di/data_analyst.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/roles/di/data_analyst.py b/metagpt/roles/di/data_analyst.py index b16e9690c..1debd681c 100644 --- a/metagpt/roles/di/data_analyst.py +++ b/metagpt/roles/di/data_analyst.py @@ -60,11 +60,11 @@ class DataAnalyst(RoleZero): self.rc.working_memory.add(Message(content=browser_actions, role="user", cause_by="browser")) return memory - async def write_and_exec_code(self, task_desc: str): + async def write_and_exec_code(self, instruction: str = ""): """Write a code block for current task and execute it in an interactive notebook environment. Args: - task_desc (str): The task description for which the code needs to be written. + instruction: The specific task description for which the code needs to be written. """ counter = 0 success = False @@ -72,7 +72,7 @@ class DataAnalyst(RoleZero): # plan info plan_status = self.planner.get_plan_status() - plan_status = plan_status + f"\nSpecific Task Description:{task_desc}" + plan_status = plan_status + f"\nFurther Task Instruction: {instruction}" # tool info if self.custom_tool_recommender: