From e0a104dd05b0cfc13859848176ec598c1b40eeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Thu, 12 Sep 2024 15:52:14 +0800 Subject: [PATCH] write_new_code_fail --- metagpt/prompts/di/engineer2.py | 3 +++ metagpt/roles/di/engineer2.py | 11 +++++++++-- metagpt/roles/di/role_zero.py | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/metagpt/prompts/di/engineer2.py b/metagpt/prompts/di/engineer2.py index 73127a2be..1345f6f6b 100644 --- a/metagpt/prompts/di/engineer2.py +++ b/metagpt/prompts/di/engineer2.py @@ -103,6 +103,9 @@ WRITE_CODE_PROMPT = """ # Plan Status {plan_status} +# Current Coding File +{file_path} + # Further Instruction {instruction} diff --git a/metagpt/roles/di/engineer2.py b/metagpt/roles/di/engineer2.py index 1a224623b..6d5abfe2d 100644 --- a/metagpt/roles/di/engineer2.py +++ b/metagpt/roles/di/engineer2.py @@ -1,5 +1,6 @@ from __future__ import annotations +import re from pathlib import Path from pydantic import Field @@ -14,7 +15,7 @@ from metagpt.prompts.di.engineer2 import ( WRITE_CODE_SYSTEM_PROMPT, ) from metagpt.roles.di.role_zero import RoleZero -from metagpt.schema import UserMessage +from metagpt.schema import AIMessage, UserMessage from metagpt.strategy.experience_retriever import ENGINEER_EXAMPLE from metagpt.tools.libs.cr import CodeReview from metagpt.tools.libs.git import git_create_pull @@ -117,10 +118,16 @@ class Engineer2(RoleZero): plan_status, _ = self._get_plan_status() prompt = WRITE_CODE_PROMPT.format( user_requirement=self.planner.plan.goal, + file_path=path, plan_status=plan_status, instruction=instruction, ) - context = self.llm.format_msg(self.rc.memory.get(self.memory_k) + [UserMessage(content=prompt)]) + # Remove the json command in last message. + memory = self.rc.memory.get(self.memory_k) + pattern = r"```json.*?\s+(.*)\n```" + last_memory_content = re.sub(pattern, "", memory[-1].content, flags=re.DOTALL) + memory = memory[:-1] + [AIMessage(content=last_memory_content)] + context = self.llm.format_msg(memory + [UserMessage(content=prompt)]) async with EditorReporter(enable_llm_stream=True) as reporter: await reporter.async_report({"type": "code", "filename": Path(path).name, "src_path": path}, "meta") diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index ab8179618..2d1fb556a 100644 --- a/metagpt/roles/di/role_zero.py +++ b/metagpt/roles/di/role_zero.py @@ -430,7 +430,7 @@ class RoleZero(Role): for index, cmd in enumerate(commands) if index == index_of_first_exclusive or cmd["command_name"] not in self.exclusive_tool_commands ] - command_rsp = "```json\n" + json.dumps(commands, indent=4, ensure_ascii=False) + "\n```json" + command_rsp = "```json\n" + json.dumps(commands, indent=4, ensure_ascii=False) + "\n```" logger.info( "exclusive command more than one in current command list. change the command list.\n" + command_rsp )