diff --git a/metagpt/actions/debug_code.py b/metagpt/actions/debug_code.py index 0dc3ce919..9a8b4c122 100644 --- a/metagpt/actions/debug_code.py +++ b/metagpt/actions/debug_code.py @@ -81,9 +81,9 @@ class DebugCode(BaseWriteAnalysisCode): async def run_reflection( self, - context: List[Message], - code, - runtime_result, + context: list[Message], + code: str, + runtime_result: str, ) -> dict: info = [] reflection_prompt = REFLECTION_PROMPT.format( @@ -107,12 +107,11 @@ class DebugCode(BaseWriteAnalysisCode): runtime_result: str = "", ) -> str: """ - 根据当前运行代码和报错信息进行reflection和纠错 + use reflection to debug, based on current code and the execution errors """ reflection = await self.run_reflection( code=code, context=context, runtime_result=runtime_result, ) - # 根据reflection结果重写代码 return {"code": reflection["improved_impl"]} diff --git a/metagpt/actions/execute_nb_code.py b/metagpt/actions/execute_nb_code.py index 7dfbecb5c..835233dfa 100644 --- a/metagpt/actions/execute_nb_code.py +++ b/metagpt/actions/execute_nb_code.py @@ -5,6 +5,7 @@ @File : code_executor.py """ import asyncio +import base64 import re import traceback from pathlib import Path @@ -117,8 +118,6 @@ class ExecuteNbCode(Action): return parsed_output def show_bytes_figure(self, image_base64: str, interaction_type: str = "ipython"): - import base64 - image_bytes = base64.b64decode(image_base64) if interaction_type == "ipython": from IPython.display import Image, display @@ -145,8 +144,8 @@ class ExecuteNbCode(Action): # 如果在Python脚本中运行,__file__ 变量存在 return False - def _process_code(self, code: Union[str, Dict], language: str = None) -> Tuple: - language = language or "python" + def _process_code(self, code: Union[str, Dict], language: str = "python") -> Tuple: + """handle different code response formats, support str or dict""" if isinstance(code, str) and Path(code).suffix in (".py", ".txt"): code = Path(code).read_text(encoding="utf-8") return code, language diff --git a/metagpt/actions/ml_action.py b/metagpt/actions/ml_action.py index d419026fa..88476707c 100644 --- a/metagpt/actions/ml_action.py +++ b/metagpt/actions/ml_action.py @@ -1,4 +1,3 @@ -import json from typing import List, Tuple from metagpt.actions import Action @@ -11,7 +10,7 @@ from metagpt.prompts.ml_action import ( ) from metagpt.prompts.write_analysis_code import CODE_GENERATOR_WITH_TOOLS from metagpt.schema import Message, Plan -from metagpt.utils.common import CodeParser, create_func_call_config, remove_comments +from metagpt.utils.common import create_func_call_config, remove_comments class WriteCodeWithToolsML(WriteCodeWithTools): @@ -61,36 +60,6 @@ class WriteCodeWithToolsML(WriteCodeWithTools): return context, rsp -class Reflect(Action): - PROMPT_TEMPLATE: str = """ - # Context - __context__ - # Latest User Requirement - __user_requirement__ - # Summary - Above is all your attempts to tackle the user requirement. You plan, act, submit your output, and get the result and feedback. - Output a json following the format: - ```json - { - "summary": str = "summarize each of your previous trial in a triple of (your methods, the corresponding result, potential improvement), list them out", - "takeaways": str = "carefully find key takeaways from your summarization", - "reflection": str = "give specific instruction to improve your next trial in a step-by-step thinking process", - } - ``` - """ - REWRITE_PLAN_INSTRUCTION: str = """Take this reflection for rewriting plan, modify the current plan in place, make reference to your specific instruction, think about you should - change which task, add or delete what tasks in the plan. Only make necessary changes, keep reusable tasks unchanged, output the COMPLETE new plan starting from the first task. Your plan should have no more than 5 tasks.""" - - async def run(self, context: str, user_requirement: str = "") -> str: - user_requirement = user_requirement or "Score as high as possible in a data modeling competition" - # prompt = self.PROMPT_TEMPLATE.format(context=context, user_requirement=user_requirement) - prompt = self.PROMPT_TEMPLATE.replace("__context__", context).replace("__user_requirement__", user_requirement) - rsp_json = await self._aask(prompt) - rsp = CodeParser.parse_code(block=None, text=rsp_json) - reflection = json.loads(rsp)["reflection"] - return reflection - - class UpdateDataColumns(Action): async def run(self, plan: Plan = None) -> dict: finished_tasks = plan.get_finished_tasks() diff --git a/metagpt/actions/write_analysis_code.py b/metagpt/actions/write_analysis_code.py index bf00e8ed1..c47685bdf 100644 --- a/metagpt/actions/write_analysis_code.py +++ b/metagpt/actions/write_analysis_code.py @@ -67,7 +67,7 @@ class BaseWriteAnalysisCode(Action): class WriteCodeByGenerate(BaseWriteAnalysisCode): - """Write code fully by generation""" + """Ask LLM to generate codes purely by itself without local user-defined tools""" async def run( self,