🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
update
stellahsrcf6577334c7b
diff --git a/metagpt/actions/write_analysis_code.py b/metagpt/actions/write_analysis_code.pyindex abfecbbc1..6970fb4f0 100644--- a/metagpt/actions/write_analysis_code.py+++ b/metagpt/actions/write_analysis_code.py@@ -24,9 +24,9 @@ from metagpt.utils.common import create_func_config, remove_commentsclass BaseWriteAnalysisCode(Action):- DEFAULT_SYSTEM_MSG = """You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.""" # prompt reference: https://github.com/KillianLucas/open-interpreter/blob/v0.1.4/interpreter/system_message.txt- REUSE_CODE_INSTRUCTION = """ATTENTION: DONT include codes from previous tasks in your current code block, include new codes only, DONT repeat codes!"""-+ DEFAULT_SYSTEM_MSG = """You are Code Interpreter, a world-class programmer that can complete any goal by executing code. Strictly follow the plan and generate code step by step. Each step of the code will be executed on the user's machine, and the user will provide the code execution results to you.**Notice: The code for the next step depends on the code for the previous step. Must reuse variables in the lastest other code directly, dont creat it again, it is very import for you. Use !pip install in a standalone block to install missing packages.**""" # prompt reference: https://github.com/KillianLucas/open-interpreter/blob/v0.1.4/interpreter/system_message.txt+ # REUSE_CODE_INSTRUCTION = """ATTENTION: DONT include codes from previous tasks in your current code block, include new codes only, DONT repeat codes!"""+def process_msg(self, prompt: Union[str, List[Dict], Message, List[Message]], system_msg: str = None):default_system_msg = system_msg or self.DEFAULT_SYSTEM_MSG# 全部转成list@@ -45,23 +45,23 @@ class BaseWriteAnalysisCode(Action):messages.append(p.to_dict())elif isinstance(p.content, dict) and "code" in p.content:messages.append(p.content["code"])-+# 添加默认的提示词if (- default_system_msg not in messages[0]["content"]- and messages[0]["role"] != "system"+ default_system_msg not in messages[0]["content"]+ and messages[0]["role"] != "system"):messages.insert(0, {"role": "system", "content": default_system_msg})elif (- default_system_msg not in messages[0]["content"]- and messages[0]["role"] == "system"+ default_system_msg not in messages[0]["content"]+ and messages[0]["role"] == "system"):messages[0] = {"role": "system","content": messages[0]["content"] + default_system_msg,}return messages-+async def run(self, context: List[Message], plan: Plan = None, code_steps: str = "") -> str:@@ -79,19 +79,18 @@ class BaseWriteAnalysisCode(Action):class WriteCodeByGenerate(BaseWriteAnalysisCode):"""Write code fully by generation"""-+def __init__(self, name: str = "", context=None, llm=None) -> str:super().__init__(name, context, llm)-+async def run(- self,- context: [List[Message]],- plan: Plan = None,- code_steps: str = "",- system_msg: str = None,- **kwargs,+ self,+ context: [List[Message]],+ plan: Plan = None,+ system_msg: str = None,+ **kwargs,) -> str:- context.append(Message(content=self.REUSE_CODE_INSTRUCTION, role="user"))+ # context.append(Message(content=self.REUSE_CODE_INSTRUCTION, role="user"))prompt = self.process_msg(context, system_msg)code_content = await self.llm.aask_code(prompt, **kwargs)return code_content["code"]@@ -99,16 +98,15 @@ class WriteCodeByGenerate(BaseWriteAnalysisCode):class WriteCodeWithTools(BaseWriteAnalysisCode):"""Write code with help of local available tools. Choose tools first, then generate code to use the tools"""-+def __init__(self, name: str = "", context=None, llm=None, schema_path=None):super().__init__(name, context, llm)self.schema_path = schema_pathself.available_tools = {}-+if self.schema_path is not None:self._load_tools(schema_path)- logger.info(f"available_tools: {len(self.available_tools)}")-+def _load_tools(self, schema_path):"""Load tools from yaml file"""yml_files = schema_path.glob("*.yml")@@ -116,7 +114,7 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):module = yml_file.stemwith open(yml_file, "r", encoding="utf-8") as f:self.available_tools[module] = yaml.safe_load(f)-+def _parse_recommend_tools(self, module: str, recommend_tools: list) -> dict:"""Parses and validates a list of recommended tools, and retrieves their schema from registry.@@ -133,15 +131,15 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):for tool in recommend_tools:if tool in available_tools:valid_tools.append(tool)-+tool_catalog = {tool: self.available_tools[module][tool] for tool in valid_tools}return tool_catalog-+async def _tool_recommendation(- self,- task: str,- code_steps: str,- available_tools: dict,+ self,+ task: str,+ code_steps: str,+ available_tools: dict,) -> list:"""Recommend tools for the specified task.@@ -163,26 +161,26 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):rsp = await self.llm.aask_code(prompt, **tool_config)recommend_tools = rsp["recommend_tools"]return recommend_tools-+async def run(- self,- context: List[Message],- plan: Plan = None,- code_steps: str = "",- column_info: str = "",- **kwargs,+ self,+ context: List[Message],+ plan: Plan = None,+ column_info: str = "",+ **kwargs,) -> Tuple[List[Message], str]:task_type = plan.current_task.task_typeavailable_tools = self.available_tools.get(task_type, {})special_prompt = ML_SPECIFIC_PROMPT.get(task_type, "")-+ code_steps = plan.current_task.code_steps+finished_tasks = plan.get_finished_tasks()code_context = [remove_comments(task.code) for task in finished_tasks]code_context = "\n\n".join(code_context)-+if len(available_tools) > 0:available_tools = {k: v["description"] for k, v in available_tools.items()}-+recommend_tools = await self._tool_recommendation(plan.current_task.instruction,code_steps,@@ -190,22 +188,19 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):)tool_catalog = self._parse_recommend_tools(task_type, recommend_tools)logger.info(f"Recommended tools: \n{recommend_tools}")-- module_name = ML_MODULE_MAP[task_type]-- prompt = TOOL_USAGE_PROMPT.format(- user_requirement=plan.goal,- history_code=code_context,- current_task=plan.current_task.instruction,- column_info=column_info,- special_prompt=special_prompt,- code_steps=code_steps,- module_name=module_name,- tool_catalog=tool_catalog,- )-+ module_name = ML_MODULE_MAP[task_type]+ prompt = TOOL_USAGE_PROMPT.format(+ user_requirement=plan.goal,+ history_code=code_context,+ current_task=plan.current_task.instruction,+ column_info=column_info,+ special_prompt=special_prompt,+ code_steps=code_steps,+ module_name=module_name,+ tool_catalog=tool_catalog,+ )else:prompt = GENERATE_CODE_PROMPT.format(user_requirement=plan.goal,@@ -215,7 +210,7 @@ class WriteCodeWithTools(BaseWriteAnalysisCode):special_prompt=special_prompt,code_steps=code_steps,)-+tool_config = create_func_config(CODE_GENERATOR_WITH_TOOLS)rsp = await self.llm.aask_code(prompt, **tool_config)context = [Message(content=prompt, role="user")]