From 33810829a072467a8f61f2f7dc14ffd1792e793a Mon Sep 17 00:00:00 2001 From: lidanyang Date: Wed, 13 Dec 2023 14:31:32 +0800 Subject: [PATCH] support tool in debug --- metagpt/actions/debug_code.py | 106 ++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 38 deletions(-) diff --git a/metagpt/actions/debug_code.py b/metagpt/actions/debug_code.py index 9efe93efc..53ca2f54d 100644 --- a/metagpt/actions/debug_code.py +++ b/metagpt/actions/debug_code.py @@ -3,7 +3,7 @@ from typing import Dict, List, Union, Tuple, Optional, Any from metagpt.actions import Action from metagpt.logs import logger from metagpt.schema import Message, Plan -from metagpt.utils.common import CodeParser +from metagpt.utils.common import CodeParser, create_func_config from metagpt.actions.write_analysis_code import BaseWriteAnalysisCode DEBUG_REFLECTION_EXAMPLE = '''Example 1: @@ -39,25 +39,39 @@ DEBUG_REFLECTION_EXAMPLE = '''Example 1: REFLECTION_PROMPT = """ Here is an example for you. {debug_example} - [requirement] - {goal} - [finished code] - finished code are executable, and you should based on the code to continue your current code debug - {finished_code} - - try to reuse the code here to understand the coding task. + [context] + {context} [previous impl] {code} [runtime Error] {runtime_result} - Analysis the error step by step, provide me improve method. Do not repeat [previous impl] + Analysis the error step by step, provide me improve method and code. Remember to follow [context] requirement. [reflection on previous impl]: xxx """ +CODE_REFLECTION = { + "name": "execute_reflection_code", + "description": "Execute reflection code.", + "parameters": { + "type": "object", + "properties": { + "reflection": { + "type": "string", + "description": "Reflection on previous impl.", + }, + "improved_impl": { + "type": "string", + "description": "Refined code after reflection.", + }, + }, + "required": ["reflection", "improved_impl"], + }, +} + def message_to_str(message: Message) -> str: return f"{message.role}: {message.content}" @@ -75,52 +89,68 @@ class DebugCode(BaseWriteAnalysisCode): def __init__(self, **kwargs: Any): super().__init__(**kwargs) - async def run_reflection(self, goal, finished_code, finished_code_result, code, runtime_result) -> str: + async def run_reflection( + self, + # goal, + # finished_code, + # finished_code_result, + context: List[Message], + code, + runtime_result, + ) -> dict: info = [] - finished_code_and_result = finished_code + "\n [finished results]\n\n" + finished_code_result + # finished_code_and_result = finished_code + "\n [finished results]\n\n" + finished_code_result reflection_prompt = REFLECTION_PROMPT.format(debug_example=DEBUG_REFLECTION_EXAMPLE, - goal=goal, - finished_code=finished_code_and_result, + context=context, + # goal=goal, + # finished_code=finished_code_and_result, code=code, runtime_result=runtime_result ) - system_prompt = "You are an AI Python assistant. You will be given your previous implementation of a function, runtime error results, and a hint to change the implementation appropriately. Write your full implementation " + system_prompt = "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 " info.append(Message(role="system", content=system_prompt)) - info.append(Message(role="assistant", content=reflection_prompt)) + info.append(Message(role="user", content=reflection_prompt)) - msg = messages_to_str(info) - resp = await self.llm.aask(msg=msg) + # msg = messages_to_str(info) + # resp = await self.llm.aask(msg=msg) + resp = await self.llm.aask_code(messages=info, **create_func_config(CODE_REFLECTION)) logger.info(f"reflection is {resp}") return resp - async def rewrite_code(self, reflection: str = "", code_context: str = "") -> str: - """ - 根据reflection重写代码 - """ - info = [] - info.append(Message(role="assistant", content=f"[code context]:{code_context}" - f"finished code are executable, and you should based on the code to continue your current code debug and improvement" - f"[reflection]: \n {reflection}")) - info.append(Message(role="user", content=f"[improved impl]:\n Return in Python block")) - msg = messages_to_str(info) - resp = await self.llm.aask(msg=msg) - logger.info(f"improve code is {resp}") - improv_code = CodeParser.parse_code(block=None, text=resp) - return improv_code + # async def rewrite_code(self, reflection: str = "", context: List[Message] = None) -> str: + # """ + # 根据reflection重写代码 + # """ + # info = context + # # info.append(Message(role="assistant", content=f"[code context]:{code_context}" + # # f"finished code are executable, and you should based on the code to continue your current code debug and improvement" + # # f"[reflection]: \n {reflection}")) + # info.append(Message(role="assistant", content=f"[reflection]: \n {reflection}")) + # info.append(Message(role="user", content=f"[improved impl]:\n Return in Python block")) + # msg = messages_to_str(info) + # resp = await self.llm.aask(msg=msg) + # improv_code = CodeParser.parse_code(block=None, text=resp) + # return improv_code async def run(self, + context: List[Message] = None, plan: str = "", - finished_code: str = "", - finished_code_result: str = "", + # finished_code: str = "", + # finished_code_result: str = "", code: str = "", runtime_result: str = "") -> str: """ 根据当前运行代码和报错信息进行reflection和纠错 """ - reflection = await self.run_reflection(plan, finished_code=finished_code, - finished_code_result=finished_code_result, - code=code, - runtime_result=runtime_result) + reflection = await self.run_reflection( + # plan, + # finished_code=finished_code, + # finished_code_result=finished_code_result, + code=code, + context=context, + runtime_result=runtime_result, + ) # 根据reflection结果重写代码 - improv_code = await self.rewrite_code(reflection, code_context=finished_code) + # improv_code = await self.rewrite_code(reflection, context=context) + improv_code = reflection['improved_impl'] return improv_code