From b19e4908b20d1c3217ed97edcfd75c4dc18569f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 28 Nov 2023 11:24:52 +0800 Subject: [PATCH 1/6] fix: install missing package. --- metagpt/actions/write_analysis_code.py | 2 +- metagpt/roles/ml_engineer.py | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) 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" From 311cb5e8b4f5b564b6709fe3a65c3d6b2deef75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 28 Nov 2023 12:13:13 +0800 Subject: [PATCH 2/6] add comment for system message. --- metagpt/actions/write_analysis_code.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/actions/write_analysis_code.py b/metagpt/actions/write_analysis_code.py index 038f3db7f..409de5a8f 100644 --- a/metagpt/actions/write_analysis_code.py +++ b/metagpt/actions/write_analysis_code.py @@ -30,6 +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): + # Reference: https://github.com/KillianLucas/open-interpreter/blob/v0.1.4/interpreter/system_message.txt 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): From 460e373dae6078c6353b755e4b3db0b5e449a300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 28 Nov 2023 13:40:28 +0800 Subject: [PATCH 3/6] feat: add auto_run. --- metagpt/roles/ml_engineer.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/metagpt/roles/ml_engineer.py b/metagpt/roles/ml_engineer.py index 2e4bbfc82..af1f3b5b5 100644 --- a/metagpt/roles/ml_engineer.py +++ b/metagpt/roles/ml_engineer.py @@ -38,13 +38,14 @@ class WriteTaskGuide(Action): return "" class MLEngineer(Role): - def __init__(self, name="ABC", profile="MLEngineer", goal=""): + def __init__(self, name="ABC", profile="MLEngineer", goal="", auto_run: bool = False): super().__init__(name=name, profile=profile, goal=goal) self._set_react_mode(react_mode="plan_and_act") self.plan = Plan(goal=goal) self.use_tools = False self.use_task_guide = False self.execute_code = ExecutePyCode() + self.auto_run = auto_run async def _plan_and_act(self): @@ -104,11 +105,13 @@ class MLEngineer(Role): return code, result, success async def _ask_review(self): - context = self.get_useful_memories() - review, confirmed = await AskReview().run(context=context[-5:], plan=self.plan) - if review.lower() not in ("confirm", "y", "yes"): - self._rc.memory.add(Message(content=review, role="user", cause_by=AskReview)) - return confirmed + if not self.auto_run: + context = self.get_useful_memories() + review, confirmed = await AskReview().run(context=context[-5:], plan=self.plan) + if review.lower() not in ("confirm", "y", "yes"): + self._rc.memory.add(Message(content=review, role="user", cause_by=AskReview)) + return confirmed + return True async def _update_plan(self, max_tasks: int = 3): current_plan = str([task.json() for task in self.plan.tasks]) From 608126e1f9906bc69c6ea489674c25c0198ec9ae Mon Sep 17 00:00:00 2001 From: yzlin Date: Tue, 28 Nov 2023 13:50:15 +0800 Subject: [PATCH 4/6] update context --- metagpt/actions/write_plan.py | 7 +++-- metagpt/roles/ml_engineer.py | 48 +++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/metagpt/actions/write_plan.py b/metagpt/actions/write_plan.py index e35ba7a92..dcfa25d55 100644 --- a/metagpt/actions/write_plan.py +++ b/metagpt/actions/write_plan.py @@ -15,8 +15,6 @@ class WritePlan(Action): PROMPT_TEMPLATE = """ # Context: __context__ - # Current Plan: - __current_plan__ # Task: Based on the context, write a plan or modify an existing plan of what you should do to achieve the goal. A plan consists of one to __max_tasks__ tasks. If you are modifying an existing plan, carefully follow the instruction, don't make unnecessary changes. @@ -32,10 +30,11 @@ class WritePlan(Action): ] ``` """ - async def run(self, context: List[Message], current_plan: str = "", max_tasks: int = 5) -> str: + async def run(self, context: List[Message], max_tasks: int = 5) -> str: prompt = ( self.PROMPT_TEMPLATE.replace("__context__", "\n".join([str(ct) for ct in context])) - .replace("__current_plan__", current_plan).replace("__max_tasks__", str(max_tasks)) + # .replace("__current_plan__", current_plan) + .replace("__max_tasks__", str(max_tasks)) ) rsp = await self._aask(prompt) rsp = CodeParser.parse_code(block=None, text=rsp) diff --git a/metagpt/roles/ml_engineer.py b/metagpt/roles/ml_engineer.py index 480f6cecf..910b94432 100644 --- a/metagpt/roles/ml_engineer.py +++ b/metagpt/roles/ml_engineer.py @@ -12,18 +12,27 @@ from metagpt.actions.write_plan import WritePlan from metagpt.actions.write_analysis_code import WriteCodeByGenerate, WriteCodeWithTools from metagpt.actions.execute_code import ExecutePyCode +STRUCTURAL_CONTEXT = """ +## User Requirement +{user_requirement} +## Current Plan +{tasks} +## Current Task +{current_task} +""" + class AskReview(Action): async def run(self, context: List[Message], plan: Plan = None): logger.info("Current overall plan:") - logger.info("\n".join([f"{task.task_id}: {task.instruction}" for task in plan.tasks])) + logger.info("\n".join([f"{task.task_id}: {task.instruction}, is_finished: {task.is_finished}" for task in plan.tasks])) logger.info("most recent context:") # prompt = "\n".join( # [f"{msg.cause_by.__name__ if msg.cause_by else 'Main Requirement'}: {msg.content}" for msg in context] # ) prompt = "" - latest_action = context[-1].cause_by.__name__ + latest_action = context[-1].cause_by.__name__ if context[-1].cause_by else "" prompt += f"\nPlease review output from {latest_action}:\n" \ "If you want to change a task in the plan, say 'change task task_id, ... (things to change)'\n" \ "If you confirm the output and wish to continue with the current process, type CONFIRM:\n" @@ -44,6 +53,7 @@ class MLEngineer(Role): self.plan = Plan(goal=goal) self.use_tools = False self.use_task_guide = False + self.execute_code_action = ExecutePyCode() async def _plan_and_act(self): @@ -65,6 +75,7 @@ class MLEngineer(Role): task.code = code task.result = result self.plan.finish_current_task() + self.working_memory.clear() else: # update plan according to user's feedback and to take on changed tasks @@ -79,6 +90,11 @@ class MLEngineer(Role): while not success and counter < max_retry: context = self.get_useful_memories() + # print("*" * 10) + # print(context) + # print("*" * 10) + # breakpoint() + if not self.use_tools: # code = "print('abc')" code = await WriteCodeByGenerate().run(context=context, plan=self.plan, task_guide=task_guide) @@ -88,11 +104,11 @@ class MLEngineer(Role): code = await WriteCodeWithTools().run(context=context, plan=self.plan, task_guide=task_guide) cause_by = WriteCodeWithTools - self._rc.memory.add(Message(content=code, role="assistant", cause_by=cause_by)) + self.working_memory.add(Message(content=code, role="assistant", cause_by=cause_by)) - result, success = await ExecutePyCode().run(code) + result, success = await self.execute_code_action.run(code) print(result) - self._rc.memory.add(Message(content=result, role="user", cause_by=ExecutePyCode)) + self.working_memory.add(Message(content=result, role="user", cause_by=ExecutePyCode)) # if not success: # await self._ask_review() @@ -108,21 +124,31 @@ class MLEngineer(Role): return confirmed async def _update_plan(self, max_tasks: int = 3): - current_plan = str([task.json() for task in self.plan.tasks]) plan_confirmed = False while not plan_confirmed: context = self.get_useful_memories() - rsp = await WritePlan().run(context, current_plan=current_plan, max_tasks=max_tasks) - self._rc.memory.add(Message(content=rsp, role="assistant", cause_by=WritePlan)) + rsp = await WritePlan().run(context, max_tasks=max_tasks) + self.working_memory.add(Message(content=rsp, role="assistant", cause_by=WritePlan)) plan_confirmed = await self._ask_review() tasks = WritePlan.rsp_to_tasks(rsp) self.plan.add_tasks(tasks) + self.working_memory.clear() - def get_useful_memories(self, current_task_memories: List[str] = []) -> List[Message]: + def get_useful_memories(self) -> List[Message]: """find useful memories only to reduce context length and improve performance""" - memories = super().get_memories() - return memories + + user_requirement = self.plan.goal + tasks = json.dumps([task.dict() for task in self.plan.tasks], indent=4, ensure_ascii=False) + current_task = self.plan.current_task.json() if self.plan.current_task else {} + context = STRUCTURAL_CONTEXT.format(user_requirement=user_requirement, tasks=tasks, current_task=current_task) + context_msg = [Message(content=context, role="user")] + + return context_msg + self.working_memory.get() + + @property + def working_memory(self): + return self._rc.memory if __name__ == "__main__": From 0843a82a1bf40fe1111482dc16bc20ee955122c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 28 Nov 2023 14:21:32 +0800 Subject: [PATCH 5/6] fix: module not found error. --- metagpt/roles/ml_engineer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/ml_engineer.py b/metagpt/roles/ml_engineer.py index 53d693c8e..56fbd2525 100644 --- a/metagpt/roles/ml_engineer.py +++ b/metagpt/roles/ml_engineer.py @@ -84,7 +84,7 @@ class MLEngineer(Role): # ask for acceptance, users can other refuse and change tasks in the plan task_result_confirmed = await self._ask_review() - if success and task_result_confirmed: + if success and task_result_confirmed and not code.startswith("!pip"): # tick off this task and record progress task.code = code task.result = result From 4760dfd13b84db970a32647f0d2e774999ba3c12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 28 Nov 2023 14:50:59 +0800 Subject: [PATCH 6/6] fix: module not found. --- metagpt/roles/ml_engineer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metagpt/roles/ml_engineer.py b/metagpt/roles/ml_engineer.py index 56fbd2525..3f46b9451 100644 --- a/metagpt/roles/ml_engineer.py +++ b/metagpt/roles/ml_engineer.py @@ -84,7 +84,7 @@ class MLEngineer(Role): # ask for acceptance, users can other refuse and change tasks in the plan task_result_confirmed = await self._ask_review() - if success and task_result_confirmed and not code.startswith("!pip"): + if success and task_result_confirmed: # tick off this task and record progress task.code = code task.result = result @@ -126,6 +126,8 @@ class MLEngineer(Role): # print(result) self.working_memory.add(Message(content=result, role="user", cause_by=ExecutePyCode)) + if code.startswith("!pip"): + success = False # if not success: # await self._ask_review()