diff --git a/metagpt/actions/write_analysis_code.py b/metagpt/actions/write_analysis_code.py index 8d8f80f4a..038f3db7f 100644 --- a/metagpt/actions/write_analysis_code.py +++ b/metagpt/actions/write_analysis_code.py @@ -30,7 +30,7 @@ class WriteCodeByGenerate(BaseWriteAnalysisCode): super().__init__(name, context, llm) def process_msg(self, prompt: Union[str, List[Dict], Message, List[Message]], system_msg: str = None): - default_system_msg = """You are Open 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.**""" + 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. Reuse existing code directly. Use !pip install to install missing packages.**""" # 全部转成list if not isinstance(prompt, list): prompt = [prompt] diff --git a/metagpt/roles/ml_engineer.py b/metagpt/roles/ml_engineer.py index 480f6cecf..2e4bbfc82 100644 --- a/metagpt/roles/ml_engineer.py +++ b/metagpt/roles/ml_engineer.py @@ -44,6 +44,7 @@ class MLEngineer(Role): self.plan = Plan(goal=goal) self.use_tools = False self.use_task_guide = False + self.execute_code = ExecutePyCode() async def _plan_and_act(self): @@ -90,9 +91,10 @@ class MLEngineer(Role): self._rc.memory.add(Message(content=code, role="assistant", cause_by=cause_by)) - result, success = await ExecutePyCode().run(code) - print(result) - self._rc.memory.add(Message(content=result, role="user", cause_by=ExecutePyCode)) + result, success = await self.execute_code.run(code) + # truncated the result + print(self.truncate(result)) + self._rc.memory.add(Message(content=self.truncate(result), role="user", cause_by=ExecutePyCode)) # if not success: # await self._ask_review() @@ -104,7 +106,8 @@ class MLEngineer(Role): async def _ask_review(self): context = self.get_useful_memories() review, confirmed = await AskReview().run(context=context[-5:], plan=self.plan) - self._rc.memory.add(Message(content=review, role="user", cause_by=AskReview)) + if review.lower() not in ("confirm", "y", "yes"): + self._rc.memory.add(Message(content=review, role="user", cause_by=AskReview)) return confirmed async def _update_plan(self, max_tasks: int = 3): @@ -124,6 +127,18 @@ class MLEngineer(Role): memories = super().get_memories() return memories + def truncate(self, result: str, keep_len: int = 1000) -> str: + desc = """I truncated the result to only keep the last 1000 characters\n""" + if result.startswith(desc): + result = result[-len(desc):] + + if len(result) > keep_len: + result = result[-keep_len:] + + if not result.startswith(desc): + return desc + result + return desc + if __name__ == "__main__": # requirement = "create a normal distribution and visualize it"