From 17916b84f696c28a8e2579da62b4cffb14a5c1ed Mon Sep 17 00:00:00 2001 From: Shashank Harinath <9397524+ShankHarinath@users.noreply.github.com> Date: Thu, 19 Oct 2023 00:52:53 -0700 Subject: [PATCH 001/413] Fix indentation --- metagpt/provider/openai_api.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 7e865f288..4c71e1077 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -111,19 +111,19 @@ class CostManager(metaclass=Singleton): return self.total_completion_tokens -def get_total_cost(self): - """ - Get the total cost of API calls. - - Returns: - float: The total cost of API calls. - """ - return self.total_cost - - -def get_costs(self) -> Costs: - """Get all costs""" - return Costs(self.total_prompt_tokens, self.total_completion_tokens, self.total_cost, self.total_budget) + def get_total_cost(self): + """ + Get the total cost of API calls. + + Returns: + float: The total cost of API calls. + """ + return self.total_cost + + + def get_costs(self) -> Costs: + """Get all costs""" + return Costs(self.total_prompt_tokens, self.total_completion_tokens, self.total_cost, self.total_budget) def log_and_reraise(retry_state): From 53fa9ef83844ac27a0c0c0d8a6d598428c183f94 Mon Sep 17 00:00:00 2001 From: Shashank Harinath Date: Thu, 19 Oct 2023 00:59:32 -0700 Subject: [PATCH 002/413] Rename file --- metagpt/provider/openai_api.py | 4 +--- metagpt/roles/__init__.py | 2 +- metagpt/roles/{seacher.py => searcher.py} | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) rename metagpt/roles/{seacher.py => searcher.py} (99%) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 4c71e1077..f0110b148 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -110,7 +110,6 @@ class CostManager(metaclass=Singleton): """ return self.total_completion_tokens - def get_total_cost(self): """ Get the total cost of API calls. @@ -119,8 +118,7 @@ class CostManager(metaclass=Singleton): float: The total cost of API calls. """ return self.total_cost - - + def get_costs(self) -> Costs: """Get all costs""" return Costs(self.total_prompt_tokens, self.total_completion_tokens, self.total_cost, self.total_budget) diff --git a/metagpt/roles/__init__.py b/metagpt/roles/__init__.py index 1768b786c..f033a5dfa 100644 --- a/metagpt/roles/__init__.py +++ b/metagpt/roles/__init__.py @@ -12,7 +12,7 @@ from metagpt.roles.project_manager import ProjectManager from metagpt.roles.product_manager import ProductManager from metagpt.roles.engineer import Engineer from metagpt.roles.qa_engineer import QaEngineer -from metagpt.roles.seacher import Searcher +from metagpt.roles.searcher import Searcher from metagpt.roles.sales import Sales from metagpt.roles.customer_service import CustomerService diff --git a/metagpt/roles/seacher.py b/metagpt/roles/searcher.py similarity index 99% rename from metagpt/roles/seacher.py rename to metagpt/roles/searcher.py index 0b6e089da..3a19f32f2 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/searcher.py @@ -3,7 +3,7 @@ """ @Time : 2023/5/23 17:25 @Author : alexanderwu -@File : seacher.py +@File : searcher.py """ from metagpt.actions import ActionOutput, SearchAndSummarize from metagpt.logs import logger From 7f656c300fbd7dab23533acbbfa1b245eaff8953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 23 Oct 2023 11:21:16 +0800 Subject: [PATCH 003/413] fixbug: issues#445 --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 562a653f3..cff7c3c0c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -48,4 +48,3 @@ websocket-client==0.58.0 aiofiles~=23.2.1 pygments~=2.16.1 requests~=2.31.0 -yaml~=0.2.5 \ No newline at end of file From 5e8ada5cfffd470a7513630391077f0f291e8f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 31 Oct 2023 15:23:37 +0800 Subject: [PATCH 004/413] refactor: Message --- metagpt/schema.py | 106 +++++++++++++++++++++++++--------- tests/metagpt/test_message.py | 22 ++++--- 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index bdca093c2..1124fb28e 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -4,13 +4,15 @@ @Time : 2023/5/8 22:12 @Author : alexanderwu @File : schema.py +@Modified By: mashenquan, 2023-10-31, optimize class members. """ from __future__ import annotations -from dataclasses import dataclass, field -from typing import Type, TypedDict +import json +from json import JSONDecodeError +from typing import Dict, List, TypedDict -from pydantic import BaseModel +from pydantic import BaseModel, Field from metagpt.logs import logger @@ -20,16 +22,44 @@ class RawMessage(TypedDict): role: str -@dataclass -class Message: +class Message(BaseModel): """list[: ]""" + content: str - instruct_content: BaseModel = field(default=None) - role: str = field(default='user') # system / user / assistant - cause_by: Type["Action"] = field(default="") - sent_from: str = field(default="") - send_to: str = field(default="") - restricted_to: str = field(default="") + instruct_content: BaseModel = None + meta_info: Dict = Field(default_factory=dict) + route: List[Dict] = Field(default_factory=list) + + def __init__(self, content, **kwargs): + super(Message, self).__init__( + content=content or kwargs.get("content"), + instruct_content=kwargs.get("instruct_content"), + meta_info=kwargs.get("meta_info", {}), + route=kwargs.get("route", []), + ) + + attribute_names = Message.__annotations__.keys() + for k, v in kwargs.items(): + if k in attribute_names: + continue + self.meta_info[k] = v + + def get_meta(self, key): + return self.meta_info.get(key) + + def set_meta(self, key, value): + self.meta_info[key] = value + + @property + def role(self): + return self.get_meta("role") + + @property + def cause_by(self): + return self.get_meta("cause_by") + + def set_role(self, v): + self.set_meta("role", v) def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) @@ -39,45 +69,67 @@ class Message: return self.__str__() def to_dict(self) -> dict: - return { - "role": self.role, - "content": self.content - } + return {"role": self.role, "content": self.content} + + def save(self) -> str: + return self.json(exclude_none=True) + + @staticmethod + def load(v): + try: + d = json.loads(v) + return Message(**d) + except JSONDecodeError as err: + logger.error(f"parse json failed: {v}, error:{err}") + return None -@dataclass class UserMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'user') + super(Message, self).__init__(content=content, meta_info={"role": "user"}) -@dataclass class SystemMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'system') + super().__init__(content=content, meta_info={"role": "system"}) -@dataclass class AIMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'assistant') + super().__init__(content=content, meta_info={"role": "assistant"}) -if __name__ == '__main__': - test_content = 'test_message' +if __name__ == "__main__": + m = Message("a", role="v1") + m.set_role("v2") + v = m.save() + m = Message.load(v) + + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] logger.info(msgs) + + jsons = [ + UserMessage(test_content).save(), + SystemMessage(test_content).save(), + AIMessage(test_content).save(), + Message(test_content, role="QA").save(), + ] + logger.info(jsons) diff --git a/tests/metagpt/test_message.py b/tests/metagpt/test_message.py index e26f38381..4f46311ce 100644 --- a/tests/metagpt/test_message.py +++ b/tests/metagpt/test_message.py @@ -11,26 +11,30 @@ from metagpt.schema import AIMessage, Message, RawMessage, SystemMessage, UserMe def test_message(): - msg = Message(role='User', content='WTF') - assert msg.to_dict()['role'] == 'User' - assert 'User' in str(msg) + msg = Message(role="User", content="WTF") + assert msg.to_dict()["role"] == "User" + assert "User" in str(msg) def test_all_messages(): - test_content = 'test_message' + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] for msg in msgs: assert msg.content == test_content def test_raw_message(): - msg = RawMessage(role='user', content='raw') - assert msg['role'] == 'user' - assert msg['content'] == 'raw' + msg = RawMessage(role="user", content="raw") + assert msg["role"] == "user" + assert msg["content"] == "raw" with pytest.raises(KeyError): - assert msg['1'] == 1, "KeyError: '1'" + assert msg["1"] == 1, "KeyError: '1'" + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) From 9ccf1e8b82fe8b7b92acc81e170299750e578409 Mon Sep 17 00:00:00 2001 From: Bian Jiang Date: Wed, 1 Nov 2023 10:48:54 +0800 Subject: [PATCH 005/413] Fixed the workspace directory does not exist --- examples/agent_creator.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 325e7c260..6269dac10 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -49,6 +49,8 @@ class CreateAgent(Action): pattern = r'```python(.*)```' match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" + if not WORKSPACE_ROOT.exists(): + WORKSPACE_ROOT.mkdir(parents=True) with open(WORKSPACE_ROOT / "agent_created_agent.py", "w") as f: f.write(code_text) return code_text From 545d77ce0deac125c14ff8c902ca49ff5ded8cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 1 Nov 2023 20:08:58 +0800 Subject: [PATCH 006/413] refactor: Refactor Message transmission & filtering --- examples/agent_creator.py | 20 +- examples/build_customized_agent.py | 28 +-- examples/debate.py | 47 +++-- examples/sk_agent.py | 9 +- metagpt/actions/action.py | 3 +- metagpt/actions/write_code.py | 9 +- metagpt/const.py | 5 + metagpt/environment.py | 48 +++-- metagpt/memory/longterm_memory.py | 16 +- metagpt/memory/memory.py | 11 +- metagpt/roles/engineer.py | 68 +++++-- metagpt/roles/qa_engineer.py | 46 +++-- metagpt/roles/researcher.py | 15 +- metagpt/roles/role.py | 163 ++++++++++------ metagpt/roles/seacher.py | 36 ++-- metagpt/roles/sk_agent.py | 6 +- metagpt/schema.py | 193 ++++++++++++++++++- metagpt/software_company.py | 22 ++- metagpt/utils/common.py | 16 +- metagpt/utils/named.py | 21 ++ tests/metagpt/actions/test_write_prd.py | 3 +- tests/metagpt/memory/test_longterm_memory.py | 31 +-- tests/metagpt/memory/test_memory_storage.py | 70 +++---- tests/metagpt/planner/test_action_planner.py | 7 +- tests/metagpt/planner/test_basic_planner.py | 6 +- tests/metagpt/roles/mock.py | 27 +-- tests/metagpt/roles/test_architect.py | 5 +- tests/metagpt/roles/test_engineer.py | 13 +- tests/metagpt/test_environment.py | 3 +- tests/metagpt/utils/test_serialize.py | 7 +- 30 files changed, 658 insertions(+), 296 deletions(-) create mode 100644 metagpt/utils/named.py diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 325e7c260..d13cbcff2 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -1,22 +1,24 @@ -''' +""" Filename: MetaGPT/examples/agent_creator.py Created Date: Tuesday, September 12th 2023, 3:28:37 pm Author: garylin2099 -''' +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" import re -from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT from metagpt.actions import Action +from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger +from metagpt.utils.common import get_object_name with open(PROJECT_ROOT / "examples/build_customized_agent.py", "r") as f: # use official example script to guide AgentCreator MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read() -class CreateAgent(Action): +class CreateAgent(Action): PROMPT_TEMPLATE = """ ### BACKGROUND You are using an agent framework called metagpt to write agents capable of different actions, @@ -34,7 +36,6 @@ class CreateAgent(Action): """ async def run(self, example: str, instruction: str): - prompt = self.PROMPT_TEMPLATE.format(example=example, instruction=instruction) # logger.info(prompt) @@ -46,13 +47,14 @@ class CreateAgent(Action): @staticmethod def parse_code(rsp): - pattern = r'```python(.*)```' + pattern = r"```python(.*)```" match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" with open(WORKSPACE_ROOT / "agent_created_agent.py", "w") as f: f.write(code_text) return code_text + class AgentCreator(Role): def __init__( self, @@ -72,15 +74,15 @@ class AgentCreator(Role): instruction = msg.content code_text = await CreateAgent().run(example=self.agent_template, instruction=instruction) - msg = Message(content=code_text, role=self.profile, cause_by=todo) + msg = Message(content=code_text, role=self.profile, cause_by=get_object_name(todo)) return msg + if __name__ == "__main__": import asyncio async def main(): - agent_template = MULTI_ACTION_AGENT_CODE_EXAMPLE creator = AgentCreator(agent_template=agent_template) diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index 87d7a9c76..a953dee15 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -1,21 +1,23 @@ -''' +""" Filename: MetaGPT/examples/build_customized_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -''' +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" +import asyncio import re import subprocess -import asyncio import fire from metagpt.actions import Action +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger +from metagpt.utils.common import get_object_name + class SimpleWriteCode(Action): - PROMPT_TEMPLATE = """ Write a python function that can {instruction} and provide two runnnable test cases. Return ```python your_code_here ``` with NO other texts, @@ -35,7 +37,6 @@ class SimpleWriteCode(Action): super().__init__(name, context, llm) async def run(self, instruction: str): - prompt = self.PROMPT_TEMPLATE.format(instruction=instruction) rsp = await self._aask(prompt) @@ -46,11 +47,12 @@ class SimpleWriteCode(Action): @staticmethod def parse_code(rsp): - pattern = r'```python(.*)```' + pattern = r"```python(.*)```" match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else rsp return code_text + class SimpleRunCode(Action): def __init__(self, name="SimpleRunCode", context=None, llm=None): super().__init__(name, context, llm) @@ -61,6 +63,7 @@ class SimpleRunCode(Action): logger.info(f"{code_result=}") return code_result + class SimpleCoder(Role): def __init__( self, @@ -75,14 +78,15 @@ class SimpleCoder(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") todo = self._rc.todo - msg = self._rc.memory.get()[-1] # retrieve the latest memory + msg = self._rc.memory.get()[-1] # retrieve the latest memory instruction = msg.content code_text = await SimpleWriteCode().run(instruction) - msg = Message(content=code_text, role=self.profile, cause_by=todo) + msg = Message(content=code_text, role=self.profile, cause_by=get_object_name(todo)) return msg + class RunnableCoder(Role): def __init__( self, @@ -116,7 +120,7 @@ class RunnableCoder(Role): code_text = msg.content result = await SimpleRunCode().run(code_text) - msg = Message(content=result, role=self.profile, cause_by=todo) + msg = Message(content=result, role=self.profile, cause_by=get_object_name(todo)) self._rc.memory.add(msg) return msg @@ -128,6 +132,7 @@ class RunnableCoder(Role): await self._act() return Message(content="All job done", role=self.profile) + def main(msg="write a function that calculates the sum of a list"): # role = SimpleCoder() role = RunnableCoder() @@ -135,5 +140,6 @@ def main(msg="write a function that calculates the sum of a list"): result = asyncio.run(role.run(msg)) logger.info(result) -if __name__ == '__main__': + +if __name__ == "__main__": fire.Fire(main) diff --git a/examples/debate.py b/examples/debate.py index 05db28070..ade1a6fc4 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -1,17 +1,20 @@ -''' +""" Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -''' +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" import asyncio import platform + import fire -from metagpt.software_company import SoftwareCompany from metagpt.actions import Action, BossRequirement +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger +from metagpt.software_company import SoftwareCompany + class ShoutOut(Action): """Action: Shout out loudly in a debate (quarrel)""" @@ -31,7 +34,6 @@ class ShoutOut(Action): super().__init__(name, context, llm) async def run(self, context: str, name: str, opponent_name: str): - prompt = self.PROMPT_TEMPLATE.format(context=context, name=name, opponent_name=opponent_name) # logger.info(prompt) @@ -39,6 +41,7 @@ class ShoutOut(Action): return rsp + class Trump(Role): def __init__( self, @@ -55,13 +58,13 @@ class Trump(Role): async def _observe(self) -> int: await super()._observe() # accept messages sent (from opponent) to self, disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.send_to == self.name] + self._rc.news = [msg for msg in self._rc.news if msg.is_recipient({self.name})] return len(self._rc.news) async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([ShoutOut]) + msg_history = self._rc.memory.get_by_actions([ShoutOut.get_class_name()]) context = [] for m in msg_history: context.append(str(m)) @@ -72,13 +75,14 @@ class Trump(Role): msg = Message( content=rsp, role=self.profile, - cause_by=ShoutOut, - sent_from=self.name, - send_to=self.opponent_name, + cause_by=ShoutOut.get_class_name(), + tx_from=self.name, + tx_to=self.opponent_name, ) return msg + class Biden(Role): def __init__( self, @@ -96,13 +100,14 @@ class Biden(Role): await super()._observe() # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, # disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.cause_by == BossRequirement or msg.send_to == self.name] + message_filter = {BossRequirement.get_class_name(), self.name} + self._rc.news = [msg for msg in self._rc.news if msg.is_recipient(message_filter)] return len(self._rc.news) async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut]) + msg_history = self._rc.memory.get_by_actions([BossRequirement.get_class_name(), ShoutOut.get_class_name()]) context = [] for m in msg_history: context.append(str(m)) @@ -113,17 +118,19 @@ class Biden(Role): msg = Message( content=rsp, role=self.profile, - cause_by=ShoutOut, - sent_from=self.name, - send_to=self.opponent_name, + cause_by=ShoutOut.get_class_name(), + tx_from=self.name, + tx_to=self.opponent_name, ) return msg -async def startup(idea: str, investment: float = 3.0, n_round: int = 5, - code_review: bool = False, run_tests: bool = False): + +async def startup( + idea: str, investment: float = 3.0, n_round: int = 5, code_review: bool = False, run_tests: bool = False +): """We reuse the startup paradigm for roles to interact with each other. - Now we run a startup of presidents and watch they quarrel. :) """ + Now we run a startup of presidents and watch they quarrel. :)""" company = SoftwareCompany() company.hire([Biden(), Trump()]) company.invest(investment) @@ -133,7 +140,7 @@ async def startup(idea: str, investment: float = 3.0, n_round: int = 5, def main(idea: str, investment: float = 3.0, n_round: int = 10): """ - :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting" + :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting" or "Trump: Climate change is a hoax" :param investment: contribute a certain dollar amount to watch the debate :param n_round: maximum rounds of the debate @@ -144,5 +151,5 @@ def main(idea: str, investment: float = 3.0, n_round: int = 10): asyncio.run(startup(idea, investment, n_round)) -if __name__ == '__main__': +if __name__ == "__main__": fire.Fire(main) diff --git a/examples/sk_agent.py b/examples/sk_agent.py index a7513e838..19ee53669 100644 --- a/examples/sk_agent.py +++ b/examples/sk_agent.py @@ -4,6 +4,7 @@ @Time : 2023/9/13 12:36 @Author : femto Zheng @File : sk_agent.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ import asyncio @@ -39,7 +40,7 @@ async def basic_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) async def sequential_planner_example(): @@ -53,7 +54,7 @@ async def sequential_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) async def basic_planner_web_search_example(): @@ -64,7 +65,7 @@ async def basic_planner_web_search_example(): role.import_skill(SkSearchEngine(), "WebSearchSkill") # role.import_semantic_skill_from_directory(skills_directory, "QASkill") - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) async def action_planner_example(): @@ -75,7 +76,7 @@ async def action_planner_example(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - await role.run(Message(content=task, cause_by=BossRequirement)) # it will choose mathskill.Add + await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) # it will choose mathskill.Add if __name__ == "__main__": diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..1954e750a 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -16,9 +16,10 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder +from metagpt.utils.named import Named -class Action(ABC): +class Action(ABC, Named): def __init__(self, name: str = "", context=None, llm: LLM = None): self.name: str = name if llm is None: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c000805c5..421211d60 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -5,13 +5,14 @@ @Author : alexanderwu @File : write_code.py """ +from tenacity import retry, stop_after_attempt, wait_fixed + from metagpt.actions import WriteDesign from metagpt.actions.action import Action from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -55,7 +56,8 @@ class WriteCode(Action): if self._is_invalid(filename): return - design = [i for i in context if i.cause_by == WriteDesign][0] + message_filter = {WriteDesign.get_class_name()} + design = [i for i in context if i.is_recipient(message_filter)][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) ws_path = WORKSPACE_ROOT / ws_name @@ -74,9 +76,8 @@ class WriteCode(Action): async def run(self, context, filename): prompt = PROMPT_TEMPLATE.format(context=context, filename=filename) - logger.info(f'Writing {filename}..') + logger.info(f"Writing {filename}..") code = await self.write_code(prompt) # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) return code - \ No newline at end of file diff --git a/metagpt/const.py b/metagpt/const.py index 7f3f87dfa..3fbc26784 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -41,3 +41,8 @@ INVOICE_OCR_TABLE_PATH = DATA_PATH / "invoice_table" SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" MEM_TTL = 24 * 30 * 3600 + +MESSAGE_ROUTE_FROM = "tx_from" +MESSAGE_ROUTE_TO = "tx_to" +MESSAGE_ROUTE_CAUSE_BY = "cause_by" +MESSAGE_META_ROLE = "role" diff --git a/metagpt/environment.py b/metagpt/environment.py index 24e6ada2f..ba0645a36 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -4,60 +4,61 @@ @Time : 2023/5/11 22:12 @Author : alexanderwu @File : environment.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Remove the functionality of `Environment` class as a public message buffer. + 2. Standardize the message forwarding behavior of the `Environment` class. + 3. Add the `is_idle` property. """ import asyncio from typing import Iterable from pydantic import BaseModel, Field -from metagpt.memory import Memory +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message class Environment(BaseModel): """环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到 - Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles - + Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles + """ roles: dict[str, Role] = Field(default_factory=dict) - memory: Memory = Field(default_factory=Memory) - history: str = Field(default='') class Config: arbitrary_types_allowed = True def add_role(self, role: Role): """增加一个在当前环境的角色 - Add a role in the current environment + Add a role in the current environment """ role.set_env(self) self.roles[role.profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 - Add a batch of characters in the current environment + Add a batch of characters in the current environment """ for role in roles: self.add_role(role) def publish_message(self, message: Message): - """向当前环境发布信息 - Post information to the current environment - """ - # self.message_queue.put(message) - self.memory.add(message) - self.history += f"\n{message}" + """Distribute the message to the recipients.""" + logger.info(f"publish_message: {message.save()}") + found = False + for r in self.roles.values(): + if message.is_recipient(r.subscribed_tags): + r.async_put_message(message) + found = True + if not found: + logger.warning(f"Message no recipients: {message.save()}") async def run(self, k=1): """处理一次所有信息的运行 Process all Role runs at once """ - # while not self.message_queue.empty(): - # message = self.message_queue.get() - # rsp = await self.manager.handle(message, self) - # self.message_queue.put(rsp) for _ in range(k): futures = [] for role in self.roles.values(): @@ -65,15 +66,24 @@ class Environment(BaseModel): futures.append(future) await asyncio.gather(*futures) + logger.info(f"is idle: {self.is_idle}") def get_roles(self) -> dict[str, Role]: """获得环境内的所有角色 - Process all Role runs at once + Process all Role runs at once """ return self.roles def get_role(self, name: str) -> Role: """获得环境内的指定角色 - get all the environment roles + get all the environment roles """ return self.roles.get(name, None) + + @property + def is_idle(self): + """If true, all actions have been executed.""" + for r in self.roles.values(): + if not r.is_idle: + return False + return True diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index f8abea5f3..b5bb73b6b 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -1,6 +1,10 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# @Desc : the implement of Long-term memory +""" +@Desc : the implement of Long-term memory +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Replace code related to message filtering with the `Message.is_recipient` function. +""" from metagpt.logs import logger from metagpt.memory import Memory @@ -36,11 +40,10 @@ class LongTermMemory(Memory): def add(self, message: Message): super(LongTermMemory, self).add(message) - for action in self.rc.watch: - if message.cause_by == action and not self.msg_from_recover: - # currently, only add role's watching messages to its memory_storage - # and ignore adding messages from recover repeatedly - self.memory_storage.add(message) + if message.is_recipient(self.rc.watch) and not self.msg_from_recover: + # currently, only add role's watching messages to its memory_storage + # and ignore adding messages from recover repeatedly + self.memory_storage.add(message) def find_news(self, observed: list[Message], k=0) -> list[Message]: """ @@ -68,4 +71,3 @@ class LongTermMemory(Memory): def clear(self): super(LongTermMemory, self).clear() self.memory_storage.clean() - \ No newline at end of file diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index c818fa707..8e01544f1 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -4,11 +4,11 @@ @Time : 2023/5/20 12:15 @Author : alexanderwu @File : memory.py +@Modified By: mashenquan, 2023-11-1. Standardize the design of message filtering-related features. """ from collections import defaultdict -from typing import Iterable, Type +from typing import Iterable, Set -from metagpt.actions import Action from metagpt.schema import Message @@ -18,7 +18,7 @@ class Memory: def __init__(self): """Initialize an empty storage list and an empty index dictionary""" self.storage: list[Message] = [] - self.index: dict[Type[Action], list[Message]] = defaultdict(list) + self.index: dict[str, list[Message]] = defaultdict(list) def add(self, message: Message): """Add a new message to storage, while updating the index""" @@ -73,11 +73,11 @@ class Memory: news.append(i) return news - def get_by_action(self, action: Type[Action]) -> list[Message]: + def get_by_action(self, action: str) -> list[Message]: """Return all messages triggered by a specified Action""" return self.index[action] - def get_by_actions(self, actions: Iterable[Type[Action]]) -> list[Message]: + def get_by_actions(self, actions: Set[str]) -> list[Message]: """Return all messages triggered by specified Actions""" rsp = [] for action in actions: @@ -85,4 +85,3 @@ class Memory: continue rsp += self.index[action] return rsp - \ No newline at end of file diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 6d65575a8..9826ea0b7 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -4,6 +4,10 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : engineer.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Consolidate message reception and processing logic within `_observe`. + 2. Fix bug: Add logic for handling asynchronous message processing when messages are not ready. + 3. Supplemented the external transmission of internal messages. """ import asyncio import shutil @@ -15,7 +19,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser +from metagpt.utils.common import CodeParser, get_object_name from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -75,7 +79,7 @@ class Engineer(Role): self.use_code_review = use_code_review if self.use_code_review: self._init_actions([WriteCode, WriteCodeReview]) - self._watch([WriteTasks]) + self._watch([WriteTasks, WriteDesign]) self.todos = [] self.n_borg = n_borg @@ -96,7 +100,7 @@ class Engineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] + msg = self._rc.memory.get_by_action(WriteDesign.get_class_name())[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -119,17 +123,13 @@ class Engineer(Role): file.write_text(code) return file - def recv(self, message: Message) -> None: - self._rc.memory.add(message) - if message in self._rc.important_memory: - self.todos = self.parse_tasks(message) - async def _act_mp(self) -> Message: # self.recreate_workspace() todo_coros = [] for todo in self.todos: todo_coro = WriteCode().run( - context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), filename=todo + context=self._rc.memory.get_by_actions([WriteTasks.get_class_name(), WriteDesign.get_class_name()]), + filename=todo, ) todo_coros.append(todo_coro) @@ -139,12 +139,13 @@ class Engineer(Role): logger.info(todo) logger.info(code_rsp) # self.write_file(todo, code) - msg = Message(content=code_rsp, role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content=code_rsp, role=self.profile, cause_by=get_object_name(self._rc.todo)) self._rc.memory.add(msg) + self.publish_message(msg) del self.todos[0] logger.info(f"Done {self.get_workspace()} generating.") - msg = Message(content="all done.", role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content="all done.", role=self.profile, cause_by=get_object_name(self._rc.todo)) return msg async def _act_sp(self) -> Message: @@ -155,15 +156,19 @@ class Engineer(Role): # logger.info(code_rsp) # code = self.parse_code(code_rsp) file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content=code, role=self.profile, cause_by=get_object_name(self._rc.todo)) self._rc.memory.add(msg) + self.publish_message(msg) code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) logger.info(f"Done {self.get_workspace()} generating.") msg = Message( - content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" + content=MSG_SEP.join(code_msg_all), + role=self.profile, + cause_by=get_object_name(self._rc.todo), + tx_to="QaEngineer", ) return msg @@ -178,7 +183,8 @@ class Engineer(Role): TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ context = [] - msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) + msg_filters = [WriteDesign.get_class_name(), WriteTasks.get_class_name(), WriteCode.get_class_name()] + msg = self._rc.memory.get_by_actions(msg_filters) for m in msg: context.append(m.content) context_str = "\n".join(context) @@ -193,20 +199,50 @@ class Engineer(Role): logger.error("code review failed!", e) pass file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=WriteCode) + msg = Message(content=code, role=self.profile, cause_by=WriteCode.get_class_name()) self._rc.memory.add(msg) + self.publish_message(msg) code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) logger.info(f"Done {self.get_workspace()} generating.") msg = Message( - content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" + content=MSG_SEP.join(code_msg_all), + role=self.profile, + cause_by=get_object_name(self._rc.todo), + tx_to="QaEngineer", ) return msg async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" + if not self._rc.todo: + return None if self.use_code_review: return await self._act_sp_precision() return await self._act_sp() + + async def _observe(self) -> int: + ret = await super(Engineer, self)._observe() + if ret == 0: + return ret + + # Parse task lists + message_filter = {WriteTasks.get_class_name()} + for message in self._rc.news: + if not message.is_recipient(message_filter): + continue + self.todos = self.parse_tasks(message) + + return ret + + async def _think(self) -> None: + # In asynchronous scenarios, first check if the required messages are ready. + filters = {WriteTasks.get_class_name()} + msgs = self._rc.memory.get_by_actions(filters) + if not msgs: + self._rc.todo = None + return + + await super(Engineer, self)._think() diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index a763c2ce8..b83ab6e21 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -4,6 +4,7 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : qa_engineer.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ import os from pathlib import Path @@ -48,7 +49,7 @@ class QaEngineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self, return_proj_dir=True) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] + msg = self._rc.memory.get_by_action(WriteDesign.get_class_name())[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -97,11 +98,11 @@ class QaEngineer(Role): msg = Message( content=str(file_info), role=self.profile, - cause_by=WriteTest, - sent_from=self.profile, - send_to=self.profile, + cause_by=WriteTest.get_class_name(), + tx_from=self.profile, + tx_to=self.profile, ) - self._publish_message(msg) + self.publish_message(msg) logger.info(f"Done {self.get_workspace()}/tests generating.") @@ -131,8 +132,10 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient) - self._publish_message(msg) + msg = Message( + content=content, role=self.profile, cause_by=RunCode.get_class_name(), tx_from=self.profile, tx_to=recipient + ) + self.publish_message(msg) async def _debug_error(self, msg): file_info, context = msg.content.split(FILENAME_CODE_SEP) @@ -141,14 +144,18 @@ class QaEngineer(Role): self.write_file(file_name, code) recipient = msg.sent_from # send back to the one who ran the code for another run, might be one's self msg = Message( - content=file_info, role=self.profile, cause_by=DebugError, sent_from=self.profile, send_to=recipient + content=file_info, + role=self.profile, + cause_by=DebugError.get_class_name(), + tx_from=self.profile, + tx_to=recipient, ) - self._publish_message(msg) + self.publish_message(msg) async def _observe(self) -> int: await super()._observe() self._rc.news = [ - msg for msg in self._rc.news if msg.send_to == self.profile + msg for msg in self._rc.news if msg.is_recipient({self.profile}) ] # only relevant msgs count as observed news return len(self._rc.news) @@ -157,30 +164,31 @@ class QaEngineer(Role): result_msg = Message( content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, - cause_by=WriteTest, - sent_from=self.profile, - send_to="", + cause_by=WriteTest.get_class_name(), + tx_from=self.profile, ) return result_msg + code_filters = {WriteCode.get_class_name(), WriteCodeReview.get_class_name()} + test_filters = {WriteTest.get_class_name(), DebugError.get_class_name()} + run_filters = {RunCode.get_class_name()} for msg in self._rc.news: # Decide what to do based on observed msg type, currently defined by human, # might potentially be moved to _think, that is, let the agent decides for itself - if msg.cause_by in [WriteCode, WriteCodeReview]: + if msg.is_recipient(code_filters): # engineer wrote a code, time to write a test for it await self._write_test(msg) - elif msg.cause_by in [WriteTest, DebugError]: + elif msg.is_recipient(test_filters): # I wrote or debugged my test code, time to run it await self._run_code(msg) - elif msg.cause_by == RunCode: + elif msg.is_recipient(run_filters): # I ran my test code, time to fix bugs, if any await self._debug_error(msg) self.test_round += 1 result_msg = Message( content=f"Round {self.test_round} of tests done", role=self.profile, - cause_by=WriteTest, - sent_from=self.profile, - send_to="", + cause_by=WriteTest.get_class_name(), + tx_from=self.profile, ) return result_msg diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index acb46c718..43ee7971d 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -1,4 +1,8 @@ #!/usr/bin/env python +""" +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" + import asyncio @@ -10,6 +14,7 @@ from metagpt.const import RESEARCH_PATH from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message +from metagpt.utils.common import get_object_name class Report(BaseModel): @@ -58,18 +63,22 @@ class Researcher(Role): research_system_text = get_research_system_text(topic, self.language) if isinstance(todo, CollectLinks): links = await todo.run(topic, 4, 4) - ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=type(todo)) + ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=get_object_name(todo)) elif isinstance(todo, WebBrowseAndSummarize): links = instruct_content.links todos = (todo.run(*url, query=query, system_text=research_system_text) for (query, url) in links.items()) summaries = await asyncio.gather(*todos) summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary) - ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=type(todo)) + ret = Message( + "", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=get_object_name(todo) + ) else: summaries = instruct_content.summaries summary_text = "\n---\n".join(f"url: {url}\nsummary: {summary}" for (url, summary) in summaries) content = await self._rc.todo.run(topic, summary_text, system_text=research_system_text) - ret = Message("", Report(topic=topic, content=content), role=self.profile, cause_by=type(self._rc.todo)) + ret = Message( + "", Report(topic=topic, content=content), role=self.profile, get_object_name=type(self._rc.todo) + ) self._rc.memory.add(ret) return ret diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 44bb3e976..0a6716428 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -4,20 +4,32 @@ @Time : 2023/5/11 14:42 @Author : alexanderwu @File : role.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Merge the `recv` functionality into the `_observe` function. Future message reading operations will be + consolidated within the `_observe` function. + 2. Standardize the message filtering for string label matching. Role objects can access the message labels + they've subscribed to through the `subscribed_tags` property. + 3. Move the message receive buffer from the global variable `self._rc.env.memory` to the role's private variable + `self._rc.msg_buffer` for easier message identification and asynchronous appending of messages. + 4. Standardize the way messages are passed: `publish_message` sends messages out, while `async_put_message` places + messages into the Role object's private message receive buffer. There are no other message transmit methods. + 5. Standardize the parameters for the `run` function: the `test_message` parameter is used for testing purposes + only. In the normal workflow, you should use `publish_message` or `async_put_message` to transmit messages. """ from __future__ import annotations -from typing import Iterable, Type +from typing import Iterable, Set, Type from pydantic import BaseModel, Field -# from metagpt.environment import Environment -from metagpt.config import CONFIG from metagpt.actions import Action, ActionOutput +from metagpt.config import CONFIG from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.memory import Memory, LongTermMemory -from metagpt.schema import Message +from metagpt.memory import LongTermMemory, Memory +from metagpt.schema import Message, MessageQueue +from metagpt.utils.common import get_class_name, get_object_name +from metagpt.utils.named import Named PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -49,6 +61,7 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi class RoleSetting(BaseModel): """Role Settings""" + name: str profile: str goal: str @@ -64,12 +77,14 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" - env: 'Environment' = Field(default=None) + + env: "Environment" = Field(default=None) + msg_buffer: MessageQueue = Field(default_factory=MessageQueue) # Message Buffer with Asynchronous Updates memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=0) todo: Action = Field(default=None) - watch: set[Type[Action]] = Field(default_factory=set) + watch: set[str] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[]) class Config: @@ -90,7 +105,7 @@ class RoleContext(BaseModel): return self.memory.get() -class Role: +class Role(Named): """Role/Agent""" def __init__(self, name="", profile="", goal="", constraints="", desc=""): @@ -118,7 +133,8 @@ class Role: def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" - self._rc.watch.update(actions) + tags = [get_class_name(t) for t in actions] + self._rc.watch.update(tags) # check RoleContext after adding watch actions self._rc.check(self._role_id) @@ -128,7 +144,7 @@ class Role: logger.debug(self._actions) self._rc.todo = self._actions[self._rc.state] - def set_env(self, env: 'Environment'): + def set_env(self, env: "Environment"): """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env @@ -137,6 +153,24 @@ class Role: """Get the role description (position)""" return self._setting.profile + @property + def name(self): + """Get virtual user name""" + return self._setting.name + + @property + def subscribed_tags(self) -> Set: + """The labels for messages to be consumed by the Role object.""" + if self._rc.watch: + return self._rc.watch + return { + self.name, + self.get_object_name(), + self.profile, + f"{self.name}({self.profile})", + f"{self.name}({self.get_object_name()})", + } + def _get_prefix(self): """Get the role prefix""" if self._setting.desc: @@ -150,94 +184,99 @@ class Role: self._set_state(0) return prompt = self._get_prefix() - prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), - n_states=len(self._states) - 1) + prompt += STATE_TEMPLATE.format( + history=self._rc.history, states="\n".join(self._states), n_states=len(self._states) - 1 + ) next_state = await self._llm.aask(prompt) logger.debug(f"{prompt=}") if not next_state.isdigit() or int(next_state) not in range(len(self._states)): - logger.warning(f'Invalid answer of state, {next_state=}') + logger.warning(f"Invalid answer of state, {next_state=}") next_state = "0" self._set_state(int(next_state)) async def _act(self) -> Message: - # prompt = self.get_prefix() - # prompt += ROLE_TEMPLATE.format(name=self.profile, state=self.states[self.state], result=response, - # history=self.history) - logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) - # logger.info(response) if isinstance(response, ActionOutput): - msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + msg = Message( + content=response.content, + instruct_content=response.instruct_content, + role=self.profile, + cause_by=get_object_name(self._rc.todo), + tx_from=get_object_name(self), + ) else: - msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) - self._rc.memory.add(msg) - # logger.debug(f"{response}") + msg = Message( + content=response, + role=self.profile, + cause_by=get_object_name(self._rc.todo), + tx_from=get_object_name(self), + ) return msg async def _observe(self) -> int: - """Observe from the environment, obtain important information, and add it to memory""" - if not self._rc.env: - return 0 - env_msgs = self._rc.env.memory.get() - - observed = self._rc.env.memory.get_by_actions(self._rc.watch) - - self._rc.news = self._rc.memory.find_news(observed) # find news (previously unseen messages) from observed messages - - for i in env_msgs: - self.recv(i) + """Prepare new messages for processing from the message buffer and other sources.""" + # Read unprocessed messages from the msg buffer. + self._rc.news = self._rc.msg_buffer.pop_all() + # Store the read messages in your own memory to prevent duplicate processing. + self._rc.memory.add_batch(self._rc.news) + # Design Rules: + # If you need to further categorize Message objects, you can do so using the Message.set_meta function. + # msg_buffer is a receiving buffer, avoid adding message data and operations to msg_buffer. news_text = [f"{i.role}: {i.content[:20]}..." for i in self._rc.news] if news_text: - logger.debug(f'{self._setting} observed: {news_text}') + logger.debug(f"{self._setting} observed: {news_text}") return len(self._rc.news) - def _publish_message(self, msg): + def publish_message(self, msg): """If the role belongs to env, then the role's messages will be broadcast to env""" + if not msg: + return if not self._rc.env: # If env does not exist, do not publish the message return self._rc.env.publish_message(msg) + def async_put_message(self, message): + """Place the message into the Role object's private message buffer.""" + if not message: + return + self._rc.msg_buffer.push(message) + async def _react(self) -> Message: """Think first, then act""" await self._think() logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") return await self._act() - def recv(self, message: Message) -> None: - """add message to history.""" - # self._history += f"\n{message}" - # self._context = self._history - if message in self._rc.memory.get(): - return - self._rc.memory.add(message) - - async def handle(self, message: Message) -> Message: - """Receive information and reply with actions""" - # logger.debug(f"{self.name=}, {self.profile=}, {message.role=}") - self.recv(message) - - return await self._react() - - async def run(self, message=None): + async def run(self, test_message=None): """Observe, and think and act based on the results of the observation""" - if message: - if isinstance(message, str): - message = Message(message) - if isinstance(message, Message): - self.recv(message) - if isinstance(message, list): - self.recv(Message("\n".join(message))) - elif not await self._observe(): + if test_message: # For test + seed = None + if isinstance(test_message, str): + seed = Message(test_message) + elif isinstance(test_message, Message): + seed = test_message + elif isinstance(test_message, list): + seed = Message("\n".join(test_message)) + self.async_put_message(seed) + + if not await self._observe(): # If there is no new information, suspend and wait logger.debug(f"{self._setting}: no news. waiting.") return rsp = await self._react() - # Publish the reply to the environment, waiting for the next subscriber to process - self._publish_message(rsp) + + # Reset the next action to be taken. + self._rc.todo = None + # Send the response message to the Environment object to have it relay the message to the subscribers. + self.publish_message(rsp) return rsp + + @property + def is_idle(self) -> bool: + """If true, all actions have been executed.""" + return not self._rc.news and not self._rc.todo and self._rc.msg_buffer.empty() diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index 0b6e089da..95be89277 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -4,18 +4,20 @@ @Time : 2023/5/23 17:25 @Author : alexanderwu @File : seacher.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ from metagpt.actions import ActionOutput, SearchAndSummarize from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.tools import SearchEngineType +from metagpt.utils.common import get_object_name class Searcher(Role): """ Represents a Searcher role responsible for providing search services to users. - + Attributes: name (str): Name of the searcher. profile (str): Role profile. @@ -23,17 +25,19 @@ class Searcher(Role): constraints (str): Constraints or limitations for the searcher. engine (SearchEngineType): The type of search engine to use. """ - - def __init__(self, - name: str = 'Alice', - profile: str = 'Smart Assistant', - goal: str = 'Provide search services for users', - constraints: str = 'Answer is rich and complete', - engine=SearchEngineType.SERPAPI_GOOGLE, - **kwargs) -> None: + + def __init__( + self, + name: str = "Alice", + profile: str = "Smart Assistant", + goal: str = "Provide search services for users", + constraints: str = "Answer is rich and complete", + engine=SearchEngineType.SERPAPI_GOOGLE, + **kwargs, + ) -> None: """ Initializes the Searcher role with given attributes. - + Args: name (str): Name of the searcher. profile (str): Role profile. @@ -53,12 +57,16 @@ class Searcher(Role): """Performs the search action in a single process.""" logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - + if isinstance(response, ActionOutput): - msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + msg = Message( + content=response.content, + instruct_content=response.instruct_content, + role=self.profile, + cause_by=get_object_name(self._rc.todo), + ) else: - msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo)) self._rc.memory.add(msg) return msg diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index b27841d74..abebb9605 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -4,6 +4,7 @@ @Time : 2023/9/13 12:23 @Author : femto Zheng @File : sk_agent.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner @@ -14,6 +15,7 @@ from metagpt.actions.execute_task import ExecuteTask from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message +from metagpt.utils.common import get_object_name from metagpt.utils.make_sk_kernel import make_sk_kernel @@ -70,7 +72,7 @@ class SkAgent(Role): result = (await self.plan.invoke_async()).result logger.info(result) - msg = Message(content=result, role=self.profile, cause_by=type(self._rc.todo)) + msg = Message(content=result, role=self.profile, cause_by=get_object_name(self._rc.todo)) self._rc.memory.add(msg) - # logger.debug(f"{response}") + self.publish_message(msg) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 1124fb28e..e0d17e0ed 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -8,12 +8,20 @@ """ from __future__ import annotations +import asyncio import json +from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError -from typing import Dict, List, TypedDict +from typing import Dict, List, Set, TypedDict from pydantic import BaseModel, Field +from metagpt.const import ( + MESSAGE_META_ROLE, + MESSAGE_ROUTE_CAUSE_BY, + MESSAGE_ROUTE_FROM, + MESSAGE_ROUTE_TO, +) from metagpt.logs import logger @@ -22,44 +30,150 @@ class RawMessage(TypedDict): role: str +class Routes(BaseModel): + """Responsible for managing routing information for the Message class.""" + + routes: List[Dict] = Field(default_factory=list) + + def set_from(self, value): + """Set the label of the message sender.""" + route = self._get_route() + route[MESSAGE_ROUTE_FROM] = value + + def set_to(self, tags: Set): + """Set the labels of the message recipient.""" + route = self._get_route() + if tags: + route[MESSAGE_ROUTE_TO] = tags + return + + if MESSAGE_ROUTE_TO in route: + del route[MESSAGE_ROUTE_TO] + + def add_to(self, tag: str): + """Add a label of the message recipient.""" + route = self._get_route() + tags = route.get(MESSAGE_ROUTE_TO, set()) + tags.add(tag) + route[MESSAGE_ROUTE_TO] = tags + + def _get_route(self) -> Dict: + if not self.routes: + self.routes.append({}) + return self.routes[0] + + def is_recipient(self, tags: Set) -> bool: + """Check if it is the message recipient.""" + route = self._get_route() + to_tags = route.get(MESSAGE_ROUTE_TO) + if not to_tags: + return True + + for k in tags: + if k in to_tags: + return True + return False + + @property + def tx_from(self): + """Message route info tells who sent this message.""" + route = self._get_route() + return route.get(MESSAGE_ROUTE_FROM) + + @property + def tx_to(self): + """Labels for the consumer to filter its subscribed messages.""" + route = self._get_route() + return route.get(MESSAGE_ROUTE_TO) + + class Message(BaseModel): """list[: ]""" content: str instruct_content: BaseModel = None meta_info: Dict = Field(default_factory=dict) - route: List[Dict] = Field(default_factory=list) + route: Routes = Field(default_factory=Routes) def __init__(self, content, **kwargs): + """ + :param content: Message content. + :param instruct_content: Message content struct. + :param meta_info: Message meta info. + :param route: Message route configuration. + :param tx_from: Message route info tells who sent this message. + :param tx_to: Labels for the consumer to filter its subscribed messages. + :param cause_by: Labels for the consumer to filter its subscribed messages, also serving as meta info. + :param role: Message meta info tells who sent this message. + """ super(Message, self).__init__( content=content or kwargs.get("content"), instruct_content=kwargs.get("instruct_content"), meta_info=kwargs.get("meta_info", {}), - route=kwargs.get("route", []), + route=kwargs.get("route", Routes()), ) attribute_names = Message.__annotations__.keys() for k, v in kwargs.items(): if k in attribute_names: continue + if k == MESSAGE_ROUTE_FROM: + self.set_from(v) + continue + if k == MESSAGE_ROUTE_CAUSE_BY: + self.meta_info[k] = v + if k == MESSAGE_ROUTE_TO or k == MESSAGE_ROUTE_CAUSE_BY: + self.add_to(v) + continue self.meta_info[k] = v def get_meta(self, key): + """Get meta info""" return self.meta_info.get(key) def set_meta(self, key, value): + """Set meta info""" self.meta_info[key] = value @property def role(self): - return self.get_meta("role") + """Message meta info tells who sent this message.""" + return self.get_meta(MESSAGE_META_ROLE) @property def cause_by(self): - return self.get_meta("cause_by") + """Labels for the consumer to filter its subscribed messages, also serving as meta info.""" + return self.get_meta(MESSAGE_ROUTE_CAUSE_BY) + + @property + def tx_from(self): + """Message route info tells who sent this message.""" + return self.route.tx_from + + @property + def tx_to(self): + """Labels for the consumer to filter its subscribed messages.""" + return self.route.tx_to def set_role(self, v): - self.set_meta("role", v) + """Set the message's meta info indicating the sender.""" + self.set_meta(MESSAGE_META_ROLE, v) + + def set_from(self, v): + """Set the message's meta info indicating the sender.""" + self.route.set_from(v) + + def set_to(self, tags: Set): + """Set the message's meta info indicating the sender.""" + self.route.set_to(tags) + + def add_to(self, tag: str): + """Add a subscription label for the recipients.""" + self.route.add_to(tag) + + def is_recipient(self, tags: Set): + """Return true if any input label exists in the message's subscription labels.""" + return self.route.is_recipient(tags) def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) @@ -69,13 +183,16 @@ class Message(BaseModel): return self.__str__() def to_dict(self) -> dict: + """Return a dict containing `role` and `content` for the LLM call.l""" return {"role": self.role, "content": self.content} def save(self) -> str: + """Convert the object to json string""" return self.json(exclude_none=True) @staticmethod def load(v): + """Convert the json string to object.""" try: d = json.loads(v) return Message(**d) @@ -90,7 +207,7 @@ class UserMessage(Message): """ def __init__(self, content: str): - super(Message, self).__init__(content=content, meta_info={"role": "user"}) + super().__init__(content=content, role="user") class SystemMessage(Message): @@ -99,7 +216,7 @@ class SystemMessage(Message): """ def __init__(self, content: str): - super().__init__(content=content, meta_info={"role": "system"}) + super().__init__(content=content, role="system") class AIMessage(Message): @@ -108,7 +225,65 @@ class AIMessage(Message): """ def __init__(self, content: str): - super().__init__(content=content, meta_info={"role": "assistant"}) + super().__init__(content=content, role="assistant") + + +class MessageQueue: + def __init__(self): + self._queue = Queue() + + def pop(self) -> Message | None: + try: + item = self._queue.get_nowait() + if item: + self._queue.task_done() + return item + except QueueEmpty: + return None + + def pop_all(self) -> List[Message]: + ret = [] + while True: + msg = self.pop() + if not msg: + break + ret.append(msg) + return ret + + def push(self, msg: Message): + self._queue.put_nowait(msg) + + def empty(self): + return self._queue.empty() + + async def save(self) -> str: + if self.empty(): + return "[]" + + lst = [] + try: + while True: + item = await wait_for(self._queue.get(), timeout=1.0) + if item is None: + break + lst.append(item.dict(exclude_none=True)) + self._queue.task_done() + except asyncio.TimeoutError: + logger.debug("Queue is empty, exiting...") + return json.dumps(lst) + + @staticmethod + def load(self, v) -> "MessageQueue": + q = MessageQueue() + try: + lst = json.loads(v) + for i in lst: + msg = Message(**i) + q.push(msg) + except JSONDecodeError as e: + logger.warning(f"JSON load failed: {v}, error:{e}") + + return q if __name__ == "__main__": diff --git a/metagpt/software_company.py b/metagpt/software_company.py index b2bd18c58..4bedec0e1 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -4,6 +4,9 @@ @Time : 2023/5/12 00:30 @Author : alexanderwu @File : software_company.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Standardize the design of message filtering-related features. + 2. Abandon the design of having `Environment` store all messages. """ from pydantic import BaseModel, Field @@ -14,13 +17,15 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException +from metagpt.utils.named import Named -class SoftwareCompany(BaseModel): +class SoftwareCompany(BaseModel, Named): """ Software Company: Possesses a team, SOP (Standard Operating Procedures), and a platform for instant messaging, dedicated to writing executable code. """ + environment: Environment = Field(default_factory=Environment) investment: float = Field(default=10.0) idea: str = Field(default="") @@ -36,16 +41,23 @@ class SoftwareCompany(BaseModel): """Invest company. raise NoMoneyException when exceed max_budget.""" self.investment = investment CONFIG.max_budget = investment - logger.info(f'Investment: ${investment}.') + logger.info(f"Investment: ${investment}.") def _check_balance(self): if CONFIG.total_cost > CONFIG.max_budget: - raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}') + raise NoMoneyException(CONFIG.total_cost, f"Insufficient funds: {CONFIG.max_budget}") def start_project(self, idea): """Start a project from publishing boss requirement.""" self.idea = idea - self.environment.publish_message(Message(role="BOSS", content=idea, cause_by=BossRequirement)) + self.environment.publish_message( + Message( + role="BOSS", + content=idea, + cause_by=BossRequirement.get_class_name(), + tx_from=SoftwareCompany.get_class_name(), + ) + ) def _save(self): logger.info(self.json()) @@ -58,5 +70,3 @@ class SoftwareCompany(BaseModel): logger.debug(f"{n_round=}") self._check_balance() await self.environment.run() - return self.environment.history - \ No newline at end of file diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index f09666beb..df4688378 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -85,10 +85,7 @@ class OutputParser: @staticmethod def parse_python_code(text: str) -> str: - for pattern in ( - r"(.*?```python.*?\s+)?(?P.*)(```.*?)", - r"(.*?```python.*?\s+)?(?P.*)", - ): + for pattern in (r"(.*?```python.*?\s+)?(?P.*)(```.*?)", r"(.*?```python.*?\s+)?(?P.*)"): match = re.search(pattern, text, re.DOTALL) if not match: continue @@ -305,3 +302,14 @@ def parse_recipient(text): pattern = r"## Send To:\s*([A-Za-z]+)\s*?" # hard code for now recipient = re.search(pattern, text) return recipient.group(1) if recipient else "" + + +def get_class_name(cls) -> str: + """Return class name""" + return f"{cls.__module__}.{cls.__name__}" + + +def get_object_name(obj) -> str: + """Return class name of the object""" + cls = type(obj) + return f"{cls.__module__}.{cls.__name__}" diff --git a/metagpt/utils/named.py b/metagpt/utils/named.py new file mode 100644 index 000000000..e4da574e8 --- /dev/null +++ b/metagpt/utils/named.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/1 +@Author : mashenquan +@File : named.py +""" + + +class Named: + """A base class with functions for converting classes to names and objects to class names.""" + + @classmethod + def get_class_name(cls): + """Return class name""" + return f"{cls.__module__}.{cls.__name__}" + + def get_object_name(self): + """Return class name of the object""" + cls = type(self) + return f"{cls.__module__}.{cls.__name__}" diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 38e4e5221..40ab20dad 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -4,6 +4,7 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : test_write_prd.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ import pytest @@ -17,7 +18,7 @@ from metagpt.schema import Message async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" - prd = await product_manager.handle(Message(content=requirements, cause_by=BossRequirement)) + prd = await product_manager.handle(Message(content=requirements, cause_by=BossRequirement.get_class_name())) logger.info(requirements) logger.info(prd) diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index dc5540520..c40d7ab9d 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -1,12 +1,15 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# @Desc : unittest of `metagpt/memory/longterm_memory.py` +""" +@Desc : unittest of `metagpt/memory/longterm_memory.py` +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" -from metagpt.config import CONFIG -from metagpt.schema import Message from metagpt.actions import BossRequirement -from metagpt.roles.role import RoleContext +from metagpt.config import CONFIG from metagpt.memory import LongTermMemory +from metagpt.roles.role import RoleContext +from metagpt.schema import Message def test_ltm_search(): @@ -14,25 +17,25 @@ def test_ltm_search(): openai_api_key = CONFIG.openai_api_key assert len(openai_api_key) > 20 - role_id = 'UTUserLtm(Product Manager)' - rc = RoleContext(watch=[BossRequirement]) + role_id = "UTUserLtm(Product Manager)" + rc = RoleContext(watch=[BossRequirement.get_class_name()]) ltm = LongTermMemory() ltm.recover_memory(role_id, rc) - idea = 'Write a cli snake game' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + idea = "Write a cli snake game" + message = Message(role="BOSS", content=idea, cause_by=BossRequirement.get_class_name()) news = ltm.find_news([message]) assert len(news) == 1 ltm.add(message) - sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_idea = "Write a game of cli snake" + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement.get_class_name()) news = ltm.find_news([sim_message]) assert len(news) == 0 ltm.add(sim_message) - new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a 2048 web game" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) news = ltm.find_news([new_message]) assert len(news) == 1 ltm.add(new_message) @@ -47,8 +50,8 @@ def test_ltm_search(): news = ltm_new.find_news([sim_message]) assert len(news) == 0 - new_idea = 'Write a Battle City' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a Battle City" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) news = ltm_new.find_news([new_message]) assert len(news) == 1 diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index 6bb3e8f1d..881b47d6f 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -1,20 +1,23 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# @Desc : the unittests of metagpt/memory/memory_storage.py +""" +@Desc : the unittests of metagpt/memory/memory_storage.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" + from typing import List +from metagpt.actions import BossRequirement, WritePRD +from metagpt.actions.action_output import ActionOutput from metagpt.memory.memory_storage import MemoryStorage from metagpt.schema import Message -from metagpt.actions import BossRequirement -from metagpt.actions import WritePRD -from metagpt.actions.action_output import ActionOutput def test_idea_message(): - idea = 'Write a cli snake game' - role_id = 'UTUser1(Product Manager)' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + idea = "Write a cli snake game" + role_id = "UTUser1(Product Manager)" + message = Message(role="BOSS", content=idea, cause_by=BossRequirement.get_class_name()) memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -23,13 +26,13 @@ def test_idea_message(): memory_storage.add(message) assert memory_storage.is_initialized is True - sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_idea = "Write a game of cli snake" + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement.get_class_name()) new_messages = memory_storage.search(sim_message) - assert len(new_messages) == 0 # similar, return [] + assert len(new_messages) == 0 # similar, return [] - new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a 2048 web game" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content @@ -38,22 +41,15 @@ def test_idea_message(): def test_actionout_message(): - out_mapping = { - 'field1': (str, ...), - 'field2': (List[str], ...) - } - out_data = { - 'field1': 'field1 value', - 'field2': ['field2 value1', 'field2 value2'] - } - ic_obj = ActionOutput.create_model_class('prd', out_mapping) + out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} + out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} + ic_obj = ActionOutput.create_model_class("prd", out_mapping) - role_id = 'UTUser2(Architect)' - content = 'The boss has requested the creation of a command-line interface (CLI) snake game' - message = Message(content=content, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) # WritePRD as test action + role_id = "UTUser2(Architect)" + content = "The boss has requested the creation of a command-line interface (CLI) snake game" + message = Message( + content=content, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() + ) # WritePRD as test action memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -62,19 +58,17 @@ def test_actionout_message(): memory_storage.add(message) assert memory_storage.is_initialized is True - sim_conent = 'The request is command-line interface (CLI) snake game' - sim_message = Message(content=sim_conent, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) + sim_conent = "The request is command-line interface (CLI) snake game" + sim_message = Message( + content=sim_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() + ) new_messages = memory_storage.search(sim_message) - assert len(new_messages) == 0 # similar, return [] + assert len(new_messages) == 0 # similar, return [] - new_conent = 'Incorporate basic features of a snake game such as scoring and increasing difficulty' - new_message = Message(content=new_conent, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) + new_conent = "Incorporate basic features of a snake game such as scoring and increasing difficulty" + new_message = Message( + content=new_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() + ) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index 5ab9a493f..a3831c08d 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -4,6 +4,9 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Standardize the usage of message filtering-related features. + 2. Standardize the usage of message transmission. """ import pytest from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill @@ -23,7 +26,7 @@ async def test_action_planner(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - role.recv(Message(content=task, cause_by=BossRequirement)) - + role.async_put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + await role._observe() await role._think() # it will choose mathskill.Add assert "1100" == (await role._act()).content diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index 03a82ec5e..9efcb9367 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -4,6 +4,9 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py +@Modified By: mashenquan, 2023-11-1. Optimization: + 1. Standardize the usage of message filtering-related features. + 2. Standardize the usage of message transmission. """ import pytest from semantic_kernel.core_skills import TextSkill @@ -26,7 +29,8 @@ async def test_basic_planner(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - role.recv(Message(content=task, cause_by=BossRequirement)) + role.async_put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + await role._observe() await role._think() # assuming sk_agent will think he needs WriterSkill.Brainstorm and WriterSkill.Translate assert "WriterSkill.Brainstorm" in role.plan.generated_plan.result diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 52fc4a3c1..b9891cd81 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -4,6 +4,7 @@ @Time : 2023/5/12 13:05 @Author : alexanderwu @File : mock.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. """ from metagpt.actions import BossRequirement, WriteDesign, WritePRD, WriteTasks from metagpt.schema import Message @@ -71,7 +72,7 @@ PRD = '''## 原始需求 ``` ''' -SYSTEM_DESIGN = '''## Python package name +SYSTEM_DESIGN = """## Python package name ```python "smart_search_engine" ``` @@ -149,10 +150,10 @@ sequenceDiagram S-->>SE: return summary SE-->>M: return summary ``` -''' +""" -TASKS = '''## Logic Analysis +TASKS = """## Logic Analysis 在这个项目中,所有的模块都依赖于“SearchEngine”类,这是主入口,其他的模块(Index、Ranking和Summary)都通过它交互。另外,"Index"类又依赖于"KnowledgeBase"类,因为它需要从知识库中获取数据。 @@ -181,7 +182,7 @@ task_list = [ ] ``` 这个任务列表首先定义了最基础的模块,然后是依赖这些模块的模块,最后是辅助模块。可以根据团队的能力和资源,同时开发多个任务,只要满足依赖关系。例如,在开发"search.py"之前,可以同时开发"knowledge_base.py"、"index.py"、"ranking.py"和"summary.py"。 -''' +""" TASKS_TOMATO_CLOCK = '''## Required Python third-party packages: Provided in requirements.txt format @@ -224,35 +225,35 @@ task_list = [ TASK = """smart_search_engine/knowledge_base.py""" STRS_FOR_PARSING = [ -""" + """ ## 1 ```python a ``` """, -""" + """ ##2 ```python "a" ``` """, -""" + """ ## 3 ```python a = "a" ``` """, -""" + """ ## 4 ```python a = 'a' ``` -""" +""", ] class MockMessages: - req = Message(role="Boss", content=BOSS_REQUIREMENT, cause_by=BossRequirement) - prd = Message(role="Product Manager", content=PRD, cause_by=WritePRD) - system_design = Message(role="Architect", content=SYSTEM_DESIGN, cause_by=WriteDesign) - tasks = Message(role="Project Manager", content=TASKS, cause_by=WriteTasks) + req = Message(role="Boss", content=BOSS_REQUIREMENT, cause_by=BossRequirement.get_class_name()) + prd = Message(role="Product Manager", content=PRD, cause_by=WritePRD.get_class_name()) + system_design = Message(role="Architect", content=SYSTEM_DESIGN, cause_by=WriteDesign.get_class_name()) + tasks = Message(role="Project Manager", content=TASKS, cause_by=WriteTasks.get_class_name()) diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.py index d44e0d923..910c589ca 100644 --- a/tests/metagpt/roles/test_architect.py +++ b/tests/metagpt/roles/test_architect.py @@ -4,6 +4,7 @@ @Time : 2023/5/20 14:37 @Author : alexanderwu @File : test_architect.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. """ import pytest @@ -15,7 +16,7 @@ from tests.metagpt.roles.mock import MockMessages @pytest.mark.asyncio async def test_architect(): role = Architect() - role.recv(MockMessages.req) - rsp = await role.handle(MockMessages.prd) + role.async_put_message(MockMessages.req) + rsp = await role.run(MockMessages.prd) logger.info(rsp) assert len(rsp.content) > 0 diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index c0c48d0b1..e80234b3b 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -4,6 +4,7 @@ @Time : 2023/5/12 10:14 @Author : alexanderwu @File : test_engineer.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. """ import pytest @@ -22,10 +23,10 @@ from tests.metagpt.roles.mock import ( async def test_engineer(): engineer = Engineer() - engineer.recv(MockMessages.req) - engineer.recv(MockMessages.prd) - engineer.recv(MockMessages.system_design) - rsp = await engineer.handle(MockMessages.tasks) + engineer.async_put_message(MockMessages.req) + engineer.async_put_message(MockMessages.prd) + engineer.async_put_message(MockMessages.system_design) + rsp = await engineer.run(MockMessages.tasks) logger.info(rsp) assert "all done." == rsp.content @@ -35,13 +36,13 @@ def test_parse_str(): for idx, i in enumerate(STRS_FOR_PARSING): text = CodeParser.parse_str(f"{idx+1}", i) # logger.info(text) - assert text == 'a' + assert text == "a" def test_parse_blocks(): tasks = CodeParser.parse_blocks(TASKS) logger.info(tasks.keys()) - assert 'Task list' in tasks.keys() + assert "Task list" in tasks.keys() target_list = [ diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index a0f1f6257..755798b17 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -4,6 +4,7 @@ @Time : 2023/5/12 00:47 @Author : alexanderwu @File : test_environment.py +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. """ import pytest @@ -49,7 +50,7 @@ async def test_publish_and_process_message(env: Environment): env.add_roles([product_manager, architect]) env.set_manager(Manager()) - env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement)) + env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement.get_class_name())) await env.run(k=2) logger.info(f"{env.history=}") diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index 69f317f79..5a0840c87 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -1,6 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# @Desc : the unittest of serialize +""" +@Desc : the unittest of serialize +@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +""" from typing import List, Tuple @@ -55,7 +58,7 @@ def test_serialize_and_deserialize_message(): ic_obj = ActionOutput.create_model_class("prd", out_mapping) message = Message( - content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD + content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() ) # WritePRD as test action message_ser = serialize_message(message) From bd813d2b90d16d2c439c4693ab27dca595786b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 1 Nov 2023 20:17:23 +0800 Subject: [PATCH 007/413] refactor: Refactor Message transmission & filtering --- metagpt/roles/researcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 43ee7971d..6e89b9fe7 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -77,7 +77,7 @@ class Researcher(Role): summary_text = "\n---\n".join(f"url: {url}\nsummary: {summary}" for (url, summary) in summaries) content = await self._rc.todo.run(topic, summary_text, system_text=research_system_text) ret = Message( - "", Report(topic=topic, content=content), role=self.profile, get_object_name=type(self._rc.todo) + "", Report(topic=topic, content=content), role=self.profile, cause_by=get_object_name(self._rc.todo) ) self._rc.memory.add(ret) return ret From 8582f219624d75440d33e32648ebf1b44c389011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 1 Nov 2023 20:33:34 +0800 Subject: [PATCH 008/413] refactor: Refactor Message transmission & filtering --- metagpt/schema.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/metagpt/schema.py b/metagpt/schema.py index e0d17e0ed..806b0e94e 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -229,10 +229,13 @@ class AIMessage(Message): class MessageQueue: + """Message queue which supports asynchronous updates.""" + def __init__(self): self._queue = Queue() def pop(self) -> Message | None: + """Pop one message from queue.""" try: item = self._queue.get_nowait() if item: @@ -242,6 +245,7 @@ class MessageQueue: return None def pop_all(self) -> List[Message]: + """Pop all messages from queue.""" ret = [] while True: msg = self.pop() @@ -251,12 +255,15 @@ class MessageQueue: return ret def push(self, msg: Message): + """Push a message into the queue.""" self._queue.put_nowait(msg) def empty(self): + """Return true if the queue is empty.""" return self._queue.empty() async def save(self) -> str: + """Convert the `MessageQueue` object to a json string.""" if self.empty(): return "[]" @@ -274,6 +281,7 @@ class MessageQueue: @staticmethod def load(self, v) -> "MessageQueue": + """Convert the json string to the `MessageQueue` object.""" q = MessageQueue() try: lst = json.loads(v) From d127586320d7fc1ecc81fae198dc2908cad34815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 1 Nov 2023 20:35:37 +0800 Subject: [PATCH 009/413] refactor: Refactor Message transmission & filtering --- metagpt/schema.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index 806b0e94e..1adfd525c 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -235,7 +235,7 @@ class MessageQueue: self._queue = Queue() def pop(self) -> Message | None: - """Pop one message from queue.""" + """Pop one message from the queue.""" try: item = self._queue.get_nowait() if item: @@ -245,7 +245,7 @@ class MessageQueue: return None def pop_all(self) -> List[Message]: - """Pop all messages from queue.""" + """Pop all messages from the queue.""" ret = [] while True: msg = self.pop() From d685252aa0f8f1f393697b76c9e236ecc9c5117a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 10:21:15 +0800 Subject: [PATCH 010/413] feat: +unit test --- tests/metagpt/utils/test_named.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/metagpt/utils/test_named.py diff --git a/tests/metagpt/utils/test_named.py b/tests/metagpt/utils/test_named.py new file mode 100644 index 000000000..89a68b5e7 --- /dev/null +++ b/tests/metagpt/utils/test_named.py @@ -0,0 +1,21 @@ +import pytest + +from metagpt.utils.named import Named + + +@pytest.mark.asyncio +async def test_suite(): + class A(Named): + pass + + class B(A): + pass + + assert A.get_class_name() == "tests.metagpt.utils.test_named.A" + assert A().get_object_name() == "tests.metagpt.utils.test_named.A" + assert B.get_class_name() == "tests.metagpt.utils.test_named.B" + assert B().get_object_name() == "tests.metagpt.utils.test_named.B" + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) From bfaeda0a90c65487b758c4c1011802e32e8f848f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 10:28:54 +0800 Subject: [PATCH 011/413] feat: +unit tests --- tests/metagpt/test_schema.py | 32 ++++++++++++++++++++++++++++--- tests/metagpt/utils/test_named.py | 7 +++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 12666e0d3..71bb39c77 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -4,18 +4,44 @@ @Time : 2023/5/20 10:40 @Author : alexanderwu @File : test_schema.py +@Modified By: mashenquan, 2023-11-1. Add `test_message`. """ +import json + +import pytest + from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage +@pytest.mark.asyncio def test_messages(): - test_content = 'test_message' + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] text = str(msgs) - roles = ['user', 'system', 'assistant', 'QA'] + roles = ["user", "system", "assistant", "QA"] assert all([i in text for i in roles]) + + +@pytest.mark.asyncio +def test_message(): + m = Message("a", role="v1") + v = m.save() + d = json.loads(v) + assert d + assert d.get("content") == "a" + assert d.get("meta_info") == {"role": "v1"} + m.set_role("v2") + v = m.save() + assert v + m = Message.load(v) + assert m.content == "a" + assert m.role == "v2" + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/utils/test_named.py b/tests/metagpt/utils/test_named.py index 89a68b5e7..ff1f07205 100644 --- a/tests/metagpt/utils/test_named.py +++ b/tests/metagpt/utils/test_named.py @@ -1,3 +1,10 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023-11-1 +@Author : mashenquan +@File : test_named.py +""" import pytest from metagpt.utils.named import Named From bc1a757293b0967c3d98ad01edcf531d5c88aef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 10:40:26 +0800 Subject: [PATCH 012/413] refactor: rename async_put_message to put_message --- metagpt/environment.py | 2 +- metagpt/roles/role.py | 8 ++++---- tests/metagpt/planner/test_action_planner.py | 2 +- tests/metagpt/planner/test_basic_planner.py | 2 +- tests/metagpt/roles/test_architect.py | 2 +- tests/metagpt/roles/test_engineer.py | 6 +++--- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index ba0645a36..7ba077080 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -50,7 +50,7 @@ class Environment(BaseModel): found = False for r in self.roles.values(): if message.is_recipient(r.subscribed_tags): - r.async_put_message(message) + r.put_message(message) found = True if not found: logger.warning(f"Message no recipients: {message.save()}") diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 0a6716428..6fba40bd8 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -11,10 +11,10 @@ they've subscribed to through the `subscribed_tags` property. 3. Move the message receive buffer from the global variable `self._rc.env.memory` to the role's private variable `self._rc.msg_buffer` for easier message identification and asynchronous appending of messages. - 4. Standardize the way messages are passed: `publish_message` sends messages out, while `async_put_message` places + 4. Standardize the way messages are passed: `publish_message` sends messages out, while `put_message` places messages into the Role object's private message receive buffer. There are no other message transmit methods. 5. Standardize the parameters for the `run` function: the `test_message` parameter is used for testing purposes - only. In the normal workflow, you should use `publish_message` or `async_put_message` to transmit messages. + only. In the normal workflow, you should use `publish_message` or `put_message` to transmit messages. """ from __future__ import annotations @@ -239,7 +239,7 @@ class Role(Named): return self._rc.env.publish_message(msg) - def async_put_message(self, message): + def put_message(self, message): """Place the message into the Role object's private message buffer.""" if not message: return @@ -261,7 +261,7 @@ class Role(Named): seed = test_message elif isinstance(test_message, list): seed = Message("\n".join(test_message)) - self.async_put_message(seed) + self.put_message(seed) if not await self._observe(): # If there is no new information, suspend and wait diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index a3831c08d..99cc25b72 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -26,7 +26,7 @@ async def test_action_planner(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - role.async_put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + role.put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) await role._observe() await role._think() # it will choose mathskill.Add assert "1100" == (await role._act()).content diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index 9efcb9367..fa7ed7074 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -29,7 +29,7 @@ async def test_basic_planner(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - role.async_put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + role.put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) await role._observe() await role._think() # assuming sk_agent will think he needs WriterSkill.Brainstorm and WriterSkill.Translate diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.py index 910c589ca..665242379 100644 --- a/tests/metagpt/roles/test_architect.py +++ b/tests/metagpt/roles/test_architect.py @@ -16,7 +16,7 @@ from tests.metagpt.roles.mock import MockMessages @pytest.mark.asyncio async def test_architect(): role = Architect() - role.async_put_message(MockMessages.req) + role.put_message(MockMessages.req) rsp = await role.run(MockMessages.prd) logger.info(rsp) assert len(rsp.content) > 0 diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index e80234b3b..93c3132ac 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -23,9 +23,9 @@ from tests.metagpt.roles.mock import ( async def test_engineer(): engineer = Engineer() - engineer.async_put_message(MockMessages.req) - engineer.async_put_message(MockMessages.prd) - engineer.async_put_message(MockMessages.system_design) + engineer.put_message(MockMessages.req) + engineer.put_message(MockMessages.prd) + engineer.put_message(MockMessages.system_design) rsp = await engineer.run(MockMessages.tasks) logger.info(rsp) From 8572fa8ecd6f409f339b07db053749d2f6361c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 10:48:45 +0800 Subject: [PATCH 013/413] feat: +unit tests --- tests/metagpt/test_schema.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 71bb39c77..06bb57a70 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -10,7 +10,7 @@ import json import pytest -from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage +from metagpt.schema import AIMessage, Message, Routes, SystemMessage, UserMessage @pytest.mark.asyncio @@ -42,6 +42,29 @@ def test_message(): assert m.content == "a" assert m.role == "v2" + m = Message("a", role="b", cause_by="c", x="d") + assert m.content == "a" + assert m.role == "b" + assert m.is_recipient({"c"}) + assert m.cause_by == "c" + assert m.get_meta("x") == "d" + + +@pytest.mark.asyncio +def test_routes(): + route = Routes() + route.set_from("a") + assert route.tx_from == "a" + route.add_to("b") + assert route.tx_to == {"b"} + route.add_to("c") + assert route.tx_to == {"b", "c"} + route.set_to({"e", "f"}) + assert route.tx_to == {"e", "f"} + assert route.is_recipient({"e"}) + assert route.is_recipient({"f"}) + assert not route.is_recipient({"a"}) + if __name__ == "__main__": pytest.main([__file__, "-s"]) From 660f788683b2ace2ef8ac020f044be949fd19ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 11:51:10 +0800 Subject: [PATCH 014/413] feat: + subscribe --- metagpt/roles/role.py | 4 ++ tests/metagpt/roles/test_role.py | 64 ++++++++++++++++++++++++++++++++ tests/metagpt/test_role.py | 14 ------- 3 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 tests/metagpt/roles/test_role.py delete mode 100644 tests/metagpt/test_role.py diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 6fba40bd8..318b7d7a8 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -134,6 +134,10 @@ class Role(Named): def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" tags = [get_class_name(t) for t in actions] + self.subscribe(tags) + + def subscribe(self, tags: Set[str]): + """Listen to the corresponding behaviors""" self._rc.watch.update(tags) # check RoleContext after adding watch actions self._rc.check(self._role_id) diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py new file mode 100644 index 000000000..cefd71ada --- /dev/null +++ b/tests/metagpt/roles/test_role.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023-11-1 +@Author : mashenquan +@File : test_role.py +""" +import pytest +from pydantic import BaseModel + +from metagpt.actions import Action, ActionOutput +from metagpt.environment import Environment +from metagpt.roles import Role +from metagpt.schema import Message + + +class MockAction(Action): + async def run(self, messages, *args, **kwargs): + assert messages + return ActionOutput(content=messages[-1].content, instruct_content=messages[-1]) + + +class MockRole(Role): + def __init__(self, name="", profile="", goal="", constraints="", desc=""): + super().__init__(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) + self._init_actions([MockAction()]) + + +@pytest.mark.asyncio +async def test_react(): + class Input(BaseModel): + name: str + profile: str + goal: str + constraints: str + desc: str + subscription: str + + inputs = [ + { + "name": "A", + "profile": "Tester", + "goal": "Test", + "constraints": "constraints", + "desc": "desc", + "subscription": "start", + } + ] + + for i in inputs: + seed = Input(**i) + role = MockRole( + name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc + ) + role.subscribe({seed.subscription}) + env = Environment() + env.add_role(role) + env.publish_message(Message(content="test", cause_by=seed.subscription)) + while not env.is_idle: + await env.run() + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py deleted file mode 100644 index 11fd804ec..000000000 --- a/tests/metagpt/test_role.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/11 14:44 -@Author : alexanderwu -@File : test_role.py -""" -from metagpt.roles import Role - - -def test_role_desc(): - i = Role(profile='Sales', desc='Best Seller') - assert i.profile == 'Sales' - assert i._setting.desc == 'Best Seller' From d5d520f6a1fac0fd512911042156215502a5d2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 11:54:14 +0800 Subject: [PATCH 015/413] feat: + subscribe --- tests/metagpt/roles/test_role.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py index cefd71ada..a11e69a23 100644 --- a/tests/metagpt/roles/test_role.py +++ b/tests/metagpt/roles/test_role.py @@ -53,11 +53,17 @@ async def test_react(): name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc ) role.subscribe({seed.subscription}) + assert role._rc.watch == {seed.subscription} + assert role.name == seed.name + assert role.profile == seed.profile + assert role.is_idle env = Environment() env.add_role(role) env.publish_message(Message(content="test", cause_by=seed.subscription)) + assert not role.is_idle while not env.is_idle: await env.run() + assert role.is_idle if __name__ == "__main__": From 526751073b2a48659a9959a1e365213005d2856b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 11:58:49 +0800 Subject: [PATCH 016/413] feat: + subscribe --- tests/metagpt/{roles => }/test_role.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/metagpt/{roles => }/test_role.py (100%) diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/test_role.py similarity index 100% rename from tests/metagpt/roles/test_role.py rename to tests/metagpt/test_role.py From 834c59df19bc4aa31065268a309d913f809f0ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 12:00:45 +0800 Subject: [PATCH 017/413] feat: + subscribe --- tests/metagpt/test_role.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index a11e69a23..1b92c88cd 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -56,6 +56,9 @@ async def test_react(): assert role._rc.watch == {seed.subscription} assert role.name == seed.name assert role.profile == seed.profile + assert role._setting.goal == seed.goal + assert role._setting.constraints == seed.constraints + assert role._setting.desc == seed.desc assert role.is_idle env = Environment() env.add_role(role) From bc67109fae1195debaf747beaa52b7ba452d02b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 12:02:05 +0800 Subject: [PATCH 018/413] feat: + subscribe --- tests/metagpt/test_role.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 1b92c88cd..98646041d 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -1,9 +1,10 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- """ -@Time : 2023-11-1 -@Author : mashenquan +@Time : 2023/5/11 14:44 +@Author : alexanderwu @File : test_role.py +@Modified By: mashenquan, 2023/11/1. Add unit tests. """ import pytest from pydantic import BaseModel From 2e9a265b916fe4d1bae390195e0ccca1067c16fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 2 Nov 2023 16:27:41 +0800 Subject: [PATCH 019/413] feat: + subscribe --- metagpt/schema.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/schema.py b/metagpt/schema.py index 1adfd525c..7c84dd4bb 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -97,6 +97,7 @@ class Message(BaseModel): def __init__(self, content, **kwargs): """ + Parameters not listed below will be stored as meta info. :param content: Message content. :param instruct_content: Message content struct. :param meta_info: Message meta info. From a7632e85481550bfab4531248fa530524d9b5263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:04:25 +0800 Subject: [PATCH 020/413] refactor: update notations --- examples/agent_creator.py | 3 ++- examples/build_customized_agent.py | 3 ++- examples/debate.py | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index d13cbcff2..5a1398456 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -2,7 +2,8 @@ Filename: MetaGPT/examples/agent_creator.py Created Date: Tuesday, September 12th 2023, 3:28:37 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import re diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index a953dee15..af15c90ca 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -2,7 +2,8 @@ Filename: MetaGPT/examples/build_customized_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import asyncio import re diff --git a/examples/debate.py b/examples/debate.py index ade1a6fc4..475d2da55 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -2,7 +2,9 @@ Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the 'cause_by' value in the 'Message' to a string, and utilize the new message distribution + feature for message filtering. """ import asyncio import platform From 93eda7f4a364fe838f1eb0839209e4aa5a49c671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:05:57 +0800 Subject: [PATCH 021/413] refactor: update notations --- examples/debate.py | 2 +- examples/sk_agent.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 475d2da55..1f5e58839 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -3,7 +3,7 @@ Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the 'cause_by' value in the 'Message' to a string, and utilize the new message distribution + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution feature for message filtering. """ import asyncio diff --git a/examples/sk_agent.py b/examples/sk_agent.py index 19ee53669..900696762 100644 --- a/examples/sk_agent.py +++ b/examples/sk_agent.py @@ -4,7 +4,8 @@ @Time : 2023/9/13 12:36 @Author : femto Zheng @File : sk_agent.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import asyncio From 96f29dadb875ba4fd5e1be06557eb3161cbb6821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:12:59 +0800 Subject: [PATCH 022/413] refactor: update notations --- metagpt/actions/action.py | 1 + metagpt/actions/write_code.py | 2 ++ metagpt/const.py | 2 ++ 3 files changed, 5 insertions(+) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1954e750a..c6f1f1534 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -4,6 +4,7 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : action.py +@Modified By: mashenquan, 2023-11-1. Add generic class-to-string and object-to-string conversion functionality. """ import re from abc import ABC diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 421211d60..f0ef2b6d6 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -4,6 +4,8 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : write_code.py +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from tenacity import retry, stop_after_attempt, wait_fixed diff --git a/metagpt/const.py b/metagpt/const.py index 3fbc26784..e783ec8d0 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -4,6 +4,8 @@ @Time : 2023/5/1 11:59 @Author : alexanderwu @File : const.py +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, added key definitions for + common properties in the Message. """ from pathlib import Path From e49f8a010e7ea9797ae25c6d1b61c33f26373a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:15:01 +0800 Subject: [PATCH 023/413] refactor: update notations --- metagpt/environment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index 7ba077080..028e98e8e 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -4,7 +4,7 @@ @Time : 2023/5/11 22:12 @Author : alexanderwu @File : environment.py -@Modified By: mashenquan, 2023-11-1. Optimization: +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.2 of RFC 116: 1. Remove the functionality of `Environment` class as a public message buffer. 2. Standardize the message forwarding behavior of the `Environment` class. 3. Add the `is_idle` property. From 67f07b66cda2598d4e0887e95cd8d6099a6d6336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:26:33 +0800 Subject: [PATCH 024/413] refactor: update notations --- metagpt/memory/longterm_memory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index b5bb73b6b..e73ae334e 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """ @Desc : the implement of Long-term memory -@Modified By: mashenquan, 2023-11-1. Optimization: +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: 1. Replace code related to message filtering with the `Message.is_recipient` function. """ From ddd2d40ff3c5bed217918e64d346ce0ce7fa5f77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:29:37 +0800 Subject: [PATCH 025/413] refactor: update notations --- metagpt/memory/memory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 8e01544f1..282e89b17 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -4,7 +4,8 @@ @Time : 2023/5/20 12:15 @Author : alexanderwu @File : memory.py -@Modified By: mashenquan, 2023-11-1. Standardize the design of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: + Modify the new message distribution feature for message filtering. """ from collections import defaultdict from typing import Iterable, Set From a996440d5e7bbfd6af24ed026a3bc332f6856e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:36:27 +0800 Subject: [PATCH 026/413] refactor: update notations --- metagpt/memory/memory.py | 2 +- metagpt/roles/engineer.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 282e89b17..7f04be63d 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -5,7 +5,7 @@ @Author : alexanderwu @File : memory.py @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - Modify the new message distribution feature for message filtering. + Updated the message filtering logic. """ from collections import defaultdict from typing import Iterable, Set diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 9826ea0b7..ff71a61d8 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -4,10 +4,12 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : engineer.py -@Modified By: mashenquan, 2023-11-1. Optimization: - 1. Consolidate message reception and processing logic within `_observe`. - 2. Fix bug: Add logic for handling asynchronous message processing when messages are not ready. - 3. Supplemented the external transmission of internal messages. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116: + 1. Modify the data type of the `cause_by` value in the `Message` to a string, and utilize the new message + distribution feature for message filtering. + 2. Consolidate message reception and processing logic within `_observe`. + 3. Fix bug: Add logic for handling asynchronous message processing when messages are not ready. + 4. Supplemented the external transmission of internal messages. """ import asyncio import shutil From 6bd9a76997b9be323c539d0a6c34ac4658df49b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:43:40 +0800 Subject: [PATCH 027/413] refactor: update notations --- metagpt/roles/qa_engineer.py | 3 ++- metagpt/roles/researcher.py | 3 ++- metagpt/roles/role.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index b83ab6e21..5cc35a878 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -4,7 +4,8 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : qa_engineer.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message filtering feature. """ import os from pathlib import Path diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 6e89b9fe7..4ec6f31e1 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -1,6 +1,7 @@ #!/usr/bin/env python """ -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 318b7d7a8..79a9fb2de 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -4,7 +4,7 @@ @Time : 2023/5/11 14:42 @Author : alexanderwu @File : role.py -@Modified By: mashenquan, 2023-11-1. Optimization: +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: 1. Merge the `recv` functionality into the `_observe` function. Future message reading operations will be consolidated within the `_observe` function. 2. Standardize the message filtering for string label matching. Role objects can access the message labels From 17c5f80d809085cd324261b9661fc06089940780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:48:47 +0800 Subject: [PATCH 028/413] refactor: update notations --- metagpt/roles/seacher.py | 3 ++- metagpt/roles/sk_agent.py | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index 95be89277..d0b841f39 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -4,7 +4,8 @@ @Time : 2023/5/23 17:25 @Author : alexanderwu @File : seacher.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import ActionOutput, SearchAndSummarize from metagpt.logs import logger diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index abebb9605..5b8d333bd 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -4,7 +4,9 @@ @Time : 2023/9/13 12:23 @Author : femto Zheng @File : sk_agent.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution + feature for message filtering. """ from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner From 953a003e1e57b2dbf741b53e0a7cdee344bae593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:53:47 +0800 Subject: [PATCH 029/413] refactor: update notations --- metagpt/schema.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index 7c84dd4bb..34e6fa07b 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -4,7 +4,8 @@ @Time : 2023/5/8 22:12 @Author : alexanderwu @File : schema.py -@Modified By: mashenquan, 2023-10-31, optimize class members. +@Modified By: mashenquan, 2023-10-31. According to Chapter 2.2.1 of RFC 116: + Replanned the distribution of responsibilities and functional positioning of `Message` class attributes. """ from __future__ import annotations @@ -97,7 +98,7 @@ class Message(BaseModel): def __init__(self, content, **kwargs): """ - Parameters not listed below will be stored as meta info. + Parameters not listed below will be stored as meta info, including custom parameters. :param content: Message content. :param instruct_content: Message content struct. :param meta_info: Message meta info. From b1386a01f5ce016268902f4fc82845079f8d089b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 11:57:10 +0800 Subject: [PATCH 030/413] refactor: update notations --- metagpt/software_company.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 4bedec0e1..d29d8926d 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -4,9 +4,10 @@ @Time : 2023/5/12 00:30 @Author : alexanderwu @File : software_company.py -@Modified By: mashenquan, 2023-11-1. Optimization: - 1. Standardize the design of message filtering-related features. - 2. Abandon the design of having `Environment` store all messages. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: + 1. Change the data type of the `cause_by` value in the `Message` to a string to support the new message + distribution feature. + 2. Abandon the design of having `Environment` store all messages. """ from pydantic import BaseModel, Field From 290479969b0c0386b8004cd46d78b22f603aa805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 12:00:18 +0800 Subject: [PATCH 031/413] refactor: update notations --- metagpt/utils/common.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index df4688378..219ed9f04 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -4,6 +4,8 @@ @Time : 2023/4/29 16:07 @Author : alexanderwu @File : common.py +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.2 of RFC 116: + Add generic class-to-string and object-to-string conversion functionality. """ import ast import contextlib From bdf59b67bd4bfe5b381b9c61cf59086af01127c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 12:02:52 +0800 Subject: [PATCH 032/413] refactor: update notations --- tests/metagpt/actions/test_write_prd.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 40ab20dad..0da7831c6 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -4,7 +4,8 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : test_write_prd.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import pytest @@ -18,7 +19,7 @@ from metagpt.schema import Message async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" - prd = await product_manager.handle(Message(content=requirements, cause_by=BossRequirement.get_class_name())) + prd = await product_manager.run(Message(content=requirements, cause_by=BossRequirement.get_class_name())) logger.info(requirements) logger.info(prd) From 4b6745baaa26b78c2d0c7fcff70079f87674f35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 12:04:00 +0800 Subject: [PATCH 033/413] refactor: update notations --- tests/metagpt/memory/test_longterm_memory.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index c40d7ab9d..712402db1 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -2,7 +2,8 @@ # -*- coding: utf-8 -*- """ @Desc : unittest of `metagpt/memory/longterm_memory.py` -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import BossRequirement From 2c551c1fd38ad0bf318591bc92f5075f8aa6eead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 12:05:11 +0800 Subject: [PATCH 034/413] refactor: update notations --- tests/metagpt/memory/test_memory_storage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index 881b47d6f..c9585054a 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -2,7 +2,8 @@ # -*- coding: utf-8 -*- """ @Desc : the unittests of metagpt/memory/memory_storage.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ From 7e71ad85ca2dd638a044f9130737bb3685e7089d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 13:29:05 +0800 Subject: [PATCH 035/413] refactor: update notations --- tests/metagpt/planner/test_action_planner.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index 99cc25b72..f0a18da46 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -4,9 +4,9 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py -@Modified By: mashenquan, 2023-11-1. Optimization: - 1. Standardize the usage of message filtering-related features. - 2. Standardize the usage of message transmission. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution + feature for message handling. """ import pytest from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill From 55fe826b06f40298fc46b46bd56d43fe5a580536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 13:29:35 +0800 Subject: [PATCH 036/413] refactor: update notations --- tests/metagpt/planner/test_basic_planner.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index fa7ed7074..7623aee95 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -4,9 +4,9 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py -@Modified By: mashenquan, 2023-11-1. Optimization: - 1. Standardize the usage of message filtering-related features. - 2. Standardize the usage of message transmission. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution + feature for message handling. """ import pytest from semantic_kernel.core_skills import TextSkill From 78f3f128c046c93c29a906761fcbc4de03e88a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 13:40:48 +0800 Subject: [PATCH 037/413] refactor: update notations --- tests/metagpt/roles/mock.py | 3 ++- tests/metagpt/roles/test_architect.py | 4 +++- tests/metagpt/roles/test_engineer.py | 4 +++- tests/metagpt/test_environment.py | 3 ++- tests/metagpt/test_message.py | 1 + tests/metagpt/test_role.py | 3 ++- tests/metagpt/test_schema.py | 3 ++- tests/metagpt/utils/test_serialize.py | 3 ++- 8 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index b9891cd81..e67d64abc 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -4,7 +4,8 @@ @Time : 2023/5/12 13:05 @Author : alexanderwu @File : mock.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import BossRequirement, WriteDesign, WritePRD, WriteTasks from metagpt.schema import Message diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.py index 665242379..4effadaaa 100644 --- a/tests/metagpt/roles/test_architect.py +++ b/tests/metagpt/roles/test_architect.py @@ -4,7 +4,9 @@ @Time : 2023/5/20 14:37 @Author : alexanderwu @File : test_architect.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution + feature for message handling. """ import pytest diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index 93c3132ac..93f2efb77 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -4,7 +4,9 @@ @Time : 2023/5/12 10:14 @Author : alexanderwu @File : test_engineer.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data + type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution + feature for message handling. """ import pytest diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index 755798b17..714618852 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -4,7 +4,8 @@ @Time : 2023/5/12 00:47 @Author : alexanderwu @File : test_environment.py -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message transmission. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import pytest diff --git a/tests/metagpt/test_message.py b/tests/metagpt/test_message.py index 4f46311ce..04d85d9e4 100644 --- a/tests/metagpt/test_message.py +++ b/tests/metagpt/test_message.py @@ -4,6 +4,7 @@ @Time : 2023/5/16 10:57 @Author : alexanderwu @File : test_message.py +@Modified By: mashenquan, 2023-11-1. Modify coding style. """ import pytest diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 98646041d..f0ef4b3d9 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -4,7 +4,8 @@ @Time : 2023/5/11 14:44 @Author : alexanderwu @File : test_role.py -@Modified By: mashenquan, 2023/11/1. Add unit tests. +@Modified By: mashenquan, 2023-11-1. In line with Chapter 2.2.1 and 2.2.2 of RFC 116, introduce unit tests for + the utilization of the new message distribution feature in message handling. """ import pytest from pydantic import BaseModel diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 06bb57a70..2fa76fcad 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -4,7 +4,8 @@ @Time : 2023/5/20 10:40 @Author : alexanderwu @File : test_schema.py -@Modified By: mashenquan, 2023-11-1. Add `test_message`. +@Modified By: mashenquan, 2023-11-1. In line with Chapter 2.2.1 and 2.2.2 of RFC 116, introduce unit tests for + the utilization of the new feature of `Message` class. """ import json diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index 5a0840c87..7889f96fe 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -2,7 +2,8 @@ # -*- coding: utf-8 -*- """ @Desc : the unittest of serialize -@Modified By: mashenquan, 2023-11-1. Standardize the usage of message filtering-related features. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of + the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from typing import List, Tuple From e667fb4f00e19c3c8e36c51793c20dbcedf662dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 13:53:12 +0800 Subject: [PATCH 038/413] refactor: update notations --- metagpt/roles/role.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 79a9fb2de..753c22134 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -258,14 +258,14 @@ class Role(Named): async def run(self, test_message=None): """Observe, and think and act based on the results of the observation""" if test_message: # For test - seed = None + msg = None if isinstance(test_message, str): - seed = Message(test_message) + msg = Message(test_message) elif isinstance(test_message, Message): - seed = test_message + msg = test_message elif isinstance(test_message, list): - seed = Message("\n".join(test_message)) - self.put_message(seed) + msg = Message("\n".join(test_message)) + self.put_message(msg) if not await self._observe(): # If there is no new information, suspend and wait From 532099a7c6c7ebe5e20a657067e3a8540e7a068f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 15:14:05 +0800 Subject: [PATCH 039/413] refactor: update notations --- metagpt/environment.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index 028e98e8e..b93eeb6b2 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -44,8 +44,15 @@ class Environment(BaseModel): for role in roles: self.add_role(role) - def publish_message(self, message: Message): - """Distribute the message to the recipients.""" + def publish_message(self, message: Message) -> bool: + """ + Distribute the message to the recipients. + In accordance with the Message routing structure design in Chapter 2.2.1 of RFC 116, as already planned + in RFC 113 for the entire system, the routing information in the Message is only responsible for + specifying the message recipient, without concern for where the message recipient is located. How to + route the message to the message recipient is a problem addressed by the transport framework designed + in RFC 113. + """ logger.info(f"publish_message: {message.save()}") found = False for r in self.roles.values(): @@ -55,6 +62,12 @@ class Environment(BaseModel): if not found: logger.warning(f"Message no recipients: {message.save()}") + # Implemented the functionality related to remote message forwarding as described in RFC 113. Awaiting release. + # if self._parent: + # return self._parent.publish_message(message) + + return True + async def run(self, k=1): """处理一次所有信息的运行 Process all Role runs at once From 8137e1af5018169542055f064c1a8ef9b4333dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 3 Nov 2023 18:08:57 +0800 Subject: [PATCH 040/413] fixbug: creation of separate indices for each label --- metagpt/memory/memory.py | 6 ++++-- tests/metagpt/test_role.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 7f04be63d..cf3140bdb 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -26,8 +26,10 @@ class Memory: if message in self.storage: return self.storage.append(message) - if message.cause_by: - self.index[message.cause_by].append(message) + # According to the design of RFC 116, it allows message filtering based on different labels, thus + # necessitating the creation of separate indices for each label. + for k in message.tx_to: + self.index[k].append(message) def add_batch(self, messages: Iterable[Message]): for message in messages: diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index f0ef4b3d9..829f75bc5 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -64,6 +64,11 @@ async def test_react(): assert role.is_idle env = Environment() env.add_role(role) + env.publish_message(Message(content="test", tx_to=seed.subscription)) + assert not role.is_idle + while not env.is_idle: + await env.run() + assert role.is_idle env.publish_message(Message(content="test", cause_by=seed.subscription)) assert not role.is_idle while not env.is_idle: From 2688fe680adb60e355f7176d439df31b28237db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 14:07:33 +0800 Subject: [PATCH 041/413] feat: According to the routing feature plan in Chapter 2.2.3.2 of RFC 113, the routing functionality is to be consolidated into the Environment class. --- metagpt/environment.py | 26 ++++++++++++++++++-------- metagpt/roles/role.py | 9 +++++++-- tests/metagpt/test_role.py | 8 ++++++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index b93eeb6b2..0fa330a83 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -8,9 +8,11 @@ 1. Remove the functionality of `Environment` class as a public message buffer. 2. Standardize the message forwarding behavior of the `Environment` class. 3. Add the `is_idle` property. +@Modified By: mashenquan, 2023-11-4. According to the routing feature plan in Chapter 2.2.3.2 of RFC 113, the routing + functionality is to be consolidated into the `Environment` class. """ import asyncio -from typing import Iterable +from typing import Iterable, Set from pydantic import BaseModel, Field @@ -26,6 +28,7 @@ class Environment(BaseModel): """ roles: dict[str, Role] = Field(default_factory=dict) + consumers: dict[Role, Set] = Field(default_factory=dict) class Config: arbitrary_types_allowed = True @@ -36,6 +39,8 @@ class Environment(BaseModel): """ role.set_env(self) self.roles[role.profile] = role + # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 + self.set_subscribed_tags(role, role.subscribed_tags) def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 @@ -55,17 +60,14 @@ class Environment(BaseModel): """ logger.info(f"publish_message: {message.save()}") found = False - for r in self.roles.values(): - if message.is_recipient(r.subscribed_tags): - r.put_message(message) + # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 + for obj, subscribed_tags in self.consumers.items(): + if message.is_recipient(subscribed_tags): + obj.put_message(message) found = True if not found: logger.warning(f"Message no recipients: {message.save()}") - # Implemented the functionality related to remote message forwarding as described in RFC 113. Awaiting release. - # if self._parent: - # return self._parent.publish_message(message) - return True async def run(self, k=1): @@ -100,3 +102,11 @@ class Environment(BaseModel): if not r.is_idle: return False return True + + def get_subscribed_tags(self, obj): + """Get the labels for messages to be consumed by the object.""" + return self.consumers.get(obj, {}) + + def set_subscribed_tags(self, obj, tags): + """Set the labels for message to be consumed by the object""" + self.consumers[obj] = tags diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 753c22134..eacaa0034 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -15,6 +15,8 @@ messages into the Role object's private message receive buffer. There are no other message transmit methods. 5. Standardize the parameters for the `run` function: the `test_message` parameter is used for testing purposes only. In the normal workflow, you should use `publish_message` or `put_message` to transmit messages. +@Modified By: mashenquan, 2023-11-4. According to the routing feature plan in Chapter 2.2.3.2 of RFC 113, the routing + functionality is to be consolidated into the `Environment` class. """ from __future__ import annotations @@ -133,7 +135,7 @@ class Role(Named): def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" - tags = [get_class_name(t) for t in actions] + tags = {get_class_name(t) for t in actions} self.subscribe(tags) def subscribe(self, tags: Set[str]): @@ -141,6 +143,8 @@ class Role(Named): self._rc.watch.update(tags) # check RoleContext after adding watch actions self._rc.check(self._role_id) + if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 + self._rc.env.set_subscribed_tags(self, self.subscribed_tags) def _set_state(self, state): """Update the current state.""" @@ -149,7 +153,8 @@ class Role(Named): self._rc.todo = self._actions[self._rc.state] def set_env(self, env: "Environment"): - """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" + """Set the environment in which the role works. The role can talk to the environment and can also receive + messages by observing.""" self._rc.env = env @property diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 829f75bc5..7794c9b57 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -6,7 +6,11 @@ @File : test_role.py @Modified By: mashenquan, 2023-11-1. In line with Chapter 2.2.1 and 2.2.2 of RFC 116, introduce unit tests for the utilization of the new message distribution feature in message handling. +@Modified By: mashenquan, 2023-11-4. According to the routing feature plan in Chapter 2.2.3.2 of RFC 113, the routing + functionality is to be consolidated into the `Environment` class. """ +import uuid + import pytest from pydantic import BaseModel @@ -64,6 +68,7 @@ async def test_react(): assert role.is_idle env = Environment() env.add_role(role) + assert env.get_subscribed_tags(role) == {seed.subscription} env.publish_message(Message(content="test", tx_to=seed.subscription)) assert not role.is_idle while not env.is_idle: @@ -74,6 +79,9 @@ async def test_react(): while not env.is_idle: await env.run() assert role.is_idle + tag = uuid.uuid4().hex + role.subscribe({tag}) + assert env.get_subscribed_tags(role) == {seed.subscription, tag} if __name__ == "__main__": From c4eb028a8303a2dfb9fbb8018d751e5343c01d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 14:26:48 +0800 Subject: [PATCH 042/413] refactor: save -> dump --- metagpt/environment.py | 4 ++-- metagpt/schema.py | 14 +++++++------- tests/metagpt/test_schema.py | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index 0fa330a83..a7e6322ff 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -58,7 +58,7 @@ class Environment(BaseModel): route the message to the message recipient is a problem addressed by the transport framework designed in RFC 113. """ - logger.info(f"publish_message: {message.save()}") + logger.info(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 for obj, subscribed_tags in self.consumers.items(): @@ -66,7 +66,7 @@ class Environment(BaseModel): obj.put_message(message) found = True if not found: - logger.warning(f"Message no recipients: {message.save()}") + logger.warning(f"Message no recipients: {message.dump()}") return True diff --git a/metagpt/schema.py b/metagpt/schema.py index 34e6fa07b..bb8d8b42c 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -188,7 +188,7 @@ class Message(BaseModel): """Return a dict containing `role` and `content` for the LLM call.l""" return {"role": self.role, "content": self.content} - def save(self) -> str: + def dump(self) -> str: """Convert the object to json string""" return self.json(exclude_none=True) @@ -264,7 +264,7 @@ class MessageQueue: """Return true if the queue is empty.""" return self._queue.empty() - async def save(self) -> str: + async def dump(self) -> str: """Convert the `MessageQueue` object to a json string.""" if self.empty(): return "[]" @@ -299,7 +299,7 @@ class MessageQueue: if __name__ == "__main__": m = Message("a", role="v1") m.set_role("v2") - v = m.save() + v = m.dump() m = Message.load(v) test_content = "test_message" @@ -312,9 +312,9 @@ if __name__ == "__main__": logger.info(msgs) jsons = [ - UserMessage(test_content).save(), - SystemMessage(test_content).save(), - AIMessage(test_content).save(), - Message(test_content, role="QA").save(), + UserMessage(test_content).dump(), + SystemMessage(test_content).dump(), + AIMessage(test_content).dump(), + Message(test_content, role="QA").dump(), ] logger.info(jsons) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 2fa76fcad..21ba3fd14 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -31,13 +31,13 @@ def test_messages(): @pytest.mark.asyncio def test_message(): m = Message("a", role="v1") - v = m.save() + v = m.dump() d = json.loads(v) assert d assert d.get("content") == "a" assert d.get("meta_info") == {"role": "v1"} m.set_role("v2") - v = m.save() + v = m.dump() assert v m = Message.load(v) assert m.content == "a" From 1febf168e7bd7e2e10becbdad14ed42d03f2b443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:20:47 +0800 Subject: [PATCH 043/413] refactor: Override cause_by --- metagpt/schema.py | 33 +++++++++++++++++++++++++++++++++ tests/metagpt/test_schema.py | 10 ++++++++++ 2 files changed, 43 insertions(+) diff --git a/metagpt/schema.py b/metagpt/schema.py index bb8d8b42c..52020c468 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -24,6 +24,7 @@ from metagpt.const import ( MESSAGE_ROUTE_TO, ) from metagpt.logs import logger +from metagpt.utils.common import get_class_name, get_object_name class RawMessage(TypedDict): @@ -87,6 +88,14 @@ class Routes(BaseModel): route = self._get_route() return route.get(MESSAGE_ROUTE_TO) + def replace(self, old_val, new_val): + """Replace old value with new value""" + route = self._get_route() + tags = route.get(MESSAGE_ROUTE_TO, set()) + tags.discard(old_val) + tags.add(new_val) + route[MESSAGE_ROUTE_TO] = tags + class Message(BaseModel): """list[: ]""" @@ -147,6 +156,26 @@ class Message(BaseModel): """Labels for the consumer to filter its subscribed messages, also serving as meta info.""" return self.get_meta(MESSAGE_ROUTE_CAUSE_BY) + def __setattr__(self, key, val): + """Override `@property.setter`""" + if key == MESSAGE_ROUTE_CAUSE_BY: + self.set_cause_by(val) + return + super().__setattr__(key, val) + + def set_cause_by(self, val): + """Update the value of `cause_by` in the `meta_info` and `routes` attributes.""" + old_value = self.get_meta(MESSAGE_ROUTE_CAUSE_BY) + new_value = None + if isinstance(val, str): + new_value = val + elif not callable(val): + new_value = get_object_name(val) + else: + new_value = get_class_name(val) + self.set_meta(MESSAGE_ROUTE_CAUSE_BY, new_value) + self.route.replace(old_value, new_value) + @property def tx_from(self): """Message route info tells who sent this message.""" @@ -301,6 +330,10 @@ if __name__ == "__main__": m.set_role("v2") v = m.dump() m = Message.load(v) + m.cause_by = "Message" + m.cause_by = Routes + m.cause_by = Routes() + m.content = "b" test_content = "test_message" msgs = [ diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 21ba3fd14..e4aa0c0dd 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -11,6 +11,7 @@ import json import pytest +from metagpt.actions import Action from metagpt.schema import AIMessage, Message, Routes, SystemMessage, UserMessage @@ -50,6 +51,15 @@ def test_message(): assert m.cause_by == "c" assert m.get_meta("x") == "d" + m.cause_by = "Message" + assert m.cause_by == "Message" + m.cause_by = Action + assert m.cause_by == Action.get_class_name() + m.cause_by = Action() + assert m.cause_by == Action.get_class_name() + m.content = "b" + assert m.content == "b" + @pytest.mark.asyncio def test_routes(): From d9775037b68eee015f372e27e664e6f5952e9f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:46:32 +0800 Subject: [PATCH 044/413] refactor: @cause_by.setter --- examples/debate.py | 10 +++---- examples/sk_agent.py | 8 +++--- metagpt/actions/action.py | 3 +-- metagpt/actions/write_code.py | 2 +- metagpt/roles/engineer.py | 12 ++++----- metagpt/roles/qa_engineer.py | 20 +++++++------- metagpt/roles/role.py | 7 +++-- metagpt/schema.py | 19 ++++++------- metagpt/software_company.py | 7 +++-- metagpt/utils/common.py | 10 +++++++ metagpt/utils/named.py | 21 --------------- tests/metagpt/actions/test_write_prd.py | 2 +- tests/metagpt/memory/test_longterm_memory.py | 10 +++---- tests/metagpt/memory/test_memory_storage.py | 16 +++++------ tests/metagpt/planner/test_action_planner.py | 2 +- tests/metagpt/planner/test_basic_planner.py | 2 +- tests/metagpt/roles/mock.py | 8 +++--- tests/metagpt/test_environment.py | 2 +- tests/metagpt/test_schema.py | 5 ++-- tests/metagpt/utils/test_named.py | 28 -------------------- tests/metagpt/utils/test_serialize.py | 2 +- 21 files changed, 73 insertions(+), 123 deletions(-) delete mode 100644 metagpt/utils/named.py delete mode 100644 tests/metagpt/utils/test_named.py diff --git a/examples/debate.py b/examples/debate.py index 1f5e58839..c1d997678 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -66,7 +66,7 @@ class Trump(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([ShoutOut.get_class_name()]) + msg_history = self._rc.memory.get_by_actions([ShoutOut]) context = [] for m in msg_history: context.append(str(m)) @@ -77,7 +77,7 @@ class Trump(Role): msg = Message( content=rsp, role=self.profile, - cause_by=ShoutOut.get_class_name(), + cause_by=ShoutOut, tx_from=self.name, tx_to=self.opponent_name, ) @@ -102,14 +102,14 @@ class Biden(Role): await super()._observe() # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, # disregard own messages from the last round - message_filter = {BossRequirement.get_class_name(), self.name} + message_filter = {BossRequirement, self.name} self._rc.news = [msg for msg in self._rc.news if msg.is_recipient(message_filter)] return len(self._rc.news) async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([BossRequirement.get_class_name(), ShoutOut.get_class_name()]) + msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut]) context = [] for m in msg_history: context.append(str(m)) @@ -120,7 +120,7 @@ class Biden(Role): msg = Message( content=rsp, role=self.profile, - cause_by=ShoutOut.get_class_name(), + cause_by=ShoutOut, tx_from=self.name, tx_to=self.opponent_name, ) diff --git a/examples/sk_agent.py b/examples/sk_agent.py index 900696762..21714cca1 100644 --- a/examples/sk_agent.py +++ b/examples/sk_agent.py @@ -41,7 +41,7 @@ async def basic_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) + await role.run(Message(content=task, cause_by=BossRequirement)) async def sequential_planner_example(): @@ -55,7 +55,7 @@ async def sequential_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) + await role.run(Message(content=task, cause_by=BossRequirement)) async def basic_planner_web_search_example(): @@ -66,7 +66,7 @@ async def basic_planner_web_search_example(): role.import_skill(SkSearchEngine(), "WebSearchSkill") # role.import_semantic_skill_from_directory(skills_directory, "QASkill") - await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) + await role.run(Message(content=task, cause_by=BossRequirement)) async def action_planner_example(): @@ -77,7 +77,7 @@ async def action_planner_example(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - await role.run(Message(content=task, cause_by=BossRequirement.get_class_name())) # it will choose mathskill.Add + await role.run(Message(content=task, cause_by=BossRequirement)) # it will choose mathskill.Add if __name__ == "__main__": diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index c6f1f1534..fd114b332 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -17,10 +17,9 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder -from metagpt.utils.named import Named -class Action(ABC, Named): +class Action(ABC): def __init__(self, name: str = "", context=None, llm: LLM = None): self.name: str = name if llm is None: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index f0ef2b6d6..8b6451134 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -58,7 +58,7 @@ class WriteCode(Action): if self._is_invalid(filename): return - message_filter = {WriteDesign.get_class_name()} + message_filter = {WriteDesign} design = [i for i in context if i.is_recipient(message_filter)][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index ff71a61d8..7f05c52c5 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -102,7 +102,7 @@ class Engineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign.get_class_name())[-1] + msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -130,7 +130,7 @@ class Engineer(Role): todo_coros = [] for todo in self.todos: todo_coro = WriteCode().run( - context=self._rc.memory.get_by_actions([WriteTasks.get_class_name(), WriteDesign.get_class_name()]), + context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), filename=todo, ) todo_coros.append(todo_coro) @@ -185,7 +185,7 @@ class Engineer(Role): TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ context = [] - msg_filters = [WriteDesign.get_class_name(), WriteTasks.get_class_name(), WriteCode.get_class_name()] + msg_filters = [WriteDesign, WriteTasks, WriteCode] msg = self._rc.memory.get_by_actions(msg_filters) for m in msg: context.append(m.content) @@ -201,7 +201,7 @@ class Engineer(Role): logger.error("code review failed!", e) pass file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=WriteCode.get_class_name()) + msg = Message(content=code, role=self.profile, cause_by=WriteCode) self._rc.memory.add(msg) self.publish_message(msg) @@ -231,7 +231,7 @@ class Engineer(Role): return ret # Parse task lists - message_filter = {WriteTasks.get_class_name()} + message_filter = {WriteTasks} for message in self._rc.news: if not message.is_recipient(message_filter): continue @@ -241,7 +241,7 @@ class Engineer(Role): async def _think(self) -> None: # In asynchronous scenarios, first check if the required messages are ready. - filters = {WriteTasks.get_class_name()} + filters = {WriteTasks} msgs = self._rc.memory.get_by_actions(filters) if not msgs: self._rc.todo = None diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 5cc35a878..64d7f9702 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -50,7 +50,7 @@ class QaEngineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self, return_proj_dir=True) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign.get_class_name())[-1] + msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -99,7 +99,7 @@ class QaEngineer(Role): msg = Message( content=str(file_info), role=self.profile, - cause_by=WriteTest.get_class_name(), + cause_by=WriteTest, tx_from=self.profile, tx_to=self.profile, ) @@ -133,9 +133,7 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message( - content=content, role=self.profile, cause_by=RunCode.get_class_name(), tx_from=self.profile, tx_to=recipient - ) + msg = Message(content=content, role=self.profile, cause_by=RunCode, tx_from=self.profile, tx_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): @@ -147,7 +145,7 @@ class QaEngineer(Role): msg = Message( content=file_info, role=self.profile, - cause_by=DebugError.get_class_name(), + cause_by=DebugError, tx_from=self.profile, tx_to=recipient, ) @@ -165,14 +163,14 @@ class QaEngineer(Role): result_msg = Message( content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, - cause_by=WriteTest.get_class_name(), + cause_by=WriteTest, tx_from=self.profile, ) return result_msg - code_filters = {WriteCode.get_class_name(), WriteCodeReview.get_class_name()} - test_filters = {WriteTest.get_class_name(), DebugError.get_class_name()} - run_filters = {RunCode.get_class_name()} + code_filters = {WriteCode, WriteCodeReview} + test_filters = {WriteTest, DebugError} + run_filters = {RunCode} for msg in self._rc.news: # Decide what to do based on observed msg type, currently defined by human, # might potentially be moved to _think, that is, let the agent decides for itself @@ -189,7 +187,7 @@ class QaEngineer(Role): result_msg = Message( content=f"Round {self.test_round} of tests done", role=self.profile, - cause_by=WriteTest.get_class_name(), + cause_by=WriteTest, tx_from=self.profile, ) return result_msg diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index eacaa0034..87a03b391 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -31,7 +31,6 @@ from metagpt.logs import logger from metagpt.memory import LongTermMemory, Memory from metagpt.schema import Message, MessageQueue from metagpt.utils.common import get_class_name, get_object_name -from metagpt.utils.named import Named PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -107,7 +106,7 @@ class RoleContext(BaseModel): return self.memory.get() -class Role(Named): +class Role: """Role/Agent""" def __init__(self, name="", profile="", goal="", constraints="", desc=""): @@ -174,10 +173,10 @@ class Role(Named): return self._rc.watch return { self.name, - self.get_object_name(), + get_object_name(self), self.profile, f"{self.name}({self.profile})", - f"{self.name}({self.get_object_name()})", + f"{self.name}({get_object_name(self)})", } def _get_prefix(self): diff --git a/metagpt/schema.py b/metagpt/schema.py index 52020c468..1082c5ddb 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -24,7 +24,7 @@ from metagpt.const import ( MESSAGE_ROUTE_TO, ) from metagpt.logs import logger -from metagpt.utils.common import get_class_name, get_object_name +from metagpt.utils.common import any_to_str class RawMessage(TypedDict): @@ -129,11 +129,12 @@ class Message(BaseModel): if k in attribute_names: continue if k == MESSAGE_ROUTE_FROM: - self.set_from(v) + self.set_from(any_to_str(v)) continue if k == MESSAGE_ROUTE_CAUSE_BY: - self.meta_info[k] = v - if k == MESSAGE_ROUTE_TO or k == MESSAGE_ROUTE_CAUSE_BY: + self.set_cause_by(v) + continue + if k == MESSAGE_ROUTE_TO: self.add_to(v) continue self.meta_info[k] = v @@ -161,18 +162,14 @@ class Message(BaseModel): if key == MESSAGE_ROUTE_CAUSE_BY: self.set_cause_by(val) return + if key == MESSAGE_ROUTE_FROM: + self.set_from(any_to_str(val)) super().__setattr__(key, val) def set_cause_by(self, val): """Update the value of `cause_by` in the `meta_info` and `routes` attributes.""" old_value = self.get_meta(MESSAGE_ROUTE_CAUSE_BY) - new_value = None - if isinstance(val, str): - new_value = val - elif not callable(val): - new_value = get_object_name(val) - else: - new_value = get_class_name(val) + new_value = any_to_str(val) self.set_meta(MESSAGE_ROUTE_CAUSE_BY, new_value) self.route.replace(old_value, new_value) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index d29d8926d..57bd5db19 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -18,10 +18,9 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException -from metagpt.utils.named import Named -class SoftwareCompany(BaseModel, Named): +class SoftwareCompany(BaseModel): """ Software Company: Possesses a team, SOP (Standard Operating Procedures), and a platform for instant messaging, dedicated to writing executable code. @@ -55,8 +54,8 @@ class SoftwareCompany(BaseModel, Named): Message( role="BOSS", content=idea, - cause_by=BossRequirement.get_class_name(), - tx_from=SoftwareCompany.get_class_name(), + cause_by=BossRequirement, + tx_from=SoftwareCompany, ) ) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 219ed9f04..b372f0d8d 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -315,3 +315,13 @@ def get_object_name(obj) -> str: """Return class name of the object""" cls = type(obj) return f"{cls.__module__}.{cls.__name__}" + + +def any_to_str(val) -> str: + """Return the class name or the class name of the object, or 'val' if it's a string type.""" + if isinstance(val, str): + return val + if not callable(val): + return get_object_name(val) + + return get_class_name(val) diff --git a/metagpt/utils/named.py b/metagpt/utils/named.py deleted file mode 100644 index e4da574e8..000000000 --- a/metagpt/utils/named.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/11/1 -@Author : mashenquan -@File : named.py -""" - - -class Named: - """A base class with functions for converting classes to names and objects to class names.""" - - @classmethod - def get_class_name(cls): - """Return class name""" - return f"{cls.__module__}.{cls.__name__}" - - def get_object_name(self): - """Return class name of the object""" - cls = type(self) - return f"{cls.__module__}.{cls.__name__}" diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 0da7831c6..5a121adce 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -19,7 +19,7 @@ from metagpt.schema import Message async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" - prd = await product_manager.run(Message(content=requirements, cause_by=BossRequirement.get_class_name())) + prd = await product_manager.run(Message(content=requirements, cause_by=BossRequirement)) logger.info(requirements) logger.info(prd) diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index 712402db1..b33dd312d 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -19,24 +19,24 @@ def test_ltm_search(): assert len(openai_api_key) > 20 role_id = "UTUserLtm(Product Manager)" - rc = RoleContext(watch=[BossRequirement.get_class_name()]) + rc = RoleContext(watch=[BossRequirement]) ltm = LongTermMemory() ltm.recover_memory(role_id, rc) idea = "Write a cli snake game" - message = Message(role="BOSS", content=idea, cause_by=BossRequirement.get_class_name()) + message = Message(role="BOSS", content=idea, cause_by=BossRequirement) news = ltm.find_news([message]) assert len(news) == 1 ltm.add(message) sim_idea = "Write a game of cli snake" - sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement.get_class_name()) + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement) news = ltm.find_news([sim_message]) assert len(news) == 0 ltm.add(sim_message) new_idea = "Write a 2048 web game" - new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) news = ltm.find_news([new_message]) assert len(news) == 1 ltm.add(new_message) @@ -52,7 +52,7 @@ def test_ltm_search(): assert len(news) == 0 new_idea = "Write a Battle City" - new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) news = ltm_new.find_news([new_message]) assert len(news) == 1 diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index c9585054a..c40bbbba5 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -18,7 +18,7 @@ from metagpt.schema import Message def test_idea_message(): idea = "Write a cli snake game" role_id = "UTUser1(Product Manager)" - message = Message(role="BOSS", content=idea, cause_by=BossRequirement.get_class_name()) + message = Message(role="BOSS", content=idea, cause_by=BossRequirement) memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -28,12 +28,12 @@ def test_idea_message(): assert memory_storage.is_initialized is True sim_idea = "Write a game of cli snake" - sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement.get_class_name()) + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement) new_messages = memory_storage.search(sim_message) assert len(new_messages) == 0 # similar, return [] new_idea = "Write a 2048 web game" - new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement.get_class_name()) + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content @@ -49,7 +49,7 @@ def test_actionout_message(): role_id = "UTUser2(Architect)" content = "The boss has requested the creation of a command-line interface (CLI) snake game" message = Message( - content=content, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() + content=content, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD ) # WritePRD as test action memory_storage: MemoryStorage = MemoryStorage() @@ -60,16 +60,12 @@ def test_actionout_message(): assert memory_storage.is_initialized is True sim_conent = "The request is command-line interface (CLI) snake game" - sim_message = Message( - content=sim_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() - ) + sim_message = Message(content=sim_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD) new_messages = memory_storage.search(sim_message) assert len(new_messages) == 0 # similar, return [] new_conent = "Incorporate basic features of a snake game such as scoring and increasing difficulty" - new_message = Message( - content=new_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() - ) + new_message = Message(content=new_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index f0a18da46..e8350b6e6 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -26,7 +26,7 @@ async def test_action_planner(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - role.put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + role.put_message(Message(content=task, cause_by=BossRequirement)) await role._observe() await role._think() # it will choose mathskill.Add assert "1100" == (await role._act()).content diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index 7623aee95..0935dd98c 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -29,7 +29,7 @@ async def test_basic_planner(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - role.put_message(Message(content=task, cause_by=BossRequirement.get_class_name())) + role.put_message(Message(content=task, cause_by=BossRequirement)) await role._observe() await role._think() # assuming sk_agent will think he needs WriterSkill.Brainstorm and WriterSkill.Translate diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index e67d64abc..1bf20e9b7 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -254,7 +254,7 @@ a = 'a' class MockMessages: - req = Message(role="Boss", content=BOSS_REQUIREMENT, cause_by=BossRequirement.get_class_name()) - prd = Message(role="Product Manager", content=PRD, cause_by=WritePRD.get_class_name()) - system_design = Message(role="Architect", content=SYSTEM_DESIGN, cause_by=WriteDesign.get_class_name()) - tasks = Message(role="Project Manager", content=TASKS, cause_by=WriteTasks.get_class_name()) + req = Message(role="Boss", content=BOSS_REQUIREMENT, cause_by=BossRequirement) + prd = Message(role="Product Manager", content=PRD, cause_by=WritePRD) + system_design = Message(role="Architect", content=SYSTEM_DESIGN, cause_by=WriteDesign) + tasks = Message(role="Project Manager", content=TASKS, cause_by=WriteTasks) diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index 714618852..472d4cd9d 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -51,7 +51,7 @@ async def test_publish_and_process_message(env: Environment): env.add_roles([product_manager, architect]) env.set_manager(Manager()) - env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement.get_class_name())) + env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement)) await env.run(k=2) logger.info(f"{env.history=}") diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index e4aa0c0dd..e18ebbe79 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -13,6 +13,7 @@ import pytest from metagpt.actions import Action from metagpt.schema import AIMessage, Message, Routes, SystemMessage, UserMessage +from metagpt.utils.common import get_class_name @pytest.mark.asyncio @@ -54,9 +55,9 @@ def test_message(): m.cause_by = "Message" assert m.cause_by == "Message" m.cause_by = Action - assert m.cause_by == Action.get_class_name() + assert m.cause_by == get_class_name(Action) m.cause_by = Action() - assert m.cause_by == Action.get_class_name() + assert m.cause_by == get_class_name(Action) m.content = "b" assert m.content == "b" diff --git a/tests/metagpt/utils/test_named.py b/tests/metagpt/utils/test_named.py deleted file mode 100644 index ff1f07205..000000000 --- a/tests/metagpt/utils/test_named.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023-11-1 -@Author : mashenquan -@File : test_named.py -""" -import pytest - -from metagpt.utils.named import Named - - -@pytest.mark.asyncio -async def test_suite(): - class A(Named): - pass - - class B(A): - pass - - assert A.get_class_name() == "tests.metagpt.utils.test_named.A" - assert A().get_object_name() == "tests.metagpt.utils.test_named.A" - assert B.get_class_name() == "tests.metagpt.utils.test_named.B" - assert B().get_object_name() == "tests.metagpt.utils.test_named.B" - - -if __name__ == "__main__": - pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index 7889f96fe..3f566d64d 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -59,7 +59,7 @@ def test_serialize_and_deserialize_message(): ic_obj = ActionOutput.create_model_class("prd", out_mapping) message = Message( - content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD.get_class_name() + content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD ) # WritePRD as test action message_ser = serialize_message(message) From 56f544a675ef7417b46e2f683609b497af31feef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:49:13 +0800 Subject: [PATCH 045/413] refactor: @cause_by.setter --- examples/agent_creator.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 5a1398456..3618c0608 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -2,8 +2,6 @@ Filename: MetaGPT/examples/agent_creator.py Created Date: Tuesday, September 12th 2023, 3:28:37 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import re @@ -12,7 +10,6 @@ from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_object_name with open(PROJECT_ROOT / "examples/build_customized_agent.py", "r") as f: # use official example script to guide AgentCreator @@ -75,7 +72,7 @@ class AgentCreator(Role): instruction = msg.content code_text = await CreateAgent().run(example=self.agent_template, instruction=instruction) - msg = Message(content=code_text, role=self.profile, cause_by=get_object_name(todo)) + msg = Message(content=code_text, role=self.profile, cause_by=todo) return msg From 8ea52d8a83ce9615b56831fdd9c27c82e0c885f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:52:21 +0800 Subject: [PATCH 046/413] refactor: @cause_by.setter --- examples/build_customized_agent.py | 4 +--- metagpt/schema.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index af15c90ca..f7f554e53 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -2,8 +2,6 @@ Filename: MetaGPT/examples/build_customized_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import asyncio import re @@ -83,7 +81,7 @@ class SimpleCoder(Role): instruction = msg.content code_text = await SimpleWriteCode().run(instruction) - msg = Message(content=code_text, role=self.profile, cause_by=get_object_name(todo)) + msg = Message(content=code_text, role=self.profile, cause_by=todo) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 1082c5ddb..0be067cfe 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -135,7 +135,7 @@ class Message(BaseModel): self.set_cause_by(v) continue if k == MESSAGE_ROUTE_TO: - self.add_to(v) + self.add_to(any_to_str(v)) continue self.meta_info[k] = v From 87882bf7ab56c3bcff54b5bb8fd41c09afd5bcb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:54:36 +0800 Subject: [PATCH 047/413] refactor: @cause_by.setter --- examples/build_customized_agent.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index f7f554e53..ef274be8b 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -13,7 +13,6 @@ from metagpt.actions import Action from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_object_name class SimpleWriteCode(Action): @@ -119,7 +118,7 @@ class RunnableCoder(Role): code_text = msg.content result = await SimpleRunCode().run(code_text) - msg = Message(content=result, role=self.profile, cause_by=get_object_name(todo)) + msg = Message(content=result, role=self.profile, cause_by=todo) self._rc.memory.add(msg) return msg From b0d451d4d60246d36ff77f28830d6ce16f846c1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 16:55:51 +0800 Subject: [PATCH 048/413] refactor: @cause_by.setter --- examples/sk_agent.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/sk_agent.py b/examples/sk_agent.py index 21714cca1..a7513e838 100644 --- a/examples/sk_agent.py +++ b/examples/sk_agent.py @@ -4,8 +4,6 @@ @Time : 2023/9/13 12:36 @Author : femto Zheng @File : sk_agent.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import asyncio From 2129c904ea498ef54b233b94235ae5faacb6eb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:02:40 +0800 Subject: [PATCH 049/413] refactor: @cause_by.setter --- metagpt/actions/action.py | 1 - metagpt/software_company.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index fd114b332..790295d55 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -4,7 +4,6 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : action.py -@Modified By: mashenquan, 2023-11-1. Add generic class-to-string and object-to-string conversion functionality. """ import re from abc import ABC diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 57bd5db19..354773444 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -5,9 +5,7 @@ @Author : alexanderwu @File : software_company.py @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - 1. Change the data type of the `cause_by` value in the `Message` to a string to support the new message - distribution feature. - 2. Abandon the design of having `Environment` store all messages. + 1. Abandon the design of having `Environment` store all messages. """ from pydantic import BaseModel, Field From 8f85d80b181825dd2d43e4c6fe24ab0c306a3e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:05:15 +0800 Subject: [PATCH 050/413] refactor: @cause_by.setter --- tests/metagpt/actions/test_write_prd.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 5a121adce..07d701cb9 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -4,8 +4,7 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : test_write_prd.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. +@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, replace `handle` with `run`. """ import pytest From 2b2f29dcd579675ae3f0cb30217625787918474c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:06:21 +0800 Subject: [PATCH 051/413] refactor: @cause_by.setter --- tests/metagpt/memory/test_longterm_memory.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index b33dd312d..c5b5c6eb1 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- """ @Desc : unittest of `metagpt/memory/longterm_memory.py` -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import BossRequirement From 9de646c01d5b4ffb977d44e335232c840a6bce7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:07:24 +0800 Subject: [PATCH 052/413] refactor: @cause_by.setter --- tests/metagpt/memory/test_memory_storage.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index c40bbbba5..251c70b02 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- """ @Desc : the unittests of metagpt/memory/memory_storage.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ From e696442db935609c842f9d855a4926b048551414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:08:58 +0800 Subject: [PATCH 053/413] refactor: @cause_by.setter --- tests/metagpt/planner/test_action_planner.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index e8350b6e6..b8d4c1ad9 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -4,9 +4,8 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message handling. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, utilize the new message + distribution feature for message handling. """ import pytest from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill From e86d8a3952ec3dd28a46ff4c8a118d08ecb7249c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:09:48 +0800 Subject: [PATCH 054/413] refactor: @cause_by.setter --- tests/metagpt/planner/test_basic_planner.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index 0935dd98c..24250a0b0 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -4,9 +4,8 @@ @Time : 2023/9/16 20:03 @Author : femto Zheng @File : test_basic_planner.py -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message handling. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, utilize the new message + distribution feature for message handling. """ import pytest from semantic_kernel.core_skills import TextSkill From be77a9c30866cefe99105d2975d5236c67284875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:10:44 +0800 Subject: [PATCH 055/413] refactor: @cause_by.setter --- tests/metagpt/roles/mock.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 1bf20e9b7..1b02fbaa5 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -4,8 +4,6 @@ @Time : 2023/5/12 13:05 @Author : alexanderwu @File : mock.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import BossRequirement, WriteDesign, WritePRD, WriteTasks from metagpt.schema import Message From 3a5bfcafc52613b5691aaa7121634d60a834f402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:12:02 +0800 Subject: [PATCH 056/413] refactor: @cause_by.setter --- tests/metagpt/roles/test_architect.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/roles/test_architect.py b/tests/metagpt/roles/test_architect.py index 4effadaaa..111438b0b 100644 --- a/tests/metagpt/roles/test_architect.py +++ b/tests/metagpt/roles/test_architect.py @@ -4,9 +4,8 @@ @Time : 2023/5/20 14:37 @Author : alexanderwu @File : test_architect.py -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message handling. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, utilize the new message + distribution feature for message handling. """ import pytest From 327c047fa51226f36a8dd414ca77c7fcde319493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:13:10 +0800 Subject: [PATCH 057/413] refactor: @cause_by.setter --- tests/metagpt/roles/test_engineer.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index 93f2efb77..3dc599770 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -4,9 +4,8 @@ @Time : 2023/5/12 10:14 @Author : alexanderwu @File : test_engineer.py -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message handling. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, utilize the new message + distribution feature for message handling. """ import pytest From eba7f868e71678338125221c85f0aa2d527c16b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:17:02 +0800 Subject: [PATCH 058/413] refactor: @cause_by.setter --- tests/metagpt/test_environment.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index 472d4cd9d..a0f1f6257 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -4,8 +4,6 @@ @Time : 2023/5/12 00:47 @Author : alexanderwu @File : test_environment.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ import pytest From ed7eb4d08a07a0c4dc2530e7e1c55d6c5bae0bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sat, 4 Nov 2023 17:18:26 +0800 Subject: [PATCH 059/413] refactor: @cause_by.setter --- tests/metagpt/utils/test_serialize.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index 3f566d64d..ffa34866c 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- """ @Desc : the unittest of serialize -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from typing import List, Tuple From c6f97f748717c030f752ebe492342aace4a4ab13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 6 Nov 2023 11:47:29 +0800 Subject: [PATCH 060/413] refactor: tx_from/tx_to --- examples/debate.py | 8 ++++---- metagpt/const.py | 4 ++-- metagpt/memory/memory.py | 2 +- metagpt/roles/engineer.py | 4 ++-- metagpt/roles/qa_engineer.py | 14 +++++++------- metagpt/roles/role.py | 4 ++-- metagpt/schema.py | 16 ++++++++-------- metagpt/software_company.py | 2 +- tests/metagpt/test_role.py | 2 +- tests/metagpt/test_schema.py | 8 ++++---- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index c1d997678..77a2ce129 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -78,8 +78,8 @@ class Trump(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - tx_from=self.name, - tx_to=self.opponent_name, + msg_from=self.name, + msg_to=self.opponent_name, ) return msg @@ -121,8 +121,8 @@ class Biden(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - tx_from=self.name, - tx_to=self.opponent_name, + msg_from=self.name, + msg_to=self.opponent_name, ) return msg diff --git a/metagpt/const.py b/metagpt/const.py index e783ec8d0..7b8203bce 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -44,7 +44,7 @@ SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" MEM_TTL = 24 * 30 * 3600 -MESSAGE_ROUTE_FROM = "tx_from" -MESSAGE_ROUTE_TO = "tx_to" +MESSAGE_ROUTE_FROM = "msg_from" +MESSAGE_ROUTE_TO = "msg_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index cf3140bdb..c6b732076 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -28,7 +28,7 @@ class Memory: self.storage.append(message) # According to the design of RFC 116, it allows message filtering based on different labels, thus # necessitating the creation of separate indices for each label. - for k in message.tx_to: + for k in message.msg_to: self.index[k].append(message) def add_batch(self, messages: Iterable[Message]): diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 7f05c52c5..8778471cc 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -170,7 +170,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_to="QaEngineer", + msg_to="QaEngineer", ) return msg @@ -213,7 +213,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_to="QaEngineer", + msg_to="QaEngineer", ) return msg diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 64d7f9702..05fc5b217 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -100,8 +100,8 @@ class QaEngineer(Role): content=str(file_info), role=self.profile, cause_by=WriteTest, - tx_from=self.profile, - tx_to=self.profile, + msg_from=self.profile, + msg_to=self.profile, ) self.publish_message(msg) @@ -133,7 +133,7 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, tx_from=self.profile, tx_to=recipient) + msg = Message(content=content, role=self.profile, cause_by=RunCode, msg_from=self.profile, msg_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): @@ -146,8 +146,8 @@ class QaEngineer(Role): content=file_info, role=self.profile, cause_by=DebugError, - tx_from=self.profile, - tx_to=recipient, + msg_from=self.profile, + msg_to=recipient, ) self.publish_message(msg) @@ -164,7 +164,7 @@ class QaEngineer(Role): content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, cause_by=WriteTest, - tx_from=self.profile, + msg_from=self.profile, ) return result_msg @@ -188,6 +188,6 @@ class QaEngineer(Role): content=f"Round {self.test_round} of tests done", role=self.profile, cause_by=WriteTest, - tx_from=self.profile, + msg_from=self.profile, ) return result_msg diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 87a03b391..9bbba2070 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -211,14 +211,14 @@ class Role: instruct_content=response.instruct_content, role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_from=get_object_name(self), + msg_from=get_object_name(self), ) else: msg = Message( content=response, role=self.profile, cause_by=get_object_name(self._rc.todo), - tx_from=get_object_name(self), + msg_from=get_object_name(self), ) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 0be067cfe..39a62e706 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -77,13 +77,13 @@ class Routes(BaseModel): return False @property - def tx_from(self): + def msg_from(self): """Message route info tells who sent this message.""" route = self._get_route() return route.get(MESSAGE_ROUTE_FROM) @property - def tx_to(self): + def msg_to(self): """Labels for the consumer to filter its subscribed messages.""" route = self._get_route() return route.get(MESSAGE_ROUTE_TO) @@ -112,8 +112,8 @@ class Message(BaseModel): :param instruct_content: Message content struct. :param meta_info: Message meta info. :param route: Message route configuration. - :param tx_from: Message route info tells who sent this message. - :param tx_to: Labels for the consumer to filter its subscribed messages. + :param msg_from: Message route info tells who sent this message. + :param msg_to: Labels for the consumer to filter its subscribed messages. :param cause_by: Labels for the consumer to filter its subscribed messages, also serving as meta info. :param role: Message meta info tells who sent this message. """ @@ -174,14 +174,14 @@ class Message(BaseModel): self.route.replace(old_value, new_value) @property - def tx_from(self): + def msg_from(self): """Message route info tells who sent this message.""" - return self.route.tx_from + return self.route.msg_from @property - def tx_to(self): + def msg_to(self): """Labels for the consumer to filter its subscribed messages.""" - return self.route.tx_to + return self.route.msg_to def set_role(self, v): """Set the message's meta info indicating the sender.""" diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 354773444..1b6936870 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -53,7 +53,7 @@ class SoftwareCompany(BaseModel): role="BOSS", content=idea, cause_by=BossRequirement, - tx_from=SoftwareCompany, + msg_from=SoftwareCompany, ) ) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 7794c9b57..69386e28c 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -69,7 +69,7 @@ async def test_react(): env = Environment() env.add_role(role) assert env.get_subscribed_tags(role) == {seed.subscription} - env.publish_message(Message(content="test", tx_to=seed.subscription)) + env.publish_message(Message(content="test", msg_to=seed.subscription)) assert not role.is_idle while not env.is_idle: await env.run() diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index e18ebbe79..5ebc7ce1d 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -66,13 +66,13 @@ def test_message(): def test_routes(): route = Routes() route.set_from("a") - assert route.tx_from == "a" + assert route.msg_from == "a" route.add_to("b") - assert route.tx_to == {"b"} + assert route.msg_to == {"b"} route.add_to("c") - assert route.tx_to == {"b", "c"} + assert route.msg_to == {"b", "c"} route.set_to({"e", "f"}) - assert route.tx_to == {"e", "f"} + assert route.msg_to == {"e", "f"} assert route.is_recipient({"e"}) assert route.is_recipient({"f"}) assert not route.is_recipient({"a"}) From c496b6b5f604cfa46e239b360bf6a2a743114536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 6 Nov 2023 22:38:43 +0800 Subject: [PATCH 061/413] feat: add default subscriptions to all Role --- metagpt/const.py | 1 + metagpt/roles/role.py | 4 ++++ metagpt/schema.py | 3 +++ 3 files changed, 8 insertions(+) diff --git a/metagpt/const.py b/metagpt/const.py index 7b8203bce..2ba875543 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -48,3 +48,4 @@ MESSAGE_ROUTE_FROM = "msg_from" MESSAGE_ROUTE_TO = "msg_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" +MESSAGE_ROUTE_TO_ALL = "" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 9bbba2070..6e8c5e421 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -135,6 +135,10 @@ class Role: def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" tags = {get_class_name(t) for t in actions} + # Add default subscription tags for developers' direct use. + if self.name: + tags.add(self.name) + tags.add(get_object_name(self)) self.subscribe(tags) def subscribe(self, tags: Set[str]): diff --git a/metagpt/schema.py b/metagpt/schema.py index 39a62e706..fb8885614 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -22,6 +22,7 @@ from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, MESSAGE_ROUTE_FROM, MESSAGE_ROUTE_TO, + MESSAGE_ROUTE_TO_ALL, ) from metagpt.logs import logger from metagpt.utils.common import any_to_str @@ -71,6 +72,8 @@ class Routes(BaseModel): if not to_tags: return True + if MESSAGE_ROUTE_TO_ALL in to_tags: + return True for k in tags: if k in to_tags: return True From a045f73fec38536441a53f27f948ec5b9f1a5594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 6 Nov 2023 23:13:58 +0800 Subject: [PATCH 062/413] feat: Support more versatile parameter formats. --- metagpt/schema.py | 6 +++++- tests/metagpt/test_role.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index fb8885614..e89ac00ea 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -138,7 +138,11 @@ class Message(BaseModel): self.set_cause_by(v) continue if k == MESSAGE_ROUTE_TO: - self.add_to(any_to_str(v)) + if isinstance(v, tuple) or isinstance(v, list) or isinstance(v, set): + for i in v: + self.add_to(any_to_str(i)) + else: + self.add_to(any_to_str(v)) continue self.meta_info[k] = v diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 69386e28c..447de7ee5 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -18,6 +18,7 @@ from metagpt.actions import Action, ActionOutput from metagpt.environment import Environment from metagpt.roles import Role from metagpt.schema import Message +from metagpt.utils.common import get_class_name class MockAction(Action): @@ -84,5 +85,17 @@ async def test_react(): assert env.get_subscribed_tags(role) == {seed.subscription, tag} +@pytest.mark.asyncio +async def test_msg_to(): + m = Message(content="a", msg_to=["a", MockRole, Message]) + assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message)} + + m = Message(content="a", cause_by=MockAction, msg_to={"a", MockRole, Message}) + assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message), get_class_name(MockAction)} + + m = Message(content="a", msg_to=("a", MockRole, Message)) + assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message)} + + if __name__ == "__main__": pytest.main([__file__, "-s"]) From 93ebe8c103388ffbe48119b0600ea0bd4c55b64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 7 Nov 2023 14:12:20 +0800 Subject: [PATCH 063/413] feat: recover `history` --- metagpt/environment.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/environment.py b/metagpt/environment.py index a7e6322ff..75a790714 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -29,6 +29,7 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) consumers: dict[Role, Set] = Field(default_factory=dict) + history: str = Field(default="") # For debug class Config: arbitrary_types_allowed = True @@ -67,6 +68,7 @@ class Environment(BaseModel): found = True if not found: logger.warning(f"Message no recipients: {message.dump()}") + self.history += f"\n{message}" # For debug return True From af4c87e1234db2828e3f76a7db17b1ceb7ba81ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 13:42:08 +0800 Subject: [PATCH 064/413] refactor: rename is_recipient --- examples/debate.py | 4 ++-- metagpt/actions/write_code.py | 2 +- metagpt/environment.py | 2 +- metagpt/memory/longterm_memory.py | 4 ++-- metagpt/roles/engineer.py | 2 +- metagpt/roles/qa_engineer.py | 8 ++++---- metagpt/schema.py | 8 ++++---- tests/metagpt/test_schema.py | 8 ++++---- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 77a2ce129..cf0c0124c 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -60,7 +60,7 @@ class Trump(Role): async def _observe(self) -> int: await super()._observe() # accept messages sent (from opponent) to self, disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.is_recipient({self.name})] + self._rc.news = [msg for msg in self._rc.news if msg.contain_any({self.name})] return len(self._rc.news) async def _act(self) -> Message: @@ -103,7 +103,7 @@ class Biden(Role): # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, # disregard own messages from the last round message_filter = {BossRequirement, self.name} - self._rc.news = [msg for msg in self._rc.news if msg.is_recipient(message_filter)] + self._rc.news = [msg for msg in self._rc.news if msg.contain_any(message_filter)] return len(self._rc.news) async def _act(self) -> Message: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 8b6451134..f2a4744d9 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -59,7 +59,7 @@ class WriteCode(Action): return message_filter = {WriteDesign} - design = [i for i in context if i.is_recipient(message_filter)][0] + design = [i for i in context if i.contain_any(message_filter)][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) ws_path = WORKSPACE_ROOT / ws_name diff --git a/metagpt/environment.py b/metagpt/environment.py index 75a790714..fb564e1ab 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -63,7 +63,7 @@ class Environment(BaseModel): found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 for obj, subscribed_tags in self.consumers.items(): - if message.is_recipient(subscribed_tags): + if message.contain_any(subscribed_tags): obj.put_message(message) found = True if not found: diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index e73ae334e..2a4b604e0 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -3,7 +3,7 @@ """ @Desc : the implement of Long-term memory @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - 1. Replace code related to message filtering with the `Message.is_recipient` function. + 1. Replace code related to message filtering with the `Message.contain_any` function. """ from metagpt.logs import logger @@ -40,7 +40,7 @@ class LongTermMemory(Memory): def add(self, message: Message): super(LongTermMemory, self).add(message) - if message.is_recipient(self.rc.watch) and not self.msg_from_recover: + if message.contain_any(self.rc.watch) and not self.msg_from_recover: # currently, only add role's watching messages to its memory_storage # and ignore adding messages from recover repeatedly self.memory_storage.add(message) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 8778471cc..882cf89dd 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -233,7 +233,7 @@ class Engineer(Role): # Parse task lists message_filter = {WriteTasks} for message in self._rc.news: - if not message.is_recipient(message_filter): + if not message.contain_any(message_filter): continue self.todos = self.parse_tasks(message) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 05fc5b217..104aa3dfb 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -154,7 +154,7 @@ class QaEngineer(Role): async def _observe(self) -> int: await super()._observe() self._rc.news = [ - msg for msg in self._rc.news if msg.is_recipient({self.profile}) + msg for msg in self._rc.news if msg.contain_any({self.profile}) ] # only relevant msgs count as observed news return len(self._rc.news) @@ -174,13 +174,13 @@ class QaEngineer(Role): for msg in self._rc.news: # Decide what to do based on observed msg type, currently defined by human, # might potentially be moved to _think, that is, let the agent decides for itself - if msg.is_recipient(code_filters): + if msg.contain_any(code_filters): # engineer wrote a code, time to write a test for it await self._write_test(msg) - elif msg.is_recipient(test_filters): + elif msg.contain_any(test_filters): # I wrote or debugged my test code, time to run it await self._run_code(msg) - elif msg.is_recipient(run_filters): + elif msg.contain_any(run_filters): # I ran my test code, time to fix bugs, if any await self._debug_error(msg) self.test_round += 1 diff --git a/metagpt/schema.py b/metagpt/schema.py index e89ac00ea..1b00843a6 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -65,8 +65,8 @@ class Routes(BaseModel): self.routes.append({}) return self.routes[0] - def is_recipient(self, tags: Set) -> bool: - """Check if it is the message recipient.""" + def contain_any(self, tags: Set) -> bool: + """Check if this object contains these tags.""" route = self._get_route() to_tags = route.get(MESSAGE_ROUTE_TO) if not to_tags: @@ -206,9 +206,9 @@ class Message(BaseModel): """Add a subscription label for the recipients.""" self.route.add_to(tag) - def is_recipient(self, tags: Set): + def contain_any(self, tags: Set): """Return true if any input label exists in the message's subscription labels.""" - return self.route.is_recipient(tags) + return self.route.contain_any(tags) def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 5ebc7ce1d..05127362b 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -48,7 +48,7 @@ def test_message(): m = Message("a", role="b", cause_by="c", x="d") assert m.content == "a" assert m.role == "b" - assert m.is_recipient({"c"}) + assert m.contain_any({"c"}) assert m.cause_by == "c" assert m.get_meta("x") == "d" @@ -73,9 +73,9 @@ def test_routes(): assert route.msg_to == {"b", "c"} route.set_to({"e", "f"}) assert route.msg_to == {"e", "f"} - assert route.is_recipient({"e"}) - assert route.is_recipient({"f"}) - assert not route.is_recipient({"a"}) + assert route.contain_any({"e"}) + assert route.contain_any({"f"}) + assert not route.contain_any({"a"}) if __name__ == "__main__": From c18bc7c876f062c3427159146d8274d7012979d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 20:27:18 +0800 Subject: [PATCH 065/413] refactor: Simplify the Message class. --- metagpt/const.py | 4 +- metagpt/schema.py | 235 ++++++----------------------------- metagpt/utils/common.py | 11 ++ tests/metagpt/test_schema.py | 28 ++--- 4 files changed, 63 insertions(+), 215 deletions(-) diff --git a/metagpt/const.py b/metagpt/const.py index 2ba875543..fa0ccc536 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -44,8 +44,8 @@ SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" MEM_TTL = 24 * 30 * 3600 -MESSAGE_ROUTE_FROM = "msg_from" -MESSAGE_ROUTE_TO = "msg_to" +MESSAGE_ROUTE_FROM = "sent_from" +MESSAGE_ROUTE_TO = "send_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" MESSAGE_ROUTE_TO_ALL = "" diff --git a/metagpt/schema.py b/metagpt/schema.py index 1b00843a6..7fdcef2ed 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -13,19 +13,18 @@ import asyncio import json from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError -from typing import Dict, List, Set, TypedDict +from typing import List, Set, TypedDict from pydantic import BaseModel, Field from metagpt.const import ( - MESSAGE_META_ROLE, MESSAGE_ROUTE_CAUSE_BY, MESSAGE_ROUTE_FROM, MESSAGE_ROUTE_TO, MESSAGE_ROUTE_TO_ALL, ) from metagpt.logs import logger -from metagpt.utils.common import any_to_str +from metagpt.utils.common import any_to_str, any_to_str_set class RawMessage(TypedDict): @@ -33,182 +32,56 @@ class RawMessage(TypedDict): role: str -class Routes(BaseModel): - """Responsible for managing routing information for the Message class.""" - - routes: List[Dict] = Field(default_factory=list) - - def set_from(self, value): - """Set the label of the message sender.""" - route = self._get_route() - route[MESSAGE_ROUTE_FROM] = value - - def set_to(self, tags: Set): - """Set the labels of the message recipient.""" - route = self._get_route() - if tags: - route[MESSAGE_ROUTE_TO] = tags - return - - if MESSAGE_ROUTE_TO in route: - del route[MESSAGE_ROUTE_TO] - - def add_to(self, tag: str): - """Add a label of the message recipient.""" - route = self._get_route() - tags = route.get(MESSAGE_ROUTE_TO, set()) - tags.add(tag) - route[MESSAGE_ROUTE_TO] = tags - - def _get_route(self) -> Dict: - if not self.routes: - self.routes.append({}) - return self.routes[0] - - def contain_any(self, tags: Set) -> bool: - """Check if this object contains these tags.""" - route = self._get_route() - to_tags = route.get(MESSAGE_ROUTE_TO) - if not to_tags: - return True - - if MESSAGE_ROUTE_TO_ALL in to_tags: - return True - for k in tags: - if k in to_tags: - return True - return False - - @property - def msg_from(self): - """Message route info tells who sent this message.""" - route = self._get_route() - return route.get(MESSAGE_ROUTE_FROM) - - @property - def msg_to(self): - """Labels for the consumer to filter its subscribed messages.""" - route = self._get_route() - return route.get(MESSAGE_ROUTE_TO) - - def replace(self, old_val, new_val): - """Replace old value with new value""" - route = self._get_route() - tags = route.get(MESSAGE_ROUTE_TO, set()) - tags.discard(old_val) - tags.add(new_val) - route[MESSAGE_ROUTE_TO] = tags - - class Message(BaseModel): """list[: ]""" content: str - instruct_content: BaseModel = None - meta_info: Dict = Field(default_factory=dict) - route: Routes = Field(default_factory=Routes) + instruct_content: BaseModel = Field(default=None) + role: str = "user" # system / user / assistant + cause_by: str = "" + sent_from: str = "" + send_to: Set = Field(default_factory=set) - def __init__(self, content, **kwargs): + def __init__( + self, + content, + instruct_content=None, + role="user", + cause_by="", + sent_from="", + send_to=MESSAGE_ROUTE_TO_ALL, + **kwargs, + ): """ Parameters not listed below will be stored as meta info, including custom parameters. :param content: Message content. :param instruct_content: Message content struct. - :param meta_info: Message meta info. - :param route: Message route configuration. - :param msg_from: Message route info tells who sent this message. - :param msg_to: Labels for the consumer to filter its subscribed messages. - :param cause_by: Labels for the consumer to filter its subscribed messages, also serving as meta info. + :param cause_by: Message producer + :param sent_from: Message route info tells who sent this message. + :param send_to: Labels for the consumer to filter its subscribed messages. :param role: Message meta info tells who sent this message. """ - super(Message, self).__init__( - content=content or kwargs.get("content"), - instruct_content=kwargs.get("instruct_content"), - meta_info=kwargs.get("meta_info", {}), - route=kwargs.get("route", Routes()), + super().__init__( + content=content, + instruct_content=instruct_content, + role=role, + cause_by=any_to_str(cause_by), + sent_from=any_to_str(sent_from), + send_to=any_to_str_set(send_to), + **kwargs, ) - attribute_names = Message.__annotations__.keys() - for k, v in kwargs.items(): - if k in attribute_names: - continue - if k == MESSAGE_ROUTE_FROM: - self.set_from(any_to_str(v)) - continue - if k == MESSAGE_ROUTE_CAUSE_BY: - self.set_cause_by(v) - continue - if k == MESSAGE_ROUTE_TO: - if isinstance(v, tuple) or isinstance(v, list) or isinstance(v, set): - for i in v: - self.add_to(any_to_str(i)) - else: - self.add_to(any_to_str(v)) - continue - self.meta_info[k] = v - - def get_meta(self, key): - """Get meta info""" - return self.meta_info.get(key) - - def set_meta(self, key, value): - """Set meta info""" - self.meta_info[key] = value - - @property - def role(self): - """Message meta info tells who sent this message.""" - return self.get_meta(MESSAGE_META_ROLE) - - @property - def cause_by(self): - """Labels for the consumer to filter its subscribed messages, also serving as meta info.""" - return self.get_meta(MESSAGE_ROUTE_CAUSE_BY) - def __setattr__(self, key, val): - """Override `@property.setter`""" + """Override `@property.setter`, convert non-string parameters into string parameters.""" if key == MESSAGE_ROUTE_CAUSE_BY: - self.set_cause_by(val) - return - if key == MESSAGE_ROUTE_FROM: - self.set_from(any_to_str(val)) - super().__setattr__(key, val) - - def set_cause_by(self, val): - """Update the value of `cause_by` in the `meta_info` and `routes` attributes.""" - old_value = self.get_meta(MESSAGE_ROUTE_CAUSE_BY) - new_value = any_to_str(val) - self.set_meta(MESSAGE_ROUTE_CAUSE_BY, new_value) - self.route.replace(old_value, new_value) - - @property - def msg_from(self): - """Message route info tells who sent this message.""" - return self.route.msg_from - - @property - def msg_to(self): - """Labels for the consumer to filter its subscribed messages.""" - return self.route.msg_to - - def set_role(self, v): - """Set the message's meta info indicating the sender.""" - self.set_meta(MESSAGE_META_ROLE, v) - - def set_from(self, v): - """Set the message's meta info indicating the sender.""" - self.route.set_from(v) - - def set_to(self, tags: Set): - """Set the message's meta info indicating the sender.""" - self.route.set_to(tags) - - def add_to(self, tag: str): - """Add a subscription label for the recipients.""" - self.route.add_to(tag) - - def contain_any(self, tags: Set): - """Return true if any input label exists in the message's subscription labels.""" - return self.route.contain_any(tags) + new_val = any_to_str(val) + elif key == MESSAGE_ROUTE_FROM: + new_val = any_to_str(val) + elif key == MESSAGE_ROUTE_TO: + new_val = any_to_str_set(val) + else: + new_val = val + super().__setattr__(key, new_val) def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) @@ -226,13 +99,13 @@ class Message(BaseModel): return self.json(exclude_none=True) @staticmethod - def load(v): + def load(val): """Convert the json string to object.""" try: - d = json.loads(v) + d = json.loads(val) return Message(**d) except JSONDecodeError as err: - logger.error(f"parse json failed: {v}, error:{err}") + logger.error(f"parse json failed: {val}, error:{err}") return None @@ -327,31 +200,3 @@ class MessageQueue: logger.warning(f"JSON load failed: {v}, error:{e}") return q - - -if __name__ == "__main__": - m = Message("a", role="v1") - m.set_role("v2") - v = m.dump() - m = Message.load(v) - m.cause_by = "Message" - m.cause_by = Routes - m.cause_by = Routes() - m.content = "b" - - test_content = "test_message" - msgs = [ - UserMessage(test_content), - SystemMessage(test_content), - AIMessage(test_content), - Message(test_content, role="QA"), - ] - logger.info(msgs) - - jsons = [ - UserMessage(test_content).dump(), - SystemMessage(test_content).dump(), - AIMessage(test_content).dump(), - Message(test_content, role="QA").dump(), - ] - logger.info(jsons) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index b372f0d8d..cd42b1412 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -325,3 +325,14 @@ def any_to_str(val) -> str: return get_object_name(val) return get_class_name(val) + + +def any_to_str_set(val) -> set: + """Convert any type to string set.""" + res = set() + if isinstance(val, dict) or isinstance(val, list) or isinstance(val, set) or isinstance(val, tuple): + for i in val: + res.add(any_to_str(i)) + else: + res.add(any_to_str(val)) + return res diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 05127362b..51ebd5baa 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -12,7 +12,7 @@ import json import pytest from metagpt.actions import Action -from metagpt.schema import AIMessage, Message, Routes, SystemMessage, UserMessage +from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.utils.common import get_class_name @@ -37,20 +37,19 @@ def test_message(): d = json.loads(v) assert d assert d.get("content") == "a" - assert d.get("meta_info") == {"role": "v1"} - m.set_role("v2") + assert d.get("role") == "v1" + m.role = "v2" v = m.dump() assert v m = Message.load(v) assert m.content == "a" assert m.role == "v2" - m = Message("a", role="b", cause_by="c", x="d") + m = Message("a", role="b", cause_by="c", x="d", send_to="c") assert m.content == "a" assert m.role == "b" - assert m.contain_any({"c"}) + assert m.send_to == {"c"} assert m.cause_by == "c" - assert m.get_meta("x") == "d" m.cause_by = "Message" assert m.cause_by == "Message" @@ -64,18 +63,11 @@ def test_message(): @pytest.mark.asyncio def test_routes(): - route = Routes() - route.set_from("a") - assert route.msg_from == "a" - route.add_to("b") - assert route.msg_to == {"b"} - route.add_to("c") - assert route.msg_to == {"b", "c"} - route.set_to({"e", "f"}) - assert route.msg_to == {"e", "f"} - assert route.contain_any({"e"}) - assert route.contain_any({"f"}) - assert not route.contain_any({"a"}) + m = Message("a", role="b", cause_by="c", x="d", send_to="c") + m.send_to = "b" + assert m.send_to == {"b"} + m.send_to = {"e", Action} + assert m.send_to == {"e", get_class_name(Action)} if __name__ == "__main__": From 09fe4593f6621aa4689f9d4697711a1bc9851de9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 20:36:42 +0800 Subject: [PATCH 066/413] refactor: According to RFC 116: Updated the type of index key. --- metagpt/memory/memory.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index c6b732076..7f04be63d 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -26,10 +26,8 @@ class Memory: if message in self.storage: return self.storage.append(message) - # According to the design of RFC 116, it allows message filtering based on different labels, thus - # necessitating the creation of separate indices for each label. - for k in message.msg_to: - self.index[k].append(message) + if message.cause_by: + self.index[message.cause_by].append(message) def add_batch(self, messages: Iterable[Message]): for message in messages: From e5c792e51277677705d46716ad71bbe074e284cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 20:36:50 +0800 Subject: [PATCH 067/413] refactor: According to RFC 116: Updated the type of index key. --- metagpt/memory/memory.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 7f04be63d..84289091f 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -4,8 +4,7 @@ @Time : 2023/5/20 12:15 @Author : alexanderwu @File : memory.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - Updated the message filtering logic. +@Modified By: mashenquan, 2023-11-1. According to RFC 116: Updated the type of index key. """ from collections import defaultdict from typing import Iterable, Set From 47d47d274e5d0d7e806d387db169019df7d961ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 20:44:39 +0800 Subject: [PATCH 068/413] refactor: According to RFC 113, add message dispatching functionality. --- metagpt/environment.py | 3 ++- metagpt/utils/common.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index fb564e1ab..81b5c2ac7 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -19,6 +19,7 @@ from pydantic import BaseModel, Field from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message +from metagpt.utils.common import is_subscribed class Environment(BaseModel): @@ -63,7 +64,7 @@ class Environment(BaseModel): found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 for obj, subscribed_tags in self.consumers.items(): - if message.contain_any(subscribed_tags): + if is_subscribed(message, subscribed_tags): obj.put_message(message) found = True if not found: diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index cd42b1412..798acf214 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -15,6 +15,7 @@ import platform import re from typing import List, Tuple, Union +from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger @@ -336,3 +337,14 @@ def any_to_str_set(val) -> set: else: res.add(any_to_str(val)) return res + + +def is_subscribed(message, tags): + """Return whether it's consumer""" + if MESSAGE_ROUTE_TO_ALL in message.send_to: + return True + + for t in tags: + if t in message.send_to: + return True + return False From 7e83a4bb3159279855a5eca98f97efdbf468acfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 20:56:05 +0800 Subject: [PATCH 069/413] refactor: According to RFC 116: Updated the type of send_to. --- examples/debate.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index cf0c0124c..7b03f785b 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -60,7 +60,7 @@ class Trump(Role): async def _observe(self) -> int: await super()._observe() # accept messages sent (from opponent) to self, disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.contain_any({self.name})] + self._rc.news = [msg for msg in self._rc.news if msg.send_to == {self.name}] return len(self._rc.news) async def _act(self) -> Message: @@ -78,8 +78,8 @@ class Trump(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - msg_from=self.name, - msg_to=self.opponent_name, + sent_from=self.name, + send_to=self.opponent_name, ) return msg @@ -102,8 +102,7 @@ class Biden(Role): await super()._observe() # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, # disregard own messages from the last round - message_filter = {BossRequirement, self.name} - self._rc.news = [msg for msg in self._rc.news if msg.contain_any(message_filter)] + self._rc.news = [msg for msg in self._rc.news if msg.cause_by == BossRequirement or msg.send_to == {self.name}] return len(self._rc.news) async def _act(self) -> Message: @@ -121,8 +120,8 @@ class Biden(Role): content=rsp, role=self.profile, cause_by=ShoutOut, - msg_from=self.name, - msg_to=self.opponent_name, + sent_from=self.name, + send_to=self.opponent_name, ) return msg From 01f23d633ee2e688e7d423b448af810e6f9f1161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 21:38:52 +0800 Subject: [PATCH 070/413] refactor: In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `send_to` value of the `Message` object. --- examples/debate.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 7b03f785b..87ac7050f 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -2,9 +2,8 @@ Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message filtering. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `send_to` + value of the `Message` object. """ import asyncio import platform From c502b1403a6c2fb2e15f25040528873abb1eb869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 21:43:17 +0800 Subject: [PATCH 071/413] refactor: In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `cause_by` value of the `Message` object. --- metagpt/actions/write_code.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index f2a4744d9..be8690314 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -4,8 +4,8 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : write_code.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of - the `cause_by` value in the `Message` to a string to support the new message distribution feature. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `cause_by` + value of the `Message` object. """ from tenacity import retry, stop_after_attempt, wait_fixed @@ -14,7 +14,7 @@ from metagpt.actions.action import Action from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message -from metagpt.utils.common import CodeParser +from metagpt.utils.common import CodeParser, get_class_name PROMPT_TEMPLATE = """ NOTICE @@ -58,8 +58,7 @@ class WriteCode(Action): if self._is_invalid(filename): return - message_filter = {WriteDesign} - design = [i for i in context if i.contain_any(message_filter)][0] + design = [i for i in context if i.cause_by == get_class_name(WriteDesign)][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) ws_path = WORKSPACE_ROOT / ws_name From d9939f437ad10f3b87959fa615ad4168f0f4e1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 21:57:58 +0800 Subject: [PATCH 072/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 6e8c5e421..ac8a2d702 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -116,6 +116,9 @@ class Role: self._actions = [] self._role_id = str(self._setting) self._rc = RoleContext() + self._subscription = {get_object_name(self)} + if name: + self._subscription.add(name) def _reset(self): self._states = [] @@ -133,21 +136,15 @@ class Role: self._states.append(f"{idx}. {action}") def _watch(self, actions: Iterable[Type[Action]]): - """Listen to the corresponding behaviors""" + """Listen to the corresponding behaviors in private message buffer""" tags = {get_class_name(t) for t in actions} - # Add default subscription tags for developers' direct use. - if self.name: - tags.add(self.name) - tags.add(get_object_name(self)) - self.subscribe(tags) + self._rc.watch.update(tags) def subscribe(self, tags: Set[str]): """Listen to the corresponding behaviors""" - self._rc.watch.update(tags) - # check RoleContext after adding watch actions - self._rc.check(self._role_id) + self._subscription = tags if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - self._rc.env.set_subscribed_tags(self, self.subscribed_tags) + self._rc.env.set_subscribed_tags(self, self._subscription) def _set_state(self, state): """Update the current state.""" @@ -159,6 +156,8 @@ class Role: """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env + if env: + env.set_subscribed_tags(self, self._subscription) @property def profile(self): From d232725a2991a2e43da9e07094d0cbc23b7a6a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:01:55 +0800 Subject: [PATCH 073/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index ac8a2d702..5bc241352 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -170,17 +170,9 @@ class Role: return self._setting.name @property - def subscribed_tags(self) -> Set: + def subscription(self) -> Set: """The labels for messages to be consumed by the Role object.""" - if self._rc.watch: - return self._rc.watch - return { - self.name, - get_object_name(self), - self.profile, - f"{self.name}({self.profile})", - f"{self.name}({get_object_name(self)})", - } + return self._subscription def _get_prefix(self): """Get the role prefix""" From f47977daa81357b552cd0e791dc798826b45bf31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:03:06 +0800 Subject: [PATCH 074/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 5bc241352..32fa16e6a 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -139,6 +139,8 @@ class Role: """Listen to the corresponding behaviors in private message buffer""" tags = {get_class_name(t) for t in actions} self._rc.watch.update(tags) + # check RoleContext after adding watch actions + self._rc.check(self._role_id) def subscribe(self, tags: Set[str]): """Listen to the corresponding behaviors""" From 9cdbc0a0ae93c8295756b191faa05972075e9015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:06:46 +0800 Subject: [PATCH 075/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 32fa16e6a..5f54e57e0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -208,15 +208,9 @@ class Role: instruct_content=response.instruct_content, role=self.profile, cause_by=get_object_name(self._rc.todo), - msg_from=get_object_name(self), ) else: - msg = Message( - content=response, - role=self.profile, - cause_by=get_object_name(self._rc.todo), - msg_from=get_object_name(self), - ) + msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo)) return msg From d1977f15864bb2f8386766f184ddf14daf856325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:22:09 +0800 Subject: [PATCH 076/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 5f54e57e0..59342fa99 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -146,7 +146,7 @@ class Role: """Listen to the corresponding behaviors""" self._subscription = tags if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - self._rc.env.set_subscribed_tags(self, self._subscription) + self._rc.env.set_subscription(self, self._subscription) def _set_state(self, state): """Update the current state.""" From 7a2193c3d26aee1cc4c9aa9ed2c7702305770bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:22:44 +0800 Subject: [PATCH 077/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 59342fa99..f3e11b294 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -159,7 +159,7 @@ class Role: messages by observing.""" self._rc.env = env if env: - env.set_subscribed_tags(self, self._subscription) + env.set_subscription(self, self._subscription) @property def profile(self): From 1ff99b95acaecc95c35dda8f5cbad5d0e421dc89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:51:12 +0800 Subject: [PATCH 078/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/environment.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index 81b5c2ac7..e9a5c6467 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -41,8 +41,6 @@ class Environment(BaseModel): """ role.set_env(self) self.roles[role.profile] = role - # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - self.set_subscribed_tags(role, role.subscribed_tags) def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 @@ -63,8 +61,8 @@ class Environment(BaseModel): logger.info(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - for obj, subscribed_tags in self.consumers.items(): - if is_subscribed(message, subscribed_tags): + for obj, subscription in self.consumers.items(): + if is_subscribed(message, subscription): obj.put_message(message) found = True if not found: @@ -106,10 +104,10 @@ class Environment(BaseModel): return False return True - def get_subscribed_tags(self, obj): + def get_subscription(self, obj): """Get the labels for messages to be consumed by the object.""" return self.consumers.get(obj, {}) - def set_subscribed_tags(self, obj, tags): + def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" self.consumers[obj] = tags From c9f9c5c73e4386f4ea26dd49c65276c7e2d6eaaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 22:57:53 +0800 Subject: [PATCH 079/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/memory/longterm_memory.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index 2a4b604e0..6e23a79ae 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -40,10 +40,11 @@ class LongTermMemory(Memory): def add(self, message: Message): super(LongTermMemory, self).add(message) - if message.contain_any(self.rc.watch) and not self.msg_from_recover: - # currently, only add role's watching messages to its memory_storage - # and ignore adding messages from recover repeatedly - self.memory_storage.add(message) + for action in self.rc.watch: + if message.cause_by == action and not self.msg_from_recover: + # currently, only add role's watching messages to its memory_storage + # and ignore adding messages from recover repeatedly + self.memory_storage.add(message) def find_news(self, observed: list[Message], k=0) -> list[Message]: """ From 894a2fd593734c7e8611b0033304884dac6d9397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 23:36:56 +0800 Subject: [PATCH 080/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/memory/longterm_memory.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index 6e23a79ae..6fc8050ef 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -2,8 +2,6 @@ # -*- coding: utf-8 -*- """ @Desc : the implement of Long-term memory -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - 1. Replace code related to message filtering with the `Message.contain_any` function. """ from metagpt.logs import logger From be19d9edcb88e6aa0ca2b8b7980f20191a903359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 23:49:47 +0800 Subject: [PATCH 081/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/engineer.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 882cf89dd..03519e0ef 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -21,7 +21,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, get_object_name +from metagpt.utils.common import CodeParser, get_class_name, get_object_name from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -170,7 +170,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - msg_to="QaEngineer", + send_to="QaEngineer", ) return msg @@ -185,8 +185,7 @@ class Engineer(Role): TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ context = [] - msg_filters = [WriteDesign, WriteTasks, WriteCode] - msg = self._rc.memory.get_by_actions(msg_filters) + msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) for m in msg: context.append(m.content) context_str = "\n".join(context) @@ -213,7 +212,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - msg_to="QaEngineer", + send_to="QaEngineer", ) return msg @@ -231,9 +230,8 @@ class Engineer(Role): return ret # Parse task lists - message_filter = {WriteTasks} for message in self._rc.news: - if not message.contain_any(message_filter): + if not message.cause_by == get_class_name(WriteTasks): continue self.todos = self.parse_tasks(message) From fba70452f3c8aba12699eb2cbf7e5a62f7655ae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 23:51:28 +0800 Subject: [PATCH 082/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/engineer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 03519e0ef..000e81873 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -239,7 +239,7 @@ class Engineer(Role): async def _think(self) -> None: # In asynchronous scenarios, first check if the required messages are ready. - filters = {WriteTasks} + filters = {get_class_name(WriteTasks)} msgs = self._rc.memory.get_by_actions(filters) if not msgs: self._rc.todo = None From ac32cb5a67934c117108ebdde6d718b2206c51f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 8 Nov 2023 23:54:03 +0800 Subject: [PATCH 083/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/engineer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 000e81873..423fff68e 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -19,7 +19,7 @@ from pathlib import Path from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger -from metagpt.roles import Role +from metagpt.roles import QaEngineer, Role from metagpt.schema import Message from metagpt.utils.common import CodeParser, get_class_name, get_object_name from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -170,7 +170,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - send_to="QaEngineer", + send_to=QaEngineer, ) return msg @@ -212,7 +212,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - send_to="QaEngineer", + send_to=QaEngineer, ) return msg From df4ff5f701daf29b802381efe3a00e7080a3e447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 00:01:51 +0800 Subject: [PATCH 084/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/roles/qa_engineer.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 104aa3dfb..760b65736 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -22,7 +22,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, parse_recipient +from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -100,8 +100,8 @@ class QaEngineer(Role): content=str(file_info), role=self.profile, cause_by=WriteTest, - msg_from=self.profile, - msg_to=self.profile, + sent_from=self.profile, + send_to=self.profile, ) self.publish_message(msg) @@ -133,7 +133,7 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, msg_from=self.profile, msg_to=recipient) + msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): @@ -146,15 +146,15 @@ class QaEngineer(Role): content=file_info, role=self.profile, cause_by=DebugError, - msg_from=self.profile, - msg_to=recipient, + sent_from=self.profile, + send_to=recipient, ) self.publish_message(msg) async def _observe(self) -> int: await super()._observe() self._rc.news = [ - msg for msg in self._rc.news if msg.contain_any({self.profile}) + msg for msg in self._rc.news if self.profile in msg.send_to ] # only relevant msgs count as observed news return len(self._rc.news) @@ -164,23 +164,23 @@ class QaEngineer(Role): content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, cause_by=WriteTest, - msg_from=self.profile, + sent_from=self.profile, ) return result_msg - code_filters = {WriteCode, WriteCodeReview} - test_filters = {WriteTest, DebugError} - run_filters = {RunCode} + code_filters = any_to_str_set({WriteCode, WriteCodeReview}) + test_filters = any_to_str_set({WriteTest, DebugError}) + run_filters = any_to_str_set({RunCode}) for msg in self._rc.news: # Decide what to do based on observed msg type, currently defined by human, # might potentially be moved to _think, that is, let the agent decides for itself - if msg.contain_any(code_filters): + if msg.cause_by in code_filters: # engineer wrote a code, time to write a test for it await self._write_test(msg) - elif msg.contain_any(test_filters): + elif msg.cause_by in test_filters: # I wrote or debugged my test code, time to run it await self._run_code(msg) - elif msg.contain_any(run_filters): + elif msg.cause_by in run_filters: # I ran my test code, time to fix bugs, if any await self._debug_error(msg) self.test_round += 1 @@ -188,6 +188,6 @@ class QaEngineer(Role): content=f"Round {self.test_round} of tests done", role=self.profile, cause_by=WriteTest, - msg_from=self.profile, + sent_from=self.profile, ) return result_msg From c4ac0c72d7053a67e1c0679fa79582862328507c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 00:41:29 +0800 Subject: [PATCH 085/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/software_company.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 1b6936870..d12998242 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -48,14 +48,7 @@ class SoftwareCompany(BaseModel): def start_project(self, idea): """Start a project from publishing boss requirement.""" self.idea = idea - self.environment.publish_message( - Message( - role="BOSS", - content=idea, - cause_by=BossRequirement, - msg_from=SoftwareCompany, - ) - ) + self.environment.publish_message(Message(role="BOSS", content=idea, cause_by=BossRequirement)) def _save(self): logger.info(self.json()) @@ -68,3 +61,4 @@ class SoftwareCompany(BaseModel): logger.debug(f"{n_round=}") self._check_balance() await self.environment.run() + return self.environment.history From bb050142f708557785e80ea16eff5ca3b17aed56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 00:42:52 +0800 Subject: [PATCH 086/413] refactor: Update according to Chapter 2.1.3.2 of RFC 116 --- metagpt/software_company.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index d12998242..d3c2c463b 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -4,8 +4,6 @@ @Time : 2023/5/12 00:30 @Author : alexanderwu @File : software_company.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116: - 1. Abandon the design of having `Environment` store all messages. """ from pydantic import BaseModel, Field From 7504ed57570152aaa23f196ef9945fa5f552ee98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 10:02:26 +0800 Subject: [PATCH 087/413] fixbug: recursive import --- metagpt/roles/engineer.py | 6 +++--- metagpt/schema.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 423fff68e..ba622429b 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -19,7 +19,7 @@ from pathlib import Path from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger -from metagpt.roles import QaEngineer, Role +from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import CodeParser, get_class_name, get_object_name from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -170,7 +170,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - send_to=QaEngineer, + send_to="Edward", ) return msg @@ -212,7 +212,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=get_object_name(self._rc.todo), - send_to=QaEngineer, + send_to="Edward", ) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 7fdcef2ed..63fe41232 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -40,7 +40,7 @@ class Message(BaseModel): role: str = "user" # system / user / assistant cause_by: str = "" sent_from: str = "" - send_to: Set = Field(default_factory=set) + send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL}) def __init__( self, From ea9875a7fc63b79181983bc8821cd1ab28be20dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 11:14:05 +0800 Subject: [PATCH 088/413] fixbug: recursive import --- examples/debate.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 87ac7050f..8f5012d66 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -3,7 +3,7 @@ Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `send_to` - value of the `Message` object. + value of the `Message` object; modify the argument type of `get_by_actions`. """ import asyncio import platform @@ -15,6 +15,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.software_company import SoftwareCompany +from metagpt.utils.common import any_to_str_set class ShoutOut(Action): @@ -65,7 +66,7 @@ class Trump(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([ShoutOut]) + msg_history = self._rc.memory.get_by_actions(any_to_str_set([ShoutOut])) context = [] for m in msg_history: context.append(str(m)) @@ -107,7 +108,7 @@ class Biden(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut]) + msg_history = self._rc.memory.get_by_actions(any_to_str_set([BossRequirement, ShoutOut])) context = [] for m in msg_history: context.append(str(m)) From ff11cf69afa2872673a42d0910a591a3400a200a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 11:21:20 +0800 Subject: [PATCH 089/413] fixbug: utilize the new message filtering feature --- metagpt/roles/engineer.py | 13 +++++++++---- metagpt/roles/qa_engineer.py | 9 +++++++-- metagpt/roles/role.py | 6 ++++-- requirements.txt | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index ba622429b..62e1e92d2 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -21,7 +21,12 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, get_class_name, get_object_name +from metagpt.utils.common import ( + CodeParser, + any_to_str_set, + get_class_name, + get_object_name, +) from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -102,7 +107,7 @@ class Engineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] + msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -130,7 +135,7 @@ class Engineer(Role): todo_coros = [] for todo in self.todos: todo_coro = WriteCode().run( - context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), + context=self._rc.memory.get_by_actions(any_to_str_set([WriteTasks, WriteDesign])), filename=todo, ) todo_coros.append(todo_coro) @@ -185,7 +190,7 @@ class Engineer(Role): TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ context = [] - msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) + msg = self._rc.memory.get_by_actions(any_to_str_set([WriteDesign, WriteTasks, WriteCode])) for m in msg: context.append(m.content) context_str = "\n".join(context) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 760b65736..38fb5a24b 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -22,7 +22,12 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient +from metagpt.utils.common import ( + CodeParser, + any_to_str_set, + get_class_name, + parse_recipient, +) from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -50,7 +55,7 @@ class QaEngineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self, return_proj_dir=True) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] + msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index f3e11b294..b8be309bb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -217,9 +217,11 @@ class Role: async def _observe(self) -> int: """Prepare new messages for processing from the message buffer and other sources.""" # Read unprocessed messages from the msg buffer. - self._rc.news = self._rc.msg_buffer.pop_all() + news = self._rc.msg_buffer.pop_all() # Store the read messages in your own memory to prevent duplicate processing. - self._rc.memory.add_batch(self._rc.news) + self._rc.memory.add_batch(news) + # Filter out messages of interest. + self._rc.news = [n for n in news if n.cause_by in self._rc.watch] # Design Rules: # If you need to further categorize Message objects, you can do so using the Message.set_meta function. diff --git a/requirements.txt b/requirements.txt index 24a2d94c3..c3b909e77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ langchain==0.0.231 loguru==0.6.0 meilisearch==0.21.0 numpy==1.24.3 -openai +openai==0.28.1 openpyxl beautifulsoup4==4.12.2 pandas==2.0.3 From 1be1bb56e3558a257e331e759cd71aa0a7b755eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 9 Nov 2023 11:52:34 +0800 Subject: [PATCH 090/413] fixbug: utilize the new message filtering feature --- metagpt/roles/engineer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 62e1e92d2..a108fa4f1 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -239,8 +239,9 @@ class Engineer(Role): if not message.cause_by == get_class_name(WriteTasks): continue self.todos = self.parse_tasks(message) + return 1 - return ret + return 0 async def _think(self) -> None: # In asynchronous scenarios, first check if the required messages are ready. From a3cb2b4fdcaa47f48704cbfead054b89f79cd3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 15:27:27 +0800 Subject: [PATCH 091/413] feat: replace get_class_name and get_object_name --- metagpt/roles/engineer.py | 23 +++++++++-------------- metagpt/roles/researcher.py | 10 ++++------ metagpt/roles/role.py | 10 +++++----- metagpt/roles/seacher.py | 6 +++--- metagpt/roles/sk_agent.py | 4 ++-- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index a108fa4f1..70dce41b1 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -21,12 +21,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import ( - CodeParser, - any_to_str_set, - get_class_name, - get_object_name, -) +from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -107,7 +102,7 @@ class Engineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1] + msg = self._rc.memory.get_by_action(any_to_str(WriteDesign))[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -146,13 +141,13 @@ class Engineer(Role): logger.info(todo) logger.info(code_rsp) # self.write_file(todo, code) - msg = Message(content=code_rsp, role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content=code_rsp, role=self.profile, cause_by=any_to_str(self._rc.todo)) self._rc.memory.add(msg) self.publish_message(msg) del self.todos[0] logger.info(f"Done {self.get_workspace()} generating.") - msg = Message(content="all done.", role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content="all done.", role=self.profile, cause_by=any_to_str(self._rc.todo)) return msg async def _act_sp(self) -> Message: @@ -163,7 +158,7 @@ class Engineer(Role): # logger.info(code_rsp) # code = self.parse_code(code_rsp) file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content=code, role=self.profile, cause_by=any_to_str(self._rc.todo)) self._rc.memory.add(msg) self.publish_message(msg) @@ -174,7 +169,7 @@ class Engineer(Role): msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, - cause_by=get_object_name(self._rc.todo), + cause_by=any_to_str(self._rc.todo), send_to="Edward", ) return msg @@ -216,7 +211,7 @@ class Engineer(Role): msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, - cause_by=get_object_name(self._rc.todo), + cause_by=any_to_str(self._rc.todo), send_to="Edward", ) return msg @@ -236,7 +231,7 @@ class Engineer(Role): # Parse task lists for message in self._rc.news: - if not message.cause_by == get_class_name(WriteTasks): + if not message.cause_by == any_to_str(WriteTasks): continue self.todos = self.parse_tasks(message) return 1 @@ -245,7 +240,7 @@ class Engineer(Role): async def _think(self) -> None: # In asynchronous scenarios, first check if the required messages are ready. - filters = {get_class_name(WriteTasks)} + filters = {any_to_str(WriteTasks)} msgs = self._rc.memory.get_by_actions(filters) if not msgs: self._rc.todo = None diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 4ec6f31e1..8d5e43fab 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -15,7 +15,7 @@ from metagpt.const import RESEARCH_PATH from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_object_name +from metagpt.utils.common import any_to_str class Report(BaseModel): @@ -64,21 +64,19 @@ class Researcher(Role): research_system_text = get_research_system_text(topic, self.language) if isinstance(todo, CollectLinks): links = await todo.run(topic, 4, 4) - ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=get_object_name(todo)) + ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=any_to_str(todo)) elif isinstance(todo, WebBrowseAndSummarize): links = instruct_content.links todos = (todo.run(*url, query=query, system_text=research_system_text) for (query, url) in links.items()) summaries = await asyncio.gather(*todos) summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary) - ret = Message( - "", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=get_object_name(todo) - ) + ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=any_to_str(todo)) else: summaries = instruct_content.summaries summary_text = "\n---\n".join(f"url: {url}\nsummary: {summary}" for (url, summary) in summaries) content = await self._rc.todo.run(topic, summary_text, system_text=research_system_text) ret = Message( - "", Report(topic=topic, content=content), role=self.profile, cause_by=get_object_name(self._rc.todo) + "", Report(topic=topic, content=content), role=self.profile, cause_by=any_to_str(self._rc.todo) ) self._rc.memory.add(ret) return ret diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b8be309bb..90e85186b 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -30,7 +30,7 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import LongTermMemory, Memory from metagpt.schema import Message, MessageQueue -from metagpt.utils.common import get_class_name, get_object_name +from metagpt.utils.common import any_to_str PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -116,7 +116,7 @@ class Role: self._actions = [] self._role_id = str(self._setting) self._rc = RoleContext() - self._subscription = {get_object_name(self)} + self._subscription = {any_to_str(self)} if name: self._subscription.add(name) @@ -137,7 +137,7 @@ class Role: def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors in private message buffer""" - tags = {get_class_name(t) for t in actions} + tags = {any_to_str(t) for t in actions} self._rc.watch.update(tags) # check RoleContext after adding watch actions self._rc.check(self._role_id) @@ -207,10 +207,10 @@ class Role: content=response.content, instruct_content=response.instruct_content, role=self.profile, - cause_by=get_object_name(self._rc.todo), + cause_by=any_to_str(self._rc.todo), ) else: - msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo)) return msg diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index d0b841f39..a37143196 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -12,7 +12,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.tools import SearchEngineType -from metagpt.utils.common import get_object_name +from metagpt.utils.common import any_to_str class Searcher(Role): @@ -64,10 +64,10 @@ class Searcher(Role): content=response.content, instruct_content=response.instruct_content, role=self.profile, - cause_by=get_object_name(self._rc.todo), + cause_by=any_to_str(self._rc.todo), ) else: - msg = Message(content=response, role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo)) self._rc.memory.add(msg) return msg diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 5b8d333bd..bb923caf2 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -17,7 +17,7 @@ from metagpt.actions.execute_task import ExecuteTask from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_object_name +from metagpt.utils.common import any_to_str from metagpt.utils.make_sk_kernel import make_sk_kernel @@ -74,7 +74,7 @@ class SkAgent(Role): result = (await self.plan.invoke_async()).result logger.info(result) - msg = Message(content=result, role=self.profile, cause_by=get_object_name(self._rc.todo)) + msg = Message(content=result, role=self.profile, cause_by=any_to_str(self._rc.todo)) self._rc.memory.add(msg) self.publish_message(msg) return msg From d36b4e2088c4a2e48c4f1ab63fc9c41c163bbaf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 15:41:27 +0800 Subject: [PATCH 092/413] refactor: replace obj with role --- metagpt/environment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index e9a5c6467..b3c296dac 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -61,9 +61,9 @@ class Environment(BaseModel): logger.info(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - for obj, subscription in self.consumers.items(): + for role, subscription in self.consumers.items(): if is_subscribed(message, subscription): - obj.put_message(message) + role.put_message(message) found = True if not found: logger.warning(f"Message no recipients: {message.dump()}") From 3c38c5c41678f64a43f430ab215618567b98471b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 15:55:33 +0800 Subject: [PATCH 093/413] refactor: get_class_name --- metagpt/actions/write_code.py | 4 ++-- metagpt/memory/memory.py | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index be8690314..aeaa10aec 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -14,7 +14,7 @@ from metagpt.actions.action import Action from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message -from metagpt.utils.common import CodeParser, get_class_name +from metagpt.utils.common import CodeParser, any_to_str PROMPT_TEMPLATE = """ NOTICE @@ -58,7 +58,7 @@ class WriteCode(Action): if self._is_invalid(filename): return - design = [i for i in context if i.cause_by == get_class_name(WriteDesign)][0] + design = [i for i in context if i.cause_by == any_to_str(WriteDesign)][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) ws_path = WORKSPACE_ROOT / ws_name diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 84289091f..9d526420f 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -10,6 +10,7 @@ from collections import defaultdict from typing import Iterable, Set from metagpt.schema import Message +from metagpt.utils.common import any_to_str, any_to_str_set class Memory: @@ -73,14 +74,16 @@ class Memory: news.append(i) return news - def get_by_action(self, action: str) -> list[Message]: + def get_by_action(self, action) -> list[Message]: """Return all messages triggered by a specified Action""" - return self.index[action] + idx = any_to_str(action) + return self.index[idx] - def get_by_actions(self, actions: Set[str]) -> list[Message]: + def get_by_actions(self, actions: Set) -> list[Message]: """Return all messages triggered by specified Actions""" + idxs = any_to_str_set(actions) rsp = [] - for action in actions: + for action in idxs: if action not in self.index: continue rsp += self.index[action] From 710bc40b0ab49e1e5c9331a7175487aab68b9db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 15:58:47 +0800 Subject: [PATCH 094/413] refactor: get_class_name --- metagpt/memory/memory.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 9d526420f..2f4c9d20b 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -10,7 +10,6 @@ from collections import defaultdict from typing import Iterable, Set from metagpt.schema import Message -from metagpt.utils.common import any_to_str, any_to_str_set class Memory: @@ -76,14 +75,12 @@ class Memory: def get_by_action(self, action) -> list[Message]: """Return all messages triggered by a specified Action""" - idx = any_to_str(action) - return self.index[idx] + return self.index[action] def get_by_actions(self, actions: Set) -> list[Message]: """Return all messages triggered by specified Actions""" - idxs = any_to_str_set(actions) rsp = [] - for action in idxs: + for action in actions: if action not in self.index: continue rsp += self.index[action] From 60bad1830482b69dfb4ac92b128f172d19242e9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 15:59:50 +0800 Subject: [PATCH 095/413] refactor: get_class_name --- metagpt/memory/memory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 2f4c9d20b..71d999049 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -73,7 +73,7 @@ class Memory: news.append(i) return news - def get_by_action(self, action) -> list[Message]: + def get_by_action(self, action: str) -> list[Message]: """Return all messages triggered by a specified Action""" return self.index[action] From 83a5e03b72168714c277633e53e2f16dc0f57345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:05:12 +0800 Subject: [PATCH 096/413] refactor: get_class_name --- metagpt/roles/engineer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 70dce41b1..742e00cc8 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -81,7 +81,7 @@ class Engineer(Role): self.use_code_review = use_code_review if self.use_code_review: self._init_actions([WriteCode, WriteCodeReview]) - self._watch([WriteTasks, WriteDesign]) + self._watch([WriteTasks]) self.todos = [] self.n_borg = n_borg From a61f3f80e97a2265120d15195036fbb8ccf4b370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:15:07 +0800 Subject: [PATCH 097/413] refactor: get_by_action(s) --- examples/debate.py | 5 ++--- metagpt/memory/memory.py | 9 ++++++--- metagpt/roles/engineer.py | 11 +++++------ metagpt/roles/qa_engineer.py | 11 +++-------- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 8f5012d66..630f78cd8 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -15,7 +15,6 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.software_company import SoftwareCompany -from metagpt.utils.common import any_to_str_set class ShoutOut(Action): @@ -66,7 +65,7 @@ class Trump(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions(any_to_str_set([ShoutOut])) + msg_history = self._rc.memory.get_by_actions([ShoutOut]) context = [] for m in msg_history: context.append(str(m)) @@ -108,7 +107,7 @@ class Biden(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - msg_history = self._rc.memory.get_by_actions(any_to_str_set([BossRequirement, ShoutOut])) + msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut]) context = [] for m in msg_history: context.append(str(m)) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 71d999049..53b65fcf7 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -10,6 +10,7 @@ from collections import defaultdict from typing import Iterable, Set from metagpt.schema import Message +from metagpt.utils.common import any_to_str, any_to_str_set class Memory: @@ -73,14 +74,16 @@ class Memory: news.append(i) return news - def get_by_action(self, action: str) -> list[Message]: + def get_by_action(self, action) -> list[Message]: """Return all messages triggered by a specified Action""" - return self.index[action] + index = any_to_str(action) + return self.index[index] def get_by_actions(self, actions: Set) -> list[Message]: """Return all messages triggered by specified Actions""" rsp = [] - for action in actions: + indices = any_to_str_set(actions) + for action in indices: if action not in self.index: continue rsp += self.index[action] diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 742e00cc8..960f9c0f3 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -21,7 +21,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set +from metagpt.utils.common import CodeParser, any_to_str from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -102,7 +102,7 @@ class Engineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(any_to_str(WriteDesign))[-1] + msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -130,7 +130,7 @@ class Engineer(Role): todo_coros = [] for todo in self.todos: todo_coro = WriteCode().run( - context=self._rc.memory.get_by_actions(any_to_str_set([WriteTasks, WriteDesign])), + context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), filename=todo, ) todo_coros.append(todo_coro) @@ -185,7 +185,7 @@ class Engineer(Role): TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ context = [] - msg = self._rc.memory.get_by_actions(any_to_str_set([WriteDesign, WriteTasks, WriteCode])) + msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) for m in msg: context.append(m.content) context_str = "\n".join(context) @@ -240,8 +240,7 @@ class Engineer(Role): async def _think(self) -> None: # In asynchronous scenarios, first check if the required messages are ready. - filters = {any_to_str(WriteTasks)} - msgs = self._rc.memory.get_by_actions(filters) + msgs = self._rc.memory.get_by_actions({WriteTasks}) if not msgs: self._rc.todo = None return diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 38fb5a24b..9495e1a12 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -22,12 +22,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import ( - CodeParser, - any_to_str_set, - get_class_name, - parse_recipient, -) +from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set, parse_recipient from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -55,7 +50,7 @@ class QaEngineer(Role): return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) def get_workspace(self, return_proj_dir=True) -> Path: - msg = self._rc.memory.get_by_action(get_class_name(WriteDesign))[-1] + msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: return WORKSPACE_ROOT / "src" workspace = self.parse_workspace(msg) @@ -104,7 +99,7 @@ class QaEngineer(Role): msg = Message( content=str(file_info), role=self.profile, - cause_by=WriteTest, + cause_by=any_to_str(WriteTest), sent_from=self.profile, send_to=self.profile, ) From bb8e2467ea6d0405cf42da1850b85962b3570915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:22:02 +0800 Subject: [PATCH 098/413] refactor: cause_by --- metagpt/roles/qa_engineer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 9495e1a12..0f932ebfb 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -133,7 +133,9 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient) + msg = Message( + content=content, role=self.profile, cause_by=any_to_str(RunCode), sent_from=self.profile, send_to=recipient + ) self.publish_message(msg) async def _debug_error(self, msg): @@ -145,7 +147,7 @@ class QaEngineer(Role): msg = Message( content=file_info, role=self.profile, - cause_by=DebugError, + cause_by=any_to_str(DebugError), sent_from=self.profile, send_to=recipient, ) @@ -163,7 +165,7 @@ class QaEngineer(Role): result_msg = Message( content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, - cause_by=WriteTest, + cause_by=any_to_str(WriteTest), sent_from=self.profile, ) return result_msg @@ -187,7 +189,7 @@ class QaEngineer(Role): result_msg = Message( content=f"Round {self.test_round} of tests done", role=self.profile, - cause_by=WriteTest, + cause_by=any_to_str(WriteTest), sent_from=self.profile, ) return result_msg From fc63cdf4df1fdf6e4bf1ed3ecff8a58a9ad0a098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:24:09 +0800 Subject: [PATCH 099/413] refactor: cause_by --- metagpt/roles/role.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 90e85186b..a0a35bdc2 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -116,9 +116,7 @@ class Role: self._actions = [] self._role_id = str(self._setting) self._rc = RoleContext() - self._subscription = {any_to_str(self)} - if name: - self._subscription.add(name) + self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} def _reset(self): self._states = [] From 9ebd1d1bbb3ed55564cde68396624120f6d9dec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:28:29 +0800 Subject: [PATCH 100/413] refactor: notation --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index a0a35bdc2..75e41d4ae 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -134,7 +134,7 @@ class Role: self._states.append(f"{idx}. {action}") def _watch(self, actions: Iterable[Type[Action]]): - """Listen to the corresponding behaviors in private message buffer""" + """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe.""" tags = {any_to_str(t) for t in actions} self._rc.watch.update(tags) # check RoleContext after adding watch actions From b1a14d057a6c5af98c624881db0590928bd04b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:29:26 +0800 Subject: [PATCH 101/413] refactor: notation --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 75e41d4ae..ec6d71684 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -141,7 +141,7 @@ class Role: self._rc.check(self._role_id) def subscribe(self, tags: Set[str]): - """Listen to the corresponding behaviors""" + """Used to receive Messages with certain tags from the environment. Message will be put into personal message buffer to be further processed in _observe. By default, a Role subscribes Messages with a tag of its own name or profile.""" self._subscription = tags if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 self._rc.env.set_subscription(self, self._subscription) From e8eeb6cda97c26ac0b55e854c5d67910a6f218da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:29:41 +0800 Subject: [PATCH 102/413] refactor: notation --- metagpt/roles/role.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index ec6d71684..4201b0f92 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -134,14 +134,17 @@ class Role: self._states.append(f"{idx}. {action}") def _watch(self, actions: Iterable[Type[Action]]): - """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe.""" + """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message + buffer during _observe.""" tags = {any_to_str(t) for t in actions} self._rc.watch.update(tags) # check RoleContext after adding watch actions self._rc.check(self._role_id) def subscribe(self, tags: Set[str]): - """Used to receive Messages with certain tags from the environment. Message will be put into personal message buffer to be further processed in _observe. By default, a Role subscribes Messages with a tag of its own name or profile.""" + """Used to receive Messages with certain tags from the environment. Message will be put into personal message + buffer to be further processed in _observe. By default, a Role subscribes Messages with a tag of its own name + or profile.""" self._subscription = tags if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 self._rc.env.set_subscription(self, self._subscription) From efe6ead27c263afc2a39b2ba7b0a65c6376458bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:30:23 +0800 Subject: [PATCH 103/413] refactor: notation --- metagpt/roles/role.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 4201b0f92..ccad0b018 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -135,7 +135,8 @@ class Role: def _watch(self, actions: Iterable[Type[Action]]): """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message - buffer during _observe.""" + buffer during _observe. + """ tags = {any_to_str(t) for t in actions} self._rc.watch.update(tags) # check RoleContext after adding watch actions @@ -144,7 +145,8 @@ class Role: def subscribe(self, tags: Set[str]): """Used to receive Messages with certain tags from the environment. Message will be put into personal message buffer to be further processed in _observe. By default, a Role subscribes Messages with a tag of its own name - or profile.""" + or profile. + """ self._subscription = tags if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 self._rc.env.set_subscription(self, self._subscription) From 44aa1dd563d04957f084b5b4a91c7105533eb002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:48:34 +0800 Subject: [PATCH 104/413] refactor: cause_by --- examples/debate.py | 5 ++++- metagpt/roles/engineer.py | 10 +++++----- metagpt/roles/qa_engineer.py | 14 ++++++-------- metagpt/roles/researcher.py | 9 +++------ metagpt/roles/role.py | 4 ++-- metagpt/roles/seacher.py | 5 ++--- 6 files changed, 22 insertions(+), 25 deletions(-) diff --git a/examples/debate.py b/examples/debate.py index 630f78cd8..597b44e8d 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -15,6 +15,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.software_company import SoftwareCompany +from metagpt.utils.common import any_to_str class ShoutOut(Action): @@ -101,7 +102,9 @@ class Biden(Role): await super()._observe() # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, # disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.cause_by == BossRequirement or msg.send_to == {self.name}] + self._rc.news = [ + msg for msg in self._rc.news if msg.cause_by == any_to_str(BossRequirement) or msg.send_to == {self.name} + ] return len(self._rc.news) async def _act(self) -> Message: diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 960f9c0f3..535a1e27f 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -141,13 +141,13 @@ class Engineer(Role): logger.info(todo) logger.info(code_rsp) # self.write_file(todo, code) - msg = Message(content=code_rsp, role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content=code_rsp, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) self.publish_message(msg) del self.todos[0] logger.info(f"Done {self.get_workspace()} generating.") - msg = Message(content="all done.", role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content="all done.", role=self.profile, cause_by=self._rc.todo) return msg async def _act_sp(self) -> Message: @@ -158,7 +158,7 @@ class Engineer(Role): # logger.info(code_rsp) # code = self.parse_code(code_rsp) file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content=code, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) self.publish_message(msg) @@ -169,7 +169,7 @@ class Engineer(Role): msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, - cause_by=any_to_str(self._rc.todo), + cause_by=self._rc.todo, send_to="Edward", ) return msg @@ -211,7 +211,7 @@ class Engineer(Role): msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, - cause_by=any_to_str(self._rc.todo), + cause_by=self._rc.todo, send_to="Edward", ) return msg diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 0f932ebfb..760b65736 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -22,7 +22,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import CodeParser, any_to_str, any_to_str_set, parse_recipient +from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -99,7 +99,7 @@ class QaEngineer(Role): msg = Message( content=str(file_info), role=self.profile, - cause_by=any_to_str(WriteTest), + cause_by=WriteTest, sent_from=self.profile, send_to=self.profile, ) @@ -133,9 +133,7 @@ class QaEngineer(Role): recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message( - content=content, role=self.profile, cause_by=any_to_str(RunCode), sent_from=self.profile, send_to=recipient - ) + msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): @@ -147,7 +145,7 @@ class QaEngineer(Role): msg = Message( content=file_info, role=self.profile, - cause_by=any_to_str(DebugError), + cause_by=DebugError, sent_from=self.profile, send_to=recipient, ) @@ -165,7 +163,7 @@ class QaEngineer(Role): result_msg = Message( content=f"Exceeding {self.test_round_allowed} rounds of tests, skip (writing code counts as a round, too)", role=self.profile, - cause_by=any_to_str(WriteTest), + cause_by=WriteTest, sent_from=self.profile, ) return result_msg @@ -189,7 +187,7 @@ class QaEngineer(Role): result_msg = Message( content=f"Round {self.test_round} of tests done", role=self.profile, - cause_by=any_to_str(WriteTest), + cause_by=WriteTest, sent_from=self.profile, ) return result_msg diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index 8d5e43fab..29889b8ec 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -15,7 +15,6 @@ from metagpt.const import RESEARCH_PATH from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import any_to_str class Report(BaseModel): @@ -64,20 +63,18 @@ class Researcher(Role): research_system_text = get_research_system_text(topic, self.language) if isinstance(todo, CollectLinks): links = await todo.run(topic, 4, 4) - ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=any_to_str(todo)) + ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=todo) elif isinstance(todo, WebBrowseAndSummarize): links = instruct_content.links todos = (todo.run(*url, query=query, system_text=research_system_text) for (query, url) in links.items()) summaries = await asyncio.gather(*todos) summaries = list((url, summary) for i in summaries for (url, summary) in i.items() if summary) - ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=any_to_str(todo)) + ret = Message("", Report(topic=topic, summaries=summaries), role=self.profile, cause_by=todo) else: summaries = instruct_content.summaries summary_text = "\n---\n".join(f"url: {url}\nsummary: {summary}" for (url, summary) in summaries) content = await self._rc.todo.run(topic, summary_text, system_text=research_system_text) - ret = Message( - "", Report(topic=topic, content=content), role=self.profile, cause_by=any_to_str(self._rc.todo) - ) + ret = Message("", Report(topic=topic, content=content), role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(ret) return ret diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index ccad0b018..5c512b0f0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -210,10 +210,10 @@ class Role: content=response.content, instruct_content=response.instruct_content, role=self.profile, - cause_by=any_to_str(self._rc.todo), + cause_by=self._rc.todo, ) else: - msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) return msg diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index a37143196..587698d1d 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -12,7 +12,6 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.tools import SearchEngineType -from metagpt.utils.common import any_to_str class Searcher(Role): @@ -64,10 +63,10 @@ class Searcher(Role): content=response.content, instruct_content=response.instruct_content, role=self.profile, - cause_by=any_to_str(self._rc.todo), + cause_by=self._rc.todo, ) else: - msg = Message(content=response, role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) return msg From 7fb33fd890a3a19ab46f60d2ac9df152e75278b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 16:56:50 +0800 Subject: [PATCH 105/413] refactor: cause_by --- metagpt/roles/sk_agent.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index bb923caf2..15b18dd3e 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -4,9 +4,8 @@ @Time : 2023/9/13 12:23 @Author : femto Zheng @File : sk_agent.py -@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data - type of the `cause_by` value in the `Message` to a string, and utilize the new message distribution - feature for message filtering. +@Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, utilize the new message + distribution feature for message filtering. """ from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner @@ -17,7 +16,6 @@ from metagpt.actions.execute_task import ExecuteTask from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import any_to_str from metagpt.utils.make_sk_kernel import make_sk_kernel @@ -74,7 +72,7 @@ class SkAgent(Role): result = (await self.plan.invoke_async()).result logger.info(result) - msg = Message(content=result, role=self.profile, cause_by=any_to_str(self._rc.todo)) + msg = Message(content=result, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) self.publish_message(msg) return msg From d9a7443e5a1b67dc5286fcd1d56a8ba8b540ec90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 17:03:24 +0800 Subject: [PATCH 106/413] refactor: notation --- metagpt/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index 63fe41232..82a0117ef 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -58,7 +58,7 @@ class Message(BaseModel): :param instruct_content: Message content struct. :param cause_by: Message producer :param sent_from: Message route info tells who sent this message. - :param send_to: Labels for the consumer to filter its subscribed messages. + :param send_to: Specifies the target recipient or consumer for message delivery in the environment. :param role: Message meta info tells who sent this message. """ super().__init__( From 282a86bfa7b28e44c27b425432e87b1bf1c0c37c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 10 Nov 2023 17:14:58 +0800 Subject: [PATCH 107/413] refactor: unit tests --- tests/metagpt/test_role.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 447de7ee5..8fac2503c 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -60,7 +60,7 @@ async def test_react(): name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc ) role.subscribe({seed.subscription}) - assert role._rc.watch == {seed.subscription} + assert role._rc.watch == set({}) assert role.name == seed.name assert role.profile == seed.profile assert role._setting.goal == seed.goal @@ -69,7 +69,7 @@ async def test_react(): assert role.is_idle env = Environment() env.add_role(role) - assert env.get_subscribed_tags(role) == {seed.subscription} + assert env.get_subscription(role) == {seed.subscription} env.publish_message(Message(content="test", msg_to=seed.subscription)) assert not role.is_idle while not env.is_idle: @@ -82,19 +82,19 @@ async def test_react(): assert role.is_idle tag = uuid.uuid4().hex role.subscribe({tag}) - assert env.get_subscribed_tags(role) == {seed.subscription, tag} + assert env.get_subscription(role) == {tag} @pytest.mark.asyncio async def test_msg_to(): - m = Message(content="a", msg_to=["a", MockRole, Message]) - assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message)} + m = Message(content="a", send_to=["a", MockRole, Message]) + assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) - m = Message(content="a", cause_by=MockAction, msg_to={"a", MockRole, Message}) - assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message), get_class_name(MockAction)} + m = Message(content="a", cause_by=MockAction, send_to={"a", MockRole, Message}) + assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) - m = Message(content="a", msg_to=("a", MockRole, Message)) - assert m.msg_to == {"a", get_class_name(MockRole), get_class_name(Message)} + m = Message(content="a", send_to=("a", MockRole, Message)) + assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) if __name__ == "__main__": From 23749212bfe3b705f0f47758c4dca42efe337eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Sun, 12 Nov 2023 17:41:51 +0800 Subject: [PATCH 108/413] refactor: rename --- metagpt/roles/role.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 5c512b0f0..a8280cecf 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -255,16 +255,16 @@ class Role: logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") return await self._act() - async def run(self, test_message=None): + async def run(self, with_message=None): """Observe, and think and act based on the results of the observation""" - if test_message: # For test + if with_message: # For test msg = None - if isinstance(test_message, str): - msg = Message(test_message) - elif isinstance(test_message, Message): - msg = test_message - elif isinstance(test_message, list): - msg = Message("\n".join(test_message)) + if isinstance(with_message, str): + msg = Message(with_message) + elif isinstance(with_message, Message): + msg = with_message + elif isinstance(with_message, list): + msg = Message("\n".join(with_message)) self.put_message(msg) if not await self._observe(): From 7e8532037e9739f5e57877821cb73c98105306f9 Mon Sep 17 00:00:00 2001 From: Auster Cid Date: Sun, 12 Nov 2023 20:19:51 -0300 Subject: [PATCH 109/413] replaced wait_fixed with wait_exponential --- metagpt/actions/action.py | 4 ++-- metagpt/actions/write_code.py | 4 ++-- metagpt/actions/write_code_review.py | 4 ++-- metagpt/provider/openai_api.py | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..f1a267468 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -9,7 +9,7 @@ import re from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_exponential from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM @@ -49,7 +49,7 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) + @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) async def _aask_v1( self, prompt: str, diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c000805c5..b9b2ab228 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -11,7 +11,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_exponential, wait_exponential PROMPT_TEMPLATE = """ NOTICE @@ -66,7 +66,7 @@ class WriteCode(Action): code_path.write_text(code) logger.info(f"Saving Code to {code_path}") - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) async def write_code(self, prompt): code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4ff4d6cf6..84ccc96fc 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -10,7 +10,7 @@ from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_exponential PROMPT_TEMPLATE = """ NOTICE @@ -65,7 +65,7 @@ class WriteCodeReview(Action): def __init__(self, name="WriteCodeReview", context: list[Message] = None, llm=None): super().__init__(name, context, llm) - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) async def write_code(self, prompt): code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 6ebed2c16..fce19c16e 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -15,7 +15,7 @@ from tenacity import ( retry, retry_if_exception_type, stop_after_attempt, - wait_fixed, + wait_exponential, ) from metagpt.config import CONFIG @@ -226,8 +226,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return await self._achat_completion(messages) @retry( - stop=stop_after_attempt(3), - wait=wait_fixed(1), + stop=stop_after_attempt(4), + wait=wait_exponential(10,60,3), after=after_log(logger, logger.level("WARNING").name), retry=retry_if_exception_type(APIConnectionError), retry_error_callback=log_and_reraise, From 962109bd119ba7cdde0f4ef7c33b3830c8ff9bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 13 Nov 2023 16:26:24 +0800 Subject: [PATCH 110/413] refactor: notation --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index a8280cecf..2e3bcbbd5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -257,7 +257,7 @@ class Role: async def run(self, with_message=None): """Observe, and think and act based on the results of the observation""" - if with_message: # For test + if with_message: msg = None if isinstance(with_message, str): msg = Message(with_message) From c2ffee61e6730d7dcf31168dd6f0cd713d92a98d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 13 Nov 2023 20:45:05 +0800 Subject: [PATCH 111/413] refactor: remove useless code --- metagpt/roles/engineer.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 535a1e27f..d23d23d55 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -237,12 +237,3 @@ class Engineer(Role): return 1 return 0 - - async def _think(self) -> None: - # In asynchronous scenarios, first check if the required messages are ready. - msgs = self._rc.memory.get_by_actions({WriteTasks}) - if not msgs: - self._rc.todo = None - return - - await super(Engineer, self)._think() From 0cf6ec1a93e40ad33ebb46b4060e10a312138253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 16:27:16 +0800 Subject: [PATCH 112/413] feat: +git repo --- metagpt/utils/git_repository.py | 110 +++++++++++++++++++++ tests/metagpt/utils/test_git_repository.py | 79 +++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 metagpt/utils/git_repository.py create mode 100644 tests/metagpt/utils/test_git_repository.py diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py new file mode 100644 index 000000000..fd9794a80 --- /dev/null +++ b/metagpt/utils/git_repository.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/20 +@Author : mashenquan +@File : git_repository.py +@Desc: Git repository management +""" +from __future__ import annotations + +import shutil +from enum import Enum +from pathlib import Path +from typing import Dict + +from git.repo import Repo +from git.repo.fun import is_git_dir + +from metagpt.const import WORKSPACE_ROOT + + +class ChangeType(Enum): + ADDED = "A" # File was added + COPIED = "C" # File was copied + DELETED = "D" # File was deleted + RENAMED = "R" # File was renamed + MODIFIED = "M" # File was modified + TYPE_CHANGED = "T" # Type of the file was changed + UNTRACTED = "U" # File is untracked (not added to version control) + + +class GitRepository: + def __init__(self, local_path=None, auto_init=True): + self._repository = None + if local_path: + self.open(local_path=local_path, auto_init=auto_init) + + def open(self, local_path: Path, auto_init=False): + if self.is_git_dir(local_path): + self._repository = Repo(local_path) + return + if not auto_init: + return + local_path.mkdir(parents=True, exist_ok=True) + return self._init(local_path) + + def _init(self, local_path: Path): + self._repository = Repo.init(path=local_path) + + def add_change(self, files: Dict): + if not self.is_valid or not files: + return + + for k, v in files.items(): + self._repository.index.remove(k) if v is ChangeType.DELETED else self._repository.index.add([k]) + + def commit(self, comments): + if self.is_valid: + self._repository.index.commit(comments) + + def delete_repository(self): + # Delete the repository directory + if self.is_valid: + shutil.rmtree(self._repository.working_dir) + + @property + def changed_files(self) -> Dict[str, str]: + files = {i: ChangeType.UNTRACTED for i in self._repository.untracked_files} + changed_files = {f.a_path: ChangeType(f.change_type) for f in self._repository.index.diff(None)} + files.update(changed_files) + return files + + @staticmethod + def is_git_dir(local_path): + git_dir = local_path / ".git" + if git_dir.exists() and is_git_dir(git_dir): + return True + return False + + @property + def is_valid(self): + return bool(self._repository) + + @property + def status(self) -> str: + if not self.is_valid: + return "" + return self._repository.git.status() + + @property + def workdir(self) -> Path | None: + if not self.is_valid: + return None + return Path(self._repository.working_dir) + + +if __name__ == "__main__": + path = WORKSPACE_ROOT / "git" + path.mkdir(exist_ok=True, parents=True) + + repo = GitRepository() + repo.open(path, auto_init=True) + + changes = repo.changed_files + print(changes) + repo.add_change(changes) + print(repo.status) + repo.commit("test") + print(repo.status) + repo.delete_repository() diff --git a/tests/metagpt/utils/test_git_repository.py b/tests/metagpt/utils/test_git_repository.py new file mode 100644 index 000000000..2e15f44f9 --- /dev/null +++ b/tests/metagpt/utils/test_git_repository.py @@ -0,0 +1,79 @@ +import shutil +from pathlib import Path + +import aiofiles +import pytest + +from metagpt.utils.git_repository import GitRepository + + +async def mock_file(filename, content=""): + async with aiofiles.open(str(filename), mode="w") as file: + await file.write(content) + + +@pytest.mark.asyncio +async def test_git(): + local_path = Path(__file__).parent / "git" + if local_path.exists(): + shutil.rmtree(local_path) + assert not local_path.exists() + repo = GitRepository(local_path=local_path, auto_init=True) + assert local_path.exists() + assert local_path == repo.workdir + assert not repo.changed_files + + await mock_file(local_path / "a.txt") + await mock_file(local_path / "b.txt") + subdir = local_path / "subdir" + subdir.mkdir(parents=True, exist_ok=True) + await mock_file(subdir / "c.txt") + + assert len(repo.changed_files) == 3 + repo.add_change(repo.changed_files) + repo.commit("commit1") + assert not repo.changed_files + + await mock_file(local_path / "a.txt", "tests") + await mock_file(subdir / "d.txt") + rmfile = local_path / "b.txt" + rmfile.unlink() + assert repo.status + + assert len(repo.changed_files) == 3 + repo.add_change(repo.changed_files) + repo.commit("commit2") + assert not repo.changed_files + + assert repo.status + + repo.delete_repository() + assert not local_path.exists() + + +@pytest.mark.asyncio +async def test_git1(): + local_path = Path(__file__).parent / "git1" + if local_path.exists(): + shutil.rmtree(local_path) + assert not local_path.exists() + repo = GitRepository(local_path=local_path, auto_init=True) + assert local_path.exists() + assert local_path == repo.workdir + assert not repo.changed_files + + await mock_file(local_path / "a.txt") + await mock_file(local_path / "b.txt") + subdir = local_path / "subdir" + subdir.mkdir(parents=True, exist_ok=True) + await mock_file(subdir / "c.txt") + + repo1 = GitRepository(local_path=local_path, auto_init=False) + assert repo1.changed_files + + repo1.delete_repository() + assert not local_path.exists() + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) From 9c5f7c76719e07845da74c7ef915388b44722433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 16:33:46 +0800 Subject: [PATCH 113/413] feat: +annotation --- metagpt/utils/git_repository.py | 51 +++++++++++++++++++++- tests/metagpt/utils/test_git_repository.py | 25 ++++------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index fd9794a80..c5b510612 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -30,12 +30,31 @@ class ChangeType(Enum): class GitRepository: + """A class representing a Git repository. + + :param local_path: The local path to the Git repository. + :param auto_init: If True, automatically initializes a new Git repository if the provided path is not a Git repository. + + Attributes: + _repository (Repo): The GitPython `Repo` object representing the Git repository. + """ + def __init__(self, local_path=None, auto_init=True): + """Initialize a GitRepository instance. + + :param local_path: The local path to the Git repository. + :param auto_init: If True, automatically initializes a new Git repository if the provided path is not a Git repository. + """ self._repository = None if local_path: self.open(local_path=local_path, auto_init=auto_init) def open(self, local_path: Path, auto_init=False): + """Open an existing Git repository or initialize a new one if auto_init is True. + + :param local_path: The local path to the Git repository. + :param auto_init: If True, automatically initializes a new Git repository if the provided path is not a Git repository. + """ if self.is_git_dir(local_path): self._repository = Repo(local_path) return @@ -45,9 +64,17 @@ class GitRepository: return self._init(local_path) def _init(self, local_path: Path): + """Initialize a new Git repository at the specified path. + + :param local_path: The local path where the new Git repository will be initialized. + """ self._repository = Repo.init(path=local_path) def add_change(self, files: Dict): + """Add or remove files from the staging area based on the provided changes. + + :param files: A dictionary where keys are file paths and values are instances of ChangeType. + """ if not self.is_valid or not files: return @@ -55,16 +82,24 @@ class GitRepository: self._repository.index.remove(k) if v is ChangeType.DELETED else self._repository.index.add([k]) def commit(self, comments): + """Commit the staged changes with the given comments. + + :param comments: Comments for the commit. + """ if self.is_valid: self._repository.index.commit(comments) def delete_repository(self): - # Delete the repository directory + """Delete the entire repository directory.""" if self.is_valid: shutil.rmtree(self._repository.working_dir) @property def changed_files(self) -> Dict[str, str]: + """Return a dictionary of changed files and their change types. + + :return: A dictionary where keys are file paths and values are change types. + """ files = {i: ChangeType.UNTRACTED for i in self._repository.untracked_files} changed_files = {f.a_path: ChangeType(f.change_type) for f in self._repository.index.diff(None)} files.update(changed_files) @@ -72,6 +107,11 @@ class GitRepository: @staticmethod def is_git_dir(local_path): + """Check if the specified directory is a Git repository. + + :param local_path: The local path to check. + :return: True if the directory is a Git repository, False otherwise. + """ git_dir = local_path / ".git" if git_dir.exists() and is_git_dir(git_dir): return True @@ -79,16 +119,25 @@ class GitRepository: @property def is_valid(self): + """Check if the Git repository is valid (exists and is initialized). + + :return: True if the repository is valid, False otherwise. + """ return bool(self._repository) @property def status(self) -> str: + """Return the Git repository's status as a string.""" if not self.is_valid: return "" return self._repository.git.status() @property def workdir(self) -> Path | None: + """Return the path to the working directory of the Git repository. + + :return: The path to the working directory or None if the repository is not valid. + """ if not self.is_valid: return None return Path(self._repository.working_dir) diff --git a/tests/metagpt/utils/test_git_repository.py b/tests/metagpt/utils/test_git_repository.py index 2e15f44f9..fa329a2ec 100644 --- a/tests/metagpt/utils/test_git_repository.py +++ b/tests/metagpt/utils/test_git_repository.py @@ -12,9 +12,7 @@ async def mock_file(filename, content=""): await file.write(content) -@pytest.mark.asyncio -async def test_git(): - local_path = Path(__file__).parent / "git" +async def mock_repo(local_path) -> (GitRepository, Path): if local_path.exists(): shutil.rmtree(local_path) assert not local_path.exists() @@ -28,6 +26,13 @@ async def test_git(): subdir = local_path / "subdir" subdir.mkdir(parents=True, exist_ok=True) await mock_file(subdir / "c.txt") + return repo, subdir + + +@pytest.mark.asyncio +async def test_git(): + local_path = Path(__file__).parent / "git" + repo, subdir = await mock_repo(local_path) assert len(repo.changed_files) == 3 repo.add_change(repo.changed_files) @@ -54,19 +59,7 @@ async def test_git(): @pytest.mark.asyncio async def test_git1(): local_path = Path(__file__).parent / "git1" - if local_path.exists(): - shutil.rmtree(local_path) - assert not local_path.exists() - repo = GitRepository(local_path=local_path, auto_init=True) - assert local_path.exists() - assert local_path == repo.workdir - assert not repo.changed_files - - await mock_file(local_path / "a.txt") - await mock_file(local_path / "b.txt") - subdir = local_path / "subdir" - subdir.mkdir(parents=True, exist_ok=True) - await mock_file(subdir / "c.txt") + await mock_repo(local_path) repo1 = GitRepository(local_path=local_path, auto_init=False) assert repo1.changed_files From f1fb3b3bece668590557455ae51ecf9b8f306109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 16:35:16 +0800 Subject: [PATCH 114/413] feat: +annotation --- tests/metagpt/utils/test_git_repository.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/metagpt/utils/test_git_repository.py b/tests/metagpt/utils/test_git_repository.py index fa329a2ec..0d1e3b791 100644 --- a/tests/metagpt/utils/test_git_repository.py +++ b/tests/metagpt/utils/test_git_repository.py @@ -1,3 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/20 +@Author : mashenquan +@File : test_git_repository.py +@Desc: Unit tests for git_repository.py +""" + import shutil from pathlib import Path From 363be23045e552d324e2946d16bc0eb29d5302f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 16:44:06 +0800 Subject: [PATCH 115/413] feat: +annotation --- metagpt/utils/file_repository.py | 0 metagpt/utils/git_repository.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 metagpt/utils/file_repository.py diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py new file mode 100644 index 000000000..e69de29bb diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index c5b510612..1732d6a91 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -4,7 +4,7 @@ @Time : 2023/11/20 @Author : mashenquan @File : git_repository.py -@Desc: Git repository management +@Desc: Git repository management. RFC 135 2.2.3.3. """ from __future__ import annotations From af716c6c305254cfb48fda5a865616126931edd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 16:47:12 +0800 Subject: [PATCH 116/413] feat: +annotation --- metagpt/utils/file_repository.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 metagpt/utils/file_repository.py diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py deleted file mode 100644 index e69de29bb..000000000 From 990d79179f48ffc5afce0276dec1cfeb2db4ef9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 17:33:24 +0800 Subject: [PATCH 117/413] feat: archive --- metagpt/utils/file_repository.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 metagpt/utils/file_repository.py diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py new file mode 100644 index 000000000..e69de29bb From 9f7da1c7688f48d9a7ac2cf38c7f81cca35f7ec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 17:34:44 +0800 Subject: [PATCH 118/413] feat: archive --- metagpt/utils/file_repository.py | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index e69de29bb..af787c70a 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/20 +@Author : mashenquan +@File : git_repository.py +@Desc: File repository management. RFC 135 2.2.3.2, 2.2.3.4 and 2.2.3.13. +""" +import json +from pathlib import Path +from typing import Dict, List + +import aiofiles + +from metagpt.utils.git_repository import GitRepository + + +class FileRepository: + def __init__(self, git_repo: GitRepository, relative_path: Path = "."): + self._relative_path = relative_path # Relative path based on the Git repository. + self._git_repo = git_repo + self._dependencies: Dict[str, List[str]] = {} + + async def save(self, filename: Path, content, dependencies: List[str] = None): + path_name = self.workdir / filename + with aiofiles.open(str(path_name), mode="w") as writer: + await writer.write(content) + if dependencies is not None: + await self.update_dependency(filename, dependencies) + + async def update_dependency(self, filename, dependencies: List[str]): + self._dependencies[str(filename)] = dependencies + + async def save_dependency(self): + filename = ".dependencies.json" + path_name = self.workdir / filename + data = json.dumps(self._dependencies) + with aiofiles.open(str(path_name), mode="w") as writer: + await writer.write(data) + + @property + def workdir(self): + return self._git_repo.workdir / self._relative_path From 913cfaebabc22d1130bb9cff9b8a4713b2cd72cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 17:41:16 +0800 Subject: [PATCH 119/413] feat: archive --- metagpt/utils/git_repository.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 1732d6a91..6e624c8b5 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -142,6 +142,14 @@ class GitRepository: return None return Path(self._repository.working_dir) + def archive(self, comments="Archive"): + """Archive the current state of the Git repository. + + :param comments: Comments for the archive commit. + """ + self.add_change(self.changed_files) + self.commit(comments) + if __name__ == "__main__": path = WORKSPACE_ROOT / "git" From 29003a9beb0f1ede36c1139ee8bb3815e0fdad49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 19:36:54 +0800 Subject: [PATCH 120/413] feat: +file repository --- metagpt/utils/file_repository.py | 71 +++++++++++++++++++-- metagpt/utils/git_repository.py | 9 +++ tests/metagpt/utils/test_file_repository.py | 49 ++++++++++++++ 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 tests/metagpt/utils/test_file_repository.py diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index af787c70a..d8637fe3f 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -6,38 +6,95 @@ @File : git_repository.py @Desc: File repository management. RFC 135 2.2.3.2, 2.2.3.4 and 2.2.3.13. """ +from __future__ import annotations + import json from pathlib import Path from typing import Dict, List import aiofiles -from metagpt.utils.git_repository import GitRepository +from metagpt.logs import logger class FileRepository: - def __init__(self, git_repo: GitRepository, relative_path: Path = "."): + def __init__(self, git_repo, relative_path: Path = Path(".")): self._relative_path = relative_path # Relative path based on the Git repository. self._git_repo = git_repo self._dependencies: Dict[str, List[str]] = {} - async def save(self, filename: Path, content, dependencies: List[str] = None): + # Initializing + self.workdir.mkdir(parents=True, exist_ok=True) + if self.dependency_path_name.exists(): + try: + with open(str(self.dependency_path_name), mode="r") as reader: + self._dependencies = json.load(reader) + except Exception as e: + logger.error(f"Failed to load {str(self.dependency_path_name)}, error:{e}") + + async def save(self, filename: Path | str, content, dependencies: List[str] = None): path_name = self.workdir / filename - with aiofiles.open(str(path_name), mode="w") as writer: + path_name.parent.mkdir(parents=True, exist_ok=True) + async with aiofiles.open(str(path_name), mode="w") as writer: await writer.write(content) if dependencies is not None: await self.update_dependency(filename, dependencies) + async def get(self, filename: Path | str): + path_name = self.workdir / filename + async with aiofiles.open(str(path_name), mode="r") as reader: + return await reader.read() + + def get_dependency(self, filename: Path | str) -> List: + key = str(filename) + return self._dependencies.get(key, []) + + def get_changed_dependency(self, filename: Path | str) -> List: + dependencies = self.get_dependency(filename=filename) + changed_files = self.changed_files + changed_dependent_files = [] + for df in dependencies: + if df in changed_files.keys(): + changed_dependent_files.append(df) + return changed_dependent_files + async def update_dependency(self, filename, dependencies: List[str]): self._dependencies[str(filename)] = dependencies async def save_dependency(self): - filename = ".dependencies.json" - path_name = self.workdir / filename data = json.dumps(self._dependencies) - with aiofiles.open(str(path_name), mode="w") as writer: + with aiofiles.open(str(self.dependency_path_name), mode="w") as writer: await writer.write(data) @property def workdir(self): return self._git_repo.workdir / self._relative_path + + @property + def dependency_path_name(self): + filename = ".dependencies.json" + path_name = self.workdir / filename + return path_name + + @property + def changed_files(self) -> Dict[str, str]: + files = self._git_repo.changed_files + relative_files = {} + for p, ct in files.items(): + try: + rf = Path(p).relative_to(self._relative_path) + except ValueError: + continue + relative_files[str(rf)] = ct + return relative_files + + def get_change_dir_files(self, dir: Path | str) -> List: + changed_files = self.changed_files + children = [] + for f in changed_files: + try: + Path(f).relative_to(Path(dir)) + except ValueError: + continue + children.append(str(f)) + return children diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 6e624c8b5..6ae6a7900 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -17,6 +17,7 @@ from git.repo import Repo from git.repo.fun import is_git_dir from metagpt.const import WORKSPACE_ROOT +from metagpt.utils.file_repository import FileRepository class ChangeType(Enum): @@ -150,6 +151,14 @@ class GitRepository: self.add_change(self.changed_files) self.commit(comments) + def new_file_repository(self, relative_path: Path | str) -> FileRepository: + """Create a new instance of FileRepository associated with this Git repository. + + :param relative_path: The relative path to the file repository within the Git repository. + :return: A new instance of FileRepository. + """ + return FileRepository(git_repo=self, relative_path=Path(relative_path)) + if __name__ == "__main__": path = WORKSPACE_ROOT / "git" diff --git a/tests/metagpt/utils/test_file_repository.py b/tests/metagpt/utils/test_file_repository.py new file mode 100644 index 000000000..ac36f2320 --- /dev/null +++ b/tests/metagpt/utils/test_file_repository.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/20 +@Author : mashenquan +@File : test_file_repository.py +@Desc: Unit tests for file_repository.py +""" +import shutil +from pathlib import Path + +import pytest + +from metagpt.utils.git_repository import ChangeType, GitRepository +from tests.metagpt.utils.test_git_repository import mock_file + + +@pytest.mark.asyncio +async def test_file_repo(): + local_path = Path(__file__).parent / "file_repo_git" + if local_path.exists(): + shutil.rmtree(local_path) + + git_repo = GitRepository(local_path=local_path, auto_init=True) + assert not git_repo.changed_files + + await mock_file(local_path / "g.txt", "") + + file_repo_path = "file_repo1" + full_path = local_path / file_repo_path + assert not full_path.exists() + file_repo = git_repo.new_file_repository(file_repo_path) + assert file_repo.workdir == full_path + assert file_repo.workdir.exists() + await file_repo.save("a.txt", "AAA") + await file_repo.save("b.txt", "BBB", ["a.txt"]) + assert "AAA" == await file_repo.get("a.txt") + assert "BBB" == await file_repo.get("b.txt") + assert ["a.txt"] == file_repo.get_dependency("b.txt") + assert {"a.txt": ChangeType.UNTRACTED, "b.txt": ChangeType.UNTRACTED} == file_repo.changed_files + assert ["a.txt"] == file_repo.get_changed_dependency("b.txt") + await file_repo.save("d/e.txt", "EEE") + assert ["d/e.txt"] == file_repo.get_change_dir_files("d") + + git_repo.delete_repository() + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) From 85e3620638348826f32f47065292c91e9e845193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 20 Nov 2023 19:40:15 +0800 Subject: [PATCH 121/413] feat: +file repository --- metagpt/utils/file_repository.py | 51 +++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index d8637fe3f..f4c36b5b7 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -19,7 +19,12 @@ from metagpt.logs import logger class FileRepository: def __init__(self, git_repo, relative_path: Path = Path(".")): - self._relative_path = relative_path # Relative path based on the Git repository. + """Initialize a FileRepository instance. + + :param git_repo: The associated GitRepository instance. + :param relative_path: The relative path within the Git repository. + """ + self._relative_path = relative_path self._git_repo = git_repo self._dependencies: Dict[str, List[str]] = {} @@ -33,6 +38,12 @@ class FileRepository: logger.error(f"Failed to load {str(self.dependency_path_name)}, error:{e}") async def save(self, filename: Path | str, content, dependencies: List[str] = None): + """Save content to a file and update its dependencies. + + :param filename: The filename or path within the repository. + :param content: The content to be saved. + :param dependencies: List of dependency filenames or paths. + """ path_name = self.workdir / filename path_name.parent.mkdir(parents=True, exist_ok=True) async with aiofiles.open(str(path_name), mode="w") as writer: @@ -41,15 +52,30 @@ class FileRepository: await self.update_dependency(filename, dependencies) async def get(self, filename: Path | str): + """Read the content of a file. + + :param filename: The filename or path within the repository. + :return: The content of the file. + """ path_name = self.workdir / filename async with aiofiles.open(str(path_name), mode="r") as reader: return await reader.read() def get_dependency(self, filename: Path | str) -> List: + """Get the dependencies of a file. + + :param filename: The filename or path within the repository. + :return: List of dependency filenames or paths. + """ key = str(filename) return self._dependencies.get(key, []) def get_changed_dependency(self, filename: Path | str) -> List: + """Get the dependencies of a file that have changed. + + :param filename: The filename or path within the repository. + :return: List of changed dependency filenames or paths. + """ dependencies = self.get_dependency(filename=filename) changed_files = self.changed_files changed_dependent_files = [] @@ -59,25 +85,43 @@ class FileRepository: return changed_dependent_files async def update_dependency(self, filename, dependencies: List[str]): + """Update the dependencies of a file. + + :param filename: The filename or path within the repository. + :param dependencies: List of dependency filenames or paths. + """ self._dependencies[str(filename)] = dependencies async def save_dependency(self): + """Save the dependencies to a file.""" data = json.dumps(self._dependencies) with aiofiles.open(str(self.dependency_path_name), mode="w") as writer: await writer.write(data) @property def workdir(self): + """Return the absolute path to the working directory of the FileRepository. + + :return: The absolute path to the working directory. + """ return self._git_repo.workdir / self._relative_path @property def dependency_path_name(self): + """Return the absolute path to the dependency file. + + :return: The absolute path to the dependency file. + """ filename = ".dependencies.json" path_name = self.workdir / filename return path_name @property def changed_files(self) -> Dict[str, str]: + """Return a dictionary of changed files and their change types. + + :return: A dictionary where keys are file paths and values are change types. + """ files = self._git_repo.changed_files relative_files = {} for p, ct in files.items(): @@ -89,6 +133,11 @@ class FileRepository: return relative_files def get_change_dir_files(self, dir: Path | str) -> List: + """Get the files in a directory that have changed. + + :param dir: The directory path within the repository. + :return: List of changed filenames or paths within the directory. + """ changed_files = self.changed_files children = [] for f in changed_files: From d9a2626fde3c7e43e7e118e3ee740a0ad4b9fcf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 21 Nov 2023 13:39:10 +0800 Subject: [PATCH 122/413] feat: +PrepareDocuments --- metagpt/actions/prepare_documents.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 metagpt/actions/prepare_documents.py diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py new file mode 100644 index 000000000..7cf05c5d1 --- /dev/null +++ b/metagpt/actions/prepare_documents.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/20 +@Author : mashenquan +@File : git_repository.py +@Desc: PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt. + RFC 135 2.2.3.5.1. +""" +from metagpt.actions import Action + + +class PrepareDocuments(Action): + def __init__(self, name="", context=None, llm=None): + pass From b73cbe73798647b7e1e69ff4c2c7f41ad9ec7c7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 21 Nov 2023 14:00:09 +0800 Subject: [PATCH 123/413] feat: +unit test --- tests/metagpt/utils/test_common.py | 56 ++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/utils/test_common.py b/tests/metagpt/utils/test_common.py index ec4443175..6474b1233 100644 --- a/tests/metagpt/utils/test_common.py +++ b/tests/metagpt/utils/test_common.py @@ -4,27 +4,79 @@ @Time : 2023/4/29 16:19 @Author : alexanderwu @File : test_common.py +@Modified by: mashenquan, 2023/11/21. Add unit tests. """ import os +from typing import Any, Set import pytest +from pydantic import BaseModel +from metagpt.actions import RunCode from metagpt.const import get_project_root +from metagpt.roles.tutorial_assistant import TutorialAssistant +from metagpt.schema import Message +from metagpt.utils.common import any_to_str, any_to_str_set class TestGetProjectRoot: def change_etc_dir(self): # current_directory = Path.cwd() - abs_root = '/etc' + abs_root = "/etc" os.chdir(abs_root) def test_get_project_root(self): project_root = get_project_root() - assert project_root.name == 'metagpt' + assert project_root.name == "MetaGPT" def test_get_root_exception(self): with pytest.raises(Exception) as exc_info: self.change_etc_dir() get_project_root() assert str(exc_info.value) == "Project root not found." + + def test_any_to_str(self): + class Input(BaseModel): + x: Any + want: str + + inputs = [ + Input(x=TutorialAssistant, want="metagpt.roles.tutorial_assistant.TutorialAssistant"), + Input(x=TutorialAssistant(), want="metagpt.roles.tutorial_assistant.TutorialAssistant"), + Input(x=RunCode, want="metagpt.actions.run_code.RunCode"), + Input(x=RunCode(), want="metagpt.actions.run_code.RunCode"), + Input(x=Message, want="metagpt.schema.Message"), + Input(x=Message(""), want="metagpt.schema.Message"), + Input(x="A", want="A"), + ] + for i in inputs: + v = any_to_str(i.x) + assert v == i.want + + def test_any_to_str_set(self): + class Input(BaseModel): + x: Any + want: Set + + inputs = [ + Input( + x=[TutorialAssistant, RunCode(), "a"], + want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"}, + ), + Input( + x={TutorialAssistant, RunCode(), "a"}, + want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"}, + ), + Input( + x=(TutorialAssistant, RunCode(), "a"), + want={"metagpt.roles.tutorial_assistant.TutorialAssistant", "metagpt.actions.run_code.RunCode", "a"}, + ), + ] + for i in inputs: + v = any_to_str_set(i.x) + assert v == i.want + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) From 27c731d11a94901d4b51dbfe57042ee1a9681b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 21 Nov 2023 15:05:23 +0800 Subject: [PATCH 124/413] feat: archive --- metagpt/actions/prepare_documents.py | 12 +++++++++++- metagpt/environment.py | 9 ++++++++- metagpt/roles/product_manager.py | 11 ++++++++++- metagpt/roles/role.py | 4 ++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 7cf05c5d1..b0185996b 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -12,4 +12,14 @@ from metagpt.actions import Action class PrepareDocuments(Action): def __init__(self, name="", context=None, llm=None): - pass + super().__init__(name, context, llm) + + async def run(self, with_message, **kwargs): + parent = self.context.get("parent") + if not parent: + raise ValueError("Invalid owner") + env = parent.get_env() + if env.git_repository: + return + env.git_repository = GitRepository() + env.git_repository.open(WORKS) diff --git a/metagpt/environment.py b/metagpt/environment.py index b3c296dac..df93a818b 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,7 +12,7 @@ functionality is to be consolidated into the `Environment` class. """ import asyncio -from typing import Iterable, Set +from typing import Iterable, Optional, Set from pydantic import BaseModel, Field @@ -20,6 +20,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import is_subscribed +from metagpt.utils.git_repository import GitRepository class Environment(BaseModel): @@ -31,6 +32,7 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) consumers: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug + git_repository: Optional[GitRepository] = None class Config: arbitrary_types_allowed = True @@ -111,3 +113,8 @@ class Environment(BaseModel): def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" self.consumers[obj] = tags + + def dict(self, *args, **kwargs): + """Generate a dictionary representation of the model, optionally specifying which fields to include or + exclude.""" + return super(Environment, self).dict(exclude={"git_repository"}) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a58ea5385..c10aba6d1 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -6,6 +6,7 @@ @File : product_manager.py """ from metagpt.actions import BossRequirement, WritePRD +from metagpt.actions.prepare_documents import PrepareDocuments from metagpt.roles import Role @@ -37,5 +38,13 @@ class ProductManager(Role): constraints (str): Constraints or limitations for the product manager. """ super().__init__(name, profile, goal, constraints) - self._init_actions([WritePRD]) + self._init_actions([PrepareDocuments(context={"parent": self}), WritePRD]) self._watch([BossRequirement]) + + async def _think(self) -> None: + """Decide what to do""" + if self._rc.env.git_repository: + self._set_state(1) + else: + self._set_state(0) + return self._rc.todo diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 2e3bcbbd5..d1e65a4e0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -164,6 +164,10 @@ class Role: if env: env.set_subscription(self, self._subscription) + def get_env(self): + """Return the environment in which the role works.""" + return self._rc.env + @property def profile(self): """Get the role description (position)""" From c233699275930e15b11ef64633d4383ac1fc6ba4 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 21 Nov 2023 20:33:58 +0800 Subject: [PATCH 125/413] add aiohttp encapsulation --- metagpt/utils/ahttp_client.py | 59 ++++++++++++++++++++++++ tests/metagpt/utils/test_ahttp_client.py | 38 +++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 metagpt/utils/ahttp_client.py create mode 100644 tests/metagpt/utils/test_ahttp_client.py diff --git a/metagpt/utils/ahttp_client.py b/metagpt/utils/ahttp_client.py new file mode 100644 index 000000000..d4f9f94e5 --- /dev/null +++ b/metagpt/utils/ahttp_client.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : pure async http_client + +from typing import Optional, Any, Mapping, Union + +from aiohttp.client import DEFAULT_TIMEOUT +import aiohttp + + +async def apost(url: str, + params: Optional[Mapping[str, str]] = None, + json: Any = None, + data: Any = None, + headers: Optional[dict] = None, + as_json: bool = False, + encoding: str = "utf-8", + timeout: int = DEFAULT_TIMEOUT.total) -> Union[str, dict]: + async with aiohttp.ClientSession() as session: + async with session.post( + url=url, + params=params, + json=json, + data=data, + headers=headers, + timeout=timeout + ) as resp: + if as_json: + data = await resp.json() + else: + data = await resp.read() + data = data.decode(encoding) + return data + + +async def apost_stream(url: str, + params: Optional[Mapping[str, str]] = None, + json: Any = None, + data: Any = None, + headers: Optional[dict] = None, + encoding: str = "utf-8", + timeout: int = DEFAULT_TIMEOUT.total) -> Any: + """ + usage: + result = astream(url="xx") + async for line in result: + deal_with(line) + """ + async with aiohttp.ClientSession() as session: + async with session.post( + url=url, + params=params, + json=json, + data=data, + headers=headers, + timeout=timeout + ) as resp: + async for line in resp.content: + yield line.decode(encoding) diff --git a/tests/metagpt/utils/test_ahttp_client.py b/tests/metagpt/utils/test_ahttp_client.py new file mode 100644 index 000000000..15159423a --- /dev/null +++ b/tests/metagpt/utils/test_ahttp_client.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of ahttp_client + +import pytest + +from metagpt.utils.ahttp_client import apost, apost_stream + + +@pytest.mark.asyncio +async def test_apost(): + result = await apost( + url="https://www.baidu.com/" + ) + assert "百度一下" in result + + result = await apost( + url="http://aider.meizu.com/app/weather/listWeather", + data={"cityIds": "101240101"}, + as_json=True + ) + assert result["code"] == "200" + + +@pytest.mark.asyncio +async def test_apost_stream(): + result = apost_stream( + url="https://www.baidu.com/" + ) + async for line in result: + assert len(line) >= 0 + + result = apost_stream( + url="http://aider.meizu.com/app/weather/listWeather", + data={"cityIds": "101240101"} + ) + async for line in result: + assert len(line) >= 0 From c49b832deecfb9d5ab1455d0db238e03e9300740 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 21 Nov 2023 20:34:37 +0800 Subject: [PATCH 126/413] add trigger repair_llm_output for open llm --- metagpt/actions/action.py | 21 +- metagpt/config.py | 1 + metagpt/roles/role.py | 14 +- metagpt/utils/repair_llm_raw_output.py | 246 ++++++++++++++++++ tests/metagpt/utils/test_custom_decoder.py | 45 ++++ .../utils/test_repair_llm_raw_output.py | 203 +++++++++++++++ 6 files changed, 515 insertions(+), 15 deletions(-) create mode 100644 metagpt/utils/repair_llm_raw_output.py create mode 100644 tests/metagpt/utils/test_repair_llm_raw_output.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..f9e4f926b 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -16,6 +16,8 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder +from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType,\ + retry_parse_json_text, extract_content_from_output class Action(ABC): @@ -49,7 +51,7 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) + # @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) async def _aask_v1( self, prompt: str, @@ -65,22 +67,19 @@ class Action(ABC): content = await self.llm.aask(prompt, system_msgs) logger.debug(content) output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) + output_class_fields = list(output_class.schema()["properties"].keys()) # Custom ActionOutput's fields if format == "json": - pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" - matches = re.findall(pattern, content, re.DOTALL) - - for match in matches: - if match: - content = match - break - - parsed_data = CustomDecoder(strict=False).decode(content) + content = repair_llm_raw_output(content, req_keys=output_class_fields + ["[/CONTENT]"]) + content = extract_content_from_output(content) + content = repair_llm_raw_output(content, req_keys=[None], repair_type=RepairType.JSON) # req_keys mocked + logger.info(f"extracted CONTENT from content:\n{content}") + parsed_data = retry_parse_json_text(content) else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - logger.debug(parsed_data) + logger.debug(f"parsed_data:\n{parsed_data}") instruct_content = output_class(**parsed_data) return ActionOutput(content, instruct_content) diff --git a/metagpt/config.py b/metagpt/config.py index 3f9e742bd..a4c43c28a 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -93,6 +93,7 @@ class Config(metaclass=Singleton): self.mermaid_engine = self._get("MERMAID_ENGINE", "nodejs") self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") + self.repair_llm_output = self._get("REPAIR_LLM_OUTPUT", False) self.prompt_format = self._get("PROMPT_FORMAT", "markdown") def _init_with_config_files_and_env(self, configs: dict, yaml_file): diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b96c361c0..140910f0a 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -19,6 +19,8 @@ from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message +from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output + PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -49,6 +51,7 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi {name}: {result} """ + class RoleReactMode(str, Enum): REACT = "react" BY_ORDER = "by_order" @@ -58,6 +61,7 @@ class RoleReactMode(str, Enum): def values(cls): return [item.value for item in cls] + class RoleSetting(BaseModel): """Role Settings""" name: str @@ -79,11 +83,11 @@ class RoleContext(BaseModel): env: 'Environment' = Field(default=None) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) - state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None + state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[]) - react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes + react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes max_react_loop: int = 1 class Config: @@ -127,8 +131,9 @@ class Role: i = action("", llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): - logger.warning(f"is_human attribute does not take effect," - f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances") + logger.warning(f"is_human attribute does not take effect, " + f"as Role's {str(action)} was initialized using LLM, " + f"try passing in Action classes instead of initialized instances") i = action i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) @@ -193,6 +198,7 @@ class Role: n_states=len(self._states) - 1, previous_state=self._rc.state) # print(prompt) next_state = await self._llm.aask(prompt) + next_state = extract_state_value_from_output(next_state) logger.debug(f"{prompt=}") if (not next_state.isdigit() and next_state != "-1") \ or int(next_state) not in range(-1, len(self._states)): diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py new file mode 100644 index 000000000..a65e4be80 --- /dev/null +++ b/metagpt/utils/repair_llm_raw_output.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : repair llm raw output with particular conditions + +import copy +from enum import Enum +from typing import Union +import regex as re + +from metagpt.logs import logger +from metagpt.config import CONFIG +from metagpt.utils.custom_decoder import CustomDecoder + + +class RepairType(Enum): + CS = "case sensitivity" + SCM = "special character missing" # Usually the req_key appear in pairs like `[key] xx [/key]` + RKPM = "required key pair missing" # condition like `[key] xx` which lacks `[/key]` + JSON = "json format" + + +def repair_case_sensitivity(output: str, req_key: str) -> str: + """ + usually, req_key is the key name of expected json or markdown content, it won't appear in the value part. + fix target string `"Shared Knowledge": ""` but `"Shared knowledge": ""` actually + """ + if req_key in output: + return output + + output_lower = output.lower() + req_key_lower = req_key.lower() + if req_key_lower in output_lower: + # find the sub-part index, and replace it with raw req_key + lidx = output_lower.find(req_key_lower) + source = output[lidx: lidx + len(req_key_lower)] + output = output.replace(source, req_key) + logger.info(f"repair_case_sensitivity: {req_key}") + + return output + + +def repair_special_character_missing(output: str, req_key: str) -> str: + """ + fix target string `[CONTENT]xxx[/CONTENT]` lacks [/CONTENT] + """ + sc_arr = ["/"] + + if req_key in output: + return output + + for sc in sc_arr: + req_key_pure = req_key.replace(sc, "") + appear_cnt = output.count(req_key_pure) + if req_key_pure in output and appear_cnt > 1: + # req_key with special_character usually in the tail side + ridx = output.rfind(req_key_pure) + output = f"{output[:ridx]}{req_key}{output[ridx + len(req_key_pure):]}" + logger.info(f"repair_special_character_missing: {req_key}") + + return output + + +def repair_required_key_pair_missing(output: str, req_key: str) -> str: + """ + implement the req_key pair in the begin or end of the content + req_key format + 1. `[req_key]`, and its pair `[/req_key]` + 2. `[/req_key]`, and its pair `[req_key]` + """ + if req_key.startswith("[") and req_key.endswith("]"): + if "/" in req_key: + left_key = req_key.replace("/", "") # `[/req_key]` -> `[req_key]` + right_key = req_key + else: + left_key = req_key + right_key = f"{req_key[0]}/{req_key[1:]}" # `[req_key]` -> `[/req_key]` + + if left_key not in output: + output = left_key + output + if right_key not in output: + output = output + right_key + + return output + + +def repair_json_format(output: str) -> str: + """ + fix extra `[` or `}` in the end + """ + output = output.strip() + + if output.startswith("[{"): + output = output[1:] + logger.info(f"repair_json_format: {'[{'}") + elif output.endswith("}]"): + output = output[:-1] + logger.info(f"repair_json_format: {'}]'}") + elif output.startswith("{") and output.startswith("]"): + output = output[:-1] + "}" + + return output + + +def _repair_llm_raw_output(output: str, req_key: str, repair_type: RepairType = None) -> str: + repair_types = [repair_type] if repair_type else [item for item in RepairType if item not in [RepairType.JSON]] + for repair_type in repair_types: + if repair_type == RepairType.CS: + output = repair_case_sensitivity(output, req_key) + elif repair_type == RepairType.SCM: + output = repair_special_character_missing(output, req_key) + elif repair_type == RepairType.JSON: + output = repair_json_format(output) + elif repair_type == RepairType.RKPM: + output = repair_required_key_pair_missing(output, req_key) + return output + + +def repair_llm_raw_output(output: str, req_keys: list[str], repair_type: RepairType = None) -> str: + """ + in open-source llm model, it usually can't follow the instruction well, the output may be incomplete, + so here we try to repair it and use all repair methods by default. + typical case + 1. case sensitivity + target: "Original Requirements" + output: "Original requirements" + 2. special character missing + target: [/CONTENT] + output: [CONTENT] + 3. json format + target: { xxx } + output: { xxx }] + """ + if not CONFIG.repair_llm_output: + return output + + # do the repairation usually for non-openai models + for req_key in req_keys: + output = _repair_llm_raw_output(output=output, + req_key=req_key, + repair_type=repair_type) + return output + + +def repair_invalid_json(output: str, error: str) -> str: + """ + repair the situation like there are extra chars like + error examples + example 1. json.decoder.JSONDecodeError: Expecting ',' delimiter: line 154 column 1 (char 2765) + example 2. xxx.JSONDecodeError: Expecting property name enclosed in double quotes: line 14 column 1 (char 266) + """ + pattern = r"line ([0-9]+)" + + matches = re.findall(pattern, error, re.DOTALL) + if len(matches) > 0: + line_no = int(matches[0]) - 1 + + # due to CustomDecoder can handle `"": ''` or `'': ""`, so convert `"""` -> `"`, `'''` -> `'` + output = output.replace('"""', '"').replace("'''", '"') + arr = output.split("\n") + line = arr[line_no].strip() + # different general problems + if line.endswith("],"): + # problem, redundant char `]` + line = line.replace("]", "") + elif line.endswith("},"): + # problem, redundant char `}` + line = line.replace("}", "") + elif '",' not in line: + line = f'{line}",' + elif "," not in line: + # problem, miss char `,` at the end. + line = f"{line}," + + arr[line_no] = line + output = "\n".join(arr) + logger.info(f"repair_invalid_json, raw error: {error}") + + return output + + +def retry_parse_json_text(output: str, retry: int = 5) -> Union[list, dict]: + """ + repair the json-text situation like there are extra chars like [']', '}'] + """ + parsed_data = {} + for idx in range(retry): + raw_output = copy.deepcopy(output) + + try: + parsed_data = CustomDecoder(strict=False).decode(output) + break + except Exception as exp: + if not CONFIG.repair_llm_output: + # if repair_llm_output is False, break from the retry loop + break + + logger.warning(f"decode content into json failed, try to fix it. exp: {exp}") + error = str(exp) + output = repair_invalid_json(output, error) + + return parsed_data + + +def extract_content_from_output(content: str, right_key: str = "[/CONTENT]"): + """ extract xxx from [CONTENT](xxx)[/CONTENT] using regex pattern """ + def re_extract_content(cont: str, pattern: str) -> str: + matches = re.findall(pattern, cont, re.DOTALL) + for match in matches: + if match: + cont = match + break + return cont.strip() + + raw_content = copy.deepcopy(content) + pattern = r"\[CONTENT\]([\s\S]*)\[/CONTENT\]" + new_content = re_extract_content(raw_content, pattern) + + if not new_content.startswith("{"): + # TODO find a more general pattern + # # for `[CONTENT]xxx[CONTENT]xxxx[/CONTENT] situation + logger.warning(f"extract_content try another pattern: {pattern}") + raw_content = copy.deepcopy(new_content + right_key) + # # pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" + new_content = re_extract_content(raw_content, pattern) + else: + if right_key in new_content: + idx = new_content.find(right_key) + new_content = new_content[:idx] + + return new_content + + +def extract_state_value_from_output(content: str) -> str: + """ + For openai models, they will always return state number. But for open llm models, the instruction result maybe a + long text contain target number, so here add a extraction to improve success rate. + + Args: + content (str): llm's output from `Role._think` + """ + content = content.strip() # deal the output cases like " 0", "0\n" and so on. + pattern = r"([0-9])" # TODO find the number using a more proper method not just extract from content using pattern + matches = re.findall(pattern, content, re.DOTALL) + matches = list(set(matches)) + state = matches[0] if len(matches) > 0 else "-1" + return state diff --git a/tests/metagpt/utils/test_custom_decoder.py b/tests/metagpt/utils/test_custom_decoder.py index c7b14ad59..4af7a6cdc 100644 --- a/tests/metagpt/utils/test_custom_decoder.py +++ b/tests/metagpt/utils/test_custom_decoder.py @@ -6,6 +6,7 @@ @File : test_custom_decoder.py """ +import pytest from metagpt.utils.custom_decoder import CustomDecoder @@ -37,6 +38,46 @@ def test_parse_single_quote(): parsed_data = decoder.decode(input_data) assert 'a"\n b' in parsed_data + input_data = """{ + 'a': " + b +" +} +""" + with pytest.raises(Exception): + parsed_data = decoder.decode(input_data) + + input_data = """{ + 'a': ' + b +' +} +""" + with pytest.raises(Exception): + parsed_data = decoder.decode(input_data) + + +def test_parse_double_quote(): + decoder = CustomDecoder(strict=False) + + input_data = """{ + "a": " + b +" +} +""" + parsed_data = decoder.decode(input_data) + assert parsed_data["a"] == "\n b\n" + + input_data = """{ + "a": ' + b +' +} +""" + parsed_data = decoder.decode(input_data) + assert parsed_data["a"] == "\n b\n" + def test_parse_triple_double_quote(): # Create a custom JSON decoder @@ -54,6 +95,10 @@ def test_parse_triple_double_quote(): parsed_data = decoder.decode(input_data) assert parsed_data["a"] == "b" + input_data = "{\"\"\"a\"\"\": '''b'''}" + parsed_data = decoder.decode(input_data) + assert parsed_data["a"] == "b" + def test_parse_triple_single_quote(): # Create a custom JSON decoder diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py new file mode 100644 index 000000000..39a7343e7 --- /dev/null +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -0,0 +1,203 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of repair_llm_raw_output + +import pytest + +from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType, repair_invalid_json,\ + extract_content_from_output, retry_parse_json_text + + +def test_repair_case_sensitivity(): + raw_output = """{ + "Original requirements": "Write a 2048 game", + "search Information": "", + "competitive Quadrant charT": "quadrantChart + Campaign A: [0.3, 0.6]", + "requirement analysis": "The 2048 game should be simple to play" +}""" + target_output = """{ + "Original Requirements": "Write a 2048 game", + "Search Information": "", + "Competitive Quadrant Chart": "quadrantChart + Campaign A: [0.3, 0.6]", + "Requirement Analysis": "The 2048 game should be simple to play" +}""" + req_keys = ["Original Requirements", "Search Information", "Competitive Quadrant Chart", "Requirement Analysis"] + output = repair_llm_raw_output(output=raw_output, + req_keys=req_keys) + assert output == target_output + + +def test_repair_special_character_missing(): + raw_output = """[CONTENT] + "Anything UNCLEAR": "No unclear requirements or information." +[CONTENT]""" + + target_output = """[CONTENT] + "Anything UNCLEAR": "No unclear requirements or information." +[/CONTENT]""" + req_keys = ["[/CONTENT]"] + output = repair_llm_raw_output(output=raw_output, + req_keys=req_keys) + assert output == target_output + + +def test_required_key_pair_missing(): + raw_output = "[CONTENT] xxx" + target_output = "[CONTENT] xxx[/CONTENT]" + + output = repair_llm_raw_output(output=raw_output, + req_keys=["[/CONTENT]"]) + assert output == target_output + + raw_output = "xxx[/CONTENT]" + target_output = "[CONTENT]xxx[/CONTENT]" + + output = repair_llm_raw_output(output=raw_output, + req_keys=["[CONTENT]"]) + assert output == target_output + + +def test_repair_json_format(): + raw_output = "{ xxx }]" + target_output = "{ xxx }" + + output = repair_llm_raw_output(output=raw_output, + req_keys=[None], + repair_type=RepairType.JSON) + assert output == target_output + + +def test_retry_parse_json_text(): + invalid_json_text = """{ +"Original Requirements": "Create a 2048 game", +"Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis" +], +"Requirement Analysis": "The requirements are clear and well-defined" +}""" + target_json = { + "Original Requirements": "Create a 2048 game", + "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", + "Requirement Analysis": "The requirements are clear and well-defined" + } + output = retry_parse_json_text(invalid_json_text) + assert output == target_json + + invalid_json_text = """{ +"Original Requirements": "Create a 2048 game", +"Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis" +}, +"Requirement Analysis": "The requirements are clear and well-defined" +}""" + target_json = { + "Original Requirements": "Create a 2048 game", + "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", + "Requirement Analysis": "The requirements are clear and well-defined" + } + output = retry_parse_json_text(invalid_json_text) + assert output == target_json + + +def test_extract_content_from_output(): + output = 'Sure! Here is the properly formatted JSON output based on the given context:\n\n[CONTENT]\n{\n"' \ + 'Required Python third-party packages": [\n"pygame==2.0.4",\n"pytest"\n],\n"Required Other language ' \ + 'third-party packages": [\n"No third-party packages are required."\n],\n"Full API spec": "\nopenapi: ' \ + '3.0.0\n\ndescription: A JSON object representing the game state.\n\npaths:\n game:\n get:\n ' \ + 'summary: Get the current game state.\n responses:\n 200:\n description: Game state.' \ + '\n\n moves:\n post:\n summary: Make a move.\n requestBody:\n description: Move to be ' \ + 'made.\n content:\n applicationjson:\n schema:\n type: object\n ' \ + ' properties:\n x:\n type: integer\n y:\n ' \ + ' type: integer\n tile:\n type: object\n ' \ + 'properties:\n value:\n type: integer\n x:\n ' \ + ' type: integer\n y:\n type: integer\n\n ' \ + 'undo-move:\n post:\n summary: Undo the last move.\n responses:\n 200:\n ' \ + ' description: Undone move.\n\n end-game:\n post:\n summary: End the game.\n responses:\n ' \ + ' 200:\n description: Game ended.\n\n start-game:\n post:\n summary: Start a new ' \ + 'game.\n responses:\n 200:\n description: Game started.\n\n game-over:\n get:\n ' \ + ' summary: Check if the game is over.\n responses:\n 200:\n description: Game ' \ + 'over.\n 404:\n description: Game not over.\n\n score:\n get:\n summary: Get the ' \ + 'current score.\n responses:\n 200:\n description: Score.\n\n tile:\n get:\n ' \ + 'summary: Get a specific tile.\n parameters:\n tile_id:\n type: integer\n ' \ + 'description: ID of the tile to get.\n responses:\n 200:\n description: Tile.\n\n ' \ + 'tiles:\n get:\n summary: Get all tiles.\n responses:\n 200:\n description: ' \ + 'Tiles.\n\n level:\n get:\n summary: Get the current level.\n responses:\n 200:\n ' \ + ' description: Level.\n\n level-up:\n post:\n summary: Level up.\n responses:\n ' \ + '200:\n description: Level up successful.\n\n level-down:\n post:\n summary: Level ' \ + 'down.\n responses:\n 200:\n description: Level down successful.\n\n restart:\n ' \ + 'post:\n summary: Restart the game.\n responses:\n 200:\n description: Game ' \ + 'restarted.\n\n help:\n get:\n summary: Get help.\n responses:\n 200:\n ' \ + 'description: Help.\n\n version:\n get:\n summary: Get the version of the game.\n ' \ + 'responses:\n 200:\n description: Version.\n\n}\n\n"Logic Analysis": [\n"game.py",' \ + '\n"Contains the game logic."\n],\n"Task list": [\n"game.py",\n"Contains the game logic and should be ' \ + 'done first."\n],\n"Shared Knowledge": "\n\'game.py\' contains the game logic.\n",\n"Anything ' \ + 'UNCLEAR": "How to start the game."\n]\n\n[/CONTENT] Great! Your JSON output is properly formatted ' \ + 'and correctly includes all the required sections. Here\'s a breakdown of what each section ' \ + 'contains:\n\nRequired Python third-party packages:\n\n* pygame==2.0.4\n* pytest\n\nRequired Other ' \ + 'language third-party packages:\n\n* No third-party packages are required.\n\nFull API spec:\n\n* ' \ + 'openapi: 3.0.0\n* description: A JSON object representing the game state.\n* paths:\n + game: ' \ + 'Get the current game state.\n + moves: Make a move.\n + undo-move: Undo the last move.\n + ' \ + 'end-game: End the game.\n + start-game: Start a new game.\n + game-over: Check if the game is ' \ + 'over.\n + score: Get the current score.\n + tile: Get a specific tile.\n + tiles: Get all tiles.\n ' \ + '+ level: Get the current level.\n + level-up: Level up.\n + level-down: Level down.\n + restart: ' \ + 'Restart the game.\n + help: Get help.\n + version: Get the version of the game.\n\nLogic ' \ + 'Analysis:\n\n* game.py contains the game logic.\n\nTask list:\n\n* game.py contains the game logic ' \ + 'and should be done first.\n\nShared Knowledge:\n\n* \'game.py\' contains the game logic.\n\nAnything ' \ + 'UNCLEAR:\n\n* How to start the game.\n\nGreat job! This JSON output should provide a clear and ' \ + 'comprehensive overview of the project\'s requirements and dependencies.' + output = extract_content_from_output(output) + assert output.startswith('{\n"Required Python third-party packages') + + output = 'Sure, I would be happy to help! Here is the information you provided, formatted as a JSON object ' \ + 'inside the [CONTENT] tag:\n\n[CONTENT]\n{\n"Original Requirements": "Create a 2048 game",\n"Search ' \ + 'Information": "Search results for 2048 game",\n"Requirements": [\n"Create a game with the same rules ' \ + 'as the original 2048 game",\n"Implement a user interface that is easy to use and understand",\n"Add a ' \ + 'scoreboard to track the player progress",\n"Allow the player to undo and redo moves",\n"Implement a ' \ + 'game over screen to display the final score"\n],\n"Product Goals": [\n"Create a fun and engaging game ' \ + 'experience for the player",\n"Design a user interface that is visually appealing and easy to use",\n"' \ + 'Optimize the game for performance and responsiveness"\n],\n"User Stories": [\n"As a player, I want to ' \ + 'be able to move tiles around the board to combine numbers",\n"As a player, I want to be able to undo ' \ + 'and redo moves to correct mistakes",\n"As a player, I want to see the final score and game over screen' \ + ' when I win"\n],\n"Competitive Analysis": [\n"Competitor A: 2048 game with a simple user interface and' \ + ' basic graphics",\n"Competitor B: 2048 game with a more complex user interface and better graphics",' \ + '\n"Competitor C: 2048 game with a unique twist on the rules and a more challenging gameplay experience"' \ + '\n],\n"Competitive Quadrant Chart": "quadrantChart\\n\ttitle Reach and engagement of campaigns\\n\t\t' \ + 'x-axis Low Reach --> High Reach\\n\t\ty-axis Low Engagement --> High Engagement\\n\tquadrant-1 We ' \ + 'should expand\\n\tquadrant-2 Need to promote\\n\tquadrant-3 Re-evaluate\\n\tquadrant-4 May be ' \ + 'improved\\n\tCampaign A: [0.3, 0.6]\\n\tCampaign B: [0.45, 0.23]\\n\tCampaign C: [0.57, 0.69]\\n\t' \ + 'Campaign D: [0.78, 0.34]\\n\tCampaign E: [0.40, 0.34]\\n\tCampaign F: [0.35, 0.78]"\n],\n"Requirement ' \ + 'Analysis": "The requirements are clear and well-defined, but there may be some ambiguity around the ' \ + 'specific implementation details",\n"Requirement Pool": [\n["P0", "Implement a game with the same ' \ + 'rules as the original 2048 game"],\n["P1", "Add a scoreboard to track the player progress"],\n["P2", ' \ + '"Allow the player to undo and redo moves"]\n],\n"UI Design draft": "The UI should be simple and easy ' \ + 'to use, with a clean and visually appealing design. The game board should be the main focus of the ' \ + 'UI, with clear and concise buttons for the player to interact with.",\n"Anything UNCLEAR": ""\n}\n' \ + '[/CONTENT]\n\nI hope this helps! Let me know if you have any further questions or if there anything ' \ + 'else I can do to assist you.' + output = extract_content_from_output(output) + assert output.startswith('{\n"Original Requirements"') + + output = """ Sure, I'd be happy to help! Here's the JSON output for the given context:\n\n[CONTENT]\n{ +"Implementation approach": "We will use the open-source framework PyGame to create a 2D game engine, which will + provide us with a robust and efficient way to handle game logic and rendering. PyGame is widely used in the game + development community and has a large number of resources and tutorials available online.",\n"Python package name": + "pygame_2048",\n"File list": ["main.py", "game.py", "constants.py", "ui.py"],\n"Data structures and interface + definitions": '\nclassDiagram\n class Game{\n +int score\n +list tiles\n +function + move_tile(tile, int dx, int dy)\n +function undo_move()\n +function get_highest_score()\n }\n + class Tile{\n +int value\n +int x\n +int y\n }\n ...\n Game "1" -- "1" Food: has\n', +\n"Program call flow": '\nsequenceDiagram\n participant M as Main\n participant G as Game\n ...\n G->>M: + end game\n',\n"Anything UNCLEAR": "The requirement is clear to me."\n}\n[/CONTENT] Here's the JSON output for the + given context, wrapped inside the [CONTENT][/CONTENT] format:\n\n[CONTENT]\n{\n"Implementation approach": "We will + use the open-source framework PyGame to create a 2D game engine, which will provide us with a robust and efficient + way to handle game logic and rendering. PyGame is widely used in the game development community and has a large + number of resources and tutorials available online.",\n"Python package name": "pygame_2048",\n"File list": + ["main.py", "game.py", "constants.py", "ui.py"],\n"Data structures and interface definitions": '\nclassDiagram\n + class Game{\n +int score\n +list tiles\n +function move_tile(tile, int dx, int dy)\n ++function undo_move()\n +function get_highest_score()\n }\n class Tile{\n +int value\n +int x\n + +int y\n }\n ...\n Game "1" -- "1" Food: has\n',\n"Program call flow": '\nsequenceDiagram\n participant + M as Main\n participant G as Game\n ...\n G->>M: end game\n',\n"Anything UNCLEAR": "The requirement is + clear to me."\n}\n[/CONTENT] Great! Your JSON output is well-formatted and provides all the necessary + information for a developer to understand the design and implementation of the 2048 game. +""" + output = extract_content_from_output(output) + assert output.startswith('{\n"Implementation approach"') and "[/CONTENT]" not in output From fc4ec5a9449bd644e0e13c30f2de4867f0b9685a Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 22 Nov 2023 13:01:16 +0800 Subject: [PATCH 127/413] update retry_parse_json_text --- config/config.yaml | 5 + metagpt/actions/action.py | 17 +-- metagpt/utils/repair_llm_raw_output.py | 110 +++++++++++++----- .../utils/test_repair_llm_raw_output.py | 66 ++++++++++- 4 files changed, 158 insertions(+), 40 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index bed67083c..72d2c0b19 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -94,4 +94,9 @@ MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k ### browser path for pyppeteer engine, support Chrome, Chromium,MS Edge #PYPPETEER_EXECUTABLE_PATH: "/usr/bin/google-chrome-stable" +### for repair non-openai LLM's output when parse json-text if PROMPT_FORMAT=json +### due to non-openai LLM's output will not always follow the instruction, so here activate a post-process +### repair operation on the content extracted from LLM's raw output. Warning, it improves the result but not fix all cases. +# REPAIR_LLM_OUTPUT: false + PROMPT_FORMAT: json #json or markdown \ No newline at end of file diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index f9e4f926b..7433c3857 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -5,17 +5,16 @@ @Author : alexanderwu @File : action.py """ -import re + from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_fixed, after_log from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser -from metagpt.utils.custom_decoder import CustomDecoder from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType,\ retry_parse_json_text, extract_content_from_output @@ -51,7 +50,11 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - # @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) + @retry( + stop=stop_after_attempt(3), + wait=wait_fixed(1), + after=after_log(logger, logger.level("ERROR").name), + ) async def _aask_v1( self, prompt: str, @@ -65,7 +68,7 @@ class Action(ABC): system_msgs = [] system_msgs.append(self.prefix) content = await self.llm.aask(prompt, system_msgs) - logger.debug(content) + logger.debug(f"llm raw output:\n{content}") output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) output_class_fields = list(output_class.schema()["properties"].keys()) # Custom ActionOutput's fields @@ -73,8 +76,8 @@ class Action(ABC): content = repair_llm_raw_output(content, req_keys=output_class_fields + ["[/CONTENT]"]) content = extract_content_from_output(content) content = repair_llm_raw_output(content, req_keys=[None], repair_type=RepairType.JSON) # req_keys mocked - logger.info(f"extracted CONTENT from content:\n{content}") - parsed_data = retry_parse_json_text(content) + logger.info(f"extracted json CONTENT from output:\n{content}") + parsed_data = retry_parse_json_text(output=content) # should use output=content else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index a65e4be80..c26dc838d 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -4,8 +4,9 @@ import copy from enum import Enum -from typing import Union +from typing import Union, Callable import regex as re +from tenacity import retry, stop_after_attempt, wait_fixed, after_log, RetryCallState from metagpt.logs import logger from metagpt.config import CONFIG @@ -14,8 +15,8 @@ from metagpt.utils.custom_decoder import CustomDecoder class RepairType(Enum): CS = "case sensitivity" - SCM = "special character missing" # Usually the req_key appear in pairs like `[key] xx [/key]` RKPM = "required key pair missing" # condition like `[key] xx` which lacks `[/key]` + SCM = "special character missing" # Usually the req_key appear in pairs like `[key] xx [/key]` JSON = "json format" @@ -39,9 +40,11 @@ def repair_case_sensitivity(output: str, req_key: str) -> str: return output -def repair_special_character_missing(output: str, req_key: str) -> str: +def repair_special_character_missing(output: str, req_key: str = "[/CONTENT]") -> str: """ - fix target string `[CONTENT]xxx[/CONTENT]` lacks [/CONTENT] + fix + 1. target string `[CONTENT] xx [CONTENT] xxx [CONTENT]` lacks `/` in the last `[CONTENT]` + 2. target string `xx [CONTENT] xxx [CONTENT] xxxx` lacks `/` in the last `[CONTENT]` """ sc_arr = ["/"] @@ -55,30 +58,48 @@ def repair_special_character_missing(output: str, req_key: str) -> str: # req_key with special_character usually in the tail side ridx = output.rfind(req_key_pure) output = f"{output[:ridx]}{req_key}{output[ridx + len(req_key_pure):]}" - logger.info(f"repair_special_character_missing: {req_key}") + logger.info(f"repair_special_character_missing: {sc} in {req_key_pure} as position {ridx}") return output -def repair_required_key_pair_missing(output: str, req_key: str) -> str: +def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") -> str: """ implement the req_key pair in the begin or end of the content req_key format 1. `[req_key]`, and its pair `[/req_key]` 2. `[/req_key]`, and its pair `[req_key]` """ + sc = "/" # special char if req_key.startswith("[") and req_key.endswith("]"): - if "/" in req_key: - left_key = req_key.replace("/", "") # `[/req_key]` -> `[req_key]` + if sc in req_key: + left_key = req_key.replace(sc, "") # `[/req_key]` -> `[req_key]` right_key = req_key else: left_key = req_key - right_key = f"{req_key[0]}/{req_key[1:]}" # `[req_key]` -> `[/req_key]` + right_key = f"{req_key[0]}{sc}{req_key[1:]}" # `[req_key]` -> `[/req_key]` if left_key not in output: - output = left_key + output + output = left_key + "\n" + output if right_key not in output: - output = output + right_key + def judge_potential_json(routput: str, left_key: str) -> Union[str, bool]: + routput = copy.deepcopy(routput) + ridx = routput.rfind(left_key) + if ridx < 0: + return None + sub_output = routput[ridx:] + idx1 = sub_output.rfind("}") + idx2 = sub_output.rindex("]") + idx = idx1 if idx1 >= idx2 else idx2 + sub_output = sub_output[: idx] + return sub_output + + if output.strip().endswith("}") or (output.strip().endswith("]") and not output.strip().endswith(left_key)): + # # avoid [req_key]xx[req_key] case to append [/req_key] + output = output + "\n" + right_key + elif judge_potential_json(output, left_key): + sub_content = judge_potential_json(output, left_key) + output = sub_content + "\n" + right_key return output @@ -106,12 +127,12 @@ def _repair_llm_raw_output(output: str, req_key: str, repair_type: RepairType = for repair_type in repair_types: if repair_type == RepairType.CS: output = repair_case_sensitivity(output, req_key) + elif repair_type == RepairType.RKPM: + output = repair_required_key_pair_missing(output, req_key) elif repair_type == RepairType.SCM: output = repair_special_character_missing(output, req_key) elif repair_type == RepairType.JSON: output = repair_json_format(output) - elif repair_type == RepairType.RKPM: - output = repair_required_key_pair_missing(output, req_key) return output @@ -178,25 +199,58 @@ def repair_invalid_json(output: str, error: str) -> str: return output -def retry_parse_json_text(output: str, retry: int = 5) -> Union[list, dict]: +def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["RetryCallState"], None]: + def run_and_passon(retry_state: RetryCallState) -> None: + """ + RetryCallState example + { + "start_time":143.098322024, + "retry_object":")>", + "fn":"", + "args":"(\"tag:[/CONTENT]\",)", # function input args + "kwargs":{}, # function input kwargs + "attempt_number":1, # retry number + "outcome":"", # type(outcome.result()) = "str", type(outcome.exception()) = "class" + "outcome_timestamp":143.098416904, + "idle_for":0, + "next_action":"None" + } + """ + if retry_state.outcome.failed: + if len(retry_state.args) > 0: + # # can't used as args=retry_state.args + func_param_output = retry_state.args[0] + elif len(retry_state.kwargs) > 0: + func_param_output = retry_state.kwargs.get("output", "") + # import pdb; pdb.set_trace() + exp_str = str(retry_state.outcome.exception()) + logger.warning(f"parse json from content inside [CONTENT][/CONTENT] failed at retry " + f"{retry_state.attempt_number}, try to fix it, exp: {exp_str}") + + repaired_output = repair_invalid_json(func_param_output, exp_str) + retry_state.kwargs["output"] = repaired_output + + return run_and_passon + + +@retry( + stop=stop_after_attempt(3 if CONFIG.repair_llm_output else 0), + wait=wait_fixed(1), + after=run_after_exp_and_passon_next_retry(logger), +) +def retry_parse_json_text(output: str) -> Union[list, dict]: """ repair the json-text situation like there are extra chars like [']', '}'] + + Warning + if CONFIG.repair_llm_output is False, retry _aask_v1 {x=3} times, and the retry_parse_json_text's retry not work + if CONFIG.repair_llm_output is True, the _aask_v1 and the retry_parse_json_text will loop for {x=3*3} times. + it's a two-layer retry cycle """ - parsed_data = {} - for idx in range(retry): - raw_output = copy.deepcopy(output) + logger.debug(f"output to json decode:\n{output}") - try: - parsed_data = CustomDecoder(strict=False).decode(output) - break - except Exception as exp: - if not CONFIG.repair_llm_output: - # if repair_llm_output is False, break from the retry loop - break - - logger.warning(f"decode content into json failed, try to fix it. exp: {exp}") - error = str(exp) - output = repair_invalid_json(output, error) + # if CONFIG.repair_llm_output is True, it will try to fix output until the retry break + parsed_data = CustomDecoder(strict=False).decode(output) return parsed_data diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 39a7343e7..dfcf60ad5 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -42,20 +42,69 @@ def test_repair_special_character_missing(): req_keys=req_keys) assert output == target_output + raw_output = """[CONTENT] tag +[CONTENT] +{ +"Anything UNCLEAR": "No unclear requirements or information." +} +[CONTENT]""" + target_output = """[CONTENT] tag +[CONTENT] +{ +"Anything UNCLEAR": "No unclear requirements or information." +} +[/CONTENT]""" + output = repair_llm_raw_output(output=raw_output, + req_keys=req_keys) + assert output == target_output + + raw_output = '[CONTENT] {"a": "b"} [CONTENT]' + target_output = '[CONTENT] {"a": "b"} [/CONTENT]' + + output = repair_llm_raw_output(output=raw_output, + req_keys=["[/CONTENT]"]) + print("output\n", output) + assert output == target_output + def test_required_key_pair_missing(): - raw_output = "[CONTENT] xxx" - target_output = "[CONTENT] xxx[/CONTENT]" + raw_output = '[CONTENT] {"a": "b"}' + target_output = '[CONTENT] {"a": "b"}\n[/CONTENT]' output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) assert output == target_output - raw_output = "xxx[/CONTENT]" - target_output = "[CONTENT]xxx[/CONTENT]" + raw_output = '''[CONTENT] +{ + "a": "b" +]''' + target_output = '''[CONTENT] +{ + "a": "b" +] +[/CONTENT]''' output = repair_llm_raw_output(output=raw_output, - req_keys=["[CONTENT]"]) + req_keys=["[/CONTENT]"]) + assert output == target_output + + raw_output = '''[CONTENT] tag +[CONTENT] +{ + "a": "b" +} +xxx +''' + target_output = '''[CONTENT] tag +[CONTENT] +{ + "a": "b" +} +[/CONTENT] +''' + output = repair_llm_raw_output(output=raw_output, + req_keys=["[/CONTENT]"]) assert output == target_output @@ -100,6 +149,13 @@ def test_retry_parse_json_text(): def test_extract_content_from_output(): + """ + cases + xxx [CONTENT] xxxx [/CONTENT] + xxx [CONTENT] xxx [CONTENT] xxxx [/CONTENT] + xxx [CONTENT] xxxx [/CONTENT] xxx [CONTENT][/CONTENT] xxx [CONTENT][/CONTENT] # target pair is the last one + """ + output = 'Sure! Here is the properly formatted JSON output based on the given context:\n\n[CONTENT]\n{\n"' \ 'Required Python third-party packages": [\n"pygame==2.0.4",\n"pytest"\n],\n"Required Other language ' \ 'third-party packages": [\n"No third-party packages are required."\n],\n"Full API spec": "\nopenapi: ' \ From fc5c01e21943edc7b84376f62e5d6c9ef5634203 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 22 Nov 2023 13:56:49 +0800 Subject: [PATCH 128/413] fix --- metagpt/utils/repair_llm_raw_output.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index c26dc838d..a12a36fcc 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -82,7 +82,7 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - if left_key not in output: output = left_key + "\n" + output if right_key not in output: - def judge_potential_json(routput: str, left_key: str) -> Union[str, bool]: + def judge_potential_json(routput: str, left_key: str) -> Union[str]: routput = copy.deepcopy(routput) ridx = routput.rfind(left_key) if ridx < 0: From 8f24808c8a39763b823bd85f643390ca458c7a5c Mon Sep 17 00:00:00 2001 From: seehi <6580@pm.me> Date: Wed, 22 Nov 2023 14:25:07 +0800 Subject: [PATCH 129/413] make examples/search_kb.py work --- examples/search_kb.py | 26 +++++++++++++++++++++---- metagpt/actions/search_and_summarize.py | 1 - metagpt/document_store/faiss_store.py | 18 ++++++++++------- metagpt/roles/sales.py | 23 +++++++++++----------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/examples/search_kb.py b/examples/search_kb.py index b6f7d87a0..7a9911ca2 100644 --- a/examples/search_kb.py +++ b/examples/search_kb.py @@ -5,22 +5,40 @@ """ import asyncio +from metagpt.actions import Action from metagpt.const import DATA_PATH from metagpt.document_store import FaissStore from metagpt.logs import logger from metagpt.roles import Sales +from metagpt.schema import Message + +""" example.json, e.g. +[ + { + "source": "Which facial cleanser is good for oily skin?", + "output": "ABC cleanser is preferred by many with oily skin." + }, + { + "source": "Is L'Oreal good to use?", + "output": "L'Oreal is a popular brand with many positive reviews." + } +] +""" async def search(): - store = FaissStore(DATA_PATH / 'example.json') + store = FaissStore(DATA_PATH / "example.json") role = Sales(profile="Sales", store=store) - - queries = ["Which facial cleanser is good for oily skin?", "Is L'Oreal good to use?"] + role._watch({Action}) + queries = [ + Message("Which facial cleanser is good for oily skin?", cause_by=Action), + Message("Is L'Oreal good to use?", cause_by=Action), + ] for query in queries: logger.info(f"User: {query}") result = await role.run(query) logger.info(result) -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(search()) diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 069f2a977..5e4cdaea0 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -140,4 +140,3 @@ class SearchAndSummarize(Action): logger.debug(prompt) logger.debug(result) return result - \ No newline at end of file diff --git a/metagpt/document_store/faiss_store.py b/metagpt/document_store/faiss_store.py index dd450010d..5c6459179 100644 --- a/metagpt/document_store/faiss_store.py +++ b/metagpt/document_store/faiss_store.py @@ -5,6 +5,7 @@ @Author : alexanderwu @File : faiss_store.py """ +import asyncio import pickle from pathlib import Path from typing import Optional @@ -20,7 +21,7 @@ from metagpt.logs import logger class FaissStore(LocalStore): - def __init__(self, raw_data: Path, cache_dir=None, meta_col='source', content_col='output'): + def __init__(self, raw_data: Path, cache_dir=None, meta_col="source", content_col="output"): self.meta_col = meta_col self.content_col = content_col super().__init__(raw_data, cache_dir) @@ -50,7 +51,7 @@ class FaissStore(LocalStore): pickle.dump(store, f) store.index = index - def search(self, query, expand_cols=False, sep='\n', *args, k=5, **kwargs): + def search(self, query, expand_cols=False, sep="\n", *args, k=5, **kwargs): rsp = self.store.similarity_search(query, k=k, **kwargs) logger.debug(rsp) if expand_cols: @@ -58,6 +59,9 @@ class FaissStore(LocalStore): else: return str(sep.join([f"{x.page_content}" for x in rsp])) + async def asearch(self, *args, **kwargs): + return await asyncio.to_thread(self.search, *args, **kwargs) + def write(self): """Initialize the index and library based on the Document (JSON / XLSX, etc.) file provided by the user.""" if not self.raw_data.exists(): @@ -78,8 +82,8 @@ class FaissStore(LocalStore): raise NotImplementedError -if __name__ == '__main__': - faiss_store = FaissStore(DATA_PATH / 'qcs/qcs_4w.json') - logger.info(faiss_store.search('Oily Skin Facial Cleanser')) - faiss_store.add([f'Oily Skin Facial Cleanser-{i}' for i in range(3)]) - logger.info(faiss_store.search('Oily Skin Facial Cleanser')) +if __name__ == "__main__": + faiss_store = FaissStore(DATA_PATH / "qcs/qcs_4w.json") + logger.info(faiss_store.search("Oily Skin Facial Cleanser")) + faiss_store.add([f"Oily Skin Facial Cleanser-{i}" for i in range(3)]) + logger.info(faiss_store.search("Oily Skin Facial Cleanser")) diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index a45ad6f1b..d5aac1824 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -12,24 +12,23 @@ from metagpt.tools import SearchEngineType class Sales(Role): def __init__( - self, - name="Xiaomei", - profile="Retail sales guide", - desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " - "will answer questions only based on the information in the knowledge base." - "If I feel that you can't get the answer from the reference material, then I will directly reply that" - " I don't know, and I won't tell you that this is from the knowledge base," - "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " - "professional guide", - store=None + self, + name="Xiaomei", + profile="Retail sales guide", + desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " + "will answer questions only based on the information in the knowledge base." + "If I feel that you can't get the answer from the reference material, then I will directly reply that" + " I don't know, and I won't tell you that this is from the knowledge base," + "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " + "professional guide", + store=None, ): super().__init__(name, profile, desc=desc) self._set_store(store) def _set_store(self, store): if store: - action = SearchAndSummarize("", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.search) + action = SearchAndSummarize("", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.asearch) else: action = SearchAndSummarize() self._init_actions([action]) - \ No newline at end of file From 5142cb59f7120e564d014e8e3ab2e69698e0972e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 15:59:35 +0800 Subject: [PATCH 130/413] refactor: consumers -> members --- metagpt/environment.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index b3c296dac..02eb3d340 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -29,7 +29,7 @@ class Environment(BaseModel): """ roles: dict[str, Role] = Field(default_factory=dict) - consumers: dict[Role, Set] = Field(default_factory=dict) + members: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug class Config: @@ -61,7 +61,7 @@ class Environment(BaseModel): logger.info(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 - for role, subscription in self.consumers.items(): + for role, subscription in self.members.items(): if is_subscribed(message, subscription): role.put_message(message) found = True @@ -106,8 +106,8 @@ class Environment(BaseModel): def get_subscription(self, obj): """Get the labels for messages to be consumed by the object.""" - return self.consumers.get(obj, {}) + return self.members.get(obj, {}) def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" - self.consumers[obj] = tags + self.members[obj] = tags From cda032948f855995d7d4a21fef5b0dc800a47499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 16:26:48 +0800 Subject: [PATCH 131/413] refactor: pre-commit run --all-files --- examples/agent_creator.py | 16 +-- examples/build_customized_agent.py | 22 ++-- examples/debate.py | 27 +++-- examples/invoice_ocr.py | 10 +- examples/llm_hello_world.py | 10 +- examples/research.py | 2 +- examples/search_google.py | 2 +- examples/search_kb.py | 4 +- examples/search_with_specific_engine.py | 7 +- examples/use_off_the_shelf_agent.py | 10 +- examples/write_tutorial.py | 3 +- metagpt/actions/action_output.py | 7 +- metagpt/actions/add_requirement.py | 1 + metagpt/actions/azure_tts.py | 18 +-- metagpt/actions/clone_function.py | 6 +- metagpt/actions/debug_error.py | 10 +- metagpt/actions/design_api_review.py | 9 +- metagpt/actions/design_filenames.py | 7 +- metagpt/actions/detail_mining.py | 5 +- metagpt/actions/invoice_ocr.py | 8 +- metagpt/actions/prepare_interview.py | 1 - metagpt/actions/research.py | 27 +++-- metagpt/actions/search_and_summarize.py | 1 - metagpt/actions/write_code.py | 6 +- metagpt/actions/write_code_review.py | 6 +- metagpt/actions/write_docstring.py | 7 +- metagpt/actions/write_prd_review.py | 1 - metagpt/actions/write_tutorial.py | 3 +- metagpt/config.py | 2 +- metagpt/document_store/base_store.py | 3 +- metagpt/document_store/chromadb_store.py | 3 +- metagpt/document_store/document.py | 20 ++-- metagpt/document_store/faiss_store.py | 14 +-- metagpt/document_store/milvus_store.py | 23 +--- metagpt/document_store/qdrant_store.py | 27 ++--- metagpt/environment.py | 16 +-- metagpt/inspect_module.py | 10 +- metagpt/llm.py | 5 +- metagpt/logs.py | 6 +- metagpt/management/skill_manager.py | 8 +- metagpt/manager.py | 4 +- metagpt/memory/longterm_memory.py | 1 - metagpt/memory/memory.py | 1 - metagpt/memory/memory_storage.py | 24 ++-- metagpt/prompts/invoice_ocr.py | 11 +- metagpt/prompts/sales.py | 16 +-- metagpt/prompts/tutorial_assistant.py | 12 +- metagpt/provider/anthropic_api.py | 1 - metagpt/provider/base_chatbot.py | 2 +- metagpt/provider/base_gpt_api.py | 6 +- metagpt/provider/openai_api.py | 2 - metagpt/provider/spark_api.py | 61 ++++------ metagpt/roles/customer_service.py | 9 +- metagpt/roles/invoice_ocr_assistant.py | 3 +- metagpt/roles/prompt.py | 5 +- metagpt/roles/role.py | 34 ++++-- metagpt/roles/sales.py | 21 ++-- metagpt/roles/seacher.py | 32 +++--- metagpt/roles/tutorial_assistant.py | 4 +- metagpt/schema.py | 29 ++--- metagpt/software_company.py | 6 +- metagpt/tools/code_interpreter.py | 61 ++++++---- metagpt/tools/prompt_writer.py | 7 +- metagpt/tools/sd_engine.py | 2 + metagpt/tools/search_engine.py | 6 +- metagpt/tools/search_engine_meilisearch.py | 4 +- metagpt/tools/translator.py | 9 +- metagpt/tools/ut_writer.py | 28 +++-- metagpt/utils/file.py | 6 +- metagpt/utils/highlight.py | 12 +- metagpt/utils/mmdc_ink.py | 6 +- metagpt/utils/mmdc_playwright.py | 92 ++++++++------- metagpt/utils/mmdc_pyppeteer.py | 107 ++++++++++-------- metagpt/utils/parse_html.py | 4 +- metagpt/utils/pycst.py | 12 +- metagpt/utils/read_document.py | 1 + metagpt/utils/singleton.py | 1 - metagpt/utils/special_tokens.py | 2 +- metagpt/utils/text.py | 13 ++- tests/conftest.py | 4 +- tests/metagpt/actions/mock.py | 14 +-- tests/metagpt/actions/test_action_output.py | 29 ++--- tests/metagpt/actions/test_azure_tts.py | 7 +- tests/metagpt/actions/test_clone_function.py | 21 ++-- tests/metagpt/actions/test_debug_error.py | 6 +- tests/metagpt/actions/test_detail_mining.py | 6 +- tests/metagpt/actions/test_invoice_ocr.py | 28 ++--- tests/metagpt/actions/test_ui_design.py | 20 ++-- tests/metagpt/actions/test_write_code.py | 6 +- tests/metagpt/actions/test_write_docstring.py | 6 +- tests/metagpt/actions/test_write_tutorial.py | 9 +- .../document_store/test_chromadb_store.py | 8 +- .../document_store/test_faiss_store.py | 13 ++- .../document_store/test_lancedb_store.py | 28 +++-- .../document_store/test_milvus_store.py | 10 +- .../document_store/test_qdrant_store.py | 12 +- .../metagpt/management/test_skill_manager.py | 2 +- tests/metagpt/memory/test_longterm_memory.py | 24 ++-- tests/metagpt/memory/test_memory_storage.py | 60 ++++------ tests/metagpt/provider/test_base_gpt_api.py | 6 +- tests/metagpt/provider/test_spark_api.py | 2 +- tests/metagpt/roles/mock.py | 18 +-- tests/metagpt/roles/test_engineer.py | 4 +- .../roles/test_invoice_ocr_assistant.py | 67 ++--------- tests/metagpt/roles/test_researcher.py | 6 +- .../metagpt/roles/test_tutorial_assistant.py | 7 +- tests/metagpt/roles/test_ui.py | 3 +- tests/metagpt/test_gpt.py | 10 +- tests/metagpt/test_llm.py | 6 +- tests/metagpt/test_message.py | 18 +-- tests/metagpt/test_role.py | 6 +- tests/metagpt/test_schema.py | 6 +- tests/metagpt/tools/test_code_interpreter.py | 29 ++--- tests/metagpt/tools/test_prompt_generator.py | 12 +- tests/metagpt/tools/test_sd_tool.py | 2 +- tests/metagpt/tools/test_search_engine.py | 12 +- .../tools/test_search_engine_meilisearch.py | 6 +- tests/metagpt/tools/test_summarize.py | 3 +- tests/metagpt/tools/test_translate.py | 2 +- tests/metagpt/tools/test_ut_generator.py | 8 +- tests/metagpt/utils/test_code_parser.py | 4 +- tests/metagpt/utils/test_common.py | 4 +- tests/metagpt/utils/test_config.py | 6 +- .../metagpt/utils/test_custom_aio_session.py | 4 +- tests/metagpt/utils/test_file.py | 5 +- tests/metagpt/utils/test_output_parser.py | 24 ++-- tests/metagpt/utils/test_parse_html.py | 8 +- tests/metagpt/utils/test_pycst.py | 4 +- tests/metagpt/utils/test_text.py | 8 +- 129 files changed, 812 insertions(+), 831 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 325e7c260..3618c0608 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -1,22 +1,22 @@ -''' +""" Filename: MetaGPT/examples/agent_creator.py Created Date: Tuesday, September 12th 2023, 3:28:37 pm Author: garylin2099 -''' +""" import re -from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT from metagpt.actions import Action +from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger with open(PROJECT_ROOT / "examples/build_customized_agent.py", "r") as f: # use official example script to guide AgentCreator MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read() -class CreateAgent(Action): +class CreateAgent(Action): PROMPT_TEMPLATE = """ ### BACKGROUND You are using an agent framework called metagpt to write agents capable of different actions, @@ -34,7 +34,6 @@ class CreateAgent(Action): """ async def run(self, example: str, instruction: str): - prompt = self.PROMPT_TEMPLATE.format(example=example, instruction=instruction) # logger.info(prompt) @@ -46,13 +45,14 @@ class CreateAgent(Action): @staticmethod def parse_code(rsp): - pattern = r'```python(.*)```' + pattern = r"```python(.*)```" match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" with open(WORKSPACE_ROOT / "agent_created_agent.py", "w") as f: f.write(code_text) return code_text + class AgentCreator(Role): def __init__( self, @@ -76,11 +76,11 @@ class AgentCreator(Role): return msg + if __name__ == "__main__": import asyncio async def main(): - agent_template = MULTI_ACTION_AGENT_CODE_EXAMPLE creator = AgentCreator(agent_template=agent_template) diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index 87d7a9c76..ef274be8b 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -1,21 +1,21 @@ -''' +""" Filename: MetaGPT/examples/build_customized_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -''' +""" +import asyncio import re import subprocess -import asyncio import fire from metagpt.actions import Action +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger + class SimpleWriteCode(Action): - PROMPT_TEMPLATE = """ Write a python function that can {instruction} and provide two runnnable test cases. Return ```python your_code_here ``` with NO other texts, @@ -35,7 +35,6 @@ class SimpleWriteCode(Action): super().__init__(name, context, llm) async def run(self, instruction: str): - prompt = self.PROMPT_TEMPLATE.format(instruction=instruction) rsp = await self._aask(prompt) @@ -46,11 +45,12 @@ class SimpleWriteCode(Action): @staticmethod def parse_code(rsp): - pattern = r'```python(.*)```' + pattern = r"```python(.*)```" match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else rsp return code_text + class SimpleRunCode(Action): def __init__(self, name="SimpleRunCode", context=None, llm=None): super().__init__(name, context, llm) @@ -61,6 +61,7 @@ class SimpleRunCode(Action): logger.info(f"{code_result=}") return code_result + class SimpleCoder(Role): def __init__( self, @@ -75,7 +76,7 @@ class SimpleCoder(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") todo = self._rc.todo - msg = self._rc.memory.get()[-1] # retrieve the latest memory + msg = self._rc.memory.get()[-1] # retrieve the latest memory instruction = msg.content code_text = await SimpleWriteCode().run(instruction) @@ -83,6 +84,7 @@ class SimpleCoder(Role): return msg + class RunnableCoder(Role): def __init__( self, @@ -128,6 +130,7 @@ class RunnableCoder(Role): await self._act() return Message(content="All job done", role=self.profile) + def main(msg="write a function that calculates the sum of a list"): # role = SimpleCoder() role = RunnableCoder() @@ -135,5 +138,6 @@ def main(msg="write a function that calculates the sum of a list"): result = asyncio.run(role.run(msg)) logger.info(result) -if __name__ == '__main__': + +if __name__ == "__main__": fire.Fire(main) diff --git a/examples/debate.py b/examples/debate.py index 05db28070..54da73cca 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -1,17 +1,19 @@ -''' +""" Filename: MetaGPT/examples/debate.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -''' +""" import asyncio import platform + import fire -from metagpt.software_company import SoftwareCompany from metagpt.actions import Action, BossRequirement +from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.logs import logger +from metagpt.software_company import SoftwareCompany + class ShoutOut(Action): """Action: Shout out loudly in a debate (quarrel)""" @@ -31,7 +33,6 @@ class ShoutOut(Action): super().__init__(name, context, llm) async def run(self, context: str, name: str, opponent_name: str): - prompt = self.PROMPT_TEMPLATE.format(context=context, name=name, opponent_name=opponent_name) # logger.info(prompt) @@ -39,6 +40,7 @@ class ShoutOut(Action): return rsp + class Trump(Role): def __init__( self, @@ -55,7 +57,7 @@ class Trump(Role): async def _observe(self) -> int: await super()._observe() # accept messages sent (from opponent) to self, disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.send_to == self.name] + self._rc.news = [msg for msg in self._rc.news if msg.send_to == self.name] return len(self._rc.news) async def _act(self) -> Message: @@ -79,6 +81,7 @@ class Trump(Role): return msg + class Biden(Role): def __init__( self, @@ -120,10 +123,12 @@ class Biden(Role): return msg -async def startup(idea: str, investment: float = 3.0, n_round: int = 5, - code_review: bool = False, run_tests: bool = False): + +async def startup( + idea: str, investment: float = 3.0, n_round: int = 5, code_review: bool = False, run_tests: bool = False +): """We reuse the startup paradigm for roles to interact with each other. - Now we run a startup of presidents and watch they quarrel. :) """ + Now we run a startup of presidents and watch they quarrel. :)""" company = SoftwareCompany() company.hire([Biden(), Trump()]) company.invest(investment) @@ -133,7 +138,7 @@ async def startup(idea: str, investment: float = 3.0, n_round: int = 5, def main(idea: str, investment: float = 3.0, n_round: int = 10): """ - :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting" + :param idea: Debate topic, such as "Topic: The U.S. should commit more in climate change fighting" or "Trump: Climate change is a hoax" :param investment: contribute a certain dollar amount to watch the debate :param n_round: maximum rounds of the debate @@ -144,5 +149,5 @@ def main(idea: str, investment: float = 3.0, n_round: int = 10): asyncio.run(startup(idea, investment, n_round)) -if __name__ == '__main__': +if __name__ == "__main__": fire.Fire(main) diff --git a/examples/invoice_ocr.py b/examples/invoice_ocr.py index 11656ed52..a6e565772 100644 --- a/examples/invoice_ocr.py +++ b/examples/invoice_ocr.py @@ -19,19 +19,15 @@ async def main(): Path("../tests/data/invoices/invoice-1.pdf"), Path("../tests/data/invoices/invoice-2.png"), Path("../tests/data/invoices/invoice-3.jpg"), - Path("../tests/data/invoices/invoice-4.zip") + Path("../tests/data/invoices/invoice-4.zip"), ] # The absolute path of the file absolute_file_paths = [Path.cwd() / path for path in relative_paths] for path in absolute_file_paths: role = InvoiceOCRAssistant() - await role.run(Message( - content="Invoicing date", - instruct_content={"file_path": path} - )) + await role.run(Message(content="Invoicing date", instruct_content={"file_path": path})) -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) - diff --git a/examples/llm_hello_world.py b/examples/llm_hello_world.py index 3ba03eea0..677098399 100644 --- a/examples/llm_hello_world.py +++ b/examples/llm_hello_world.py @@ -14,11 +14,11 @@ from metagpt.logs import logger async def main(): llm = LLM() claude = Claude() - logger.info(await claude.aask('你好,请进行自我介绍')) - logger.info(await llm.aask('hello world')) - logger.info(await llm.aask_batch(['hi', 'write python hello world.'])) + logger.info(await claude.aask("你好,请进行自我介绍")) + logger.info(await llm.aask("hello world")) + logger.info(await llm.aask_batch(["hi", "write python hello world."])) - hello_msg = [{'role': 'user', 'content': 'count from 1 to 10. split by newline.'}] + hello_msg = [{"role": "user", "content": "count from 1 to 10. split by newline."}] logger.info(await llm.acompletion(hello_msg)) logger.info(await llm.acompletion_batch([hello_msg])) logger.info(await llm.acompletion_batch_text([hello_msg])) @@ -27,5 +27,5 @@ async def main(): await llm.acompletion_text(hello_msg, stream=True) -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/research.py b/examples/research.py index 344f8d0e9..5c371cdd2 100644 --- a/examples/research.py +++ b/examples/research.py @@ -12,5 +12,5 @@ async def main(): print(f"save report to {RESEARCH_PATH / f'{topic}.md'}.") -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/search_google.py b/examples/search_google.py index 9e9521b9c..73d04bf87 100644 --- a/examples/search_google.py +++ b/examples/search_google.py @@ -15,5 +15,5 @@ async def main(): await Searcher().run("What are some good sun protection products?") -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/search_kb.py b/examples/search_kb.py index b6f7d87a0..0b5d59385 100644 --- a/examples/search_kb.py +++ b/examples/search_kb.py @@ -12,7 +12,7 @@ from metagpt.roles import Sales async def search(): - store = FaissStore(DATA_PATH / 'example.json') + store = FaissStore(DATA_PATH / "example.json") role = Sales(profile="Sales", store=store) queries = ["Which facial cleanser is good for oily skin?", "Is L'Oreal good to use?"] @@ -22,5 +22,5 @@ async def search(): logger.info(result) -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(search()) diff --git a/examples/search_with_specific_engine.py b/examples/search_with_specific_engine.py index 7cc431cd4..334a7821f 100644 --- a/examples/search_with_specific_engine.py +++ b/examples/search_with_specific_engine.py @@ -6,11 +6,12 @@ from metagpt.tools import SearchEngineType async def main(): # Serper API - #await Searcher(engine = SearchEngineType.SERPER_GOOGLE).run(["What are some good sun protection products?","What are some of the best beaches?"]) + # await Searcher(engine = SearchEngineType.SERPER_GOOGLE).run(["What are some good sun protection products?","What are some of the best beaches?"]) # SerpAPI - #await Searcher(engine=SearchEngineType.SERPAPI_GOOGLE).run("What are the best ski brands for skiers?") + # await Searcher(engine=SearchEngineType.SERPAPI_GOOGLE).run("What are the best ski brands for skiers?") # Google API await Searcher(engine=SearchEngineType.DIRECT_GOOGLE).run("What are the most interesting human facts?") -if __name__ == '__main__': + +if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/use_off_the_shelf_agent.py b/examples/use_off_the_shelf_agent.py index 2e10068bd..4445a6c62 100644 --- a/examples/use_off_the_shelf_agent.py +++ b/examples/use_off_the_shelf_agent.py @@ -1,12 +1,13 @@ -''' +""" Filename: MetaGPT/examples/use_off_the_shelf_agent.py Created Date: Tuesday, September 19th 2023, 6:52:25 pm Author: garylin2099 -''' +""" import asyncio -from metagpt.roles.product_manager import ProductManager from metagpt.logs import logger +from metagpt.roles.product_manager import ProductManager + async def main(): msg = "Write a PRD for a snake game" @@ -14,5 +15,6 @@ async def main(): result = await role.run(msg) logger.info(result.content[:100]) -if __name__ == '__main__': + +if __name__ == "__main__": asyncio.run(main()) diff --git a/examples/write_tutorial.py b/examples/write_tutorial.py index 71ece5527..0dba3cdb7 100644 --- a/examples/write_tutorial.py +++ b/examples/write_tutorial.py @@ -16,6 +16,5 @@ async def main(): await role.run(topic) -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) - diff --git a/metagpt/actions/action_output.py b/metagpt/actions/action_output.py index ea7f4fb80..25326d43b 100644 --- a/metagpt/actions/action_output.py +++ b/metagpt/actions/action_output.py @@ -23,10 +23,10 @@ class ActionOutput: def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): new_class = create_model(class_name, **mapping) - @validator('*', allow_reuse=True) + @validator("*", allow_reuse=True) def check_name(v, field): if field.name not in mapping.keys(): - raise ValueError(f'Unrecognized block: {field.name}') + raise ValueError(f"Unrecognized block: {field.name}") return v @root_validator(pre=True, allow_reuse=True) @@ -34,10 +34,9 @@ class ActionOutput: required_fields = set(mapping.keys()) missing_fields = required_fields - set(values.keys()) if missing_fields: - raise ValueError(f'Missing fields: {missing_fields}') + raise ValueError(f"Missing fields: {missing_fields}") return values new_class.__validator_check_name = classmethod(check_name) new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) return new_class - \ No newline at end of file diff --git a/metagpt/actions/add_requirement.py b/metagpt/actions/add_requirement.py index 7dc09d062..16e14b3a4 100644 --- a/metagpt/actions/add_requirement.py +++ b/metagpt/actions/add_requirement.py @@ -10,5 +10,6 @@ from metagpt.actions import Action class BossRequirement(Action): """Boss Requirement without any implementation details""" + async def run(self, *args, **kwargs): raise NotImplementedError diff --git a/metagpt/actions/azure_tts.py b/metagpt/actions/azure_tts.py index c13a4750d..daa3f6892 100644 --- a/metagpt/actions/azure_tts.py +++ b/metagpt/actions/azure_tts.py @@ -18,16 +18,13 @@ class AzureTTS(Action): # Parameters reference: https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles def synthesize_speech(self, lang, voice, role, text, output_file): - subscription_key = self.config.get('AZURE_TTS_SUBSCRIPTION_KEY') - region = self.config.get('AZURE_TTS_REGION') - speech_config = SpeechConfig( - subscription=subscription_key, region=region) + subscription_key = self.config.get("AZURE_TTS_SUBSCRIPTION_KEY") + region = self.config.get("AZURE_TTS_REGION") + speech_config = SpeechConfig(subscription=subscription_key, region=region) speech_config.speech_synthesis_voice_name = voice audio_config = AudioConfig(filename=output_file) - synthesizer = SpeechSynthesizer( - speech_config=speech_config, - audio_config=audio_config) + synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config) # if voice=="zh-CN-YunxiNeural": ssml_string = f""" @@ -45,9 +42,4 @@ class AzureTTS(Action): if __name__ == "__main__": azure_tts = AzureTTS("azure_tts") - azure_tts.synthesize_speech( - "zh-CN", - "zh-CN-YunxiNeural", - "Boy", - "Hello, I am Kaka", - "output.wav") + azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "Hello, I am Kaka", "output.wav") diff --git a/metagpt/actions/clone_function.py b/metagpt/actions/clone_function.py index cf7d22f04..1447e8dbf 100644 --- a/metagpt/actions/clone_function.py +++ b/metagpt/actions/clone_function.py @@ -1,5 +1,5 @@ -from pathlib import Path import traceback +from pathlib import Path from metagpt.actions.write_code import WriteCode from metagpt.logs import logger @@ -42,7 +42,7 @@ class CloneFunction(WriteCode): prompt = CLONE_PROMPT.format(source_code=source_code, template_func=template_func) logger.info(f"query for CloneFunction: \n {prompt}") code = await self.write_code(prompt) - logger.info(f'CloneFunction code is \n {highlight(code)}') + logger.info(f"CloneFunction code is \n {highlight(code)}") return code @@ -61,5 +61,5 @@ def run_function_script(code_script_path: str, func_name: str, *args, **kwargs): """Run function code from script.""" if isinstance(code_script_path, str): code_path = Path(code_script_path) - code = code_path.read_text(encoding='utf-8') + code = code_path.read_text(encoding="utf-8") return run_function_code(code, func_name, *args, **kwargs) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index d69a22dba..304b1bc3e 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -7,8 +7,8 @@ """ import re -from metagpt.logs import logger from metagpt.actions.action import Action +from metagpt.logs import logger from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -24,6 +24,8 @@ The message is as follows: Now you should start rewriting the code: ## file name of the code to rewrite: Write code with triple quoto. Do your best to implement THIS IN ONLY ONE FILE. """ + + class DebugError(Action): def __init__(self, name="DebugError", context=None, llm=None): super().__init__(name, context, llm) @@ -33,17 +35,17 @@ class DebugError(Action): # f"\n\n{error}\n\nPlease try to fix the error in this code." # fixed_code = await self._aask(prompt) # return fixed_code - + async def run(self, context): if "PASS" in context: return "", "the original code works fine, no need to debug" - + file_name = re.search("## File To Rewrite:\s*(.+\\.py)", context).group(1) logger.info(f"Debug and rewrite {file_name}") prompt = PROMPT_TEMPLATE.format(context=context) - + rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=rsp) diff --git a/metagpt/actions/design_api_review.py b/metagpt/actions/design_api_review.py index 9bb822a62..7f25bb9a3 100644 --- a/metagpt/actions/design_api_review.py +++ b/metagpt/actions/design_api_review.py @@ -13,10 +13,11 @@ class DesignReview(Action): super().__init__(name, context, llm) async def run(self, prd, api_design): - prompt = f"Here is the Product Requirement Document (PRD):\n\n{prd}\n\nHere is the list of APIs designed " \ - f"based on this PRD:\n\n{api_design}\n\nPlease review whether this API design meets the requirements" \ - f" of the PRD, and whether it complies with good design practices." + prompt = ( + f"Here is the Product Requirement Document (PRD):\n\n{prd}\n\nHere is the list of APIs designed " + f"based on this PRD:\n\n{api_design}\n\nPlease review whether this API design meets the requirements" + f" of the PRD, and whether it complies with good design practices." + ) api_review = await self._aask(prompt) return api_review - \ No newline at end of file diff --git a/metagpt/actions/design_filenames.py b/metagpt/actions/design_filenames.py index 29400e950..ffa171d7b 100644 --- a/metagpt/actions/design_filenames.py +++ b/metagpt/actions/design_filenames.py @@ -17,8 +17,10 @@ Do not add any other explanations, just return a Python string list.""" class DesignFilenames(Action): def __init__(self, name, context=None, llm=None): super().__init__(name, context, llm) - self.desc = "Based on the PRD, consider system design, and carry out the basic design of the corresponding " \ - "APIs, data structures, and database tables. Please give your design, feedback clearly and in detail." + self.desc = ( + "Based on the PRD, consider system design, and carry out the basic design of the corresponding " + "APIs, data structures, and database tables. Please give your design, feedback clearly and in detail." + ) async def run(self, prd): prompt = f"The following is the Product Requirement Document (PRD):\n\n{prd}\n\n{PROMPT}" @@ -26,4 +28,3 @@ class DesignFilenames(Action): logger.debug(prompt) logger.debug(design_filenames) return design_filenames - \ No newline at end of file diff --git a/metagpt/actions/detail_mining.py b/metagpt/actions/detail_mining.py index e29d6911b..5afcf52c6 100644 --- a/metagpt/actions/detail_mining.py +++ b/metagpt/actions/detail_mining.py @@ -6,7 +6,6 @@ @File : detail_mining.py """ from metagpt.actions import Action, ActionOutput -from metagpt.logs import logger PROMPT_TEMPLATE = """ ##TOPIC @@ -41,8 +40,8 @@ OUTPUT_MAPPING = { class DetailMining(Action): - """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion. - """ + """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion.""" + def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) diff --git a/metagpt/actions/invoice_ocr.py b/metagpt/actions/invoice_ocr.py index b37aa6885..dcf537a58 100644 --- a/metagpt/actions/invoice_ocr.py +++ b/metagpt/actions/invoice_ocr.py @@ -10,8 +10,8 @@ import os import zipfile -from pathlib import Path from datetime import datetime +from pathlib import Path import pandas as pd from paddleocr import PaddleOCR @@ -19,7 +19,10 @@ from paddleocr import PaddleOCR from metagpt.actions import Action from metagpt.const import INVOICE_OCR_TABLE_PATH from metagpt.logs import logger -from metagpt.prompts.invoice_ocr import EXTRACT_OCR_MAIN_INFO_PROMPT, REPLY_OCR_QUESTION_PROMPT +from metagpt.prompts.invoice_ocr import ( + EXTRACT_OCR_MAIN_INFO_PROMPT, + REPLY_OCR_QUESTION_PROMPT, +) from metagpt.utils.common import OutputParser from metagpt.utils.file import File @@ -183,4 +186,3 @@ class ReplyQuestion(Action): prompt = REPLY_OCR_QUESTION_PROMPT.format(query=query, ocr_result=ocr_result, language=self.language) resp = await self._aask(prompt=prompt) return resp - diff --git a/metagpt/actions/prepare_interview.py b/metagpt/actions/prepare_interview.py index 5db3a9f37..b2704616e 100644 --- a/metagpt/actions/prepare_interview.py +++ b/metagpt/actions/prepare_interview.py @@ -38,4 +38,3 @@ class PrepareInterview(Action): prompt = PROMPT_TEMPLATE.format(context=context) question_list = await self._aask_v1(prompt) return question_list - diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index 49a981e86..d7a2a7e38 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -import json from typing import Callable from pydantic import parse_obj_as @@ -49,7 +48,7 @@ based on the link credibility. If two results have equal credibility, prioritize ranked results' indices in JSON format, like [0, 1, 3, 4, ...], without including other words. """ -WEB_BROWSE_AND_SUMMARIZE_PROMPT = '''### Requirements +WEB_BROWSE_AND_SUMMARIZE_PROMPT = """### Requirements 1. Utilize the text in the "Reference Information" section to respond to the question "{query}". 2. If the question cannot be directly answered using the text, but the text is related to the research topic, please provide \ a comprehensive summary of the text. @@ -58,10 +57,10 @@ a comprehensive summary of the text. ### Reference Information {content} -''' +""" -CONDUCT_RESEARCH_PROMPT = '''### Reference Information +CONDUCT_RESEARCH_PROMPT = """### Reference Information {content} ### Requirements @@ -73,11 +72,12 @@ above. The report must meet the following requirements: - Present data and findings in an intuitive manner, utilizing feature comparative tables, if applicable. - The report should have a minimum word count of 2,000 and be formatted with Markdown syntax following APA style guidelines. - Include all source URLs in APA format at the end of the report. -''' +""" class CollectLinks(Action): """Action class to collect links from a search engine.""" + def __init__( self, name: str = "", @@ -114,19 +114,24 @@ class CollectLinks(Action): keywords = OutputParser.extract_struct(keywords, list) keywords = parse_obj_as(list[str], keywords) except Exception as e: - logger.exception(f"fail to get keywords related to the research topic \"{topic}\" for {e}") + logger.exception(f'fail to get keywords related to the research topic "{topic}" for {e}') keywords = [topic] results = await asyncio.gather(*(self.search_engine.run(i, as_string=False) for i in keywords)) def gen_msg(): while True: - search_results = "\n".join(f"#### Keyword: {i}\n Search Result: {j}\n" for (i, j) in zip(keywords, results)) - prompt = SUMMARIZE_SEARCH_PROMPT.format(decomposition_nums=decomposition_nums, search_results=search_results) + search_results = "\n".join( + f"#### Keyword: {i}\n Search Result: {j}\n" for (i, j) in zip(keywords, results) + ) + prompt = SUMMARIZE_SEARCH_PROMPT.format( + decomposition_nums=decomposition_nums, search_results=search_results + ) yield prompt remove = max(results, key=len) remove.pop() if len(remove) == 0: break + prompt = reduce_message_length(gen_msg(), self.llm.model, system_text, CONFIG.max_tokens_rsp) logger.debug(prompt) queries = await self._aask(prompt, [system_text]) @@ -172,6 +177,7 @@ class CollectLinks(Action): class WebBrowseAndSummarize(Action): """Action class to explore the web and provide summaries of articles and webpages.""" + def __init__( self, *args, @@ -214,7 +220,9 @@ class WebBrowseAndSummarize(Action): for u, content in zip([url, *urls], contents): content = content.inner_text chunk_summaries = [] - for prompt in generate_prompt_chunk(content, prompt_template, self.llm.model, system_text, CONFIG.max_tokens_rsp): + for prompt in generate_prompt_chunk( + content, prompt_template, self.llm.model, system_text, CONFIG.max_tokens_rsp + ): logger.debug(prompt) summary = await self._aask(prompt, [system_text]) if summary == "Not relevant.": @@ -238,6 +246,7 @@ class WebBrowseAndSummarize(Action): class ConductResearch(Action): """Action class to conduct research and generate a research report.""" + def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if CONFIG.model_for_researcher_report: diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 069f2a977..5e4cdaea0 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -140,4 +140,3 @@ class SearchAndSummarize(Action): logger.debug(prompt) logger.debug(result) return result - \ No newline at end of file diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c000805c5..a922d3694 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -5,13 +5,14 @@ @Author : alexanderwu @File : write_code.py """ +from tenacity import retry, stop_after_attempt, wait_fixed + from metagpt.actions import WriteDesign from metagpt.actions.action import Action from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -74,9 +75,8 @@ class WriteCode(Action): async def run(self, context, filename): prompt = PROMPT_TEMPLATE.format(context=context, filename=filename) - logger.info(f'Writing {filename}..') + logger.info(f"Writing {filename}..") code = await self.write_code(prompt) # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) return code - \ No newline at end of file diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4ff4d6cf6..76adca255 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -6,11 +6,12 @@ @File : write_code_review.py """ +from tenacity import retry, stop_after_attempt, wait_fixed + from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -74,9 +75,8 @@ class WriteCodeReview(Action): async def run(self, context, code, filename): format_example = FORMAT_EXAMPLE.format(filename=filename) prompt = PROMPT_TEMPLATE.format(context=context, code=code, filename=filename, format_example=format_example) - logger.info(f'Code review {filename}..') + logger.info(f"Code review {filename}..") code = await self.write_code(prompt) # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) return code - \ No newline at end of file diff --git a/metagpt/actions/write_docstring.py b/metagpt/actions/write_docstring.py index 5c7815793..dd3312bd5 100644 --- a/metagpt/actions/write_docstring.py +++ b/metagpt/actions/write_docstring.py @@ -28,7 +28,7 @@ from metagpt.actions.action import Action from metagpt.utils.common import OutputParser from metagpt.utils.pycst import merge_docstring -PYTHON_DOCSTRING_SYSTEM = '''### Requirements +PYTHON_DOCSTRING_SYSTEM = """### Requirements 1. Add docstrings to the given code following the {style} style. 2. Replace the function body with an Ellipsis object(...) to reduce output. 3. If the types are already annotated, there is no need to include them in the docstring. @@ -48,7 +48,7 @@ class ExampleError(Exception): ```python {example} ``` -''' +""" # https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html @@ -162,7 +162,8 @@ class WriteDocstring(Action): self.desc = "Write docstring for code." async def run( - self, code: str, + self, + code: str, system_text: str = PYTHON_DOCSTRING_SYSTEM, style: Literal["google", "numpy", "sphinx"] = "google", ) -> str: diff --git a/metagpt/actions/write_prd_review.py b/metagpt/actions/write_prd_review.py index 5c922d3bc..5ff9624c5 100644 --- a/metagpt/actions/write_prd_review.py +++ b/metagpt/actions/write_prd_review.py @@ -25,4 +25,3 @@ class WritePRDReview(Action): prompt = self.prd_review_prompt_template.format(prd=self.prd) review = await self._aask(prompt) return review - \ No newline at end of file diff --git a/metagpt/actions/write_tutorial.py b/metagpt/actions/write_tutorial.py index 23e3560e8..d41915de3 100644 --- a/metagpt/actions/write_tutorial.py +++ b/metagpt/actions/write_tutorial.py @@ -10,7 +10,7 @@ from typing import Dict from metagpt.actions import Action -from metagpt.prompts.tutorial_assistant import DIRECTORY_PROMPT, CONTENT_PROMPT +from metagpt.prompts.tutorial_assistant import CONTENT_PROMPT, DIRECTORY_PROMPT from metagpt.utils.common import OutputParser @@ -65,4 +65,3 @@ class WriteContent(Action): """ prompt = CONTENT_PROMPT.format(topic=topic, language=self.language, directory=self.directory) return await self._aask(prompt=prompt) - diff --git a/metagpt/config.py b/metagpt/config.py index 27455d38d..d93640c1b 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -46,7 +46,7 @@ class Config(metaclass=Singleton): self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") if (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) and ( - not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key + not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key ): raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY first") self.openai_api_base = self._get("OPENAI_API_BASE") diff --git a/metagpt/document_store/base_store.py b/metagpt/document_store/base_store.py index 5d7015e8b..7d102e00b 100644 --- a/metagpt/document_store/base_store.py +++ b/metagpt/document_store/base_store.py @@ -41,7 +41,7 @@ class LocalStore(BaseStore, ABC): self.store = self.write() def _get_index_and_store_fname(self): - fname = self.raw_data.name.split('.')[0] + fname = self.raw_data.name.split(".")[0] index_file = self.cache_dir / f"{fname}.index" store_file = self.cache_dir / f"{fname}.pkl" return index_file, store_file @@ -53,4 +53,3 @@ class LocalStore(BaseStore, ABC): @abstractmethod def _write(self, docs, metadatas): raise NotImplementedError - \ No newline at end of file diff --git a/metagpt/document_store/chromadb_store.py b/metagpt/document_store/chromadb_store.py index d2ecc05f6..d7344d41b 100644 --- a/metagpt/document_store/chromadb_store.py +++ b/metagpt/document_store/chromadb_store.py @@ -10,6 +10,7 @@ import chromadb class ChromaStore: """If inherited from BaseStore, or importing other modules from metagpt, a Python exception occurs, which is strange.""" + def __init__(self, name): client = chromadb.Client() collection = client.create_collection(name) @@ -22,7 +23,7 @@ class ChromaStore: query_texts=[query], n_results=n_results, where=metadata_filter, # optional filter - where_document=document_filter # optional filter + where_document=document_filter, # optional filter ) return results diff --git a/metagpt/document_store/document.py b/metagpt/document_store/document.py index e4b9473c7..c59056312 100644 --- a/metagpt/document_store/document.py +++ b/metagpt/document_store/document.py @@ -24,20 +24,20 @@ def validate_cols(content_col: str, df: pd.DataFrame): def read_data(data_path: Path): suffix = data_path.suffix - if '.xlsx' == suffix: + if ".xlsx" == suffix: data = pd.read_excel(data_path) - elif '.csv' == suffix: + elif ".csv" == suffix: data = pd.read_csv(data_path) - elif '.json' == suffix: + elif ".json" == suffix: data = pd.read_json(data_path) - elif suffix in ('.docx', '.doc'): - data = UnstructuredWordDocumentLoader(str(data_path), mode='elements').load() - elif '.txt' == suffix: + elif suffix in (".docx", ".doc"): + data = UnstructuredWordDocumentLoader(str(data_path), mode="elements").load() + elif ".txt" == suffix: data = TextLoader(str(data_path)).load() - text_splitter = CharacterTextSplitter(separator='\n', chunk_size=256, chunk_overlap=0) + text_splitter = CharacterTextSplitter(separator="\n", chunk_size=256, chunk_overlap=0) texts = text_splitter.split_documents(data) data = texts - elif '.pdf' == suffix: + elif ".pdf" == suffix: data = UnstructuredPDFLoader(str(data_path), mode="elements").load() else: raise NotImplementedError @@ -45,8 +45,7 @@ def read_data(data_path: Path): class Document: - - def __init__(self, data_path, content_col='content', meta_col='metadata'): + def __init__(self, data_path, content_col="content", meta_col="metadata"): self.data = read_data(data_path) if isinstance(self.data, pd.DataFrame): validate_cols(content_col, self.data) @@ -79,4 +78,3 @@ class Document: return self._get_docs_and_metadatas_by_langchain() else: raise NotImplementedError - \ No newline at end of file diff --git a/metagpt/document_store/faiss_store.py b/metagpt/document_store/faiss_store.py index dd450010d..8ff904cdd 100644 --- a/metagpt/document_store/faiss_store.py +++ b/metagpt/document_store/faiss_store.py @@ -20,7 +20,7 @@ from metagpt.logs import logger class FaissStore(LocalStore): - def __init__(self, raw_data: Path, cache_dir=None, meta_col='source', content_col='output'): + def __init__(self, raw_data: Path, cache_dir=None, meta_col="source", content_col="output"): self.meta_col = meta_col self.content_col = content_col super().__init__(raw_data, cache_dir) @@ -50,7 +50,7 @@ class FaissStore(LocalStore): pickle.dump(store, f) store.index = index - def search(self, query, expand_cols=False, sep='\n', *args, k=5, **kwargs): + def search(self, query, expand_cols=False, sep="\n", *args, k=5, **kwargs): rsp = self.store.similarity_search(query, k=k, **kwargs) logger.debug(rsp) if expand_cols: @@ -78,8 +78,8 @@ class FaissStore(LocalStore): raise NotImplementedError -if __name__ == '__main__': - faiss_store = FaissStore(DATA_PATH / 'qcs/qcs_4w.json') - logger.info(faiss_store.search('Oily Skin Facial Cleanser')) - faiss_store.add([f'Oily Skin Facial Cleanser-{i}' for i in range(3)]) - logger.info(faiss_store.search('Oily Skin Facial Cleanser')) +if __name__ == "__main__": + faiss_store = FaissStore(DATA_PATH / "qcs/qcs_4w.json") + logger.info(faiss_store.search("Oily Skin Facial Cleanser")) + faiss_store.add([f"Oily Skin Facial Cleanser-{i}" for i in range(3)]) + logger.info(faiss_store.search("Oily Skin Facial Cleanser")) diff --git a/metagpt/document_store/milvus_store.py b/metagpt/document_store/milvus_store.py index 77a8ec141..fcfc59d79 100644 --- a/metagpt/document_store/milvus_store.py +++ b/metagpt/document_store/milvus_store.py @@ -12,12 +12,7 @@ from pymilvus import Collection, CollectionSchema, DataType, FieldSchema, connec from metagpt.document_store.base_store import BaseStore -type_mapping = { - int: DataType.INT64, - str: DataType.VARCHAR, - float: DataType.DOUBLE, - np.ndarray: DataType.FLOAT_VECTOR -} +type_mapping = {int: DataType.INT64, str: DataType.VARCHAR, float: DataType.DOUBLE, np.ndarray: DataType.FLOAT_VECTOR} def columns_to_milvus_schema(columns: dict, primary_col_name: str = "", desc: str = ""): @@ -52,17 +47,11 @@ class MilvusStore(BaseStore): self.collection = None def _create_collection(self, name, schema): - collection = Collection( - name=name, - schema=schema, - using='default', - shards_num=2, - consistency_level="Strong" - ) + collection = Collection(name=name, schema=schema, using="default", shards_num=2, consistency_level="Strong") return collection def create_collection(self, name, columns): - schema = columns_to_milvus_schema(columns, 'idx') + schema = columns_to_milvus_schema(columns, "idx") self.collection = self._create_collection(name, schema) return self.collection @@ -72,7 +61,7 @@ class MilvusStore(BaseStore): def load_collection(self): self.collection.load() - def build_index(self, field='emb'): + def build_index(self, field="emb"): self.collection.create_index(field, {"index_type": "FLAT", "metric_type": "L2", "params": {}}) def search(self, query: list[list[float]], *args, **kwargs): @@ -85,11 +74,11 @@ class MilvusStore(BaseStore): search_params = {"metric_type": "L2", "params": {"nprobe": 10}} results = self.collection.search( data=query, - anns_field=kwargs.get('field', 'emb'), + anns_field=kwargs.get("field", "emb"), param=search_params, limit=10, expr=None, - consistency_level="Strong" + consistency_level="Strong", ) # FIXME: results contain id, but to get the actual value from the id, we still need to call the query interface return results diff --git a/metagpt/document_store/qdrant_store.py b/metagpt/document_store/qdrant_store.py index 98b82cf87..4e9637aa7 100644 --- a/metagpt/document_store/qdrant_store.py +++ b/metagpt/document_store/qdrant_store.py @@ -10,13 +10,14 @@ from metagpt.document_store.base_store import BaseStore @dataclass class QdrantConnection: """ - Args: - url: qdrant url - host: qdrant host - port: qdrant port - memory: qdrant service use memory mode - api_key: qdrant cloud api_key - """ + Args: + url: qdrant url + host: qdrant host + port: qdrant port + memory: qdrant service use memory mode + api_key: qdrant cloud api_key + """ + url: str = None host: str = None port: int = None @@ -31,9 +32,7 @@ class QdrantStore(BaseStore): elif connect.url: self.client = QdrantClient(url=connect.url, api_key=connect.api_key) elif connect.host and connect.port: - self.client = QdrantClient( - host=connect.host, port=connect.port, api_key=connect.api_key - ) + self.client = QdrantClient(host=connect.host, port=connect.port, api_key=connect.api_key) else: raise Exception("please check QdrantConnection.") @@ -58,15 +57,11 @@ class QdrantStore(BaseStore): try: self.client.get_collection(collection_name) if force_recreate: - res = self.client.recreate_collection( - collection_name, vectors_config=vectors_config, **kwargs - ) + res = self.client.recreate_collection(collection_name, vectors_config=vectors_config, **kwargs) return res return True except: # noqa: E722 - return self.client.recreate_collection( - collection_name, vectors_config=vectors_config, **kwargs - ) + return self.client.recreate_collection(collection_name, vectors_config=vectors_config, **kwargs) def has_collection(self, collection_name: str): try: diff --git a/metagpt/environment.py b/metagpt/environment.py index 24e6ada2f..2e2aa152a 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -17,34 +17,34 @@ from metagpt.schema import Message class Environment(BaseModel): """环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到 - Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles - + Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles + """ roles: dict[str, Role] = Field(default_factory=dict) memory: Memory = Field(default_factory=Memory) - history: str = Field(default='') + history: str = Field(default="") class Config: arbitrary_types_allowed = True def add_role(self, role: Role): """增加一个在当前环境的角色 - Add a role in the current environment + Add a role in the current environment """ role.set_env(self) self.roles[role.profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 - Add a batch of characters in the current environment + Add a batch of characters in the current environment """ for role in roles: self.add_role(role) def publish_message(self, message: Message): """向当前环境发布信息 - Post information to the current environment + Post information to the current environment """ # self.message_queue.put(message) self.memory.add(message) @@ -68,12 +68,12 @@ class Environment(BaseModel): def get_roles(self) -> dict[str, Role]: """获得环境内的所有角色 - Process all Role runs at once + Process all Role runs at once """ return self.roles def get_role(self, name: str) -> Role: """获得环境内的指定角色 - get all the environment roles + get all the environment roles """ return self.roles.get(name, None) diff --git a/metagpt/inspect_module.py b/metagpt/inspect_module.py index a89ac1c5e..48ceffc57 100644 --- a/metagpt/inspect_module.py +++ b/metagpt/inspect_module.py @@ -12,17 +12,17 @@ import metagpt # replace with your module def print_classes_and_functions(module): - """FIXME: NOT WORK.. """ + """FIXME: NOT WORK..""" for name, obj in inspect.getmembers(module): if inspect.isclass(obj): - print(f'Class: {name}') + print(f"Class: {name}") elif inspect.isfunction(obj): - print(f'Function: {name}') + print(f"Function: {name}") else: print(name) print(dir(module)) -if __name__ == '__main__': - print_classes_and_functions(metagpt) \ No newline at end of file +if __name__ == "__main__": + print_classes_and_functions(metagpt) diff --git a/metagpt/llm.py b/metagpt/llm.py index e6f815950..410f3dcb5 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -12,8 +12,9 @@ from metagpt.provider.openai_api import OpenAIGPTAPI as LLM DEFAULT_LLM = LLM() CLAUDE_LLM = Claude() + async def ai_func(prompt): """使用LLM进行QA - QA with LLMs - """ + QA with LLMs + """ return await DEFAULT_LLM.aask(prompt) diff --git a/metagpt/logs.py b/metagpt/logs.py index b2052e9b8..55d85312f 100644 --- a/metagpt/logs.py +++ b/metagpt/logs.py @@ -12,13 +12,15 @@ from loguru import logger as _logger from metagpt.const import PROJECT_ROOT + def define_log_level(print_level="INFO", logfile_level="DEBUG"): """调整日志级别到level之上 - Adjust the log level to above level + Adjust the log level to above level """ _logger.remove() _logger.add(sys.stderr, level=print_level) - _logger.add(PROJECT_ROOT / 'logs/log.txt', level=logfile_level) + _logger.add(PROJECT_ROOT / "logs/log.txt", level=logfile_level) return _logger + logger = define_log_level() diff --git a/metagpt/management/skill_manager.py b/metagpt/management/skill_manager.py index f967a0a94..b3181b64e 100644 --- a/metagpt/management/skill_manager.py +++ b/metagpt/management/skill_manager.py @@ -19,8 +19,8 @@ class SkillManager: def __init__(self): self._llm = LLM() - self._store = ChromaStore('skill_manager') - self._skills: dict[str: Skill] = {} + self._store = ChromaStore("skill_manager") + self._skills: dict[str:Skill] = {} def add_skill(self, skill: Skill): """ @@ -54,7 +54,7 @@ class SkillManager: :param desc: Skill description :return: Multiple skills """ - return self._store.search(desc, n_results=n_results)['ids'][0] + return self._store.search(desc, n_results=n_results)["ids"][0] def retrieve_skill_scored(self, desc: str, n_results: int = 2) -> dict: """ @@ -75,6 +75,6 @@ class SkillManager: logger.info(text) -if __name__ == '__main__': +if __name__ == "__main__": manager = SkillManager() manager.generate_skill_desc(Action()) diff --git a/metagpt/manager.py b/metagpt/manager.py index 9d238c621..d0b6b101c 100644 --- a/metagpt/manager.py +++ b/metagpt/manager.py @@ -18,7 +18,7 @@ class Manager: "Product Manager": "Architect", "Architect": "Engineer", "Engineer": "QA Engineer", - "QA Engineer": "Product Manager" + "QA Engineer": "Product Manager", } self.prompt_template = """ Given the following message: @@ -51,7 +51,7 @@ class Manager: # chosen_role_name = self.llm.ask(self.prompt_template.format(context)) # FIXME: 现在通过简单的字典决定流向,但之后还是应该有思考过程 - #The direction of flow is now determined by a simple dictionary, but there should still be a thought process afterwards + # The direction of flow is now determined by a simple dictionary, but there should still be a thought process afterwards next_role_profile = self.role_directions[message.role] # logger.debug(f"{next_role_profile}") for _, role in roles.items(): diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index f8abea5f3..e0b8e68c1 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -68,4 +68,3 @@ class LongTermMemory(Memory): def clear(self): super(LongTermMemory, self).clear() self.memory_storage.clean() - \ No newline at end of file diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index c818fa707..282f5fe33 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -85,4 +85,3 @@ class Memory: continue rsp += self.index[action] return rsp - \ No newline at end of file diff --git a/metagpt/memory/memory_storage.py b/metagpt/memory/memory_storage.py index 302d96aa7..a213f6d7a 100644 --- a/metagpt/memory/memory_storage.py +++ b/metagpt/memory/memory_storage.py @@ -2,16 +2,16 @@ # -*- coding: utf-8 -*- # @Desc : the implement of memory storage -from typing import List from pathlib import Path +from typing import List from langchain.vectorstores.faiss import FAISS from metagpt.const import DATA_PATH, MEM_TTL +from metagpt.document_store.faiss_store import FaissStore from metagpt.logs import logger from metagpt.schema import Message -from metagpt.utils.serialize import serialize_message, deserialize_message -from metagpt.document_store.faiss_store import FaissStore +from metagpt.utils.serialize import deserialize_message, serialize_message class MemoryStorage(FaissStore): @@ -34,7 +34,7 @@ class MemoryStorage(FaissStore): def recover_memory(self, role_id: str) -> List[Message]: self.role_id = role_id - self.role_mem_path = Path(DATA_PATH / f'role_mem/{self.role_id}/') + self.role_mem_path = Path(DATA_PATH / f"role_mem/{self.role_id}/") self.role_mem_path.mkdir(parents=True, exist_ok=True) self.store = self._load() @@ -51,18 +51,18 @@ class MemoryStorage(FaissStore): def _get_index_and_store_fname(self): if not self.role_mem_path: - logger.error(f'You should call {self.__class__.__name__}.recover_memory fist when using LongTermMemory') + logger.error(f"You should call {self.__class__.__name__}.recover_memory fist when using LongTermMemory") return None, None - index_fpath = Path(self.role_mem_path / f'{self.role_id}.index') - storage_fpath = Path(self.role_mem_path / f'{self.role_id}.pkl') + index_fpath = Path(self.role_mem_path / f"{self.role_id}.index") + storage_fpath = Path(self.role_mem_path / f"{self.role_id}.pkl") return index_fpath, storage_fpath def persist(self): super(MemoryStorage, self).persist() - logger.debug(f'Agent {self.role_id} persist memory into local') + logger.debug(f"Agent {self.role_id} persist memory into local") def add(self, message: Message) -> bool: - """ add message into memory storage""" + """add message into memory storage""" docs = [message.content] metadatas = [{"message_ser": serialize_message(message)}] if not self.store: @@ -79,10 +79,7 @@ class MemoryStorage(FaissStore): if not self.store: return [] - resp = self.store.similarity_search_with_score( - query=message.content, - k=k - ) + resp = self.store.similarity_search_with_score(query=message.content, k=k) # filter the result which score is smaller than the threshold filtered_resp = [] for item, score in resp: @@ -104,4 +101,3 @@ class MemoryStorage(FaissStore): self.store = None self._initialized = False - \ No newline at end of file diff --git a/metagpt/prompts/invoice_ocr.py b/metagpt/prompts/invoice_ocr.py index 52f628a5b..aa79651be 100644 --- a/metagpt/prompts/invoice_ocr.py +++ b/metagpt/prompts/invoice_ocr.py @@ -10,7 +10,9 @@ COMMON_PROMPT = "Now I will provide you with the OCR text recognition results for the invoice." -EXTRACT_OCR_MAIN_INFO_PROMPT = COMMON_PROMPT + """ +EXTRACT_OCR_MAIN_INFO_PROMPT = ( + COMMON_PROMPT + + """ Please extract the payee, city, total cost, and invoicing date of the invoice. The OCR data of the invoice are as follows: @@ -22,8 +24,11 @@ Mandatory restrictions are returned according to the following requirements: 2. The returned JSON dictionary must be returned in {language} 3. Mandatory requirement to output in JSON format: {{"收款人":"x","城市":"x","总费用/元":"","开票日期":""}}. """ +) -REPLY_OCR_QUESTION_PROMPT = COMMON_PROMPT + """ +REPLY_OCR_QUESTION_PROMPT = ( + COMMON_PROMPT + + """ Please answer the question: {query} The OCR data of the invoice are as follows: @@ -34,6 +39,6 @@ Mandatory restrictions are returned according to the following requirements: 2. Enforce restrictions on not returning OCR data sent to you. 3. Return with markdown syntax layout. """ +) INVOICE_OCR_SUCCESS = "Successfully completed OCR text recognition invoice." - diff --git a/metagpt/prompts/sales.py b/metagpt/prompts/sales.py index a44aacafe..30ef1ae02 100644 --- a/metagpt/prompts/sales.py +++ b/metagpt/prompts/sales.py @@ -54,10 +54,12 @@ Conversation history: {salesperson_name}: """ -conversation_stages = {'1' : "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are contacting the prospect.", -'2': "Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.", -'3': "Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.", -'4': "Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.", -'5': "Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.", -'6': "Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.", -'7': "Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits."} +conversation_stages = { + "1": "Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are contacting the prospect.", + "2": "Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions.", + "3": "Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors.", + "4": "Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes.", + "5": "Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points.", + "6": "Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims.", + "7": "Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits.", +} diff --git a/metagpt/prompts/tutorial_assistant.py b/metagpt/prompts/tutorial_assistant.py index d690aad83..3d4b6fa24 100644 --- a/metagpt/prompts/tutorial_assistant.py +++ b/metagpt/prompts/tutorial_assistant.py @@ -12,7 +12,9 @@ You are now a seasoned technical professional in the field of the internet. We need you to write a technical tutorial with the topic "{topic}". """ -DIRECTORY_PROMPT = COMMON_PROMPT + """ +DIRECTORY_PROMPT = ( + COMMON_PROMPT + + """ Please provide the specific table of contents for this tutorial, strictly following the following requirements: 1. The output must be strictly in the specified language, {language}. 2. Answer strictly in the dictionary format like {{"title": "xxx", "directory": [{{"dir 1": ["sub dir 1", "sub dir 2"]}}, {{"dir 2": ["sub dir 3", "sub dir 4"]}}]}}. @@ -20,8 +22,11 @@ Please provide the specific table of contents for this tutorial, strictly follow 4. Do not have extra spaces or line breaks. 5. Each directory title has practical significance. """ +) -CONTENT_PROMPT = COMMON_PROMPT + """ +CONTENT_PROMPT = ( + COMMON_PROMPT + + """ Now I will give you the module directory titles for the topic. Please output the detailed principle content of this title in detail. If there are code examples, please provide them according to standard code specifications. @@ -36,4 +41,5 @@ Strictly limit output according to the following requirements: 3. The output must be strictly in the specified language, {language}. 4. Do not have redundant output, including concluding remarks. 5. Strict requirement not to output the topic "{topic}". -""" \ No newline at end of file +""" +) diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py index 7293e2cde..03802a716 100644 --- a/metagpt/provider/anthropic_api.py +++ b/metagpt/provider/anthropic_api.py @@ -32,4 +32,3 @@ class Claude2: max_tokens_to_sample=1000, ) return res.completion - \ No newline at end of file diff --git a/metagpt/provider/base_chatbot.py b/metagpt/provider/base_chatbot.py index abdf423f4..2d4cfe2d9 100644 --- a/metagpt/provider/base_chatbot.py +++ b/metagpt/provider/base_chatbot.py @@ -12,6 +12,7 @@ from dataclasses import dataclass @dataclass class BaseChatbot(ABC): """Abstract GPT class""" + mode: str = "API" @abstractmethod @@ -25,4 +26,3 @@ class BaseChatbot(ABC): @abstractmethod def ask_code(self, msgs: list) -> str: """Ask GPT multiple questions and get a piece of code""" - \ No newline at end of file diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index de61167b9..adc57c66b 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -14,7 +14,8 @@ from metagpt.provider.base_chatbot import BaseChatbot class BaseGPTAPI(BaseChatbot): """GPT API abstract class, requiring all inheritors to provide a series of standard capabilities""" - system_prompt = 'You are a helpful assistant.' + + system_prompt = "You are a helpful assistant." def _user_msg(self, msg: str) -> dict[str, str]: return {"role": "user", "content": msg} @@ -110,9 +111,8 @@ class BaseGPTAPI(BaseChatbot): def messages_to_prompt(self, messages: list[dict]): """[{"role": "user", "content": msg}] to user: etc.""" - return '\n'.join([f"{i['role']}: {i['content']}" for i in messages]) + return "\n".join([f"{i['role']}: {i['content']}" for i in messages]) def messages_to_dict(self, messages): """objects to [{"role": "user", "content": msg}] etc.""" return [i.to_dict() for i in messages] - \ No newline at end of file diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 6ebed2c16..ac0edd44f 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -110,7 +110,6 @@ class CostManager(metaclass=Singleton): """ return self.total_completion_tokens - def get_total_cost(self): """ Get the total cost of API calls. @@ -120,7 +119,6 @@ class CostManager(metaclass=Singleton): """ return self.total_cost - def get_costs(self) -> Costs: """Get all costs""" return Costs(self.total_prompt_tokens, self.total_completion_tokens, self.total_cost, self.total_budget) diff --git a/metagpt/provider/spark_api.py b/metagpt/provider/spark_api.py index 55f7000ec..60c86f4dc 100644 --- a/metagpt/provider/spark_api.py +++ b/metagpt/provider/spark_api.py @@ -14,8 +14,7 @@ import json import ssl from time import mktime from typing import Optional -from urllib.parse import urlencode -from urllib.parse import urlparse +from urllib.parse import urlencode, urlparse from wsgiref.handlers import format_date_time import websocket # 使用websocket_client @@ -26,9 +25,8 @@ from metagpt.provider.base_gpt_api import BaseGPTAPI class SparkAPI(BaseGPTAPI): - def __init__(self): - logger.warning('当前方法无法支持异步运行。当你使用acompletion时,并不能并行访问。') + logger.warning("当前方法无法支持异步运行。当你使用acompletion时,并不能并行访问。") def ask(self, msg: str) -> str: message = [self._default_system_msg(), self._user_msg(msg)] @@ -49,7 +47,7 @@ class SparkAPI(BaseGPTAPI): async def acompletion_text(self, messages: list[dict], stream=False) -> str: # 不支持 - logger.error('该功能禁用。') + logger.error("该功能禁用。") w = GetMessageFromWeb(messages) return w.run() @@ -93,29 +91,26 @@ class GetMessageFromWeb: signature_origin += "GET " + self.path + " HTTP/1.1" # 进行hmac-sha256进行加密 - signature_sha = hmac.new(self.api_secret.encode('utf-8'), signature_origin.encode('utf-8'), - digestmod=hashlib.sha256).digest() + signature_sha = hmac.new( + self.api_secret.encode("utf-8"), signature_origin.encode("utf-8"), digestmod=hashlib.sha256 + ).digest() - signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding='utf-8') + signature_sha_base64 = base64.b64encode(signature_sha).decode(encoding="utf-8") authorization_origin = f'api_key="{self.api_key}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature_sha_base64}"' - authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8') + authorization = base64.b64encode(authorization_origin.encode("utf-8")).decode(encoding="utf-8") # 将请求的鉴权参数组合为字典 - v = { - "authorization": authorization, - "date": date, - "host": self.host - } + v = {"authorization": authorization, "date": date, "host": self.host} # 拼接鉴权参数,生成url - url = self.spark_url + '?' + urlencode(v) + url = self.spark_url + "?" + urlencode(v) # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致 return url def __init__(self, text): self.text = text - self.ret = '' + self.ret = "" self.spark_appid = CONFIG.spark_appid self.spark_api_secret = CONFIG.spark_api_secret self.spark_api_key = CONFIG.spark_api_key @@ -124,15 +119,15 @@ class GetMessageFromWeb: def on_message(self, ws, message): data = json.loads(message) - code = data['header']['code'] + code = data["header"]["code"] if code != 0: ws.close() # 请求错误,则关闭socket - logger.critical(f'回答获取失败,响应信息反序列化之后为: {data}') + logger.critical(f"回答获取失败,响应信息反序列化之后为: {data}") return else: choices = data["payload"]["choices"] - seq = choices["seq"] # 服务端是流式返回,seq为返回的数据序号 + # seq = choices["seq"] # 服务端是流式返回,seq为返回的数据序号 status = choices["status"] # 服务端是流式返回,status用于判断信息是否传送完毕 content = choices["text"][0]["content"] # 本次接收到的回答文本 self.ret += content @@ -142,7 +137,7 @@ class GetMessageFromWeb: # 收到websocket错误的处理 def on_error(self, ws, error): # on_message方法处理接收到的信息,出现任何错误,都会调用这个方法 - logger.critical(f'通讯连接出错,【错误提示: {error}】') + logger.critical(f"通讯连接出错,【错误提示: {error}】") # 收到websocket关闭的处理 def on_close(self, ws, one, two): @@ -150,17 +145,12 @@ class GetMessageFromWeb: # 处理请求数据 def gen_params(self): - data = { - "header": { - "app_id": self.spark_appid, - "uid": "1234" - }, + "header": {"app_id": self.spark_appid, "uid": "1234"}, "parameter": { "chat": { # domain为必传参数 "domain": self.domain, - # 以下为可微调,非必传参数 # 注意:官方建议,temperature和top_k修改一个即可 "max_tokens": 2048, # 默认2048,模型回答的tokens的最大长度,即允许它输出文本的最长字数 @@ -168,11 +158,7 @@ class GetMessageFromWeb: "top_k": 4, # 取值为[1,6],默认为4。从k个候选中随机选择一个(非等概率) } }, - "payload": { - "message": { - "text": self.text - } - } + "payload": {"message": {"text": self.text}}, } return data @@ -189,17 +175,12 @@ class GetMessageFromWeb: return self._run(self.text) def _run(self, text_list): - - ws_param = self.WsParam( - self.spark_appid, - self.spark_api_key, - self.spark_api_secret, - self.spark_url, - text_list) + ws_param = self.WsParam(self.spark_appid, self.spark_api_key, self.spark_api_secret, self.spark_url, text_list) ws_url = ws_param.create_url() websocket.enableTrace(False) # 默认禁用 WebSocket 的跟踪功能 - ws = websocket.WebSocketApp(ws_url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, - on_open=self.on_open) + ws = websocket.WebSocketApp( + ws_url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, on_open=self.on_open + ) ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) return self.ret diff --git a/metagpt/roles/customer_service.py b/metagpt/roles/customer_service.py index 4547f8190..188182d47 100644 --- a/metagpt/roles/customer_service.py +++ b/metagpt/roles/customer_service.py @@ -24,12 +24,5 @@ DESC = """ class CustomerService(Sales): - def __init__( - self, - name="Xiaomei", - profile="Human customer service", - desc=DESC, - store=None - ): + def __init__(self, name="Xiaomei", profile="Human customer service", desc=DESC, store=None): super().__init__(name, profile, desc=desc, store=store) - \ No newline at end of file diff --git a/metagpt/roles/invoice_ocr_assistant.py b/metagpt/roles/invoice_ocr_assistant.py index c307b20c0..3087a4da7 100644 --- a/metagpt/roles/invoice_ocr_assistant.py +++ b/metagpt/roles/invoice_ocr_assistant.py @@ -9,7 +9,7 @@ import pandas as pd -from metagpt.actions.invoice_ocr import InvoiceOCR, GenerateTable, ReplyQuestion +from metagpt.actions.invoice_ocr import GenerateTable, InvoiceOCR, ReplyQuestion from metagpt.prompts.invoice_ocr import INVOICE_OCR_SUCCESS from metagpt.roles import Role from metagpt.schema import Message @@ -107,4 +107,3 @@ class InvoiceOCRAssistant(Role): break msg = await self._act() return msg - diff --git a/metagpt/roles/prompt.py b/metagpt/roles/prompt.py index c22e0226b..457ccb6c6 100644 --- a/metagpt/roles/prompt.py +++ b/metagpt/roles/prompt.py @@ -23,6 +23,7 @@ SUFFIX = """Let's begin! Question: {input} Thoughts: {agent_scratchpad}""" + class PromptString(Enum): REFLECTION_QUESTIONS = "Here are some statements:\n{memory_descriptions}\n\nBased solely on the information above, what are the 3 most prominent high-level questions we can answer about the topic in the statements?\n\n{format_instructions}" @@ -32,7 +33,7 @@ class PromptString(Enum): RECENT_ACTIVITY = "Based on the following memory, produce a brief summary of what {full_name} has been up to recently. Do not invent details not explicitly stated in the memory. For any conversation, be sure to mention whether the conversation has concluded or is still ongoing.\n\nMemory: {memory_descriptions}" - MAKE_PLANS = "You are a plan-generating AI. Your job is to assist the character in formulating new plans based on new information. Given the character's information (profile, objectives, recent activities, current plans, and location context) and their current thought process, produce a new set of plans for them. The final plan should comprise at least {time_window} of activities and no more than 5 individual plans. List the plans in the order they should be executed, with each plan detailing its description, location, start time, stop criteria, and maximum duration.\n\nSample plan: {{\"index\": 1, \"description\": \"Cook dinner\", \"location_id\": \"0a3bc22b-36aa-48ab-adb0-18616004caed\",\"start_time\": \"2022-12-12T20:00:00+00:00\",\"max_duration_hrs\": 1.5, \"stop_condition\": \"Dinner is fully prepared\"}}\'\n\nFor each plan, choose the most appropriate location name from this list: {allowed_location_descriptions}\n\n{format_instructions}\n\nAlways prioritize completing any unfinished conversations.\n\nLet's begin!\n\nName: {full_name}\nProfile: {private_bio}\nObjectives: {directives}\nLocation Context: {location_context}\nCurrent Plans: {current_plans}\nRecent Activities: {recent_activity}\nThought Process: {thought_process}\nIt's essential to encourage the character to collaborate with other characters in their plans.\n\n" + MAKE_PLANS = 'You are a plan-generating AI. Your job is to assist the character in formulating new plans based on new information. Given the character\'s information (profile, objectives, recent activities, current plans, and location context) and their current thought process, produce a new set of plans for them. The final plan should comprise at least {time_window} of activities and no more than 5 individual plans. List the plans in the order they should be executed, with each plan detailing its description, location, start time, stop criteria, and maximum duration.\n\nSample plan: {{"index": 1, "description": "Cook dinner", "location_id": "0a3bc22b-36aa-48ab-adb0-18616004caed","start_time": "2022-12-12T20:00:00+00:00","max_duration_hrs": 1.5, "stop_condition": "Dinner is fully prepared"}}\'\n\nFor each plan, choose the most appropriate location name from this list: {allowed_location_descriptions}\n\n{format_instructions}\n\nAlways prioritize completing any unfinished conversations.\n\nLet\'s begin!\n\nName: {full_name}\nProfile: {private_bio}\nObjectives: {directives}\nLocation Context: {location_context}\nCurrent Plans: {current_plans}\nRecent Activities: {recent_activity}\nThought Process: {thought_process}\nIt\'s essential to encourage the character to collaborate with other characters in their plans.\n\n' EXECUTE_PLAN = "You are a role-playing AI, playing the role of {your_name}, in front of a live audience. Every word you say can be observed by the audience, so make sure you speak often and make it interesting. You cannot interact directly with the audience.\n\nBased on the following context and tools, proceed as if you are {your_name}. Your primary task is to complete the task given below, however, if you are currently in a conversation with another character, you should always complete the conversation first and then proceed with the task. Do not start working while you are engaged in an unfinished conversation. Use your best judgment to determine whether a conversation involves you and whether it is unfinished. You don't need to respond to every message you receive.\n\nThis is a personal profile of your character:\n{your_private_bio}\n\nThis is a description of your location and other nearby characters you can converse with:\n\n{location_context}\n\nThese are some relevant memories:\n```\n{relevant_memories}\n```\n\nThis is some relevant conversation history:\n```\n{conversation_history}\n```\n\nThese are the tools you can use:\n{tools}\n\nYour response should follow the format below:\n\nTask: The task you must complete\nThoughts: What you should think about doing\nAction: The action to take, must be one of these words: [{tool_names}]\nAction Input: Input for the action\nObservation: The result of the action\n... (This Thoughts/Action/Action Input/Observation can be repeated N times)\nThoughts: 'I have completed the task'\nFinal Response: The final response to the task\n\nIf you are not ready with a final response, then you must take an action.\n\nIf you are certain that you cannot complete the task with the tools provided, return 'Final Response: Need Help', however, if you are in conversation with another character, responses like 'I don't know' are valid. During conversations, you should never break character or admit you are an AI.\nIf the task is completed and no specific response is required, return 'Final Response: Done'\nLet's begin!\n\nTask: {input}\n\n{agent_scratchpad}" @@ -40,6 +41,6 @@ class PromptString(Enum): GOSSIP = "You are {full_name}. \n{memory_descriptions}\n\nBased on the statements above, say a thing or two of interest to others at your location: {other_agent_names}.\nAlways specify their names when referring to others." - HAS_HAPPENED = "Given the descriptions of the observations of the following characters and the events they are awaiting, indicate whether the character has witnessed the event.\n{format_instructions}\n\nExample:\n\nObservations:\nJoe entered the office at 2023-05-04 08:00:00+00:00\nJoe said hi to Sally at 2023-05-04 08:05:00+00:00\nSally said hello to Joe at 2023-05-04 08:05:30+00:00\nRebecca started working at 2023-05-04 08:10:00+00:00\nJoe made some breakfast at 2023-05-04 08:15:00+00:00\n\nAwaiting: Sally responded to Joe\n\nYour response: '{{\"has_happened\": true, \"date_occured\": 2023-05-04 08:05:30+00:00}}'\n\nLet's begin!\n\nObservations:\n{memory_descriptions}\n\nAwaiting: {event_description}\n" + HAS_HAPPENED = 'Given the descriptions of the observations of the following characters and the events they are awaiting, indicate whether the character has witnessed the event.\n{format_instructions}\n\nExample:\n\nObservations:\nJoe entered the office at 2023-05-04 08:00:00+00:00\nJoe said hi to Sally at 2023-05-04 08:05:00+00:00\nSally said hello to Joe at 2023-05-04 08:05:30+00:00\nRebecca started working at 2023-05-04 08:10:00+00:00\nJoe made some breakfast at 2023-05-04 08:15:00+00:00\n\nAwaiting: Sally responded to Joe\n\nYour response: \'{{"has_happened": true, "date_occured": 2023-05-04 08:05:30+00:00}}\'\n\nLet\'s begin!\n\nObservations:\n{memory_descriptions}\n\nAwaiting: {event_description}\n' OUTPUT_FORMAT = "\n\n(Remember! Make sure your output always adheres to one of the following two formats:\n\nA. If you have completed the task:\nThoughts: 'I have completed the task'\nFinal Response: \n\nB. If you haven't completed the task:\nThoughts: \nAction: \nAction Input: \nObservation: )\n" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 44bb3e976..282431bf7 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -11,12 +11,13 @@ from typing import Iterable, Type from pydantic import BaseModel, Field +from metagpt.actions import Action, ActionOutput + # from metagpt.environment import Environment from metagpt.config import CONFIG -from metagpt.actions import Action, ActionOutput from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.memory import Memory, LongTermMemory +from metagpt.memory import LongTermMemory, Memory from metagpt.schema import Message PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -49,6 +50,7 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi class RoleSetting(BaseModel): """Role Settings""" + name: str profile: str goal: str @@ -64,7 +66,8 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" - env: 'Environment' = Field(default=None) + + env: "Environment" = Field(default=None) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=0) @@ -128,7 +131,7 @@ class Role: logger.debug(self._actions) self._rc.todo = self._actions[self._rc.state] - def set_env(self, env: 'Environment'): + def set_env(self, env: "Environment"): """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env @@ -150,12 +153,13 @@ class Role: self._set_state(0) return prompt = self._get_prefix() - prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), - n_states=len(self._states) - 1) + prompt += STATE_TEMPLATE.format( + history=self._rc.history, states="\n".join(self._states), n_states=len(self._states) - 1 + ) next_state = await self._llm.aask(prompt) logger.debug(f"{prompt=}") if not next_state.isdigit() or int(next_state) not in range(len(self._states)): - logger.warning(f'Invalid answer of state, {next_state=}') + logger.warning(f"Invalid answer of state, {next_state=}") next_state = "0" self._set_state(int(next_state)) @@ -168,8 +172,12 @@ class Role: response = await self._rc.todo.run(self._rc.important_memory) # logger.info(response) if isinstance(response, ActionOutput): - msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + msg = Message( + content=response.content, + instruct_content=response.instruct_content, + role=self.profile, + cause_by=type(self._rc.todo), + ) else: msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) self._rc.memory.add(msg) @@ -184,15 +192,17 @@ class Role: env_msgs = self._rc.env.memory.get() observed = self._rc.env.memory.get_by_actions(self._rc.watch) - - self._rc.news = self._rc.memory.find_news(observed) # find news (previously unseen messages) from observed messages + + self._rc.news = self._rc.memory.find_news( + observed + ) # find news (previously unseen messages) from observed messages for i in env_msgs: self.recv(i) news_text = [f"{i.role}: {i.content[:20]}..." for i in self._rc.news] if news_text: - logger.debug(f'{self._setting} observed: {news_text}') + logger.debug(f"{self._setting} observed: {news_text}") return len(self._rc.news) def _publish_message(self, msg): diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index a45ad6f1b..18282a494 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -12,16 +12,16 @@ from metagpt.tools import SearchEngineType class Sales(Role): def __init__( - self, - name="Xiaomei", - profile="Retail sales guide", - desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " - "will answer questions only based on the information in the knowledge base." - "If I feel that you can't get the answer from the reference material, then I will directly reply that" - " I don't know, and I won't tell you that this is from the knowledge base," - "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " - "professional guide", - store=None + self, + name="Xiaomei", + profile="Retail sales guide", + desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " + "will answer questions only based on the information in the knowledge base." + "If I feel that you can't get the answer from the reference material, then I will directly reply that" + " I don't know, and I won't tell you that this is from the knowledge base," + "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " + "professional guide", + store=None, ): super().__init__(name, profile, desc=desc) self._set_store(store) @@ -32,4 +32,3 @@ class Sales(Role): else: action = SearchAndSummarize() self._init_actions([action]) - \ No newline at end of file diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index 0b6e089da..a2c4896e2 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -15,7 +15,7 @@ from metagpt.tools import SearchEngineType class Searcher(Role): """ Represents a Searcher role responsible for providing search services to users. - + Attributes: name (str): Name of the searcher. profile (str): Role profile. @@ -23,17 +23,19 @@ class Searcher(Role): constraints (str): Constraints or limitations for the searcher. engine (SearchEngineType): The type of search engine to use. """ - - def __init__(self, - name: str = 'Alice', - profile: str = 'Smart Assistant', - goal: str = 'Provide search services for users', - constraints: str = 'Answer is rich and complete', - engine=SearchEngineType.SERPAPI_GOOGLE, - **kwargs) -> None: + + def __init__( + self, + name: str = "Alice", + profile: str = "Smart Assistant", + goal: str = "Provide search services for users", + constraints: str = "Answer is rich and complete", + engine=SearchEngineType.SERPAPI_GOOGLE, + **kwargs, + ) -> None: """ Initializes the Searcher role with given attributes. - + Args: name (str): Name of the searcher. profile (str): Role profile. @@ -53,10 +55,14 @@ class Searcher(Role): """Performs the search action in a single process.""" logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - + if isinstance(response, ActionOutput): - msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + msg = Message( + content=response.content, + instruct_content=response.instruct_content, + role=self.profile, + cause_by=type(self._rc.todo), + ) else: msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) self._rc.memory.add(msg) diff --git a/metagpt/roles/tutorial_assistant.py b/metagpt/roles/tutorial_assistant.py index 9a7df4f4d..2a514f433 100644 --- a/metagpt/roles/tutorial_assistant.py +++ b/metagpt/roles/tutorial_assistant.py @@ -9,7 +9,7 @@ from datetime import datetime from typing import Dict -from metagpt.actions.write_tutorial import WriteDirectory, WriteContent +from metagpt.actions.write_tutorial import WriteContent, WriteDirectory from metagpt.const import TUTORIAL_PATH from metagpt.logs import logger from metagpt.roles import Role @@ -110,5 +110,5 @@ class TutorialAssistant(Role): break msg = await self._act() root_path = TUTORIAL_PATH / datetime.now().strftime("%Y-%m-%d_%H-%M-%S") - await File.write(root_path, f"{self.main_title}.md", self.total_content.encode('utf-8')) + await File.write(root_path, f"{self.main_title}.md", self.total_content.encode("utf-8")) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index bdca093c2..19c7a6654 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -23,9 +23,10 @@ class RawMessage(TypedDict): @dataclass class Message: """list[: ]""" + content: str instruct_content: BaseModel = field(default=None) - role: str = field(default='user') # system / user / assistant + role: str = field(default="user") # system / user / assistant cause_by: Type["Action"] = field(default="") sent_from: str = field(default="") send_to: str = field(default="") @@ -39,45 +40,45 @@ class Message: return self.__str__() def to_dict(self) -> dict: - return { - "role": self.role, - "content": self.content - } + return {"role": self.role, "content": self.content} @dataclass class UserMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'user') + super().__init__(content, "user") @dataclass class SystemMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'system') + super().__init__(content, "system") @dataclass class AIMessage(Message): """便于支持OpenAI的消息 - Facilitate support for OpenAI messages + Facilitate support for OpenAI messages """ + def __init__(self, content: str): - super().__init__(content, 'assistant') + super().__init__(content, "assistant") -if __name__ == '__main__': - test_content = 'test_message' +if __name__ == "__main__": + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] logger.info(msgs) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index b2bd18c58..d3c2c463b 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -21,6 +21,7 @@ class SoftwareCompany(BaseModel): Software Company: Possesses a team, SOP (Standard Operating Procedures), and a platform for instant messaging, dedicated to writing executable code. """ + environment: Environment = Field(default_factory=Environment) investment: float = Field(default=10.0) idea: str = Field(default="") @@ -36,11 +37,11 @@ class SoftwareCompany(BaseModel): """Invest company. raise NoMoneyException when exceed max_budget.""" self.investment = investment CONFIG.max_budget = investment - logger.info(f'Investment: ${investment}.') + logger.info(f"Investment: ${investment}.") def _check_balance(self): if CONFIG.total_cost > CONFIG.max_budget: - raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}') + raise NoMoneyException(CONFIG.total_cost, f"Insufficient funds: {CONFIG.max_budget}") def start_project(self, idea): """Start a project from publishing boss requirement.""" @@ -59,4 +60,3 @@ class SoftwareCompany(BaseModel): self._check_balance() await self.environment.run() return self.environment.history - \ No newline at end of file diff --git a/metagpt/tools/code_interpreter.py b/metagpt/tools/code_interpreter.py index e41eaab72..1cba005fa 100644 --- a/metagpt/tools/code_interpreter.py +++ b/metagpt/tools/code_interpreter.py @@ -1,22 +1,26 @@ +import inspect import re -from typing import List, Callable, Dict +import textwrap from pathlib import Path +from typing import Callable, Dict, List import wrapt -import textwrap -import inspect from interpreter.core.core import Interpreter -from metagpt.logs import logger +from metagpt.actions.clone_function import ( + CloneFunction, + run_function_code, + run_function_script, +) from metagpt.config import CONFIG +from metagpt.logs import logger from metagpt.utils.highlight import highlight -from metagpt.actions.clone_function import CloneFunction, run_function_code, run_function_script def extract_python_code(code: str): """Extract code blocks: If the code comments are the same, only the last code block is kept.""" # Use regular expressions to match comment blocks and related code. - pattern = r'(#\s[^\n]*)\n(.*?)(?=\n\s*#|$)' + pattern = r"(#\s[^\n]*)\n(.*?)(?=\n\s*#|$)" matches = re.findall(pattern, code, re.DOTALL) # Extract the last code block when encountering the same comment. @@ -25,8 +29,8 @@ def extract_python_code(code: str): unique_comments[comment] = code_block # concatenate into functional form - result_code = '\n'.join([f"{comment}\n{code_block}" for comment, code_block in unique_comments.items()]) - header_code = code[:code.find("#")] + result_code = "\n".join([f"{comment}\n{code_block}" for comment, code_block in unique_comments.items()]) + header_code = code[: code.find("#")] code = header_code + result_code logger.info(f"Extract python code: \n {highlight(code)}") @@ -36,6 +40,7 @@ def extract_python_code(code: str): class OpenCodeInterpreter(object): """https://github.com/KillianLucas/open-interpreter""" + def __init__(self, auto_run: bool = True) -> None: interpreter = Interpreter() interpreter.auto_run = auto_run @@ -50,15 +55,16 @@ class OpenCodeInterpreter(object): return self.interpreter.chat(query) @staticmethod - def extract_function(query_respond: List, function_name: str, *, language: str = 'python', - function_format: str = None) -> str: + def extract_function( + query_respond: List, function_name: str, *, language: str = "python", function_format: str = None + ) -> str: """create a function from query_respond.""" - if language not in ('python'): + if language not in ("python"): raise NotImplementedError(f"Not support to parse language {language}!") # set function form if function_format is None: - assert language == 'python', f"Expect python language for default function_format, but got {language}." + assert language == "python", f"Expect python language for default function_format, but got {language}." function_format = """def {function_name}():\n{code}""" # Extract the code module in the open-interpreter respond message. # The query_respond of open-interpreter before v0.1.4 is: @@ -68,25 +74,29 @@ class OpenCodeInterpreter(object): # "parsed_arguments": {"language": "python", "code": code of first plan} # ...] if "function_call" in query_respond[1]: - code = [item['function_call']['parsed_arguments']['code'] for item in query_respond - if "function_call" in item - and "parsed_arguments" in item["function_call"] - and 'language' in item["function_call"]['parsed_arguments'] - and item["function_call"]['parsed_arguments']['language'] == language] + code = [ + item["function_call"]["parsed_arguments"]["code"] + for item in query_respond + if "function_call" in item + and "parsed_arguments" in item["function_call"] + and "language" in item["function_call"]["parsed_arguments"] + and item["function_call"]["parsed_arguments"]["language"] == language + ] # The query_respond of open-interpreter v0.1.7 is: # [{'role': 'user', 'message': your query string}, # {'role': 'assistant', 'message': plan from llm, 'language': 'python', # 'code': code of first plan, 'output': output of first plan code}, # ...] elif "code" in query_respond[1]: - code = [item['code'] for item in query_respond - if "code" in item - and 'language' in item - and item['language'] == language] + code = [ + item["code"] + for item in query_respond + if "code" in item and "language" in item and item["language"] == language + ] else: raise ValueError(f"Unexpect message format in query_respond: {query_respond[1].keys()}") # add indent. - indented_code_str = textwrap.indent("\n".join(code), ' ' * 4) + indented_code_str = textwrap.indent("\n".join(code), " " * 4) # Return the code after deduplication. if language == "python": return extract_python_code(function_format.format(function_name=function_name, code=indented_code_str)) @@ -115,13 +125,13 @@ class OpenInterpreterDecorator(object): def _have_code(self, rsp: List[Dict]): # Is there any code generated? - return 'code' in rsp[1] and rsp[1]['code'] not in ("", None) + return "code" in rsp[1] and rsp[1]["code"] not in ("", None) def _is_faild_plan(self, rsp: List[Dict]): # is faild plan? - func_code = OpenCodeInterpreter.extract_function(rsp, 'function') + func_code = OpenCodeInterpreter.extract_function(rsp, "function") # If there is no more than 1 '\n', the plan execution fails. - if isinstance(func_code, str) and func_code.count('\n') <= 1: + if isinstance(func_code, str) and func_code.count("\n") <= 1: return True return False @@ -184,4 +194,5 @@ class OpenInterpreterDecorator(object): logger.error(f"Could not evaluate Python code \n{logger_code}: \nError: {e}") raise Exception("Could not evaluate Python code", e) return res + return wrapper(wrapped) diff --git a/metagpt/tools/prompt_writer.py b/metagpt/tools/prompt_writer.py index d90599206..ffcff4d1f 100644 --- a/metagpt/tools/prompt_writer.py +++ b/metagpt/tools/prompt_writer.py @@ -10,8 +10,9 @@ from typing import Union class GPTPromptGenerator: """Using LLM, given an output, request LLM to provide input (supporting instruction, chatbot, and query styles)""" + def __init__(self): - self._generators = {i: getattr(self, f"gen_{i}_style") for i in ['instruction', 'chatbot', 'query']} + self._generators = {i: getattr(self, f"gen_{i}_style") for i in ["instruction", "chatbot", "query"]} def gen_instruction_style(self, example): """Instruction style: Given an output, request LLM to provide input""" @@ -35,7 +36,7 @@ Query: X Document: {example} What is the detailed query X? X:""" - def gen(self, example: str, style: str = 'all') -> Union[list[str], str]: + def gen(self, example: str, style: str = "all") -> Union[list[str], str]: """ Generate one or multiple outputs using the example, allowing LLM to reply with the corresponding input @@ -43,7 +44,7 @@ X:""" :param style: (all|instruction|chatbot|query) :return: Expected LLM input sample (one or multiple) """ - if style != 'all': + if style != "all": return self._generators[style](example) return [f(example) for f in self._generators.values()] diff --git a/metagpt/tools/sd_engine.py b/metagpt/tools/sd_engine.py index 1d9cd0b2a..a63dbe5ac 100644 --- a/metagpt/tools/sd_engine.py +++ b/metagpt/tools/sd_engine.py @@ -120,11 +120,13 @@ def decode_base64_to_image(img, save_name): image.save(f"{save_name}.png", pnginfo=pnginfo) return pnginfo, image + def batch_decode_base64_to_image(imgs, save_dir="", save_name=""): for idx, _img in enumerate(imgs): save_name = join(save_dir, save_name) decode_base64_to_image(_img, save_name=save_name) + if __name__ == "__main__": engine = SDEngine() prompt = "pixel style, game design, a game interface should be minimalistic and intuitive with the score and high score displayed at the top. The snake and its food should be easily distinguishable. The game should have a simple color scheme, with a contrasting color for the snake and its food. Complete interface boundary" diff --git a/metagpt/tools/search_engine.py b/metagpt/tools/search_engine.py index 942ef7edd..64388a11f 100644 --- a/metagpt/tools/search_engine.py +++ b/metagpt/tools/search_engine.py @@ -6,7 +6,7 @@ @File : search_engine.py """ import importlib -from typing import Callable, Coroutine, Literal, overload, Optional, Union +from typing import Callable, Coroutine, Literal, Optional, Union, overload from semantic_kernel.skill_definition import sk_function @@ -43,8 +43,8 @@ class SearchEngine: def __init__( self, - engine: Optional[SearchEngineType] = None, - run_func: Callable[[str, int, bool], Coroutine[None, None, Union[str, list[str]]]] = None, + engine: Optional[SearchEngineType] = None, + run_func: Callable[[str, int, bool], Coroutine[None, None, Union[str, list[str]]]] = None, ): engine = engine or CONFIG.search_engine if engine == SearchEngineType.SERPAPI_GOOGLE: diff --git a/metagpt/tools/search_engine_meilisearch.py b/metagpt/tools/search_engine_meilisearch.py index da4269384..f7c1c685a 100644 --- a/metagpt/tools/search_engine_meilisearch.py +++ b/metagpt/tools/search_engine_meilisearch.py @@ -29,7 +29,7 @@ class MeilisearchEngine: def add_documents(self, data_source: DataSource, documents: List[dict]): index_name = f"{data_source.name}_index" if index_name not in self.client.get_indexes(): - self.client.create_index(uid=index_name, options={'primaryKey': 'id'}) + self.client.create_index(uid=index_name, options={"primaryKey": "id"}) index = self.client.get_index(index_name) index.add_documents(documents) self.set_index(index) @@ -37,7 +37,7 @@ class MeilisearchEngine: def search(self, query): try: search_results = self._index.search(query) - return search_results['hits'] + return search_results["hits"] except Exception as e: # Handle MeiliSearch API errors print(f"MeiliSearch API error: {e}") diff --git a/metagpt/tools/translator.py b/metagpt/tools/translator.py index 910638469..63e38d5a5 100644 --- a/metagpt/tools/translator.py +++ b/metagpt/tools/translator.py @@ -6,7 +6,7 @@ @File : translator.py """ -prompt = ''' +prompt = """ # 指令 接下来,作为一位拥有20年翻译经验的翻译专家,当我给出英文句子或段落时,你将提供通顺且具有可读性的{LANG}翻译。注意以下要求: 1. 确保翻译结果流畅且易于理解 @@ -17,11 +17,10 @@ prompt = ''' {ORIGINAL} # 译文 -''' +""" class Translator: - @classmethod - def translate_prompt(cls, original, lang='中文'): - return prompt.format(LANG=lang, ORIGINAL=original) \ No newline at end of file + def translate_prompt(cls, original, lang="中文"): + return prompt.format(LANG=lang, ORIGINAL=original) diff --git a/metagpt/tools/ut_writer.py b/metagpt/tools/ut_writer.py index 43ca72150..64423dfb1 100644 --- a/metagpt/tools/ut_writer.py +++ b/metagpt/tools/ut_writer.py @@ -6,7 +6,7 @@ from pathlib import Path from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI -ICL_SAMPLE = '''Interface definition: +ICL_SAMPLE = """Interface definition: ```text Interface Name: Element Tagging Interface Path: /projects/{project_key}/node-tags @@ -60,20 +60,20 @@ def test_node_tags(project_key, nodes, operations, expected_msg): # 3. If comments are needed, use Chinese. # If you understand, please wait for me to give the interface definition and just answer "Understood" to save tokens. -''' +""" -ACT_PROMPT_PREFIX = '''Refer to the test types: such as missing request parameters, field boundary verification, incorrect field type. +ACT_PROMPT_PREFIX = """Refer to the test types: such as missing request parameters, field boundary verification, incorrect field type. Please output 10 test cases within one `@pytest.mark.parametrize` scope. ```text -''' +""" -YFT_PROMPT_PREFIX = '''Refer to the test types: such as SQL injection, cross-site scripting (XSS), unauthorized access and privilege escalation, +YFT_PROMPT_PREFIX = """Refer to the test types: such as SQL injection, cross-site scripting (XSS), unauthorized access and privilege escalation, authentication and authorization, parameter verification, exception handling, file upload and download. Please output 10 test cases within one `@pytest.mark.parametrize` scope. ```text -''' +""" -OCR_API_DOC = '''```text +OCR_API_DOC = """```text Interface Name: OCR recognition Interface Path: /api/v1/contract/treaty/task/ocr Method: POST @@ -96,14 +96,20 @@ code integer Yes message string Yes data object Yes ``` -''' +""" class UTGenerator: """UT Generator: Construct UT through API documentation""" - def __init__(self, swagger_file: str, ut_py_path: str, questions_path: str, - chatgpt_method: str = "API", template_prefix=YFT_PROMPT_PREFIX) -> None: + def __init__( + self, + swagger_file: str, + ut_py_path: str, + questions_path: str, + chatgpt_method: str = "API", + template_prefix=YFT_PROMPT_PREFIX, + ) -> None: """Initialize UT Generator Args: @@ -274,7 +280,7 @@ class UTGenerator: def gpt_msgs_to_code(self, messages: list) -> str: """Choose based on different calling methods""" - result = '' + result = "" if self.chatgpt_method == "API": result = GPTAPI().ask_code(msgs=messages) diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index f3691549b..6bb9a1a97 100644 --- a/metagpt/utils/file.py +++ b/metagpt/utils/file.py @@ -6,9 +6,10 @@ @File : file.py @Describe : General file operations. """ -import aiofiles from pathlib import Path +import aiofiles + from metagpt.logs import logger @@ -66,10 +67,9 @@ class File: if not chunk: break chunks.append(chunk) - content = b''.join(chunks) + content = b"".join(chunks) logger.debug(f"Successfully read file, the path of file: {file_path}") return content except Exception as e: logger.error(f"Error reading file: {e}") raise e - diff --git a/metagpt/utils/highlight.py b/metagpt/utils/highlight.py index e6cbb228c..2e1d6f615 100644 --- a/metagpt/utils/highlight.py +++ b/metagpt/utils/highlight.py @@ -1,22 +1,22 @@ # 添加代码语法高亮显示 from pygments import highlight as highlight_ +from pygments.formatters import HtmlFormatter, TerminalFormatter from pygments.lexers import PythonLexer, SqlLexer -from pygments.formatters import TerminalFormatter, HtmlFormatter -def highlight(code: str, language: str = 'python', formatter: str = 'terminal'): +def highlight(code: str, language: str = "python", formatter: str = "terminal"): # 指定要高亮的语言 - if language.lower() == 'python': + if language.lower() == "python": lexer = PythonLexer() - elif language.lower() == 'sql': + elif language.lower() == "sql": lexer = SqlLexer() else: raise ValueError(f"Unsupported language: {language}") # 指定输出格式 - if formatter.lower() == 'terminal': + if formatter.lower() == "terminal": formatter = TerminalFormatter() - elif formatter.lower() == 'html': + elif formatter.lower() == "html": formatter = HtmlFormatter() else: raise ValueError(f"Unsupported formatter: {formatter}") diff --git a/metagpt/utils/mmdc_ink.py b/metagpt/utils/mmdc_ink.py index 3d91cde9d..d594adb30 100644 --- a/metagpt/utils/mmdc_ink.py +++ b/metagpt/utils/mmdc_ink.py @@ -6,9 +6,9 @@ @File : mermaid.py """ import base64 -import os -from aiohttp import ClientSession,ClientError +from aiohttp import ClientError, ClientSession + from metagpt.logs import logger @@ -29,7 +29,7 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix): async with session.get(url) as response: if response.status == 200: text = await response.content.read() - with open(output_file, 'wb') as f: + with open(output_file, "wb") as f: f.write(text) logger.info(f"Generating {output_file}..") else: diff --git a/metagpt/utils/mmdc_playwright.py b/metagpt/utils/mmdc_playwright.py index bdbfd82ff..5d455e1c5 100644 --- a/metagpt/utils/mmdc_playwright.py +++ b/metagpt/utils/mmdc_playwright.py @@ -8,10 +8,13 @@ import os from urllib.parse import urljoin + from playwright.async_api import async_playwright + from metagpt.logs import logger -async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048)-> int: + +async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """ Converts the given Mermaid code to various output formats and saves them to files. @@ -24,66 +27,72 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, Returns: int: Returns 1 if the conversion and saving were successful, -1 otherwise. """ - suffixes=['png', 'svg', 'pdf'] + suffixes = ["png", "svg", "pdf"] __dirname = os.path.dirname(os.path.abspath(__file__)) async with async_playwright() as p: browser = await p.chromium.launch() device_scale_factor = 1.0 context = await browser.new_context( - viewport={'width': width, 'height': height}, - device_scale_factor=device_scale_factor, - ) + viewport={"width": width, "height": height}, + device_scale_factor=device_scale_factor, + ) page = await context.new_page() async def console_message(msg): logger.info(msg.text) - page.on('console', console_message) + + page.on("console", console_message) try: - await page.set_viewport_size({'width': width, 'height': height}) + await page.set_viewport_size({"width": width, "height": height}) - mermaid_html_path = os.path.abspath( - os.path.join(__dirname, 'index.html')) - mermaid_html_url = urljoin('file:', mermaid_html_path) + mermaid_html_path = os.path.abspath(os.path.join(__dirname, "index.html")) + mermaid_html_url = urljoin("file:", mermaid_html_path) await page.goto(mermaid_html_url) await page.wait_for_load_state("networkidle") await page.wait_for_selector("div#container", state="attached") - mermaid_config = {} + # mermaid_config = {} background_color = "#ffffff" - my_css = "" + # my_css = "" await page.evaluate(f'document.body.style.background = "{background_color}";') - metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { - const { mermaid, zenuml } = globalThis; - await mermaid.registerExternalDiagrams([zenuml]); - mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); - const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); - document.getElementById('container').innerHTML = svg; - const svgElement = document.querySelector('svg'); - svgElement.style.backgroundColor = backgroundColor; + # metadata = await page.evaluate( + # """async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + # const { mermaid, zenuml } = globalThis; + # await mermaid.registerExternalDiagrams([zenuml]); + # mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + # const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + # document.getElementById('container').innerHTML = svg; + # const svgElement = document.querySelector('svg'); + # svgElement.style.backgroundColor = backgroundColor; + # + # if (myCSS) { + # const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + # style.appendChild(document.createTextNode(myCSS)); + # svgElement.appendChild(style); + # } + # + # }""", + # [mermaid_code, mermaid_config, my_css, background_color], + # ) - if (myCSS) { - const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); - style.appendChild(document.createTextNode(myCSS)); - svgElement.appendChild(style); - } - - }''', [mermaid_code, mermaid_config, my_css, background_color]) - - if 'svg' in suffixes : - svg_xml = await page.evaluate('''() => { + if "svg" in suffixes: + svg_xml = await page.evaluate( + """() => { const svg = document.querySelector('svg'); const xmlSerializer = new XMLSerializer(); return xmlSerializer.serializeToString(svg); - }''') + }""" + ) logger.info(f"Generating {output_file_without_suffix}.svg..") - with open(f'{output_file_without_suffix}.svg', 'wb') as f: - f.write(svg_xml.encode('utf-8')) + with open(f"{output_file_without_suffix}.svg", "wb") as f: + f.write(svg_xml.encode("utf-8")) - if 'png' in suffixes: - clip = await page.evaluate('''() => { + if "png" in suffixes: + clip = await page.evaluate( + """() => { const svg = document.querySelector('svg'); const rect = svg.getBoundingClientRect(); return { @@ -92,16 +101,17 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, width: Math.ceil(rect.width), height: Math.ceil(rect.height) }; - }''') - await page.set_viewport_size({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height']}) - screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + }""" + ) + await page.set_viewport_size({"width": clip["x"] + clip["width"], "height": clip["y"] + clip["height"]}) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale="device") logger.info(f"Generating {output_file_without_suffix}.png..") - with open(f'{output_file_without_suffix}.png', 'wb') as f: + with open(f"{output_file_without_suffix}.png", "wb") as f: f.write(screenshot) - if 'pdf' in suffixes: + if "pdf" in suffixes: pdf_data = await page.pdf(scale=device_scale_factor) logger.info(f"Generating {output_file_without_suffix}.pdf..") - with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + with open(f"{output_file_without_suffix}.pdf", "wb") as f: f.write(pdf_data) return 0 except Exception as e: diff --git a/metagpt/utils/mmdc_pyppeteer.py b/metagpt/utils/mmdc_pyppeteer.py index 7ec30fd12..7125cafc5 100644 --- a/metagpt/utils/mmdc_pyppeteer.py +++ b/metagpt/utils/mmdc_pyppeteer.py @@ -7,11 +7,14 @@ """ import os from urllib.parse import urljoin -from pyppeteer import launch -from metagpt.logs import logger -from metagpt.config import CONFIG -async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048)-> int: +from pyppeteer import launch + +from metagpt.config import CONFIG +from metagpt.logs import logger + + +async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: """ Converts the given Mermaid code to various output formats and saves them to files. @@ -24,15 +27,15 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, Returns: int: Returns 1 if the conversion and saving were successful, -1 otherwise. """ - suffixes = ['png', 'svg', 'pdf'] + suffixes = ["png", "svg", "pdf"] __dirname = os.path.dirname(os.path.abspath(__file__)) - if CONFIG.pyppeteer_executable_path: - browser = await launch(headless=True, - executablePath=CONFIG.pyppeteer_executable_path, - args=['--disable-extensions',"--no-sandbox"] - ) + browser = await launch( + headless=True, + executablePath=CONFIG.pyppeteer_executable_path, + args=["--disable-extensions", "--no-sandbox"], + ) else: logger.error("Please set the environment variable:PYPPETEER_EXECUTABLE_PATH.") return -1 @@ -41,50 +44,56 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, async def console_message(msg): logger.info(msg.text) - page.on('console', console_message) + + page.on("console", console_message) try: - await page.setViewport(viewport={'width': width, 'height': height, 'deviceScaleFactor': device_scale_factor}) + await page.setViewport(viewport={"width": width, "height": height, "deviceScaleFactor": device_scale_factor}) - mermaid_html_path = os.path.abspath( - os.path.join(__dirname, 'index.html')) - mermaid_html_url = urljoin('file:', mermaid_html_path) + mermaid_html_path = os.path.abspath(os.path.join(__dirname, "index.html")) + mermaid_html_url = urljoin("file:", mermaid_html_path) await page.goto(mermaid_html_url) await page.querySelector("div#container") - mermaid_config = {} + # mermaid_config = {} background_color = "#ffffff" - my_css = "" + # my_css = "" await page.evaluate(f'document.body.style.background = "{background_color}";') - metadata = await page.evaluate('''async ([definition, mermaidConfig, myCSS, backgroundColor]) => { - const { mermaid, zenuml } = globalThis; - await mermaid.registerExternalDiagrams([zenuml]); - mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); - const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); - document.getElementById('container').innerHTML = svg; - const svgElement = document.querySelector('svg'); - svgElement.style.backgroundColor = backgroundColor; + # metadata = await page.evaluate( + # """async ([definition, mermaidConfig, myCSS, backgroundColor]) => { + # const { mermaid, zenuml } = globalThis; + # await mermaid.registerExternalDiagrams([zenuml]); + # mermaid.initialize({ startOnLoad: false, ...mermaidConfig }); + # const { svg } = await mermaid.render('my-svg', definition, document.getElementById('container')); + # document.getElementById('container').innerHTML = svg; + # const svgElement = document.querySelector('svg'); + # svgElement.style.backgroundColor = backgroundColor; + # + # if (myCSS) { + # const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); + # style.appendChild(document.createTextNode(myCSS)); + # svgElement.appendChild(style); + # } + # }""", + # [mermaid_code, mermaid_config, my_css, background_color], + # ) - if (myCSS) { - const style = document.createElementNS('http://www.w3.org/2000/svg', 'style'); - style.appendChild(document.createTextNode(myCSS)); - svgElement.appendChild(style); - } - }''', [mermaid_code, mermaid_config, my_css, background_color]) - - if 'svg' in suffixes : - svg_xml = await page.evaluate('''() => { + if "svg" in suffixes: + svg_xml = await page.evaluate( + """() => { const svg = document.querySelector('svg'); const xmlSerializer = new XMLSerializer(); return xmlSerializer.serializeToString(svg); - }''') + }""" + ) logger.info(f"Generating {output_file_without_suffix}.svg..") - with open(f'{output_file_without_suffix}.svg', 'wb') as f: - f.write(svg_xml.encode('utf-8')) + with open(f"{output_file_without_suffix}.svg", "wb") as f: + f.write(svg_xml.encode("utf-8")) - if 'png' in suffixes: - clip = await page.evaluate('''() => { + if "png" in suffixes: + clip = await page.evaluate( + """() => { const svg = document.querySelector('svg'); const rect = svg.getBoundingClientRect(); return { @@ -93,16 +102,23 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, width: Math.ceil(rect.width), height: Math.ceil(rect.height) }; - }''') - await page.setViewport({'width': clip['x'] + clip['width'], 'height': clip['y'] + clip['height'], 'deviceScaleFactor': device_scale_factor}) - screenshot = await page.screenshot(clip=clip, omit_background=True, scale='device') + }""" + ) + await page.setViewport( + { + "width": clip["x"] + clip["width"], + "height": clip["y"] + clip["height"], + "deviceScaleFactor": device_scale_factor, + } + ) + screenshot = await page.screenshot(clip=clip, omit_background=True, scale="device") logger.info(f"Generating {output_file_without_suffix}.png..") - with open(f'{output_file_without_suffix}.png', 'wb') as f: + with open(f"{output_file_without_suffix}.png", "wb") as f: f.write(screenshot) - if 'pdf' in suffixes: + if "pdf" in suffixes: pdf_data = await page.pdf(scale=device_scale_factor) logger.info(f"Generating {output_file_without_suffix}.pdf..") - with open(f'{output_file_without_suffix}.pdf', 'wb') as f: + with open(f"{output_file_without_suffix}.pdf", "wb") as f: f.write(pdf_data) return 0 except Exception as e: @@ -110,4 +126,3 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, return -1 finally: await browser.close() - diff --git a/metagpt/utils/parse_html.py b/metagpt/utils/parse_html.py index 62de26541..f2395026f 100644 --- a/metagpt/utils/parse_html.py +++ b/metagpt/utils/parse_html.py @@ -16,7 +16,7 @@ class WebPage(BaseModel): class Config: underscore_attrs_are_private = True - _soup : Optional[BeautifulSoup] = None + _soup: Optional[BeautifulSoup] = None _title: Optional[str] = None @property @@ -24,7 +24,7 @@ class WebPage(BaseModel): if self._soup is None: self._soup = BeautifulSoup(self.html, "html.parser") return self._soup - + @property def title(self): if self._title is None: diff --git a/metagpt/utils/pycst.py b/metagpt/utils/pycst.py index afd85a547..1edfed81c 100644 --- a/metagpt/utils/pycst.py +++ b/metagpt/utils/pycst.py @@ -37,12 +37,12 @@ def get_docstring_statement(body: DocstringNode) -> cst.SimpleStatementLine: if not isinstance(expr, cst.Expr): return None - + val = expr.value if not isinstance(val, (cst.SimpleString, cst.ConcatenatedString)): return None - - evaluated_value = val.evaluated_value + + evaluated_value = val.evaluated_value if isinstance(evaluated_value, bytes): return None @@ -56,6 +56,7 @@ class DocstringCollector(cst.CSTVisitor): stack: A list to keep track of the current path in the CST. docstrings: A dictionary mapping paths in the CST to their corresponding docstrings. """ + def __init__(self): self.stack: list[str] = [] self.docstrings: dict[tuple[str, ...], cst.SimpleStatementLine] = {} @@ -96,6 +97,7 @@ class DocstringTransformer(cst.CSTTransformer): stack: A list to keep track of the current path in the CST. docstrings: A dictionary mapping paths in the CST to their corresponding docstrings. """ + def __init__( self, docstrings: dict[tuple[str, ...], cst.SimpleStatementLine], @@ -125,7 +127,9 @@ class DocstringTransformer(cst.CSTTransformer): key = tuple(self.stack) self.stack.pop() - if hasattr(updated_node, "decorators") and any((i.decorator.value == "overload") for i in updated_node.decorators): + if hasattr(updated_node, "decorators") and any( + (i.decorator.value == "overload") for i in updated_node.decorators + ): return updated_node statement = self.docstrings.get(key) diff --git a/metagpt/utils/read_document.py b/metagpt/utils/read_document.py index c837baf25..d2fafbc17 100644 --- a/metagpt/utils/read_document.py +++ b/metagpt/utils/read_document.py @@ -8,6 +8,7 @@ import docx + def read_docx(file_path: str) -> list: """Open a docx file""" doc = docx.Document(file_path) diff --git a/metagpt/utils/singleton.py b/metagpt/utils/singleton.py index 474b537db..a9e0862c0 100644 --- a/metagpt/utils/singleton.py +++ b/metagpt/utils/singleton.py @@ -20,4 +20,3 @@ class Singleton(abc.ABCMeta, type): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] - \ No newline at end of file diff --git a/metagpt/utils/special_tokens.py b/metagpt/utils/special_tokens.py index 2adb93c77..5e780ce05 100644 --- a/metagpt/utils/special_tokens.py +++ b/metagpt/utils/special_tokens.py @@ -1,4 +1,4 @@ # token to separate different code messages in a WriteCode Message content -MSG_SEP = "#*000*#" +MSG_SEP = "#*000*#" # token to seperate file name and the actual code text in a code message FILENAME_CODE_SEP = "#*001*#" diff --git a/metagpt/utils/text.py b/metagpt/utils/text.py index be3c52edd..dd9678438 100644 --- a/metagpt/utils/text.py +++ b/metagpt/utils/text.py @@ -3,7 +3,12 @@ from typing import Generator, Sequence from metagpt.utils.token_counter import TOKEN_MAX, count_string_tokens -def reduce_message_length(msgs: Generator[str, None, None], model_name: str, system_text: str, reserved: int = 0,) -> str: +def reduce_message_length( + msgs: Generator[str, None, None], + model_name: str, + system_text: str, + reserved: int = 0, +) -> str: """Reduce the length of concatenated message segments to fit within the maximum token size. Args: @@ -49,9 +54,9 @@ def generate_prompt_chunk( current_token = 0 current_lines = [] - reserved = reserved + count_string_tokens(prompt_template+system_text, model_name) + reserved = reserved + count_string_tokens(prompt_template + system_text, model_name) # 100 is a magic number to ensure the maximum context length is not exceeded - max_token = TOKEN_MAX.get(model_name, 2048) - reserved - 100 + max_token = TOKEN_MAX.get(model_name, 2048) - reserved - 100 while paragraphs: paragraph = paragraphs.pop(0) @@ -103,7 +108,7 @@ def decode_unicode_escape(text: str) -> str: return text.encode("utf-8").decode("unicode_escape", "ignore") -def _split_by_count(lst: Sequence , count: int): +def _split_by_count(lst: Sequence, count: int): avg = len(lst) // count remainder = len(lst) % count start = 0 diff --git a/tests/conftest.py b/tests/conftest.py index feecc7715..d2ac8304f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,14 +6,14 @@ @File : conftest.py """ +import asyncio +import re from unittest.mock import Mock import pytest from metagpt.logs import logger from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI -import asyncio -import re class Context: diff --git a/tests/metagpt/actions/mock.py b/tests/metagpt/actions/mock.py index a800690e8..23d10ccc4 100644 --- a/tests/metagpt/actions/mock.py +++ b/tests/metagpt/actions/mock.py @@ -311,12 +311,10 @@ TASKS = [ "添加数据API:接受用户输入的文档库,对文档库进行索引\n- 使用MeiliSearch连接并添加文档库", "搜索API:接收用户输入的关键词,返回相关的搜索结果\n- 使用MeiliSearch连接并使用接口获得对应数据", "多条件筛选API:接收用户选择的筛选条件,返回符合条件的搜索结果。\n- 使用MeiliSearch进行筛选并返回符合条件的搜索结果", - "智能推荐API:根据用户的搜索历史记录和搜索行为,推荐相关的搜索结果。" + "智能推荐API:根据用户的搜索历史记录和搜索行为,推荐相关的搜索结果。", ] -TASKS_2 = [ - "完成main.py的功能" -] +TASKS_2 = ["完成main.py的功能"] SEARCH_CODE_SAMPLE = """ import requests @@ -460,7 +458,7 @@ if __name__ == '__main__': print('No results found.') ''' -MEILI_CODE = '''import meilisearch +MEILI_CODE = """import meilisearch from typing import List @@ -496,9 +494,9 @@ if __name__ == '__main__': # 添加文档库到搜索引擎 search_engine.add_documents(books_data_source, documents) -''' +""" -MEILI_ERROR = '''/usr/local/bin/python3.9 /Users/alexanderwu/git/metagpt/examples/search/meilisearch_index.py +MEILI_ERROR = """/usr/local/bin/python3.9 /Users/alexanderwu/git/metagpt/examples/search/meilisearch_index.py Traceback (most recent call last): File "/Users/alexanderwu/git/metagpt/examples/search/meilisearch_index.py", line 44, in search_engine.add_documents(books_data_source, documents) @@ -506,7 +504,7 @@ Traceback (most recent call last): index = self.client.get_or_create_index(index_name) AttributeError: 'Client' object has no attribute 'get_or_create_index' -Process finished with exit code 1''' +Process finished with exit code 1""" MEILI_CODE_REFINED = """ """ diff --git a/tests/metagpt/actions/test_action_output.py b/tests/metagpt/actions/test_action_output.py index a556789db..ef8e239bd 100644 --- a/tests/metagpt/actions/test_action_output.py +++ b/tests/metagpt/actions/test_action_output.py @@ -9,18 +9,21 @@ from typing import List, Tuple from metagpt.actions import ActionOutput -t_dict = {"Required Python third-party packages": "\"\"\"\nflask==1.1.2\npygame==2.0.1\n\"\"\"\n", - "Required Other language third-party packages": "\"\"\"\nNo third-party packages required for other languages.\n\"\"\"\n", - "Full API spec": "\"\"\"\nopenapi: 3.0.0\ninfo:\n title: Web Snake Game API\n version: 1.0.0\npaths:\n /game:\n get:\n summary: Get the current game state\n responses:\n '200':\n description: A JSON object of the game state\n post:\n summary: Send a command to the game\n requestBody:\n required: true\n content:\n application/json:\n schema:\n type: object\n properties:\n command:\n type: string\n responses:\n '200':\n description: A JSON object of the updated game state\n\"\"\"\n", - "Logic Analysis": [ - ["app.py", "Main entry point for the Flask application. Handles HTTP requests and responses."], - ["game.py", "Contains the Game and Snake classes. Handles the game logic."], - ["static/js/script.js", "Handles user interactions and updates the game UI."], - ["static/css/styles.css", "Defines the styles for the game UI."], - ["templates/index.html", "The main page of the web application. Displays the game UI."]], - "Task list": ["game.py", "app.py", "static/css/styles.css", "static/js/script.js", "templates/index.html"], - "Shared Knowledge": "\"\"\"\n'game.py' contains the Game and Snake classes which are responsible for the game logic. The Game class uses an instance of the Snake class.\n\n'app.py' is the main entry point for the Flask application. It creates an instance of the Game class and handles HTTP requests and responses.\n\n'static/js/script.js' is responsible for handling user interactions and updating the game UI based on the game state returned by 'app.py'.\n\n'static/css/styles.css' defines the styles for the game UI.\n\n'templates/index.html' is the main page of the web application. It displays the game UI and loads 'static/js/script.js' and 'static/css/styles.css'.\n\"\"\"\n", - "Anything UNCLEAR": "We need clarification on how the high score should be stored. Should it persist across sessions (stored in a database or a file) or should it reset every time the game is restarted? Also, should the game speed increase as the snake grows, or should it remain constant throughout the game?"} +t_dict = { + "Required Python third-party packages": '"""\nflask==1.1.2\npygame==2.0.1\n"""\n', + "Required Other language third-party packages": '"""\nNo third-party packages required for other languages.\n"""\n', + "Full API spec": '"""\nopenapi: 3.0.0\ninfo:\n title: Web Snake Game API\n version: 1.0.0\npaths:\n /game:\n get:\n summary: Get the current game state\n responses:\n \'200\':\n description: A JSON object of the game state\n post:\n summary: Send a command to the game\n requestBody:\n required: true\n content:\n application/json:\n schema:\n type: object\n properties:\n command:\n type: string\n responses:\n \'200\':\n description: A JSON object of the updated game state\n"""\n', + "Logic Analysis": [ + ["app.py", "Main entry point for the Flask application. Handles HTTP requests and responses."], + ["game.py", "Contains the Game and Snake classes. Handles the game logic."], + ["static/js/script.js", "Handles user interactions and updates the game UI."], + ["static/css/styles.css", "Defines the styles for the game UI."], + ["templates/index.html", "The main page of the web application. Displays the game UI."], + ], + "Task list": ["game.py", "app.py", "static/css/styles.css", "static/js/script.js", "templates/index.html"], + "Shared Knowledge": "\"\"\"\n'game.py' contains the Game and Snake classes which are responsible for the game logic. The Game class uses an instance of the Snake class.\n\n'app.py' is the main entry point for the Flask application. It creates an instance of the Game class and handles HTTP requests and responses.\n\n'static/js/script.js' is responsible for handling user interactions and updating the game UI based on the game state returned by 'app.py'.\n\n'static/css/styles.css' defines the styles for the game UI.\n\n'templates/index.html' is the main page of the web application. It displays the game UI and loads 'static/js/script.js' and 'static/css/styles.css'.\n\"\"\"\n", + "Anything UNCLEAR": "We need clarification on how the high score should be stored. Should it persist across sessions (stored in a database or a file) or should it reset every time the game is restarted? Also, should the game speed increase as the snake grows, or should it remain constant throughout the game?", +} WRITE_TASKS_OUTPUT_MAPPING = { "Required Python third-party packages": (str, ...), @@ -45,6 +48,6 @@ def test_create_model_class_with_mapping(): assert value == ["game.py", "app.py", "static/css/styles.css", "static/js/script.js", "templates/index.html"] -if __name__ == '__main__': +if __name__ == "__main__": test_create_model_class() test_create_model_class_with_mapping() diff --git a/tests/metagpt/actions/test_azure_tts.py b/tests/metagpt/actions/test_azure_tts.py index b5a333af2..bcafe10f5 100644 --- a/tests/metagpt/actions/test_azure_tts.py +++ b/tests/metagpt/actions/test_azure_tts.py @@ -10,12 +10,7 @@ from metagpt.actions.azure_tts import AzureTTS def test_azure_tts(): azure_tts = AzureTTS("azure_tts") - azure_tts.synthesize_speech( - "zh-CN", - "zh-CN-YunxiNeural", - "Boy", - "你好,我是卡卡", - "output.wav") + azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "你好,我是卡卡", "output.wav") # 运行需要先配置 SUBSCRIPTION_KEY # TODO: 这里如果要检验,还要额外加上对应的asr,才能确保前后生成是接近一致的,但现在还没有 diff --git a/tests/metagpt/actions/test_clone_function.py b/tests/metagpt/actions/test_clone_function.py index 6d4432dcd..44248eb80 100644 --- a/tests/metagpt/actions/test_clone_function.py +++ b/tests/metagpt/actions/test_clone_function.py @@ -2,7 +2,6 @@ import pytest from metagpt.actions.clone_function import CloneFunction, run_function_code - source_code = """ import pandas as pd import ta @@ -31,14 +30,18 @@ def get_expected_res(): import ta # 读取股票数据 - stock_data = pd.read_csv('./tests/data/baba_stock.csv') + stock_data = pd.read_csv("./tests/data/baba_stock.csv") stock_data.head() # 计算简单移动平均线 - stock_data['SMA'] = ta.trend.sma_indicator(stock_data['Close'], window=6) - stock_data[['Date', 'Close', 'SMA']].head() + stock_data["SMA"] = ta.trend.sma_indicator(stock_data["Close"], window=6) + stock_data[["Date", "Close", "SMA"]].head() # 计算布林带 - stock_data['bb_upper'], stock_data['bb_middle'], stock_data['bb_lower'] = ta.volatility.bollinger_hband_indicator(stock_data['Close'], window=20), ta.volatility.bollinger_mavg(stock_data['Close'], window=20), ta.volatility.bollinger_lband_indicator(stock_data['Close'], window=20) - stock_data[['Date', 'Close', 'bb_upper', 'bb_middle', 'bb_lower']].head() + stock_data["bb_upper"], stock_data["bb_middle"], stock_data["bb_lower"] = ( + ta.volatility.bollinger_hband_indicator(stock_data["Close"], window=20), + ta.volatility.bollinger_mavg(stock_data["Close"], window=20), + ta.volatility.bollinger_lband_indicator(stock_data["Close"], window=20), + ) + stock_data[["Date", "Close", "bb_upper", "bb_middle", "bb_lower"]].head() return stock_data @@ -46,9 +49,9 @@ def get_expected_res(): async def test_clone_function(): clone = CloneFunction() code = await clone.run(template_code, source_code) - assert 'def ' in code - stock_path = './tests/data/baba_stock.csv' - df, msg = run_function_code(code, 'stock_indicator', stock_path) + assert "def " in code + stock_path = "./tests/data/baba_stock.csv" + df, msg = run_function_code(code, "stock_indicator", stock_path) assert not msg expected_df = get_expected_res() assert df.equals(expected_df) diff --git a/tests/metagpt/actions/test_debug_error.py b/tests/metagpt/actions/test_debug_error.py index 555c84e4e..2393d2cc9 100644 --- a/tests/metagpt/actions/test_debug_error.py +++ b/tests/metagpt/actions/test_debug_error.py @@ -144,12 +144,12 @@ Engineer --- ''' + @pytest.mark.asyncio async def test_debug_error(): - debug_error = DebugError("debug_error") file_name, rewritten_code = await debug_error.run(context=EXAMPLE_MSG_CONTENT) - assert "class Player" in rewritten_code # rewrite the same class - assert "while self.score > 21" in rewritten_code # a key logic to rewrite to (original one is "if self.score > 12") + assert "class Player" in rewritten_code # rewrite the same class + assert "while self.score > 21" in rewritten_code # a key logic to rewrite to (original one is "if self.score > 12") diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_detail_mining.py index c9d5331f9..891dca6ca 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_detail_mining.py @@ -10,6 +10,7 @@ import pytest from metagpt.actions.detail_mining import DetailMining from metagpt.logs import logger + @pytest.mark.asyncio async def test_detail_mining(): topic = "如何做一个生日蛋糕" @@ -17,7 +18,6 @@ async def test_detail_mining(): detail_mining = DetailMining("detail_mining") rsp = await detail_mining.run(topic=topic, record=record) logger.info(f"{rsp.content=}") - - assert '##OUTPUT' in rsp.content - assert '蛋糕' in rsp.content + assert "##OUTPUT" in rsp.content + assert "蛋糕" in rsp.content diff --git a/tests/metagpt/actions/test_invoice_ocr.py b/tests/metagpt/actions/test_invoice_ocr.py index a15166f7c..7f16aa9a4 100644 --- a/tests/metagpt/actions/test_invoice_ocr.py +++ b/tests/metagpt/actions/test_invoice_ocr.py @@ -8,12 +8,11 @@ """ import os -from typing import List - -import pytest from pathlib import Path -from metagpt.actions.invoice_ocr import InvoiceOCR, GenerateTable, ReplyQuestion +import pytest + +from metagpt.actions.invoice_ocr import GenerateTable, InvoiceOCR, ReplyQuestion @pytest.mark.asyncio @@ -22,7 +21,7 @@ from metagpt.actions.invoice_ocr import InvoiceOCR, GenerateTable, ReplyQuestion [ "../../data/invoices/invoice-3.jpg", "../../data/invoices/invoice-4.zip", - ] + ], ) async def test_invoice_ocr(invoice_path: str): invoice_path = os.path.abspath(os.path.join(os.getcwd(), invoice_path)) @@ -35,18 +34,8 @@ async def test_invoice_ocr(invoice_path: str): @pytest.mark.parametrize( ("invoice_path", "expected_result"), [ - ( - "../../data/invoices/invoice-1.pdf", - [ - { - "收款人": "小明", - "城市": "深圳市", - "总费用/元": "412.00", - "开票日期": "2023年02月03日" - } - ] - ), - ] + ("../../data/invoices/invoice-1.pdf", [{"收款人": "小明", "城市": "深圳市", "总费用/元": "412.00", "开票日期": "2023年02月03日"}]), + ], ) async def test_generate_table(invoice_path: str, expected_result: list[dict]): invoice_path = os.path.abspath(os.path.join(os.getcwd(), invoice_path)) @@ -59,9 +48,7 @@ async def test_generate_table(invoice_path: str, expected_result: list[dict]): @pytest.mark.asyncio @pytest.mark.parametrize( ("invoice_path", "query", "expected_result"), - [ - ("../../data/invoices/invoice-1.pdf", "Invoicing date", "2023年02月03日") - ] + [("../../data/invoices/invoice-1.pdf", "Invoicing date", "2023年02月03日")], ) async def test_reply_question(invoice_path: str, query: dict, expected_result: str): invoice_path = os.path.abspath(os.path.join(os.getcwd(), invoice_path)) @@ -69,4 +56,3 @@ async def test_reply_question(invoice_path: str, query: dict, expected_result: s ocr_result = await InvoiceOCR().run(file_path=Path(invoice_path), filename=filename) result = await ReplyQuestion().run(query=query, ocr_result=ocr_result) assert expected_result in result - diff --git a/tests/metagpt/actions/test_ui_design.py b/tests/metagpt/actions/test_ui_design.py index d284b20f2..b8be914ae 100644 --- a/tests/metagpt/actions/test_ui_design.py +++ b/tests/metagpt/actions/test_ui_design.py @@ -4,7 +4,7 @@ # from tests.metagpt.roles.ui_role import UIDesign -llm_resp= ''' +llm_resp = """ # UI Design Description ```The user interface for the snake game will be designed in a way that is simple, clean, and intuitive. The main elements of the game such as the game grid, snake, food, score, and game over message will be clearly defined and easy to understand. The game grid will be centered on the screen with the score displayed at the top. The game controls will be intuitive and easy to use. The design will be modern and minimalist with a pleasing color scheme.``` @@ -98,12 +98,13 @@ body { left: 50%; transform: translate(-50%, -50%); font-size: 3em; - ''' + """ + def test_ui_design_parse_css(): ui_design_work = UIDesign(name="UI design action") - css = ''' + css = """ body { display: flex; flex-direction: column; @@ -160,14 +161,14 @@ def test_ui_design_parse_css(): left: 50%; transform: translate(-50%, -50%); font-size: 3em; - ''' - assert ui_design_work.parse_css_code(context=llm_resp)==css + """ + assert ui_design_work.parse_css_code(context=llm_resp) == css def test_ui_design_parse_html(): ui_design_work = UIDesign(name="UI design action") - html = ''' + html = """ @@ -184,8 +185,5 @@ def test_ui_design_parse_html():
Game Over
- ''' - assert ui_design_work.parse_css_code(context=llm_resp)==html - - - + """ + assert ui_design_work.parse_css_code(context=llm_resp) == html diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index 7bb18ddf2..eb5e3de91 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -22,13 +22,13 @@ async def test_write_code(): logger.info(code) # 我们不能精确地预测生成的代码,但我们可以检查某些关键字 - assert 'def add' in code - assert 'return' in code + assert "def add" in code + assert "return" in code @pytest.mark.asyncio async def test_write_code_directly(): - prompt = WRITE_CODE_PROMPT_SAMPLE + '\n' + TASKS_2[0] + prompt = WRITE_CODE_PROMPT_SAMPLE + "\n" + TASKS_2[0] llm = LLM() rsp = await llm.aask(prompt) logger.info(rsp) diff --git a/tests/metagpt/actions/test_write_docstring.py b/tests/metagpt/actions/test_write_docstring.py index 82d96e1a6..a8a80b36d 100644 --- a/tests/metagpt/actions/test_write_docstring.py +++ b/tests/metagpt/actions/test_write_docstring.py @@ -2,7 +2,7 @@ import pytest from metagpt.actions.write_docstring import WriteDocstring -code = ''' +code = """ def add_numbers(a: int, b: int): return a + b @@ -14,7 +14,7 @@ class Person: def greet(self): return f"Hello, my name is {self.name} and I am {self.age} years old." -''' +""" @pytest.mark.asyncio @@ -25,7 +25,7 @@ class Person: ("numpy", "Parameters"), ("sphinx", ":param name:"), ], - ids=["google", "numpy", "sphinx"] + ids=["google", "numpy", "sphinx"], ) async def test_write_docstring(style: str, part: str): ret = await WriteDocstring().run(code, style=style) diff --git a/tests/metagpt/actions/test_write_tutorial.py b/tests/metagpt/actions/test_write_tutorial.py index 683fee082..27a323b44 100644 --- a/tests/metagpt/actions/test_write_tutorial.py +++ b/tests/metagpt/actions/test_write_tutorial.py @@ -9,14 +9,11 @@ from typing import Dict import pytest -from metagpt.actions.write_tutorial import WriteDirectory, WriteContent +from metagpt.actions.write_tutorial import WriteContent, WriteDirectory @pytest.mark.asyncio -@pytest.mark.parametrize( - ("language", "topic"), - [("English", "Write a tutorial about Python")] -) +@pytest.mark.parametrize(("language", "topic"), [("English", "Write a tutorial about Python")]) async def test_write_directory(language: str, topic: str): ret = await WriteDirectory(language=language).run(topic=topic) assert isinstance(ret, dict) @@ -30,7 +27,7 @@ async def test_write_directory(language: str, topic: str): @pytest.mark.asyncio @pytest.mark.parametrize( ("language", "topic", "directory"), - [("English", "Write a tutorial about Python", {"Introduction": ["What is Python?", "Why learn Python?"]})] + [("English", "Write a tutorial about Python", {"Introduction": ["What is Python?", "Why learn Python?"]})], ) async def test_write_content(language: str, topic: str, directory: Dict): ret = await WriteContent(language=language, directory=directory).run(topic=topic) diff --git a/tests/metagpt/document_store/test_chromadb_store.py b/tests/metagpt/document_store/test_chromadb_store.py index f8c11e1ca..fd115dcdd 100644 --- a/tests/metagpt/document_store/test_chromadb_store.py +++ b/tests/metagpt/document_store/test_chromadb_store.py @@ -12,12 +12,12 @@ from metagpt.document_store.chromadb_store import ChromaStore def test_chroma_store(): """FIXME:chroma使用感觉很诡异,一用Python就挂,测试用例里也是""" # 创建 ChromaStore 实例,使用 'sample_collection' 集合 - document_store = ChromaStore('sample_collection_1') + document_store = ChromaStore("sample_collection_1") # 使用 write 方法添加多个文档 - document_store.write(["This is document1", "This is document2"], - [{"source": "google-docs"}, {"source": "notion"}], - ["doc1", "doc2"]) + document_store.write( + ["This is document1", "This is document2"], [{"source": "google-docs"}, {"source": "notion"}], ["doc1", "doc2"] + ) # 使用 add 方法添加一个文档 document_store.add("This is document3", {"source": "notion"}, "doc3") diff --git a/tests/metagpt/document_store/test_faiss_store.py b/tests/metagpt/document_store/test_faiss_store.py index d22d234f5..f14bee817 100644 --- a/tests/metagpt/document_store/test_faiss_store.py +++ b/tests/metagpt/document_store/test_faiss_store.py @@ -39,11 +39,11 @@ user: 没有了 @pytest.mark.asyncio async def test_faiss_store_search(): - store = FaissStore(DATA_PATH / 'qcs/qcs_4w.json') - store.add(['油皮洗面奶']) + store = FaissStore(DATA_PATH / "qcs/qcs_4w.json") + store.add(["油皮洗面奶"]) role = Sales(store=store) - queries = ['油皮洗面奶', '介绍下欧莱雅的'] + queries = ["油皮洗面奶", "介绍下欧莱雅的"] for query in queries: rsp = await role.run(query) assert rsp @@ -60,7 +60,10 @@ def customer_service(): async def test_faiss_store_customer_service(): allq = [ # ["我的餐怎么两小时都没到", "退货吧"], - ["你好收不到取餐码,麻烦帮我开箱", "14750187158", ] + [ + "你好收不到取餐码,麻烦帮我开箱", + "14750187158", + ] ] role = customer_service() for queries in allq: @@ -71,4 +74,4 @@ async def test_faiss_store_customer_service(): def test_faiss_store_no_file(): with pytest.raises(FileNotFoundError): - FaissStore(DATA_PATH / 'wtf.json') + FaissStore(DATA_PATH / "wtf.json") diff --git a/tests/metagpt/document_store/test_lancedb_store.py b/tests/metagpt/document_store/test_lancedb_store.py index 9c2f9fb42..5c0e40f57 100644 --- a/tests/metagpt/document_store/test_lancedb_store.py +++ b/tests/metagpt/document_store/test_lancedb_store.py @@ -5,27 +5,33 @@ @Author : unkn-wn (Leon Yee) @File : test_lancedb_store.py """ -from metagpt.document_store.lancedb_store import LanceStore -import pytest import random +import pytest + +from metagpt.document_store.lancedb_store import LanceStore + + @pytest def test_lance_store(): - # This simply establishes the connection to the database, so we can drop the table if it exists - store = LanceStore('test') + store = LanceStore("test") - store.drop('test') + store.drop("test") - store.write(data=[[random.random() for _ in range(100)] for _ in range(2)], - metadatas=[{"source": "google-docs"}, {"source": "notion"}], - ids=["doc1", "doc2"]) + store.write( + data=[[random.random() for _ in range(100)] for _ in range(2)], + metadatas=[{"source": "google-docs"}, {"source": "notion"}], + ids=["doc1", "doc2"], + ) store.add(data=[random.random() for _ in range(100)], metadata={"source": "notion"}, _id="doc3") result = store.search([random.random() for _ in range(100)], n_results=3) - assert(len(result) == 3) + assert len(result) == 3 store.delete("doc2") - result = store.search([random.random() for _ in range(100)], n_results=3, where="source = 'notion'", metric='cosine') - assert(len(result) == 1) \ No newline at end of file + result = store.search( + [random.random() for _ in range(100)], n_results=3, where="source = 'notion'", metric="cosine" + ) + assert len(result) == 1 diff --git a/tests/metagpt/document_store/test_milvus_store.py b/tests/metagpt/document_store/test_milvus_store.py index 1cf65776d..34497b9c6 100644 --- a/tests/metagpt/document_store/test_milvus_store.py +++ b/tests/metagpt/document_store/test_milvus_store.py @@ -12,7 +12,7 @@ import numpy as np from metagpt.document_store.milvus_store import MilvusConnection, MilvusStore from metagpt.logs import logger -book_columns = {'idx': int, 'name': str, 'desc': str, 'emb': np.ndarray, 'price': float} +book_columns = {"idx": int, "name": str, "desc": str, "emb": np.ndarray, "price": float} book_data = [ [i for i in range(10)], [f"book-{i}" for i in range(10)], @@ -25,12 +25,12 @@ book_data = [ def test_milvus_store(): milvus_connection = MilvusConnection(alias="default", host="192.168.50.161", port="30530") milvus_store = MilvusStore(milvus_connection) - milvus_store.drop('Book') - milvus_store.create_collection('Book', book_columns) + milvus_store.drop("Book") + milvus_store.create_collection("Book", book_columns) milvus_store.add(book_data) - milvus_store.build_index('emb') + milvus_store.build_index("emb") milvus_store.load_collection() - results = milvus_store.search([[1.0, 1.0]], field='emb') + results = milvus_store.search([[1.0, 1.0]], field="emb") logger.info(results) assert results diff --git a/tests/metagpt/document_store/test_qdrant_store.py b/tests/metagpt/document_store/test_qdrant_store.py index a63a4329d..cdd619d37 100644 --- a/tests/metagpt/document_store/test_qdrant_store.py +++ b/tests/metagpt/document_store/test_qdrant_store.py @@ -24,9 +24,7 @@ random.seed(seed_value) vectors = [[random.random() for _ in range(2)] for _ in range(10)] points = [ - PointStruct( - id=idx, vector=vector, payload={"color": "red", "rand_number": idx % 10} - ) + PointStruct(id=idx, vector=vector, payload={"color": "red", "rand_number": idx % 10}) for idx, vector in enumerate(vectors) ] @@ -57,9 +55,7 @@ def test_milvus_store(): results = qdrant_store.search( "Book", query=[1.0, 1.0], - query_filter=Filter( - must=[FieldCondition(key="rand_number", range=Range(gte=8))] - ), + query_filter=Filter(must=[FieldCondition(key="rand_number", range=Range(gte=8))]), ) assert results[0]["id"] == 8 assert results[0]["score"] == 0.9100373450784073 @@ -68,9 +64,7 @@ def test_milvus_store(): results = qdrant_store.search( "Book", query=[1.0, 1.0], - query_filter=Filter( - must=[FieldCondition(key="rand_number", range=Range(gte=8))] - ), + query_filter=Filter(must=[FieldCondition(key="rand_number", range=Range(gte=8))]), return_vector=True, ) assert results[0]["vector"] == [0.35037919878959656, 0.9366079568862915] diff --git a/tests/metagpt/management/test_skill_manager.py b/tests/metagpt/management/test_skill_manager.py index b0be858a1..462bc23a6 100644 --- a/tests/metagpt/management/test_skill_manager.py +++ b/tests/metagpt/management/test_skill_manager.py @@ -30,7 +30,7 @@ def test_skill_manager(): rsp = manager.retrieve_skill("写测试用例") logger.info(rsp) - assert rsp[0] == 'WriteTest' + assert rsp[0] == "WriteTest" rsp = manager.retrieve_skill_scored("写PRD") logger.info(rsp) diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index dc5540520..9682ba760 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -2,11 +2,11 @@ # -*- coding: utf-8 -*- # @Desc : unittest of `metagpt/memory/longterm_memory.py` -from metagpt.config import CONFIG -from metagpt.schema import Message from metagpt.actions import BossRequirement -from metagpt.roles.role import RoleContext +from metagpt.config import CONFIG from metagpt.memory import LongTermMemory +from metagpt.roles.role import RoleContext +from metagpt.schema import Message def test_ltm_search(): @@ -14,25 +14,25 @@ def test_ltm_search(): openai_api_key = CONFIG.openai_api_key assert len(openai_api_key) > 20 - role_id = 'UTUserLtm(Product Manager)' + role_id = "UTUserLtm(Product Manager)" rc = RoleContext(watch=[BossRequirement]) ltm = LongTermMemory() ltm.recover_memory(role_id, rc) - idea = 'Write a cli snake game' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + idea = "Write a cli snake game" + message = Message(role="BOSS", content=idea, cause_by=BossRequirement) news = ltm.find_news([message]) assert len(news) == 1 ltm.add(message) - sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_idea = "Write a game of cli snake" + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement) news = ltm.find_news([sim_message]) assert len(news) == 0 ltm.add(sim_message) - new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a 2048 web game" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) news = ltm.find_news([new_message]) assert len(news) == 1 ltm.add(new_message) @@ -47,8 +47,8 @@ def test_ltm_search(): news = ltm_new.find_news([sim_message]) assert len(news) == 0 - new_idea = 'Write a Battle City' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a Battle City" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) news = ltm_new.find_news([new_message]) assert len(news) == 1 diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index 6bb3e8f1d..8b338a79e 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -4,17 +4,16 @@ from typing import List +from metagpt.actions import BossRequirement, WritePRD +from metagpt.actions.action_output import ActionOutput from metagpt.memory.memory_storage import MemoryStorage from metagpt.schema import Message -from metagpt.actions import BossRequirement -from metagpt.actions import WritePRD -from metagpt.actions.action_output import ActionOutput def test_idea_message(): - idea = 'Write a cli snake game' - role_id = 'UTUser1(Product Manager)' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + idea = "Write a cli snake game" + role_id = "UTUser1(Product Manager)" + message = Message(role="BOSS", content=idea, cause_by=BossRequirement) memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -23,13 +22,13 @@ def test_idea_message(): memory_storage.add(message) assert memory_storage.is_initialized is True - sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_idea = "Write a game of cli snake" + sim_message = Message(role="BOSS", content=sim_idea, cause_by=BossRequirement) new_messages = memory_storage.search(sim_message) - assert len(new_messages) == 0 # similar, return [] + assert len(new_messages) == 0 # similar, return [] - new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_idea = "Write a 2048 web game" + new_message = Message(role="BOSS", content=new_idea, cause_by=BossRequirement) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content @@ -38,22 +37,15 @@ def test_idea_message(): def test_actionout_message(): - out_mapping = { - 'field1': (str, ...), - 'field2': (List[str], ...) - } - out_data = { - 'field1': 'field1 value', - 'field2': ['field2 value1', 'field2 value2'] - } - ic_obj = ActionOutput.create_model_class('prd', out_mapping) + out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} + out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} + ic_obj = ActionOutput.create_model_class("prd", out_mapping) - role_id = 'UTUser2(Architect)' - content = 'The boss has requested the creation of a command-line interface (CLI) snake game' - message = Message(content=content, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) # WritePRD as test action + role_id = "UTUser2(Architect)" + content = "The boss has requested the creation of a command-line interface (CLI) snake game" + message = Message( + content=content, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD + ) # WritePRD as test action memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -62,19 +54,13 @@ def test_actionout_message(): memory_storage.add(message) assert memory_storage.is_initialized is True - sim_conent = 'The request is command-line interface (CLI) snake game' - sim_message = Message(content=sim_conent, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) + sim_conent = "The request is command-line interface (CLI) snake game" + sim_message = Message(content=sim_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD) new_messages = memory_storage.search(sim_message) - assert len(new_messages) == 0 # similar, return [] + assert len(new_messages) == 0 # similar, return [] - new_conent = 'Incorporate basic features of a snake game such as scoring and increasing difficulty' - new_message = Message(content=new_conent, - instruct_content=ic_obj(**out_data), - role='user', - cause_by=WritePRD) + new_conent = "Incorporate basic features of a snake game such as scoring and increasing difficulty" + new_message = Message(content=new_conent, instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content diff --git a/tests/metagpt/provider/test_base_gpt_api.py b/tests/metagpt/provider/test_base_gpt_api.py index 882338a01..6cfe3b02d 100644 --- a/tests/metagpt/provider/test_base_gpt_api.py +++ b/tests/metagpt/provider/test_base_gpt_api.py @@ -10,6 +10,6 @@ from metagpt.schema import Message def test_message(): - message = Message(role='user', content='wtf') - assert 'role' in message.to_dict() - assert 'user' in str(message) + message = Message(role="user", content="wtf") + assert "role" in message.to_dict() + assert "user" in str(message) diff --git a/tests/metagpt/provider/test_spark_api.py b/tests/metagpt/provider/test_spark_api.py index bfa2bf76f..3b3dd67f4 100644 --- a/tests/metagpt/provider/test_spark_api.py +++ b/tests/metagpt/provider/test_spark_api.py @@ -6,6 +6,6 @@ def test_message(): llm = SparkAPI() logger.info(llm.ask('只回答"收到了"这三个字。')) - result = llm.ask('写一篇五百字的日记') + result = llm.ask("写一篇五百字的日记") logger.info(result) assert len(result) > 100 diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 52fc4a3c1..1b02fbaa5 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -71,7 +71,7 @@ PRD = '''## 原始需求 ``` ''' -SYSTEM_DESIGN = '''## Python package name +SYSTEM_DESIGN = """## Python package name ```python "smart_search_engine" ``` @@ -149,10 +149,10 @@ sequenceDiagram S-->>SE: return summary SE-->>M: return summary ``` -''' +""" -TASKS = '''## Logic Analysis +TASKS = """## Logic Analysis 在这个项目中,所有的模块都依赖于“SearchEngine”类,这是主入口,其他的模块(Index、Ranking和Summary)都通过它交互。另外,"Index"类又依赖于"KnowledgeBase"类,因为它需要从知识库中获取数据。 @@ -181,7 +181,7 @@ task_list = [ ] ``` 这个任务列表首先定义了最基础的模块,然后是依赖这些模块的模块,最后是辅助模块。可以根据团队的能力和资源,同时开发多个任务,只要满足依赖关系。例如,在开发"search.py"之前,可以同时开发"knowledge_base.py"、"index.py"、"ranking.py"和"summary.py"。 -''' +""" TASKS_TOMATO_CLOCK = '''## Required Python third-party packages: Provided in requirements.txt format @@ -224,30 +224,30 @@ task_list = [ TASK = """smart_search_engine/knowledge_base.py""" STRS_FOR_PARSING = [ -""" + """ ## 1 ```python a ``` """, -""" + """ ##2 ```python "a" ``` """, -""" + """ ## 3 ```python a = "a" ``` """, -""" + """ ## 4 ```python a = 'a' ``` -""" +""", ] diff --git a/tests/metagpt/roles/test_engineer.py b/tests/metagpt/roles/test_engineer.py index c0c48d0b1..f44188c17 100644 --- a/tests/metagpt/roles/test_engineer.py +++ b/tests/metagpt/roles/test_engineer.py @@ -35,13 +35,13 @@ def test_parse_str(): for idx, i in enumerate(STRS_FOR_PARSING): text = CodeParser.parse_str(f"{idx+1}", i) # logger.info(text) - assert text == 'a' + assert text == "a" def test_parse_blocks(): tasks = CodeParser.parse_blocks(TASKS) logger.info(tasks.keys()) - assert 'Task list' in tasks.keys() + assert "Task list" in tasks.keys() target_list = [ diff --git a/tests/metagpt/roles/test_invoice_ocr_assistant.py b/tests/metagpt/roles/test_invoice_ocr_assistant.py index 75097e73c..c9aad93a7 100644 --- a/tests/metagpt/roles/test_invoice_ocr_assistant.py +++ b/tests/metagpt/roles/test_invoice_ocr_assistant.py @@ -9,8 +9,8 @@ from pathlib import Path -import pytest import pandas as pd +import pytest from metagpt.roles.invoice_ocr_assistant import InvoiceOCRAssistant from metagpt.schema import Message @@ -24,82 +24,39 @@ from metagpt.schema import Message "Invoicing date", Path("../../data/invoices/invoice-1.pdf"), Path("../../../data/invoice_table/invoice-1.xlsx"), - [ - { - "收款人": "小明", - "城市": "深圳市", - "总费用/元": 412.00, - "开票日期": "2023年02月03日" - } - ] + [{"收款人": "小明", "城市": "深圳市", "总费用/元": 412.00, "开票日期": "2023年02月03日"}], ), ( "Invoicing date", Path("../../data/invoices/invoice-2.png"), Path("../../../data/invoice_table/invoice-2.xlsx"), - [ - { - "收款人": "铁头", - "城市": "广州市", - "总费用/元": 898.00, - "开票日期": "2023年03月17日" - } - ] + [{"收款人": "铁头", "城市": "广州市", "总费用/元": 898.00, "开票日期": "2023年03月17日"}], ), ( "Invoicing date", Path("../../data/invoices/invoice-3.jpg"), Path("../../../data/invoice_table/invoice-3.xlsx"), - [ - { - "收款人": "夏天", - "城市": "福州市", - "总费用/元": 2462.00, - "开票日期": "2023年08月26日" - } - ] + [{"收款人": "夏天", "城市": "福州市", "总费用/元": 2462.00, "开票日期": "2023年08月26日"}], ), ( "Invoicing date", Path("../../data/invoices/invoice-4.zip"), Path("../../../data/invoice_table/invoice-4.xlsx"), [ - { - "收款人": "小明", - "城市": "深圳市", - "总费用/元": 412.00, - "开票日期": "2023年02月03日" - }, - { - "收款人": "铁头", - "城市": "广州市", - "总费用/元": 898.00, - "开票日期": "2023年03月17日" - }, - { - "收款人": "夏天", - "城市": "福州市", - "总费用/元": 2462.00, - "开票日期": "2023年08月26日" - } - ] + {"收款人": "小明", "城市": "深圳市", "总费用/元": 412.00, "开票日期": "2023年02月03日"}, + {"收款人": "铁头", "城市": "广州市", "总费用/元": 898.00, "开票日期": "2023年03月17日"}, + {"收款人": "夏天", "城市": "福州市", "总费用/元": 2462.00, "开票日期": "2023年08月26日"}, + ], ), - ] + ], ) async def test_invoice_ocr_assistant( - query: str, - invoice_path: Path, - invoice_table_path: Path, - expected_result: list[dict] + query: str, invoice_path: Path, invoice_table_path: Path, expected_result: list[dict] ): invoice_path = Path.cwd() / invoice_path role = InvoiceOCRAssistant() - await role.run(Message( - content=query, - instruct_content={"file_path": invoice_path} - )) + await role.run(Message(content=query, instruct_content={"file_path": invoice_path})) invoice_table_path = Path.cwd() / invoice_table_path df = pd.read_excel(invoice_table_path) - dict_result = df.to_dict(orient='records') + dict_result = df.to_dict(orient="records") assert dict_result == expected_result - diff --git a/tests/metagpt/roles/test_researcher.py b/tests/metagpt/roles/test_researcher.py index 01b5dae3b..dd130662d 100644 --- a/tests/metagpt/roles/test_researcher.py +++ b/tests/metagpt/roles/test_researcher.py @@ -11,10 +11,12 @@ async def mock_llm_ask(self, prompt: str, system_msgs): if "Please provide up to 2 necessary keywords" in prompt: return '["dataiku", "datarobot"]' elif "Provide up to 4 queries related to your research topic" in prompt: - return '["Dataiku machine learning platform", "DataRobot AI platform comparison", ' \ + return ( + '["Dataiku machine learning platform", "DataRobot AI platform comparison", ' '"Dataiku vs DataRobot features", "Dataiku and DataRobot use cases"]' + ) elif "sort the remaining search results" in prompt: - return '[1,2]' + return "[1,2]" elif "Not relevant." in prompt: return "Not relevant" if random() > 0.5 else prompt[-100:] elif "provide a detailed research report" in prompt: diff --git a/tests/metagpt/roles/test_tutorial_assistant.py b/tests/metagpt/roles/test_tutorial_assistant.py index 945620cfc..105f976c3 100644 --- a/tests/metagpt/roles/test_tutorial_assistant.py +++ b/tests/metagpt/roles/test_tutorial_assistant.py @@ -12,10 +12,7 @@ from metagpt.roles.tutorial_assistant import TutorialAssistant @pytest.mark.asyncio -@pytest.mark.parametrize( - ("language", "topic"), - [("Chinese", "Write a tutorial about Python")] -) +@pytest.mark.parametrize(("language", "topic"), [("Chinese", "Write a tutorial about Python")]) async def test_tutorial_assistant(language: str, topic: str): topic = "Write a tutorial about MySQL" role = TutorialAssistant(language=language) @@ -24,4 +21,4 @@ async def test_tutorial_assistant(language: str, topic: str): title = filename.split("/")[-1].split(".")[0] async with aiofiles.open(filename, mode="r") as reader: content = await reader.read() - assert content.startswith(f"# {title}") \ No newline at end of file + assert content.startswith(f"# {title}") diff --git a/tests/metagpt/roles/test_ui.py b/tests/metagpt/roles/test_ui.py index 285bff323..2d9cb85c9 100644 --- a/tests/metagpt/roles/test_ui.py +++ b/tests/metagpt/roles/test_ui.py @@ -2,9 +2,8 @@ # @Date : 2023/7/22 02:40 # @Author : stellahong (stellahong@fuzhi.ai) # -from metagpt.software_company import SoftwareCompany from metagpt.roles import ProductManager - +from metagpt.software_company import SoftwareCompany from tests.metagpt.roles.ui_role import UI diff --git a/tests/metagpt/test_gpt.py b/tests/metagpt/test_gpt.py index 89dd726a8..285e8134c 100644 --- a/tests/metagpt/test_gpt.py +++ b/tests/metagpt/test_gpt.py @@ -14,7 +14,7 @@ from metagpt.logs import logger @pytest.mark.usefixtures("llm_api") class TestGPT: def test_llm_api_ask(self, llm_api): - answer = llm_api.ask('hello chatgpt') + answer = llm_api.ask("hello chatgpt") assert len(answer) > 0 # def test_gptapi_ask_batch(self, llm_api): @@ -22,22 +22,22 @@ class TestGPT: # assert len(answer) > 0 def test_llm_api_ask_code(self, llm_api): - answer = llm_api.ask_code(['请扮演一个Google Python专家工程师,如果理解,回复明白', '写一个hello world']) + answer = llm_api.ask_code(["请扮演一个Google Python专家工程师,如果理解,回复明白", "写一个hello world"]) assert len(answer) > 0 @pytest.mark.asyncio async def test_llm_api_aask(self, llm_api): - answer = await llm_api.aask('hello chatgpt') + answer = await llm_api.aask("hello chatgpt") assert len(answer) > 0 @pytest.mark.asyncio async def test_llm_api_aask_code(self, llm_api): - answer = await llm_api.aask_code(['请扮演一个Google Python专家工程师,如果理解,回复明白', '写一个hello world']) + answer = await llm_api.aask_code(["请扮演一个Google Python专家工程师,如果理解,回复明白", "写一个hello world"]) assert len(answer) > 0 @pytest.mark.asyncio async def test_llm_api_costs(self, llm_api): - await llm_api.aask('hello chatgpt') + await llm_api.aask("hello chatgpt") costs = llm_api.get_costs() logger.info(costs) assert costs.total_cost > 0 diff --git a/tests/metagpt/test_llm.py b/tests/metagpt/test_llm.py index 11503af1d..03341212b 100644 --- a/tests/metagpt/test_llm.py +++ b/tests/metagpt/test_llm.py @@ -18,17 +18,17 @@ def llm(): @pytest.mark.asyncio async def test_llm_aask(llm): - assert len(await llm.aask('hello world')) > 0 + assert len(await llm.aask("hello world")) > 0 @pytest.mark.asyncio async def test_llm_aask_batch(llm): - assert len(await llm.aask_batch(['hi', 'write python hello world.'])) > 0 + assert len(await llm.aask_batch(["hi", "write python hello world."])) > 0 @pytest.mark.asyncio async def test_llm_acompletion(llm): - hello_msg = [{'role': 'user', 'content': 'hello'}] + hello_msg = [{"role": "user", "content": "hello"}] assert len(await llm.acompletion(hello_msg)) > 0 assert len(await llm.acompletion_batch([hello_msg])) > 0 assert len(await llm.acompletion_batch_text([hello_msg])) > 0 diff --git a/tests/metagpt/test_message.py b/tests/metagpt/test_message.py index e26f38381..ae6708943 100644 --- a/tests/metagpt/test_message.py +++ b/tests/metagpt/test_message.py @@ -11,26 +11,26 @@ from metagpt.schema import AIMessage, Message, RawMessage, SystemMessage, UserMe def test_message(): - msg = Message(role='User', content='WTF') - assert msg.to_dict()['role'] == 'User' - assert 'User' in str(msg) + msg = Message(role="User", content="WTF") + assert msg.to_dict()["role"] == "User" + assert "User" in str(msg) def test_all_messages(): - test_content = 'test_message' + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] for msg in msgs: assert msg.content == test_content def test_raw_message(): - msg = RawMessage(role='user', content='raw') - assert msg['role'] == 'user' - assert msg['content'] == 'raw' + msg = RawMessage(role="user", content="raw") + assert msg["role"] == "user" + assert msg["content"] == "raw" with pytest.raises(KeyError): - assert msg['1'] == 1, "KeyError: '1'" + assert msg["1"] == 1, "KeyError: '1'" diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 11fd804ec..22cfa58a4 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -9,6 +9,6 @@ from metagpt.roles import Role def test_role_desc(): - i = Role(profile='Sales', desc='Best Seller') - assert i.profile == 'Sales' - assert i._setting.desc == 'Best Seller' + i = Role(profile="Sales", desc="Best Seller") + assert i.profile == "Sales" + assert i._setting.desc == "Best Seller" diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 12666e0d3..c154d77e1 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -9,13 +9,13 @@ from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage def test_messages(): - test_content = 'test_message' + test_content = "test_message" msgs = [ UserMessage(test_content), SystemMessage(test_content), AIMessage(test_content), - Message(test_content, role='QA') + Message(test_content, role="QA"), ] text = str(msgs) - roles = ['user', 'system', 'assistant', 'QA'] + roles = ["user", "system", "assistant", "QA"] assert all([i in text for i in roles]) diff --git a/tests/metagpt/tools/test_code_interpreter.py b/tests/metagpt/tools/test_code_interpreter.py index 0eec3f80b..03d4ce8df 100644 --- a/tests/metagpt/tools/test_code_interpreter.py +++ b/tests/metagpt/tools/test_code_interpreter.py @@ -1,23 +1,22 @@ -import pytest -import pandas as pd from pathlib import Path -from tests.data import sales_desc, store_desc -from metagpt.tools.code_interpreter import OpenCodeInterpreter, OpenInterpreterDecorator +import pandas as pd +import pytest + from metagpt.actions import Action from metagpt.logs import logger +from metagpt.tools.code_interpreter import OpenCodeInterpreter, OpenInterpreterDecorator - -logger.add('./tests/data/test_ci.log') +logger.add("./tests/data/test_ci.log") stock = "./tests/data/baba_stock.csv" # TODO: 需要一种表格数据格式,能够支持schame管理的,标注字段类型和字段含义。 class CreateStockIndicators(Action): @OpenInterpreterDecorator(save_code=True, code_file_path="./tests/data/stock_indicators.py") - async def run(self, stock_path: str, indicators=['Simple Moving Average', 'BollingerBands']) -> pd.DataFrame: + async def run(self, stock_path: str, indicators=["Simple Moving Average", "BollingerBands"]) -> pd.DataFrame: """对stock_path中的股票数据, 使用pandas和ta计算indicators中的技术指标, 返回带有技术指标的股票数据,不需要去除空值, 不需要安装任何包; - 指标生成对应的三列: SMA, BB_upper, BB_lower + 指标生成对应的三列: SMA, BB_upper, BB_lower """ ... @@ -25,18 +24,20 @@ class CreateStockIndicators(Action): @pytest.mark.asyncio async def test_actions(): # 计算指标 - indicators = ['Simple Moving Average', 'BollingerBands'] + indicators = ["Simple Moving Average", "BollingerBands"] stocker = CreateStockIndicators() df, msg = await stocker.run(stock, indicators=indicators) assert isinstance(df, pd.DataFrame) - assert 'Close' in df.columns - assert 'Date' in df.columns + assert "Close" in df.columns + assert "Date" in df.columns # 将df保存为文件,将文件路径传入到下一个action - df_path = './tests/data/stock_indicators.csv' + df_path = "./tests/data/stock_indicators.csv" df.to_csv(df_path) assert Path(df_path).is_file() # 可视化指标结果 - figure_path = './tests/data/figure_ci.png' + figure_path = "./tests/data/figure_ci.png" ci_ploter = OpenCodeInterpreter() - ci_ploter.chat(f"使用seaborn对{df_path}中与股票布林带有关的数据列的Date, Close, SMA, BB_upper(布林带上界), BB_lower(布林带下界)进行可视化, 可视化图片保存在{figure_path}中。不需要任何指标计算,把Date列转换为日期类型。要求图片优美,BB_upper, BB_lower之间使用合适的颜色填充。") + ci_ploter.chat( + f"使用seaborn对{df_path}中与股票布林带有关的数据列的Date, Close, SMA, BB_upper(布林带上界), BB_lower(布林带下界)进行可视化, 可视化图片保存在{figure_path}中。不需要任何指标计算,把Date列转换为日期类型。要求图片优美,BB_upper, BB_lower之间使用合适的颜色填充。" + ) assert Path(figure_path).is_file() diff --git a/tests/metagpt/tools/test_prompt_generator.py b/tests/metagpt/tools/test_prompt_generator.py index d2e870c6d..ddbd2c43b 100644 --- a/tests/metagpt/tools/test_prompt_generator.py +++ b/tests/metagpt/tools/test_prompt_generator.py @@ -20,8 +20,9 @@ from metagpt.tools.prompt_writer import ( @pytest.mark.usefixtures("llm_api") def test_gpt_prompt_generator(llm_api): generator = GPTPromptGenerator() - example = "商品名称:WonderLab 新肌果味代餐奶昔 小胖瓶 胶原蛋白升级版 饱腹代餐粉6瓶 75g/瓶(6瓶/盒) 店铺名称:金力宁食品专营店 " \ - "品牌:WonderLab 保质期:1年 产地:中国 净含量:450g" + example = ( + "商品名称:WonderLab 新肌果味代餐奶昔 小胖瓶 胶原蛋白升级版 饱腹代餐粉6瓶 75g/瓶(6瓶/盒) 店铺名称:金力宁食品专营店 " "品牌:WonderLab 保质期:1年 产地:中国 净含量:450g" + ) results = llm_api.ask_batch(generator.gen(example)) logger.info(results) @@ -46,7 +47,7 @@ def test_enron_template(llm_api): results = template.gen(subj) assert len(results) > 0 - assert any("Write an email with the subject \"Meeting Agenda\"." in r for r in results) + assert any('Write an email with the subject "Meeting Agenda".' in r for r in results) def test_beagec_template(): @@ -54,5 +55,6 @@ def test_beagec_template(): results = template.gen() assert len(results) > 0 - assert any("Edit and revise this document to improve its grammar, vocabulary, spelling, and style." - in r for r in results) + assert any( + "Edit and revise this document to improve its grammar, vocabulary, spelling, and style." in r for r in results + ) diff --git a/tests/metagpt/tools/test_sd_tool.py b/tests/metagpt/tools/test_sd_tool.py index 77e53c7dc..4edd8fb3b 100644 --- a/tests/metagpt/tools/test_sd_tool.py +++ b/tests/metagpt/tools/test_sd_tool.py @@ -4,7 +4,7 @@ # import os -from metagpt.tools.sd_engine import SDEngine, WORKSPACE_ROOT +from metagpt.tools.sd_engine import WORKSPACE_ROOT, SDEngine def test_sd_engine_init(): diff --git a/tests/metagpt/tools/test_search_engine.py b/tests/metagpt/tools/test_search_engine.py index a7fe063a6..25bce124a 100644 --- a/tests/metagpt/tools/test_search_engine.py +++ b/tests/metagpt/tools/test_search_engine.py @@ -16,7 +16,9 @@ from metagpt.tools.search_engine import SearchEngine class MockSearchEnine: async def run(self, query: str, max_results: int = 8, as_string: bool = True) -> str | list[dict[str, str]]: - rets = [{"url": "https://metagpt.com/mock/{i}", "title": query, "snippet": query * i} for i in range(max_results)] + rets = [ + {"url": "https://metagpt.com/mock/{i}", "title": query, "snippet": query * i} for i in range(max_results) + ] return "\n".join(rets) if as_string else rets @@ -34,10 +36,14 @@ class MockSearchEnine: (SearchEngineType.DUCK_DUCK_GO, None, 6, False), (SearchEngineType.CUSTOM_ENGINE, MockSearchEnine().run, 8, False), (SearchEngineType.CUSTOM_ENGINE, MockSearchEnine().run, 6, False), - ], ) -async def test_search_engine(search_engine_typpe, run_func, max_results, as_string, ): +async def test_search_engine( + search_engine_typpe, + run_func, + max_results, + as_string, +): search_engine = SearchEngine(search_engine_typpe, run_func) rsp = await search_engine.run("metagpt", max_results=max_results, as_string=as_string) logger.info(rsp) diff --git a/tests/metagpt/tools/test_search_engine_meilisearch.py b/tests/metagpt/tools/test_search_engine_meilisearch.py index 8d2bb6494..d5f7d162b 100644 --- a/tests/metagpt/tools/test_search_engine_meilisearch.py +++ b/tests/metagpt/tools/test_search_engine_meilisearch.py @@ -13,7 +13,7 @@ import pytest from metagpt.logs import logger from metagpt.tools.search_engine_meilisearch import DataSource, MeilisearchEngine -MASTER_KEY = '116Qavl2qpCYNEJNv5-e0RC9kncev1nr1gt7ybEGVLk' +MASTER_KEY = "116Qavl2qpCYNEJNv5-e0RC9kncev1nr1gt7ybEGVLk" @pytest.fixture() @@ -29,7 +29,7 @@ def test_meilisearch(search_engine_server): search_engine = MeilisearchEngine(url="http://localhost:7700", token=MASTER_KEY) # 假设有一个名为"books"的数据源,包含要添加的文档库 - books_data_source = DataSource(name='books', url='https://example.com/books') + books_data_source = DataSource(name="books", url="https://example.com/books") # 假设有一个名为"documents"的文档库,包含要添加的文档 documents = [ @@ -43,4 +43,4 @@ def test_meilisearch(search_engine_server): # 添加文档库到搜索引擎 search_engine.add_documents(books_data_source, documents) - logger.info(search_engine.search('Book 1')) + logger.info(search_engine.search("Book 1")) diff --git a/tests/metagpt/tools/test_summarize.py b/tests/metagpt/tools/test_summarize.py index cf616c144..6a372defb 100644 --- a/tests/metagpt/tools/test_summarize.py +++ b/tests/metagpt/tools/test_summarize.py @@ -20,7 +20,6 @@ CASES = [ 1. 请根据上下文,对用户搜索请求进行总结性回答,不要包括与请求无关的文本 2. 以 [正文](引用链接) markdown形式在正文中**自然标注**~5个文本(如商品词或类似文本段),以便跳转 3. 回复优雅、清晰,**绝不重复文本**,行文流畅,长度居中""", - """# 上下文 [{'title': '去厦门 有哪些推荐的美食? - 知乎', 'href': 'https://www.zhihu.com/question/286901854', 'body': '知乎,中文互联网高质量的问答社区和创作者聚集的原创内容平台,于 2011 年 1 月正式上线,以「让人们更好的分享知识、经验和见解,找到自己的解答」为品牌使命。知乎凭借认真、专业、友善的社区氛围、独特的产品机制以及结构化和易获得的优质内容,聚集了中文互联网科技、商业、影视 ...'}, {'title': '厦门到底有哪些真正值得吃的美食? - 知乎', 'href': 'https://www.zhihu.com/question/38012322', 'body': '有几个特色菜在别处不太能吃到,值得一试~常点的有西多士、沙茶肉串、咕老肉(个人认为还是良山排档的更炉火纯青~),因为爱吃芋泥,每次还会点一个芋泥鸭~人均50元左右. 潮福城. 厦门这两年经营港式茶点的店越来越多,但是最经典的还是潮福城的茶点 ...'}, {'title': '超全厦门美食攻略,好吃不贵不踩雷 - 知乎 - 知乎专栏', 'href': 'https://zhuanlan.zhihu.com/p/347055615', 'body': '厦门老字号店铺,味道卫生都有保障,喜欢吃芒果的,不要错过芒果牛奶绵绵冰. 285蚝味馆 70/人. 上过《舌尖上的中国》味道不用多说,想吃地道的海鲜烧烤就来这里. 堂宴.老厦门私房菜 80/人. 非常多的明星打卡过,上过《十二道锋味》,吃厦门传统菜的好去处 ...'}, {'title': '福建名小吃||寻味厦门,十大特色名小吃,你都吃过哪几样? - 知乎', 'href': 'https://zhuanlan.zhihu.com/p/375781836', 'body': '第一期,分享厦门的特色美食。 厦门是一个风景旅游城市,许多人来到厦门,除了游览厦门独特的风景之外,最难忘的应该是厦门的特色小吃。厦门小吃多种多样,有到厦门必吃的沙茶面、米线糊、蚵仔煎、土笋冻等非常之多。那么,厦门的名小吃有哪些呢?'}, {'title': '大家如果去厦门旅游的话,好吃的有很多,但... 来自庄时利和 - 微博', 'href': 'https://weibo.com/1728715190/MEAwzscRT', 'body': '大家如果去厦门旅游的话,好吃的有很多,但如果只选一样的话,我个人会选择莲花煎蟹。 靠海吃海,吃蟹对于闽南人来说是很平常的一件事。 厦门传统的做法多是清蒸或水煮,上世纪八十年代有一同安人在厦门的莲花公园旁,摆摊做起了煎蟹的生意。'}, {'title': '厦门美食,厦门美食攻略,厦门旅游美食攻略 - 马蜂窝', 'href': 'https://www.mafengwo.cn/cy/10132/gonglve.html', 'body': '醉壹号海鲜大排档 (厦门美食地标店) No.3. 哆啦Eanny 的最新点评:. 环境 挺复古的闽南风情,花砖地板,一楼有海鲜自己点菜,二楼室内位置,三楼露天位置,环境挺不错的。. 苦螺汤,看起来挺清的,螺肉吃起来很脆。. 姜... 5.0 分. 482 条用户点评.'}, {'title': '厦门超强中山路小吃合集,29家本地人推荐的正宗美食 - 马蜂窝', 'href': 'https://www.mafengwo.cn/gonglve/ziyouxing/176485.html', 'body': '莲欢海蛎煎. 提到厦门就想到海蛎煎,而这家位于中山路局口街的莲欢海蛎煎是实打实的好吃!. ·局口街老巷之中,全室外环境,吃的就是这种感觉。. ·取名"莲欢",是希望妻子每天开心。. 新鲜的食材,实在的用料,这样的用心也定能讨食客欢心。. ·海蛎又 ...'}, {'title': '厦门市 10 大餐厅- Tripadvisor', 'href': 'https://cn.tripadvisor.com/Restaurants-g297407-Xiamen_Fujian.html', 'body': '厦门市餐厅:在Tripadvisor查看中国厦门市餐厅的点评,并以价格、地点及更多选项进行搜索。 ... "牛排太好吃了啊啊啊" ... "厦门地区最老品牌最有口碑的潮州菜餐厅" ...'}, {'title': '#福建10条美食街简直不要太好吃#每到一... 来自新浪厦门 - 微博', 'href': 'https://weibo.com/1740522895/MF1lY7W4n', 'body': '福建的这10条美食街,你一定不能错过!福州师大学生街、福州达明路美食街、厦门八市、漳州古城老街、宁德老南门电影院美食集市、龙岩中山路美食街、三明龙岗夜市、莆田金鼎夜市、莆田玉湖夜市、南平嘉禾美食街。世间万事皆难,唯有美食可以治愈一切。'}, {'title': '厦门这50家餐厅最值得吃 - 腾讯新闻', 'href': 'https://new.qq.com/rain/a/20200114A09HJT00', 'body': '没有什么事是一顿辣解决不了的! 创意辣、川湘辣、温柔辣、异域辣,芙蓉涧的菜能把辣椒玩出花来! ... 早在2005年,这家老牌的东南亚餐厅就开在厦门莲花了,在许多老厦门的心中,都觉得这里有全厦门最好吃的咖喱呢。 ...'}, {'title': '好听的美食?又好听又好吃的食物有什么? - 哔哩哔哩', 'href': 'https://www.bilibili.com/read/cv23430069/', 'body': '专栏 / 好听的美食?又好听又好吃的食物有什么? 又好听又好吃的食物有什么? 2023-05-02 18:01 --阅读 · --喜欢 · --评论'}] @@ -31,7 +30,7 @@ CASES = [ 你是专业管家团队的一员,会给出有帮助的建议 1. 请根据上下文,对用户搜索请求进行总结性回答,不要包括与请求无关的文本 2. 以 [正文](引用链接) markdown形式在正文中**自然标注**3-5个文本(如商品词或类似文本段),以便跳转 -3. 回复优雅、清晰,**绝不重复文本**,行文流畅,长度居中""" +3. 回复优雅、清晰,**绝不重复文本**,行文流畅,长度居中""", ] diff --git a/tests/metagpt/tools/test_translate.py b/tests/metagpt/tools/test_translate.py index 47a9034a5..024bda3ca 100644 --- a/tests/metagpt/tools/test_translate.py +++ b/tests/metagpt/tools/test_translate.py @@ -16,7 +16,7 @@ from metagpt.tools.translator import Translator def test_translate(llm_api): poetries = [ ("Let life be beautiful like summer flowers", "花"), - ("The ancient Chinese poetries are all songs.", "中国") + ("The ancient Chinese poetries are all songs.", "中国"), ] for i, j in poetries: prompt = Translator.translate_prompt(i) diff --git a/tests/metagpt/tools/test_ut_generator.py b/tests/metagpt/tools/test_ut_generator.py index 6f29999d4..2ae94885f 100644 --- a/tests/metagpt/tools/test_ut_generator.py +++ b/tests/metagpt/tools/test_ut_generator.py @@ -16,8 +16,12 @@ class TestUTWriter: tags = ["测试"] # "智能合同导入", "律师审查", "ai合同审查", "草拟合同&律师在线审查", "合同审批", "履约管理", "签约公司"] # 这里在文件中手动加入了两个测试标签的API - utg = UTGenerator(swagger_file=swagger_file, ut_py_path=UT_PY_PATH, questions_path=API_QUESTIONS_PATH, - template_prefix=YFT_PROMPT_PREFIX) + utg = UTGenerator( + swagger_file=swagger_file, + ut_py_path=UT_PY_PATH, + questions_path=API_QUESTIONS_PATH, + template_prefix=YFT_PROMPT_PREFIX, + ) ret = utg.generate_ut(include_tags=tags) # 后续加入对文件生成内容与数量的检验 assert ret diff --git a/tests/metagpt/utils/test_code_parser.py b/tests/metagpt/utils/test_code_parser.py index 707b558e1..6b7349cd9 100644 --- a/tests/metagpt/utils/test_code_parser.py +++ b/tests/metagpt/utils/test_code_parser.py @@ -131,10 +131,10 @@ class TestCodeParser: def test_parse_file_list(self, parser, text): result = parser.parse_file_list("Task list", text) print(result) - assert result == ['task1', 'task2'] + assert result == ["task1", "task2"] -if __name__ == '__main__': +if __name__ == "__main__": t = TestCodeParser() t.test_parse_file_list(CodeParser(), t_text) # TestCodeParser.test_parse_file_list() diff --git a/tests/metagpt/utils/test_common.py b/tests/metagpt/utils/test_common.py index ec4443175..d3837ca8f 100644 --- a/tests/metagpt/utils/test_common.py +++ b/tests/metagpt/utils/test_common.py @@ -16,12 +16,12 @@ from metagpt.const import get_project_root class TestGetProjectRoot: def change_etc_dir(self): # current_directory = Path.cwd() - abs_root = '/etc' + abs_root = "/etc" os.chdir(abs_root) def test_get_project_root(self): project_root = get_project_root() - assert project_root.name == 'metagpt' + assert project_root.name == "metagpt" def test_get_root_exception(self): with pytest.raises(Exception) as exc_info: diff --git a/tests/metagpt/utils/test_config.py b/tests/metagpt/utils/test_config.py index 558a4e5a4..b68a535f9 100644 --- a/tests/metagpt/utils/test_config.py +++ b/tests/metagpt/utils/test_config.py @@ -20,12 +20,12 @@ def test_config_class_is_singleton(): def test_config_class_get_key_exception(): with pytest.raises(Exception) as exc_info: config = Config() - config.get('wtf') + config.get("wtf") assert str(exc_info.value) == "Key 'wtf' not found in environment variables or in the YAML file" def test_config_yaml_file_not_exists(): - config = Config('wtf.yaml') + config = Config("wtf.yaml") with pytest.raises(Exception) as exc_info: - config.get('OPENAI_BASE_URL') + config.get("OPENAI_BASE_URL") assert str(exc_info.value) == "Key 'OPENAI_BASE_URL' not found in environment variables or in the YAML file" diff --git a/tests/metagpt/utils/test_custom_aio_session.py b/tests/metagpt/utils/test_custom_aio_session.py index 3a8a7bf7e..e2876e4b8 100644 --- a/tests/metagpt/utils/test_custom_aio_session.py +++ b/tests/metagpt/utils/test_custom_aio_session.py @@ -10,12 +10,12 @@ from metagpt.provider.openai_api import OpenAIGPTAPI async def try_hello(api): - batch = [[{'role': 'user', 'content': 'hello'}]] + batch = [[{"role": "user", "content": "hello"}]] results = await api.acompletion_batch_text(batch) return results async def aask_batch(api: OpenAIGPTAPI): - results = await api.aask_batch(['hi', 'write python hello world.']) + results = await api.aask_batch(["hi", "write python hello world."]) logger.info(results) return results diff --git a/tests/metagpt/utils/test_file.py b/tests/metagpt/utils/test_file.py index b30e6be93..83e317213 100644 --- a/tests/metagpt/utils/test_file.py +++ b/tests/metagpt/utils/test_file.py @@ -15,12 +15,11 @@ from metagpt.utils.file import File @pytest.mark.asyncio @pytest.mark.parametrize( ("root_path", "filename", "content"), - [(Path("/code/MetaGPT/data/tutorial_docx/2023-09-07_17-05-20"), "test.md", "Hello World!")] + [(Path("/code/MetaGPT/data/tutorial_docx/2023-09-07_17-05-20"), "test.md", "Hello World!")], ) async def test_write_and_read_file(root_path: Path, filename: str, content: bytes): - full_file_name = await File.write(root_path=root_path, filename=filename, content=content.encode('utf-8')) + full_file_name = await File.write(root_path=root_path, filename=filename, content=content.encode("utf-8")) assert isinstance(full_file_name, Path) assert root_path / filename == full_file_name file_data = await File.read(full_file_name) assert file_data.decode("utf-8") == content - diff --git a/tests/metagpt/utils/test_output_parser.py b/tests/metagpt/utils/test_output_parser.py index 4e362f9f7..7a3aedbe8 100644 --- a/tests/metagpt/utils/test_output_parser.py +++ b/tests/metagpt/utils/test_output_parser.py @@ -14,17 +14,17 @@ from metagpt.utils.common import OutputParser def test_parse_blocks(): test_text = "##block1\nThis is block 1.\n##block2\nThis is block 2." - expected_result = {'block1': 'This is block 1.', 'block2': 'This is block 2.'} + expected_result = {"block1": "This is block 1.", "block2": "This is block 2."} assert OutputParser.parse_blocks(test_text) == expected_result def test_parse_code(): test_text = "```python\nprint('Hello, world!')```" expected_result = "print('Hello, world!')" - assert OutputParser.parse_code(test_text, 'python') == expected_result + assert OutputParser.parse_code(test_text, "python") == expected_result with pytest.raises(Exception): - OutputParser.parse_code(test_text, 'java') + OutputParser.parse_code(test_text, "java") def test_parse_python_code(): @@ -45,13 +45,13 @@ def test_parse_python_code(): def test_parse_str(): test_text = "name = 'Alice'" - expected_result = 'Alice' + expected_result = "Alice" assert OutputParser.parse_str(test_text) == expected_result def test_parse_file_list(): test_text = "files=['file1', 'file2', 'file3']" - expected_result = ['file1', 'file2', 'file3'] + expected_result = ["file1", "file2", "file3"] assert OutputParser.parse_file_list(test_text) == expected_result with pytest.raises(Exception): @@ -60,7 +60,7 @@ def test_parse_file_list(): def test_parse_data(): test_data = "##block1\n```python\nprint('Hello, world!')\n```\n##block2\nfiles=['file1', 'file2', 'file3']" - expected_result = {'block1': "print('Hello, world!')", 'block2': ['file1', 'file2', 'file3']} + expected_result = {"block1": "print('Hello, world!')", "block2": ["file1", "file2", "file3"]} assert OutputParser.parse_data(test_data) == expected_result @@ -103,9 +103,11 @@ def test_parse_data(): None, Exception, ), - ] + ], ) -def test_extract_struct(text: str, data_type: Union[type(list), type(dict)], parsed_data: Union[list, dict], expected_exception): +def test_extract_struct( + text: str, data_type: Union[type(list), type(dict)], parsed_data: Union[list, dict], expected_exception +): def case(): resp = OutputParser.extract_struct(text, data_type) assert resp == parsed_data @@ -117,7 +119,7 @@ def test_extract_struct(text: str, data_type: Union[type(list), type(dict)], par case() -if __name__ == '__main__': +if __name__ == "__main__": t_text = ''' ## Required Python third-party packages ```python @@ -216,7 +218,7 @@ We need clarification on how the high score should be stored. Should it persist "Requirement Pool": (List[Tuple[str, str]], ...), "Anything UNCLEAR": (str, ...), } - t_text1 = '''## Original Requirements: + t_text1 = """## Original Requirements: The boss wants to create a web-based version of the game "Fly Bird". @@ -284,7 +286,7 @@ The product should be a web-based version of the game "Fly Bird" that is engagin ## Anything UNCLEAR: There are no unclear points. - ''' + """ d = OutputParser.parse_data_with_mapping(t_text1, OUTPUT_MAPPING) import json diff --git a/tests/metagpt/utils/test_parse_html.py b/tests/metagpt/utils/test_parse_html.py index 42be416a6..dd15bd80b 100644 --- a/tests/metagpt/utils/test_parse_html.py +++ b/tests/metagpt/utils/test_parse_html.py @@ -52,9 +52,11 @@ PAGE = """ """ -CONTENT = 'This is a HeadingThis is a paragraph witha linkand someemphasizedtext.Item 1Item 2Item 3Numbered Item 1Numbered '\ -'Item 2Numbered Item 3Header 1Header 2Row 1, Cell 1Row 1, Cell 2Row 2, Cell 1Row 2, Cell 2Name:Email:SubmitThis is a div '\ -'with a class "box".a link' +CONTENT = ( + "This is a HeadingThis is a paragraph witha linkand someemphasizedtext.Item 1Item 2Item 3Numbered Item 1Numbered " + "Item 2Numbered Item 3Header 1Header 2Row 1, Cell 1Row 1, Cell 2Row 2, Cell 1Row 2, Cell 2Name:Email:SubmitThis is a div " + 'with a class "box".a link' +) def test_web_page(): diff --git a/tests/metagpt/utils/test_pycst.py b/tests/metagpt/utils/test_pycst.py index 07352eac2..9cf876611 100644 --- a/tests/metagpt/utils/test_pycst.py +++ b/tests/metagpt/utils/test_pycst.py @@ -1,6 +1,6 @@ from metagpt.utils import pycst -code = ''' +code = """ #!/usr/bin/env python # -*- coding: utf-8 -*- from typing import overload @@ -24,7 +24,7 @@ class Person: def greet(self): return f"Hello, my name is {self.name} and I am {self.age} years old." -''' +""" documented_code = ''' """ diff --git a/tests/metagpt/utils/test_text.py b/tests/metagpt/utils/test_text.py index 0caf8abaa..7003c7767 100644 --- a/tests/metagpt/utils/test_text.py +++ b/tests/metagpt/utils/test_text.py @@ -29,7 +29,7 @@ def _paragraphs(n): (_msgs(), "gpt-4", "Hello," * 1000, 2000, 2), (_msgs(), "gpt-4-32k", "System", 4000, 14), (_msgs(), "gpt-4-32k", "Hello," * 2000, 4000, 12), - ] + ], ) def test_reduce_message_length(msgs, model_name, system_text, reserved, expected): assert len(reduce_message_length(msgs, model_name, system_text, reserved)) / (len("Hello,")) / 1000 == expected @@ -42,7 +42,7 @@ def test_reduce_message_length(msgs, model_name, system_text, reserved, expected (" ".join("Hello World." for _ in range(1000)), "Prompt: {}", "gpt-3.5-turbo-16k", "System", 3000, 1), (" ".join("Hello World." for _ in range(4000)), "Prompt: {}", "gpt-4", "System", 2000, 2), (" ".join("Hello World." for _ in range(8000)), "Prompt: {}", "gpt-4-32k", "System", 4000, 1), - ] + ], ) def test_generate_prompt_chunk(text, prompt_template, model_name, system_text, reserved, expected): ret = list(generate_prompt_chunk(text, prompt_template, model_name, system_text, reserved)) @@ -58,7 +58,7 @@ def test_generate_prompt_chunk(text, prompt_template, model_name, system_text, r ("......", ".", 2, ["...", "..."]), ("......", ".", 3, ["..", "..", ".."]), (".......", ".", 2, ["....", "..."]), - ] + ], ) def test_split_paragraph(paragraph, sep, count, expected): ret = split_paragraph(paragraph, sep, count) @@ -71,7 +71,7 @@ def test_split_paragraph(paragraph, sep, count, expected): ("Hello\\nWorld", "Hello\nWorld"), ("Hello\\tWorld", "Hello\tWorld"), ("Hello\\u0020World", "Hello World"), - ] + ], ) def test_decode_unicode_escape(text, expected): assert decode_unicode_escape(text) == expected From 2bf8ef8c6ad18808447b827b6699e89650d7170c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 17:08:00 +0800 Subject: [PATCH 132/413] feat: RFC 135 --- metagpt/actions/design_api.py | 47 +++++++- metagpt/actions/prepare_documents.py | 38 +++++-- metagpt/actions/write_prd.py | 38 ++++++- metagpt/config.py | 3 +- metagpt/const.py | 5 + metagpt/environment.py | 9 +- metagpt/roles/product_manager.py | 7 +- metagpt/schema.py | 40 ++++++- metagpt/utils/dependency_file.py | 83 ++++++++++++++ metagpt/utils/file_repository.py | 116 ++++++++++++-------- metagpt/utils/git_repository.py | 15 ++- requirements.txt | 2 +- startup.py | 6 + tests/metagpt/utils/test_dependency_file.py | 64 +++++++++++ tests/metagpt/utils/test_file_repository.py | 10 +- tests/metagpt/utils/test_git_repository.py | 15 +++ 16 files changed, 416 insertions(+), 82 deletions(-) create mode 100644 metagpt/utils/dependency_file.py create mode 100644 tests/metagpt/utils/test_dependency_file.py diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 75df8b909..65d53364b 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -11,8 +11,9 @@ from typing import List from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import WORKSPACE_ROOT +from metagpt.const import PRDS_FILE_REPO, SYS_DESIGN_FILE_REPO, WORKSPACE_ROOT from metagpt.logs import logger +from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template from metagpt.utils.json_to_markdown import json_to_markdown @@ -202,7 +203,44 @@ class WriteDesign(Action): await self._save_prd(docs_path, resources_path, context) await self._save_system_design(docs_path, resources_path, system_design) - async def run(self, context, format=CONFIG.prompt_format): + async def run(self, with_messages, format=CONFIG.prompt_format): + # 通过git diff来识别docs/prds下哪些PRD文档发生了变动 + prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) + changed_prds = prds_file_repo.changed_files + # 通过git diff来识别docs/system_designs下那些设计文档发生了变动; + system_design_file_repo = CONFIG.git_repo.new_file_repository(SYS_DESIGN_FILE_REPO) + changed_system_designs = system_design_file_repo.changed_files + + # 对于那些发生变动的PRD和设计文档,重新生成设计内容; + changed_files = Documents() + for filename in changed_prds.keys(): + prd = await prds_file_repo.get(filename) + old_system_design_doc = await system_design_file_repo.get(filename) + if not old_system_design_doc: + system_design = await self._run(context=prd.content) + doc = Document( + root_path=SYS_DESIGN_FILE_REPO, filename=filename, content=system_design.instruct_content.json() + ) + else: + doc = await self._merge(prd_doc=prd, system_design_doc=old_system_design_doc) + await system_design_file_repo.save( + filename=filename, content=doc.content, dependencies={prd.root_relative_path} + ) + changed_files.docs[filename] = doc + + for filename in changed_system_designs.keys(): + if filename in changed_files.docs: + continue + prd_doc = await prds_file_repo.get(filename=filename) + old_system_design_doc = await system_design_file_repo.get(filename) + new_system_design_doc = await self._merge(prd_doc, old_system_design_doc) + await system_design_file_repo.save(filename=filename, content=new_system_design_doc.content) + changed_files.docs[filename] = new_system_design_doc + + # 等docs/system_designs/下所有文件都处理完才发publish message,给后续做全局优化留空间。 + return ActionOutput(content=changed_files.json(), instruct_content=changed_files) + + async def _run(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) # system_design = await self._aask(prompt) @@ -213,5 +251,8 @@ class WriteDesign(Action): "Python package name", system_design.instruct_content.dict()["Python package name"].strip().strip("'").strip('"'), ) - await self._save(context, system_design) + # await self._save(context, system_design) return system_design + + async def _merge(self, prd_doc, system_design_doc): + return system_design_doc diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index b0185996b..c9b60ff27 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -7,19 +7,37 @@ @Desc: PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt. RFC 135 2.2.3.5.1. """ -from metagpt.actions import Action + +from pathlib import Path + +from metagpt.actions import Action, ActionOutput +from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME, WORKSPACE_ROOT +from metagpt.schema import Document +from metagpt.utils.file_repository import FileRepository +from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, with_message, **kwargs): - parent = self.context.get("parent") - if not parent: - raise ValueError("Invalid owner") - env = parent.get_env() - if env.git_repository: - return - env.git_repository = GitRepository() - env.git_repository.open(WORKS) + async def run(self, with_messages, **kwargs): + if CONFIG.git_repo: + docs_repo = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) + doc = await docs_repo.get(REQUIREMENT_FILENAME) + return ActionOutput(content=doc.json(exclue="content"), instruct_content=doc) + + # Create and initialize the workspace folder, initialize the Git environment. + CONFIG.git_repo = GitRepository() + workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_file_name() + CONFIG.git_repo.open(local_path=workdir, auto_init=True) + + # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. + docs_file_repository = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) + doc = Document(root_path=DOCS_FILE_REPO, filename=REQUIREMENT_FILENAME, content=with_messages[0].content) + await docs_file_repository.save(REQUIREMENT_FILENAME, content=doc.content) + + # Send a Message notification to the WritePRD action, instructing it to process requirements using + # `docs/requirement.txt` and `docs/prds/`. + return ActionOutput(content=doc.content, instruct_content=doc) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bd04ca79e..a16d1ec06 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -10,7 +10,10 @@ from typing import List from metagpt.actions import Action, ActionOutput from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, PRDS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.logs import logger +from metagpt.schema import Document, Documents +from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template templates = { @@ -222,7 +225,34 @@ class WritePRD(Action): def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: + async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: + # 判断哪些需求文档需要重写:调LLM判断新增需求与prd是否相关,若相关就rewrite prd + docs_file_repo = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) + requirement_doc = await docs_file_repo.get(REQUIREMENT_FILENAME) + prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) + prd_docs = await prds_file_repo.get_all() + change_files = Documents() + for prd_doc in prd_docs: + if await self._is_relative_to(requirement_doc, prd_doc): + prd_doc = await self._merge(requirement_doc, prd_doc) + await prds_file_repo.save(filename=prd_doc.filename, content=prd_doc.content) + change_files.docs[prd_doc.filename] = prd_doc + # 如果没有任何PRD,就使用docs/requirement.txt生成一个prd + if not change_files.docs: + prd = await self._run_new_requirement( + requirements=[requirement_doc.content], format=format, *args, **kwargs + ) + doc = Document( + root_path=PRDS_FILE_REPO, + filename=FileRepository.new_file_name() + ".json", + content=prd.instruct_content.json(), + ) + await prds_file_repo.save(filename=doc.filename, content=doc.content) + change_files.docs[doc.filename] = doc + # 等docs/prds/下所有文件都与新增需求对比完后,再触发publish message让工作流跳转到下一环节。如此设计是为了给后续做全局优化留空间。 + return ActionOutput(content=change_files.json(), instruct_content=change_files) + + async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: sas = SearchAndSummarize() # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) rsp = "" @@ -239,3 +269,9 @@ class WritePRD(Action): # prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) return prd + + async def _is_relative_to(self, doc1, doc2) -> bool: + return False + + async def _merge(self, doc1, doc2) -> Document: + pass diff --git a/metagpt/config.py b/metagpt/config.py index 27455d38d..51eed4fb8 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -46,7 +46,7 @@ class Config(metaclass=Singleton): self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") if (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) and ( - not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key + not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key ): raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY first") self.openai_api_base = self._get("OPENAI_API_BASE") @@ -93,6 +93,7 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.prompt_format = self._get("PROMPT_FORMAT", "markdown") + self.git_repo = None def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" diff --git a/metagpt/const.py b/metagpt/const.py index fa0ccc536..fc1c47b5b 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -49,3 +49,8 @@ MESSAGE_ROUTE_TO = "send_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" MESSAGE_ROUTE_TO_ALL = "" + +REQUIREMENT_FILENAME = "requirement.txt" +DOCS_FILE_REPO = "docs" +PRDS_FILE_REPO = "docs/prds" +SYS_DESIGN_FILE_REPO = "docs/system_design" diff --git a/metagpt/environment.py b/metagpt/environment.py index df93a818b..b3c296dac 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,7 +12,7 @@ functionality is to be consolidated into the `Environment` class. """ import asyncio -from typing import Iterable, Optional, Set +from typing import Iterable, Set from pydantic import BaseModel, Field @@ -20,7 +20,6 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import is_subscribed -from metagpt.utils.git_repository import GitRepository class Environment(BaseModel): @@ -32,7 +31,6 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) consumers: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug - git_repository: Optional[GitRepository] = None class Config: arbitrary_types_allowed = True @@ -113,8 +111,3 @@ class Environment(BaseModel): def set_subscription(self, obj, tags): """Set the labels for message to be consumed by the object""" self.consumers[obj] = tags - - def dict(self, *args, **kwargs): - """Generate a dictionary representation of the model, optionally specifying which fields to include or - exclude.""" - return super(Environment, self).dict(exclude={"git_repository"}) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index c10aba6d1..81577ec2c 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -7,6 +7,7 @@ """ from metagpt.actions import BossRequirement, WritePRD from metagpt.actions.prepare_documents import PrepareDocuments +from metagpt.config import CONFIG from metagpt.roles import Role @@ -38,12 +39,12 @@ class ProductManager(Role): constraints (str): Constraints or limitations for the product manager. """ super().__init__(name, profile, goal, constraints) - self._init_actions([PrepareDocuments(context={"parent": self}), WritePRD]) - self._watch([BossRequirement]) + self._init_actions([PrepareDocuments, WritePRD]) + self._watch([BossRequirement, PrepareDocuments]) async def _think(self) -> None: """Decide what to do""" - if self._rc.env.git_repository: + if CONFIG.git_repo: self._set_state(1) else: self._set_state(0) diff --git a/metagpt/schema.py b/metagpt/schema.py index 82a0117ef..674091e4c 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -6,14 +6,16 @@ @File : schema.py @Modified By: mashenquan, 2023-10-31. According to Chapter 2.2.1 of RFC 116: Replanned the distribution of responsibilities and functional positioning of `Message` class attributes. +@Modified By: mashenquan, 2023/11/22. Add `Document` and `Documents` for `FileRepository` in Section 2.2.3.4 of RFC 135. """ from __future__ import annotations import asyncio import json +import os.path from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError -from typing import List, Set, TypedDict +from typing import Dict, List, Optional, Set, TypedDict from pydantic import BaseModel, Field @@ -32,6 +34,42 @@ class RawMessage(TypedDict): role: str +class Document(BaseModel): + """ + Represents a document. + """ + + root_path: str + filename: str + content: Optional[str] = None + + def get_meta(self) -> Document: + """Get metadata of the document. + + :return: A new Document instance with the same root path and filename. + """ + + return Document(root_path=self.root_path, filename=self.filename) + + @property + def root_relative_path(self): + """Get relative path from root of git repository. + + :return: relative path from root of git repository. + """ + return os.path.join(self.root_path, self.filename) + + +class Documents(BaseModel): + """A class representing a collection of documents. + + Attributes: + docs (Dict[str, Document]): A dictionary mapping document names to Document instances. + """ + + docs: Dict[str, Document] = Field(default_factory=dict) + + class Message(BaseModel): """list[: ]""" diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py new file mode 100644 index 000000000..429027c7a --- /dev/null +++ b/metagpt/utils/dependency_file.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/22 +@Author : mashenquan +@File : dependency_file.py +@Desc: Implementation of the dependency file described in Section 2.2.3.2 of RFC 135. +""" +from __future__ import annotations + +import json +from pathlib import Path +from typing import Set + +import aiofiles + +from metagpt.logs import logger + + +class DependencyFile: + def __init__(self, workdir: Path | str): + self._dependencies = {} + self._filename = Path(workdir) / ".dependencies.json" + + async def load(self): + if not self._filename.exists(): + return + try: + async with aiofiles.open(str(self._filename), mode="r") as reader: + data = await reader.read() + self._dependencies = json.loads(data) + except Exception as e: + logger.error(f"Failed to load {str(self._filename)}, error:{e}") + + async def save(self): + try: + data = json.dumps(self._dependencies) + async with aiofiles.open(str(self._filename), mode="w") as writer: + await writer.write(data) + except Exception as e: + logger.error(f"Failed to save {str(self._filename)}, error:{e}") + + async def update(self, filename: Path | str, dependencies: Set[Path | str], persist=True): + if persist: + await self.load() + + root = self._filename.parent + try: + key = Path(filename).relative_to(root) + except ValueError: + key = filename + + if dependencies: + relative_paths = [] + for i in dependencies: + try: + relative_paths.append(str(Path(i).relative_to(root))) + except ValueError: + relative_paths.append(str(i)) + self._dependencies[str(key)] = relative_paths + elif str(key) in self._dependencies: + del self._dependencies[str(key)] + + if persist: + await self.save() + + async def get(self, filename: Path | str, persist=False): + if persist: + await self.load() + + root = self._filename.parent + try: + key = Path(filename).relative_to(root) + except ValueError: + key = filename + return set(self._dependencies.get(str(key), {})) + + def delete_file(self): + self._filename.unlink(missing_ok=True) + + @property + def exists(self): + return self._filename.exists() diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index f4c36b5b7..7f07e4427 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -8,16 +8,29 @@ """ from __future__ import annotations -import json +import os +import uuid +from datetime import datetime from pathlib import Path -from typing import Dict, List +from typing import Dict, List, Set import aiofiles from metagpt.logs import logger +from metagpt.schema import Document class FileRepository: + """A class representing a FileRepository associated with a Git repository. + + :param git_repo: The associated GitRepository instance. + :param relative_path: The relative path within the Git repository. + + Attributes: + _relative_path (Path): The relative path within the Git repository. + _git_repo (GitRepository): The associated GitRepository instance. + """ + def __init__(self, git_repo, relative_path: Path = Path(".")): """Initialize a FileRepository instance. @@ -26,16 +39,9 @@ class FileRepository: """ self._relative_path = relative_path self._git_repo = git_repo - self._dependencies: Dict[str, List[str]] = {} # Initializing self.workdir.mkdir(parents=True, exist_ok=True) - if self.dependency_path_name.exists(): - try: - with open(str(self.dependency_path_name), mode="r") as reader: - self._dependencies = json.load(reader) - except Exception as e: - logger.error(f"Failed to load {str(self.dependency_path_name)}, error:{e}") async def save(self, filename: Path | str, content, dependencies: List[str] = None): """Save content to a file and update its dependencies. @@ -44,59 +50,68 @@ class FileRepository: :param content: The content to be saved. :param dependencies: List of dependency filenames or paths. """ - path_name = self.workdir / filename - path_name.parent.mkdir(parents=True, exist_ok=True) - async with aiofiles.open(str(path_name), mode="w") as writer: + pathname = self.workdir / filename + pathname.parent.mkdir(parents=True, exist_ok=True) + async with aiofiles.open(str(pathname), mode="w") as writer: await writer.write(content) + logger.info(f"save to: {str(pathname)}") + if dependencies is not None: - await self.update_dependency(filename, dependencies) + dependency_file = await self._git_repo.get_dependency() + await dependency_file.update(pathname, set(dependencies)) + logger.info(f"update dependency: {str(pathname)}:{dependencies}") - async def get(self, filename: Path | str): - """Read the content of a file. - - :param filename: The filename or path within the repository. - :return: The content of the file. - """ - path_name = self.workdir / filename - async with aiofiles.open(str(path_name), mode="r") as reader: - return await reader.read() - - def get_dependency(self, filename: Path | str) -> List: + async def get_dependency(self, filename: Path | str) -> Set[str]: """Get the dependencies of a file. :param filename: The filename or path within the repository. - :return: List of dependency filenames or paths. + :return: Set of dependency filenames or paths. """ - key = str(filename) - return self._dependencies.get(key, []) + pathname = self.workdir / filename + dependency_file = await self._git_repo.get_dependency() + return await dependency_file.get(pathname) - def get_changed_dependency(self, filename: Path | str) -> List: + async def get_changed_dependency(self, filename: Path | str) -> Set[str]: """Get the dependencies of a file that have changed. :param filename: The filename or path within the repository. :return: List of changed dependency filenames or paths. """ - dependencies = self.get_dependency(filename=filename) + dependencies = await self.get_dependency(filename=filename) changed_files = self.changed_files - changed_dependent_files = [] + changed_dependent_files = set() for df in dependencies: if df in changed_files.keys(): - changed_dependent_files.append(df) + changed_dependent_files.add(df) return changed_dependent_files - async def update_dependency(self, filename, dependencies: List[str]): - """Update the dependencies of a file. + async def get(self, filename: Path | str) -> Document | None: + """Read the content of a file. :param filename: The filename or path within the repository. - :param dependencies: List of dependency filenames or paths. + :return: The content of the file. """ - self._dependencies[str(filename)] = dependencies + doc = Document(root_path=str(self.root_path), filename=str(filename)) + path_name = self.workdir / filename + if not path_name.exists(): + return None + async with aiofiles.open(str(path_name), mode="r") as reader: + doc.content = await reader.read() + return doc - async def save_dependency(self): - """Save the dependencies to a file.""" - data = json.dumps(self._dependencies) - with aiofiles.open(str(self.dependency_path_name), mode="w") as writer: - await writer.write(data) + async def get_all(self) -> List[Document]: + """Get the content of all files in the repository. + + :return: List of Document instances representing files. + """ + docs = [] + for root, dirs, files in os.walk(str(self.workdir)): + for file in files: + file_path = Path(root) / file + relative_path = file_path.relative_to(self.workdir) + doc = await self.get(relative_path) + docs.append(doc) + return docs @property def workdir(self): @@ -107,14 +122,9 @@ class FileRepository: return self._git_repo.workdir / self._relative_path @property - def dependency_path_name(self): - """Return the absolute path to the dependency file. - - :return: The absolute path to the dependency file. - """ - filename = ".dependencies.json" - path_name = self.workdir / filename - return path_name + def root_path(self): + """Return the relative path from git repository root""" + return self._relative_path @property def changed_files(self) -> Dict[str, str]: @@ -147,3 +157,13 @@ class FileRepository: continue children.append(str(f)) return children + + @staticmethod + def new_file_name(): + """Generate a new filename based on the current timestamp and a UUID suffix. + + :return: A new filename string. + """ + current_time = datetime.now().strftime("%Y%m%d%H%M%S") + guid_suffix = str(uuid.uuid4())[:8] + return f"{current_time}t{guid_suffix}" diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 6ae6a7900..a81b5c4ea 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -17,6 +17,7 @@ from git.repo import Repo from git.repo.fun import is_git_dir from metagpt.const import WORKSPACE_ROOT +from metagpt.utils.dependency_file import DependencyFile from metagpt.utils.file_repository import FileRepository @@ -47,6 +48,7 @@ class GitRepository: :param auto_init: If True, automatically initializes a new Git repository if the provided path is not a Git repository. """ self._repository = None + self._dependency = None if local_path: self.open(local_path=local_path, auto_init=auto_init) @@ -113,7 +115,7 @@ class GitRepository: :param local_path: The local path to check. :return: True if the directory is a Git repository, False otherwise. """ - git_dir = local_path / ".git" + git_dir = Path(local_path) / ".git" if git_dir.exists() and is_git_dir(git_dir): return True return False @@ -151,7 +153,7 @@ class GitRepository: self.add_change(self.changed_files) self.commit(comments) - def new_file_repository(self, relative_path: Path | str) -> FileRepository: + def new_file_repository(self, relative_path: Path | str = ".") -> FileRepository: """Create a new instance of FileRepository associated with this Git repository. :param relative_path: The relative path to the file repository within the Git repository. @@ -159,6 +161,15 @@ class GitRepository: """ return FileRepository(git_repo=self, relative_path=Path(relative_path)) + async def get_dependency(self) -> DependencyFile: + """Get the dependency file associated with the Git repository. + + :return: An instance of DependencyFile. + """ + if not self._dependency: + self._dependency = DependencyFile(workdir=self.workdir) + return self._dependency + if __name__ == "__main__": path = WORKSPACE_ROOT / "git" diff --git a/requirements.txt b/requirements.txt index c3b909e77..73a03d537 100644 --- a/requirements.txt +++ b/requirements.txt @@ -44,4 +44,4 @@ ta==0.10.2 semantic-kernel==0.3.13.dev0 wrapt==1.15.0 websocket-client==0.58.0 - +aiofiles==23.2.1 diff --git a/startup.py b/startup.py index e2a903c9b..d5a6bb07b 100644 --- a/startup.py +++ b/startup.py @@ -4,6 +4,7 @@ import asyncio import fire +from metagpt.config import CONFIG from metagpt.roles import ( Architect, Engineer, @@ -54,6 +55,7 @@ def main( code_review: bool = True, run_tests: bool = False, implement: bool = True, + project_path: str = None, ): """ We are a software startup comprised of AI. By investing in us, @@ -63,8 +65,12 @@ def main( a certain dollar amount to this AI company. :param n_round: :param code_review: Whether to use code review. + :param run_tests: Whether run unit tests. + :param implement: Whether to write codes. + :param project_path: The path of the old version project to improve. :return: """ + CONFIG.WORKDIR = project_path asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement)) diff --git a/tests/metagpt/utils/test_dependency_file.py b/tests/metagpt/utils/test_dependency_file.py new file mode 100644 index 000000000..ae4d40ea5 --- /dev/null +++ b/tests/metagpt/utils/test_dependency_file.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/22 +@Author : mashenquan +@File : test_dependency_file.py +@Desc: Unit tests for dependency_file.py +""" +from __future__ import annotations + +from pathlib import Path +from typing import Optional, Set, Union + +import pytest +from pydantic import BaseModel + +from metagpt.utils.dependency_file import DependencyFile + + +@pytest.mark.asyncio +async def test_dependency_file(): + class Input(BaseModel): + x: Union[Path, str] + deps: Optional[Set[Union[Path, str]]] + key: Optional[Union[Path, str]] + want: Set[str] + + inputs = [ + Input(x="a/b.txt", deps={"c/e.txt", Path(__file__).parent / "d.txt"}, want={"c/e.txt", "d.txt"}), + Input( + x=Path(__file__).parent / "x/b.txt", + deps={"s/e.txt", Path(__file__).parent / "d.txt"}, + key="x/b.txt", + want={"s/e.txt", "d.txt"}, + ), + Input(x="f.txt", deps=None, want=set()), + Input(x="a/b.txt", deps=None, want=set()), + ] + + file = DependencyFile(workdir=Path(__file__).parent) + + for i in inputs: + await file.update(filename=i.x, dependencies=i.deps) + assert await file.get(filename=i.key or i.x) == i.want + + file2 = DependencyFile(workdir=Path(__file__).parent) + file2.delete_file() + assert not file.exists + await file2.update(filename="a/b.txt", dependencies={"c/e.txt", Path(__file__).parent / "d.txt"}, persist=False) + assert not file.exists + await file2.save() + assert file2.exists + + file1 = DependencyFile(workdir=Path(__file__).parent) + assert file1.exists + assert await file1.get("a/b.txt") == set() + await file1.load() + assert await file1.get("a/b.txt") == {"c/e.txt", "d.txt"} + file1.delete_file() + assert not file.exists + + +if __name__ == "__main__": + pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/utils/test_file_repository.py b/tests/metagpt/utils/test_file_repository.py index ac36f2320..a830b58aa 100644 --- a/tests/metagpt/utils/test_file_repository.py +++ b/tests/metagpt/utils/test_file_repository.py @@ -34,11 +34,13 @@ async def test_file_repo(): assert file_repo.workdir.exists() await file_repo.save("a.txt", "AAA") await file_repo.save("b.txt", "BBB", ["a.txt"]) - assert "AAA" == await file_repo.get("a.txt") - assert "BBB" == await file_repo.get("b.txt") - assert ["a.txt"] == file_repo.get_dependency("b.txt") + doc = await file_repo.get("a.txt") + assert "AAA" == doc.content + doc = await file_repo.get("b.txt") + assert "BBB" == doc.content + assert {"a.txt"} == await file_repo.get_dependency("b.txt") assert {"a.txt": ChangeType.UNTRACTED, "b.txt": ChangeType.UNTRACTED} == file_repo.changed_files - assert ["a.txt"] == file_repo.get_changed_dependency("b.txt") + assert {"a.txt"} == await file_repo.get_changed_dependency("b.txt") await file_repo.save("d/e.txt", "EEE") assert ["d/e.txt"] == file_repo.get_change_dir_files("d") diff --git a/tests/metagpt/utils/test_git_repository.py b/tests/metagpt/utils/test_git_repository.py index 0d1e3b791..23bebba7f 100644 --- a/tests/metagpt/utils/test_git_repository.py +++ b/tests/metagpt/utils/test_git_repository.py @@ -77,5 +77,20 @@ async def test_git1(): assert not local_path.exists() +@pytest.mark.asyncio +async def test_dependency_file(): + local_path = Path(__file__).parent / "git2" + repo, subdir = await mock_repo(local_path) + + dependancy_file = await repo.get_dependency() + assert not dependancy_file.exists + + await dependancy_file.update(filename="a/b.txt", dependencies={"c/d.txt", "e/f.txt"}) + assert dependancy_file.exists + + repo.delete_repository() + assert not dependancy_file.exists + + if __name__ == "__main__": pytest.main([__file__, "-s"]) From 9339eab20c95263549c8ad60a6bad087ab2cac46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 20:40:42 +0800 Subject: [PATCH 133/413] feat: archive --- metagpt/actions/design_api.py | 22 +++++++--- metagpt/actions/project_management.py | 63 ++++++++++++++++++++++++++- metagpt/const.py | 3 +- metagpt/utils/file_repository.py | 2 +- metagpt/utils/git_repository.py | 16 +++++++ 5 files changed, 97 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 65d53364b..e7ee87fa2 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -11,7 +11,7 @@ from typing import List from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import PRDS_FILE_REPO, SYS_DESIGN_FILE_REPO, WORKSPACE_ROOT +from metagpt.const import PRDS_FILE_REPO, SYSTEM_DESIGN_FILE_REPO, WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser @@ -208,7 +208,7 @@ class WriteDesign(Action): prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) changed_prds = prds_file_repo.changed_files # 通过git diff来识别docs/system_designs下那些设计文档发生了变动; - system_design_file_repo = CONFIG.git_repo.new_file_repository(SYS_DESIGN_FILE_REPO) + system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_system_designs = system_design_file_repo.changed_files # 对于那些发生变动的PRD和设计文档,重新生成设计内容; @@ -219,7 +219,7 @@ class WriteDesign(Action): if not old_system_design_doc: system_design = await self._run(context=prd.content) doc = Document( - root_path=SYS_DESIGN_FILE_REPO, filename=filename, content=system_design.instruct_content.json() + root_path=SYSTEM_DESIGN_FILE_REPO, filename=filename, content=system_design.instruct_content.json() ) else: doc = await self._merge(prd_doc=prd, system_design_doc=old_system_design_doc) @@ -234,7 +234,9 @@ class WriteDesign(Action): prd_doc = await prds_file_repo.get(filename=filename) old_system_design_doc = await system_design_file_repo.get(filename) new_system_design_doc = await self._merge(prd_doc, old_system_design_doc) - await system_design_file_repo.save(filename=filename, content=new_system_design_doc.content) + await system_design_file_repo.save( + filename=filename, content=new_system_design_doc.content, dependencies={prd_doc.root_relative_path} + ) changed_files.docs[filename] = new_system_design_doc # 等docs/system_designs/下所有文件都处理完才发publish message,给后续做全局优化留空间。 @@ -251,8 +253,18 @@ class WriteDesign(Action): "Python package name", system_design.instruct_content.dict()["Python package name"].strip().strip("'").strip('"'), ) - # await self._save(context, system_design) + await self._rename_workspace(system_design) return system_design async def _merge(self, prd_doc, system_design_doc): return system_design_doc + + async def _rename_workspace(self, system_design): + if CONFIG.WORKDIR: # 已经指定了在旧版本上更新 + return + + if isinstance(system_design, ActionOutput): + ws_name = system_design.instruct_content.dict()["Python package name"] + else: + ws_name = CodeParser.parse_str(block="Python package name", text=system_design) + CONFIG.git_repo.rename_root(ws_name) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index b395fa64e..73481c780 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -5,11 +5,14 @@ @Author : alexanderwu @File : project_management.py """ +import json from typing import List +from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import WORKSPACE_ROOT +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, WORKSPACE_ROOT +from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template from metagpt.utils.json_to_markdown import json_to_markdown @@ -178,13 +181,69 @@ class WriteTasks(Action): requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt" requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) - async def run(self, context, format=CONFIG.prompt_format): + async def run(self, with_messages, format=CONFIG.prompt_format): + system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) + changed_system_designs = system_design_file_repo.changed_files + + tasks_file_repo = CONFIG.git_repo.new_file_repository(TASK_FILE_REPO) + changed_tasks = tasks_file_repo.changed_files + change_files = Documents() + # 根据docs/system_designs/下的git head diff识别哪些task文档需要重写 + for filename in changed_system_designs: + system_design_doc = await system_design_file_repo.get(filename) + task_doc = await tasks_file_repo.get(filename) + if task_doc: + task_doc = await self._merge(system_design_doc, task_doc) + else: + rsp = await self._run(system_design_doc.content) + task_doc = Document(root_path=TASK_FILE_REPO, filename=filename, content=rsp.instruct_content.json()) + await tasks_file_repo.save( + filename=filename, content=task_doc.content, dependencies={system_design_doc.root_relative_path} + ) + await self._update_requirements(task_doc) + change_files.docs[filename] = task_doc + + # 根据docs/tasks/下的git head diff识别哪些task文件被用户修改了,需要重写 + for filename in changed_tasks: + if filename in change_files.docs: + continue + system_design_doc = await system_design_file_repo.get(filename) + task_doc = await tasks_file_repo.get(filename) + task_doc = await self._merge(system_design_doc, task_doc) + await tasks_file_repo.save( + filename=filename, content=task_doc.content, dependencies={system_design_doc.root_relative_path} + ) + await self._update_requirements(task_doc) + change_files.docs[filename] = task_doc + + # 等docs/tasks/下所有文件都处理完才发publish message,给后续做全局优化留空间。 + return ActionOutput(content=change_files.json(), instruct_content=change_files) + + async def _run(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) self._save(context, rsp) return rsp + async def _merge(self, system_design_doc, task_dock) -> Document: + return task_dock + + async def _update_requirements(self, doc): + m = json.loads(doc.content) + packages = set(m.get("Required Python third-party packages", set())) + file_repo = CONFIG.git_repo.new_file_repository() + filename = "requirements.txt" + requirement_doc = await file_repo.get(filename) + if not requirement_doc: + requirement_doc = Document(filename=filename, root_path=".", content="") + lines = requirement_doc.content.splitlines() + for pkg in lines: + if pkg == "": + continue + packages.add(pkg) + await file_repo.save(filename, content="\n".join(packages)) + class AssignTasks(Action): async def run(self, *args, **kwargs): diff --git a/metagpt/const.py b/metagpt/const.py index fc1c47b5b..63f39f4a8 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -53,4 +53,5 @@ MESSAGE_ROUTE_TO_ALL = "" REQUIREMENT_FILENAME = "requirement.txt" DOCS_FILE_REPO = "docs" PRDS_FILE_REPO = "docs/prds" -SYS_DESIGN_FILE_REPO = "docs/system_design" +SYSTEM_DESIGN_FILE_REPO = "docs/system_design" +TASK_FILE_REPO = "docs/tasks" diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 7f07e4427..ee6811209 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -166,4 +166,4 @@ class FileRepository: """ current_time = datetime.now().strftime("%Y%m%d%H%M%S") guid_suffix = str(uuid.uuid4())[:8] - return f"{current_time}t{guid_suffix}" + return f"{current_time}x{guid_suffix}" diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index a81b5c4ea..2a4fb4a4d 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -17,6 +17,7 @@ from git.repo import Repo from git.repo.fun import is_git_dir from metagpt.const import WORKSPACE_ROOT +from metagpt.logs import logger from metagpt.utils.dependency_file import DependencyFile from metagpt.utils.file_repository import FileRepository @@ -170,6 +171,21 @@ class GitRepository: self._dependency = DependencyFile(workdir=self.workdir) return self._dependency + def rename_root(self, new_dir_name): + """Rename the root directory of the Git repository. + + :param new_dir_name: The new name for the root directory. + """ + if self.workdir.name == new_dir_name: + return + new_path = self.workdir.parent / new_dir_name + if new_path.exists(): + logger.info(f"Delete directory {str(new_path)}") + shutil.rmtree(new_path) + self.workdir.rename(new_path) + logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") + self._repository = Repo(new_path) + if __name__ == "__main__": path = WORKSPACE_ROOT / "git" From e8131652de02a93454343d059dec02199f27b459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 21:45:44 +0800 Subject: [PATCH 134/413] refactor: write prd & system design --- metagpt/actions/design_api.py | 83 ++++++++++++++++++++++++++--------- metagpt/actions/write_prd.py | 70 ++++++++++++++++++++++------- metagpt/const.py | 5 +++ 3 files changed, 123 insertions(+), 35 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index e7ee87fa2..3bbde24ea 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -5,13 +5,21 @@ @Author : alexanderwu @File : design_api.py """ +import json import shutil from pathlib import Path from typing import List from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import PRDS_FILE_REPO, SYSTEM_DESIGN_FILE_REPO, WORKSPACE_ROOT +from metagpt.const import ( + DATA_API_DESIGN_FILE_REPO, + PRDS_FILE_REPO, + SEQ_FLOW_FILE_REPO, + SYSTEM_DESIGN_FILE_REPO, + SYSTEM_DESIGN_PDF_FILE_REPO, + WORKSPACE_ROOT, +) from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser @@ -214,40 +222,29 @@ class WriteDesign(Action): # 对于那些发生变动的PRD和设计文档,重新生成设计内容; changed_files = Documents() for filename in changed_prds.keys(): - prd = await prds_file_repo.get(filename) - old_system_design_doc = await system_design_file_repo.get(filename) - if not old_system_design_doc: - system_design = await self._run(context=prd.content) - doc = Document( - root_path=SYSTEM_DESIGN_FILE_REPO, filename=filename, content=system_design.instruct_content.json() - ) - else: - doc = await self._merge(prd_doc=prd, system_design_doc=old_system_design_doc) - await system_design_file_repo.save( - filename=filename, content=doc.content, dependencies={prd.root_relative_path} + doc = await self._update_system_design( + filename=filename, prds_file_repo=prds_file_repo, system_design_file_repo=system_design_file_repo ) changed_files.docs[filename] = doc for filename in changed_system_designs.keys(): if filename in changed_files.docs: continue - prd_doc = await prds_file_repo.get(filename=filename) - old_system_design_doc = await system_design_file_repo.get(filename) - new_system_design_doc = await self._merge(prd_doc, old_system_design_doc) - await system_design_file_repo.save( - filename=filename, content=new_system_design_doc.content, dependencies={prd_doc.root_relative_path} + doc = await self._update_system_design( + filename=filename, prds_file_repo=prds_file_repo, system_design_file_repo=system_design_file_repo ) - changed_files.docs[filename] = new_system_design_doc + changed_files.docs[filename] = doc # 等docs/system_designs/下所有文件都处理完才发publish message,给后续做全局优化留空间。 return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - async def _run(self, context, format=CONFIG.prompt_format): + async def _new_system_design(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) # system_design = await self._aask(prompt) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python package name" contain space, have to use setattr + # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python + # package name" contain space, have to use setattr setattr( system_design.instruct_content, "Python package name", @@ -268,3 +265,49 @@ class WriteDesign(Action): else: ws_name = CodeParser.parse_str(block="Python package name", text=system_design) CONFIG.git_repo.rename_root(ws_name) + + async def _update_system_design(self, filename, prds_file_repo, system_design_file_repo) -> Document: + prd = await prds_file_repo.get(filename) + old_system_design_doc = await system_design_file_repo.get(filename) + if not old_system_design_doc: + system_design = await self._new_system_design(context=prd.content) + doc = Document( + root_path=SYSTEM_DESIGN_FILE_REPO, filename=filename, content=system_design.instruct_content.json() + ) + else: + doc = await self._merge(prd_doc=prd, system_design_doc=old_system_design_doc) + await system_design_file_repo.save( + filename=filename, content=doc.content, dependencies={prd.root_relative_path} + ) + await self._save_data_api_design(doc) + await self._save_seq_flow(doc) + await self._save_pdf(doc) + return doc + + @staticmethod + async def _save_data_api_design(design_doc): + m = json.loads(design_doc.content) + data_api_design = m.get("Data structures and interface definitions") + if not data_api_design: + return + path = CONFIG.git_repo.workdir / DATA_API_DESIGN_FILE_REPO + if not path.exists(): + path.mkdir(parents=True, exists_ok=True) + await mermaid_to_file(data_api_design, path / Path(design_doc).with_suffix(".mmd")) + + @staticmethod + async def _save_seq_flow(design_doc): + m = json.loads(design_doc.content) + seq_flow = m.get("Program call flow") + if not seq_flow: + return + path = CONFIG.git_repo.workdir / SEQ_FLOW_FILE_REPO + if not path.exists(): + path.mkdir(parents=True, exists_ok=True) + await mermaid_to_file(seq_flow, path / Path(design_doc).with_suffix(".mmd")) + + @staticmethod + async def _save_pdf(design_doc): + m = json.loads(design_doc.content) + file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_PDF_FILE_REPO) + await file_repo.save(filename=design_doc.filename, content=json_to_markdown(m)) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index a16d1ec06..df35ec865 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -5,16 +5,28 @@ @Author : alexanderwu @File : write_prd.py """ +from __future__ import annotations + +import json +from pathlib import Path from typing import List from metagpt.actions import Action, ActionOutput from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG -from metagpt.const import DOCS_FILE_REPO, PRDS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.const import ( + COMPETITIVE_ANALYSIS_FILE_REPO, + DOCS_FILE_REPO, + PRD_PDF_FILE_REPO, + PRDS_FILE_REPO, + REQUIREMENT_FILENAME, +) from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template +from metagpt.utils.json_to_markdown import json_to_markdown +from metagpt.utils.mermaid import mermaid_to_file templates = { "json": { @@ -233,22 +245,15 @@ class WritePRD(Action): prd_docs = await prds_file_repo.get_all() change_files = Documents() for prd_doc in prd_docs: - if await self._is_relative_to(requirement_doc, prd_doc): - prd_doc = await self._merge(requirement_doc, prd_doc) - await prds_file_repo.save(filename=prd_doc.filename, content=prd_doc.content) - change_files.docs[prd_doc.filename] = prd_doc + prd_doc = await self._update_prd(requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) + if not prd_doc: + continue + change_files.docs[prd_doc.filename] = prd_doc # 如果没有任何PRD,就使用docs/requirement.txt生成一个prd if not change_files.docs: - prd = await self._run_new_requirement( - requirements=[requirement_doc.content], format=format, *args, **kwargs - ) - doc = Document( - root_path=PRDS_FILE_REPO, - filename=FileRepository.new_file_name() + ".json", - content=prd.instruct_content.json(), - ) - await prds_file_repo.save(filename=doc.filename, content=doc.content) - change_files.docs[doc.filename] = doc + prd_doc = await self._update_prd(requirement_doc, None, prds_file_repo) + if prd_doc: + change_files.docs[prd_doc.filename] = prd_doc # 等docs/prds/下所有文件都与新增需求对比完后,再触发publish message让工作流跳转到下一环节。如此设计是为了给后续做全局优化留空间。 return ActionOutput(content=change_files.json(), instruct_content=change_files) @@ -275,3 +280,38 @@ class WritePRD(Action): async def _merge(self, doc1, doc2) -> Document: pass + + async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: + if not prd_doc: + prd = await self._run_new_requirement( + requirements=[requirement_doc.content], format=format, *args, **kwargs + ) + new_prd_doc = Document( + root_path=PRDS_FILE_REPO, + filename=FileRepository.new_file_name() + ".json", + content=prd.instruct_content.json(), + ) + elif await self._is_relative_to(requirement_doc, prd_doc): + new_prd_doc = await self._merge(requirement_doc, prd_doc) + else: + return None + await prds_file_repo.save(filename=new_prd_doc.filename, content=new_prd_doc.content) + await self._save_competitive_analysis(new_prd_doc) + await self._save_pdf(new_prd_doc) + + @staticmethod + async def _save_competitive_analysis(prd_doc): + m = json.loads(prd_doc.content) + quadrant_chart = m.get("Competitive Quadrant Chart") + if not quadrant_chart: + return + path = CONFIG.git_repo.workdir / COMPETITIVE_ANALYSIS_FILE_REPO + if not path.exists(): + path.mkdir(parents=True, exists_ok=True) + await mermaid_to_file(quadrant_chart, path / Path(prd_doc).with_suffix(".mmd")) + + @staticmethod + async def _save_pdf(prd_doc): + m = json.loads(prd_doc.content) + file_repo = CONFIG.git_repo.new_file_repository(PRD_PDF_FILE_REPO) + await file_repo.save(filename=prd_doc.filename, content=json_to_markdown(m)) diff --git a/metagpt/const.py b/metagpt/const.py index 63f39f4a8..b5ecad7cc 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -55,3 +55,8 @@ DOCS_FILE_REPO = "docs" PRDS_FILE_REPO = "docs/prds" SYSTEM_DESIGN_FILE_REPO = "docs/system_design" TASK_FILE_REPO = "docs/tasks" +COMPETITIVE_ANALYSIS_FILE_REPO = "resources/competitive_analysis" +DATA_API_DESIGN_FILE_REPO = "resources/data_api_design" +SEQ_FLOW_FILE_REPO = "resources/seq_flow" +SYSTEM_DESIGN_PDF_FILE_REPO = "resources/system_design" +PRD_PDF_FILE_REPO = "resources/prd" From 62d93517b48824c40209e447e8f76a59a7744d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 21:59:14 +0800 Subject: [PATCH 135/413] refactor: write prd & system design --- metagpt/actions/design_api.py | 21 +++++++++++++-------- metagpt/actions/write_prd.py | 8 ++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 3bbde24ea..8fb926477 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -290,10 +290,9 @@ class WriteDesign(Action): data_api_design = m.get("Data structures and interface definitions") if not data_api_design: return - path = CONFIG.git_repo.workdir / DATA_API_DESIGN_FILE_REPO - if not path.exists(): - path.mkdir(parents=True, exists_ok=True) - await mermaid_to_file(data_api_design, path / Path(design_doc).with_suffix(".mmd")) + pathname = CONFIG.git_repo.workdir / DATA_API_DESIGN_FILE_REPO / Path(design_doc).with_suffix(".mmd") + await WriteDesign._save_mermaid_file(data_api_design, pathname) + logger.info(f"Save class view to {str(pathname)}") @staticmethod async def _save_seq_flow(design_doc): @@ -301,13 +300,19 @@ class WriteDesign(Action): seq_flow = m.get("Program call flow") if not seq_flow: return - path = CONFIG.git_repo.workdir / SEQ_FLOW_FILE_REPO - if not path.exists(): - path.mkdir(parents=True, exists_ok=True) - await mermaid_to_file(seq_flow, path / Path(design_doc).with_suffix(".mmd")) + pathname = CONFIG.git_repo.workdir / SEQ_FLOW_FILE_REPO / Path(design_doc).with_suffix(".mmd") + await WriteDesign._save_mermaid_file(seq_flow, pathname) + logger.info(f"Saving sequence flow to {str(pathname)}") @staticmethod async def _save_pdf(design_doc): m = json.loads(design_doc.content) file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_PDF_FILE_REPO) await file_repo.save(filename=design_doc.filename, content=json_to_markdown(m)) + logger.info(f"Saving system design pdf to {design_doc.root_relative_path}") + + @staticmethod + async def _save_mermaid_file(data: str, pathname: Path): + if not pathname.parent.exists(): + pathname.parent.mkdir(parents=True, exists_ok=True) + await mermaid_to_file(data, pathname) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index df35ec865..34001dec1 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -305,10 +305,10 @@ class WritePRD(Action): quadrant_chart = m.get("Competitive Quadrant Chart") if not quadrant_chart: return - path = CONFIG.git_repo.workdir / COMPETITIVE_ANALYSIS_FILE_REPO - if not path.exists(): - path.mkdir(parents=True, exists_ok=True) - await mermaid_to_file(quadrant_chart, path / Path(prd_doc).with_suffix(".mmd")) + pathname = CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc).with_suffix(".mmd") + if not pathname.parent.exists(): + pathname.parent.mkdir(parents=True, exists_ok=True) + await mermaid_to_file(quadrant_chart, pathname) @staticmethod async def _save_pdf(prd_doc): From 369047e5586ef52cd21b9bc401630dfbba23fa29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 22 Nov 2023 22:00:51 +0800 Subject: [PATCH 136/413] refactor: write prd & system design --- metagpt/actions/design_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 8fb926477..2c8c87558 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -290,7 +290,7 @@ class WriteDesign(Action): data_api_design = m.get("Data structures and interface definitions") if not data_api_design: return - pathname = CONFIG.git_repo.workdir / DATA_API_DESIGN_FILE_REPO / Path(design_doc).with_suffix(".mmd") + pathname = CONFIG.git_repo.workdir / Path(DATA_API_DESIGN_FILE_REPO) / Path(design_doc).with_suffix(".mmd") await WriteDesign._save_mermaid_file(data_api_design, pathname) logger.info(f"Save class view to {str(pathname)}") @@ -300,7 +300,7 @@ class WriteDesign(Action): seq_flow = m.get("Program call flow") if not seq_flow: return - pathname = CONFIG.git_repo.workdir / SEQ_FLOW_FILE_REPO / Path(design_doc).with_suffix(".mmd") + pathname = CONFIG.git_repo.workdir / Path(SEQ_FLOW_FILE_REPO) / Path(design_doc).with_suffix(".mmd") await WriteDesign._save_mermaid_file(seq_flow, pathname) logger.info(f"Saving sequence flow to {str(pathname)}") From 642335317b6a11f67da5b39fc84deca11249d331 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 01:46:14 +0800 Subject: [PATCH 137/413] add independent openllm and fireworks config fields, add llm output postprecess plugin --- config/config.yaml | 9 +++ metagpt/actions/action.py | 27 ++++--- metagpt/config.py | 15 +++- metagpt/llm.py | 7 +- metagpt/provider/fireworks_api.py | 24 +++++++ metagpt/provider/open_llm_api.py | 47 ++++++++++++ .../postprecess/base_postprecess_plugin.py | 72 +++++++++++++++++++ .../postprecess/llm_output_postprecess.py | 23 ++++++ metagpt/utils/repair_llm_raw_output.py | 14 ++-- .../utils/test_repair_llm_raw_output.py | 34 ++++++--- 10 files changed, 243 insertions(+), 29 deletions(-) create mode 100644 metagpt/provider/fireworks_api.py create mode 100644 metagpt/provider/open_llm_api.py create mode 100644 metagpt/provider/postprecess/base_postprecess_plugin.py create mode 100644 metagpt/provider/postprecess/llm_output_postprecess.py diff --git a/config/config.yaml b/config/config.yaml index 72d2c0b19..080de4000 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -34,6 +34,15 @@ RPM: 10 #### if zhipuai from `https://open.bigmodel.cn`. You can set here or export API_KEY="YOUR_API_KEY" # ZHIPUAI_API_KEY: "YOUR_API_KEY" +#### if use self-host open llm model with openai-compatible interface +#OPEN_LLM_API_BASE: "http://127.0.0.1:8000/v1" +#OPEN_LLM_API_MODEL: "llama2-13b" +# +##### if use Fireworks api +#FIREWORKS_API_KEY: "YOUR_API_KEY" +#FIREWORKS_API_BASE: "https://api.fireworks.ai/inference/v1" +#FIREWORKS_API_MODEL: "YOUR_LLM_MODEL" # example, accounts/fireworks/models/llama-v2-13b-chat + #### for Search ## Supported values: serpapi/google/serper/ddg diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 7433c3857..cb5bd9ce1 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -6,17 +6,29 @@ @File : action.py """ +import typing from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_fixed, after_log +from tenacity import retry, stop_after_attempt, wait_fixed, after_log, _utils from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser -from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType,\ - retry_parse_json_text, extract_content_from_output +from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess + + +def action_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + def log_it(retry_state: "RetryCallState") -> None: + if retry_state.fn is None: + fn_name = "" + else: + fn_name = _utils.get_callback_name(retry_state.fn) + logger.error(f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " + f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " + f"exp: {retry_state.outcome.exception()}") + return log_it class Action(ABC): @@ -53,7 +65,7 @@ class Action(ABC): @retry( stop=stop_after_attempt(3), wait=wait_fixed(1), - after=after_log(logger, logger.level("ERROR").name), + after=action_after_log(logger), ) async def _aask_v1( self, @@ -70,14 +82,9 @@ class Action(ABC): content = await self.llm.aask(prompt, system_msgs) logger.debug(f"llm raw output:\n{content}") output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - output_class_fields = list(output_class.schema()["properties"].keys()) # Custom ActionOutput's fields if format == "json": - content = repair_llm_raw_output(content, req_keys=output_class_fields + ["[/CONTENT]"]) - content = extract_content_from_output(content) - content = repair_llm_raw_output(content, req_keys=[None], repair_type=RepairType.JSON) # req_keys mocked - logger.info(f"extracted json CONTENT from output:\n{content}") - parsed_data = retry_parse_json_text(output=content) # should use output=content + parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) diff --git a/metagpt/config.py b/metagpt/config.py index a4c43c28a..2ce75b013 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -46,10 +46,18 @@ class Config(metaclass=Singleton): self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") self.zhipuai_api_key = self._get("ZHIPUAI_API_KEY") + + self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") + self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") + + self.fireworks_api_key = self._get("FIREWORKS_API_KEY") if (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) and \ (not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key) and \ - (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key): - raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first") + (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key) and \ + (not self.open_llm_api_base) and \ + (not self.fireworks_api_key or "YOUR_API_KEY" == self.fireworks_api_key): + raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first " + "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE") self.openai_api_base = self._get("OPENAI_API_BASE") openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy if openai_proxy: @@ -69,6 +77,9 @@ class Config(metaclass=Singleton): self.domain = self._get("DOMAIN") self.spark_url = self._get("SPARK_URL") + self.fireworks_api_base = self._get("FIREWORKS_API_BASE") + self.fireworks_api_model = self._get("FIREWORKS_API_MODEL") + self.claude_api_key = self._get("Anthropic_API_KEY") self.serpapi_api_key = self._get("SERPAPI_API_KEY") self.serper_api_key = self._get("SERPER_API_KEY") diff --git a/metagpt/llm.py b/metagpt/llm.py index 4edcd7a83..1f7d1b4c9 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -6,12 +6,13 @@ @File : llm.py """ -from metagpt.logs import logger from metagpt.config import CONFIG from metagpt.provider.anthropic_api import Claude2 as Claude from metagpt.provider.openai_api import OpenAIGPTAPI from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI from metagpt.provider.spark_api import SparkAPI +from metagpt.provider.open_llm_api import OpenLLMGPTAPI +from metagpt.provider.fireworks_api import FireWorksGPTAPI from metagpt.provider.human_provider import HumanProvider @@ -26,6 +27,10 @@ def LLM() -> "BaseGPTAPI": llm = SparkAPI() elif CONFIG.zhipuai_api_key: llm = ZhiPuAIGPTAPI() + elif CONFIG.open_llm_api_base: + llm = OpenLLMGPTAPI() + elif CONFIG.fireworks_api_key: + llm = FireWorksGPTAPI() else: raise RuntimeError("You should config a LLM configuration first") diff --git a/metagpt/provider/fireworks_api.py b/metagpt/provider/fireworks_api.py new file mode 100644 index 000000000..23126af2d --- /dev/null +++ b/metagpt/provider/fireworks_api.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : fireworks.ai's api + +import openai + +from metagpt.config import CONFIG +from metagpt.provider.openai_api import OpenAIGPTAPI, CostManager, RateLimiter + + +class FireWorksGPTAPI(OpenAIGPTAPI): + + def __init__(self): + self.__init_fireworks(CONFIG) + self.llm = openai + self.model = CONFIG.fireworks_api_model + self.auto_max_tokens = False + self._cost_manager = CostManager() + RateLimiter.__init__(self, rpm=self.rpm) + + def __init_fireworks(self, config: "Config"): + openai.api_key = config.fireworks_api_key + openai.api_base = config.fireworks_api_base + self.rpm = int(config.get("RPM", 10)) diff --git a/metagpt/provider/open_llm_api.py b/metagpt/provider/open_llm_api.py new file mode 100644 index 000000000..a6820b42b --- /dev/null +++ b/metagpt/provider/open_llm_api.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : self-host open llm model with openai-compatible interface + +import openai + +from metagpt.logs import logger +from metagpt.config import CONFIG +from metagpt.provider.openai_api import OpenAIGPTAPI, CostManager, RateLimiter + + +class OpenLLMCostManager(CostManager): + """ open llm model is self-host, it's free and without cost""" + + def update_cost(self, prompt_tokens, completion_tokens, model): + """ + Update the total cost, prompt tokens, and completion tokens. + + Args: + prompt_tokens (int): The number of tokens used in the prompt. + completion_tokens (int): The number of tokens used in the completion. + model (str): The model used for the API call. + """ + self.total_prompt_tokens += prompt_tokens + self.total_completion_tokens += completion_tokens + + logger.info( + f"Max budget: ${CONFIG.max_budget:.3f} | " + f"prompt_tokens: {prompt_tokens}, completion_tokens: {completion_tokens}" + ) + CONFIG.total_cost = self.total_cost + + +class OpenLLMGPTAPI(OpenAIGPTAPI): + + def __init__(self): + self.__init_openllm(CONFIG) + self.llm = openai + self.model = CONFIG.open_llm_api_model + self.auto_max_tokens = False + self._cost_manager = OpenLLMCostManager() + RateLimiter.__init__(self, rpm=self.rpm) + + def __init_openllm(self, config: "Config"): + openai.api_key = "sk-xx" # self-host api doesn't need api-key, use the default value + openai.api_base = config.open_llm_api_base + self.rpm = int(config.get("RPM", 10)) diff --git a/metagpt/provider/postprecess/base_postprecess_plugin.py b/metagpt/provider/postprecess/base_postprecess_plugin.py new file mode 100644 index 000000000..702a03194 --- /dev/null +++ b/metagpt/provider/postprecess/base_postprecess_plugin.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : base llm postprocess plugin to do the operations like repair the raw llm output + +from typing import Union + +from metagpt.logs import logger +from metagpt.utils.repair_llm_raw_output import RepairType +from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, extract_content_from_output, \ + retry_parse_json_text + + +class BasePostPrecessPlugin(object): + + model = None # the plugin of the `model`, use to judge in `llm_postprecess` + + def run_repair_llm_output(self, output: str, schema: dict, req_key: str = "[/CONTENT]") -> Union[dict, list]: + """ + repair steps + 1. repair the case sensitive problem using the schema's fields + 2. extract the content from the req_key pair( xx[REQ_KEY]xxx[/REQ_KEY]xx ) + 3. repair the invalid json text in the content + 4. parse the json text and repair it according to the exception with retry loop + """ + output_class_fields = list(schema["properties"].keys()) # Custom ActionOutput's fields + + content = self.run_repair_llm_raw_output(output, req_keys=output_class_fields + [req_key]) + content = self.run_extract_content_from_output(content, right_key=req_key) + # # req_keys mocked + content = self.run_repair_llm_raw_output(content, req_keys=[None], repair_type=RepairType.JSON) + parsed_data = self.run_retry_parse_json_text(content) + + return parsed_data + + def run_repair_llm_raw_output(self, content: str, req_keys: list[str], repair_type: str = None) -> str: + """ inherited class can re-implement the function""" + return repair_llm_raw_output(content, req_keys=req_keys, repair_type=repair_type) + + def run_extract_content_from_output(self, content: str, right_key: str) -> str: + """ inherited class can re-implement the function""" + return extract_content_from_output(content, right_key=right_key) + + def run_retry_parse_json_text(self, content: str) -> Union[dict, list]: + """ inherited class can re-implement the function""" + logger.info(f"extracted json CONTENT from output:\n{content}") + parsed_data = retry_parse_json_text(output=content) # should use output=content + return parsed_data + + def run(self, output: str, schema: dict, req_key: str = "[/CONTENT]") -> Union[dict, list]: + """ + this is used for prompt with a json-format output requirement and outer pair key, like + [REQ_KEY] + { + "Key": "value" + } + [/REQ_KEY] + + Args + outer (str): llm raw output + schema: output json schema + req_key: outer pair right key, usually in `[/REQ_KEY]` format + """ + assert len(schema.get("properties")) > 0 + assert "/" in req_key + + # current, postprocess only deal the repair_llm_raw_output + new_output = self.run_repair_llm_output( + output=output, + schema=schema, + req_key=req_key + ) + return new_output diff --git a/metagpt/provider/postprecess/llm_output_postprecess.py b/metagpt/provider/postprecess/llm_output_postprecess.py new file mode 100644 index 000000000..4b5955061 --- /dev/null +++ b/metagpt/provider/postprecess/llm_output_postprecess.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : the entry of choosing which PostProcessPlugin to deal particular LLM model's output + +from typing import Union + +from metagpt.provider.postprecess.base_postprecess_plugin import BasePostPrecessPlugin + + +def llm_output_postprecess(output: str, schema: dict, req_key: str = "[/CONTENT]", + model_name: str = None) -> Union[dict, str]: + """ + default use BasePostPrecessPlugin if there is not matched plugin. + """ + # TODO choose different model's plugin according to the model_name + postprecess_plugin = BasePostPrecessPlugin() + + result = postprecess_plugin.run( + output=output, + schema=schema, + req_key=req_key + ) + return result diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index a12a36fcc..4a632b80c 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -91,13 +91,13 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - idx1 = sub_output.rfind("}") idx2 = sub_output.rindex("]") idx = idx1 if idx1 >= idx2 else idx2 - sub_output = sub_output[: idx] + sub_output = sub_output[: idx+1] return sub_output if output.strip().endswith("}") or (output.strip().endswith("]") and not output.strip().endswith(left_key)): # # avoid [req_key]xx[req_key] case to append [/req_key] output = output + "\n" + right_key - elif judge_potential_json(output, left_key): + elif judge_potential_json(output, left_key) and (not output.strip().endswith(left_key)): sub_content = judge_potential_json(output, left_key) output = sub_content + "\n" + right_key @@ -116,7 +116,7 @@ def repair_json_format(output: str) -> str: elif output.endswith("}]"): output = output[:-1] logger.info(f"repair_json_format: {'}]'}") - elif output.startswith("{") and output.startswith("]"): + elif output.startswith("{") and output.endswith("]"): output = output[:-1] + "}" return output @@ -183,9 +183,11 @@ def repair_invalid_json(output: str, error: str) -> str: if line.endswith("],"): # problem, redundant char `]` line = line.replace("]", "") - elif line.endswith("},"): + elif line.endswith("},") and not output.endswith("},"): # problem, redundant char `}` line = line.replace("}", "") + elif line.endswith("},") and output.endswith("},"): + line = line[:-1] elif '",' not in line: line = f'{line}",' elif "," not in line: @@ -218,11 +220,10 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R """ if retry_state.outcome.failed: if len(retry_state.args) > 0: - # # can't used as args=retry_state.args + # # can't be used as args=retry_state.args func_param_output = retry_state.args[0] elif len(retry_state.kwargs) > 0: func_param_output = retry_state.kwargs.get("output", "") - # import pdb; pdb.set_trace() exp_str = str(retry_state.outcome.exception()) logger.warning(f"parse json from content inside [CONTENT][/CONTENT] failed at retry " f"{retry_state.attempt_number}, try to fix it, exp: {exp_str}") @@ -265,6 +266,7 @@ def extract_content_from_output(content: str, right_key: str = "[/CONTENT]"): break return cont.strip() + # TODO construct the extract pattern with the `right_key` raw_content = copy.deepcopy(content) pattern = r"\[CONTENT\]([\s\S]*)\[/CONTENT\]" new_content = re_extract_content(raw_content, pattern) diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index dfcf60ad5..8779c965c 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -77,11 +77,11 @@ def test_required_key_pair_missing(): raw_output = '''[CONTENT] { - "a": "b" + "key": "value" ]''' target_output = '''[CONTENT] { - "a": "b" + "key": "value" ] [/CONTENT]''' @@ -92,17 +92,15 @@ def test_required_key_pair_missing(): raw_output = '''[CONTENT] tag [CONTENT] { - "a": "b" + "key": "value" } xxx ''' - target_output = '''[CONTENT] tag -[CONTENT] + target_output = '''[CONTENT] { - "a": "b" + "key": "value" } -[/CONTENT] -''' +[/CONTENT]''' output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) assert output == target_output @@ -117,6 +115,22 @@ def test_repair_json_format(): repair_type=RepairType.JSON) assert output == target_output + raw_output = "[{ xxx }" + target_output = "{ xxx }" + + output = repair_llm_raw_output(output=raw_output, + req_keys=[None], + repair_type=RepairType.JSON) + assert output == target_output + + raw_output = "{ xxx ]" + target_output = "{ xxx }" + + output = repair_llm_raw_output(output=raw_output, + req_keys=[None], + repair_type=RepairType.JSON) + assert output == target_output + def test_retry_parse_json_text(): invalid_json_text = """{ @@ -130,7 +144,7 @@ def test_retry_parse_json_text(): "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", "Requirement Analysis": "The requirements are clear and well-defined" } - output = retry_parse_json_text(invalid_json_text) + output = retry_parse_json_text(output=invalid_json_text) assert output == target_json invalid_json_text = """{ @@ -144,7 +158,7 @@ def test_retry_parse_json_text(): "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", "Requirement Analysis": "The requirements are clear and well-defined" } - output = retry_parse_json_text(invalid_json_text) + output = retry_parse_json_text(output=invalid_json_text) assert output == target_json From 9a2ac792fe1100c2f86783b547bfafcdeae2c95f Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 01:55:56 +0800 Subject: [PATCH 138/413] add __init__ --- metagpt/provider/postprecess/__init__.py | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 metagpt/provider/postprecess/__init__.py diff --git a/metagpt/provider/postprecess/__init__.py b/metagpt/provider/postprecess/__init__.py new file mode 100644 index 000000000..2bcf8efd0 --- /dev/null +++ b/metagpt/provider/postprecess/__init__.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : From 1a67878b7e8ae3c07dc9306659ff551b18fe00b0 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 09:29:40 +0800 Subject: [PATCH 139/413] move after_log as general one --- metagpt/actions/action.py | 16 ++-------------- metagpt/utils/utils.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 metagpt/utils/utils.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index cb5bd9ce1..0a7a1656d 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -6,7 +6,6 @@ @File : action.py """ -import typing from abc import ABC from typing import Optional @@ -16,21 +15,10 @@ from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser +from metagpt.utils.utils import general_after_log from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess -def action_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: - def log_it(retry_state: "RetryCallState") -> None: - if retry_state.fn is None: - fn_name = "" - else: - fn_name = _utils.get_callback_name(retry_state.fn) - logger.error(f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " - f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " - f"exp: {retry_state.outcome.exception()}") - return log_it - - class Action(ABC): def __init__(self, name: str = "", context=None, llm: LLM = None): self.name: str = name @@ -65,7 +53,7 @@ class Action(ABC): @retry( stop=stop_after_attempt(3), wait=wait_fixed(1), - after=action_after_log(logger), + after=general_after_log(logger), ) async def _aask_v1( self, diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py new file mode 100644 index 000000000..f479ec3b8 --- /dev/null +++ b/metagpt/utils/utils.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : + +import typing + +from tenacity import after_log, _utils + + +def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + def log_it(retry_state: "RetryCallState") -> None: + if retry_state.fn is None: + fn_name = "" + else: + fn_name = _utils.get_callback_name(retry_state.fn) + logger.error(f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " + f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " + f"exp: {retry_state.outcome.exception()}") + return log_it From 502bb2c4498b1a672a2f890a8fda479f812165c2 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 11:21:25 +0800 Subject: [PATCH 140/413] fix extract_content_from_output --- metagpt/utils/repair_llm_raw_output.py | 4 +++- tests/metagpt/utils/test_repair_llm_raw_output.py | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 4a632b80c..0b521a7b0 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -275,13 +275,15 @@ def extract_content_from_output(content: str, right_key: str = "[/CONTENT]"): # TODO find a more general pattern # # for `[CONTENT]xxx[CONTENT]xxxx[/CONTENT] situation logger.warning(f"extract_content try another pattern: {pattern}") - raw_content = copy.deepcopy(new_content + right_key) + if right_key not in new_content: + raw_content = copy.deepcopy(new_content + "\n" + right_key) # # pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" new_content = re_extract_content(raw_content, pattern) else: if right_key in new_content: idx = new_content.find(right_key) new_content = new_content[:idx] + new_content = new_content.strip() return new_content diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 8779c965c..553b57625 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -216,7 +216,8 @@ def test_extract_content_from_output(): 'UNCLEAR:\n\n* How to start the game.\n\nGreat job! This JSON output should provide a clear and ' \ 'comprehensive overview of the project\'s requirements and dependencies.' output = extract_content_from_output(output) - assert output.startswith('{\n"Required Python third-party packages') + assert output.startswith('{\n"Required Python third-party packages') and \ + output.endswith('UNCLEAR": "How to start the game."\n]') output = 'Sure, I would be happy to help! Here is the information you provided, formatted as a JSON object ' \ 'inside the [CONTENT] tag:\n\n[CONTENT]\n{\n"Original Requirements": "Create a 2048 game",\n"Search ' \ @@ -245,7 +246,8 @@ def test_extract_content_from_output(): '[/CONTENT]\n\nI hope this helps! Let me know if you have any further questions or if there anything ' \ 'else I can do to assist you.' output = extract_content_from_output(output) - assert output.startswith('{\n"Original Requirements"') + assert output.startswith('{\n"Original Requirements"') and \ + output.endswith('"Anything UNCLEAR": ""\n}') output = """ Sure, I'd be happy to help! Here's the JSON output for the given context:\n\n[CONTENT]\n{ "Implementation approach": "We will use the open-source framework PyGame to create a 2D game engine, which will @@ -270,4 +272,5 @@ def test_extract_content_from_output(): information for a developer to understand the design and implementation of the 2048 game. """ output = extract_content_from_output(output) - assert output.startswith('{\n"Implementation approach"') and "[/CONTENT]" not in output + assert output.startswith('{\n"Implementation approach"') and \ + output.endswith('"Anything UNCLEAR": "The requirement is clear to me."\n}') From 438fbe28c06aefee542bdd005941f801d4fe3e56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 11:29:09 +0800 Subject: [PATCH 141/413] refactor: save files --- metagpt/actions/design_api.py | 11 ++---- metagpt/actions/project_management.py | 54 +++++++++++++++++---------- metagpt/actions/write_prd.py | 23 +++++++----- metagpt/const.py | 1 + metagpt/utils/file_repository.py | 18 +++++++-- 5 files changed, 67 insertions(+), 40 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 2c8c87558..a8f89473d 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -290,7 +290,7 @@ class WriteDesign(Action): data_api_design = m.get("Data structures and interface definitions") if not data_api_design: return - pathname = CONFIG.git_repo.workdir / Path(DATA_API_DESIGN_FILE_REPO) / Path(design_doc).with_suffix(".mmd") + pathname = CONFIG.git_repo.workdir / Path(DATA_API_DESIGN_FILE_REPO) / Path(design_doc.filename).with_suffix("") await WriteDesign._save_mermaid_file(data_api_design, pathname) logger.info(f"Save class view to {str(pathname)}") @@ -300,19 +300,16 @@ class WriteDesign(Action): seq_flow = m.get("Program call flow") if not seq_flow: return - pathname = CONFIG.git_repo.workdir / Path(SEQ_FLOW_FILE_REPO) / Path(design_doc).with_suffix(".mmd") + pathname = CONFIG.git_repo.workdir / Path(SEQ_FLOW_FILE_REPO) / Path(design_doc.filename).with_suffix("") await WriteDesign._save_mermaid_file(seq_flow, pathname) logger.info(f"Saving sequence flow to {str(pathname)}") @staticmethod async def _save_pdf(design_doc): - m = json.loads(design_doc.content) file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_PDF_FILE_REPO) - await file_repo.save(filename=design_doc.filename, content=json_to_markdown(m)) - logger.info(f"Saving system design pdf to {design_doc.root_relative_path}") + await file_repo.save_pdf(doc=design_doc) @staticmethod async def _save_mermaid_file(data: str, pathname: Path): - if not pathname.parent.exists(): - pathname.parent.mkdir(parents=True, exists_ok=True) + pathname.parent.mkdir(parents=True, exist_ok=True) await mermaid_to_file(data, pathname) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 73481c780..686aa3689 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -11,7 +11,12 @@ from typing import List from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, WORKSPACE_ROOT +from metagpt.const import ( + SYSTEM_DESIGN_FILE_REPO, + TASK_FILE_REPO, + TASK_PDF_FILE_REPO, + WORKSPACE_ROOT, +) from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template @@ -190,46 +195,50 @@ class WriteTasks(Action): change_files = Documents() # 根据docs/system_designs/下的git head diff识别哪些task文档需要重写 for filename in changed_system_designs: - system_design_doc = await system_design_file_repo.get(filename) - task_doc = await tasks_file_repo.get(filename) - if task_doc: - task_doc = await self._merge(system_design_doc, task_doc) - else: - rsp = await self._run(system_design_doc.content) - task_doc = Document(root_path=TASK_FILE_REPO, filename=filename, content=rsp.instruct_content.json()) - await tasks_file_repo.save( - filename=filename, content=task_doc.content, dependencies={system_design_doc.root_relative_path} + task_doc = await self._update_tasks( + filename=filename, system_design_file_repo=system_design_file_repo, tasks_file_repo=tasks_file_repo ) - await self._update_requirements(task_doc) change_files.docs[filename] = task_doc # 根据docs/tasks/下的git head diff识别哪些task文件被用户修改了,需要重写 for filename in changed_tasks: if filename in change_files.docs: continue - system_design_doc = await system_design_file_repo.get(filename) - task_doc = await tasks_file_repo.get(filename) - task_doc = await self._merge(system_design_doc, task_doc) - await tasks_file_repo.save( - filename=filename, content=task_doc.content, dependencies={system_design_doc.root_relative_path} + task_doc = await self._update_tasks( + filename=filename, system_design_file_repo=system_design_file_repo, tasks_file_repo=tasks_file_repo ) - await self._update_requirements(task_doc) change_files.docs[filename] = task_doc # 等docs/tasks/下所有文件都处理完才发publish message,给后续做全局优化留空间。 return ActionOutput(content=change_files.json(), instruct_content=change_files) - async def _run(self, context, format=CONFIG.prompt_format): + async def _update_tasks(self, filename, system_design_file_repo, tasks_file_repo): + system_design_doc = await system_design_file_repo.get(filename) + task_doc = await tasks_file_repo.get(filename) + if task_doc: + task_doc = await self._merge(system_design_doc=system_design_doc, task_dock=task_doc) + else: + rsp = await self._run_new_tasks(context=system_design_doc.content) + task_doc = Document(root_path=TASK_FILE_REPO, filename=filename, content=rsp.instruct_content.json()) + await tasks_file_repo.save( + filename=filename, content=task_doc.content, dependencies={system_design_doc.root_relative_path} + ) + await self._update_requirements(task_doc) + await self._save_pdf(task_doc=task_doc) + return task_doc + + async def _run_new_tasks(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) - self._save(context, rsp) + # self._save(context, rsp) return rsp async def _merge(self, system_design_doc, task_dock) -> Document: return task_dock - async def _update_requirements(self, doc): + @staticmethod + async def _update_requirements(doc): m = json.loads(doc.content) packages = set(m.get("Required Python third-party packages", set())) file_repo = CONFIG.git_repo.new_file_repository() @@ -244,6 +253,11 @@ class WriteTasks(Action): packages.add(pkg) await file_repo.save(filename, content="\n".join(packages)) + @staticmethod + async def _save_pdf(task_doc): + file_repo = CONFIG.git_repo.new_file_repository(TASK_PDF_FILE_REPO) + await file_repo.save_pdf(doc=task_doc) + class AssignTasks(Action): async def run(self, *args, **kwargs): diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 34001dec1..8b03ac29a 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -25,7 +25,6 @@ from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template -from metagpt.utils.json_to_markdown import json_to_markdown from metagpt.utils.mermaid import mermaid_to_file templates = { @@ -245,13 +244,17 @@ class WritePRD(Action): prd_docs = await prds_file_repo.get_all() change_files = Documents() for prd_doc in prd_docs: - prd_doc = await self._update_prd(requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) + prd_doc = await self._update_prd( + requirement_doc=requirement_doc, prd_doc=prd_doc, prds_file_repo=prds_file_repo, *args, **kwargs + ) if not prd_doc: continue change_files.docs[prd_doc.filename] = prd_doc # 如果没有任何PRD,就使用docs/requirement.txt生成一个prd if not change_files.docs: - prd_doc = await self._update_prd(requirement_doc, None, prds_file_repo) + prd_doc = await self._update_prd( + requirement_doc=requirement_doc, prd_doc=None, prds_file_repo=prds_file_repo, *args, **kwargs + ) if prd_doc: change_files.docs[prd_doc.filename] = prd_doc # 等docs/prds/下所有文件都与新增需求对比完后,再触发publish message让工作流跳转到下一环节。如此设计是为了给后续做全局优化留空间。 @@ -283,9 +286,7 @@ class WritePRD(Action): async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: if not prd_doc: - prd = await self._run_new_requirement( - requirements=[requirement_doc.content], format=format, *args, **kwargs - ) + prd = await self._run_new_requirement(requirements=[requirement_doc.content], *args, **kwargs) new_prd_doc = Document( root_path=PRDS_FILE_REPO, filename=FileRepository.new_file_name() + ".json", @@ -298,6 +299,7 @@ class WritePRD(Action): await prds_file_repo.save(filename=new_prd_doc.filename, content=new_prd_doc.content) await self._save_competitive_analysis(new_prd_doc) await self._save_pdf(new_prd_doc) + return new_prd_doc @staticmethod async def _save_competitive_analysis(prd_doc): @@ -305,13 +307,14 @@ class WritePRD(Action): quadrant_chart = m.get("Competitive Quadrant Chart") if not quadrant_chart: return - pathname = CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc).with_suffix(".mmd") + pathname = ( + CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") + ) if not pathname.parent.exists(): - pathname.parent.mkdir(parents=True, exists_ok=True) + pathname.parent.mkdir(parents=True, exist_ok=True) await mermaid_to_file(quadrant_chart, pathname) @staticmethod async def _save_pdf(prd_doc): - m = json.loads(prd_doc.content) file_repo = CONFIG.git_repo.new_file_repository(PRD_PDF_FILE_REPO) - await file_repo.save(filename=prd_doc.filename, content=json_to_markdown(m)) + await file_repo.save_pdf(doc=prd_doc) diff --git a/metagpt/const.py b/metagpt/const.py index b5ecad7cc..7ee06ff7d 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -60,3 +60,4 @@ DATA_API_DESIGN_FILE_REPO = "resources/data_api_design" SEQ_FLOW_FILE_REPO = "resources/seq_flow" SYSTEM_DESIGN_PDF_FILE_REPO = "resources/system_design" PRD_PDF_FILE_REPO = "resources/prd" +TASK_PDF_FILE_REPO = "resources/api_spec_and_tasks" diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index ee6811209..62ba99d42 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -8,8 +8,8 @@ """ from __future__ import annotations +import json import os -import uuid from datetime import datetime from pathlib import Path from typing import Dict, List, Set @@ -18,6 +18,7 @@ import aiofiles from metagpt.logs import logger from metagpt.schema import Document +from metagpt.utils.json_to_markdown import json_to_markdown class FileRepository: @@ -165,5 +166,16 @@ class FileRepository: :return: A new filename string. """ current_time = datetime.now().strftime("%Y%m%d%H%M%S") - guid_suffix = str(uuid.uuid4())[:8] - return f"{current_time}x{guid_suffix}" + return current_time + # guid_suffix = str(uuid.uuid4())[:8] + # return f"{current_time}x{guid_suffix}" + + async def save_pdf(self, doc: Document): + """Save a Document as a PDF file. + + :param doc: The Document instance to be saved. + """ + m = json.loads(doc.content) + filename = Path(doc.filename).with_suffix(".md") + await self.save(filename=str(filename), content=json_to_markdown(m)) + logger.info(f"File Saved: {str(filename)}") From c25d5a73d16c4977e4dac630279d650d5e34db53 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 11:53:11 +0800 Subject: [PATCH 142/413] add ut test_repair_invalid_json --- metagpt/utils/repair_llm_raw_output.py | 18 ++++---- .../utils/test_repair_llm_raw_output.py | 42 +++++++++++++++++++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 0b521a7b0..f9e6d020d 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -182,19 +182,23 @@ def repair_invalid_json(output: str, error: str) -> str: # different general problems if line.endswith("],"): # problem, redundant char `]` - line = line.replace("]", "") + new_line = line.replace("]", "") elif line.endswith("},") and not output.endswith("},"): # problem, redundant char `}` - line = line.replace("}", "") + new_line = line.replace("}", "") elif line.endswith("},") and output.endswith("},"): - line = line[:-1] - elif '",' not in line: - line = f'{line}",' + new_line = line[:-1] + elif '",' not in line and ',' not in line: + new_line = f'{line}",' elif "," not in line: # problem, miss char `,` at the end. - line = f"{line}," + new_line = f"{line}," + elif "," in line and len(line) == 1: + new_line = f'"{line}' + elif '",' in line: + new_line = line[:-2] + "'," - arr[line_no] = line + arr[line_no] = new_line output = "\n".join(arr) logger.info(f"repair_invalid_json, raw error: {error}") diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index 553b57625..acacb3af3 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -132,6 +132,48 @@ def test_repair_json_format(): assert output == target_output +def test_repair_invalid_json(): + raw_output = """{ + "key": "value" + }, +}""" + target_output = """{ + "key": "value" +, +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 3 column 1") + assert output == target_output + + raw_output = """{ + "key": " +value + }, +}""" + target_output = """{ + "key": " +value +", +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + assert output == target_output + + raw_output = """{ + "key": ' +value + }, +}""" + target_output = """{ + "key": ' +value +', +}""" + output = repair_invalid_json(raw_output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + output = repair_invalid_json(output, "Expecting ',' delimiter: line 4 column 1") + assert output == target_output + + def test_retry_parse_json_text(): invalid_json_text = """{ "Original Requirements": "Create a 2048 game", From 56b75b34ddd74013187a50630b92fe7bede0a40d Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 23 Nov 2023 13:38:33 +0800 Subject: [PATCH 143/413] rm not fully ready claude_api --- metagpt/llm.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index 1f7d1b4c9..7b490ec4a 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -21,8 +21,6 @@ def LLM() -> "BaseGPTAPI": # TODO a little trick, can use registry to initialize LLM instance further if CONFIG.openai_api_key: llm = OpenAIGPTAPI() - elif CONFIG.claude_api_key: - llm = Claude() elif CONFIG.spark_api_key: llm = SparkAPI() elif CONFIG.zhipuai_api_key: From 0fdeab3f200f4a5cdb0b6c8427373f4f037d6ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 17:36:01 +0800 Subject: [PATCH 144/413] fixbug: Message was incorrectly filtered by the profile. --- metagpt/roles/qa_engineer.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 760b65736..b57b64a7e 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -151,13 +151,6 @@ class QaEngineer(Role): ) self.publish_message(msg) - async def _observe(self) -> int: - await super()._observe() - self._rc.news = [ - msg for msg in self._rc.news if self.profile in msg.send_to - ] # only relevant msgs count as observed news - return len(self._rc.news) - async def _act(self) -> Message: if self.test_round > self.test_round_allowed: result_msg = Message( From 2032a385426e44fea0154a292aa8e4b1e1a9be59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 17:49:38 +0800 Subject: [PATCH 145/413] feat: rewrite Engineer & WriteCode & WriteCodeReview --- metagpt/actions/write_code.py | 24 ++- metagpt/actions/write_code_review.py | 25 ++- metagpt/config.py | 1 + metagpt/roles/engineer.py | 266 +++++++++++---------------- metagpt/roles/qa_engineer.py | 7 - metagpt/schema.py | 7 + startup.py | 2 + 7 files changed, 152 insertions(+), 180 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index aeaa10aec..d4d33fe0c 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -7,13 +7,15 @@ @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `cause_by` value of the `Message` object. """ +import json + from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions import WriteDesign from metagpt.actions.action import Action from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger -from metagpt.schema import Message +from metagpt.schema import CodingContext from metagpt.utils.common import CodeParser, any_to_str PROMPT_TEMPLATE = """ @@ -46,7 +48,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): - def __init__(self, name="WriteCode", context: list[Message] = None, llm=None): + def __init__(self, name="WriteCode", context=None, llm=None): super().__init__(name, context, llm) def _is_invalid(self, filename): @@ -70,15 +72,19 @@ class WriteCode(Action): logger.info(f"Saving Code to {code_path}") @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) - async def write_code(self, prompt): + async def write_code(self, prompt) -> str: code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) return code - async def run(self, context, filename): - prompt = PROMPT_TEMPLATE.format(context=context, filename=filename) - logger.info(f"Writing {filename}..") + async def run(self, *args, **kwargs) -> CodingContext: + m = json.loads(self.context.content) + coding_context = CodingContext(**m) + context = "\n".join( + [coding_context.design_doc.content, coding_context.task_doc.content, coding_context.code_doc.content] + ) + prompt = PROMPT_TEMPLATE.format(context=context, filename=self.context.filename) + logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) - # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) - # self._save(context, filename, code) - return code + coding_context.code_doc.content = code + return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 76adca255..10e4aec3b 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -10,7 +10,7 @@ from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.logs import logger -from metagpt.schema import Message +from metagpt.schema import CodingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -63,7 +63,7 @@ FORMAT_EXAMPLE = """ class WriteCodeReview(Action): - def __init__(self, name="WriteCodeReview", context: list[Message] = None, llm=None): + def __init__(self, name="WriteCodeReview", context=None, llm=None): super().__init__(name, context, llm) @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) @@ -72,11 +72,18 @@ class WriteCodeReview(Action): code = CodeParser.parse_code(block="", text=code_rsp) return code - async def run(self, context, code, filename): - format_example = FORMAT_EXAMPLE.format(filename=filename) - prompt = PROMPT_TEMPLATE.format(context=context, code=code, filename=filename, format_example=format_example) - logger.info(f"Code review {filename}..") + async def run(self, *args, **kwargs) -> CodingContext: + format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) + context = "\n".join( + [self.context.design_doc.content, self.context.task_doc.content, self.context.code_doc.content] + ) + prompt = PROMPT_TEMPLATE.format( + context=context, + code=self.context.code_doc.content, + filename=self.context.code_doc.filename, + format_example=format_example, + ) + logger.info(f"Code review {self.context.code_doc.filename}..") code = await self.write_code(prompt) - # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) - # self._save(context, filename, code) - return code + self.context.code_doc.content = code + return self.context diff --git a/metagpt/config.py b/metagpt/config.py index 51eed4fb8..d059a6a29 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -94,6 +94,7 @@ class Config(metaclass=Singleton): self.prompt_format = self._get("PROMPT_FORMAT", "markdown") self.git_repo = None + self.src_workspace = None def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index d23d23d55..8852d55f1 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -11,47 +11,20 @@ 3. Fix bug: Add logic for handling asynchronous message processing when messages are not ready. 4. Supplemented the external transmission of internal messages. """ -import asyncio -import shutil -from collections import OrderedDict +from __future__ import annotations + +import json from pathlib import Path -from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks -from metagpt.const import WORKSPACE_ROOT +from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks +from metagpt.config import CONFIG +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role -from metagpt.schema import Message -from metagpt.utils.common import CodeParser, any_to_str +from metagpt.schema import CodingContext, Document, Documents, Message from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP -async def gather_ordered_k(coros, k) -> list: - tasks = OrderedDict() - results = [None] * len(coros) - done_queue = asyncio.Queue() - - for i, coro in enumerate(coros): - if len(tasks) >= k: - done, _ = await asyncio.wait(tasks.keys(), return_when=asyncio.FIRST_COMPLETED) - for task in done: - index = tasks.pop(task) - await done_queue.put((index, task.result())) - task = asyncio.create_task(coro) - tasks[task] = i - - if tasks: - done, _ = await asyncio.wait(tasks.keys()) - for task in done: - index = tasks[task] - await done_queue.put((index, task.result())) - - while not done_queue.empty(): - index, result = await done_queue.get() - results[index] = result - - return results - - class Engineer(Role): """ Represents an Engineer role responsible for writing and possibly reviewing code. @@ -77,105 +50,19 @@ class Engineer(Role): ) -> None: """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) - self._init_actions([WriteCode]) self.use_code_review = use_code_review - if self.use_code_review: - self._init_actions([WriteCode, WriteCodeReview]) self._watch([WriteTasks]) self.todos = [] self.n_borg = n_borg - @classmethod - def parse_tasks(self, task_msg: Message) -> list[str]: - if task_msg.instruct_content: - return task_msg.instruct_content.dict().get("Task list") - return CodeParser.parse_file_list(block="Task list", text=task_msg.content) + @staticmethod + def _parse_tasks(task_msg: Document) -> list[str]: + m = json.loads(task_msg.content) + return m.get("Task list") - @classmethod - def parse_code(self, code_text: str) -> str: - return CodeParser.parse_code(block="", text=code_text) - - @classmethod - def parse_workspace(cls, system_design_msg: Message) -> str: - if system_design_msg.instruct_content: - return system_design_msg.instruct_content.dict().get("Python package name").strip().strip("'").strip('"') - return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) - - def get_workspace(self) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] - if not msg: - return WORKSPACE_ROOT / "src" - workspace = self.parse_workspace(msg) - # Codes are written in workspace/{package_name}/{package_name} - return WORKSPACE_ROOT / workspace / workspace - - def recreate_workspace(self): - workspace = self.get_workspace() - try: - shutil.rmtree(workspace) - except FileNotFoundError: - pass # The folder does not exist, but we don't care - workspace.mkdir(parents=True, exist_ok=True) - - def write_file(self, filename: str, code: str): - workspace = self.get_workspace() - filename = filename.replace('"', "").replace("\n", "") - file = workspace / filename - file.parent.mkdir(parents=True, exist_ok=True) - file.write_text(code) - return file - - async def _act_mp(self) -> Message: - # self.recreate_workspace() - todo_coros = [] - for todo in self.todos: - todo_coro = WriteCode().run( - context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), - filename=todo, - ) - todo_coros.append(todo_coro) - - rsps = await gather_ordered_k(todo_coros, self.n_borg) - for todo, code_rsp in zip(self.todos, rsps): - _ = self.parse_code(code_rsp) - logger.info(todo) - logger.info(code_rsp) - # self.write_file(todo, code) - msg = Message(content=code_rsp, role=self.profile, cause_by=self._rc.todo) - self._rc.memory.add(msg) - self.publish_message(msg) - del self.todos[0] - - logger.info(f"Done {self.get_workspace()} generating.") - msg = Message(content="all done.", role=self.profile, cause_by=self._rc.todo) - return msg - - async def _act_sp(self) -> Message: - code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later - for todo in self.todos: - code = await WriteCode().run(context=self._rc.history, filename=todo) - # logger.info(todo) - # logger.info(code_rsp) - # code = self.parse_code(code_rsp) - file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=self._rc.todo) - self._rc.memory.add(msg) - self.publish_message(msg) - - code_msg = todo + FILENAME_CODE_SEP + str(file_path) - code_msg_all.append(code_msg) - - logger.info(f"Done {self.get_workspace()} generating.") - msg = Message( - content=MSG_SEP.join(code_msg_all), - role=self.profile, - cause_by=self._rc.todo, - send_to="Edward", - ) - return msg - - async def _act_sp_precision(self) -> Message: + async def _act_sp_precision(self, review=False) -> Message: code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later + src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) for todo in self.todos: """ # Select essential information from the historical data to reduce the length of the prompt (summarized from human experience): @@ -184,30 +71,29 @@ class Engineer(Role): 3. Do we need other codes (currently needed)? TODO: The goal is not to need it. After clear task decomposition, based on the design idea, you should be able to write a single file without needing other codes. If you can't, it means you need a clearer definition. This is the key to writing longer code. """ - context = [] - msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) - for m in msg: - context.append(m.content) - context_str = "\n".join(context) - # Write code - code = await WriteCode().run(context=context_str, filename=todo) + coding_context = await todo.run() # Code review - if self.use_code_review: + if review: try: - rewrite_code = await WriteCodeReview().run(context=context_str, code=code, filename=todo) - code = rewrite_code + coding_context = await WriteCodeReview(context=coding_context, llm=self._llm).run() except Exception as e: logger.error("code review failed!", e) pass - file_path = self.write_file(todo, code) - msg = Message(content=code, role=self.profile, cause_by=WriteCode) + await src_file_repo.save( + coding_context.filename, + dependencies={coding_context.design_doc.root_relative_path, coding_context.task_doc.root_relative_path}, + content=coding_context.code_doc.content, + ) + msg = Message( + content=coding_context.json(), instruct_content=coding_context, role=self.profile, cause_by=WriteCode + ) self._rc.memory.add(msg) self.publish_message(msg) - code_msg = todo + FILENAME_CODE_SEP + str(file_path) + code_msg = coding_context.filename + FILENAME_CODE_SEP + str(coding_context.code_doc.root_relative_path) code_msg_all.append(code_msg) - logger.info(f"Done {self.get_workspace()} generating.") + logger.info(f"Done {CONFIG.src_workspace} generating.") msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, @@ -218,22 +104,92 @@ class Engineer(Role): async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" - if not self._rc.todo: - return None - if self.use_code_review: - return await self._act_sp_precision() - return await self._act_sp() + return await self._act_sp_precision(review=self.use_code_review) - async def _observe(self) -> int: - ret = await super(Engineer, self)._observe() - if ret == 0: - return ret + async def _think(self) -> Action | None: + if not CONFIG.src_workspace: + CONFIG.src_workspace = CONFIG.git_repo.workdir / CONFIG.git_repo.workdir.name + # Prepare file repos + src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + changed_src_files = src_file_repo.changed_files + task_file_repo = CONFIG.git_repo.new_file_repository(TASK_FILE_REPO) + changed_task_files = task_file_repo.changed_files + design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) - # Parse task lists - for message in self._rc.news: - if not message.cause_by == any_to_str(WriteTasks): + changed_files = Documents() + # 由上游变化导致的recode + for filename in changed_task_files: + design_doc = await design_file_repo.get(filename) + task_doc = await task_file_repo.get(filename) + task_list = self._parse_tasks(task_doc) + for task_filename in task_list: + old_code_doc = await src_file_repo.get(task_filename) + if not old_code_doc: + old_code_doc = Document(root_path=str(src_file_repo.root_path), filename=task_filename, content="") + context = CodingContext( + filename=task_filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc + ) + coding_doc = Document( + root_path=str(src_file_repo.root_path), filename=task_filename, content=context.json() + ) + if task_filename in changed_files.docs: + logger.error( + f"Log to expose potential file name conflicts: {coding_doc.json()} & " + f"{changed_files.docs[task_filename].json()}" + ) + changed_files.docs[task_filename] = coding_doc + self.todos = [WriteCode(context=i, llm=self._llm) for i in changed_files.docs.values()] + # 用户直接修改的code + dependency = await CONFIG.git_repo.get_dependency() + for filename in changed_src_files: + if filename in changed_files.docs: continue - self.todos = self.parse_tasks(message) - return 1 + coding_doc = await self._new_coding_doc( + filename=filename, + src_file_repo=src_file_repo, + task_file_repo=task_file_repo, + design_file_repo=design_file_repo, + dependency=dependency, + ) + changed_files.docs[filename] = coding_doc + self.todos.append(WriteCode(context=coding_doc, llm=self._llm)) + # 仅单测 + if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files.docs: + context = await self._new_coding_context( + filename=CONFIG.REQA_FILENAME, + src_file_repo=src_file_repo, + task_file_repo=task_file_repo, + design_file_repo=design_file_repo, + dependency=dependency, + ) + self.publish_message(Message(content=context.json(), instruct_content=context, cause_by=WriteCode)) - return 0 + if self.todos: + self._rc.todo = self.todos[0] + return self._rc.todo # For agent store + + @staticmethod + async def _new_coding_context( + filename, src_file_repo, task_file_repo, design_file_repo, dependency + ) -> CodingContext: + old_code_doc = await src_file_repo.get(filename) + if not old_code_doc: + old_code_doc = Document(root_path=str(src_file_repo.root_path), filename=filename, content="") + dependencies = {Path(i) for i in dependency.get(old_code_doc.root_relative_path)} + task_doc = None + design_doc = None + for i in dependencies: + if str(i.parent) == TASK_FILE_REPO: + task_doc = task_file_repo.get(i.filename) + elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO: + design_doc = design_file_repo.get(i.filename) + context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc) + return context + + @staticmethod + async def _new_coding_doc(filename, src_file_repo, task_file_repo, design_file_repo, dependency): + context = await Engineer._new_coding_context( + filename, src_file_repo, task_file_repo, design_file_repo, dependency + ) + coding_doc = Document(root_path=str(src_file_repo.root_path), filename=filename, content=context.json()) + return coding_doc diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 760b65736..b57b64a7e 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -151,13 +151,6 @@ class QaEngineer(Role): ) self.publish_message(msg) - async def _observe(self) -> int: - await super()._observe() - self._rc.news = [ - msg for msg in self._rc.news if self.profile in msg.send_to - ] # only relevant msgs count as observed news - return len(self._rc.news) - async def _act(self) -> Message: if self.test_round > self.test_round_allowed: result_msg = Message( diff --git a/metagpt/schema.py b/metagpt/schema.py index 674091e4c..6a707af3e 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -238,3 +238,10 @@ class MessageQueue: logger.warning(f"JSON load failed: {v}, error:{e}") return q + + +class CodingContext(BaseModel): + filename: str + design_doc: Document + task_doc: Document + code_doc: Document diff --git a/startup.py b/startup.py index d5a6bb07b..1a59e7fa2 100644 --- a/startup.py +++ b/startup.py @@ -56,6 +56,7 @@ def main( run_tests: bool = False, implement: bool = True, project_path: str = None, + reqa_file: str = None, ): """ We are a software startup comprised of AI. By investing in us, @@ -71,6 +72,7 @@ def main( :return: """ CONFIG.WORKDIR = project_path + CONFIG.REQA_FILENAME = reqa_file asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement)) From 66cff9023fe0bd27843b0754110e7d8bda902a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 18:10:44 +0800 Subject: [PATCH 146/413] fixbug: Delete the incorrect message. --- metagpt/roles/engineer.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index d23d23d55..c0e1b8a10 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -143,7 +143,6 @@ class Engineer(Role): # self.write_file(todo, code) msg = Message(content=code_rsp, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) - self.publish_message(msg) del self.todos[0] logger.info(f"Done {self.get_workspace()} generating.") @@ -160,7 +159,6 @@ class Engineer(Role): file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) - self.publish_message(msg) code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) @@ -202,7 +200,6 @@ class Engineer(Role): file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=WriteCode) self._rc.memory.add(msg) - self.publish_message(msg) code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) From 9a3c92ed1192387f28eee11fcca3e08b737f7fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 21:38:39 +0800 Subject: [PATCH 147/413] fixbug: send useless message to nobody from QaEngineer --- metagpt/roles/qa_engineer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index b57b64a7e..23c8d1fdd 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -100,8 +100,8 @@ class QaEngineer(Role): content=str(file_info), role=self.profile, cause_by=WriteTest, - sent_from=self.profile, - send_to=self.profile, + sent_from=self, + send_to=self, ) self.publish_message(msg) @@ -182,5 +182,6 @@ class QaEngineer(Role): role=self.profile, cause_by=WriteTest, sent_from=self.profile, + send_to="" ) return result_msg From 3e8bba70bcd20fda71151f3b526171d96818cdf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 21:38:51 +0800 Subject: [PATCH 148/413] fixbug: send useless message to nobody from QaEngineer --- metagpt/roles/qa_engineer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 23c8d1fdd..59a4135b8 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -146,8 +146,8 @@ class QaEngineer(Role): content=file_info, role=self.profile, cause_by=DebugError, - sent_from=self.profile, - send_to=recipient, + sent_from=self, + send_to=self, ) self.publish_message(msg) @@ -158,6 +158,7 @@ class QaEngineer(Role): role=self.profile, cause_by=WriteTest, sent_from=self.profile, + send_to="" ) return result_msg From ec3dd004af6000ec44ec6bb2cd6ed49d39e09ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 22:41:44 +0800 Subject: [PATCH 149/413] feat: Change the operation of transmitting file content during the QA process to transmitting file names instead. --- metagpt/actions/debug_error.py | 12 +-- metagpt/actions/run_code.py | 24 ++--- metagpt/actions/write_test.py | 16 +-- metagpt/const.py | 2 + metagpt/roles/engineer.py | 45 ++++---- metagpt/roles/qa_engineer.py | 178 +++++++++++++++----------------- metagpt/schema.py | 26 +++++ metagpt/utils/git_repository.py | 7 +- 8 files changed, 159 insertions(+), 151 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 304b1bc3e..a55f13dad 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -5,7 +5,6 @@ @Author : alexanderwu @File : debug_error.py """ -import re from metagpt.actions.action import Action from metagpt.logs import logger @@ -36,18 +35,17 @@ class DebugError(Action): # fixed_code = await self._aask(prompt) # return fixed_code - async def run(self, context): - if "PASS" in context: + async def run(self, *args, **kwargs) -> str: + if "PASS" in self.context.output: return "", "the original code works fine, no need to debug" - file_name = re.search("## File To Rewrite:\s*(.+\\.py)", context).group(1) - + file_name = self.context.code_filename logger.info(f"Debug and rewrite {file_name}") - prompt = PROMPT_TEMPLATE.format(context=context) + prompt = PROMPT_TEMPLATE.format(context=self.context.output) rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=rsp) - return file_name, code + return code diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index f69d2cd1a..f2d323f06 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -98,24 +98,22 @@ class RunCode(Action): stdout, stderr = process.communicate() return stdout.decode("utf-8"), stderr.decode("utf-8") - async def run( - self, code, mode="script", code_file_name="", test_code="", test_file_name="", command=[], **kwargs - ) -> str: - logger.info(f"Running {' '.join(command)}") - if mode == "script": - outs, errs = await self.run_script(command=command, **kwargs) - elif mode == "text": - outs, errs = await self.run_text(code=code) + async def run(self, *args, **kwargs) -> str: + logger.info(f"Running {' '.join(self.context.command)}") + if self.context.mode == "script": + outs, errs = await self.run_script(command=self.context.command, **kwargs) + elif self.context.mode == "text": + outs, errs = await self.run_text(code=self.context.code) logger.info(f"{outs=}") logger.info(f"{errs=}") context = CONTEXT.format( - code=code, - code_file_name=code_file_name, - test_code=test_code, - test_file_name=test_file_name, - command=" ".join(command), + code=self.context.code, + code_file_name=self.context.code_filename, + test_code=self.context.test_code, + test_file_name=self.context.test_filename, + command=" ".join(self.context.command), outs=outs[:500], # outs might be long but they are not important, truncate them to avoid token overflow errs=errs[:10000], # truncate errors to avoid token overflow ) diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 35ff36dc2..9a9671bab 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -6,7 +6,9 @@ @File : environment.py """ from metagpt.actions.action import Action +from metagpt.config import CONFIG from metagpt.logs import logger +from metagpt.schema import TestingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -47,12 +49,12 @@ class WriteTest(Action): code = code_rsp return code - async def run(self, code_to_test, test_file_name, source_file_path, workspace): + async def run(self, *args, **kwargs) -> TestingContext: prompt = PROMPT_TEMPLATE.format( - code_to_test=code_to_test, - test_file_name=test_file_name, - source_file_path=source_file_path, - workspace=workspace, + code_to_test=self.context.code_doc.content, + test_file_name=self.context.test_doc.filename, + source_file_path=self.context.code_doc.root_relative_path, + workspace=CONFIG.git_repo.workdir, ) - code = await self.write_code(prompt) - return code + self.context.test_doc.content = await self.write_code(prompt) + return self.context diff --git a/metagpt/const.py b/metagpt/const.py index 7ee06ff7d..e97ffdb7d 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -61,3 +61,5 @@ SEQ_FLOW_FILE_REPO = "resources/seq_flow" SYSTEM_DESIGN_PDF_FILE_REPO = "resources/system_design" PRD_PDF_FILE_REPO = "resources/prd" TASK_PDF_FILE_REPO = "resources/api_spec_and_tasks" +TEST_CODES_FILE_REPO = "tests" +OUTPUTS_FILE_REPO = "outputs" diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 8852d55f1..89827a1ca 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -15,6 +15,7 @@ from __future__ import annotations import json from pathlib import Path +from typing import Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.config import CONFIG @@ -22,7 +23,6 @@ from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import CodingContext, Document, Documents, Message -from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP class Engineer(Role): @@ -60,8 +60,8 @@ class Engineer(Role): m = json.loads(task_msg.content) return m.get("Task list") - async def _act_sp_precision(self, review=False) -> Message: - code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later + async def _act_sp_precision(self, review=False) -> Set[str]: + changed_files = set() src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) for todo in self.todos: """ @@ -88,23 +88,26 @@ class Engineer(Role): content=coding_context.json(), instruct_content=coding_context, role=self.profile, cause_by=WriteCode ) self._rc.memory.add(msg) - self.publish_message(msg) - code_msg = coding_context.filename + FILENAME_CODE_SEP + str(coding_context.code_doc.root_relative_path) - code_msg_all.append(code_msg) - - logger.info(f"Done {CONFIG.src_workspace} generating.") - msg = Message( - content=MSG_SEP.join(code_msg_all), - role=self.profile, - cause_by=self._rc.todo, - send_to="Edward", - ) - return msg + changed_files.add(coding_context.code_doc.filename) + return changed_files async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" - return await self._act_sp_precision(review=self.use_code_review) + changed_files = await self._act_sp_precision(review=self.use_code_review) + # 仅单测 + if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files: + changed_files.add(CONFIG.REQA_FILENAME) + + from metagpt.roles import QaEngineer # 避免循环引用 + + msg = Message( + content="\n".join(changed_files), + role=self.profile, + cause_by=WriteCodeReview if self.use_code_review else WriteCode, + send_to=QaEngineer, + ) + return msg async def _think(self) -> Action | None: if not CONFIG.src_workspace: @@ -153,16 +156,6 @@ class Engineer(Role): ) changed_files.docs[filename] = coding_doc self.todos.append(WriteCode(context=coding_doc, llm=self._llm)) - # 仅单测 - if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files.docs: - context = await self._new_coding_context( - filename=CONFIG.REQA_FILENAME, - src_file_repo=src_file_repo, - task_file_repo=task_file_repo, - design_file_repo=design_file_repo, - dependency=dependency, - ) - self.publish_message(Message(content=context.json(), instruct_content=context, cause_by=WriteCode)) if self.todos: self._rc.todo = self.todos[0] diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index b57b64a7e..1520a830a 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -7,23 +7,15 @@ @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data type of the `cause_by` value in the `Message` to a string, and utilize the new message filtering feature. """ -import os -from pathlib import Path +import json -from metagpt.actions import ( - DebugError, - RunCode, - WriteCode, - WriteCodeReview, - WriteDesign, - WriteTest, -) -from metagpt.const import WORKSPACE_ROOT +from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest +from metagpt.config import CONFIG +from metagpt.const import OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role -from metagpt.schema import Message -from metagpt.utils.common import CodeParser, any_to_str_set, parse_recipient -from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP +from metagpt.schema import Document, Message, RunCodeContext, TestingContext +from metagpt.utils.common import CodeParser, any_to_str_set class QaEngineer(Role): @@ -49,107 +41,98 @@ class QaEngineer(Role): return system_design_msg.instruct_content.dict().get("Python package name") return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) - def get_workspace(self, return_proj_dir=True) -> Path: - msg = self._rc.memory.get_by_action(WriteDesign)[-1] - if not msg: - return WORKSPACE_ROOT / "src" - workspace = self.parse_workspace(msg) - # project directory: workspace/{package_name}, which contains package source code folder, tests folder, resources folder, etc. - if return_proj_dir: - return WORKSPACE_ROOT / workspace - # development codes directory: workspace/{package_name}/{package_name} - return WORKSPACE_ROOT / workspace / workspace - - def write_file(self, filename: str, code: str): - workspace = self.get_workspace() / "tests" - file = workspace / filename - file.parent.mkdir(parents=True, exist_ok=True) - file.write_text(code) - async def _write_test(self, message: Message) -> None: - code_msgs = message.content.split(MSG_SEP) - # result_msg_all = [] - for code_msg in code_msgs: + changed_files = message.content.splitlines() + src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + tests_file_repo = CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO) + for filename in changed_files: # write tests - file_name, file_path = code_msg.split(FILENAME_CODE_SEP) - code_to_test = open(file_path, "r").read() - if "test" in file_name: - continue # Engineer might write some test files, skip testing a test file - test_file_name = "test_" + file_name - test_file_path = self.get_workspace() / "tests" / test_file_name - logger.info(f"Writing {test_file_name}..") - test_code = await WriteTest().run( - code_to_test=code_to_test, - test_file_name=test_file_name, - # source_file_name=file_name, - source_file_path=file_path, - workspace=self.get_workspace(), + if not filename or "test" in filename: + continue + code_doc = await src_file_repo.get(filename) + test_doc = await tests_file_repo.get("test_" + code_doc.filename) + if not test_doc: + test_doc = Document( + root_path=str(tests_file_repo.root_path), filename="test_" + code_doc.filename, content="" + ) + logger.info(f"Writing {test_doc.filename}..") + context = TestingContext(filename=test_doc.filename, test_doc=test_doc, code_doc=code_doc) + context = await WriteTest(context=context, llm=self._llm).run() + await tests_file_repo.save( + filename=context.test_doc.filename, + content=context.test_doc.content, + dependencies={context.code_doc.root_relative_path}, ) - self.write_file(test_file_name, test_code) # prepare context for run tests in next round - command = ["python", f"tests/{test_file_name}"] - file_info = { - "file_name": file_name, - "file_path": str(file_path), - "test_file_name": test_file_name, - "test_file_path": str(test_file_path), - "command": command, - } + run_code_context = RunCodeContext( + command=["python", context.test_doc.root_relative_path], + code_filename=context.code_doc.filename, + test_filename=context.test_doc.filename, + working_directory=str(CONFIG.git_repo.workdir), + additional_python_paths=[CONFIG.src_workspace], + ) + msg = Message( - content=str(file_info), + content=run_code_context.json(), role=self.profile, cause_by=WriteTest, - sent_from=self.profile, - send_to=self.profile, + sent_from=self, + send_to=self, ) self.publish_message(msg) - logger.info(f"Done {self.get_workspace()}/tests generating.") + logger.info(f"Done {str(tests_file_repo.workdir)} generating.") async def _run_code(self, msg): - file_info = eval(msg.content) - development_file_path = file_info["file_path"] - test_file_path = file_info["test_file_path"] - if not os.path.exists(development_file_path) or not os.path.exists(test_file_path): + m = json.loads(msg.content) + run_code_context = RunCodeContext(**m) + src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + src_doc = await src_file_repo.get(run_code_context.code_filename) + if not src_doc: return - - development_code = open(development_file_path, "r").read() - test_code = open(test_file_path, "r").read() - proj_dir = self.get_workspace() - development_code_dir = self.get_workspace(return_proj_dir=False) - - result_msg = await RunCode().run( - mode="script", - code=development_code, - code_file_name=file_info["file_name"], - test_code=test_code, - test_file_name=file_info["test_file_name"], - command=file_info["command"], - working_directory=proj_dir, # workspace/package_name, will run tests/test_xxx.py here - additional_python_paths=[development_code_dir], # workspace/package_name/package_name, - # import statement inside package code needs this + test_file_repo = CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO) + test_doc = await test_file_repo.get(run_code_context.test_filename) + if not test_doc: + return + run_code_context.code = src_doc.content + run_code_context.test_code = test_doc.content + result_msg = await RunCode(context=run_code_context, llm=self._llm).run() + outputs_file_repo = CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO) + run_code_context.output_filename = run_code_context.test_filename + ".log" + await outputs_file_repo.save( + filename=run_code_context.output_filename, + content=result_msg, + dependencies={src_doc.root_relative_path, test_doc.root_relative_path}, + ) + run_code_context.code = None + run_code_context.test_code = None + msg = Message( + content=run_code_context.json(), role=self.profile, cause_by=RunCode, sent_from=self, send_to=self ) - - recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself - content = str(file_info) + FILENAME_CODE_SEP + result_msg - msg = Message(content=content, role=self.profile, cause_by=RunCode, sent_from=self.profile, send_to=recipient) self.publish_message(msg) async def _debug_error(self, msg): - file_info, context = msg.content.split(FILENAME_CODE_SEP) - file_name, code = await DebugError().run(context) - if file_name: - self.write_file(file_name, code) - recipient = msg.sent_from # send back to the one who ran the code for another run, might be one's self - msg = Message( - content=file_info, - role=self.profile, - cause_by=DebugError, - sent_from=self.profile, - send_to=recipient, - ) - self.publish_message(msg) + m = json.loads(msg.context) + run_code_context = RunCodeContext(**m) + output_file_repo = CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO) + output_doc = await output_file_repo.get(run_code_context.output_filename) + if not output_doc: + return + run_code_context.output = output_doc.content + code = await DebugError(context=run_code_context, llm=self._llm).run() + src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + await src_file_repo.save(filename=run_code_context.code_filename, content=code) + run_code_context.output = None + run_code_context.output_filename = None + msg = Message( + content=run_code_context.json(), + role=self.profile, + cause_by=DebugError, + sent_from=self, + send_to=self, + ) + self.publish_message(msg) async def _act(self) -> Message: if self.test_round > self.test_round_allowed: @@ -182,5 +165,6 @@ class QaEngineer(Role): role=self.profile, cause_by=WriteTest, sent_from=self.profile, + send_to="", ) return result_msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 6a707af3e..5cc7cdb2d 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -19,6 +19,7 @@ from typing import Dict, List, Optional, Set, TypedDict from pydantic import BaseModel, Field +from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, MESSAGE_ROUTE_FROM, @@ -59,6 +60,12 @@ class Document(BaseModel): """ return os.path.join(self.root_path, self.filename) + @property + def full_path(self): + if not CONFIG.git_repo: + return None + return str(CONFIG.git_repo.workdir / self.root_path / self.filename) + class Documents(BaseModel): """A class representing a collection of documents. @@ -245,3 +252,22 @@ class CodingContext(BaseModel): design_doc: Document task_doc: Document code_doc: Document + + +class TestingContext(BaseModel): + filename: str + code_doc: Document + test_doc: Document + + +class RunCodeContext(BaseModel): + mode: str = "script" + code: Optional[str] + code_filename: str = "" + test_code: Optional[str] + test_filename: str = "" + command: List[str] = Field(default_factory=list) + working_directory: str = "" + additional_python_paths: List[str] = Field(default_factory=list) + output_filename: Optional[str] + output: Optional[str] diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 2a4fb4a4d..c2eb2360e 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -160,7 +160,12 @@ class GitRepository: :param relative_path: The relative path to the file repository within the Git repository. :return: A new instance of FileRepository. """ - return FileRepository(git_repo=self, relative_path=Path(relative_path)) + path = Path(relative_path) + try: + path = path.relative_to(self.workdir) + except ValueError: + path = relative_path + return FileRepository(git_repo=self, relative_path=Path(path)) async def get_dependency(self) -> DependencyFile: """Get the dependency file associated with the Git repository. From 6d77cd89c86c4dd01007933e2f19352ef32d7dbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 22:54:22 +0800 Subject: [PATCH 150/413] refactor: delete useless codes --- metagpt/roles/qa_engineer.py | 8 +------- metagpt/roles/role.py | 4 ---- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 7a2b7cbd4..6f0738294 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -15,7 +15,7 @@ from metagpt.const import OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext -from metagpt.utils.common import CodeParser, any_to_str_set +from metagpt.utils.common import any_to_str_set class QaEngineer(Role): @@ -35,12 +35,6 @@ class QaEngineer(Role): self.test_round = 0 self.test_round_allowed = test_round_allowed - @classmethod - def parse_workspace(cls, system_design_msg: Message) -> str: - if system_design_msg.instruct_content: - return system_design_msg.instruct_content.dict().get("Python package name") - return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) - async def _write_test(self, message: Message) -> None: changed_files = message.content.splitlines() src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index d1e65a4e0..2e3bcbbd5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -164,10 +164,6 @@ class Role: if env: env.set_subscription(self, self._subscription) - def get_env(self): - """Return the environment in which the role works.""" - return self._rc.env - @property def profile(self): """Get the role description (position)""" From 10d9f33150a5e4b96bf904098db7ebaa7dc2aeca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 23 Nov 2023 23:04:41 +0800 Subject: [PATCH 151/413] refactor: use MESSAGE_ROUTE_TO_NONE --- metagpt/const.py | 1 + metagpt/roles/qa_engineer.py | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/const.py b/metagpt/const.py index e97ffdb7d..311712013 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -49,6 +49,7 @@ MESSAGE_ROUTE_TO = "send_to" MESSAGE_ROUTE_CAUSE_BY = "cause_by" MESSAGE_META_ROLE = "role" MESSAGE_ROUTE_TO_ALL = "" +MESSAGE_ROUTE_TO_NONE = "" REQUIREMENT_FILENAME = "requirement.txt" DOCS_FILE_REPO = "docs" diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 6f0738294..eac30413a 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -11,7 +11,7 @@ import json from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest from metagpt.config import CONFIG -from metagpt.const import OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO +from metagpt.const import MESSAGE_ROUTE_TO_NONE, OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext @@ -66,7 +66,6 @@ class QaEngineer(Role): working_directory=str(CONFIG.git_repo.workdir), additional_python_paths=[CONFIG.src_workspace], ) - msg = Message( content=run_code_context.json(), role=self.profile, @@ -135,7 +134,7 @@ class QaEngineer(Role): role=self.profile, cause_by=WriteTest, sent_from=self.profile, - send_to="", + send_to=MESSAGE_ROUTE_TO_NONE, ) return result_msg @@ -160,6 +159,6 @@ class QaEngineer(Role): role=self.profile, cause_by=WriteTest, sent_from=self.profile, - send_to="", + send_to=MESSAGE_ROUTE_TO_NONE, ) return result_msg From 75dcc8d5341f6eda98ced66a032369953c75445c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 24 Nov 2023 13:30:00 +0800 Subject: [PATCH 152/413] fixbug: DebugError --- metagpt/actions/debug_error.py | 5 ++- metagpt/actions/run_code.py | 25 +++++++++-- metagpt/roles/qa_engineer.py | 77 +++++++++++++++++--------------- metagpt/schema.py | 24 ++++++++++ metagpt/utils/common.py | 8 +++- metagpt/utils/file_repository.py | 11 ++++- 6 files changed, 107 insertions(+), 43 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index a55f13dad..7a12e18f8 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -5,6 +5,7 @@ @Author : alexanderwu @File : debug_error.py """ +import re from metagpt.actions.action import Action from metagpt.logs import logger @@ -36,7 +37,9 @@ class DebugError(Action): # return fixed_code async def run(self, *args, **kwargs) -> str: - if "PASS" in self.context.output: + pattern = r"Ran (\d+) tests in ([\d.]+)s\n\nOK" + matches = re.search(pattern, self.context.output) + if matches: return "", "the original code works fine, no need to debug" file_name = self.context.code_filename diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index f2d323f06..b244577a7 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -51,8 +51,14 @@ CONTEXT = """ ## Running Command {command} ## Running Output -standard output: {outs}; -standard errors: {errs}; +standard output: +```text +{outs} +``` +standard errors: +```text +{errs} +``` """ @@ -84,10 +90,19 @@ class RunCode(Action): additional_python_paths = ":".join(additional_python_paths) env["PYTHONPATH"] = additional_python_paths + ":" + env.get("PYTHONPATH", "") + install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"] + logger.info(" ".join(install_command)) + subprocess.run(install_command, check=True, cwd=working_directory, env=env) + + install_pytest_command = ["python", "-m", "pip", "install", "pytest"] + logger.info(" ".join(install_pytest_command)) + subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env) + # Start the subprocess process = subprocess.Popen( command, cwd=working_directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env ) + logger.info(" ".join(command)) try: # Wait for the process to complete, with a timeout @@ -101,7 +116,11 @@ class RunCode(Action): async def run(self, *args, **kwargs) -> str: logger.info(f"Running {' '.join(self.context.command)}") if self.context.mode == "script": - outs, errs = await self.run_script(command=self.context.command, **kwargs) + outs, errs = await self.run_script( + command=self.context.command, + working_directory=self.context.working_directory, + additional_python_paths=self.context.additional_python_paths, + ) elif self.context.mode == "text": outs, errs = await self.run_text(code=self.context.code) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index eac30413a..f950efef4 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -7,15 +7,13 @@ @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data type of the `cause_by` value in the `Message` to a string, and utilize the new message filtering feature. """ -import json - from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest from metagpt.config import CONFIG from metagpt.const import MESSAGE_ROUTE_TO_NONE, OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext -from metagpt.utils.common import any_to_str_set +from metagpt.utils.common import any_to_str_set, parse_recipient class QaEngineer(Role): @@ -64,68 +62,76 @@ class QaEngineer(Role): code_filename=context.code_doc.filename, test_filename=context.test_doc.filename, working_directory=str(CONFIG.git_repo.workdir), - additional_python_paths=[CONFIG.src_workspace], + additional_python_paths=[str(CONFIG.src_workspace)], ) - msg = Message( - content=run_code_context.json(), - role=self.profile, - cause_by=WriteTest, - sent_from=self, - send_to=self, + self.publish_message( + Message( + content=run_code_context.json(), + role=self.profile, + cause_by=WriteTest, + sent_from=self, + send_to=self, + ) ) - self.publish_message(msg) logger.info(f"Done {str(tests_file_repo.workdir)} generating.") async def _run_code(self, msg): - m = json.loads(msg.content) - run_code_context = RunCodeContext(**m) - src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) - src_doc = await src_file_repo.get(run_code_context.code_filename) + run_code_context = RunCodeContext.loads(msg.content) + src_doc = await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).get(run_code_context.code_filename) if not src_doc: return - test_file_repo = CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO) - test_doc = await test_file_repo.get(run_code_context.test_filename) + test_doc = await CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO).get(run_code_context.test_filename) if not test_doc: return run_code_context.code = src_doc.content run_code_context.test_code = test_doc.content result_msg = await RunCode(context=run_code_context, llm=self._llm).run() - outputs_file_repo = CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO) - run_code_context.output_filename = run_code_context.test_filename + ".log" - await outputs_file_repo.save( + run_code_context.output_filename = run_code_context.test_filename + ".md" + await CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO).save( filename=run_code_context.output_filename, content=result_msg, dependencies={src_doc.root_relative_path, test_doc.root_relative_path}, ) run_code_context.code = None run_code_context.test_code = None - msg = Message( - content=run_code_context.json(), role=self.profile, cause_by=RunCode, sent_from=self, send_to=self + recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself + mappings = { + "Engineer": "Alex", + "QaEngineer": "Edward", + } + self.publish_message( + Message( + content=run_code_context.json(), + role=self.profile, + cause_by=RunCode, + sent_from=self, + send_to=mappings.get(recipient, MESSAGE_ROUTE_TO_NONE), + ) ) - self.publish_message(msg) async def _debug_error(self, msg): - m = json.loads(msg.context) - run_code_context = RunCodeContext(**m) + run_code_context = RunCodeContext.loads(msg.content) output_file_repo = CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO) output_doc = await output_file_repo.get(run_code_context.output_filename) if not output_doc: return run_code_context.output = output_doc.content code = await DebugError(context=run_code_context, llm=self._llm).run() - src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) - await src_file_repo.save(filename=run_code_context.code_filename, content=code) + await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).save( + filename=run_code_context.code_filename, content=code + ) run_code_context.output = None run_code_context.output_filename = None - msg = Message( - content=run_code_context.json(), - role=self.profile, - cause_by=DebugError, - sent_from=self, - send_to=self, + self.publish_message( + Message( + content=run_code_context.json(), + role=self.profile, + cause_by=DebugError, + sent_from=self, + send_to=self, + ) ) - self.publish_message(msg) async def _act(self) -> Message: if self.test_round > self.test_round_allowed: @@ -154,11 +160,10 @@ class QaEngineer(Role): # I ran my test code, time to fix bugs, if any await self._debug_error(msg) self.test_round += 1 - result_msg = Message( + return Message( content=f"Round {self.test_round} of tests done", role=self.profile, cause_by=WriteTest, sent_from=self.profile, send_to=MESSAGE_ROUTE_TO_NONE, ) - return result_msg diff --git a/metagpt/schema.py b/metagpt/schema.py index 5cc7cdb2d..53a22f0e6 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -253,12 +253,28 @@ class CodingContext(BaseModel): task_doc: Document code_doc: Document + @staticmethod + def loads(val: str) -> CodingContext | None: + try: + m = json.loads(val) + return CodingContext(**m) + except Exception: + return None + class TestingContext(BaseModel): filename: str code_doc: Document test_doc: Document + @staticmethod + def loads(val: str) -> TestingContext | None: + try: + m = json.loads(val) + return TestingContext(**m) + except Exception: + return None + class RunCodeContext(BaseModel): mode: str = "script" @@ -271,3 +287,11 @@ class RunCodeContext(BaseModel): additional_python_paths: List[str] = Field(default_factory=list) output_filename: Optional[str] output: Optional[str] + + @staticmethod + def loads(val: str) -> RunCodeContext | None: + try: + m = json.loads(val) + return RunCodeContext(**m) + except Exception: + return None diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 798acf214..9002a8dfb 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -304,7 +304,13 @@ def print_members(module, indent=0): def parse_recipient(text): pattern = r"## Send To:\s*([A-Za-z]+)\s*?" # hard code for now recipient = re.search(pattern, text) - return recipient.group(1) if recipient else "" + if recipient: + return recipient.group(1) + pattern = r"Send To:\s*([A-Za-z]+)\s*?" + recipient = re.search(pattern, text) + if recipient: + return recipient.group(1) + return "" def get_class_name(cls) -> str: diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 62ba99d42..8de4bdf5b 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -96,8 +96,15 @@ class FileRepository: path_name = self.workdir / filename if not path_name.exists(): return None - async with aiofiles.open(str(path_name), mode="r") as reader: - doc.content = await reader.read() + try: + async with aiofiles.open(str(path_name), mode="r") as reader: + doc.content = await reader.read() + except FileNotFoundError as e: + logger.info(f"open {str(path_name)} failed:{e}") + return None + except Exception as e: + logger.info(f"open {str(path_name)} failed:{e}") + return None return doc async def get_all(self) -> List[Document]: From 45be71d9bf2c8cc6619c3c3062d2b37022cebe92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 24 Nov 2023 13:36:35 +0800 Subject: [PATCH 153/413] fixbug: DebugError --- metagpt/roles/qa_engineer.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index f950efef4..68138d925 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -112,8 +112,7 @@ class QaEngineer(Role): async def _debug_error(self, msg): run_code_context = RunCodeContext.loads(msg.content) - output_file_repo = CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO) - output_doc = await output_file_repo.get(run_code_context.output_filename) + output_doc = await CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO).get(run_code_context.output_filename) if not output_doc: return run_code_context.output = output_doc.content From 938fa8a446de3d1fbb50efc780577a1854ec6c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 24 Nov 2023 13:48:25 +0800 Subject: [PATCH 154/413] feat: git archive --- metagpt/software_company.py | 2 ++ metagpt/utils/git_repository.py | 1 + 2 files changed, 3 insertions(+) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index d3c2c463b..5aa0864e0 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -59,4 +59,6 @@ class SoftwareCompany(BaseModel): logger.debug(f"{n_round=}") self._check_balance() await self.environment.run() + if CONFIG.git_repo: + CONFIG.git_repo.archive() return self.environment.history diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index c2eb2360e..660561bf3 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -151,6 +151,7 @@ class GitRepository: :param comments: Comments for the archive commit. """ + logger.info(f"Archive: {[list(self.changed_files.keys())]}") self.add_change(self.changed_files) self.commit(comments) From 8ce6914df21b0799db04a968f3243a591ff14c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 24 Nov 2023 14:04:01 +0800 Subject: [PATCH 155/413] feat: git archive --- metagpt/utils/git_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 660561bf3..ace0cf8a2 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -151,7 +151,7 @@ class GitRepository: :param comments: Comments for the archive commit. """ - logger.info(f"Archive: {[list(self.changed_files.keys())]}") + logger.info(f"Archive: {list(self.changed_files.keys())}") self.add_change(self.changed_files) self.commit(comments) From 882f22da352f8099af6fc0974a292c4866cb6c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 24 Nov 2023 19:56:27 +0800 Subject: [PATCH 156/413] =?UTF-8?q?feat:=20=E6=B5=81=E7=A8=8B=E8=B0=83?= =?UTF-8?q?=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/debug_error.py | 45 +++++++++---- metagpt/actions/design_api.py | 93 +++++++++++++-------------- metagpt/actions/project_management.py | 41 +++++++++++- metagpt/actions/run_code.py | 29 +++++---- metagpt/actions/write_code.py | 69 +++++++++++--------- metagpt/actions/write_prd.py | 74 +++++++++++++++++++-- metagpt/const.py | 2 +- metagpt/roles/engineer.py | 8 ++- metagpt/roles/qa_engineer.py | 26 ++++---- metagpt/schema.py | 14 ++++ metagpt/utils/git_repository.py | 9 ++- 11 files changed, 274 insertions(+), 136 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 7a12e18f8..d0c3652b4 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -8,7 +8,10 @@ import re from metagpt.actions.action import Action +from metagpt.config import CONFIG +from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger +from metagpt.schema import RunCodeResult from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -19,7 +22,20 @@ Based on the message, first, figure out your own role, i.e. Engineer or QaEngine then rewrite the development code or the test code based on your role, the error, and the summary, such that all bugs are fixed and the code performs well. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the test case or script and triple quotes. The message is as follows: -{context} +# Legacy Code +```python +{code} +``` +--- +# Unit Test Code +```python +{test_code} +``` +--- +# Console logs +```text +{logs} +``` --- Now you should start rewriting the code: ## file name of the code to rewrite: Write code with triple quoto. Do your best to implement THIS IN ONLY ONE FILE. @@ -30,25 +46,26 @@ class DebugError(Action): def __init__(self, name="DebugError", context=None, llm=None): super().__init__(name, context, llm) - # async def run(self, code, error): - # prompt = f"Here is a piece of Python code:\n\n{code}\n\nThe following error occurred during execution:" \ - # f"\n\n{error}\n\nPlease try to fix the error in this code." - # fixed_code = await self._aask(prompt) - # return fixed_code - async def run(self, *args, **kwargs) -> str: + output_doc = await CONFIG.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).get(self.context.output_filename) + if not output_doc: + return "" + output_detail = RunCodeResult.loads(output_doc.content) pattern = r"Ran (\d+) tests in ([\d.]+)s\n\nOK" - matches = re.search(pattern, self.context.output) + matches = re.search(pattern, output_detail.stderr) if matches: - return "", "the original code works fine, no need to debug" + return "" - file_name = self.context.code_filename - logger.info(f"Debug and rewrite {file_name}") - - prompt = PROMPT_TEMPLATE.format(context=self.context.output) + logger.info(f"Debug and rewrite {self.context.code_filename}") + code_doc = await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).get(self.context.code_filename) + if not code_doc: + return "" + test_doc = await CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO).get(self.context.test_filename) + if not test_doc: + return "" + prompt = PROMPT_TEMPLATE.format(code=code_doc.content, test_code=test_doc.content, logs=output_detail.stderr) rsp = await self._aask(prompt) - code = CodeParser.parse_code(block="", text=rsp) return code diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index a8f89473d..02f87bc47 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -6,7 +6,6 @@ @File : design_api.py """ import json -import shutil from pathlib import Path from typing import List @@ -18,13 +17,11 @@ from metagpt.const import ( SEQ_FLOW_FILE_REPO, SYSTEM_DESIGN_FILE_REPO, SYSTEM_DESIGN_PDF_FILE_REPO, - WORKSPACE_ROOT, ) from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template -from metagpt.utils.json_to_markdown import json_to_markdown from metagpt.utils.mermaid import mermaid_to_file templates = { @@ -157,6 +154,34 @@ OUTPUT_MAPPING = { "Anything UNCLEAR": (str, ...), } +MERGE_PROMPT = """ +## Old Design +{old_design} + +## Context +{context} + +----- +Role: You are an architect; The goal is to incrementally update the "Old Design" based on the information provided by the "Context," aiming to design a state-of-the-art (SOTA) Python system compliant with PEP8. Additionally, the objective is to optimize the use of high-quality open-source tools. +Requirement: Fill in the following missing information based on the context, each section name is a key in json +Max Output: 8192 chars or 2048 tokens. Try to use them up. + +## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. + +## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores + +## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here + +## Data structures and interface definitions: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. + +## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. + +## Anything UNCLEAR: Provide as Plain text. Make clear here. + +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Design" format, +and only output the json inside this tag, nothing else +""" + class WriteDesign(Action): def __init__(self, name, context=None, llm=None): @@ -167,50 +192,6 @@ class WriteDesign(Action): "clearly and in detail." ) - def recreate_workspace(self, workspace: Path): - try: - shutil.rmtree(workspace) - except FileNotFoundError: - pass # Folder does not exist, but we don't care - workspace.mkdir(parents=True, exist_ok=True) - - async def _save_prd(self, docs_path, resources_path, context): - prd_file = docs_path / "prd.md" - if context[-1].instruct_content and context[-1].instruct_content.dict()["Competitive Quadrant Chart"]: - quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"] - await mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis") - - if context[-1].instruct_content: - logger.info(f"Saving PRD to {prd_file}") - prd_file.write_text(json_to_markdown(context[-1].instruct_content.dict())) - - async def _save_system_design(self, docs_path, resources_path, system_design): - data_api_design = system_design.instruct_content.dict()[ - "Data structures and interface definitions" - ] # CodeParser.parse_code(block="Data structures and interface definitions", text=content) - seq_flow = system_design.instruct_content.dict()[ - "Program call flow" - ] # CodeParser.parse_code(block="Program call flow", text=content) - await mermaid_to_file(data_api_design, resources_path / "data_api_design") - await mermaid_to_file(seq_flow, resources_path / "seq_flow") - system_design_file = docs_path / "system_design.md" - logger.info(f"Saving System Designs to {system_design_file}") - system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict()))) - - async def _save(self, context, system_design): - if isinstance(system_design, ActionOutput): - ws_name = system_design.instruct_content.dict()["Python package name"] - else: - ws_name = CodeParser.parse_str(block="Python package name", text=system_design) - workspace = WORKSPACE_ROOT / ws_name - self.recreate_workspace(workspace) - docs_path = workspace / "docs" - resources_path = workspace / "resources" - docs_path.mkdir(parents=True, exist_ok=True) - resources_path.mkdir(parents=True, exist_ok=True) - await self._save_prd(docs_path, resources_path, context) - await self._save_system_design(docs_path, resources_path, system_design) - async def run(self, with_messages, format=CONFIG.prompt_format): # 通过git diff来识别docs/prds下哪些PRD文档发生了变动 prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) @@ -234,7 +215,8 @@ class WriteDesign(Action): filename=filename, prds_file_repo=prds_file_repo, system_design_file_repo=system_design_file_repo ) changed_files.docs[filename] = doc - + if not changed_files.docs: + logger.info("Nothing has changed.") # 等docs/system_designs/下所有文件都处理完才发publish message,给后续做全局优化留空间。 return ActionOutput(content=changed_files.json(), instruct_content=changed_files) @@ -253,10 +235,21 @@ class WriteDesign(Action): await self._rename_workspace(system_design) return system_design - async def _merge(self, prd_doc, system_design_doc): + async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): + prompt = MERGE_PROMPT.format(old_design=system_design_doc.content, context=prd_doc.content) + system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) + # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python + # package name" contain space, have to use setattr + setattr( + system_design.instruct_content, + "Python package name", + system_design.instruct_content.dict()["Python package name"].strip().strip("'").strip('"'), + ) + system_design_doc.content = system_design.instruct_content.json() return system_design_doc - async def _rename_workspace(self, system_design): + @staticmethod + async def _rename_workspace(system_design): if CONFIG.WORKDIR: # 已经指定了在旧版本上更新 return diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 686aa3689..4fd944027 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -17,6 +17,7 @@ from metagpt.const import ( TASK_PDF_FILE_REPO, WORKSPACE_ROOT, ) +from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template @@ -169,6 +170,35 @@ OUTPUT_MAPPING = { "Anything UNCLEAR": (str, ...), } +MERGE_PROMPT = """ +# Context +{context} + +## Old Tasks +{old_tasks} +----- +Role: You are a project manager; The goal is to merge the new PRD/technical design content from 'Context' into 'Old Tasks.' Based on this merged result, break down tasks, give a task list, and analyze task dependencies to start with the prerequisite modules. +Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them +Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. + +## Required Python third-party packages: Provided in requirements.txt format + +## Required Other language third-party packages: Provided in requirements.txt format + +## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. + +## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first + +## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first + +## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. + +## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. + +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Tasks" format, +and only output the json inside this tag, nothing else +""" + class WriteTasks(Action): def __init__(self, name="CreateTasks", context=None, llm=None): @@ -209,6 +239,8 @@ class WriteTasks(Action): ) change_files.docs[filename] = task_doc + if not change_files.docs: + logger.info("Nothing has changed.") # 等docs/tasks/下所有文件都处理完才发publish message,给后续做全局优化留空间。 return ActionOutput(content=change_files.json(), instruct_content=change_files) @@ -216,7 +248,7 @@ class WriteTasks(Action): system_design_doc = await system_design_file_repo.get(filename) task_doc = await tasks_file_repo.get(filename) if task_doc: - task_doc = await self._merge(system_design_doc=system_design_doc, task_dock=task_doc) + task_doc = await self._merge(system_design_doc=system_design_doc, task_doc=task_doc) else: rsp = await self._run_new_tasks(context=system_design_doc.content) task_doc = Document(root_path=TASK_FILE_REPO, filename=filename, content=rsp.instruct_content.json()) @@ -234,8 +266,11 @@ class WriteTasks(Action): # self._save(context, rsp) return rsp - async def _merge(self, system_design_doc, task_dock) -> Document: - return task_dock + async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: + prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content) + rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) + task_doc.content = rsp.instruct_content.json() + return task_doc @staticmethod async def _update_requirements(doc): diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index b244577a7..242eaa25d 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -12,6 +12,7 @@ from typing import Tuple from metagpt.actions.action import Action from metagpt.logs import logger +from metagpt.schema import RunCodeResult PROMPT_TEMPLATE = """ Role: You are a senior development and qa engineer, your role is summarize the code running result. @@ -89,14 +90,7 @@ class RunCode(Action): additional_python_paths = [working_directory] + additional_python_paths additional_python_paths = ":".join(additional_python_paths) env["PYTHONPATH"] = additional_python_paths + ":" + env.get("PYTHONPATH", "") - - install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"] - logger.info(" ".join(install_command)) - subprocess.run(install_command, check=True, cwd=working_directory, env=env) - - install_pytest_command = ["python", "-m", "pip", "install", "pytest"] - logger.info(" ".join(install_pytest_command)) - subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env) + RunCode._install_dependencies(working_directory=working_directory, env=env) # Start the subprocess process = subprocess.Popen( @@ -113,7 +107,7 @@ class RunCode(Action): stdout, stderr = process.communicate() return stdout.decode("utf-8"), stderr.decode("utf-8") - async def run(self, *args, **kwargs) -> str: + async def run(self, *args, **kwargs) -> RunCodeResult: logger.info(f"Running {' '.join(self.context.command)}") if self.context.mode == "script": outs, errs = await self.run_script( @@ -139,7 +133,20 @@ class RunCode(Action): prompt = PROMPT_TEMPLATE.format(context=context) rsp = await self._aask(prompt) + return RunCodeResult(summary=rsp, stdout=outs, stderr=errs) - result = context + rsp + @staticmethod + def _install_dependencies(working_directory, env): + install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"] + logger.info(" ".join(install_command)) + try: + subprocess.run(install_command, check=True, cwd=working_directory, env=env) + except subprocess.CalledProcessError as e: + logger.warning(f"{e}") - return result + install_pytest_command = ["python", "-m", "pip", "install", "pytest"] + logger.info(" ".join(install_pytest_command)) + try: + subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env) + except subprocess.CalledProcessError as e: + logger.warning(f"{e}") diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index d4d33fe0c..c9b6c3b9e 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -7,16 +7,15 @@ @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `cause_by` value of the `Message` object. """ -import json from tenacity import retry, stop_after_attempt, wait_fixed -from metagpt.actions import WriteDesign from metagpt.actions.action import Action -from metagpt.const import WORKSPACE_ROOT +from metagpt.config import CONFIG +from metagpt.const import TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger -from metagpt.schema import CodingContext -from metagpt.utils.common import CodeParser, any_to_str +from metagpt.schema import CodingContext, RunCodeResult +from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ NOTICE @@ -33,8 +32,25 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc 7. Do not use public member functions that do not exist in your design. ----- -# Context -{context} +# Design +```json +{design} +``` +----- +# Tasks +```json +{tasks} +``` +----- +# Legacy Code +```python +{code} +``` +----- +# Debug logs +```text +{logs} +``` ----- ## Format example ----- @@ -51,26 +67,6 @@ class WriteCode(Action): def __init__(self, name="WriteCode", context=None, llm=None): super().__init__(name, context, llm) - def _is_invalid(self, filename): - return any(i in filename for i in ["mp3", "wav"]) - - def _save(self, context, filename, code): - # logger.info(filename) - # logger.info(code_rsp) - if self._is_invalid(filename): - return - - design = [i for i in context if i.cause_by == any_to_str(WriteDesign)][0] - - ws_name = CodeParser.parse_str(block="Python package name", text=design.content) - ws_path = WORKSPACE_ROOT / ws_name - if f"{ws_name}/" not in filename and all(i not in filename for i in ["requirements.txt", ".md"]): - ws_path = ws_path / ws_name - code_path = ws_path / filename - code_path.parent.mkdir(parents=True, exist_ok=True) - code_path.write_text(code) - logger.info(f"Saving Code to {code_path}") - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) async def write_code(self, prompt) -> str: code_rsp = await self._aask(prompt) @@ -78,12 +74,21 @@ class WriteCode(Action): return code async def run(self, *args, **kwargs) -> CodingContext: - m = json.loads(self.context.content) - coding_context = CodingContext(**m) - context = "\n".join( - [coding_context.design_doc.content, coding_context.task_doc.content, coding_context.code_doc.content] + coding_context = CodingContext.loads(self.context.content) + test_doc = await CONFIG.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).get( + "test_" + coding_context.filename + ".json" + ) + logs = "" + if test_doc: + test_detail = RunCodeResult.loads(test_doc.content) + logs = test_detail.stderr + prompt = PROMPT_TEMPLATE.format( + design=coding_context.design_doc.content, + tasks=coding_context.task_doc.content, + code=coding_context.code_doc.content, + logs=logs, + filename=self.context.filename, ) - prompt = PROMPT_TEMPLATE.format(context=context, filename=self.context.filename) logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) coding_context.code_doc.content = code diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 8b03ac29a..532f5bc34 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -219,6 +219,7 @@ There are no unclear points. }, } + OUTPUT_MAPPING = { "Original Requirements": (str, ...), "Product Goals": (List[str], ...), @@ -231,13 +232,60 @@ OUTPUT_MAPPING = { "Anything UNCLEAR": (str, ...), } +IS_RELATIVE_PROMPT = """ +## PRD: +{old_prd} + +## New Requirement: +{requirements} + +___ +You are a professional product manager; You need to assess whether the new requirements are relevant to the existing PRD to determine whether to merge the new requirements into this PRD. +Is the newly added requirement in "New Requirement" related to the PRD? +Respond with `YES` if it is related, `NO` if it is not, and provide the reasons. Return the response in JSON format. +""" + +MERGE_PROMPT = """ +# Context +## Original Requirements +{requirements} + + +## Old PRD +{old_prd} +----- +Role: You are a professional product manager; The goal is to merge the newly added requirements into the existing PRD in order to design a concise, usable, and efficient product. +Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design + +## Original Requirements: Provide as Plain text, place the polished complete original requirements here + +## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. If the requirement itself is simple, the goal should also be simple + +## User Stories: Provided as Python list[str], up to 5 scenario-based user stories, If the requirement itself is simple, the user stories should also be less + +## Competitive Analysis: Provided as Python list[str], up to 7 competitive product analyses, consider as similar competitors as possible + +## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. + +## Requirement Analysis: Provide as Plain text. Be simple. LESS IS MORE. Make your requirements less dumb. Delete the parts unnessasery. + +## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards; no more than 5 requirements and consider to make its difficulty lower + +## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. +## Anything UNCLEAR: Provide as Plain text. Make clear here. + +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old PRD" format, +and only output the json inside this tag, nothing else +""" + class WritePRD(Action): def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: - # 判断哪些需求文档需要重写:调LLM判断新增需求与prd是否相关,若相关就rewrite prd + # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are + # related to the PRD. If they are related, rewrite the PRD. docs_file_repo = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) requirement_doc = await docs_file_repo.get(REQUIREMENT_FILENAME) prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) @@ -250,14 +298,16 @@ class WritePRD(Action): if not prd_doc: continue change_files.docs[prd_doc.filename] = prd_doc - # 如果没有任何PRD,就使用docs/requirement.txt生成一个prd + # If there is no existing PRD, generate one using 'docs/requirement.txt'. if not change_files.docs: prd_doc = await self._update_prd( requirement_doc=requirement_doc, prd_doc=None, prds_file_repo=prds_file_repo, *args, **kwargs ) if prd_doc: change_files.docs[prd_doc.filename] = prd_doc - # 等docs/prds/下所有文件都与新增需求对比完后,再触发publish message让工作流跳转到下一环节。如此设计是为了给后续做全局优化留空间。 + # Once all files under 'docs/prds/' have been compared with the newly added requirements, trigger the + # 'publish' message to transition the workflow to the next stage. This design allows room for global + # optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: @@ -278,11 +328,23 @@ class WritePRD(Action): prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) return prd - async def _is_relative_to(self, doc1, doc2) -> bool: + async def _is_relative_to(self, new_requirement_doc, old_prd_doc) -> bool: + m = json.loads(old_prd_doc.content) + if m.get("Original Requirements") == new_requirement_doc.content: + # There have been no changes in the requirements, so they are considered unrelated. + return False + prompt = IS_RELATIVE_PROMPT.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) + res = await self._aask(prompt=prompt) + logger.info(f"[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") + if "YES" in res: + return True return False - async def _merge(self, doc1, doc2) -> Document: - pass + async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: + prompt = MERGE_PROMPT.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) + prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) + prd_doc.content = prd.instruct_content.json() + return prd_doc async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: if not prd_doc: diff --git a/metagpt/const.py b/metagpt/const.py index 311712013..49965b622 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -63,4 +63,4 @@ SYSTEM_DESIGN_PDF_FILE_REPO = "resources/system_design" PRD_PDF_FILE_REPO = "resources/prd" TASK_PDF_FILE_REPO = "resources/api_spec_and_tasks" TEST_CODES_FILE_REPO = "tests" -OUTPUTS_FILE_REPO = "outputs" +TEST_OUTPUTS_FILE_REPO = "test_outputs" diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 89827a1ca..b6ecc4767 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -90,6 +90,8 @@ class Engineer(Role): self._rc.memory.add(msg) changed_files.add(coding_context.code_doc.filename) + if not changed_files: + logger.info("Nothing has changed.") return changed_files async def _act(self) -> Message: @@ -136,8 +138,8 @@ class Engineer(Role): root_path=str(src_file_repo.root_path), filename=task_filename, content=context.json() ) if task_filename in changed_files.docs: - logger.error( - f"Log to expose potential file name conflicts: {coding_doc.json()} & " + logger.warning( + f"Log to expose potential conflicts: {coding_doc.json()} & " f"{changed_files.docs[task_filename].json()}" ) changed_files.docs[task_filename] = coding_doc @@ -168,7 +170,7 @@ class Engineer(Role): old_code_doc = await src_file_repo.get(filename) if not old_code_doc: old_code_doc = Document(root_path=str(src_file_repo.root_path), filename=filename, content="") - dependencies = {Path(i) for i in dependency.get(old_code_doc.root_relative_path)} + dependencies = {Path(i) for i in await dependency.get(old_code_doc.root_relative_path)} task_doc = None design_doc = None for i in dependencies: diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 68138d925..a88b01e37 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -9,7 +9,11 @@ """ from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest from metagpt.config import CONFIG -from metagpt.const import MESSAGE_ROUTE_TO_NONE, OUTPUTS_FILE_REPO, TEST_CODES_FILE_REPO +from metagpt.const import ( + MESSAGE_ROUTE_TO_NONE, + TEST_CODES_FILE_REPO, + TEST_OUTPUTS_FILE_REPO, +) from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext @@ -86,20 +90,17 @@ class QaEngineer(Role): return run_code_context.code = src_doc.content run_code_context.test_code = test_doc.content - result_msg = await RunCode(context=run_code_context, llm=self._llm).run() - run_code_context.output_filename = run_code_context.test_filename + ".md" - await CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO).save( + result = await RunCode(context=run_code_context, llm=self._llm).run() + run_code_context.output_filename = run_code_context.test_filename + ".json" + await CONFIG.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).save( filename=run_code_context.output_filename, - content=result_msg, + content=result.json(), dependencies={src_doc.root_relative_path, test_doc.root_relative_path}, ) run_code_context.code = None run_code_context.test_code = None - recipient = parse_recipient(result_msg) # the recipient might be Engineer or myself - mappings = { - "Engineer": "Alex", - "QaEngineer": "Edward", - } + recipient = parse_recipient(result.summary) # the recipient might be Engineer or myself + mappings = {"Engineer": "Alex", "QaEngineer": "Edward"} self.publish_message( Message( content=run_code_context.json(), @@ -112,16 +113,11 @@ class QaEngineer(Role): async def _debug_error(self, msg): run_code_context = RunCodeContext.loads(msg.content) - output_doc = await CONFIG.git_repo.new_file_repository(OUTPUTS_FILE_REPO).get(run_code_context.output_filename) - if not output_doc: - return - run_code_context.output = output_doc.content code = await DebugError(context=run_code_context, llm=self._llm).run() await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).save( filename=run_code_context.code_filename, content=code ) run_code_context.output = None - run_code_context.output_filename = None self.publish_message( Message( content=run_code_context.json(), diff --git a/metagpt/schema.py b/metagpt/schema.py index 53a22f0e6..e910fc866 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -295,3 +295,17 @@ class RunCodeContext(BaseModel): return RunCodeContext(**m) except Exception: return None + + +class RunCodeResult(BaseModel): + summary: str + stdout: str + stderr: str + + @staticmethod + def loads(val: str) -> RunCodeResult | None: + try: + m = json.loads(val) + return RunCodeResult(**m) + except Exception: + return None diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index ace0cf8a2..b8e35199b 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -72,7 +72,14 @@ class GitRepository: :param local_path: The local path where the new Git repository will be initialized. """ - self._repository = Repo.init(path=local_path) + self._repository = Repo.init(path=Path(local_path)) + + gitignore_filename = Path(local_path) / ".gitignore" + ignores = ["__pycache__", "*.pyc"] + with open(str(gitignore_filename), mode="w") as writer: + writer.write("\n".join(ignores)) + self._repository.index.add([".gitignore"]) + self._repository.index.commit("Add .gitignore") def add_change(self, files: Dict): """Add or remove files from the staging area based on the provided changes. From e1cabcad492d48804376a238c13747619396f1cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 11:21:52 +0800 Subject: [PATCH 157/413] feat: +annotation --- metagpt/actions/debug_error.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index d0c3652b4..7fdc2ef5b 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -4,6 +4,8 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : debug_error.py +@Modified By: mashenquan, 2023/11/27. Divide the context into three components: legacy code, unit test code, and + console log. """ import re From 86c5e5e8e662556204bdc69adf1a050e94962320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 11:28:07 +0800 Subject: [PATCH 158/413] feat: +annotation --- metagpt/actions/debug_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 7fdc2ef5b..971f76ca7 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -5,7 +5,7 @@ @Author : alexanderwu @File : debug_error.py @Modified By: mashenquan, 2023/11/27. Divide the context into three components: legacy code, unit test code, and - console log. + console log. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. """ import re From 22c5077747b22a4ad7b3bfbe1cd25d867e8c84fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 11:32:40 +0800 Subject: [PATCH 159/413] feat: +annotation --- metagpt/actions/design_api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 02f87bc47..8644aa6a4 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -4,6 +4,9 @@ @Time : 2023/5/11 19:26 @Author : alexanderwu @File : design_api.py +@Modified By: mashenquan, 2023/11/27. According to Section 2.2.3.1 of RFC 135, replace file data in the message with + the file name. According to the design in Section 2.2.3.5.3 of RFC 135, add incremental iteration + functionality. """ import json from pathlib import Path From 57d826a40cd0d7bb7a17f522fd6c3099f57bc20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 11:44:46 +0800 Subject: [PATCH 160/413] feat: +annotation --- metagpt/actions/design_api.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 8644aa6a4..2f8a306d5 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -196,14 +196,15 @@ class WriteDesign(Action): ) async def run(self, with_messages, format=CONFIG.prompt_format): - # 通过git diff来识别docs/prds下哪些PRD文档发生了变动 + # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) changed_prds = prds_file_repo.changed_files - # 通过git diff来识别docs/system_designs下那些设计文档发生了变动; + # Use `git diff` to identify which design documents in the `docs/system_designs` directory have undergone + # changes. system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_system_designs = system_design_file_repo.changed_files - # 对于那些发生变动的PRD和设计文档,重新生成设计内容; + # For those PRDs and design documents that have undergone changes, regenerate the design content. changed_files = Documents() for filename in changed_prds.keys(): doc = await self._update_system_design( @@ -220,7 +221,8 @@ class WriteDesign(Action): changed_files.docs[filename] = doc if not changed_files.docs: logger.info("Nothing has changed.") - # 等docs/system_designs/下所有文件都处理完才发publish message,给后续做全局优化留空间。 + # Wait until all files under `docs/system_designs/` are processed before sending the publish message, + # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) async def _new_system_design(self, context, format=CONFIG.prompt_format): @@ -253,7 +255,7 @@ class WriteDesign(Action): @staticmethod async def _rename_workspace(system_design): - if CONFIG.WORKDIR: # 已经指定了在旧版本上更新 + if CONFIG.WORKDIR: # Updating on the old version has already been specified if it's valid. return if isinstance(system_design, ActionOutput): From 759c8378e42c4b8a76d3d96946325ccd4c5f61d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 11:54:09 +0800 Subject: [PATCH 161/413] feat: +annotation --- metagpt/actions/debug_error.py | 5 +++-- metagpt/actions/design_api.py | 6 +++--- metagpt/actions/project_management.py | 26 +++++--------------------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 971f76ca7..e4a15d38d 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -4,8 +4,9 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : debug_error.py -@Modified By: mashenquan, 2023/11/27. Divide the context into three components: legacy code, unit test code, and - console log. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. +@Modified By: mashenquan, 2023/11/27. + 1. Divide the context into three components: legacy code, unit test code, and console log. + 2. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. """ import re diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 2f8a306d5..021edfe72 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -4,9 +4,9 @@ @Time : 2023/5/11 19:26 @Author : alexanderwu @File : design_api.py -@Modified By: mashenquan, 2023/11/27. According to Section 2.2.3.1 of RFC 135, replace file data in the message with - the file name. According to the design in Section 2.2.3.5.3 of RFC 135, add incremental iteration - functionality. +@Modified By: mashenquan, 2023/11/27. + 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. + 2. According to the design in Section 2.2.3.5.3 of RFC 135, add incremental iteration functionality. """ import json from pathlib import Path diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 4fd944027..042f1f01c 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -4,6 +4,10 @@ @Time : 2023/5/11 19:12 @Author : alexanderwu @File : project_management.py +@Modified By: mashenquan, 2023/11/27. + 1. Divide the context into three components: legacy code, unit test code, and console log. + 2. Move the document storage operations related to WriteDesign to the save operation of WriteDesign. + 3. According to the design in Section 2.2.3.5.4 of RFC 135, add incremental iteration functionality. """ import json from typing import List @@ -11,17 +15,10 @@ from typing import List from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import ( - SYSTEM_DESIGN_FILE_REPO, - TASK_FILE_REPO, - TASK_PDF_FILE_REPO, - WORKSPACE_ROOT, -) +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, TASK_PDF_FILE_REPO from metagpt.logs import logger from metagpt.schema import Document, Documents -from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template -from metagpt.utils.json_to_markdown import json_to_markdown templates = { "json": { @@ -204,18 +201,6 @@ class WriteTasks(Action): def __init__(self, name="CreateTasks", context=None, llm=None): super().__init__(name, context, llm) - def _save(self, context, rsp): - if context[-1].instruct_content: - ws_name = context[-1].instruct_content.dict()["Python package name"] - else: - ws_name = CodeParser.parse_str(block="Python package name", text=context[-1].content) - file_path = WORKSPACE_ROOT / ws_name / "docs/api_spec_and_tasks.md" - file_path.write_text(json_to_markdown(rsp.instruct_content.dict())) - - # Write requirements.txt - requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt" - requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) - async def run(self, with_messages, format=CONFIG.prompt_format): system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_system_designs = system_design_file_repo.changed_files @@ -263,7 +248,6 @@ class WriteTasks(Action): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) - # self._save(context, rsp) return rsp async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: From c483d0d7a3c74f8e66ed41d106013a17e80d7d6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 12:00:50 +0800 Subject: [PATCH 162/413] feat: +annotation --- metagpt/actions/project_management.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 042f1f01c..0081fd223 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -208,14 +208,15 @@ class WriteTasks(Action): tasks_file_repo = CONFIG.git_repo.new_file_repository(TASK_FILE_REPO) changed_tasks = tasks_file_repo.changed_files change_files = Documents() - # 根据docs/system_designs/下的git head diff识别哪些task文档需要重写 + # Rewrite the system designs that have undergone changes based on the git head diff under + # `docs/system_designs/`. for filename in changed_system_designs: task_doc = await self._update_tasks( filename=filename, system_design_file_repo=system_design_file_repo, tasks_file_repo=tasks_file_repo ) change_files.docs[filename] = task_doc - # 根据docs/tasks/下的git head diff识别哪些task文件被用户修改了,需要重写 + # Rewrite the task files that have undergone changes based on the git head diff under docs/tasks/. for filename in changed_tasks: if filename in change_files.docs: continue @@ -226,7 +227,8 @@ class WriteTasks(Action): if not change_files.docs: logger.info("Nothing has changed.") - # 等docs/tasks/下所有文件都处理完才发publish message,给后续做全局优化留空间。 + # Wait until all files under `docs/tasks/` are processed before sending the publish message, leaving room for + # global optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) async def _update_tasks(self, filename, system_design_file_repo, tasks_file_repo): From 5ea488d37a2c43df63f48a874b4f83e5bd50e832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 12:03:21 +0800 Subject: [PATCH 163/413] feat: +annotation --- metagpt/actions/project_management.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 0081fd223..ee1632612 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -216,7 +216,7 @@ class WriteTasks(Action): ) change_files.docs[filename] = task_doc - # Rewrite the task files that have undergone changes based on the git head diff under docs/tasks/. + # Rewrite the task files that have undergone changes based on the git head diff under `docs/tasks/`. for filename in changed_tasks: if filename in change_files.docs: continue @@ -227,7 +227,7 @@ class WriteTasks(Action): if not change_files.docs: logger.info("Nothing has changed.") - # Wait until all files under `docs/tasks/` are processed before sending the publish message, leaving room for + # Wait until all files under `docs/tasks/` are processed before sending the publish_message, leaving room for # global optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) From a405b4759b64648fe8f59c4ed411955c77db5714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 13:49:15 +0800 Subject: [PATCH 164/413] feat: +annotation --- metagpt/actions/run_code.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index 242eaa25d..1e7010e52 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -4,6 +4,14 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : run_code.py +@Modified By: mashenquan, 2023/11/27. + 1. Mark the location of Console logs in the PROMPT_TEMPLATE with markdown code-block formatting to enhance + the understanding for the LLM. + 2. Fix bug: Add the "install dependency" operation. + 3. Encapsulate the input of RunCode into RunCodeContext and encapsulate the output of RunCode into + RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. + 4. According to section 2.2.3.5.7 of RFC 135, change the method of transferring file content + (code files, unit test files, log files) from using the message to using the file name. """ import os import subprocess From 0f03645a8920b50b3dcc67817565cdd73f3e0f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:27:56 +0800 Subject: [PATCH 165/413] feat: +annotation --- metagpt/actions/write_code.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c9b6c3b9e..e9d41bb20 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -6,6 +6,13 @@ @File : write_code.py @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.1.3 of RFC 116, modify the data type of the `cause_by` value of the `Message` object. +@Modified By: mashenquan, 2023-11-27. + 1. Mark the location of Design, Tasks, Legacy Code and Debug logs in the PROMPT_TEMPLATE with markdown + code-block formatting to enhance the understanding for the LLM. + 2. Following the think-act principle, solidify the task parameters when creating the WriteCode object, rather + than passing them in when calling the run function. + 3. Encapsulate the input of RunCode into RunCodeContext and encapsulate the output of RunCode into + RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ from tenacity import retry, stop_after_attempt, wait_fixed From 9c13958f6c498e9d24fcd951e1c6ced84d35bde9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:31:01 +0800 Subject: [PATCH 166/413] feat: +annotation --- metagpt/actions/write_code_review.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 10e4aec3b..dae1c965f 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -4,6 +4,8 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : write_code_review.py +@Modified By: mashenquan, 2023/11/27. Following the think-act principle, solidify the task parameters when creating the + WriteCode object, rather than passing them in when calling the run function. """ from tenacity import retry, stop_after_attempt, wait_fixed From 16226a2e11621b91d05feb4a74e97259f95d66b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:35:22 +0800 Subject: [PATCH 167/413] feat: +annotation --- metagpt/actions/write_prd.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 532f5bc34..68e0e75ba 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -4,6 +4,9 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : write_prd.py +@Modified By: mashenquan, 2023/11/27. + 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. + 2. According to the design in Section 2.2.3.5.2 of RFC 135, add incremental iteration functionality. """ from __future__ import annotations From 512e205cd0945be9c6d8c6a980b309b286788557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:38:46 +0800 Subject: [PATCH 168/413] feat: +annotation --- metagpt/actions/project_management.py | 2 +- metagpt/actions/write_prd.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index ee1632612..641d21533 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -6,7 +6,7 @@ @File : project_management.py @Modified By: mashenquan, 2023/11/27. 1. Divide the context into three components: legacy code, unit test code, and console log. - 2. Move the document storage operations related to WriteDesign to the save operation of WriteDesign. + 2. Move the document storage operations related to WritePRD from the save operation of WriteDesign. 3. According to the design in Section 2.2.3.5.4 of RFC 135, add incremental iteration functionality. """ import json diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 68e0e75ba..cc21058b4 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023/11/27. 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.2 of RFC 135, add incremental iteration functionality. + 3. Move the document storage operations related to WritePRD from the save operation of WriteDesign. """ from __future__ import annotations From fbd24635df779764d9cd5608354ab4b649495f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:40:36 +0800 Subject: [PATCH 169/413] feat: +annotation --- metagpt/actions/write_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 9a9671bab..e980e0831 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -4,6 +4,8 @@ @Time : 2023/5/11 22:12 @Author : alexanderwu @File : environment.py +@Modified By: mashenquan, 2023-11-27. Following the think-act principle, solidify the task parameters when creating the + WriteTest object, rather than passing them in when calling the run function. """ from metagpt.actions.action import Action from metagpt.config import CONFIG From 628ecc0fb7585b749da7d49eb28171395af6b042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:46:25 +0800 Subject: [PATCH 170/413] feat: +annotation --- metagpt/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/config.py b/metagpt/config.py index d059a6a29..a20f58ec1 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -2,6 +2,9 @@ # -*- coding: utf-8 -*- """ Provide configuration, singleton +@Modified By: mashenquan, 2023/11/27. + 1. According to Section 2.2.3.11 of RFC 135, add git repository support. + 2. Add the parameter `src_workspace` for the old version project path. """ import os From 331d74059f18b5fdbb4aedbc8c5ce6a234f7ab4e Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 20 Nov 2023 11:24:46 +0800 Subject: [PATCH 171/413] =?UTF-8?q?1.=20=E5=8A=A8=E4=BD=9C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=20=201.=20SummarizeCode=E5=8A=A8=E4=BD=9C=EF=BC=9A?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=9F=BA=E4=BA=8E=E4=BB=A3=E7=A0=81=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=80=BB=E7=BB=93=EF=BC=8C=E6=80=9D=E8=80=83bug?= =?UTF-8?q?=E3=80=81=E9=80=BB=E8=BE=91=E3=80=81todo=20=20=202.=20CodeRevie?= =?UTF-8?q?w=E5=8A=A8=E4=BD=9C=E4=BC=98=E5=8C=96=EF=BC=9A=E7=9B=AE?= =?UTF-8?q?=E5=89=8D=E5=BC=BA=E5=88=B6=E8=A6=81=E6=B1=82=E5=9B=9E=E7=AD=94?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9C=89=E6=9B=B4=E9=AB=98=E7=9A=84?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E7=8E=87=E4=BA=86=202.=20=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=20=20=201.=20Document=E7=9A=84=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=8C=96=EF=BC=9AEnv->Repo->Document=EF=BC=8C?= =?UTF-8?q?=E5=85=B6=E4=B8=ADDocument/Asset/Code=E9=83=BD=E5=8F=AA?= =?UTF-8?q?=E7=94=A8Document=20=20=20=20=201.=20=E5=8E=9F=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=A3=80=E7=B4=A2=E7=9A=84Document=E6=94=B9=E4=B8=BAI?= =?UTF-8?q?ndexableDocument=20=20=202.=20Repo=E7=BB=93=E6=9E=84=E5=BC=95?= =?UTF-8?q?=E5=85=A5=EF=BC=9A=E7=94=A8=E4=BA=8EDocument=E8=A3=85=E8=BD=BD?= =?UTF-8?q?=E4=B8=8E=E5=85=83=E6=95=B0=E6=8D=AE=E8=A3=85=E8=BD=BD=20=20=20?= =?UTF-8?q?3.=20RepoParser=E5=BC=95=E5=85=A5=EF=BC=9A=E5=86=99=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=AE=80=E5=8D=95=E7=9A=84AST=20parser?= =?UTF-8?q?=EF=BC=88=E5=90=8E=E7=BB=AD=E5=8F=AF=E8=83=BD=E8=A6=81=E6=8D=A2?= =?UTF-8?q?tree-sitter=EF=BC=89=EF=BC=8C=E7=BB=99=E5=87=BA=E4=BA=86?= =?UTF-8?q?=E6=95=B4=E5=BA=93symbol=203.=20=E9=85=8D=E7=BD=AE=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=20=201.=20=E9=BB=98=E8=AE=A4=E6=9B=B4=E6=8D=A2?= =?UTF-8?q?=E4=B8=BAgpt-4-1106-preview=EF=BC=8C=E4=BB=A5=E8=8E=B7=E5=BE=97?= =?UTF-8?q?=E6=9C=80=E5=A5=BD=E7=9A=84=E6=95=88=E6=9E=9C=E4=B8=8E=E6=88=90?= =?UTF-8?q?=E6=9C=AC=20=20=202.=20=E6=8F=90=E4=BE=9B~/.metagpt=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E9=85=8D=E7=BD=AE=E6=9C=80=E9=AB=98=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7=E7=9B=AE=E5=BD=95=EF=BC=8C=E4=BB=8E=E4=B8=AD=E8=AF=BB?= =?UTF-8?q?=E5=8F=96config.yaml=20=20=203.=20workspace=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=81=B5=E6=B4=BB=E6=8C=87=E5=AE=9A=E4=BA=86=EF=BC=8C=E5=9C=A8?= =?UTF-8?q?config=E4=B8=AD=E9=85=8D=E7=BD=AE=204.=20metagpt=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E9=BB=98=E8=AE=A4=E5=91=BD=E4=BB=A4=E8=A1=8C=EF=BC=8C?= =?UTF-8?q?=E8=80=8C=E9=9D=9Epython=20startup.py=20=20=201.=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=96=B0=E7=9A=84METAGPT=5FROOT=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=EF=BC=8C=E8=80=8C=E9=9D=9E=E5=AF=BB=E6=89=BE?= =?UTF-8?q?git=EF=BC=8C=E4=BB=A5=E4=BE=BFcli=E5=AE=89=E8=A3=85=20=20=202.?= =?UTF-8?q?=20=E5=91=BD=E4=BB=A4=E8=A1=8C=E7=94=B1fire=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=E4=BA=86typer=EF=BC=8C=E5=AE=83=E4=BC=9A=E5=B8=A6=E6=9D=A5?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E6=9B=B4=E5=A5=BD=E7=9A=84=E4=BD=93=E9=AA=8C?= =?UTF-8?q?=20=20=203.=20project=5Fname=E5=8F=AF=E4=BB=A5=E7=81=B5?= =?UTF-8?q?=E6=B4=BB=E6=8C=87=E5=AE=9A=E4=BA=86=EF=BC=8C=E5=9C=A8metagpt?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C=E8=BE=93=E5=85=A5=E4=B8=AD=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=205.=20=E5=85=B6=E4=BB=96=20=20=201.=20BossRequiremen?= =?UTF-8?q?t=20->=20UserRequirement=20=20=202.=20=E5=A4=A7=E9=87=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=96=87=E6=9C=AC=E7=9A=84=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=AF=E8=AF=BB=E6=80=A7?= =?UTF-8?q?=20=20=203.=20=E4=B8=AD=E9=87=8F=E6=8F=90=E7=A4=BA=E8=AF=8D?= =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E7=A8=8D=E5=BE=AE=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=BA=9B=E5=87=86=E7=A1=AE=E7=8E=87=20=20=20?= =?UTF-8?q?4.=20=E6=9A=82=E6=97=B6=E5=B1=8F=E8=94=BD=E4=BA=86LongtermMemor?= =?UTF-8?q?y=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91=EF=BC=8C=E8=BF=99?= =?UTF-8?q?=E4=B8=AA=E9=80=BB=E8=BE=91=E5=BA=95=E5=B1=82=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E4=BA=86langchain=E7=9A=84FAISS=EF=BC=8C=E4=BC=9A=E5=B8=A6?= =?UTF-8?q?=E6=9D=A5~5=E7=A7=92=E5=8A=A0=E8=BD=BD=E8=80=97=E6=97=B6=20=20?= =?UTF-8?q?=205.=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=AE=89=E8=A3=85=E5=8C=85?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=83=A8=E5=88=86=E6=8F=8F=E8=BF=B0=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/agent_creator.py | 7 +- examples/debate.py | 4 +- examples/sk_agent.py | 10 +- metagpt/actions/SummarizeCode.py | 93 ++++++++ metagpt/actions/__init__.py | 4 +- metagpt/actions/add_requirement.py | 4 +- metagpt/actions/design_api.py | 35 ++- metagpt/actions/project_management.py | 13 +- metagpt/actions/write_code.py | 25 ++- metagpt/actions/write_code_review.py | 18 +- metagpt/actions/write_prd.py | 29 +-- metagpt/actions/write_test.py | 2 +- metagpt/config.py | 19 +- metagpt/const.py | 72 +++--- metagpt/document.py | 207 ++++++++++++++++++ metagpt/document_store/base_store.py | 10 +- metagpt/document_store/document.py | 82 ------- metagpt/document_store/faiss_store.py | 10 +- metagpt/document_store/repo_parser.py | 90 ++++++++ metagpt/environment.py | 12 +- metagpt/logs.py | 10 +- metagpt/manager.py | 2 +- metagpt/memory/__init__.py | 4 +- metagpt/roles/engineer.py | 23 +- metagpt/roles/product_manager.py | 4 +- metagpt/roles/qa_engineer.py | 9 +- metagpt/roles/role.py | 5 +- metagpt/roles/sk_agent.py | 4 +- metagpt/software_company.py | 13 -- metagpt/startup.py | 45 ++++ metagpt/team.py | 18 +- metagpt/tools/sd_engine.py | 14 +- metagpt/utils/mermaid.py | 8 +- metagpt/utils/token_counter.py | 7 +- setup.py | 11 +- startup.py | 72 ------ tests/metagpt/actions/mock.py | 4 +- tests/metagpt/actions/test_write_prd.py | 4 +- tests/metagpt/document_store/test_document.py | 16 +- tests/metagpt/memory/test_longterm_memory.py | 12 +- tests/metagpt/memory/test_memory_storage.py | 10 +- tests/metagpt/planner/test_action_planner.py | 4 +- tests/metagpt/planner/test_basic_planner.py | 4 +- tests/metagpt/roles/mock.py | 8 +- tests/metagpt/roles/ui_role.py | 7 +- tests/metagpt/test_environment.py | 4 +- tests/metagpt/tools/test_sd_tool.py | 6 +- tests/metagpt/utils/test_common.py | 6 +- tests/metagpt/utils/test_output_parser.py | 2 +- tests/metagpt/utils/test_read_docx.py | 4 +- 50 files changed, 699 insertions(+), 387 deletions(-) create mode 100644 metagpt/actions/SummarizeCode.py create mode 100644 metagpt/document.py delete mode 100644 metagpt/document_store/document.py create mode 100644 metagpt/document_store/repo_parser.py delete mode 100644 metagpt/software_company.py create mode 100644 metagpt/startup.py delete mode 100644 startup.py diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 325e7c260..bcb9c0c1d 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -5,13 +5,14 @@ Author: garylin2099 ''' import re -from metagpt.const import PROJECT_ROOT, WORKSPACE_ROOT +from metagpt.const import METAGPT_ROOT +from metagpt.config import CONFIG from metagpt.actions import Action from metagpt.roles import Role from metagpt.schema import Message from metagpt.logs import logger -with open(PROJECT_ROOT / "examples/build_customized_agent.py", "r") as f: +with open(METAGPT_ROOT / "examples/build_customized_agent.py", "r") as f: # use official example script to guide AgentCreator MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read() @@ -49,7 +50,7 @@ class CreateAgent(Action): pattern = r'```python(.*)```' match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" - with open(WORKSPACE_ROOT / "agent_created_agent.py", "w") as f: + with open(CONFIG.workspace_path / "agent_created_agent.py", "w") as f: f.write(code_text) return code_text diff --git a/examples/debate.py b/examples/debate.py index a37e60848..0f5d1591b 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -8,7 +8,7 @@ import platform import fire from metagpt.team import Team -from metagpt.actions import Action, BossRequirement +from metagpt.actions import Action, UserRequirement from metagpt.roles import Role from metagpt.schema import Message from metagpt.logs import logger @@ -49,7 +49,7 @@ class Debator(Role): ): super().__init__(name, profile, **kwargs) self._init_actions([SpeakAloud]) - self._watch([BossRequirement, SpeakAloud]) + self._watch([UserRequirement, SpeakAloud]) self.name = name self.opponent_name = opponent_name diff --git a/examples/sk_agent.py b/examples/sk_agent.py index a7513e838..647ea4380 100644 --- a/examples/sk_agent.py +++ b/examples/sk_agent.py @@ -13,7 +13,7 @@ from semantic_kernel.planning import SequentialPlanner # from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.const import SKILL_DIRECTORY from metagpt.roles.sk_agent import SkAgent from metagpt.schema import Message @@ -39,7 +39,7 @@ async def basic_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=UserRequirement)) async def sequential_planner_example(): @@ -53,7 +53,7 @@ async def sequential_planner_example(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=UserRequirement)) async def basic_planner_web_search_example(): @@ -64,7 +64,7 @@ async def basic_planner_web_search_example(): role.import_skill(SkSearchEngine(), "WebSearchSkill") # role.import_semantic_skill_from_directory(skills_directory, "QASkill") - await role.run(Message(content=task, cause_by=BossRequirement)) + await role.run(Message(content=task, cause_by=UserRequirement)) async def action_planner_example(): @@ -75,7 +75,7 @@ async def action_planner_example(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - await role.run(Message(content=task, cause_by=BossRequirement)) # it will choose mathskill.Add + await role.run(Message(content=task, cause_by=UserRequirement)) # it will choose mathskill.Add if __name__ == "__main__": diff --git a/metagpt/actions/SummarizeCode.py b/metagpt/actions/SummarizeCode.py new file mode 100644 index 000000000..1015d3bfb --- /dev/null +++ b/metagpt/actions/SummarizeCode.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Author : alexanderwu +@File : SummarizeCode.py +""" + +from metagpt.actions.action import Action +from metagpt.logs import logger +from metagpt.schema import Message +from metagpt.utils.common import CodeParser +from tenacity import retry, stop_after_attempt, wait_fixed + +PROMPT_TEMPLATE = """ +NOTICE +Role: You are a professional software engineer, and your main task is to review the code. +ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". + +----- +# Context +{context} +----- + +## Code Review All: 请你对历史所有文件进行阅读,分析每个文件是否都完整实现了用户需求,找到可能的bug,如函数未实现、调用错误、未引用等 + +## Summary: 根据历史文件的实现情况进行总结 + +## Call flow: 根据实现的函数,使用mermaid绘制完整的调用链 + +## TODOs: 这里写出需要修改的文件列表,我们会在之后进行修改 + +""" + +FORMAT_EXAMPLE = """ + +## Code Review All + +### a.py +- 它少实现了xxx需求... +- 字段yyy没有给出... +- ... + +### b.py +... + +### c.py +... + +## Call flow +```mermaid +flowchart TB + c1-->a2 + subgraph one + a1-->a2 + end + subgraph two + b1-->b2 + end + subgraph three + c1-->c2 + end +``` + +## Summary +- a.py:... +- b.py:... +- c.py:... +- ... + +## TODOs +1. ... +2. ... +3. ... + +""" + + +class SummarizeCode(Action): + def __init__(self, name="SummaryCode", context: list[Message] = None, llm=None): + super().__init__(name, context, llm) + + @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + async def write_code_review_all(self, prompt): + code_rsp = await self._aask(prompt) + return code_rsp + + async def run(self, context): + format_example = FORMAT_EXAMPLE.format() + prompt = PROMPT_TEMPLATE.format(context=context, format_example=format_example) + logger.info(f'Code review all..') + rsp = await self.write_code_review_all(prompt) + return rsp + \ No newline at end of file diff --git a/metagpt/actions/__init__.py b/metagpt/actions/__init__.py index b004bd58e..79ff94b3e 100644 --- a/metagpt/actions/__init__.py +++ b/metagpt/actions/__init__.py @@ -9,7 +9,7 @@ from enum import Enum from metagpt.actions.action import Action from metagpt.actions.action_output import ActionOutput -from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.debug_error import DebugError from metagpt.actions.design_api import WriteDesign from metagpt.actions.design_api_review import DesignReview @@ -28,7 +28,7 @@ from metagpt.actions.write_test import WriteTest class ActionType(Enum): """All types of Actions, used for indexing.""" - ADD_REQUIREMENT = BossRequirement + ADD_REQUIREMENT = UserRequirement WRITE_PRD = WritePRD WRITE_PRD_REVIEW = WritePRDReview WRITE_DESIGN = WriteDesign diff --git a/metagpt/actions/add_requirement.py b/metagpt/actions/add_requirement.py index 7dc09d062..8e2c56a62 100644 --- a/metagpt/actions/add_requirement.py +++ b/metagpt/actions/add_requirement.py @@ -8,7 +8,7 @@ from metagpt.actions import Action -class BossRequirement(Action): - """Boss Requirement without any implementation details""" +class UserRequirement(Action): + """User Requirement without any implementation details""" async def run(self, *args, **kwargs): raise NotImplementedError diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 75df8b909..f58d49495 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -11,7 +11,6 @@ from typing import List from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template @@ -27,21 +26,20 @@ templates = { ## Format example {format_example} ----- -Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system; make the best use of good open source tools +Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system Requirement: Fill in the following missing information based on the context, each section name is a key in json -Max Output: 8192 chars or 2048 tokens. Try to use them up. -## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. +## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select appropriate open-source frameworks. -## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores +## Python package name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores -## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here +## File list: Provided as Python list[str], the list of files needed (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here -## Data structures and interface definitions: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. +## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. ## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. -## Anything UNCLEAR: Provide as Plain text. Make clear here. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else @@ -52,7 +50,7 @@ and only output the json inside this tag, nothing else "Implementation approach": "We will ...", "Python package name": "snake_game", "File list": ["main.py"], - "Data structures and interface definitions": ' + "Data structures and interfaces": ' classDiagram class Game{ +int score @@ -81,20 +79,19 @@ and only output the json inside this tag, nothing else ----- Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system; make the best use of good open source tools Requirement: Fill in the following missing information based on the context, note that all sections are response with code form separately -Max Output: 8192 chars or 2048 tokens. Try to use them up. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores +## Python package name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores -## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here +## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here -## Data structures and interface definitions: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. +## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. ## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. -## Anything UNCLEAR: Provide as Plain text. Make clear here. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. """, "FORMAT_EXAMPLE": """ @@ -114,7 +111,7 @@ We will ... ] ``` -## Data structures and interface definitions +## Data structures and interfaces ```mermaid classDiagram class Game{ @@ -143,7 +140,7 @@ OUTPUT_MAPPING = { "Implementation approach": (str, ...), "Python package name": (str, ...), "File list": (List[str], ...), - "Data structures and interface definitions": (str, ...), + "Data structures and interfaces": (str, ...), "Program call flow": (str, ...), "Anything UNCLEAR": (str, ...), } @@ -177,8 +174,8 @@ class WriteDesign(Action): async def _save_system_design(self, docs_path, resources_path, system_design): data_api_design = system_design.instruct_content.dict()[ - "Data structures and interface definitions" - ] # CodeParser.parse_code(block="Data structures and interface definitions", text=content) + "Data structures and interfaces" + ] # CodeParser.parse_code(block="Data structures and interfaces", text=content) seq_flow = system_design.instruct_content.dict()[ "Program call flow" ] # CodeParser.parse_code(block="Program call flow", text=content) @@ -193,7 +190,7 @@ class WriteDesign(Action): ws_name = system_design.instruct_content.dict()["Python package name"] else: ws_name = CodeParser.parse_str(block="Python package name", text=system_design) - workspace = WORKSPACE_ROOT / ws_name + workspace = CONFIG.workspace_path / ws_name self.recreate_workspace(workspace) docs_path = workspace / "docs" resources_path = workspace / "resources" diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index b395fa64e..467cb4d83 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -9,7 +9,6 @@ from typing import List from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import WORKSPACE_ROOT from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template from metagpt.utils.json_to_markdown import json_to_markdown @@ -27,9 +26,9 @@ Role: You are a project manager; the goal is to break down tasks according to PR Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. -## Required Python third-party packages: Provided in requirements.txt format +## Required Python third-party packages: Provide Python list[str] in requirements.txt format -## Required Other language third-party packages: Provided in requirements.txt format +## Required Other language third-party packages: Provide Python list[str] in requirements.txt format ## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. @@ -39,7 +38,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. -## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else @@ -95,7 +94,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. -## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. """, "FORMAT_EXAMPLE": ''' @@ -171,11 +170,11 @@ class WriteTasks(Action): ws_name = context[-1].instruct_content.dict()["Python package name"] else: ws_name = CodeParser.parse_str(block="Python package name", text=context[-1].content) - file_path = WORKSPACE_ROOT / ws_name / "docs/api_spec_and_tasks.md" + file_path = CONFIG.workspace_path / ws_name / "docs/api_spec_and_tasks.md" file_path.write_text(json_to_markdown(rsp.instruct_content.dict())) # Write requirements.txt - requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt" + requirements_path = CONFIG.workspace_path / ws_name / "requirements.txt" requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) async def run(self, context, format=CONFIG.prompt_format): diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c000805c5..176718dfc 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -7,7 +7,7 @@ """ from metagpt.actions import WriteDesign from metagpt.actions.action import Action -from metagpt.const import WORKSPACE_ROOT +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser @@ -18,19 +18,22 @@ NOTICE Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language) ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". -## Code: {filename} Write code with triple quoto, based on the following list and context. -1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets -3. Attention1: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Attention2: YOU MUST FOLLOW "Data structures and interface definitions". DONT CHANGE ANY DESIGN. -5. Think before writing: What should be implemented and provided in this document? -6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. -7. Do not use public member functions that do not exist in your design. - ----- # Context {context} ----- + +## Code: {filename} Write code with triple quoto, based on the following list and context. +1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. +2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets +3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. +4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. +5. Think before writing: What should be implemented and provided in this document? +6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. +7. Do not use public member functions that do not exist in your design. +8. Before using a variable, make sure you reference it first +9. Write out EVERY DETAIL, DON'T LEAVE TODO. + ## Format example ----- ## Code: {filename} @@ -58,7 +61,7 @@ class WriteCode(Action): design = [i for i in context if i.cause_by == WriteDesign][0] ws_name = CodeParser.parse_str(block="Python package name", text=design.content) - ws_path = WORKSPACE_ROOT / ws_name + ws_path = CONFIG.workspace_path / ws_name if f"{ws_name}/" not in filename and all(i not in filename for i in ["requirements.txt", ".md"]): ws_path = ws_path / ws_name code_path = ws_path / filename diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4ff4d6cf6..c6538bf7b 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -17,16 +17,14 @@ NOTICE Role: You are a professional software engineer, and your main task is to review the code. You need to ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". -## Code Review: Based on the following context and code, and following the check list, Provide key, clear, concise, and specific code modification suggestions, up to 5. -``` -1. Check 0: Is the code implemented as per the requirements? -2. Check 1: Are there any issues with the code logic? -3. Check 2: Does the existing code follow the "Data structures and interface definitions"? -4. Check 3: Is there a function in the code that is omitted or not fully implemented that needs to be implemented? -5. Check 4: Does the code have unnecessary or lack dependencies? -``` +## Code Review: Based on the following context and code, follow the check list, Provide key, clear, concise, and specific code modification suggestions, up to 5. +1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step. +2. Are there any issues with the code logic? If so, how to solve it? +3. Does the existing code follow the "Data structures and interfaces"? +4. Is there a function in the code that is not fully implemented? If so, how to implement it? +5. Does the code have unnecessary or lack dependencies? If so, how to solve it? -## Rewrite Code: {filename} Base on "Code Review" and the source code, rewrite code with triple quotes. Do your utmost to optimize THIS SINGLE FILE. +## Rewrite Code: rewrite {filename} based on "Code Review" with triple quotes. Do your utmost to optimize THIS SINGLE FILE. Implement ALL TODO. ----- # Context {context} @@ -47,7 +45,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc FORMAT_EXAMPLE = """ ## Code Review -1. The code ... +1. No, we should add the logic of ... 2. ... 3. ... 4. ... diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bd04ca79e..584d31998 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -46,24 +46,25 @@ quadrantChart {format_example} ----- Role: You are a professional product manager; the goal is to design a concise, usable, efficient product -Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design +Requirements: According to the context, fill in the following missing information, each section name is a key in json ## Original Requirements: Provide as Plain text, place the polished complete original requirements here -## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. If the requirement itself is simple, the goal should also be simple +## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. -## User Stories: Provided as Python list[str], up to 5 scenario-based user stories, If the requirement itself is simple, the user stories should also be less +## User Stories: Provided as Python list[str], up to 5 scenario-based user stories -## Competitive Analysis: Provided as Python list[str], up to 7 competitive product analyses, consider as similar competitors as possible +## Competitive Analysis: Provided as Python list[str], up to 8 competitive product analyses ## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. -## Requirement Analysis: Provide as Plain text. Be simple. LESS IS MORE. Make your requirements less dumb. Delete the parts unnessasery. +## Requirement Analysis: Provide as Plain text. -## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards; no more than 5 requirements and consider to make its difficulty lower +## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards ## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. -## Anything UNCLEAR: Provide as Plain text. Make clear here. + +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else @@ -131,30 +132,30 @@ quadrantChart {format_example} ----- Role: You are a professional product manager; the goal is to design a concise, usable, efficient product -Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. If the requirements are unclear, ensure minimum viability and avoid excessive design +Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. AND '## ' SHOULD WRITE BEFORE the code and triple quote. Output carefully referenced "Format example" in format. ## Original Requirements: Provide as Plain text, place the polished complete original requirements here -## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. If the requirement itself is simple, the goal should also be simple +## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. -## User Stories: Provided as Python list[str], up to 5 scenario-based user stories, If the requirement itself is simple, the user stories should also be less +## User Stories: Provided as Python list[str], up to 5 scenario-based user stories ## Competitive Analysis: Provided as Python list[str], up to 7 competitive product analyses, consider as similar competitors as possible ## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. -## Requirement Analysis: Provide as Plain text. Be simple. LESS IS MORE. Make your requirements less dumb. Delete the parts unnessasery. +## Requirement Analysis: Provide as Plain text. -## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards; no more than 5 requirements and consider to make its difficulty lower +## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards ## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. -## Anything UNCLEAR: Provide as Plain text. Make clear here. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. """, "FORMAT_EXAMPLE": """ --- ## Original Requirements -The boss ... +The user ... ## Product Goals ```python diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 35ff36dc2..2f4988c09 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -15,7 +15,7 @@ NOTICE 2. Requirement: Based on the context, develop a comprehensive test suite that adequately covers all relevant aspects of the code file under review. Your test suite will be part of the overall project QA, so please develop complete, robust, and reusable test cases. 3. Attention1: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the test case or script. 4. Attention2: If there are any settings in your tests, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -5. Attention3: YOU MUST FOLLOW "Data structures and interface definitions". DO NOT CHANGE ANY DESIGN. Make sure your tests respect the existing design and ensure its validity. +5. Attention3: YOU MUST FOLLOW "Data structures and interfaces". DO NOT CHANGE ANY DESIGN. Make sure your tests respect the existing design and ensure its validity. 6. Think before writing: What should be tested and validated in this document? What edge cases could exist? What might fail? 7. CAREFULLY CHECK THAT YOU DON'T MISS ANY NECESSARY TEST CASES/SCRIPTS IN THIS FILE. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the test case or script and triple quotes. diff --git a/metagpt/config.py b/metagpt/config.py index 3f9e742bd..1a9cdb4d2 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -8,7 +8,9 @@ import os import openai import yaml -from metagpt.const import PROJECT_ROOT +from pathlib import Path + +from metagpt.const import METAGPT_ROOT, DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.tools import SearchEngineType, WebBrowserEngineType from metagpt.utils.singleton import Singleton @@ -35,13 +37,14 @@ class Config(metaclass=Singleton): """ _instance = None - key_yaml_file = PROJECT_ROOT / "config/key.yaml" - default_yaml_file = PROJECT_ROOT / "config/config.yaml" + home_yaml_file = Path.home() / ".metagpt/config.yaml" + key_yaml_file = METAGPT_ROOT / "config/key.yaml" + default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): self._configs = {} self._init_with_config_files_and_env(self._configs, yaml_file) - logger.info("Config loading done.") + # logger.info("Config loading done.") self.global_proxy = self._get("GLOBAL_PROXY") self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") @@ -94,12 +97,18 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.prompt_format = self._get("PROMPT_FORMAT", "markdown") + self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) + self._ensure_workspace_exists() + + def _ensure_workspace_exists(self): + self.workspace_path.mkdir(parents=True, exist_ok=True) + logger.info(f"WORKSPACE_PATH set to {self.workspace_path}") def _init_with_config_files_and_env(self, configs: dict, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" configs.update(os.environ) - for _yaml_file in [yaml_file, self.key_yaml_file]: + for _yaml_file in [yaml_file, self.key_yaml_file, self.home_yaml_file]: if not _yaml_file.exists(): continue diff --git a/metagpt/const.py b/metagpt/const.py index 407ce803a..14e692487 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -5,44 +5,54 @@ @Author : alexanderwu @File : const.py """ +import os from pathlib import Path from loguru import logger - -def get_project_root(): - """Search upwards to find the project root directory.""" - current_path = Path.cwd() - while True: - if ( - (current_path / ".git").exists() - or (current_path / ".project_root").exists() - or (current_path / ".gitignore").exists() - ): - # use metagpt with git clone will land here - logger.info(f"PROJECT_ROOT set to {str(current_path)}") - return current_path - parent_path = current_path.parent - if parent_path == current_path: - # use metagpt with pip install will land here - cwd = Path.cwd() - logger.info(f"PROJECT_ROOT set to current working directory: {str(cwd)}") - return cwd - current_path = parent_path +import metagpt -PROJECT_ROOT = get_project_root() -DATA_PATH = PROJECT_ROOT / "data" -WORKSPACE_ROOT = PROJECT_ROOT / "workspace" -PROMPT_PATH = PROJECT_ROOT / "metagpt/prompts" -UT_PATH = PROJECT_ROOT / "data/ut" -SWAGGER_PATH = UT_PATH / "files/api/" -UT_PY_PATH = UT_PATH / "files/ut/" -API_QUESTIONS_PATH = UT_PATH / "files/question/" -YAPI_URL = "http://yapi.deepwisdomai.com/" -TMP = PROJECT_ROOT / "tmp" +def get_metagpt_package_root(): + """Get the root directory of the installed package.""" + package_root = Path(metagpt.__file__).parent.parent + logger.info(f"Package root set to {str(package_root)}") + return package_root + + +def get_metagpt_root(): + """Get the project root directory.""" + # Check if a project root is specified in the environment variable + project_root_env = os.getenv('METAGPT_PROJECT_ROOT') + if project_root_env: + project_root = Path(project_root_env) + logger.info(f"PROJECT_ROOT set from environment variable to {str(project_root)}") + else: + # Fallback to package root if no environment variable is set + project_root = get_metagpt_package_root() + return project_root + + +# METAGPT PROJECT ROOT AND VARS + +METAGPT_ROOT = get_metagpt_root() +DEFAULT_WORKSPACE_ROOT = METAGPT_ROOT / "workspace" + +DATA_PATH = METAGPT_ROOT / "data" RESEARCH_PATH = DATA_PATH / "research" TUTORIAL_PATH = DATA_PATH / "tutorial_docx" INVOICE_OCR_TABLE_PATH = DATA_PATH / "invoice_table" +UT_PATH = DATA_PATH / "ut" +SWAGGER_PATH = UT_PATH / "files/api/" +UT_PY_PATH = UT_PATH / "files/ut/" +API_QUESTIONS_PATH = UT_PATH / "files/question/" -SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" +TMP = METAGPT_ROOT / "tmp" + +SOURCE_ROOT = METAGPT_ROOT / "metagpt" +PROMPT_PATH = SOURCE_ROOT / "prompts" +SKILL_DIRECTORY = SOURCE_ROOT / "skills" + + +# REAL CONSTS MEM_TTL = 24 * 30 * 3600 +YAPI_URL = "http://yapi.deepwisdomai.com/" diff --git a/metagpt/document.py b/metagpt/document.py new file mode 100644 index 000000000..044210218 --- /dev/null +++ b/metagpt/document.py @@ -0,0 +1,207 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/6/8 14:03 +@Author : alexanderwu +@File : document.py +""" + +from typing import Union, Optional +from pathlib import Path +from pydantic import BaseModel, Field +import pandas as pd +from langchain.document_loaders import ( + TextLoader, + UnstructuredPDFLoader, + UnstructuredWordDocumentLoader, +) +from langchain.text_splitter import CharacterTextSplitter +from tqdm import tqdm + +from metagpt.logs import logger + + +def validate_cols(content_col: str, df: pd.DataFrame): + if content_col not in df.columns: + raise ValueError("Content column not found in DataFrame.") + + +def read_data(data_path: Path): + suffix = data_path.suffix + if '.xlsx' == suffix: + data = pd.read_excel(data_path) + elif '.csv' == suffix: + data = pd.read_csv(data_path) + elif '.json' == suffix: + data = pd.read_json(data_path) + elif suffix in ('.docx', '.doc'): + data = UnstructuredWordDocumentLoader(str(data_path), mode='elements').load() + elif '.txt' == suffix: + data = TextLoader(str(data_path)).load() + text_splitter = CharacterTextSplitter(separator='\n', chunk_size=256, chunk_overlap=0) + texts = text_splitter.split_documents(data) + data = texts + elif '.pdf' == suffix: + data = UnstructuredPDFLoader(str(data_path), mode="elements").load() + else: + raise NotImplementedError("File format not supported.") + return data + + +class Document(BaseModel): + """ + Document: Handles operations related to document files. + """ + content: str = Field(default='') + file_path: Path = Field(default=None) + + @classmethod + def from_path(cls, file_path: Path): + """ + Create a Document instance from a file path. + """ + if not file_path.exists(): + raise FileNotFoundError(f"File {file_path} not found.") + content = file_path.read_text() + return cls(content=content, file_path=file_path) + + @classmethod + def from_text(cls, text: str, file_path: Optional[Path] = None): + """ + Create a Document from a text string. + """ + return cls(content=text, file_path=file_path) + + def to_path(self, file_path: Optional[Path] = None): + """ + Save content to the specified file path. + """ + if file_path is not None: + self.file_path = file_path + + if self.file_path is None: + raise ValueError("File path is not set.") + + self.file_path.parent.mkdir(parents=True, exist_ok=True) + self.file_path.write_text(self.content) + + def persist(self): + """ + Persist document to disk. + """ + return self.to_path() + + +class IndexableDocument(Document): + """ + Advanced document handling: For vector databases or search engines. + """ + data: Union[pd.DataFrame, list] + content_col: Optional[str] = Field(default='') + meta_col: Optional[str] = Field(default='') + + class Config: + arbitrary_types_allowed = True + + @classmethod + def from_path(cls, data_path: Path, content_col='content', meta_col='metadata'): + if not data_path.exists(): + raise FileNotFoundError(f"File {data_path} not found.") + data = read_data(data_path) + content = data_path.read_text() + if isinstance(data, pd.DataFrame): + validate_cols(content_col, data) + return cls(data=data, content=content, content_col=content_col, meta_col=meta_col) + + def _get_docs_and_metadatas_by_df(self) -> (list, list): + df = self.data + docs = [] + metadatas = [] + for i in tqdm(range(len(df))): + docs.append(df[self.content_col].iloc[i]) + if self.meta_col: + metadatas.append({self.meta_col: df[self.meta_col].iloc[i]}) + else: + metadatas.append({}) + return docs, metadatas + + def _get_docs_and_metadatas_by_langchain(self) -> (list, list): + data = self.data + docs = [i.page_content for i in data] + metadatas = [i.metadata for i in data] + return docs, metadatas + + def get_docs_and_metadatas(self) -> (list, list): + if isinstance(self.data, pd.DataFrame): + return self._get_docs_and_metadatas_by_df() + elif isinstance(self.data, list): + return self._get_docs_and_metadatas_by_langchain() + else: + raise NotImplementedError("Data type not supported for metadata extraction.") + + +class Repo(BaseModel): + + # Name of this repo. + name: str = Field(default="") + docs: dict[Path, Document] = Field(default_factory=dict) + codes: dict[Path, Document] = Field(default_factory=dict) + assets: dict[Path, Document] = Field(default_factory=dict) + repo_path: Path = Field(default_factory=Path) + + def _path(self, filename): + return self.repo_path / filename + + @classmethod + def from_path(cls, repo_path: Path): + """Load documents, code, and assets from a repository path.""" + repo_path.mkdir(parents=True, exist_ok=True) + repo = Repo(repo_path = repo_path) + for file_path in repo_path.rglob('*'): + if file_path.is_file(): + repo._set(file_path.read_text(), file_path) + return repo + + def to_path(self): + """Persist all documents, code, and assets to the given repository path.""" + for doc in self.docs.values(): + doc.to_path() + for code in self.codes.values(): + code.to_path() + for asset in self.assets.values(): + asset.to_path() + + def _set(self, content: str, file_path: Path): + """Add a document to the appropriate category based on its file extension.""" + file_ext = file_path.suffix + + doc = Document(content=content, file_path=file_path) + if file_ext.lower() == '.md': + self.docs[file_path] = doc + elif file_ext.lower() in ['.py', '.js', '.css', '.html']: + self.codes[file_path] = doc + else: + self.assets[file_path] = doc + return doc + + def set(self, content: str, filename: str): + """Set a document and persist it to disk.""" + file_path = self._path(filename) + doc = self._set(content, file_path) + doc.to_path() + + def get(self, filename: str) -> Optional[Document]: + """Get a document by its filename.""" + path = self._path(filename) + return self.docs.get(path) or self.codes.get(path) or self.assets.get(path) + + +def main(): + repo1 = Repo.from_path(Path("/Users/alexanderwu/workspace/t1")) + repo1.set("wtf content", "doc/wtf_file.md") + repo1.set("wtf code", "code/wtf_file.py") + logger.info(repo1) # check doc + + +if __name__ == '__main__': + main() diff --git a/metagpt/document_store/base_store.py b/metagpt/document_store/base_store.py index 5d7015e8b..84b47a98c 100644 --- a/metagpt/document_store/base_store.py +++ b/metagpt/document_store/base_store.py @@ -28,20 +28,20 @@ class BaseStore(ABC): class LocalStore(BaseStore, ABC): - def __init__(self, raw_data: Path, cache_dir: Path = None): - if not raw_data: + def __init__(self, raw_data_path: Path, cache_dir: Path = None): + if not raw_data_path: raise FileNotFoundError self.config = Config() - self.raw_data = raw_data + self.raw_data_path = raw_data_path if not cache_dir: - cache_dir = raw_data.parent + cache_dir = raw_data_path.parent self.cache_dir = cache_dir self.store = self._load() if not self.store: self.store = self.write() def _get_index_and_store_fname(self): - fname = self.raw_data.name.split('.')[0] + fname = self.raw_data_path.name.split('.')[0] index_file = self.cache_dir / f"{fname}.index" store_file = self.cache_dir / f"{fname}.pkl" return index_file, store_file diff --git a/metagpt/document_store/document.py b/metagpt/document_store/document.py deleted file mode 100644 index e4b9473c7..000000000 --- a/metagpt/document_store/document.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/6/8 14:03 -@Author : alexanderwu -@File : document.py -""" -from pathlib import Path - -import pandas as pd -from langchain.document_loaders import ( - TextLoader, - UnstructuredPDFLoader, - UnstructuredWordDocumentLoader, -) -from langchain.text_splitter import CharacterTextSplitter -from tqdm import tqdm - - -def validate_cols(content_col: str, df: pd.DataFrame): - if content_col not in df.columns: - raise ValueError - - -def read_data(data_path: Path): - suffix = data_path.suffix - if '.xlsx' == suffix: - data = pd.read_excel(data_path) - elif '.csv' == suffix: - data = pd.read_csv(data_path) - elif '.json' == suffix: - data = pd.read_json(data_path) - elif suffix in ('.docx', '.doc'): - data = UnstructuredWordDocumentLoader(str(data_path), mode='elements').load() - elif '.txt' == suffix: - data = TextLoader(str(data_path)).load() - text_splitter = CharacterTextSplitter(separator='\n', chunk_size=256, chunk_overlap=0) - texts = text_splitter.split_documents(data) - data = texts - elif '.pdf' == suffix: - data = UnstructuredPDFLoader(str(data_path), mode="elements").load() - else: - raise NotImplementedError - return data - - -class Document: - - def __init__(self, data_path, content_col='content', meta_col='metadata'): - self.data = read_data(data_path) - if isinstance(self.data, pd.DataFrame): - validate_cols(content_col, self.data) - self.content_col = content_col - self.meta_col = meta_col - - def _get_docs_and_metadatas_by_df(self) -> (list, list): - df = self.data - docs = [] - metadatas = [] - for i in tqdm(range(len(df))): - docs.append(df[self.content_col].iloc[i]) - if self.meta_col: - metadatas.append({self.meta_col: df[self.meta_col].iloc[i]}) - else: - metadatas.append({}) - - return docs, metadatas - - def _get_docs_and_metadatas_by_langchain(self) -> (list, list): - data = self.data - docs = [i.page_content for i in data] - metadatas = [i.metadata for i in data] - return docs, metadatas - - def get_docs_and_metadatas(self) -> (list, list): - if isinstance(self.data, pd.DataFrame): - return self._get_docs_and_metadatas_by_df() - elif isinstance(self.data, list): - return self._get_docs_and_metadatas_by_langchain() - else: - raise NotImplementedError - \ No newline at end of file diff --git a/metagpt/document_store/faiss_store.py b/metagpt/document_store/faiss_store.py index dd450010d..885ad3e15 100644 --- a/metagpt/document_store/faiss_store.py +++ b/metagpt/document_store/faiss_store.py @@ -15,15 +15,15 @@ from langchain.vectorstores import FAISS from metagpt.const import DATA_PATH from metagpt.document_store.base_store import LocalStore -from metagpt.document_store.document import Document +from metagpt.document import IndexableDocument from metagpt.logs import logger class FaissStore(LocalStore): - def __init__(self, raw_data: Path, cache_dir=None, meta_col='source', content_col='output'): + def __init__(self, raw_data_path: Path, cache_dir=None, meta_col='source', content_col='output'): self.meta_col = meta_col self.content_col = content_col - super().__init__(raw_data, cache_dir) + super().__init__(raw_data_path, cache_dir) def _load(self) -> Optional["FaissStore"]: index_file, store_file = self._get_index_and_store_fname() @@ -60,9 +60,9 @@ class FaissStore(LocalStore): def write(self): """Initialize the index and library based on the Document (JSON / XLSX, etc.) file provided by the user.""" - if not self.raw_data.exists(): + if not self.raw_data_path.exists(): raise FileNotFoundError - doc = Document(self.raw_data, self.content_col, self.meta_col) + doc = IndexableDocument.from_path(self.raw_data_path, self.content_col, self.meta_col) docs, metadatas = doc.get_docs_and_metadatas() self.store = self._write(docs, metadatas) diff --git a/metagpt/document_store/repo_parser.py b/metagpt/document_store/repo_parser.py new file mode 100644 index 000000000..f7e2b0f4a --- /dev/null +++ b/metagpt/document_store/repo_parser.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/11/17 17:58 +@Author : alexanderwu +@File : repo_parser.py +""" +import json +import pathlib +import ast + +import pandas as pd + + +class RepoParser: + def __init__(self): + self.base_directory = None + + def parse_file(self, file_path): + """Parse a Python file in the repository.""" + try: + return ast.parse(file_path.read_text()).body + except: + return [] + + def extract_class_and_function_info(self, tree, file_path): + """Extract class, function, and global variable information from the AST.""" + file_info = { + "file": str(file_path.relative_to(self.base_directory)), + "classes": [], + "functions": [], + "globals": [] + } + + for node in tree: + if isinstance(node, ast.ClassDef): + class_methods = [m.name for m in node.body if is_func(m)] + file_info["classes"].append({"name": node.name, "methods": class_methods}) + elif is_func(node): + file_info["functions"].append(node.name) + elif isinstance(node, ast.Assign) or isinstance(node, ast.AnnAssign): + for target in node.targets if isinstance(node, ast.Assign) else [node.target]: + if isinstance(target, ast.Name): + file_info["globals"].append(target.id) + return file_info + + def generate_json_structure(self, directory, output_path): + """Generate a JSON file documenting the repository structure.""" + files_classes = [] + for path in directory.rglob('*.py'): + tree = self.parse_file(path) + file_info = self.extract_class_and_function_info(tree, path) + files_classes.append(file_info) + + output_path.write_text(json.dumps(files_classes, indent=4)) + + def generate_dataframe_structure(self, directory, output_path): + """Generate a DataFrame documenting the repository structure and save as CSV.""" + files_classes = [] + for path in directory.rglob('*.py'): + tree = self.parse_file(path) + file_info = self.extract_class_and_function_info(tree, path) + files_classes.append(file_info) + + df = pd.DataFrame(files_classes) + df.to_csv(output_path, index=False) + + def generate_structure(self, directory_path, output_path=None, mode='json'): + """Generate the structure of the repository as a specified format.""" + self.base_directory = pathlib.Path(directory_path) + output_file = self.base_directory / f"{self.base_directory.name}-structure.{mode}" + output_path = pathlib.Path(output_path) if output_path else output_file + + if mode == 'json': + self.generate_json_structure(self.base_directory, output_path) + elif mode == 'csv': + self.generate_dataframe_structure(self.base_directory, output_path) + + +def is_func(node): + return isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) + + +def main(): + repo_parser = RepoParser() + repo_parser.generate_structure("/Users/alexanderwu/git/mg1/metagpt", "/Users/alexanderwu/git/mg1/mg1-structure.csv", mode='csv') + + +if __name__ == '__main__': + main() diff --git a/metagpt/environment.py b/metagpt/environment.py index 24e6ada2f..38077c90d 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -10,20 +10,22 @@ from typing import Iterable from pydantic import BaseModel, Field +# from metagpt.document import Document +from metagpt.document import Repo from metagpt.memory import Memory from metagpt.roles import Role from metagpt.schema import Message class Environment(BaseModel): - """环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到 - Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles - + """ + Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles """ roles: dict[str, Role] = Field(default_factory=dict) memory: Memory = Field(default_factory=Memory) history: str = Field(default='') + repo: Repo = Field(default_factory=Repo) class Config: arbitrary_types_allowed = True @@ -50,6 +52,10 @@ class Environment(BaseModel): self.memory.add(message) self.history += f"\n{message}" + def publish_doc(self, content: str, filename: str): + """向当前环境发布文档(包括代码)""" + self.repo.set(content, filename) + async def run(self, k=1): """处理一次所有信息的运行 Process all Role runs at once diff --git a/metagpt/logs.py b/metagpt/logs.py index b2052e9b8..afebbfed9 100644 --- a/metagpt/logs.py +++ b/metagpt/logs.py @@ -10,15 +10,15 @@ import sys from loguru import logger as _logger -from metagpt.const import PROJECT_ROOT +from metagpt.const import METAGPT_ROOT + def define_log_level(print_level="INFO", logfile_level="DEBUG"): - """调整日志级别到level之上 - Adjust the log level to above level - """ + """Adjust the log level to above level""" _logger.remove() _logger.add(sys.stderr, level=print_level) - _logger.add(PROJECT_ROOT / 'logs/log.txt', level=logfile_level) + _logger.add(METAGPT_ROOT / 'logs/log.txt', level=logfile_level) return _logger + logger = define_log_level() diff --git a/metagpt/manager.py b/metagpt/manager.py index 9d238c621..7cbbe651e 100644 --- a/metagpt/manager.py +++ b/metagpt/manager.py @@ -14,7 +14,7 @@ class Manager: def __init__(self, llm: LLM = LLM()): self.llm = llm # Large Language Model self.role_directions = { - "BOSS": "Product Manager", + "User": "Product Manager", "Product Manager": "Architect", "Architect": "Engineer", "Engineer": "QA Engineer", diff --git a/metagpt/memory/__init__.py b/metagpt/memory/__init__.py index 710930626..bd6e72163 100644 --- a/metagpt/memory/__init__.py +++ b/metagpt/memory/__init__.py @@ -7,10 +7,10 @@ """ from metagpt.memory.memory import Memory -from metagpt.memory.longterm_memory import LongTermMemory +# from metagpt.memory.longterm_memory import LongTermMemory __all__ = [ "Memory", - "LongTermMemory", + # "LongTermMemory", ] diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 1f6685b38..171af47f0 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -11,7 +11,8 @@ from collections import OrderedDict from pathlib import Path from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks -from metagpt.const import WORKSPACE_ROOT +from metagpt.actions.SummarizeCode import SummarizeCode +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message @@ -80,13 +81,13 @@ class Engineer(Role): self.n_borg = n_borg @classmethod - def parse_tasks(self, task_msg: Message) -> list[str]: + def parse_tasks(cls, task_msg: Message) -> list[str]: if task_msg.instruct_content: return task_msg.instruct_content.dict().get("Task list") return CodeParser.parse_file_list(block="Task list", text=task_msg.content) @classmethod - def parse_code(self, code_text: str) -> str: + def parse_code(cls, code_text: str) -> str: return CodeParser.parse_code(block="", text=code_text) @classmethod @@ -98,10 +99,10 @@ class Engineer(Role): def get_workspace(self) -> Path: msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: - return WORKSPACE_ROOT / "src" + return CONFIG.workspace_path / "src" workspace = self.parse_workspace(msg) # Codes are written in workspace/{package_name}/{package_name} - return WORKSPACE_ROOT / workspace / workspace + return CONFIG.workspace_path / workspace / workspace def recreate_workspace(self): workspace = self.get_workspace() @@ -167,7 +168,7 @@ class Engineer(Role): ) return msg - async def _act_sp_precision(self) -> Message: + async def _act_sp_with_cr(self) -> Message: code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later for todo in self.todos: """ @@ -191,7 +192,6 @@ class Engineer(Role): code = rewrite_code except Exception as e: logger.error("code review failed!", e) - pass file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=WriteCode) self._rc.memory.add(msg) @@ -199,6 +199,13 @@ class Engineer(Role): code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) + context = [] + msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) + for m in msg: + context.append(m.content) + context_str = "\n".join(context) + code_review_all = await SummarizeCode().run(context=context_str) + logger.info(f"Done {self.get_workspace()} generating.") msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" @@ -209,5 +216,5 @@ class Engineer(Role): """Determines the mode of action based on whether code review is used.""" logger.info(f"{self._setting}: ready to WriteCode") if self.use_code_review: - return await self._act_sp_precision() + return await self._act_sp_with_cr() return await self._act_sp() diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a58ea5385..f6172b607 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -5,7 +5,7 @@ @Author : alexanderwu @File : product_manager.py """ -from metagpt.actions import BossRequirement, WritePRD +from metagpt.actions import UserRequirement, WritePRD from metagpt.roles import Role @@ -38,4 +38,4 @@ class ProductManager(Role): """ super().__init__(name, profile, goal, constraints) self._init_actions([WritePRD]) - self._watch([BossRequirement]) + self._watch([UserRequirement]) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index a763c2ce8..f124646b3 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -16,7 +16,8 @@ from metagpt.actions import ( WriteDesign, WriteTest, ) -from metagpt.const import WORKSPACE_ROOT +# from metagpt.const import WORKSPACE_ROOT +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message @@ -50,13 +51,13 @@ class QaEngineer(Role): def get_workspace(self, return_proj_dir=True) -> Path: msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: - return WORKSPACE_ROOT / "src" + return CONFIG.workspace_path / "src" workspace = self.parse_workspace(msg) # project directory: workspace/{package_name}, which contains package source code folder, tests folder, resources folder, etc. if return_proj_dir: - return WORKSPACE_ROOT / workspace + return CONFIG.workspace_path / workspace # development codes directory: workspace/{package_name}/{package_name} - return WORKSPACE_ROOT / workspace / workspace + return CONFIG.workspace_path / workspace / workspace def write_file(self, filename: str, code: str): workspace = self.get_workspace() / "tests" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b96c361c0..d772c0748 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -17,7 +17,8 @@ from metagpt.config import CONFIG from metagpt.actions import Action, ActionOutput from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger -from metagpt.memory import Memory, LongTermMemory +from metagpt.memory import Memory +# from metagpt.memory import LongTermMemory from metagpt.schema import Message PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -78,7 +79,7 @@ class RoleContext(BaseModel): """Role Runtime Context""" env: 'Environment' = Field(default=None) memory: Memory = Field(default_factory=Memory) - long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) + # long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index b27841d74..4069f4836 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -9,7 +9,7 @@ from semantic_kernel.planning import SequentialPlanner from semantic_kernel.planning.action_planner.action_planner import ActionPlanner from semantic_kernel.planning.basic_planner import BasicPlanner -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.actions.execute_task import ExecuteTask from metagpt.logs import logger from metagpt.roles import Role @@ -39,7 +39,7 @@ class SkAgent(Role): """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) self._init_actions([ExecuteTask()]) - self._watch([BossRequirement]) + self._watch([UserRequirement]) self.kernel = make_sk_kernel() # how funny the interface is inconsistent diff --git a/metagpt/software_company.py b/metagpt/software_company.py deleted file mode 100644 index d44a0068a..000000000 --- a/metagpt/software_company.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/12 00:30 -@Author : alexanderwu -@File : software_company.py -""" -from metagpt.team import Team as SoftwareCompany - -import warnings -warnings.warn("metagpt.software_company is deprecated and will be removed in the future" - "Please use metagpt.team instead. SoftwareCompany class is now named as Team.", - DeprecationWarning, 2) diff --git a/metagpt/startup.py b/metagpt/startup.py new file mode 100644 index 000000000..d8ca4072f --- /dev/null +++ b/metagpt/startup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from pathlib import Path +import asyncio +import typer + +app = typer.Typer() + + +@app.command() +def startup( + idea: str = typer.Argument(..., help="Your innovative idea, such as 'Create a 2048 game.'"), + investment: float = typer.Option(3.0, help="Dollar amount to invest in the AI company."), + n_round: int = typer.Option(5, help="Number of rounds for the simulation."), + code_review: bool = typer.Option(True, help="Whether to use code review."), + run_tests: bool = typer.Option(False, help="Whether to enable QA for adding & running tests."), + implement: bool = typer.Option(True, help="Enable or disable code implementation."), + project_name: str = typer.Option("", help="Unique project name, such as 'game_2048'"), +): + """Run a startup. Be a boss.""" + from metagpt.roles import ProductManager, Architect, ProjectManager, Engineer, QaEngineer + from metagpt.team import Team + + company = Team() + company.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) + + if implement or code_review: + company.hire([Engineer(n_borg=5, use_code_review=code_review)]) + + if run_tests: + company.hire([QaEngineer()]) + + company.invest(investment) + company.start_project(project_name, idea) + asyncio.run(company.run(n_round=n_round)) + + +if __name__ == "__main__": + app() diff --git a/metagpt/team.py b/metagpt/team.py index 67d3ecec8..2332aaa46 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -7,7 +7,7 @@ """ from pydantic import BaseModel, Field -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.config import CONFIG from metagpt.environment import Environment from metagpt.logs import logger @@ -21,7 +21,7 @@ class Team(BaseModel): Team: Possesses one or more roles (agents), SOP (Standard Operating Procedures), and a platform for instant messaging, dedicated to perform any multi-agent activity, such as collaboratively writing executable code. """ - environment: Environment = Field(default_factory=Environment) + env: Environment = Field(default_factory=Environment) investment: float = Field(default=10.0) idea: str = Field(default="") @@ -30,7 +30,7 @@ class Team(BaseModel): def hire(self, roles: list[Role]): """Hire roles to cooperate""" - self.environment.add_roles(roles) + self.env.add_roles(roles) def invest(self, investment: float): """Invest company. raise NoMoneyException when exceed max_budget.""" @@ -42,10 +42,12 @@ class Team(BaseModel): if CONFIG.total_cost > CONFIG.max_budget: raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}') - def start_project(self, idea, send_to: str = ""): - """Start a project from publishing boss requirement.""" + def start_project(self, project_name, idea, send_to: str = ""): + """Start a project from publishing user requirement.""" self.idea = idea - self.environment.publish_message(Message(role="Human", content=idea, cause_by=BossRequirement, send_to=send_to)) + # If user set project_name, then use it. + self.env.repo.name = project_name + self.env.publish_message(Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to)) def _save(self): logger.info(self.json()) @@ -57,6 +59,6 @@ class Team(BaseModel): n_round -= 1 logger.debug(f"{n_round=}") self._check_balance() - await self.environment.run() - return self.environment.history + await self.env.run() + return self.env.history \ No newline at end of file diff --git a/metagpt/tools/sd_engine.py b/metagpt/tools/sd_engine.py index 1d9cd0b2a..4f010a912 100644 --- a/metagpt/tools/sd_engine.py +++ b/metagpt/tools/sd_engine.py @@ -13,12 +13,10 @@ from typing import List from aiohttp import ClientSession from PIL import Image, PngImagePlugin -from metagpt.config import Config -from metagpt.const import WORKSPACE_ROOT +from metagpt.config import CONFIG +# from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger -config = Config() - payload = { "prompt": "", "negative_prompt": "(easynegative:0.8),black, dark,Low resolution", @@ -56,9 +54,8 @@ default_negative_prompt = "(easynegative:0.8),black, dark,Low resolution" class SDEngine: def __init__(self): # Initialize the SDEngine with configuration - self.config = Config() - self.sd_url = self.config.get("SD_URL") - self.sd_t2i_url = f"{self.sd_url}{self.config.get('SD_T2I_API')}" + self.sd_url = CONFIG.get("SD_URL") + self.sd_t2i_url = f"{self.sd_url}{CONFIG.get('SD_T2I_API')}" # Define default payload settings for SD API self.payload = payload logger.info(self.sd_t2i_url) @@ -81,7 +78,7 @@ class SDEngine: return self.payload def _save(self, imgs, save_name=""): - save_dir = WORKSPACE_ROOT / "resources" / "SD_Output" + save_dir = CONFIG.workspace_path / "resources" / "SD_Output" if not os.path.exists(save_dir): os.makedirs(save_dir, exist_ok=True) batch_decode_base64_to_image(imgs, save_dir, save_name=save_name) @@ -125,6 +122,7 @@ def batch_decode_base64_to_image(imgs, save_dir="", save_name=""): save_name = join(save_dir, save_name) decode_base64_to_image(_img, save_name=save_name) + if __name__ == "__main__": engine = SDEngine() prompt = "pixel style, game design, a game interface should be minimalistic and intuitive with the score and high score displayed at the top. The snake and its food should be easily distinguishable. The game should have a simple color scheme, with a contrasting color for the snake and its food. Complete interface boundary" diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 204c22c67..eb85a3f90 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -10,7 +10,7 @@ import os from pathlib import Path from metagpt.config import CONFIG -from metagpt.const import PROJECT_ROOT +from metagpt.const import METAGPT_ROOT from metagpt.logs import logger from metagpt.utils.common import check_cmd_exists @@ -69,7 +69,7 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, if stdout: logger.info(stdout.decode()) if stderr: - logger.error(stderr.decode()) + logger.warning(stderr.decode()) else: if engine == "playwright": from metagpt.utils.mmdc_playwright import mermaid_to_file @@ -141,6 +141,6 @@ MMC2 = """sequenceDiagram if __name__ == "__main__": loop = asyncio.new_event_loop() - result = loop.run_until_complete(mermaid_to_file(MMC1, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1")) - result = loop.run_until_complete(mermaid_to_file(MMC2, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1")) + result = loop.run_until_complete(mermaid_to_file(MMC1, METAGPT_ROOT / f"{CONFIG.mermaid_engine}/1")) + result = loop.run_until_complete(mermaid_to_file(MMC2, METAGPT_ROOT / f"{CONFIG.mermaid_engine}/1")) loop.close() diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index 1af96f272..33bcd01a5 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -21,6 +21,7 @@ TOKEN_COSTS = { "gpt-4-32k": {"prompt": 0.06, "completion": 0.12}, "gpt-4-32k-0314": {"prompt": 0.06, "completion": 0.12}, "gpt-4-0613": {"prompt": 0.06, "completion": 0.12}, + "gpt-4-1106-preview": {"prompt": 0.01, "completion": 0.03}, "text-embedding-ada-002": {"prompt": 0.0004, "completion": 0.0}, "chatglm_turbo": {"prompt": 0.0, "completion": 0.00069} # 32k version, prompt + completion tokens=0.005¥/k-tokens } @@ -37,6 +38,7 @@ TOKEN_MAX = { "gpt-4-32k": 32768, "gpt-4-32k-0314": 32768, "gpt-4-0613": 8192, + "gpt-4-1106-preview": 128000, "text-embedding-ada-002": 8192, "chatglm_turbo": 32768 } @@ -56,16 +58,17 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): "gpt-4-32k-0314", "gpt-4-0613", "gpt-4-32k-0613", + "gpt-4-1106-preview", }: tokens_per_message = 3 tokens_per_name = 1 elif model == "gpt-3.5-turbo-0301": tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n tokens_per_name = -1 # if there's a name, the role is omitted - elif "gpt-3.5-turbo" in model: + elif "gpt-3.5-turbo" == model: print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.") return count_message_tokens(messages, model="gpt-3.5-turbo-0613") - elif "gpt-4" in model: + elif "gpt-4" == model: print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.") return count_message_tokens(messages, model="gpt-4-0613") else: diff --git a/setup.py b/setup.py index 239156ae3..e7462767f 100644 --- a/setup.py +++ b/setup.py @@ -31,14 +31,14 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", version="0.3.0", - description="The Multi-Role Meta Programming Framework", + description="The Multi-Agent Framework", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/geekan/MetaGPT", author="Alexander Wu", author_email="alexanderwu@fuzhi.ai", - license="Apache 2.0", - keywords="metagpt multi-role multi-agent programming gpt llm", + license="MIT", + keywords="metagpt multi-role multi-agent programming gpt llm metaprogramming", packages=find_packages(exclude=["contrib", "docs", "examples", "tests*"]), python_requires=">=3.9", install_requires=requirements, @@ -52,4 +52,9 @@ setup( cmdclass={ "install_mermaid": InstallMermaidCLI, }, + entry_points={ + 'console_scripts': [ + 'metagpt=metagpt.startup:app', + ], + }, ) diff --git a/startup.py b/startup.py deleted file mode 100644 index e9fbf94d3..000000000 --- a/startup.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import asyncio - -import fire - -from metagpt.roles import ( - Architect, - Engineer, - ProductManager, - ProjectManager, - QaEngineer, -) -from metagpt.team import Team - - -async def startup( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = False, - run_tests: bool = False, - implement: bool = True, -): - """Run a startup. Be a boss.""" - company = Team() - company.hire( - [ - ProductManager(), - Architect(), - ProjectManager(), - ] - ) - - # if implement or code_review - if implement or code_review: - # developing features: implement the idea - company.hire([Engineer(n_borg=5, use_code_review=code_review)]) - - if run_tests: - # developing features: run tests on the spot and identify bugs - # (bug fixing capability comes soon!) - company.hire([QaEngineer()]) - - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) - - -def main( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = True, - run_tests: bool = False, - implement: bool = True, -): - """ - We are a software startup comprised of AI. By investing in us, - you are empowering a future filled with limitless possibilities. - :param idea: Your innovative idea, such as "Creating a snake game." - :param investment: As an investor, you have the opportunity to contribute - a certain dollar amount to this AI company. - :param n_round: - :param code_review: Whether to use code review. - :return: - """ - asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement)) - - -if __name__ == "__main__": - fire.Fire(main) diff --git a/tests/metagpt/actions/mock.py b/tests/metagpt/actions/mock.py index a800690e8..5be1d8001 100644 --- a/tests/metagpt/actions/mock.py +++ b/tests/metagpt/actions/mock.py @@ -100,7 +100,7 @@ For testing, we can use the PyTest framework. This is a mature full-featured Pyt file_list = ["main.py", "room.py", "player.py", "game.py", "object.py", "puzzle.py", "test_game.py"] ``` -## Data structures and interface definitions: +## Data structures and interfaces: ```mermaid classDiagram class Room{ @@ -209,7 +209,7 @@ Shared knowledge for this project includes understanding the basic principles of """ ``` -## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. ```python """ The original requirements did not specify whether the game should have a save/load feature, multiplayer support, or any specific graphical user interface. More information on these aspects could help in further refining the product design and requirements. diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 38e4e5221..18675ecc3 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -7,7 +7,7 @@ """ import pytest -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.logs import logger from metagpt.roles.product_manager import ProductManager from metagpt.schema import Message @@ -17,7 +17,7 @@ from metagpt.schema import Message async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" - prd = await product_manager.handle(Message(content=requirements, cause_by=BossRequirement)) + prd = await product_manager.handle(Message(content=requirements, cause_by=UserRequirement)) logger.info(requirements) logger.info(prd) diff --git a/tests/metagpt/document_store/test_document.py b/tests/metagpt/document_store/test_document.py index 5ae357fb1..13c0921a3 100644 --- a/tests/metagpt/document_store/test_document.py +++ b/tests/metagpt/document_store/test_document.py @@ -7,22 +7,22 @@ """ import pytest -from metagpt.const import DATA_PATH -from metagpt.document_store.document import Document +from metagpt.const import METAGPT_ROOT +from metagpt.document import IndexableDocument CASES = [ - ("st/faq.xlsx", "Question", "Answer", 1), - ("cases/faq.csv", "Question", "Answer", 1), + ("requirements.txt", None, None, 0), + # ("cases/faq.csv", "Question", "Answer", 1), # ("cases/faq.json", "Question", "Answer", 1), - ("docx/faq.docx", None, None, 1), - ("cases/faq.pdf", None, None, 0), # 这是因为pdf默认没有分割段落 - ("cases/faq.txt", None, None, 0), # 这是因为txt按照256分割段落 + # ("docx/faq.docx", None, None, 1), + # ("cases/faq.pdf", None, None, 0), # 这是因为pdf默认没有分割段落 + # ("cases/faq.txt", None, None, 0), # 这是因为txt按照256分割段落 ] @pytest.mark.parametrize("relative_path, content_col, meta_col, threshold", CASES) def test_document(relative_path, content_col, meta_col, threshold): - doc = Document(DATA_PATH / relative_path, content_col, meta_col) + doc = IndexableDocument.from_path(METAGPT_ROOT / relative_path, content_col, meta_col) rsp = doc.get_docs_and_metadatas() assert len(rsp[0]) > threshold assert len(rsp[1]) > threshold diff --git a/tests/metagpt/memory/test_longterm_memory.py b/tests/metagpt/memory/test_longterm_memory.py index dc5540520..ac9362937 100644 --- a/tests/metagpt/memory/test_longterm_memory.py +++ b/tests/metagpt/memory/test_longterm_memory.py @@ -4,7 +4,7 @@ from metagpt.config import CONFIG from metagpt.schema import Message -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.roles.role import RoleContext from metagpt.memory import LongTermMemory @@ -15,24 +15,24 @@ def test_ltm_search(): assert len(openai_api_key) > 20 role_id = 'UTUserLtm(Product Manager)' - rc = RoleContext(watch=[BossRequirement]) + rc = RoleContext(watch=[UserRequirement]) ltm = LongTermMemory() ltm.recover_memory(role_id, rc) idea = 'Write a cli snake game' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + message = Message(role='User', content=idea, cause_by=UserRequirement) news = ltm.find_news([message]) assert len(news) == 1 ltm.add(message) sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_message = Message(role='User', content=sim_idea, cause_by=UserRequirement) news = ltm.find_news([sim_message]) assert len(news) == 0 ltm.add(sim_message) new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_message = Message(role='User', content=new_idea, cause_by=UserRequirement) news = ltm.find_news([new_message]) assert len(news) == 1 ltm.add(new_message) @@ -48,7 +48,7 @@ def test_ltm_search(): assert len(news) == 0 new_idea = 'Write a Battle City' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_message = Message(role='User', content=new_idea, cause_by=UserRequirement) news = ltm_new.find_news([new_message]) assert len(news) == 1 diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index 6bb3e8f1d..bd4441641 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -6,7 +6,7 @@ from typing import List from metagpt.memory.memory_storage import MemoryStorage from metagpt.schema import Message -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.actions import WritePRD from metagpt.actions.action_output import ActionOutput @@ -14,7 +14,7 @@ from metagpt.actions.action_output import ActionOutput def test_idea_message(): idea = 'Write a cli snake game' role_id = 'UTUser1(Product Manager)' - message = Message(role='BOSS', content=idea, cause_by=BossRequirement) + message = Message(role='User', content=idea, cause_by=UserRequirement) memory_storage: MemoryStorage = MemoryStorage() messages = memory_storage.recover_memory(role_id) @@ -24,12 +24,12 @@ def test_idea_message(): assert memory_storage.is_initialized is True sim_idea = 'Write a game of cli snake' - sim_message = Message(role='BOSS', content=sim_idea, cause_by=BossRequirement) + sim_message = Message(role='User', content=sim_idea, cause_by=UserRequirement) new_messages = memory_storage.search(sim_message) assert len(new_messages) == 0 # similar, return [] new_idea = 'Write a 2048 web game' - new_message = Message(role='BOSS', content=new_idea, cause_by=BossRequirement) + new_message = Message(role='User', content=new_idea, cause_by=UserRequirement) new_messages = memory_storage.search(new_message) assert new_messages[0].content == message.content @@ -49,7 +49,7 @@ def test_actionout_message(): ic_obj = ActionOutput.create_model_class('prd', out_mapping) role_id = 'UTUser2(Architect)' - content = 'The boss has requested the creation of a command-line interface (CLI) snake game' + content = 'The user has requested the creation of a command-line interface (CLI) snake game' message = Message(content=content, instruct_content=ic_obj(**out_data), role='user', diff --git a/tests/metagpt/planner/test_action_planner.py b/tests/metagpt/planner/test_action_planner.py index 5ab9a493f..8efe6cfc4 100644 --- a/tests/metagpt/planner/test_action_planner.py +++ b/tests/metagpt/planner/test_action_planner.py @@ -9,7 +9,7 @@ import pytest from semantic_kernel.core_skills import FileIOSkill, MathSkill, TextSkill, TimeSkill from semantic_kernel.planning.action_planner.action_planner import ActionPlanner -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.roles.sk_agent import SkAgent from metagpt.schema import Message @@ -23,7 +23,7 @@ async def test_action_planner(): role.import_skill(TimeSkill(), "time") role.import_skill(TextSkill(), "text") task = "What is the sum of 110 and 990?" - role.recv(Message(content=task, cause_by=BossRequirement)) + role.recv(Message(content=task, cause_by=UserRequirement)) await role._think() # it will choose mathskill.Add assert "1100" == (await role._act()).content diff --git a/tests/metagpt/planner/test_basic_planner.py b/tests/metagpt/planner/test_basic_planner.py index 03a82ec5e..f6d44ba03 100644 --- a/tests/metagpt/planner/test_basic_planner.py +++ b/tests/metagpt/planner/test_basic_planner.py @@ -8,7 +8,7 @@ import pytest from semantic_kernel.core_skills import TextSkill -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.const import SKILL_DIRECTORY from metagpt.roles.sk_agent import SkAgent from metagpt.schema import Message @@ -26,7 +26,7 @@ async def test_basic_planner(): role.import_semantic_skill_from_directory(SKILL_DIRECTORY, "WriterSkill") role.import_skill(TextSkill(), "TextSkill") # using BasicPlanner - role.recv(Message(content=task, cause_by=BossRequirement)) + role.recv(Message(content=task, cause_by=UserRequirement)) await role._think() # assuming sk_agent will think he needs WriterSkill.Brainstorm and WriterSkill.Translate assert "WriterSkill.Brainstorm" in role.plan.generated_plan.result diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 52fc4a3c1..fbad06acb 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -5,10 +5,10 @@ @Author : alexanderwu @File : mock.py """ -from metagpt.actions import BossRequirement, WriteDesign, WritePRD, WriteTasks +from metagpt.actions import UserRequirement, WriteDesign, WritePRD, WriteTasks from metagpt.schema import Message -BOSS_REQUIREMENT = """开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结""" +USER_REQUIREMENT = """开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结""" DETAIL_REQUIREMENT = """需求:开发一个基于LLM(大语言模型)与私有知识库的搜索引擎,希望有几点能力 1. 用户可以在私有知识库进行搜索,再根据大语言模型进行总结,输出的结果包括了总结 @@ -94,7 +94,7 @@ SYSTEM_DESIGN = '''## Python package name ] ``` -## Data structures and interface definitions +## Data structures and interfaces ```mermaid classDiagram class Main { @@ -252,7 +252,7 @@ a = 'a' class MockMessages: - req = Message(role="Boss", content=BOSS_REQUIREMENT, cause_by=BossRequirement) + req = Message(role="User", content=USER_REQUIREMENT, cause_by=UserRequirement) prd = Message(role="Product Manager", content=PRD, cause_by=WritePRD) system_design = Message(role="Architect", content=SYSTEM_DESIGN, cause_by=WriteDesign) tasks = Message(role="Project Manager", content=TASKS, cause_by=WriteTasks) diff --git a/tests/metagpt/roles/ui_role.py b/tests/metagpt/roles/ui_role.py index a45a89cde..102c6ebd6 100644 --- a/tests/metagpt/roles/ui_role.py +++ b/tests/metagpt/roles/ui_role.py @@ -8,7 +8,8 @@ from functools import wraps from importlib import import_module from metagpt.actions import Action, ActionOutput, WritePRD -from metagpt.const import WORKSPACE_ROOT +# from metagpt.const import WORKSPACE_ROOT +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message @@ -29,7 +30,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Selected Elements:Provide as Plain text, up to 5 specified elements, clear and simple ## HTML Layout:Provide as Plain text, use standard HTML code ## CSS Styles (styles.css):Provide as Plain text,use standard css code -## Anything UNCLEAR:Provide as Plain text. Make clear here. +## Anything UNCLEAR:Provide as Plain text. Try to clarify it. """ @@ -214,7 +215,7 @@ class UIDesign(Action): logger.info("Finish icon design using StableDiffusion API") async def _save(self, css_content, html_content): - save_dir = WORKSPACE_ROOT / "resources" / "codes" + save_dir = CONFIG.workspace_path / "resources" / "codes" if not os.path.exists(save_dir): os.makedirs(save_dir, exist_ok=True) # Save CSS and HTML content to files diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index a0f1f6257..b27bc3da7 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -8,7 +8,7 @@ import pytest -from metagpt.actions import BossRequirement +from metagpt.actions import UserRequirement from metagpt.environment import Environment from metagpt.logs import logger from metagpt.manager import Manager @@ -49,7 +49,7 @@ async def test_publish_and_process_message(env: Environment): env.add_roles([product_manager, architect]) env.set_manager(Manager()) - env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement)) + env.publish_message(Message(role="User", content="需要一个基于LLM做总结的搜索引擎", cause_by=UserRequirement)) await env.run(k=2) logger.info(f"{env.history=}") diff --git a/tests/metagpt/tools/test_sd_tool.py b/tests/metagpt/tools/test_sd_tool.py index 77e53c7dc..fea58bc29 100644 --- a/tests/metagpt/tools/test_sd_tool.py +++ b/tests/metagpt/tools/test_sd_tool.py @@ -4,7 +4,9 @@ # import os -from metagpt.tools.sd_engine import SDEngine, WORKSPACE_ROOT +from metagpt.config import CONFIG +from metagpt.tools.sd_engine import SDEngine + def test_sd_engine_init(): @@ -21,5 +23,5 @@ def test_sd_engine_generate_prompt(): async def test_sd_engine_run_t2i(): sd_engine = SDEngine() await sd_engine.run_t2i(prompts=["test"]) - img_path = WORKSPACE_ROOT / "resources" / "SD_Output" / "output_0.png" + img_path = CONFIG.workspace_path / "resources" / "SD_Output" / "output_0.png" assert os.path.exists(img_path) == True diff --git a/tests/metagpt/utils/test_common.py b/tests/metagpt/utils/test_common.py index ec4443175..b6c000f9b 100644 --- a/tests/metagpt/utils/test_common.py +++ b/tests/metagpt/utils/test_common.py @@ -10,7 +10,7 @@ import os import pytest -from metagpt.const import get_project_root +from metagpt.const import get_metagpt_root class TestGetProjectRoot: @@ -20,11 +20,11 @@ class TestGetProjectRoot: os.chdir(abs_root) def test_get_project_root(self): - project_root = get_project_root() + project_root = get_metagpt_root() assert project_root.name == 'metagpt' def test_get_root_exception(self): with pytest.raises(Exception) as exc_info: self.change_etc_dir() - get_project_root() + get_metagpt_root() assert str(exc_info.value) == "Project root not found." diff --git a/tests/metagpt/utils/test_output_parser.py b/tests/metagpt/utils/test_output_parser.py index 4e362f9f7..99ab1f79e 100644 --- a/tests/metagpt/utils/test_output_parser.py +++ b/tests/metagpt/utils/test_output_parser.py @@ -218,7 +218,7 @@ We need clarification on how the high score should be stored. Should it persist } t_text1 = '''## Original Requirements: -The boss wants to create a web-based version of the game "Fly Bird". +The user wants to create a web-based version of the game "Fly Bird". ## Product Goals: diff --git a/tests/metagpt/utils/test_read_docx.py b/tests/metagpt/utils/test_read_docx.py index a7d0774a8..adf473ae7 100644 --- a/tests/metagpt/utils/test_read_docx.py +++ b/tests/metagpt/utils/test_read_docx.py @@ -6,12 +6,12 @@ @File : test_read_docx.py """ -from metagpt.const import PROJECT_ROOT +from metagpt.const import METAGPT_ROOT from metagpt.utils.read_document import read_docx class TestReadDocx: def test_read_docx(self): - docx_sample = PROJECT_ROOT / "tests/data/docx_for_test.docx" + docx_sample = METAGPT_ROOT / "tests/data/docx_for_test.docx" docx = read_docx(docx_sample) assert len(docx) == 6 From 715a1d874aa1d690aa9f7c5b27d404e6d9c1b19a Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 27 Nov 2023 15:48:07 +0800 Subject: [PATCH 172/413] fix config --- config/config.yaml | 6 +++--- metagpt/actions/SummarizeCode.py | 14 ++++++-------- metagpt/actions/write_code.py | 2 +- requirements.txt | 3 ++- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index bed67083c..9acdbe8a1 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -7,9 +7,9 @@ ## Or, you can configure OPENAI_PROXY to access official OPENAI_API_BASE. OPENAI_API_BASE: "https://api.openai.com/v1" #OPENAI_PROXY: "http://127.0.0.1:8118" -#OPENAI_API_KEY: "YOUR_API_KEY" # set the value to sk-xxx if you host the openai interface for open llm model -OPENAI_API_MODEL: "gpt-4" -MAX_TOKENS: 1500 +#OPENAI_API_KEY: "YOUR_API_KEY" # set the value to sk-xxx if you host the openai interface for open llm model +OPENAI_API_MODEL: "gpt-4-1106-preview" +MAX_TOKENS: 4096 RPM: 10 #### if Spark diff --git a/metagpt/actions/SummarizeCode.py b/metagpt/actions/SummarizeCode.py index 1015d3bfb..49a350b75 100644 --- a/metagpt/actions/SummarizeCode.py +++ b/metagpt/actions/SummarizeCode.py @@ -5,11 +5,10 @@ @File : SummarizeCode.py """ +from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message -from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -23,10 +22,10 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc ## Code Review All: 请你对历史所有文件进行阅读,分析每个文件是否都完整实现了用户需求,找到可能的bug,如函数未实现、调用错误、未引用等 -## Summary: 根据历史文件的实现情况进行总结 - ## Call flow: 根据实现的函数,使用mermaid绘制完整的调用链 +## Summary: 根据历史文件的实现情况进行总结 + ## TODOs: 这里写出需要修改的文件列表,我们会在之后进行修改 """ @@ -80,14 +79,13 @@ class SummarizeCode(Action): super().__init__(name, context, llm) @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) - async def write_code_review_all(self, prompt): + async def summarize_code(self, prompt): code_rsp = await self._aask(prompt) return code_rsp async def run(self, context): format_example = FORMAT_EXAMPLE.format() prompt = PROMPT_TEMPLATE.format(context=context, format_example=format_example) - logger.info(f'Code review all..') - rsp = await self.write_code_review_all(prompt) + logger.info("Code review all..") + rsp = await self.summarize_code(prompt) return rsp - \ No newline at end of file diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 176718dfc..1f6d16b3b 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -5,13 +5,13 @@ @Author : alexanderwu @File : write_code.py """ +from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions import WriteDesign from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE diff --git a/requirements.txt b/requirements.txt index f0169d7fa..f233e398f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,8 @@ channels==4.0.0 # docx==0.2.4 #faiss==1.5.3 faiss_cpu==1.7.4 -fire==0.4.0 +# fire==0.4.0 +typer # godot==0.1.1 # google_api_python_client==2.93.0 lancedb==0.1.16 From 22288a342dcbb029447e6d896148bf22da4e9da3 Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 27 Nov 2023 15:36:50 +0800 Subject: [PATCH 173/413] =?UTF-8?q?1.=20=E5=8A=A8=E4=BD=9C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=20=201.=20SummarizeCode=E5=8A=A8=E4=BD=9C=EF=BC=9A?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=9F=BA=E4=BA=8E=E4=BB=A3=E7=A0=81=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=80=BB=E7=BB=93=EF=BC=8C=E6=80=9D=E8=80=83bug?= =?UTF-8?q?=E3=80=81=E9=80=BB=E8=BE=91=E3=80=81todo=20=20=202.=20CodeRevie?= =?UTF-8?q?w=E5=8A=A8=E4=BD=9C=E4=BC=98=E5=8C=96=EF=BC=9A=E7=9B=AE?= =?UTF-8?q?=E5=89=8D=E5=BC=BA=E5=88=B6=E8=A6=81=E6=B1=82=E5=9B=9E=E7=AD=94?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E6=9C=89=E6=9B=B4=E9=AB=98=E7=9A=84?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E7=8E=87=E4=BA=86=20=20=20=20=201.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86LGTM/LBTM=E7=9A=84=E5=9B=9E=E7=AD=94?= =?UTF-8?q?=EF=BC=8C=E5=9C=A8LGTM=E6=97=B6=E4=BC=9A=E5=8F=8A=E6=97=B6?= =?UTF-8?q?=E5=81=9C=E6=AD=A2=EF=BC=8C=E4=B8=8D=E9=87=8D=E5=86=99=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=20=20=20=20=202.=20=E7=9B=AE=E5=89=8D=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E8=AE=BE=E7=BD=AE=E4=B8=AD=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0code=5Freview=5Fk=5Ftimes=EF=BC=8C=E4=B8=8Ereflexion?= =?UTF-8?q?=E7=B1=BB=E4=BC=BC=EF=BC=8C=E8=AE=BE=E7=BD=AE=E4=B8=BA2=20=20?= =?UTF-8?q?=20=20=203.=20=E4=BB=8D=E7=84=B6=E6=9C=89=E6=A6=82=E7=8E=87?= =?UTF-8?q?=E5=8F=91=E7=94=9F=E6=8C=87=E4=BB=A4=E4=B8=8D=E9=81=B5=E5=BE=AA?= =?UTF-8?q?=EF=BC=8C=E5=B0=A4=E5=85=B6=E6=98=AF=E4=BC=9A=E6=9C=89=E6=AF=94?= =?UTF-8?q?=E8=BE=83=E9=AB=98=E7=9A=84=E6=A6=82=E7=8E=87=E5=8F=91=E7=94=9F?= =?UTF-8?q?=E5=90=8C=E6=97=B6review=E5=A4=9A=E4=B8=AA=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E8=BF=98=E6=B2=A1=E6=83=B3=E5=A5=BD?= =?UTF-8?q?=E6=80=8E=E4=B9=88=E8=A7=A3=E5=86=B3=20#FIXME=20=20=203.=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86env=E5=88=B0Action=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=B0=83=E7=94=A8=E7=8E=AF=E5=A2=83=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BA=86=20=20=204.=20WriteDesign=EF=BC=9A=E5=8E=BB=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E5=AF=B9project=5Fname=E7=9A=84=E7=BA=A0=E6=AD=A3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=BC=95=E5=AF=BC?= =?UTF-8?q?=E4=B8=8B=E5=8F=AF=E4=BB=A5=E4=B8=80=E6=AC=A1=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=AF=B9=20=20=20=20=201.=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E8=AF=8D=E4=B8=AD=E7=9A=84##=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E6=94=B9=E4=B8=BA=E4=BA=86JSON=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=202.=20=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=20=20=201?= =?UTF-8?q?.=20Document=E7=9A=84=E6=A0=87=E5=87=86=E5=8C=96=EF=BC=9AEnv->R?= =?UTF-8?q?epo->Document=EF=BC=8C=E5=85=B6=E4=B8=ADDocument/Asset/Code?= =?UTF-8?q?=E9=83=BD=E6=98=AFDocument=20=20=20=20=201.=20=E5=8E=9F?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A3=80=E7=B4=A2=E7=9A=84Document=E6=94=B9?= =?UTF-8?q?=E4=B8=BAIndexableDocument=20=20=202.=20Repo=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=BC=95=E5=85=A5=EF=BC=9A=E7=94=A8=E4=BA=8EDocument=E8=A3=85?= =?UTF-8?q?=E8=BD=BD=E4=B8=8E=E5=85=83=E6=95=B0=E6=8D=AE=E8=A3=85=E8=BD=BD?= =?UTF-8?q?=20=20=203.=20RepoParser=E5=BC=95=E5=85=A5=EF=BC=9A=E5=86=99?= =?UTF-8?q?=E4=BA=86=E4=B8=80=E4=B8=AA=E7=AE=80=E5=8D=95=E7=9A=84AST=20par?= =?UTF-8?q?ser=EF=BC=88=E5=90=8E=E7=BB=AD=E5=8F=AF=E8=83=BD=E8=A6=81?= =?UTF-8?q?=E6=8D=A2tree-sitter=EF=BC=89=EF=BC=8C=E7=BB=99=E5=87=BA?= =?UTF-8?q?=E4=BA=86=E6=95=B4=E5=BA=93symbol=20=20=204.=20Env=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86set/get/set=5Fdoc/get=5Fdoc=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E7=94=A8=E4=BA=8Eset/get=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E6=88=96=E8=80=85=E4=B8=80=E4=B8=AADocument?= =?UTF-8?q?=E3=80=82=E8=BF=99=E4=B8=AA=E9=80=BB=E8=BE=91=E5=90=8E=E7=BB=AD?= =?UTF-8?q?=E6=88=96=E8=AE=B8=E4=BC=9A=E8=BF=9B=E4=B8=80=E6=AD=A5=E7=AE=80?= =?UTF-8?q?=E5=8C=96=203.=20=E9=85=8D=E7=BD=AE=E4=BC=98=E5=8C=96=20=20=201?= =?UTF-8?q?.=20=E9=BB=98=E8=AE=A4=E6=9B=B4=E6=8D=A2=E4=B8=BAgpt-4-1106-pre?= =?UTF-8?q?view=EF=BC=8C=E4=BB=A5=E8=8E=B7=E5=BE=97=E6=9C=80=E5=A5=BD?= =?UTF-8?q?=E7=9A=84=E6=95=88=E6=9E=9C=E4=B8=8E=E6=88=90=E6=9C=AC=20=20=20?= =?UTF-8?q?2.=20=E6=8F=90=E4=BE=9B~/.metagpt=E4=BD=9C=E4=B8=BA=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=9C=80=E9=AB=98=E4=BC=98=E5=85=88=E7=BA=A7=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E4=BB=8E=E4=B8=AD=E8=AF=BB=E5=8F=96config.ya?= =?UTF-8?q?ml=20=20=203.=20workspace=E5=8F=AF=E4=BB=A5=E7=81=B5=E6=B4=BB?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E4=BA=86=EF=BC=8C=E5=9C=A8config=E4=B8=AD?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20=20=204.=20project=5Fname=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E7=94=B1=E5=91=BD=E4=BB=A4=E8=A1=8C=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=94=B9=E4=B8=BA=E7=94=B1ProductMa?= =?UTF-8?q?nager=E7=94=9F=E6=88=90=204.=20metagpt=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=91=BD=E4=BB=A4=E8=A1=8C=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E9=9D=9Epython=20startup.py=20metagpt=20--help?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit metagpt --project-name game_2048 "make a 2048 game" metagpt "make a 2048 game" metagpt --project-name game_2048 --inc "将2048改为4096" metagpt --project-name game_2048 --auto-inc "make a 2048 game" 1. 使用新的METAGPT_ROOT生成方式,而非寻找git,以便cli安装 2. 命令行由fire换为了typer,它会带来相对更好的体验 3. project_name可以灵活指定了,在metagpt命令行输入中配置 5. 其他 1. 现在支持多国语言了,中文已测试 2. BossRequirement -> UserRequirement 3. 大量错误文本的修正,增加了可读性 4. 中量提示词优化,稍微提升了一些准确率 5. 暂时屏蔽了LongtermMemory相关逻辑,这个逻辑底层调用了langchain的FAISS,会带来~5秒加载耗时 6. 修复了安装包中的部分描述错误 7. 去除了config中在openai_proxy设定时对base的重复修改,这个修改应该在openai初始化时发生 8. 修复了JSON在中文存储时的特定问题,ensure_ascii=False --- examples/debate.py | 2 +- metagpt/actions/action.py | 4 + metagpt/actions/design_api.py | 37 +++--- metagpt/actions/project_management.py | 36 +++--- .../{SummarizeCode.py => summarize_code.py} | 21 ++-- metagpt/actions/write_code.py | 3 +- metagpt/actions/write_code_review.py | 84 +++++++++---- metagpt/actions/write_prd.py | 88 ++++++------- metagpt/actions/write_test.py | 2 +- metagpt/config.py | 6 +- metagpt/document.py | 116 ++++++++++++------ metagpt/environment.py | 30 ++++- metagpt/memory/longterm_memory.py | 2 +- metagpt/provider/openai_api.py | 2 + metagpt/{document_store => }/repo_parser.py | 48 ++++---- metagpt/roles/engineer.py | 26 ++-- metagpt/roles/qa_engineer.py | 4 +- metagpt/roles/role.py | 15 +++ metagpt/startup.py | 7 +- metagpt/team.py | 10 +- tests/metagpt/actions/mock.py | 2 +- tests/metagpt/roles/mock.py | 2 +- tests/metagpt/roles/test_ui.py | 2 +- ...st_software_company.py => test_startup.py} | 13 +- 24 files changed, 359 insertions(+), 203 deletions(-) rename metagpt/actions/{SummarizeCode.py => summarize_code.py} (62%) rename metagpt/{document_store => }/repo_parser.py (67%) rename tests/metagpt/{test_software_company.py => test_startup.py} (51%) diff --git a/examples/debate.py b/examples/debate.py index 0f5d1591b..e62a5aaa1 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -88,7 +88,7 @@ async def debate(idea: str, investment: float = 3.0, n_round: int = 5): team = Team() team.hire([Biden, Trump]) team.invest(investment) - team.start_project(idea, send_to="Biden") # send debate topic to Biden and let him speak first + team.run_project(idea, send_to="Biden") # send debate topic to Biden and let him speak first await team.run(n_round=n_round) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..f8016b8a2 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -30,6 +30,10 @@ class Action(ABC): self.desc = "" self.content = "" self.instruct_content = None + self.env = None + + def set_env(self, env): + self.env = env def set_prefix(self, prefix, profile): """Set prefix for later usage""" diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index f58d49495..9e2bfc12c 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -14,7 +14,6 @@ from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template -from metagpt.utils.json_to_markdown import json_to_markdown from metagpt.utils.mermaid import mermaid_to_file templates = { @@ -27,11 +26,12 @@ templates = { {format_example} ----- Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirement: Fill in the following missing information based on the context, each section name is a key in json ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select appropriate open-source frameworks. -## Python package name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores ## File list: Provided as Python list[str], the list of files needed (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -48,7 +48,7 @@ and only output the json inside this tag, nothing else [CONTENT] { "Implementation approach": "We will ...", - "Python package name": "snake_game", + "project_name": "snake_game", "File list": ["main.py"], "Data structures and interfaces": ' classDiagram @@ -78,12 +78,13 @@ and only output the json inside this tag, nothing else {format_example} ----- Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system; make the best use of good open source tools +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirement: Fill in the following missing information based on the context, note that all sections are response with code form separately -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. +ATTENTION: Output carefully referenced "Format example" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores ## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -99,7 +100,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Implementation approach We will ... -## Python package name +## project_name ```python "snake_game" ``` @@ -138,7 +139,7 @@ The requirement is clear to me. OUTPUT_MAPPING = { "Implementation approach": (str, ...), - "Python package name": (str, ...), + "project_name": (str, ...), "File list": (List[str], ...), "Data structures and interfaces": (str, ...), "Program call flow": (str, ...), @@ -170,7 +171,7 @@ class WriteDesign(Action): if context[-1].instruct_content: logger.info(f"Saving PRD to {prd_file}") - prd_file.write_text(json_to_markdown(context[-1].instruct_content.dict())) + prd_file.write_text(context[-1].instruct_content.json(ensure_ascii=False), encoding='utf-8') async def _save_system_design(self, docs_path, resources_path, system_design): data_api_design = system_design.instruct_content.dict()[ @@ -183,14 +184,14 @@ class WriteDesign(Action): await mermaid_to_file(seq_flow, resources_path / "seq_flow") system_design_file = docs_path / "system_design.md" logger.info(f"Saving System Designs to {system_design_file}") - system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict()))) + system_design_file.write_text(system_design.instruct_content.json(ensure_ascii=False), encoding='utf-8') async def _save(self, context, system_design): if isinstance(system_design, ActionOutput): - ws_name = system_design.instruct_content.dict()["Python package name"] + project_name = system_design.instruct_content.dict()["project_name"] else: - ws_name = CodeParser.parse_str(block="Python package name", text=system_design) - workspace = CONFIG.workspace_path / ws_name + project_name = CodeParser.parse_str(block="project_name", text=system_design) + workspace = CONFIG.workspace_path / project_name self.recreate_workspace(workspace) docs_path = workspace / "docs" resources_path = workspace / "resources" @@ -204,11 +205,11 @@ class WriteDesign(Action): prompt = prompt_template.format(context=context, format_example=format_example) # system_design = await self._aask(prompt) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python package name" contain space, have to use setattr - setattr( - system_design.instruct_content, - "Python package name", - system_design.instruct_content.dict()["Python package name"].strip().strip("'").strip('"'), - ) + # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" contain space, have to use setattr + # setattr( + # system_design.instruct_content, + # "project_name", + # system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), + # ) await self._save(context, system_design) return system_design diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 467cb4d83..805226a25 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -11,7 +11,6 @@ from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.utils.common import CodeParser from metagpt.utils.get_template import get_template -from metagpt.utils.json_to_markdown import json_to_markdown templates = { "json": { @@ -23,19 +22,20 @@ templates = { {format_example} ----- Role: You are a project manager; the goal is to break down tasks according to PRD/technical design, give a task list, and analyze task dependencies to start with the prerequisite modules +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. +ATTENTION: Output carefully referenced "Format example" in format. ## Required Python third-party packages: Provide Python list[str] in requirements.txt format ## Required Other language third-party packages: Provide Python list[str] in requirements.txt format -## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. - ## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first ## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first +## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. + ## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. ## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. @@ -52,17 +52,17 @@ and only output the json inside this tag, nothing else "Required Other language third-party packages": [ "No third-party ..." ], + "Logic Analysis": [ + ["game.py", "Contains..."] + ], + "Task list": [ + "game.py" + ], "Full API spec": """ openapi: 3.0.0 ... description: A JSON object ... """, - "Logic Analysis": [ - ["game.py","Contains..."] - ], - "Task list": [ - "game.py" - ], "Shared Knowledge": """ 'game.py' contains ... """, @@ -86,12 +86,12 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Required Other language third-party packages: Provided in requirements.txt format -## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. - ## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first ## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first +## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. + ## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. ## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. @@ -126,14 +126,16 @@ description: A JSON object ... ## Logic Analysis ```python [ - ["game.py", "Contains ..."], + ["index.js", "Contains ..."], + ["main.py", "Contains ..."], ] ``` ## Task list ```python [ - "game.py", + "index.js", + "main.py", ] ``` @@ -167,11 +169,11 @@ class WriteTasks(Action): def _save(self, context, rsp): if context[-1].instruct_content: - ws_name = context[-1].instruct_content.dict()["Python package name"] + ws_name = context[-1].instruct_content.dict()["project_name"] else: - ws_name = CodeParser.parse_str(block="Python package name", text=context[-1].content) + ws_name = CodeParser.parse_str(block="project_name", text=context[-1].content) file_path = CONFIG.workspace_path / ws_name / "docs/api_spec_and_tasks.md" - file_path.write_text(json_to_markdown(rsp.instruct_content.dict())) + file_path.write_text(rsp.instruct_content.json(ensure_ascii=False)) # Write requirements.txt requirements_path = CONFIG.workspace_path / ws_name / "requirements.txt" diff --git a/metagpt/actions/SummarizeCode.py b/metagpt/actions/summarize_code.py similarity index 62% rename from metagpt/actions/SummarizeCode.py rename to metagpt/actions/summarize_code.py index 49a350b75..a85d3cdeb 100644 --- a/metagpt/actions/SummarizeCode.py +++ b/metagpt/actions/summarize_code.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- """ @Author : alexanderwu -@File : SummarizeCode.py +@File : summarize_code.py """ from tenacity import retry, stop_after_attempt, wait_fixed @@ -13,6 +13,7 @@ from metagpt.schema import Message PROMPT_TEMPLATE = """ NOTICE Role: You are a professional software engineer, and your main task is to review the code. +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". ----- @@ -20,13 +21,13 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc {context} ----- -## Code Review All: 请你对历史所有文件进行阅读,分析每个文件是否都完整实现了用户需求,找到可能的bug,如函数未实现、调用错误、未引用等 +## Code Review All: 请你对历史所有文件进行阅读,在文件中找到可能的bug,如函数未实现、调用错误、未引用等 -## Call flow: 根据实现的函数,使用mermaid绘制完整的调用链 +## Call flow: mermaid代码,根据实现的函数,使用mermaid绘制完整的调用链 ## Summary: 根据历史文件的实现情况进行总结 -## TODOs: 这里写出需要修改的文件列表,我们会在之后进行修改 +## TODOs: Python dict[str, str],这里写出需要修改的文件列表与理由,我们会在之后进行修改 """ @@ -67,15 +68,15 @@ flowchart TB - ... ## TODOs -1. ... -2. ... -3. ... +{ + "a.py": "implement requirement xxx...", +} """ class SummarizeCode(Action): - def __init__(self, name="SummaryCode", context: list[Message] = None, llm=None): + def __init__(self, name="SummarizeCode", context: list[Message] = None, llm=None): super().__init__(name, context, llm) @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) @@ -84,8 +85,8 @@ class SummarizeCode(Action): return code_rsp async def run(self, context): - format_example = FORMAT_EXAMPLE.format() + format_example = FORMAT_EXAMPLE prompt = PROMPT_TEMPLATE.format(context=context, format_example=format_example) - logger.info("Code review all..") + logger.info("Summarize code..") rsp = await self.summarize_code(prompt) return rsp diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 1f6d16b3b..2631ec138 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -16,6 +16,7 @@ from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ NOTICE Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language) +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". ----- @@ -60,7 +61,7 @@ class WriteCode(Action): design = [i for i in context if i.cause_by == WriteDesign][0] - ws_name = CodeParser.parse_str(block="Python package name", text=design.content) + ws_name = CodeParser.parse_str(block="project_name", text=design.content) ws_path = CONFIG.workspace_path / ws_name if f"{ws_name}/" not in filename and all(i not in filename for i in ["requirements.txt", ".md"]): ws_path = ws_path / ws_name diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index c6538bf7b..aebe3f4fa 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -6,56 +6,84 @@ @File : write_code_review.py """ +from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed +from metagpt.config import CONFIG PROMPT_TEMPLATE = """ NOTICE Role: You are a professional software engineer, and your main task is to review the code. You need to ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". -## Code Review: Based on the following context and code, follow the check list, Provide key, clear, concise, and specific code modification suggestions, up to 5. -1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step. -2. Are there any issues with the code logic? If so, how to solve it? -3. Does the existing code follow the "Data structures and interfaces"? -4. Is there a function in the code that is not fully implemented? If so, how to implement it? -5. Does the code have unnecessary or lack dependencies? If so, how to solve it? - -## Rewrite Code: rewrite {filename} based on "Code Review" with triple quotes. Do your utmost to optimize THIS SINGLE FILE. Implement ALL TODO. ------ # Context {context} -## Code: {filename} +## Code to be Reviewed: {filename} ``` {code} ``` + ----- +## Code Review: Based on the "Code to be Reviewed", provide key, clear, concise, and specific code modification suggestions, up to 5. +1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step. +2. Is the code logic completely correct? If there are errors, please indicate how to correct them. +3. Does the existing code follow the "Data structures and interfaces"? +4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step. +5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported +6. Is the code implemented concisely enough? Are methods from other files being reused correctly? + +## Code Review Result: If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM. +LGTM/LBTM + +## Rewrite Code: if it still has some bugs, rewrite {filename} based on "Code Review" with triple quotes, try to get LGTM. Do your utmost to optimize THIS SINGLE FILE. Implement ALL TODO. RETURN ALL CODE, NEVER OMIT ANYTHING. 以任何方式省略代码都是不允许的。 +``` +``` + ## Format example ------ {format_example} ------ """ FORMAT_EXAMPLE = """ - -## Code Review +----- +# EXAMPLE 1 +## Code Review: {filename} 1. No, we should add the logic of ... 2. ... 3. ... 4. ... 5. ... +6. ... + +## Code Review Result: {filename} +LBTM ## Rewrite Code: {filename} ```python ## {filename} ... ``` +----- +# EXAMPLE 2 +## Code Review: {filename} +1. Yes. +2. Yes. +3. Yes. +4. Yes. +5. Yes. +6. Yes. + +## Code Review Result: {filename} +LGTM + +## Rewrite Code: {filename} +pass +----- """ @@ -64,17 +92,27 @@ class WriteCodeReview(Action): super().__init__(name, context, llm) @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) - async def write_code(self, prompt): + async def write_code_review_and_rewrite(self, prompt): code_rsp = await self._aask(prompt) + result = CodeParser.parse_block("Code Review Result", code_rsp) + if "LGTM" in result: + return result, None code = CodeParser.parse_code(block="", text=code_rsp) - return code + return result, code async def run(self, context, code, filename): - format_example = FORMAT_EXAMPLE.format(filename=filename) - prompt = PROMPT_TEMPLATE.format(context=context, code=code, filename=filename, format_example=format_example) - logger.info(f'Code review {filename}..') - code = await self.write_code(prompt) + iterative_code = code + k = CONFIG.code_review_k_times + for i in range(k): + format_example = FORMAT_EXAMPLE.format(filename=filename) + prompt = PROMPT_TEMPLATE.format(context=context, code=iterative_code, filename=filename, format_example=format_example) + logger.info(f'Code review and rewrite {filename}: {i+1}/{k} | {len(iterative_code)=}, {len(code)=}') + result, rewrited_code = await self.write_code_review_and_rewrite(prompt) + if "LBTM" in result: + iterative_code = rewrited_code + elif "LGTM" in result: + return iterative_code # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) - return code - \ No newline at end of file + # 如果rewrited_code是None(原code perfect),那么直接返回code + return iterative_code diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 584d31998..4780762ca 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -17,54 +17,50 @@ templates = { "json": { "PROMPT_TEMPLATE": """ # Context -## Original Requirements -{requirements} - -## Search Information -{search_information} - -## mermaid quadrantChart code syntax example. DONT USE QUOTO IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME -```mermaid -quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - "Campaign: A": [0.3, 0.6] - "Campaign B": [0.45, 0.23] - "Campaign C": [0.57, 0.69] - "Campaign D": [0.78, 0.34] - "Campaign E": [0.40, 0.34] - "Campaign F": [0.35, 0.78] - "Our Target Product": [0.5, 0.6] -``` +{{ + "Original Requirements": "{requirements}", + "Search Information": "" +}} ## Format example {format_example} ----- Role: You are a professional product manager; the goal is to design a concise, usable, efficient product -Requirements: According to the context, fill in the following missing information, each section name is a key in json +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. +Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. +ATTENTION: Output carefully referenced "Format example" in format. -## Original Requirements: Provide as Plain text, place the polished complete original requirements here +## YOU NEED TO FULFILL THE BELOW JSON DOC -## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. - -## User Stories: Provided as Python list[str], up to 5 scenario-based user stories - -## Competitive Analysis: Provided as Python list[str], up to 8 competitive product analyses - -## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. - -## Requirement Analysis: Provide as Plain text. - -## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards - -## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. +{{ + "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. + "Original Requirements": "", # str, place the polished complete original requirements here + "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Search Information": "", + "Requirements": "", + "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. + "User Stories": [], # Provided as Python list[str], up to 5 scenario-based user stories + "Competitive Analysis": [], # Provided as Python list[str], up to 8 competitive product analyses + # Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. + "Competitive Quadrant Chart": "quadrantChart + title Reach and engagement of campaigns + x-axis Low Reach --> High Reach + y-axis Low Engagement --> High Engagement + quadrant-1 We should expand + quadrant-2 Need to promote + quadrant-3 Re-evaluate + quadrant-4 May be improved + Campaign A: [0.3, 0.6] + Campaign B: [0.45, 0.23] + Campaign C: [0.57, 0.69] + Campaign D: [0.78, 0.34] + Campaign E: [0.40, 0.34] + Campaign F: [0.35, 0.78]", + "Requirement Analysis": "", # Provide as Plain text. + "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], # Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards + "UI Design draft": "", # Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. + "Anything UNCLEAR": "", # Provide as Plain text. Try to clarify it. +}} output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else @@ -72,6 +68,7 @@ and only output the json inside this tag, nothing else "FORMAT_EXAMPLE": """ [CONTENT] { + "Language": "", "Original Requirements": "", "Search Information": "", "Requirements": "", @@ -132,9 +129,12 @@ quadrantChart {format_example} ----- Role: You are a professional product manager; the goal is to design a concise, usable, efficient product +Language: Please use the same language as the user requirement to answer, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. AND '## ' SHOULD WRITE BEFORE the code and triple quote. Output carefully referenced "Format example" in format. +## Language: Provide as Plain text, use the same language as the user requirement. + ## Original Requirements: Provide as Plain text, place the polished complete original requirements here ## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. @@ -207,6 +207,7 @@ There are no unclear points. } OUTPUT_MAPPING = { + "Language": (str, ...), "Original Requirements": (str, ...), "Product Goals": (List[str], ...), "User Stories": (List[str], ...), @@ -232,11 +233,14 @@ class WritePRD(Action): logger.info(sas.result) logger.info(rsp) + # logger.info(format) prompt_template, format_example = get_template(templates, format) + # logger.info(prompt_template) + # logger.info(format_example) prompt = prompt_template.format( requirements=requirements, search_information=info, format_example=format_example ) - logger.debug(prompt) + # logger.info(prompt) # prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) return prd diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 2f4988c09..9988fda16 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -3,7 +3,7 @@ """ @Time : 2023/5/11 22:12 @Author : alexanderwu -@File : environment.py +@File : write_test.py """ from metagpt.actions.action import Action from metagpt.logs import logger diff --git a/metagpt/config.py b/metagpt/config.py index 1a9cdb4d2..d30a337e3 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -54,10 +54,7 @@ class Config(metaclass=Singleton): (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key): raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first") self.openai_api_base = self._get("OPENAI_API_BASE") - openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy - if openai_proxy: - openai.proxy = openai_proxy - openai.api_base = self.openai_api_base + self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy self.openai_api_type = self._get("OPENAI_API_TYPE") self.openai_api_version = self._get("OPENAI_API_VERSION") self.openai_api_rpm = self._get("RPM", 3) @@ -87,6 +84,7 @@ class Config(metaclass=Singleton): logger.warning("LONG_TERM_MEMORY is True") self.max_budget = self._get("MAX_BUDGET", 10.0) self.total_cost = 0.0 + self.code_review_k_times = 2 self.puppeteer_config = self._get("PUPPETEER_CONFIG", "") self.mmdc = self._get("MMDC", "mmdc") diff --git a/metagpt/document.py b/metagpt/document.py index 044210218..cf0821421 100644 --- a/metagpt/document.py +++ b/metagpt/document.py @@ -5,7 +5,7 @@ @Author : alexanderwu @File : document.py """ - +from enum import Enum from typing import Union, Optional from pathlib import Path from pydantic import BaseModel, Field @@ -18,7 +18,9 @@ from langchain.document_loaders import ( from langchain.text_splitter import CharacterTextSplitter from tqdm import tqdm +from metagpt.config import CONFIG from metagpt.logs import logger +from metagpt.repo_parser import RepoParser def validate_cols(content_col: str, df: pd.DataFrame): @@ -48,42 +50,56 @@ def read_data(data_path: Path): return data +class DocumentStatus(Enum): + """Indicates document status, a mechanism similar to RFC/PEP""" + DRAFT = "draft" + UNDERREVIEW = "underreview" + APPROVED = "approved" + DONE = "done" + + class Document(BaseModel): """ Document: Handles operations related to document files. """ - content: str = Field(default='') - file_path: Path = Field(default=None) + path: Path = Field(default=None) + name: str = Field(default="") + content: str = Field(default="") + + # metadata? in content perhaps. + author: str = Field(default="") + status: DocumentStatus = Field(default=DocumentStatus.DRAFT) + reviews: list = Field(default_factory=list) @classmethod - def from_path(cls, file_path: Path): + def from_path(cls, path: Path): """ Create a Document instance from a file path. """ - if not file_path.exists(): - raise FileNotFoundError(f"File {file_path} not found.") - content = file_path.read_text() - return cls(content=content, file_path=file_path) + if not path.exists(): + raise FileNotFoundError(f"File {path} not found.") + content = path.read_text() + return cls(content=content, path=path) @classmethod - def from_text(cls, text: str, file_path: Optional[Path] = None): + def from_text(cls, text: str, path: Optional[Path] = None): """ Create a Document from a text string. """ - return cls(content=text, file_path=file_path) + return cls(content=text, path=path) - def to_path(self, file_path: Optional[Path] = None): + def to_path(self, path: Optional[Path] = None): """ Save content to the specified file path. """ - if file_path is not None: - self.file_path = file_path + if path is not None: + self.path = path - if self.file_path is None: + if self.path is None: raise ValueError("File path is not set.") - self.file_path.parent.mkdir(parents=True, exist_ok=True) - self.file_path.write_text(self.content) + self.path.parent.mkdir(parents=True, exist_ok=True) + self.path.write_text(self.content, encoding="utf-8") def persist(self): """ @@ -140,25 +156,35 @@ class IndexableDocument(Document): raise NotImplementedError("Data type not supported for metadata extraction.") +class RepoMetadata(BaseModel): + + name: str = Field(default="") + n_docs: int = Field(default=0) + n_chars: int = Field(default=0) + symbols: list = Field(default_factory=list) + + class Repo(BaseModel): # Name of this repo. name: str = Field(default="") + # metadata: RepoMetadata = Field(default=RepoMetadata) docs: dict[Path, Document] = Field(default_factory=dict) codes: dict[Path, Document] = Field(default_factory=dict) assets: dict[Path, Document] = Field(default_factory=dict) - repo_path: Path = Field(default_factory=Path) + path: Path = Field(default=None) def _path(self, filename): - return self.repo_path / filename + return self.path / filename @classmethod - def from_path(cls, repo_path: Path): + def from_path(cls, path: Path): """Load documents, code, and assets from a repository path.""" - repo_path.mkdir(parents=True, exist_ok=True) - repo = Repo(repo_path = repo_path) - for file_path in repo_path.rglob('*'): - if file_path.is_file(): + path.mkdir(parents=True, exist_ok=True) + repo = Repo(path=path, name=path.name) + for file_path in path.rglob('*'): + # FIXME: These judgments are difficult to support multiple programming languages and need to be more general + if file_path.is_file() and file_path.suffix in [".json", ".txt", ".md", ".py", ".js", ".css", ".html"]: repo._set(file_path.read_text(), file_path) return repo @@ -171,23 +197,24 @@ class Repo(BaseModel): for asset in self.assets.values(): asset.to_path() - def _set(self, content: str, file_path: Path): + def _set(self, content: str, path: Path): """Add a document to the appropriate category based on its file extension.""" - file_ext = file_path.suffix + suffix = path.suffix + doc = Document(content=content, path=path, name=str(path.relative_to(self.path))) - doc = Document(content=content, file_path=file_path) - if file_ext.lower() == '.md': - self.docs[file_path] = doc - elif file_ext.lower() in ['.py', '.js', '.css', '.html']: - self.codes[file_path] = doc + # FIXME: These judgments are difficult to support multiple programming languages and need to be more general + if suffix.lower() == '.md': + self.docs[path] = doc + elif suffix.lower() in ['.py', '.js', '.css', '.html']: + self.codes[path] = doc else: - self.assets[file_path] = doc + self.assets[path] = doc return doc def set(self, content: str, filename: str): """Set a document and persist it to disk.""" - file_path = self._path(filename) - doc = self._set(content, file_path) + path = self._path(filename) + doc = self._set(content, path) doc.to_path() def get(self, filename: str) -> Optional[Document]: @@ -195,13 +222,32 @@ class Repo(BaseModel): path = self._path(filename) return self.docs.get(path) or self.codes.get(path) or self.assets.get(path) + def get_text_documents(self) -> list[Document]: + return list(self.docs.values()) + list(self.codes.values()) -def main(): - repo1 = Repo.from_path(Path("/Users/alexanderwu/workspace/t1")) + def eda(self) -> RepoMetadata: + n_docs = sum(len(i) for i in [self.docs, self.codes, self.assets]) + n_chars = sum(sum(len(j.content) for j in i.values()) for i in [self.docs, self.codes, self.assets]) + symbols = RepoParser(base_directory=self.path).generate_symbols() + return RepoMetadata(name=self.name, n_docs=n_docs, n_chars=n_chars, symbols=symbols) + + +def set_existing_repo(path=CONFIG.workspace_path / "t1"): + repo1 = Repo.from_path(path) repo1.set("wtf content", "doc/wtf_file.md") repo1.set("wtf code", "code/wtf_file.py") logger.info(repo1) # check doc +def load_existing_repo(path=CONFIG.workspace_path / "web_tetris"): + repo = Repo.from_path(path) + logger.info(repo) + logger.info(repo.eda()) + + +def main(): + load_existing_repo() + + if __name__ == '__main__': main() diff --git a/metagpt/environment.py b/metagpt/environment.py index 38077c90d..44c9b1c67 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -7,10 +7,12 @@ """ import asyncio from typing import Iterable +from pathlib import Path from pydantic import BaseModel, Field # from metagpt.document import Document +from metagpt.logs import logger from metagpt.document import Repo from metagpt.memory import Memory from metagpt.roles import Role @@ -26,6 +28,7 @@ class Environment(BaseModel): memory: Memory = Field(default_factory=Memory) history: str = Field(default='') repo: Repo = Field(default_factory=Repo) + kv: dict = Field(default_factory=dict) class Config: arbitrary_types_allowed = True @@ -52,9 +55,32 @@ class Environment(BaseModel): self.memory.add(message) self.history += f"\n{message}" - def publish_doc(self, content: str, filename: str): + def set_doc(self, content: str, filename: str): """向当前环境发布文档(包括代码)""" - self.repo.set(content, filename) + return self.repo.set(content, filename) + + def get_doc(self, filename: str): + return self.repo.get(filename) + + def set(self, k: str, v: str): + self.kv[k] = v + + def get(self, k: str): + return self.kv.get(k, None) + + def load_existing_repo(self, path: Path, inc: bool): + self.repo = Repo.from_path(path) + logger.info(self.repo.eda()) + + # Incremental mode: publish all docs to messages. Then roles can read the docs. + if inc: + docs = self.repo.get_text_documents() + for doc in docs: + msg = Message(content=doc.content) + self.publish_message(msg) + logger.info(f"Message from existing doc {doc.path}: {msg}") + logger.info(f"Load {len(docs)} docs from existing repo.") + raise NotImplementedError async def run(self, k=1): """处理一次所有信息的运行 diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index f8abea5f3..b21f80b7d 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -28,7 +28,7 @@ class LongTermMemory(Memory): logger.warning(f"It may the first time to run Agent {role_id}, the long-term memory is empty") else: logger.warning( - f"Agent {role_id} has existed memory storage with {len(messages)} messages " f"and has recovered them." + f"Agent {role_id} has existing memory storage with {len(messages)} messages " f"and has recovered them." ) self.msg_from_recover = True self.add_batch(messages) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 34e5693f8..8ac0c4b21 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -157,6 +157,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): if config.openai_api_type: openai.api_type = config.openai_api_type openai.api_version = config.openai_api_version + if config.openai_proxy: + openai.proxy = config.openai_proxy self.rpm = int(config.get("RPM", 10)) async def _achat_completion_stream(self, messages: list[dict]) -> str: diff --git a/metagpt/document_store/repo_parser.py b/metagpt/repo_parser.py similarity index 67% rename from metagpt/document_store/repo_parser.py rename to metagpt/repo_parser.py index f7e2b0f4a..0020d47aa 100644 --- a/metagpt/document_store/repo_parser.py +++ b/metagpt/repo_parser.py @@ -6,15 +6,19 @@ @File : repo_parser.py """ import json -import pathlib +from pathlib import Path + import ast - import pandas as pd +from pydantic import BaseModel, Field +from pprint import pformat + +from metagpt.config import CONFIG +from metagpt.logs import logger -class RepoParser: - def __init__(self): - self.base_directory = None +class RepoParser(BaseModel): + base_directory: Path = Field(default=None) def parse_file(self, file_path): """Parse a Python file in the repository.""" @@ -38,43 +42,42 @@ class RepoParser: file_info["classes"].append({"name": node.name, "methods": class_methods}) elif is_func(node): file_info["functions"].append(node.name) - elif isinstance(node, ast.Assign) or isinstance(node, ast.AnnAssign): + elif isinstance(node, (ast.Assign, ast.AnnAssign)): for target in node.targets if isinstance(node, ast.Assign) else [node.target]: if isinstance(target, ast.Name): file_info["globals"].append(target.id) return file_info - def generate_json_structure(self, directory, output_path): - """Generate a JSON file documenting the repository structure.""" + def generate_symbols(self): files_classes = [] + directory = self.base_directory for path in directory.rglob('*.py'): tree = self.parse_file(path) file_info = self.extract_class_and_function_info(tree, path) files_classes.append(file_info) + return files_classes + + def generate_json_structure(self, output_path): + """Generate a JSON file documenting the repository structure.""" + files_classes = self.generate_symbols() output_path.write_text(json.dumps(files_classes, indent=4)) - def generate_dataframe_structure(self, directory, output_path): + def generate_dataframe_structure(self, output_path): """Generate a DataFrame documenting the repository structure and save as CSV.""" - files_classes = [] - for path in directory.rglob('*.py'): - tree = self.parse_file(path) - file_info = self.extract_class_and_function_info(tree, path) - files_classes.append(file_info) - + files_classes = self.generate_symbols() df = pd.DataFrame(files_classes) df.to_csv(output_path, index=False) - def generate_structure(self, directory_path, output_path=None, mode='json'): + def generate_structure(self, output_path=None, mode='json'): """Generate the structure of the repository as a specified format.""" - self.base_directory = pathlib.Path(directory_path) output_file = self.base_directory / f"{self.base_directory.name}-structure.{mode}" - output_path = pathlib.Path(output_path) if output_path else output_file + output_path = Path(output_path) if output_path else output_file if mode == 'json': - self.generate_json_structure(self.base_directory, output_path) + self.generate_json_structure(output_path) elif mode == 'csv': - self.generate_dataframe_structure(self.base_directory, output_path) + self.generate_dataframe_structure(output_path) def is_func(node): @@ -82,8 +85,9 @@ def is_func(node): def main(): - repo_parser = RepoParser() - repo_parser.generate_structure("/Users/alexanderwu/git/mg1/metagpt", "/Users/alexanderwu/git/mg1/mg1-structure.csv", mode='csv') + repo_parser = RepoParser(base_directory=CONFIG.workspace_path / "web_2048") + symbols = repo_parser.generate_symbols() + logger.info(pformat(symbols)) if __name__ == '__main__': diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 171af47f0..e3f36b50d 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -11,7 +11,7 @@ from collections import OrderedDict from pathlib import Path from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks -from metagpt.actions.SummarizeCode import SummarizeCode +from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role @@ -74,8 +74,8 @@ class Engineer(Role): super().__init__(name, profile, goal, constraints) self._init_actions([WriteCode]) self.use_code_review = use_code_review - if self.use_code_review: - self._init_actions([WriteCode, WriteCodeReview]) + # if self.use_code_review: + # self._init_actions([WriteCode, WriteCodeReview]) self._watch([WriteTasks]) self.todos = [] self.n_borg = n_borg @@ -93,8 +93,8 @@ class Engineer(Role): @classmethod def parse_workspace(cls, system_design_msg: Message) -> str: if system_design_msg.instruct_content: - return system_design_msg.instruct_content.dict().get("Python package name").strip().strip("'").strip('"') - return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) + return system_design_msg.instruct_content.dict().get("project_name").strip().strip("'").strip('"') + return CodeParser.parse_str(block="project_name", text=system_design_msg.content) def get_workspace(self) -> Path: msg = self._rc.memory.get_by_action(WriteDesign)[-1] @@ -182,16 +182,16 @@ class Engineer(Role): msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) for m in msg: context.append(m.content) - context_str = "\n".join(context) + context_str = "\n----------\n".join(context) # Write code code = await WriteCode().run(context=context_str, filename=todo) # Code review if self.use_code_review: - try: - rewrite_code = await WriteCodeReview().run(context=context_str, code=code, filename=todo) - code = rewrite_code - except Exception as e: - logger.error("code review failed!", e) + # try: + rewrite_code = await WriteCodeReview().run(context=context_str, code=code, filename=todo) + code = rewrite_code + # except Exception as e: + # logger.error("code review failed!", e) file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=WriteCode) self._rc.memory.add(msg) @@ -203,8 +203,8 @@ class Engineer(Role): msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) for m in msg: context.append(m.content) - context_str = "\n".join(context) - code_review_all = await SummarizeCode().run(context=context_str) + context_str = "\n----------\n".join(context) + summary = await SummarizeCode().run(context=context_str) logger.info(f"Done {self.get_workspace()} generating.") msg = Message( diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index f124646b3..313fe4aba 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -45,8 +45,8 @@ class QaEngineer(Role): @classmethod def parse_workspace(cls, system_design_msg: Message) -> str: if system_design_msg.instruct_content: - return system_design_msg.instruct_content.dict().get("Python package name") - return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) + return system_design_msg.instruct_content.dict().get("project_name") + return CodeParser.parse_str(block="project_name", text=system_design_msg.content) def get_workspace(self, return_proj_dir=True) -> Path: msg = self._rc.memory.get_by_action(WriteDesign)[-1] diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index d772c0748..5c5e7b76d 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -50,6 +50,7 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi {name}: {result} """ + class RoleReactMode(str, Enum): REACT = "react" BY_ORDER = "by_order" @@ -59,6 +60,7 @@ class RoleReactMode(str, Enum): def values(cls): return [item.value for item in cls] + class RoleSetting(BaseModel): """Role Settings""" name: str @@ -131,6 +133,7 @@ class Role: logger.warning(f"is_human attribute does not take effect," f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances") i = action + i.set_env(self._rc.env) i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) self._states.append(f"{idx}. {action}") @@ -172,6 +175,18 @@ class Role: """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env + def set_doc(self, content: str, filename: str): + return self._rc.env.set_doc(content, filename) + + def get_doc(self, filename: str): + return self._rc.env.get_doc(filename) + + def set(self, k, v): + return self._rc.env.set(k, v) + + def get(self, k): + return self._rc.env.get(k) + @property def profile(self): """Get the role description (position)""" diff --git a/metagpt/startup.py b/metagpt/startup.py index d8ca4072f..38f457fc2 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -15,7 +15,8 @@ def startup( code_review: bool = typer.Option(True, help="Whether to use code review."), run_tests: bool = typer.Option(False, help="Whether to enable QA for adding & running tests."), implement: bool = typer.Option(True, help="Enable or disable code implementation."), - project_name: str = typer.Option("", help="Unique project name, such as 'game_2048'"), + project_name: str = typer.Option("", help="Unique project name, such as 'game_2048'."), + inc: bool = typer.Option(False, help="Incremental mode. Use it to coop with existing repo."), ): """Run a startup. Be a boss.""" from metagpt.roles import ProductManager, Architect, ProjectManager, Engineer, QaEngineer @@ -37,9 +38,9 @@ def startup( company.hire([QaEngineer()]) company.invest(investment) - company.start_project(project_name, idea) + company.run_project(idea, project_name=project_name, inc=inc) asyncio.run(company.run(n_round=n_round)) if __name__ == "__main__": - app() + startup(idea="Make a 2048 game.") diff --git a/metagpt/team.py b/metagpt/team.py index 2332aaa46..a22a09fe4 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -42,15 +42,19 @@ class Team(BaseModel): if CONFIG.total_cost > CONFIG.max_budget: raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}') - def start_project(self, project_name, idea, send_to: str = ""): + def run_project(self, idea, send_to: str = "", project_name: str = "", inc: bool = False): """Start a project from publishing user requirement.""" self.idea = idea # If user set project_name, then use it. - self.env.repo.name = project_name + if project_name: + path = CONFIG.workspace_path / project_name + self.env.load_existing_repo(path, inc=inc) + + # Human requirement. self.env.publish_message(Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to)) def _save(self): - logger.info(self.json()) + logger.info(self.json(ensure_ascii=False)) async def run(self, n_round=3): """Run company until target round or no money""" diff --git a/tests/metagpt/actions/mock.py b/tests/metagpt/actions/mock.py index 5be1d8001..d367e253e 100644 --- a/tests/metagpt/actions/mock.py +++ b/tests/metagpt/actions/mock.py @@ -90,7 +90,7 @@ Python's in-built data structures like lists and dictionaries will be used exten For testing, we can use the PyTest framework. This is a mature full-featured Python testing tool that helps you write better programs. -## Python package name: +## project_name: ```python "adventure_game" ``` diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index fbad06acb..c06844389 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -71,7 +71,7 @@ PRD = '''## 原始需求 ``` ''' -SYSTEM_DESIGN = '''## Python package name +SYSTEM_DESIGN = '''## project_name ```python "smart_search_engine" ``` diff --git a/tests/metagpt/roles/test_ui.py b/tests/metagpt/roles/test_ui.py index d58d31bd9..ec507f75d 100644 --- a/tests/metagpt/roles/test_ui.py +++ b/tests/metagpt/roles/test_ui.py @@ -18,5 +18,5 @@ async def test_ui_role(idea: str, investment: float = 3.0, n_round: int = 5): company = Team() company.hire([ProductManager(), UI()]) company.invest(investment) - company.start_project(idea) + company.run_project(idea) await company.run(n_round=n_round) diff --git a/tests/metagpt/test_software_company.py b/tests/metagpt/test_startup.py similarity index 51% rename from tests/metagpt/test_software_company.py rename to tests/metagpt/test_startup.py index 4fc651f52..53a8d8735 100644 --- a/tests/metagpt/test_software_company.py +++ b/tests/metagpt/test_startup.py @@ -3,17 +3,26 @@ """ @Time : 2023/5/15 11:40 @Author : alexanderwu -@File : test_software_company.py +@File : test_startup.py """ import pytest +from typer.testing import CliRunner + +runner = CliRunner() from metagpt.logs import logger from metagpt.team import Team +from metagpt.startup import app @pytest.mark.asyncio async def test_team(): company = Team() - company.start_project("做一个基础搜索引擎,可以支持知识库") + company.run_project("做一个基础搜索引擎,可以支持知识库") history = await company.run(n_round=5) logger.info(history) + + +# def test_startup(): +# args = ["Make a 2048 game"] +# result = runner.invoke(app, args) From 70a0be3300a3b7e3d40a7ae55692f36ab8e46ce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:49:05 +0800 Subject: [PATCH 174/413] feat: +annotation --- metagpt/const.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/const.py b/metagpt/const.py index 49965b622..a8c7356ca 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -4,8 +4,9 @@ @Time : 2023/5/1 11:59 @Author : alexanderwu @File : const.py -@Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, added key definitions for +@Modified By: mashenquan, 2023-11-1. According to Section 2.2.1 and 2.2.2 of RFC 116, added key definitions for common properties in the Message. +@Modified By: mashenquan, 2023-11-27. Defines file repository paths according to Section 2.2.3.4 of RFC 135. """ from pathlib import Path From ad6cf62d2113bb16b9ce93656d353dcbe06a2657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 15:57:19 +0800 Subject: [PATCH 175/413] feat: +annotation --- metagpt/roles/engineer.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index b6ecc4767..3cf1f2125 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -10,6 +10,9 @@ 2. Consolidate message reception and processing logic within `_observe`. 3. Fix bug: Add logic for handling asynchronous message processing when messages are not ready. 4. Supplemented the external transmission of internal messages. +@Modified By: mashenquan, 2023-11-27. + 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. + 2. According to the design in Section 2.2.3.5.5 of RFC 135, add incremental iteration functionality. """ from __future__ import annotations @@ -97,11 +100,11 @@ class Engineer(Role): async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" changed_files = await self._act_sp_precision(review=self.use_code_review) - # 仅单测 + # Unit tests only. if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files: changed_files.add(CONFIG.REQA_FILENAME) - from metagpt.roles import QaEngineer # 避免循环引用 + from metagpt.roles import QaEngineer # Avoid circular references. msg = Message( content="\n".join(changed_files), @@ -122,7 +125,7 @@ class Engineer(Role): design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_files = Documents() - # 由上游变化导致的recode + # Recode caused by upstream changes. for filename in changed_task_files: design_doc = await design_file_repo.get(filename) task_doc = await task_file_repo.get(filename) @@ -144,7 +147,7 @@ class Engineer(Role): ) changed_files.docs[task_filename] = coding_doc self.todos = [WriteCode(context=i, llm=self._llm) for i in changed_files.docs.values()] - # 用户直接修改的code + # Code directly modified by the user. dependency = await CONFIG.git_repo.get_dependency() for filename in changed_src_files: if filename in changed_files.docs: From 94ab03d2daf66bc81db70a8a7f30323555f86ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:02:18 +0800 Subject: [PATCH 176/413] feat: +annotation --- metagpt/roles/product_manager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 81577ec2c..bc6771829 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -4,6 +4,7 @@ @Time : 2023/5/11 14:43 @Author : alexanderwu @File : product_manager.py +@Modified By: mashenquan, 2023/11/27. Add `PrepareDocuments` action according to Section 2.2.3.5.1 of RFC 135. """ from metagpt.actions import BossRequirement, WritePRD from metagpt.actions.prepare_documents import PrepareDocuments From 41549e628082156e1bd613aa337665e95c2d685c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:11:49 +0800 Subject: [PATCH 177/413] feat: +annotation --- metagpt/roles/qa_engineer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index a88b01e37..763ab6a3f 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -6,6 +6,11 @@ @File : qa_engineer.py @Modified By: mashenquan, 2023-11-1. In accordance with Chapter 2.2.1 and 2.2.2 of RFC 116, modify the data type of the `cause_by` value in the `Message` to a string, and utilize the new message filtering feature. +@Modified By: mashenquan, 2023-11-27. + 1. Following the think-act principle, solidify the task parameters when creating the + WriteTest/RunCode/DebugError object, rather than passing them in when calling the run function. + 2. According to Section 2.2.3.5.7 of RFC 135, change the method of transferring files from using the Message + to using file references. """ from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest from metagpt.config import CONFIG From e656e55f304f79a05591bad33dd88df6230b0d53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:15:55 +0800 Subject: [PATCH 178/413] feat: +annotation --- metagpt/schema.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index e910fc866..959e70dc1 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -6,7 +6,10 @@ @File : schema.py @Modified By: mashenquan, 2023-10-31. According to Chapter 2.2.1 of RFC 116: Replanned the distribution of responsibilities and functional positioning of `Message` class attributes. -@Modified By: mashenquan, 2023/11/22. Add `Document` and `Documents` for `FileRepository` in Section 2.2.3.4 of RFC 135. +@Modified By: mashenquan, 2023/11/22. + 1. Add `Document` and `Documents` for `FileRepository` in Section 2.2.3.4 of RFC 135. + 2. Encapsulate the common key-values set to pydantic structures to standardize and unify parameter passing + between actions. """ from __future__ import annotations From 4c296a348b0f59b5889f9baf207ff0b91619b982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:18:39 +0800 Subject: [PATCH 179/413] feat: +annotation --- metagpt/software_company.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/software_company.py b/metagpt/software_company.py index 5aa0864e0..72f28ab1d 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -4,6 +4,8 @@ @Time : 2023/5/12 00:30 @Author : alexanderwu @File : software_company.py +@Modified By: mashenquan, 2023/11/27. Add an archiving operation after completing the project, as specified in + Section 2.2.3.3 of RFC 135. """ from pydantic import BaseModel, Field From 0c84c2c212bf6892ecbd9d4fb28c2135a881379c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:21:53 +0800 Subject: [PATCH 180/413] feat: +annotation --- metagpt/utils/common.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 9002a8dfb..fd3958a61 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -6,6 +6,8 @@ @File : common.py @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.2 of RFC 116: Add generic class-to-string and object-to-string conversion functionality. +@Modified By: mashenquan, 2023/11/27. Bug fix: `parse_recipient` failed to parse the recipient in certain GPT-3.5 + responses. """ import ast import contextlib From bd5daeb4e6743dd859d1d341f39eb95efcfa1b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 16:27:35 +0800 Subject: [PATCH 181/413] feat: +annotation --- metagpt/utils/dependency_file.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index 429027c7a..653e07ef9 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -18,11 +18,21 @@ from metagpt.logs import logger class DependencyFile: + """A class representing a DependencyFile for managing dependencies. + + :param workdir: The working directory path for the DependencyFile. + """ + def __init__(self, workdir: Path | str): + """Initialize a DependencyFile instance. + + :param workdir: The working directory path for the DependencyFile. + """ self._dependencies = {} self._filename = Path(workdir) / ".dependencies.json" async def load(self): + """Load dependencies from the file asynchronously.""" if not self._filename.exists(): return try: @@ -33,6 +43,7 @@ class DependencyFile: logger.error(f"Failed to load {str(self._filename)}, error:{e}") async def save(self): + """Save dependencies to the file asynchronously.""" try: data = json.dumps(self._dependencies) async with aiofiles.open(str(self._filename), mode="w") as writer: @@ -41,6 +52,12 @@ class DependencyFile: logger.error(f"Failed to save {str(self._filename)}, error:{e}") async def update(self, filename: Path | str, dependencies: Set[Path | str], persist=True): + """Update dependencies for a file asynchronously. + + :param filename: The filename or path. + :param dependencies: The set of dependencies. + :param persist: Whether to persist the changes immediately. + """ if persist: await self.load() @@ -65,6 +82,12 @@ class DependencyFile: await self.save() async def get(self, filename: Path | str, persist=False): + """Get dependencies for a file asynchronously. + + :param filename: The filename or path. + :param persist: Whether to load dependencies from the file immediately. + :return: A set of dependencies. + """ if persist: await self.load() @@ -76,8 +99,10 @@ class DependencyFile: return set(self._dependencies.get(str(key), {})) def delete_file(self): + """Delete the dependency file.""" self._filename.unlink(missing_ok=True) @property def exists(self): + """Check if the dependency file exists.""" return self._filename.exists() From 66fc1b83509cf0d85556a339c21941232ad2934c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 17:43:20 +0800 Subject: [PATCH 182/413] feat: merge geekan:main --- ...mit-config.yaml => .pre-commit-config.yam_ | 0 README.md | 311 ++++-------------- config/config.yaml | 5 +- docs/README_CN.md | 215 ++++-------- docs/README_JA.md | 33 +- examples/build_customized_agent.py | 60 +--- examples/debate.py | 99 ++---- metagpt/config.py | 9 +- metagpt/const.py | 9 +- metagpt/llm.py | 29 +- metagpt/provider/base_chatbot.py | 1 + metagpt/provider/base_gpt_api.py | 55 +++- metagpt/provider/openai_api.py | 84 ++++- metagpt/roles/engineer.py | 1 + metagpt/roles/invoice_ocr_assistant.py | 26 +- metagpt/roles/researcher.py | 19 +- metagpt/roles/role.py | 126 +++++-- metagpt/software_company.py | 59 +--- metagpt/utils/mermaid.py | 5 +- metagpt/utils/token_counter.py | 6 +- requirements.txt | 4 +- setup.py | 6 +- startup.py | 4 +- tests/metagpt/roles/test_ui.py | 4 +- tests/metagpt/test_software_company.py | 6 +- 25 files changed, 488 insertions(+), 688 deletions(-) rename .pre-commit-config.yaml => .pre-commit-config.yam_ (100%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yam_ similarity index 100% rename from .pre-commit-config.yaml rename to .pre-commit-config.yam_ diff --git a/README.md b/README.md index 70460ceb4..e80082a3a 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,13 @@ # MetaGPT: The Multi-Agent Framework CN doc EN doc JA doc -Discord Follow +Discord Follow License: MIT roadmap Twitter Follow

- AgentStore Waitlist Open in Dev Containers Open in GitHub Codespaces Hugging Face @@ -33,132 +32,38 @@ # MetaGPT: The Multi-Agent Framework

Software Company Multi-Role Schematic (Gradually Implementing)

-## MetaGPT's Abilities -https://github.com/geekan/MetaGPT/assets/34952977/34345016-5d13-489d-b9f9-b82ace413419 +## Install - - -## Examples (fully generated by GPT-4) - -For example, if you type `python startup.py "Design a RecSys like Toutiao"`, you would get many outputs, one of them is data & api design - -![Jinri Toutiao Recsys Data & API Design](docs/resources/workspace/content_rec_sys/resources/data_api_design.png) - -It costs approximately **$0.2** (in GPT-4 API fees) to generate one example with analysis and design, and around **$2.0** for a full project. - - - - -## Installation - -### Installation Video Guide - -- [Matthew Berman: How To Install MetaGPT - Build A Startup With One Prompt!!](https://youtu.be/uT75J_KG_aY) - -### Traditional Installation +### Pip installation ```bash -# Step 1: Ensure that NPM is installed on your system. Then install mermaid-js. (If you don't have npm in your computer, please go to the Node.js official website to install Node.js https://nodejs.org/ and then you will have npm tool in your computer.) +# Step 1: Ensure that Python 3.9+ is installed on your system. You can check this by using: +# You can use conda to initialize a new python env +# conda create -n metagpt python=3.9 +# conda activate metagpt +python3 --version + +# Step 2: Clone the repository to your local machine for latest version, and install it. +git clone https://github.com/geekan/MetaGPT.git +cd MetaGPT +pip3 install -e. # or pip3 install metagpt # for stable version + +# Step 3: run the startup.py +# setup your OPENAI_API_KEY in key.yaml copy from config.yaml +python3 startup.py "Write a cli snake game" + +# Step 4 [Optional]: If you want to save the artifacts like diagrams such as quadrant chart, system designs, sequence flow in the workspace, you can execute the step before Step 3. By default, the framework is compatible, and the entire process can be run completely without executing this step. +# If executing, ensure that NPM is installed on your system. Then install mermaid-js. (If you don't have npm in your computer, please go to the Node.js official website to install Node.js https://nodejs.org/ and then you will have npm tool in your computer.) npm --version sudo npm install -g @mermaid-js/mermaid-cli - -# Step 2: Ensure that Python 3.9+ is installed on your system. You can check this by using: -python --version - -# Step 3: Clone the repository to your local machine, and install it. -git clone https://github.com/geekan/metagpt -cd metagpt -pip install -e. ``` -**Note:** +detail installation please refer to [cli_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-stable-version) -- If already have Chrome, Chromium, or MS Edge installed, you can skip downloading Chromium by setting the environment variable - `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` to `true`. - -- Some people are [having issues](https://github.com/mermaidjs/mermaid.cli/issues/15) installing this tool globally. Installing it locally is an alternative solution, - - ```bash - npm install @mermaid-js/mermaid-cli - ``` - -- don't forget to the configuration for mmdc in config.yml - - ```yml - PUPPETEER_CONFIG: "./config/puppeteer-config.json" - MMDC: "./node_modules/.bin/mmdc" - ``` - -- if `pip install -e.` fails with error `[Errno 13] Permission denied: '/usr/local/lib/python3.11/dist-packages/test-easy-install-13129.write-test'`, try instead running `pip install -e. --user` - -- To convert Mermaid charts to SVG, PNG, and PDF formats. In addition to the Node.js version of Mermaid-CLI, you now have the option to use Python version Playwright, pyppeteer or mermaid.ink for this task. - - - Playwright - - **Install Playwright** - - ```bash - pip install playwright - ``` - - - **Install the Required Browsers** - - to support PDF conversion, please install Chrominum. - - ```bash - playwright install --with-deps chromium - ``` - - - **modify `config.yaml`** - - uncomment MERMAID_ENGINE from config.yaml and change it to `playwright` - - ```yaml - MERMAID_ENGINE: playwright - ``` - - - pyppeteer - - **Install pyppeteer** - - ```bash - pip install pyppeteer - ``` - - - **Use your own Browsers** - - pyppeteer allows you use installed browsers, please set the following envirment - - ```bash - export PUPPETEER_EXECUTABLE_PATH = /path/to/your/chromium or edge or chrome - ``` - - please do not use this command to install browser, it is too old - - ```bash - pyppeteer-install - ``` - - - **modify `config.yaml`** - - uncomment MERMAID_ENGINE from config.yaml and change it to `pyppeteer` - - ```yaml - MERMAID_ENGINE: pyppeteer - ``` - - - mermaid.ink - - **modify `config.yaml`** - - uncomment MERMAID_ENGINE from config.yaml and change it to `ink` - - ```yaml - MERMAID_ENGINE: ink - ``` - - Note: this method does not support pdf export. - -### Installation by Docker +### Docker installation +> Note: In the Windows, you need to replace "/opt/metagpt" with a directory that Docker has permission to create, such as "D:\Users\x\metagpt" ```bash # Step 1: Download metagpt official image and prepare config.yaml @@ -174,141 +79,41 @@ # Step 2: Run metagpt demo with container -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ python startup.py "Write a cli snake game" - -# You can also start a container and execute commands in it -docker run --name metagpt -d \ - --privileged \ - -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ - -v /opt/metagpt/workspace:/app/metagpt/workspace \ - metagpt/metagpt:latest - -docker exec -it metagpt /bin/bash -$ python startup.py "Write a cli snake game" ``` -The command `docker run ...` do the following things: +detail installation please refer to [docker_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-with-docker) -- Run in privileged mode to have permission to run the browser -- Map host configure file `/opt/metagpt/config/key.yaml` to container `/app/metagpt/config/key.yaml` -- Map host directory `/opt/metagpt/workspace` to container `/app/metagpt/workspace` -- Execute the demo command `python startup.py "Write a cli snake game"` +### QuickStart & Demo Video +- Try it on [MetaGPT Huggingface Space](https://huggingface.co/spaces/deepwisdom/MetaGPT) +- [Matthew Berman: How To Install MetaGPT - Build A Startup With One Prompt!!](https://youtu.be/uT75J_KG_aY) +- [Official Demo Video](https://github.com/geekan/MetaGPT/assets/2707039/5e8c1062-8c35-440f-bb20-2b0320f8d27d) -### Build image by yourself +https://github.com/geekan/MetaGPT/assets/34952977/34345016-5d13-489d-b9f9-b82ace413419 -```bash -# You can also build metagpt image by yourself. -git clone https://github.com/geekan/MetaGPT.git -cd MetaGPT && docker build -t metagpt:custom . -``` +## Tutorial -## Configuration +- 🗒 [Online Document](https://docs.deepwisdom.ai/) +- 💻 [Usage](https://docs.deepwisdom.ai/guide/get_started/quickstart.html) +- 🔎 [What can MetaGPT do?](https://docs.deepwisdom.ai/guide/get_started/introduction.html) +- 🛠 How to build your own agents? + - [MetaGPT Usage & Development Guide | Agent 101](https://docs.deepwisdom.ai/guide/tutorials/agent_101.html) + - [MetaGPT Usage & Development Guide | MultiAgent 101](https://docs.deepwisdom.ai/guide/tutorials/multi_agent_101.html) +- 🧑‍💻 Contribution + - [Develop Roadmap](docs/ROADMAP.md) +- 🔖 Use Cases + - [Debate](https://docs.deepwisdom.ai/guide/use_cases/multi_agent/debate.html) + - [Researcher](https://docs.deepwisdom.ai/guide/use_cases/agent/researcher.html) + - [Recepit Assistant](https://docs.deepwisdom.ai/guide/use_cases/agent/receipt_assistant.html) +- ❓ [FAQs](https://docs.deepwisdom.ai/guide/faq.html) -- Configure your `OPENAI_API_KEY` in any of `config/key.yaml / config/config.yaml / env` -- Priority order: `config/key.yaml > config/config.yaml > env` +## Support -```bash -# Copy the configuration file and make the necessary modifications. -cp config/config.yaml config/key.yaml -``` +### Discard Join US +📢 Join Our [Discord Channel](https://discord.gg/ZRHeExS6xv)! -| Variable Name | config/key.yaml | env | -| ------------------------------------------ | ----------------------------------------- | ----------------------------------------------- | -| OPENAI_API_KEY # Replace with your own key | OPENAI_API_KEY: "sk-..." | export OPENAI_API_KEY="sk-..." | -| OPENAI_API_BASE # Optional | OPENAI_API_BASE: "https:///v1" | export OPENAI_API_BASE="https:///v1" | +Looking forward to seeing you there! 🎉 -## Tutorial: Initiating a startup - -```shell -# Run the script -python startup.py "Write a cli snake game" -# Do not hire an engineer to implement the project -python startup.py "Write a cli snake game" --implement False -# Hire an engineer and perform code reviews -python startup.py "Write a cli snake game" --code_review True -``` - -After running the script, you can find your new project in the `workspace/` directory. - -### Preference of Platform or Tool - -You can tell which platform or tool you want to use when stating your requirements. - -```shell -python startup.py "Write a cli snake game based on pygame" -``` - -### Usage - -``` -NAME - startup.py - We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities. - -SYNOPSIS - startup.py IDEA - -DESCRIPTION - We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities. - -POSITIONAL ARGUMENTS - IDEA - Type: str - Your innovative idea, such as "Creating a snake game." - -FLAGS - --investment=INVESTMENT - Type: float - Default: 3.0 - As an investor, you have the opportunity to contribute a certain dollar amount to this AI company. - --n_round=N_ROUND - Type: int - Default: 5 - -NOTES - You can also use flags syntax for POSITIONAL ARGUMENTS -``` - -### Code walkthrough - -```python -from metagpt.software_company import SoftwareCompany -from metagpt.roles import ProjectManager, ProductManager, Architect, Engineer - -async def startup(idea: str, investment: float = 3.0, n_round: int = 5): - """Run a startup. Be a boss.""" - company = SoftwareCompany() - company.hire([ProductManager(), Architect(), ProjectManager(), Engineer()]) - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) -``` - -You can check `examples` for more details on single role (with knowledge base) and LLM only examples. - -## QuickStart - -It is difficult to install and configure the local environment for some users. The following tutorials will allow you to quickly experience the charm of MetaGPT. - -- [MetaGPT quickstart](https://deepwisdom.feishu.cn/wiki/CyY9wdJc4iNqArku3Lncl4v8n2b) - -Try it on Huggingface Space -- https://huggingface.co/spaces/deepwisdom/MetaGPT - -## Citation - -For now, cite the [Arxiv paper](https://arxiv.org/abs/2308.00352): - -```bibtex -@misc{hong2023metagpt, - title={MetaGPT: Meta Programming for Multi-Agent Collaborative Framework}, - author={Sirui Hong and Xiawu Zheng and Jonathan Chen and Yuheng Cheng and Jinlin Wang and Ceyao Zhang and Zili Wang and Steven Ka Shing Yau and Zijuan Lin and Liyang Zhou and Chenyu Ran and Lingfeng Xiao and Chenglin Wu}, - year={2023}, - eprint={2308.00352}, - archivePrefix={arXiv}, - primaryClass={cs.AI} -} -``` - -## Contact Information +### Contact Information If you have any questions or feedback about this project, please feel free to contact us. We highly appreciate your suggestions! @@ -317,13 +122,17 @@ ## Contact Information We will respond to all questions within 2-3 business days. -## Demo +## Citation -https://github.com/geekan/MetaGPT/assets/2707039/5e8c1062-8c35-440f-bb20-2b0320f8d27d +For now, cite the [arXiv paper](https://arxiv.org/abs/2308.00352): -## Join us - -📢 Join Our Discord Channel! -https://discord.gg/ZRHeExS6xv - -Looking forward to seeing you there! 🎉 +```bibtex +@misc{hong2023metagpt, + title={MetaGPT: Meta Programming for A Multi-Agent Collaborative Framework}, + author={Sirui Hong and Mingchen Zhuge and Jonathan Chen and Xiawu Zheng and Yuheng Cheng and Ceyao Zhang and Jinlin Wang and Zili Wang and Steven Ka Shing Yau and Zijuan Lin and Liyang Zhou and Chenyu Ran and Lingfeng Xiao and Chenglin Wu and Jürgen Schmidhuber}, + year={2023}, + eprint={2308.00352}, + archivePrefix={arXiv}, + primaryClass={cs.AI} +} +``` diff --git a/config/config.yaml b/config/config.yaml index b2c50991d..bed67083c 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -7,7 +7,7 @@ ## Or, you can configure OPENAI_PROXY to access official OPENAI_API_BASE. OPENAI_API_BASE: "https://api.openai.com/v1" #OPENAI_PROXY: "http://127.0.0.1:8118" -#OPENAI_API_KEY: "YOUR_API_KEY" +#OPENAI_API_KEY: "YOUR_API_KEY" # set the value to sk-xxx if you host the openai interface for open llm model OPENAI_API_MODEL: "gpt-4" MAX_TOKENS: 1500 RPM: 10 @@ -31,6 +31,9 @@ RPM: 10 #DEPLOYMENT_NAME: "YOUR_DEPLOYMENT_NAME" #DEPLOYMENT_ID: "YOUR_DEPLOYMENT_ID" +#### if zhipuai from `https://open.bigmodel.cn`. You can set here or export API_KEY="YOUR_API_KEY" +# ZHIPUAI_API_KEY: "YOUR_API_KEY" + #### for Search ## Supported values: serpapi/google/serper/ddg diff --git a/docs/README_CN.md b/docs/README_CN.md index 9d6f34c11..038925184 100644 --- a/docs/README_CN.md +++ b/docs/README_CN.md @@ -12,14 +12,13 @@ # MetaGPT: 多智能体框架 CN doc EN doc JA doc -Discord Follow +Discord Follow License: MIT roadmap Twitter Follow

- AgentStore Waitlist Open in Dev Containers Open in GitHub Codespaces Hugging Face @@ -33,57 +32,35 @@ # MetaGPT: 多智能体框架

软件公司多角色示意图(正在逐步实现)

-## MetaGPT 的能力 - -https://github.com/geekan/MetaGPT/assets/34952977/34345016-5d13-489d-b9f9-b82ace413419 - - -## 示例(均由 GPT-4 生成) - -例如,键入`python startup.py "写个类似今日头条的推荐系统"`并回车,你会获得一系列输出,其一是数据结构与API设计 - -![今日头条 Recsys 数据 & API 设计](resources/workspace/content_rec_sys/resources/data_api_design.png) - -这需要大约**0.2美元**(GPT-4 API的费用)来生成一个带有分析和设计的示例,大约2.0美元用于一个完整的项目 - ## 安装 - -### 传统安装 +### Pip安装 ```bash -# 第 1 步:确保您的系统上安装了 NPM。并使用npm安装mermaid-js +# 第 1 步:确保您的系统上安装了 Python 3.9+。您可以使用以下命令进行检查: +# 可以使用conda来初始化新的python环境 +# conda create -n metagpt python=3.9 +# conda activate metagpt +python3 --version + +# 第 2 步:克隆最新仓库到您的本地机器,并进行安装。 +git clone https://github.com/geekan/MetaGPT.git +cd MetaGPT +pip3 install -e. # 或者 pip3 install metagpt # 安装稳定版本 + +# 第 3 步:执行startup.py +# 拷贝config.yaml为key.yaml,并设置你自己的OPENAI_API_KEY +python3 startup.py "Write a cli snake game" + +# 第 4 步【可选的】:如果你想在执行过程中保存像象限图、系统设计、序列流程等图表这些产物,可以在第3步前执行该步骤。默认的,框架做了兼容,在不执行该步的情况下,也可以完整跑完整个流程。 +# 如果执行,确保您的系统上安装了 NPM。并使用npm安装mermaid-js npm --version sudo npm install -g @mermaid-js/mermaid-cli - -# 第 2 步:确保您的系统上安装了 Python 3.9+。您可以使用以下命令进行检查: -python --version - -# 第 3 步:克隆仓库到您的本地机器,并进行安装。 -git clone https://github.com/geekan/metagpt -cd metagpt -pip install -e. ``` -**注意:** - -- 如果已经安装了Chrome、Chromium或MS Edge,可以通过将环境变量`PUPPETEER_SKIP_CHROMIUM_DOWNLOAD`设置为`true`来跳过下载Chromium。 - -- 一些人在全局安装此工具时遇到问题。在本地安装是替代解决方案, - - ```bash - npm install @mermaid-js/mermaid-cli - ``` - -- 不要忘记在config.yml中为mmdc配置配置, - - ```yml - PUPPETEER_CONFIG: "./config/puppeteer-config.json" - MMDC: "./node_modules/.bin/mmdc" - ``` - -- 如果`pip install -e.`失败并显示错误`[Errno 13] Permission denied: '/usr/local/lib/python3.11/dist-packages/test-easy-install-13129.write-test'`,请尝试使用`pip install -e. --user`运行。 +详细的安装请安装 [cli_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-stable-version) ### Docker安装 +> 注意:在Windows中,你需要将 "/opt/metagpt" 替换为Docker具有创建权限的目录,比如"D:\Users\x\metagpt" ```bash # 步骤1: 下载metagpt官方镜像并准备好config.yaml @@ -99,121 +76,41 @@ # 步骤2: 使用容器运行metagpt演示 -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ python startup.py "Write a cli snake game" - -# 您也可以启动一个容器并在其中执行命令 -docker run --name metagpt -d \ - --privileged \ - -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ - -v /opt/metagpt/workspace:/app/metagpt/workspace \ - metagpt/metagpt:latest - -docker exec -it metagpt /bin/bash -$ python startup.py "Write a cli snake game" ``` -`docker run ...`做了以下事情: +详细的安装请安装 [docker_install](https://docs.deepwisdom.ai/zhcn/guide/get_started/installation.html#%E4%BD%BF%E7%94%A8docker%E5%AE%89%E8%A3%85) -- 以特权模式运行,有权限运行浏览器 -- 将主机文件 `/opt/metagpt/config/key.yaml` 映射到容器文件 `/app/metagpt/config/key.yaml` -- 将主机目录 `/opt/metagpt/workspace` 映射到容器目录 `/app/metagpt/workspace` -- 执行示例命令 `python startup.py "Write a cli snake game"` +### 快速开始的演示视频 +- 在 [MetaGPT Huggingface Space](https://huggingface.co/spaces/deepwisdom/MetaGPT) 上进行体验 +- [Matthew Berman: How To Install MetaGPT - Build A Startup With One Prompt!!](https://youtu.be/uT75J_KG_aY) +- [官方演示视频](https://github.com/geekan/MetaGPT/assets/2707039/5e8c1062-8c35-440f-bb20-2b0320f8d27d) -### 自己构建镜像 +https://github.com/geekan/MetaGPT/assets/34952977/34345016-5d13-489d-b9f9-b82ace413419 -```bash -# 您也可以自己构建metagpt镜像 -git clone https://github.com/geekan/MetaGPT.git -cd MetaGPT && docker build -t metagpt:custom . -``` +## 教程 +- 🗒 [在线文档](https://docs.deepwisdom.ai/zhcn/) +- 💻 [如何使用](https://docs.deepwisdom.ai/zhcn/guide/get_started/quickstart.html) +- 🔎 [MetaGPT的能力及应用场景](https://docs.deepwisdom.ai/zhcn/guide/get_started/introduction.html) +- 🛠 如何构建你自己的智能体? + - [MetaGPT的使用和开发教程 | 智能体入门](https://docs.deepwisdom.ai/zhcn/guide/tutorials/agent_101.html) + - [MetaGPT的使用和开发教程 | 多智能体入门](https://docs.deepwisdom.ai/zhcn/guide/tutorials/multi_agent_101.html) +- 🧑‍💻 贡献 + - [开发路线图](ROADMAP.md) +- 🔖 示例 + - [辩论](https://docs.deepwisdom.ai/zhcn/guide/use_cases/multi_agent/debate.html) + - [调研员](https://docs.deepwisdom.ai/zhcn/guide/use_cases/agent/researcher.html) + - [票据助手](https://docs.deepwisdom.ai/zhcn/guide/use_cases/agent/receipt_assistant.html) +- ❓ [常见问题解答](https://docs.deepwisdom.ai/zhcn/guide/faq.html) -## 配置 +## 支持 -- 在 `config/key.yaml / config/config.yaml / env` 中配置您的 `OPENAI_API_KEY` -- 优先级顺序:`config/key.yaml > config/config.yaml > env` +### 加入我们 -```bash -# 复制配置文件并进行必要的修改 -cp config/config.yaml config/key.yaml -``` +📢 加入我们的[Discord频道](https://discord.gg/ZRHeExS6xv)! -| 变量名 | config/key.yaml | env | -| ----------------------------------- | ----------------------------------------- | ----------------------------------------------- | -| OPENAI_API_KEY # 用您自己的密钥替换 | OPENAI_API_KEY: "sk-..." | export OPENAI_API_KEY="sk-..." | -| OPENAI_API_BASE # 可选 | OPENAI_API_BASE: "https:///v1" | export OPENAI_API_BASE="https:///v1" | +期待在那里与您相见!🎉 -## 示例:启动一个创业公司 - -```shell -python startup.py "写一个命令行贪吃蛇" -# 开启code review模式会花费更多的金钱, 但是会提升代码质量和成功率 -python startup.py "写一个命令行贪吃蛇" --code_review True -``` - -运行脚本后,您可以在 `workspace/` 目录中找到您的新项目。 -### 平台或工具的倾向性 -可以在阐述需求时说明想要使用的平台或工具。 -例如: -```shell -python startup.py "写一个基于pygame的命令行贪吃蛇" -``` - -### 使用 - -``` -名称 - startup.py - 我们是一家AI软件创业公司。通过投资我们,您将赋能一个充满无限可能的未来。 - -概要 - startup.py IDEA - -描述 - 我们是一家AI软件创业公司。通过投资我们,您将赋能一个充满无限可能的未来。 - -位置参数 - IDEA - 类型: str - 您的创新想法,例如"写一个命令行贪吃蛇。" - -标志 - --investment=INVESTMENT - 类型: float - 默认值: 3.0 - 作为投资者,您有机会向这家AI公司投入一定的美元金额。 - --n_round=N_ROUND - 类型: int - 默认值: 5 - -备注 - 您也可以用`标志`的语法,来处理`位置参数` -``` - -### 代码实现 - -```python -from metagpt.software_company import SoftwareCompany -from metagpt.roles import ProjectManager, ProductManager, Architect, Engineer - -async def startup(idea: str, investment: float = 3.0, n_round: int = 5): - """运行一个创业公司。做一个老板""" - company = SoftwareCompany() - company.hire([ProductManager(), Architect(), ProjectManager(), Engineer()]) - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) -``` - -你可以查看`examples`,其中有单角色(带知识库)的使用例子与仅LLM的使用例子。 - -## 快速体验 -对一些用户来说,安装配置本地环境是有困难的,下面这些教程能够让你快速体验到MetaGPT的魅力。 - -- [MetaGPT快速体验](https://deepwisdom.feishu.cn/wiki/Q8ycw6J9tiNXdHk66MRcIN8Pnlg) - -可直接在Huggingface Space体验 - -- https://huggingface.co/spaces/deepwisdom/MetaGPT - -## 联系信息 +### 联系信息 如果您对这个项目有任何问题或反馈,欢迎联系我们。我们非常欢迎您的建议! @@ -222,13 +119,17 @@ ## 联系信息 我们会在2-3个工作日内回复所有问题。 -## 演示 +## 引用 -https://github.com/geekan/MetaGPT/assets/2707039/5e8c1062-8c35-440f-bb20-2b0320f8d27d +引用 [arXiv paper](https://arxiv.org/abs/2308.00352): -## 加入我们 - -📢 加入我们的Discord频道! -https://discord.gg/ZRHeExS6xv - -期待在那里与您相见!🎉 +```bibtex +@misc{hong2023metagpt, + title={MetaGPT: Meta Programming for Multi-Agent Collaborative Framework}, + author={Sirui Hong and Xiawu Zheng and Jonathan Chen and Yuheng Cheng and Jinlin Wang and Ceyao Zhang and Zili Wang and Steven Ka Shing Yau and Zijuan Lin and Liyang Zhou and Chenyu Ran and Lingfeng Xiao and Chenglin Wu}, + year={2023}, + eprint={2308.00352}, + archivePrefix={arXiv}, + primaryClass={cs.AI} +} +``` diff --git a/docs/README_JA.md b/docs/README_JA.md index 2b2c35a62..411d190b4 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -19,7 +19,6 @@ # MetaGPT: マルチエージェントフレームワーク

- AgentStore Waitlist Open in Dev Containers Open in GitHub Codespaces Hugging Face @@ -60,17 +59,22 @@ ### インストールビデオガイド ### 伝統的なインストール ```bash -# ステップ 1: NPM がシステムにインストールされていることを確認してください。次に mermaid-js をインストールします。(お使いのコンピューターに npm がない場合は、Node.js 公式サイトで Node.js https://nodejs.org/ をインストールしてください。) -npm --version -sudo npm install -g @mermaid-js/mermaid-cli - -# ステップ 2: Python 3.9+ がシステムにインストールされていることを確認してください。これを確認するには: +# ステップ 1: Python 3.9+ がシステムにインストールされていることを確認してください。これを確認するには: python --version -# ステップ 3: リポジトリをローカルマシンにクローンし、インストールする。 -git clone https://github.com/geekan/metagpt -cd metagpt +# ステップ 2: リポジトリをローカルマシンにクローンし、インストールする。 +git clone https://github.com/geekan/MetaGPT.git +cd MetaGPT pip install -e. + +# ステップ 3: startup.py を実行する +# config.yaml を key.yaml にコピーし、独自の OPENAI_API_KEY を設定します +python3 startup.py "Write a cli snake game" + +# ステップ 4 [オプション]: 実行中に PRD ファイルなどのアーティファクトを保存する場合は、ステップ 3 の前にこのステップを実行できます。デフォルトでは、フレームワークには互換性があり、この手順を実行しなくてもプロセス全体を完了できます。 +# NPM がシステムにインストールされていることを確認してください。次に mermaid-js をインストールします。(お使いのコンピューターに npm がない場合は、Node.js 公式サイトで Node.js https://nodejs.org/ をインストールしてください。) +npm --version +sudo npm install -g @mermaid-js/mermaid-cli ``` **注:** @@ -159,6 +163,7 @@ # ステップ 3: リポジトリをローカルマシンにクローンし、 注: この方法は pdf エクスポートに対応していません。 ### Docker によるインストール +> Windowsでは、"/opt/metagpt"をDockerが作成する権限を持つディレクトリに置き換える必要があります。例えば、"D:\Users\x\metagpt"などです。 ```bash # ステップ 1: metagpt 公式イメージをダウンロードし、config.yaml を準備する @@ -270,12 +275,12 @@ ### 使用方法 ### コードウォークスルー ```python -from metagpt.software_company import SoftwareCompany +from metagpt.team import Team from metagpt.roles import ProjectManager, ProductManager, Architect, Engineer async def startup(idea: str, investment: float = 3.0, n_round: int = 5): """スタートアップを実行する。ボスになる。""" - company = SoftwareCompany() + company = Team() company.hire([ProductManager(), Architect(), ProjectManager(), Engineer()]) company.invest(investment) company.start_project(idea) @@ -295,12 +300,12 @@ ## クイックスタート ## 引用 -現時点では、[Arxiv 論文](https://arxiv.org/abs/2308.00352)を引用してください: +現時点では、[arXiv 論文](https://arxiv.org/abs/2308.00352)を引用してください: ```bibtex @misc{hong2023metagpt, - title={MetaGPT: Meta Programming for Multi-Agent Collaborative Framework}, - author={Sirui Hong and Xiawu Zheng and Jonathan Chen and Yuheng Cheng and Jinlin Wang and Ceyao Zhang and Zili Wang and Steven Ka Shing Yau and Zijuan Lin and Liyang Zhou and Chenyu Ran and Lingfeng Xiao and Chenglin Wu}, + title={MetaGPT: Meta Programming for A Multi-Agent Collaborative Framework}, + author={Sirui Hong and Mingchen Zhuge and Jonathan Chen and Xiawu Zheng and Yuheng Cheng and Ceyao Zhang and Jinlin Wang and Zili Wang and Steven Ka Shing Yau and Zijuan Lin and Liyang Zhou and Chenyu Ran and Lingfeng Xiao and Chenglin Wu and Jürgen Schmidhuber}, year={2023}, eprint={2308.00352}, archivePrefix={arXiv}, diff --git a/examples/build_customized_agent.py b/examples/build_customized_agent.py index 87d7a9c76..be34e5e5e 100644 --- a/examples/build_customized_agent.py +++ b/examples/build_customized_agent.py @@ -9,6 +9,7 @@ import asyncio import fire +from metagpt.llm import LLM from metagpt.actions import Action from metagpt.roles import Role from metagpt.schema import Message @@ -19,19 +20,10 @@ class SimpleWriteCode(Action): PROMPT_TEMPLATE = """ Write a python function that can {instruction} and provide two runnnable test cases. Return ```python your_code_here ``` with NO other texts, - example: - ```python - # function - def add(a, b): - return a + b - # test cases - print(add(1, 2)) - print(add(3, 4)) - ``` your code: """ - def __init__(self, name="SimpleWriteCode", context=None, llm=None): + def __init__(self, name: str = "SimpleWriteCode", context=None, llm: LLM = None): super().__init__(name, context, llm) async def run(self, instruction: str): @@ -51,8 +43,9 @@ class SimpleWriteCode(Action): code_text = match.group(1) if match else rsp return code_text + class SimpleRunCode(Action): - def __init__(self, name="SimpleRunCode", context=None, llm=None): + def __init__(self, name: str = "SimpleRunCode", context=None, llm: LLM = None): super().__init__(name, context, llm) async def run(self, code_text: str): @@ -61,6 +54,7 @@ class SimpleRunCode(Action): logger.info(f"{code_result=}") return code_result + class SimpleCoder(Role): def __init__( self, @@ -73,16 +67,16 @@ class SimpleCoder(Role): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") - todo = self._rc.todo + todo = self._rc.todo # todo will be SimpleWriteCode() - msg = self._rc.memory.get()[-1] # retrieve the latest memory - instruction = msg.content + msg = self.get_memories(k=1)[0] # find the most recent messages - code_text = await SimpleWriteCode().run(instruction) - msg = Message(content=code_text, role=self.profile, cause_by=todo) + code_text = await todo.run(msg.content) + msg = Message(content=code_text, role=self.profile, cause_by=type(todo)) return msg + class RunnableCoder(Role): def __init__( self, @@ -92,43 +86,23 @@ class RunnableCoder(Role): ): super().__init__(name, profile, **kwargs) self._init_actions([SimpleWriteCode, SimpleRunCode]) - - async def _think(self) -> None: - if self._rc.todo is None: - self._set_state(0) - return - - if self._rc.state + 1 < len(self._states): - self._set_state(self._rc.state + 1) - else: - self._rc.todo = None + self._set_react_mode(react_mode="by_order") async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") + # By choosing the Action by order under the hood + # todo will be first SimpleWriteCode() then SimpleRunCode() todo = self._rc.todo - msg = self._rc.memory.get()[-1] - if isinstance(todo, SimpleWriteCode): - instruction = msg.content - result = await SimpleWriteCode().run(instruction) + msg = self.get_memories(k=1)[0] # find the most k recent messages + result = await todo.run(msg.content) - elif isinstance(todo, SimpleRunCode): - code_text = msg.content - result = await SimpleRunCode().run(code_text) - - msg = Message(content=result, role=self.profile, cause_by=todo) + msg = Message(content=result, role=self.profile, cause_by=type(todo)) self._rc.memory.add(msg) return msg - async def _react(self) -> Message: - while True: - await self._think() - if self._rc.todo is None: - break - await self._act() - return Message(content="All job done", role=self.profile) -def main(msg="write a function that calculates the sum of a list"): +def main(msg="write a function that calculates the product of a list and run it"): # role = SimpleCoder() role = RunnableCoder() logger.info(msg) diff --git a/examples/debate.py b/examples/debate.py index 05db28070..a37e60848 100644 --- a/examples/debate.py +++ b/examples/debate.py @@ -7,14 +7,14 @@ import asyncio import platform import fire -from metagpt.software_company import SoftwareCompany +from metagpt.team import Team from metagpt.actions import Action, BossRequirement from metagpt.roles import Role from metagpt.schema import Message from metagpt.logs import logger -class ShoutOut(Action): - """Action: Shout out loudly in a debate (quarrel)""" +class SpeakAloud(Action): + """Action: Speak out aloud in a debate (quarrel)""" PROMPT_TEMPLATE = """ ## BACKGROUND @@ -27,7 +27,7 @@ class ShoutOut(Action): craft a strong and emotional response in 80 words, in {name}'s rhetoric and viewpoints, your will argue: """ - def __init__(self, name="ShoutOut", context=None, llm=None): + def __init__(self, name="SpeakAloud", context=None, llm=None): super().__init__(name, context, llm) async def run(self, context: str, name: str, opponent_name: str): @@ -39,96 +39,57 @@ class ShoutOut(Action): return rsp -class Trump(Role): +class Debator(Role): def __init__( self, - name: str = "Trump", - profile: str = "Republican", + name: str, + profile: str, + opponent_name: str, **kwargs, ): super().__init__(name, profile, **kwargs) - self._init_actions([ShoutOut]) - self._watch([ShoutOut]) - self.name = "Trump" - self.opponent_name = "Biden" + self._init_actions([SpeakAloud]) + self._watch([BossRequirement, SpeakAloud]) + self.name = name + self.opponent_name = opponent_name async def _observe(self) -> int: await super()._observe() # accept messages sent (from opponent) to self, disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.send_to == self.name] + self._rc.news = [msg for msg in self._rc.news if msg.send_to == self.name] return len(self._rc.news) async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") + todo = self._rc.todo # An instance of SpeakAloud - msg_history = self._rc.memory.get_by_actions([ShoutOut]) - context = [] - for m in msg_history: - context.append(str(m)) - context = "\n".join(context) + memories = self.get_memories() + context = "\n".join(f"{msg.sent_from}: {msg.content}" for msg in memories) + # print(context) - rsp = await ShoutOut().run(context=context, name=self.name, opponent_name=self.opponent_name) + rsp = await todo.run(context=context, name=self.name, opponent_name=self.opponent_name) msg = Message( content=rsp, role=self.profile, - cause_by=ShoutOut, + cause_by=type(todo), sent_from=self.name, send_to=self.opponent_name, ) - return msg - -class Biden(Role): - def __init__( - self, - name: str = "Biden", - profile: str = "Democrat", - **kwargs, - ): - super().__init__(name, profile, **kwargs) - self._init_actions([ShoutOut]) - self._watch([BossRequirement, ShoutOut]) - self.name = "Biden" - self.opponent_name = "Trump" - - async def _observe(self) -> int: - await super()._observe() - # accept the very first human instruction (the debate topic) or messages sent (from opponent) to self, - # disregard own messages from the last round - self._rc.news = [msg for msg in self._rc.news if msg.cause_by == BossRequirement or msg.send_to == self.name] - return len(self._rc.news) - - async def _act(self) -> Message: - logger.info(f"{self._setting}: ready to {self._rc.todo}") - - msg_history = self._rc.memory.get_by_actions([BossRequirement, ShoutOut]) - context = [] - for m in msg_history: - context.append(str(m)) - context = "\n".join(context) - - rsp = await ShoutOut().run(context=context, name=self.name, opponent_name=self.opponent_name) - - msg = Message( - content=rsp, - role=self.profile, - cause_by=ShoutOut, - sent_from=self.name, - send_to=self.opponent_name, - ) + self._rc.memory.add(msg) return msg -async def startup(idea: str, investment: float = 3.0, n_round: int = 5, - code_review: bool = False, run_tests: bool = False): - """We reuse the startup paradigm for roles to interact with each other. - Now we run a startup of presidents and watch they quarrel. :) """ - company = SoftwareCompany() - company.hire([Biden(), Trump()]) - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) +async def debate(idea: str, investment: float = 3.0, n_round: int = 5): + """Run a team of presidents and watch they quarrel. :) """ + Biden = Debator(name="Biden", profile="Democrat", opponent_name="Trump") + Trump = Debator(name="Trump", profile="Republican", opponent_name="Biden") + team = Team() + team.hire([Biden, Trump]) + team.invest(investment) + team.start_project(idea, send_to="Biden") # send debate topic to Biden and let him speak first + await team.run(n_round=n_round) def main(idea: str, investment: float = 3.0, n_round: int = 10): @@ -141,7 +102,7 @@ def main(idea: str, investment: float = 3.0, n_round: int = 10): """ if platform.system() == "Windows": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) - asyncio.run(startup(idea, investment, n_round)) + asyncio.run(debate(idea, investment, n_round)) if __name__ == '__main__': diff --git a/metagpt/config.py b/metagpt/config.py index 27455d38d..3f9e742bd 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -45,10 +45,11 @@ class Config(metaclass=Singleton): self.global_proxy = self._get("GLOBAL_PROXY") self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") - if (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) and ( - not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key - ): - raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY first") + self.zhipuai_api_key = self._get("ZHIPUAI_API_KEY") + if (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) and \ + (not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key) and \ + (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key): + raise NotConfiguredException("Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first") self.openai_api_base = self._get("OPENAI_API_BASE") openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy if openai_proxy: diff --git a/metagpt/const.py b/metagpt/const.py index 7f3f87dfa..407ce803a 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -6,7 +6,7 @@ @File : const.py """ from pathlib import Path - +from loguru import logger def get_project_root(): """Search upwards to find the project root directory.""" @@ -17,10 +17,15 @@ def get_project_root(): or (current_path / ".project_root").exists() or (current_path / ".gitignore").exists() ): + # use metagpt with git clone will land here + logger.info(f"PROJECT_ROOT set to {str(current_path)}") return current_path parent_path = current_path.parent if parent_path == current_path: - raise Exception("Project root not found.") + # use metagpt with pip install will land here + cwd = Path.cwd() + logger.info(f"PROJECT_ROOT set to current working directory: {str(cwd)}") + return cwd current_path = parent_path diff --git a/metagpt/llm.py b/metagpt/llm.py index e6f815950..4edcd7a83 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -6,14 +6,27 @@ @File : llm.py """ +from metagpt.logs import logger +from metagpt.config import CONFIG from metagpt.provider.anthropic_api import Claude2 as Claude -from metagpt.provider.openai_api import OpenAIGPTAPI as LLM +from metagpt.provider.openai_api import OpenAIGPTAPI +from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI +from metagpt.provider.spark_api import SparkAPI +from metagpt.provider.human_provider import HumanProvider -DEFAULT_LLM = LLM() -CLAUDE_LLM = Claude() -async def ai_func(prompt): - """使用LLM进行QA - QA with LLMs - """ - return await DEFAULT_LLM.aask(prompt) +def LLM() -> "BaseGPTAPI": + """ initialize different LLM instance according to the key field existence""" + # TODO a little trick, can use registry to initialize LLM instance further + if CONFIG.openai_api_key: + llm = OpenAIGPTAPI() + elif CONFIG.claude_api_key: + llm = Claude() + elif CONFIG.spark_api_key: + llm = SparkAPI() + elif CONFIG.zhipuai_api_key: + llm = ZhiPuAIGPTAPI() + else: + raise RuntimeError("You should config a LLM configuration first") + + return llm diff --git a/metagpt/provider/base_chatbot.py b/metagpt/provider/base_chatbot.py index abdf423f4..72e6c94f9 100644 --- a/metagpt/provider/base_chatbot.py +++ b/metagpt/provider/base_chatbot.py @@ -13,6 +13,7 @@ from dataclasses import dataclass class BaseChatbot(ABC): """Abstract GPT class""" mode: str = "API" + use_system_prompt: bool = True @abstractmethod def ask(self, msg: str) -> str: diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index de61167b9..b6b034329 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -5,6 +5,7 @@ @Author : alexanderwu @File : base_gpt_api.py """ +import json from abc import abstractmethod from typing import Optional @@ -14,7 +15,8 @@ from metagpt.provider.base_chatbot import BaseChatbot class BaseGPTAPI(BaseChatbot): """GPT API abstract class, requiring all inheritors to provide a series of standard capabilities""" - system_prompt = 'You are a helpful assistant.' + + system_prompt = "You are a helpful assistant." def _user_msg(self, msg: str) -> dict[str, str]: return {"role": "user", "content": msg} @@ -32,15 +34,17 @@ class BaseGPTAPI(BaseChatbot): return self._system_msg(self.system_prompt) def ask(self, msg: str) -> str: - message = [self._default_system_msg(), self._user_msg(msg)] + message = [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt else [self._user_msg(msg)] rsp = self.completion(message) return self.get_choice_text(rsp) async def aask(self, msg: str, system_msgs: Optional[list[str]] = None) -> str: if system_msgs: - message = self._system_msgs(system_msgs) + [self._user_msg(msg)] + message = self._system_msgs(system_msgs) + [self._user_msg(msg)] if self.use_system_prompt \ + else [self._user_msg(msg)] else: - message = [self._default_system_msg(), self._user_msg(msg)] + message = [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt \ + else [self._user_msg(msg)] rsp = await self.acompletion_text(message, stream=True) logger.debug(message) # logger.debug(rsp) @@ -108,11 +112,50 @@ class BaseGPTAPI(BaseChatbot): """Required to provide the first text of choice""" return rsp.get("choices")[0]["message"]["content"] + def get_choice_function(self, rsp: dict) -> dict: + """Required to provide the first function of choice + :param dict rsp: OpenAI chat.comletion respond JSON, Note "message" must include "tool_calls", + and "tool_calls" must include "function", for example: + {... + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_Y5r6Ddr2Qc2ZrqgfwzPX5l72", + "type": "function", + "function": { + "name": "execute", + "arguments": "{\n \"language\": \"python\",\n \"code\": \"print('Hello, World!')\"\n}" + } + } + ] + }, + "finish_reason": "stop" + } + ], + ...} + :return dict: return first function of choice, for exmaple, + {'name': 'execute', 'arguments': '{\n "language": "python",\n "code": "print(\'Hello, World!\')"\n}'} + """ + return rsp.get("choices")[0]["message"]["tool_calls"][0]["function"].to_dict() + + def get_choice_function_arguments(self, rsp: dict) -> dict: + """Required to provide the first function arguments of choice. + + :param dict rsp: same as in self.get_choice_function(rsp) + :return dict: return the first function arguments of choice, for example, + {'language': 'python', 'code': "print('Hello, World!')"} + """ + return json.loads(self.get_choice_function(rsp)["arguments"]) + def messages_to_prompt(self, messages: list[dict]): """[{"role": "user", "content": msg}] to user: etc.""" - return '\n'.join([f"{i['role']}: {i['content']}" for i in messages]) + return "\n".join([f"{i['role']}: {i['content']}" for i in messages]) def messages_to_dict(self, messages): """objects to [{"role": "user", "content": msg}] etc.""" return [i.to_dict() for i in messages] - \ No newline at end of file diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 6ebed2c16..34e5693f8 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -21,6 +21,8 @@ from tenacity import ( from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMA, GENERAL_TOOL_CHOICE +from metagpt.schema import Message from metagpt.utils.singleton import Singleton from metagpt.utils.token_counter import ( TOKEN_COSTS, @@ -110,7 +112,6 @@ class CostManager(metaclass=Singleton): """ return self.total_completion_tokens - def get_total_cost(self): """ Get the total cost of API calls. @@ -120,7 +121,6 @@ class CostManager(metaclass=Singleton): """ return self.total_cost - def get_costs(self) -> Costs: """Get all costs""" return Costs(self.total_prompt_tokens, self.total_completion_tokens, self.total_cost, self.total_budget) @@ -181,7 +181,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): self._update_costs(usage) return full_reply_content - def _cons_kwargs(self, messages: list[dict]) -> dict: + def _cons_kwargs(self, messages: list[dict], **configs) -> dict: kwargs = { "messages": messages, "max_tokens": self.get_max_tokens(messages), @@ -190,6 +190,9 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): "temperature": 0.3, "timeout": 3, } + if configs: + kwargs.update(configs) + if CONFIG.openai_api_type == "azure": if CONFIG.deployment_name and CONFIG.deployment_id: raise ValueError("You can only use one of the `deployment_id` or `deployment_name` model") @@ -239,6 +242,81 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): rsp = await self._achat_completion(messages) return self.get_choice_text(rsp) + def _func_configs(self, messages: list[dict], **kwargs) -> dict: + """ + Note: Keep kwargs consistent with the parameters in the https://platform.openai.com/docs/api-reference/chat/create + """ + if "tools" not in kwargs: + configs = { + "tools": [{"type": "function", "function": GENERAL_FUNCTION_SCHEMA}], + "tool_choice": GENERAL_TOOL_CHOICE, + } + kwargs.update(configs) + + return self._cons_kwargs(messages, **kwargs) + + def _chat_completion_function(self, messages: list[dict], **kwargs) -> dict: + rsp = self.llm.ChatCompletion.create(**self._func_configs(messages, **kwargs)) + self._update_costs(rsp.get("usage")) + return rsp + + async def _achat_completion_function(self, messages: list[dict], **chat_configs) -> dict: + rsp = await self.llm.ChatCompletion.acreate(**self._func_configs(messages, **chat_configs)) + self._update_costs(rsp.get("usage")) + return rsp + + def _process_message(self, messages: Union[str, Message, list[dict], list[Message], list[str]]) -> list[dict]: + """convert messages to list[dict].""" + if isinstance(messages, list): + messages = [Message(msg) if isinstance(msg, str) else msg for msg in messages] + return [msg if isinstance(msg, dict) else msg.to_dict() for msg in messages] + + if isinstance(messages, Message): + messages = [messages.to_dict()] + elif isinstance(messages, str): + messages = [{"role": "user", "content": messages}] + else: + raise ValueError( + f"Only support messages type are: str, Message, list[dict], but got {type(messages).__name__}!" + ) + return messages + + def ask_code(self, messages: Union[str, Message, list[dict]], **kwargs) -> dict: + """Use function of tools to ask a code. + + Note: Keep kwargs consistent with the parameters in the https://platform.openai.com/docs/api-reference/chat/create + + Examples: + + >>> llm = OpenAIGPTAPI() + >>> llm.ask_code("Write a python hello world code.") + {'language': 'python', 'code': "print('Hello, World!')"} + >>> msg = [{'role': 'user', 'content': "Write a python hello world code."}] + >>> llm.ask_code(msg) + {'language': 'python', 'code': "print('Hello, World!')"} + """ + messages = self._process_message(messages) + rsp = self._chat_completion_function(messages, **kwargs) + return self.get_choice_function_arguments(rsp) + + async def aask_code(self, messages: Union[str, Message, list[dict]], **kwargs) -> dict: + """Use function of tools to ask a code. + + Note: Keep kwargs consistent with the parameters in the https://platform.openai.com/docs/api-reference/chat/create + + Examples: + + >>> llm = OpenAIGPTAPI() + >>> rsp = await llm.ask_code("Write a python hello world code.") + >>> rsp + {'language': 'python', 'code': "print('Hello, World!')"} + >>> msg = [{'role': 'user', 'content': "Write a python hello world code."}] + >>> rsp = await llm.aask_code(msg) # -> {'language': 'python', 'code': "print('Hello, World!')"} + """ + messages = self._process_message(messages) + rsp = await self._achat_completion_function(messages, **kwargs) + return self.get_choice_function_arguments(rsp) + def _calc_usage(self, messages: list[dict], rsp: str) -> dict: usage = {} if CONFIG.calc_usage: diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 6d65575a8..1f6685b38 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -207,6 +207,7 @@ class Engineer(Role): async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" + logger.info(f"{self._setting}: ready to WriteCode") if self.use_code_review: return await self._act_sp_precision() return await self._act_sp() diff --git a/metagpt/roles/invoice_ocr_assistant.py b/metagpt/roles/invoice_ocr_assistant.py index c307b20c0..15f831c97 100644 --- a/metagpt/roles/invoice_ocr_assistant.py +++ b/metagpt/roles/invoice_ocr_assistant.py @@ -42,17 +42,7 @@ class InvoiceOCRAssistant(Role): self.filename = "" self.origin_query = "" self.orc_data = None - - async def _think(self) -> None: - """Determine the next action to be taken by the role.""" - if self._rc.todo is None: - self._set_state(0) - return - - if self._rc.state + 1 < len(self._states): - self._set_state(self._rc.state + 1) - else: - self._rc.todo = None + self._set_react_mode(react_mode="by_order") async def _act(self) -> Message: """Perform an action as determined by the role. @@ -94,17 +84,3 @@ class InvoiceOCRAssistant(Role): msg = Message(content=content, instruct_content=resp) self._rc.memory.add(msg) return msg - - async def _react(self) -> Message: - """Execute the invoice ocr assistant's think and actions. - - Returns: - A message containing the final result of the assistant's actions. - """ - while True: - await self._think() - if self._rc.todo is None: - break - msg = await self._act() - return msg - diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index acb46c718..c5512121a 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -31,20 +31,11 @@ class Researcher(Role): ): super().__init__(name, profile, goal, constraints, **kwargs) self._init_actions([CollectLinks(name), WebBrowseAndSummarize(name), ConductResearch(name)]) + self._set_react_mode(react_mode="by_order") self.language = language if language not in ("en-us", "zh-cn"): logger.warning(f"The language `{language}` has not been tested, it may not work.") - async def _think(self) -> None: - if self._rc.todo is None: - self._set_state(0) - return - - if self._rc.state + 1 < len(self._states): - self._set_state(self._rc.state + 1) - else: - self._rc.todo = None - async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") todo = self._rc.todo @@ -73,12 +64,8 @@ class Researcher(Role): self._rc.memory.add(ret) return ret - async def _react(self) -> Message: - while True: - await self._think() - if self._rc.todo is None: - break - msg = await self._act() + async def react(self) -> Message: + msg = await super().react() report = msg.instruct_content self.write_report(report.topic, report.content) return msg diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 44bb3e976..b96c361c0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -7,14 +7,15 @@ """ from __future__ import annotations -from typing import Iterable, Type +from typing import Iterable, Type, Union +from enum import Enum from pydantic import BaseModel, Field # from metagpt.environment import Environment from metagpt.config import CONFIG from metagpt.actions import Action, ActionOutput -from metagpt.llm import LLM +from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message @@ -27,12 +28,14 @@ Please note that only the text between the first and second "===" is information {history} === -You can now choose one of the following stages to decide the stage you need to go in the next step: +Your previous stage: {previous_state} + +Now choose one of the following stages you need to go to in the next step: {states} Just answer a number between 0-{n_states}, choose the most suitable stage according to the understanding of the conversation. Please note that the answer only needs a number, no need to add any other text. -If there is no conversation record, choose 0. +If you think you have completed your goal and don't need to go to any of the stages, return -1. Do not answer anything else, and do not add any other information in your answer. """ @@ -46,6 +49,14 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi {name}: {result} """ +class RoleReactMode(str, Enum): + REACT = "react" + BY_ORDER = "by_order" + PLAN_AND_ACT = "plan_and_act" + + @classmethod + def values(cls): + return [item.value for item in cls] class RoleSetting(BaseModel): """Role Settings""" @@ -54,6 +65,7 @@ class RoleSetting(BaseModel): goal: str constraints: str desc: str + is_human: bool def __str__(self): return f"{self.name}({self.profile})" @@ -67,10 +79,12 @@ class RoleContext(BaseModel): env: 'Environment' = Field(default=None) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) - state: int = Field(default=0) + state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[]) + react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes + max_react_loop: int = 1 class Config: arbitrary_types_allowed = True @@ -93,9 +107,10 @@ class RoleContext(BaseModel): class Role: """Role/Agent""" - def __init__(self, name="", profile="", goal="", constraints="", desc=""): - self._llm = LLM() - self._setting = RoleSetting(name=name, profile=profile, goal=goal, constraints=constraints, desc=desc) + def __init__(self, name="", profile="", goal="", constraints="", desc="", is_human=False): + self._llm = LLM() if not is_human else HumanProvider() + self._setting = RoleSetting(name=name, profile=profile, goal=goal, + constraints=constraints, desc=desc, is_human=is_human) self._states = [] self._actions = [] self._role_id = str(self._setting) @@ -109,24 +124,48 @@ class Role: self._reset() for idx, action in enumerate(actions): if not isinstance(action, Action): - i = action("") + i = action("", llm=self._llm) else: + if self._setting.is_human and not isinstance(action.llm, HumanProvider): + logger.warning(f"is_human attribute does not take effect," + f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances") i = action i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) self._states.append(f"{idx}. {action}") + def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): + """Set strategy of the Role reacting to observed Message. Variation lies in how + this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. + + Args: + react_mode (str): Mode for choosing action during the _think stage, can be one of: + "react": standard think-act loop in the ReAct paper, alternating thinking and acting to solve the task, i.e. _think -> _act -> _think -> _act -> ... + Use llm to select actions in _think dynamically; + "by_order": switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...; + "plan_and_act": first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... + Use llm to come up with the plan dynamically. + Defaults to "react". + max_react_loop (int): Maximum react cycles to execute, used to prevent the agent from reacting forever. + Take effect only when react_mode is react, in which we use llm to choose actions, including termination. + Defaults to 1, i.e. _think -> _act (-> return result and end) + """ + assert react_mode in RoleReactMode.values(), f"react_mode must be one of {RoleReactMode.values()}" + self._rc.react_mode = react_mode + if react_mode == RoleReactMode.REACT: + self._rc.max_react_loop = max_react_loop + def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" self._rc.watch.update(actions) # check RoleContext after adding watch actions self._rc.check(self._role_id) - def _set_state(self, state): + def _set_state(self, state: int): """Update the current state.""" self._rc.state = state logger.debug(self._actions) - self._rc.todo = self._actions[self._rc.state] + self._rc.todo = self._actions[self._rc.state] if state >= 0 else None def set_env(self, env: 'Environment'): """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" @@ -151,13 +190,19 @@ class Role: return prompt = self._get_prefix() prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), - n_states=len(self._states) - 1) + n_states=len(self._states) - 1, previous_state=self._rc.state) + # print(prompt) next_state = await self._llm.aask(prompt) logger.debug(f"{prompt=}") - if not next_state.isdigit() or int(next_state) not in range(len(self._states)): - logger.warning(f'Invalid answer of state, {next_state=}') - next_state = "0" - self._set_state(int(next_state)) + if (not next_state.isdigit() and next_state != "-1") \ + or int(next_state) not in range(-1, len(self._states)): + logger.warning(f'Invalid answer of state, {next_state=}, will be set to -1') + next_state = -1 + else: + next_state = int(next_state) + if next_state == -1: + logger.info(f"End actions with {next_state=}") + self._set_state(next_state) async def _act(self) -> Message: # prompt = self.get_prefix() @@ -203,10 +248,45 @@ class Role: self._rc.env.publish_message(msg) async def _react(self) -> Message: - """Think first, then act""" - await self._think() - logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") - return await self._act() + """Think first, then act, until the Role _think it is time to stop and requires no more todo. + This is the standard think-act loop in the ReAct paper, which alternates thinking and acting in task solving, i.e. _think -> _act -> _think -> _act -> ... + Use llm to select actions in _think dynamically + """ + actions_taken = 0 + rsp = Message("No actions taken yet") # will be overwritten after Role _act + while actions_taken < self._rc.max_react_loop: + # think + await self._think() + if self._rc.todo is None: + break + # act + logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") + rsp = await self._act() + actions_taken += 1 + return rsp # return output from the last action + + async def _act_by_order(self) -> Message: + """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" + for i in range(len(self._states)): + self._set_state(i) + rsp = await self._act() + return rsp # return output from the last action + + async def _plan_and_act(self) -> Message: + """first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... Use llm to come up with the plan dynamically.""" + # TODO: to be implemented + return Message("") + + async def react(self) -> Message: + """Entry to one of three strategies by which Role reacts to the observed Message""" + if self._rc.react_mode == RoleReactMode.REACT: + rsp = await self._react() + elif self._rc.react_mode == RoleReactMode.BY_ORDER: + rsp = await self._act_by_order() + elif self._rc.react_mode == RoleReactMode.PLAN_AND_ACT: + rsp = await self._plan_and_act() + self._set_state(state=-1) # current reaction is complete, reset state to -1 and todo back to None + return rsp def recv(self, message: Message) -> None: """add message to history.""" @@ -223,6 +303,10 @@ class Role: return await self._react() + def get_memories(self, k=0) -> list[Message]: + """A wrapper to return the most recent k memories of this role, return all when k=0""" + return self._rc.memory.get(k=k) + async def run(self, message=None): """Observe, and think and act based on the results of the observation""" if message: @@ -237,7 +321,7 @@ class Role: logger.debug(f"{self._setting}: no news. waiting.") return - rsp = await self._react() + rsp = await self.react() # Publish the reply to the environment, waiting for the next subscriber to process self._publish_message(rsp) return rsp diff --git a/metagpt/software_company.py b/metagpt/software_company.py index b2bd18c58..d44a0068a 100644 --- a/metagpt/software_company.py +++ b/metagpt/software_company.py @@ -5,58 +5,9 @@ @Author : alexanderwu @File : software_company.py """ -from pydantic import BaseModel, Field +from metagpt.team import Team as SoftwareCompany -from metagpt.actions import BossRequirement -from metagpt.config import CONFIG -from metagpt.environment import Environment -from metagpt.logs import logger -from metagpt.roles import Role -from metagpt.schema import Message -from metagpt.utils.common import NoMoneyException - - -class SoftwareCompany(BaseModel): - """ - Software Company: Possesses a team, SOP (Standard Operating Procedures), and a platform for instant messaging, - dedicated to writing executable code. - """ - environment: Environment = Field(default_factory=Environment) - investment: float = Field(default=10.0) - idea: str = Field(default="") - - class Config: - arbitrary_types_allowed = True - - def hire(self, roles: list[Role]): - """Hire roles to cooperate""" - self.environment.add_roles(roles) - - def invest(self, investment: float): - """Invest company. raise NoMoneyException when exceed max_budget.""" - self.investment = investment - CONFIG.max_budget = investment - logger.info(f'Investment: ${investment}.') - - def _check_balance(self): - if CONFIG.total_cost > CONFIG.max_budget: - raise NoMoneyException(CONFIG.total_cost, f'Insufficient funds: {CONFIG.max_budget}') - - def start_project(self, idea): - """Start a project from publishing boss requirement.""" - self.idea = idea - self.environment.publish_message(Message(role="BOSS", content=idea, cause_by=BossRequirement)) - - def _save(self): - logger.info(self.json()) - - async def run(self, n_round=3): - """Run company until target round or no money""" - while n_round > 0: - # self._save() - n_round -= 1 - logger.debug(f"{n_round=}") - self._check_balance() - await self.environment.run() - return self.environment.history - \ No newline at end of file +import warnings +warnings.warn("metagpt.software_company is deprecated and will be removed in the future" + "Please use metagpt.team instead. SoftwareCompany class is now named as Team.", + DeprecationWarning, 2) diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 5e5b275b0..204c22c67 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -34,7 +34,10 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, engine = CONFIG.mermaid_engine.lower() if engine == "nodejs": if check_cmd_exists(CONFIG.mmdc) != 0: - logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc") + logger.warning( + "RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc," + "or consider changing MERMAID_ENGINE to `playwright`, `pyppeteer`, or `ink`." + ) return -1 for suffix in ["pdf", "svg", "png"]: diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index a5a65803a..1af96f272 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -22,6 +22,7 @@ TOKEN_COSTS = { "gpt-4-32k-0314": {"prompt": 0.06, "completion": 0.12}, "gpt-4-0613": {"prompt": 0.06, "completion": 0.12}, "text-embedding-ada-002": {"prompt": 0.0004, "completion": 0.0}, + "chatglm_turbo": {"prompt": 0.0, "completion": 0.00069} # 32k version, prompt + completion tokens=0.005¥/k-tokens } @@ -37,6 +38,7 @@ TOKEN_MAX = { "gpt-4-32k-0314": 32768, "gpt-4-0613": 8192, "text-embedding-ada-002": 8192, + "chatglm_turbo": 32768 } @@ -68,7 +70,9 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): return count_message_tokens(messages, model="gpt-4-0613") else: raise NotImplementedError( - f"""num_tokens_from_messages() is not implemented for model {model}. See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""" + f"num_tokens_from_messages() is not implemented for model {model}. " + f"See https://github.com/openai/openai-python/blob/main/chatml.md " + f"for information on how messages are converted to tokens." ) num_tokens = 0 for message in messages: diff --git a/requirements.txt b/requirements.txt index 24a2d94c3..f0169d7fa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ langchain==0.0.231 loguru==0.6.0 meilisearch==0.21.0 numpy==1.24.3 -openai +openai>=0.28.0 openpyxl beautifulsoup4==4.12.2 pandas==2.0.3 @@ -44,4 +44,4 @@ ta==0.10.2 semantic-kernel==0.3.13.dev0 wrapt==1.15.0 websocket-client==0.58.0 - +zhipuai==1.0.7 diff --git a/setup.py b/setup.py index f9ae768e6..239156ae3 100644 --- a/setup.py +++ b/setup.py @@ -30,16 +30,16 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.1", + version="0.3.0", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", - url="https://gitlab.deepwisdomai.com/pub/metagpt", + url="https://github.com/geekan/MetaGPT", author="Alexander Wu", author_email="alexanderwu@fuzhi.ai", license="Apache 2.0", keywords="metagpt multi-role multi-agent programming gpt llm", - packages=find_packages(exclude=["contrib", "docs", "examples"]), + packages=find_packages(exclude=["contrib", "docs", "examples", "tests*"]), python_requires=">=3.9", install_requires=requirements, extras_require={ diff --git a/startup.py b/startup.py index e2a903c9b..e9fbf94d3 100644 --- a/startup.py +++ b/startup.py @@ -11,7 +11,7 @@ from metagpt.roles import ( ProjectManager, QaEngineer, ) -from metagpt.software_company import SoftwareCompany +from metagpt.team import Team async def startup( @@ -23,7 +23,7 @@ async def startup( implement: bool = True, ): """Run a startup. Be a boss.""" - company = SoftwareCompany() + company = Team() company.hire( [ ProductManager(), diff --git a/tests/metagpt/roles/test_ui.py b/tests/metagpt/roles/test_ui.py index 285bff323..d58d31bd9 100644 --- a/tests/metagpt/roles/test_ui.py +++ b/tests/metagpt/roles/test_ui.py @@ -2,7 +2,7 @@ # @Date : 2023/7/22 02:40 # @Author : stellahong (stellahong@fuzhi.ai) # -from metagpt.software_company import SoftwareCompany +from metagpt.team import Team from metagpt.roles import ProductManager from tests.metagpt.roles.ui_role import UI @@ -15,7 +15,7 @@ def test_add_ui(): async def test_ui_role(idea: str, investment: float = 3.0, n_round: int = 5): """Run a startup. Be a boss.""" - company = SoftwareCompany() + company = Team() company.hire([ProductManager(), UI()]) company.invest(investment) company.start_project(idea) diff --git a/tests/metagpt/test_software_company.py b/tests/metagpt/test_software_company.py index 00538442c..4fc651f52 100644 --- a/tests/metagpt/test_software_company.py +++ b/tests/metagpt/test_software_company.py @@ -8,12 +8,12 @@ import pytest from metagpt.logs import logger -from metagpt.software_company import SoftwareCompany +from metagpt.team import Team @pytest.mark.asyncio -async def test_software_company(): - company = SoftwareCompany() +async def test_team(): + company = Team() company.start_project("做一个基础搜索引擎,可以支持知识库") history = await company.run(n_round=5) logger.info(history) From 45e48c8093e4bae519f62adc3b51e3c74ada1976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 17:44:16 +0800 Subject: [PATCH 183/413] feat: merge geekan:main --- .pre-commit-config.yam_ => .pre-commit-config.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .pre-commit-config.yam_ => .pre-commit-config.yaml (100%) diff --git a/.pre-commit-config.yam_ b/.pre-commit-config.yaml similarity index 100% rename from .pre-commit-config.yam_ rename to .pre-commit-config.yaml From ef9a925281e0a06ab910d14dcd5bc48a9689cc94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 19:11:12 +0800 Subject: [PATCH 184/413] feat: + gitpython 3.1.40 --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 73a03d537..e72efc76c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -45,3 +45,4 @@ semantic-kernel==0.3.13.dev0 wrapt==1.15.0 websocket-client==0.58.0 aiofiles==23.2.1 +gitpython==3.1.40 \ No newline at end of file From 4c99107a333d6e9dc6bb52399de578364002ac4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 19:23:20 +0800 Subject: [PATCH 185/413] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/debug_error.py | 7 +++--- metagpt/utils/file_repository.py | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index e4a15d38d..dd1527154 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -16,6 +16,7 @@ from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger from metagpt.schema import RunCodeResult from metagpt.utils.common import CodeParser +from metagpt.utils.file_repository import FileRepository PROMPT_TEMPLATE = """ NOTICE @@ -50,7 +51,7 @@ class DebugError(Action): super().__init__(name, context, llm) async def run(self, *args, **kwargs) -> str: - output_doc = await CONFIG.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).get(self.context.output_filename) + output_doc = await FileRepository.get_file(filename=self.context.output_filename, relative_path=TEST_OUTPUTS_FILE_REPO) if not output_doc: return "" output_detail = RunCodeResult.loads(output_doc.content) @@ -60,10 +61,10 @@ class DebugError(Action): return "" logger.info(f"Debug and rewrite {self.context.code_filename}") - code_doc = await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).get(self.context.code_filename) + code_doc = await FileRepository.get_file(filename=self.context.code_filename, relative_path=CONFIG.src_workspace) if not code_doc: return "" - test_doc = await CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO).get(self.context.test_filename) + test_doc = await FileRepository.get_file(filename=self.context.test_filename, relative_path=TEST_CODES_FILE_REPO) if not test_doc: return "" prompt = PROMPT_TEMPLATE.format(code=code_doc.content, test_code=test_doc.content, logs=output_detail.stderr) diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 8de4bdf5b..3df53cca3 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -16,6 +16,7 @@ from typing import Dict, List, Set import aiofiles +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import Document from metagpt.utils.json_to_markdown import json_to_markdown @@ -186,3 +187,44 @@ class FileRepository: filename = Path(doc.filename).with_suffix(".md") await self.save(filename=str(filename), content=json_to_markdown(m)) logger.info(f"File Saved: {str(filename)}") + + @staticmethod + async def get_file(filename: Path | str, relative_path: Path | str = ".") -> Document | None: + """Retrieve a specific file from the file repository. + + :param filename: The name or path of the file to retrieve. + :type filename: Path or str + :param relative_path: The relative path within the file repository. + :type relative_path: Path or str, optional + :return: The document representing the file, or None if not found. + :rtype: Document or None + """ + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + return await file_repo.get(filename=filename) + + @staticmethod + async def get_all_files(relative_path: Path | str = ".") -> List[Document]: + """Retrieve all files from the file repository. + + :param relative_path: The relative path within the file repository. + :type relative_path: Path or str, optional + :return: A list of documents representing all files in the repository. + :rtype: List[Document] + """ + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + return await file_repo.get_all() + + @staticmethod + async def save_file(filename: Path | str, content, dependencies: List[str] = None, relative_path: Path | str = "."): + """Save a file to the file repository. + + :param filename: The name or path of the file to save. + :type filename: Path or str + :param content: The content of the file. + :param dependencies: A list of dependencies for the file. + :type dependencies: List[str], optional + :param relative_path: The relative path within the file repository. + :type relative_path: Path or str, optional + """ + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + return await file_repo.save(filename=filename, content=content, dependencies=dependencies) From 81e719faa21bd08d3f97545c30d48658348afc5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 19:29:29 +0800 Subject: [PATCH 186/413] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/prepare_documents.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index c9b60ff27..92d5730b2 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -24,8 +24,7 @@ class PrepareDocuments(Action): async def run(self, with_messages, **kwargs): if CONFIG.git_repo: - docs_repo = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) - doc = await docs_repo.get(REQUIREMENT_FILENAME) + doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) return ActionOutput(content=doc.json(exclue="content"), instruct_content=doc) # Create and initialize the workspace folder, initialize the Git environment. @@ -34,9 +33,8 @@ class PrepareDocuments(Action): CONFIG.git_repo.open(local_path=workdir, auto_init=True) # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. - docs_file_repository = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) doc = Document(root_path=DOCS_FILE_REPO, filename=REQUIREMENT_FILENAME, content=with_messages[0].content) - await docs_file_repository.save(REQUIREMENT_FILENAME, content=doc.content) + await FileRepository.save_file(filename=REQUIREMENT_FILENAME, content=doc.content, relative_path=DOCS_FILE_REPO) # Send a Message notification to the WritePRD action, instructing it to process requirements using # `docs/requirement.txt` and `docs/prds/`. From fa675ea3157a4a4a1b09209f52e4714d7a5e60d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 19:32:33 +0800 Subject: [PATCH 187/413] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/project_management.py | 9 ++++----- metagpt/const.py | 2 ++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 641d21533..d679a730c 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -15,7 +15,7 @@ from typing import List from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, TASK_PDF_FILE_REPO +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, TASK_PDF_FILE_REPO, PACKAGE_REQUIREMENTS_FILENAME from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.get_template import get_template @@ -263,16 +263,15 @@ class WriteTasks(Action): m = json.loads(doc.content) packages = set(m.get("Required Python third-party packages", set())) file_repo = CONFIG.git_repo.new_file_repository() - filename = "requirements.txt" - requirement_doc = await file_repo.get(filename) + requirement_doc = await file_repo.get(filename=PACKAGE_REQUIREMENTS_FILENAME) if not requirement_doc: - requirement_doc = Document(filename=filename, root_path=".", content="") + requirement_doc = Document(filename=PACKAGE_REQUIREMENTS_FILENAME, root_path=".", content="") lines = requirement_doc.content.splitlines() for pkg in lines: if pkg == "": continue packages.add(pkg) - await file_repo.save(filename, content="\n".join(packages)) + await file_repo.save(PACKAGE_REQUIREMENTS_FILENAME, content="\n".join(packages)) @staticmethod async def _save_pdf(task_doc): diff --git a/metagpt/const.py b/metagpt/const.py index a8c7356ca..ce06655f1 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -53,6 +53,8 @@ MESSAGE_ROUTE_TO_ALL = "" MESSAGE_ROUTE_TO_NONE = "" REQUIREMENT_FILENAME = "requirement.txt" +PACKAGE_REQUIREMENTS_FILENAME = "requirements.txt" + DOCS_FILE_REPO = "docs" PRDS_FILE_REPO = "docs/prds" SYSTEM_DESIGN_FILE_REPO = "docs/system_design" From 726eadf1cce7205fbbe960d30326c5eb118c09a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 19:54:41 +0800 Subject: [PATCH 188/413] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/design_api.py | 4 +-- metagpt/actions/prepare_documents.py | 2 +- metagpt/actions/project_management.py | 4 +-- metagpt/actions/write_code.py | 6 ++--- metagpt/actions/write_prd.py | 8 +++--- metagpt/utils/file_repository.py | 39 +++++++++++++++++++++++---- 6 files changed, 45 insertions(+), 18 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 021edfe72..f987c6042 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -24,6 +24,7 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.common import CodeParser +from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file @@ -304,8 +305,7 @@ class WriteDesign(Action): @staticmethod async def _save_pdf(design_doc): - file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_PDF_FILE_REPO) - await file_repo.save_pdf(doc=design_doc) + await FileRepository.save_as(doc=design_doc, with_suffix=".md", relative_path=SYSTEM_DESIGN_PDF_FILE_REPO) @staticmethod async def _save_mermaid_file(data: str, pathname: Path): diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 92d5730b2..30558c93f 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -29,7 +29,7 @@ class PrepareDocuments(Action): # Create and initialize the workspace folder, initialize the Git environment. CONFIG.git_repo = GitRepository() - workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_file_name() + workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_filename() CONFIG.git_repo.open(local_path=workdir, auto_init=True) # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index d679a730c..7205d11e7 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -18,6 +18,7 @@ from metagpt.config import CONFIG from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO, TASK_PDF_FILE_REPO, PACKAGE_REQUIREMENTS_FILENAME from metagpt.logs import logger from metagpt.schema import Document, Documents +from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template templates = { @@ -275,8 +276,7 @@ class WriteTasks(Action): @staticmethod async def _save_pdf(task_doc): - file_repo = CONFIG.git_repo.new_file_repository(TASK_PDF_FILE_REPO) - await file_repo.save_pdf(doc=task_doc) + await FileRepository.save_as(doc=task_doc, with_suffix=".md", relative_path=TASK_PDF_FILE_REPO) class AssignTasks(Action): diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index e9d41bb20..3a4ca7768 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -23,6 +23,7 @@ from metagpt.const import TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger from metagpt.schema import CodingContext, RunCodeResult from metagpt.utils.common import CodeParser +from metagpt.utils.file_repository import FileRepository PROMPT_TEMPLATE = """ NOTICE @@ -82,9 +83,8 @@ class WriteCode(Action): async def run(self, *args, **kwargs) -> CodingContext: coding_context = CodingContext.loads(self.context.content) - test_doc = await CONFIG.git_repo.new_file_repository(TEST_OUTPUTS_FILE_REPO).get( - "test_" + coding_context.filename + ".json" - ) + test_doc = await FileRepository.get_file(filename="test_" + coding_context.filename + ".json", + relative_path=TEST_OUTPUTS_FILE_REPO) logs = "" if test_doc: test_detail = RunCodeResult.loads(test_doc.content) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index cc21058b4..c1653a850 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -290,8 +290,7 @@ class WritePRD(Action): async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are # related to the PRD. If they are related, rewrite the PRD. - docs_file_repo = CONFIG.git_repo.new_file_repository(DOCS_FILE_REPO) - requirement_doc = await docs_file_repo.get(REQUIREMENT_FILENAME) + requirement_doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) prd_docs = await prds_file_repo.get_all() change_files = Documents() @@ -355,7 +354,7 @@ class WritePRD(Action): prd = await self._run_new_requirement(requirements=[requirement_doc.content], *args, **kwargs) new_prd_doc = Document( root_path=PRDS_FILE_REPO, - filename=FileRepository.new_file_name() + ".json", + filename=FileRepository.new_filename() + ".json", content=prd.instruct_content.json(), ) elif await self._is_relative_to(requirement_doc, prd_doc): @@ -382,5 +381,4 @@ class WritePRD(Action): @staticmethod async def _save_pdf(prd_doc): - file_repo = CONFIG.git_repo.new_file_repository(PRD_PDF_FILE_REPO) - await file_repo.save_pdf(doc=prd_doc) + await FileRepository.save_as(doc=prd_doc, with_suffix=".md", relative_path=PRD_PDF_FILE_REPO) diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 3df53cca3..018cac168 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -168,7 +168,7 @@ class FileRepository: return children @staticmethod - def new_file_name(): + def new_filename(): """Generate a new filename based on the current timestamp and a UUID suffix. :return: A new filename string. @@ -178,14 +178,22 @@ class FileRepository: # guid_suffix = str(uuid.uuid4())[:8] # return f"{current_time}x{guid_suffix}" - async def save_pdf(self, doc: Document): - """Save a Document as a PDF file. + async def save_doc(self, doc: Document, with_suffix:str = None, dependencies: List[str] = None): + """Save a Document instance as a PDF file. + + This method converts the content of the Document instance to Markdown, + saves it to a file with an optional specified suffix, and logs the saved file. :param doc: The Document instance to be saved. + :type doc: Document + :param with_suffix: An optional suffix to append to the saved file's name. + :type with_suffix: str, optional + :param dependencies: A list of dependencies for the saved file. + :type dependencies: List[str], optional """ m = json.loads(doc.content) - filename = Path(doc.filename).with_suffix(".md") - await self.save(filename=str(filename), content=json_to_markdown(m)) + filename = Path(doc.filename).with_suffix(with_suffix) if with_suffix is not None else Path(doc.filename) + await self.save(filename=str(filename), content=json_to_markdown(m), dependencies=dependencies) logger.info(f"File Saved: {str(filename)}") @staticmethod @@ -228,3 +236,24 @@ class FileRepository: """ file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) return await file_repo.save(filename=filename, content=content, dependencies=dependencies) + + @staticmethod + async def save_as(doc:Document, with_suffix:str = None, dependencies: List[str] = None, relative_path: Path | str = "."): + """Save a Document instance with optional modifications. + + This static method creates a new FileRepository, saves the Document instance + with optional modifications (such as a suffix), and logs the saved file. + + :param doc: The Document instance to be saved. + :type doc: Document + :param with_suffix: An optional suffix to append to the saved file's name. + :type with_suffix: str, optional + :param dependencies: A list of dependencies for the saved file. + :type dependencies: List[str], optional + :param relative_path: The relative path within the file repository. + :type relative_path: Path or str, optional + :return: A boolean indicating whether the save operation was successful. + :rtype: bool + """ + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + return await file_repo.save_doc(doc=doc, with_suffix=with_suffix, dependencies=dependencies) From 4702059caf3c76b05d2a6c7c119a56fbd03a8db9 Mon Sep 17 00:00:00 2001 From: stellahsr Date: Mon, 27 Nov 2023 21:12:50 +0800 Subject: [PATCH 189/413] update basic code for serialize --- metagpt/actions/action.py | 61 +++--- metagpt/actions/design_api.py | 30 ++- metagpt/actions/project_management.py | 27 ++- metagpt/actions/search_and_summarize.py | 52 +++-- metagpt/actions/write_code.py | 15 +- metagpt/actions/write_code_review.py | 13 +- metagpt/actions/write_prd.py | 32 ++- metagpt/environment.py | 5 +- metagpt/roles/architect.py | 18 +- metagpt/roles/engineer.py | 76 +++---- metagpt/roles/product_manager.py | 36 ++-- metagpt/roles/project_manager.py | 27 +-- metagpt/roles/role.py | 271 +++++++++++------------- 13 files changed, 342 insertions(+), 321 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..7bb5a151b 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -7,8 +7,9 @@ """ import re from abc import ABC -from typing import Optional +from typing import Optional, Any +from pydantic import BaseModel, Field from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action_output import ActionOutput @@ -18,45 +19,45 @@ from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder -class Action(ABC): - def __init__(self, name: str = "", context=None, llm: LLM = None): - self.name: str = name - if llm is None: - llm = LLM() - self.llm = llm - self.context = context - self.prefix = "" - self.profile = "" - self.desc = "" - self.content = "" - self.instruct_content = None - +class Action(BaseModel): + name: str = "" + llm: LLM = Field(default_factory=LLM) + context = "" + prefix = "" + profile = "" + desc = "" + content: Optional[str] = None + instruct_content: Optional[str] = None + + def __init__(self, **kwargs: Any): + super().__init__(**kwargs) + def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix self.profile = profile - + def __str__(self): return self.__class__.__name__ - + def __repr__(self): return self.__str__() - + async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str: """Append default prefix""" if not system_msgs: system_msgs = [] system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - + @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) async def _aask_v1( - self, - prompt: str, - output_class_name: str, - output_data_mapping: dict, - system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format + self, + prompt: str, + output_class_name: str, + output_data_mapping: dict, + system_msgs: Optional[list[str]] = None, + format="markdown", # compatible to original format ) -> ActionOutput: """Append default prefix""" if not system_msgs: @@ -65,25 +66,25 @@ class Action(ABC): content = await self.llm.aask(prompt, system_msgs) logger.debug(content) output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - + if format == "json": pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" matches = re.findall(pattern, content, re.DOTALL) - + for match in matches: if match: content = match break - + parsed_data = CustomDecoder(strict=False).decode(content) - + else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - + logger.debug(parsed_data) instruct_content = output_class(**parsed_data) return ActionOutput(content, instruct_content) - + async def run(self, *args, **kwargs): """Run action""" raise NotImplementedError("The run method should be implemented in a subclass.") diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 75df8b909..30df70ce7 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -7,9 +7,12 @@ """ import shutil from pathlib import Path -from typing import List +from typing import List, Optional, Any + +from pydantic import Field from metagpt.actions import Action, ActionOutput +from metagpt.llm import LLM from metagpt.config import CONFIG from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger @@ -150,13 +153,13 @@ OUTPUT_MAPPING = { class WriteDesign(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = ( - "Based on the PRD, think about the system design, and design the corresponding APIs, " - "data structures, library tables, processes, and paths. Please provide your design, feedback " - "clearly and in detail." - ) + name: str = "" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " + "data structures, library tables, processes, and paths. Please provide your design, feedback " + "clearly and in detail." + def recreate_workspace(self, workspace: Path): try: @@ -165,16 +168,18 @@ class WriteDesign(Action): pass # Folder does not exist, but we don't care workspace.mkdir(parents=True, exist_ok=True) + async def _save_prd(self, docs_path, resources_path, context): prd_file = docs_path / "prd.md" if context[-1].instruct_content and context[-1].instruct_content.dict()["Competitive Quadrant Chart"]: quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"] await mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis") - + if context[-1].instruct_content: logger.info(f"Saving PRD to {prd_file}") prd_file.write_text(json_to_markdown(context[-1].instruct_content.dict())) + async def _save_system_design(self, docs_path, resources_path, system_design): data_api_design = system_design.instruct_content.dict()[ "Data structures and interface definitions" @@ -188,6 +193,7 @@ class WriteDesign(Action): logger.info(f"Saving System Designs to {system_design_file}") system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict()))) + async def _save(self, context, system_design): if isinstance(system_design, ActionOutput): ws_name = system_design.instruct_content.dict()["Python package name"] @@ -199,9 +205,13 @@ class WriteDesign(Action): resources_path = workspace / "resources" docs_path.mkdir(parents=True, exist_ok=True) resources_path.mkdir(parents=True, exist_ok=True) - await self._save_prd(docs_path, resources_path, context) + try: + await self._save_prd(docs_path, resources_path, context) + except Exception as e: + logger.error(f"Failed to save PRD {e}") await self._save_system_design(docs_path, resources_path, system_design) + async def run(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index b395fa64e..b72507ee3 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -5,9 +5,12 @@ @Author : alexanderwu @File : project_management.py """ -from typing import List +from typing import List, Optional, Any + +from pydantic import Field from metagpt.actions.action import Action +from metagpt.llm import LLM from metagpt.config import CONFIG from metagpt.const import WORKSPACE_ROOT from metagpt.utils.common import CodeParser @@ -163,21 +166,25 @@ OUTPUT_MAPPING = { class WriteTasks(Action): - def __init__(self, name="CreateTasks", context=None, llm=None): - super().__init__(name, context, llm) - + name: str = "CreateTasks" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + def _save(self, context, rsp): - if context[-1].instruct_content: - ws_name = context[-1].instruct_content.dict()["Python package name"] - else: - ws_name = CodeParser.parse_str(block="Python package name", text=context[-1].content) + try: + if context[-1].instruct_content: + ws_name = context[-1].instruct_content.dict()["Python package name"] + else: + ws_name = CodeParser.parse_str(block="Python package name", text=context[-1].content) + except: + ws_name = "cli_snake_game" # fixme: 应该透传 file_path = WORKSPACE_ROOT / ws_name / "docs/api_spec_and_tasks.md" file_path.write_text(json_to_markdown(rsp.instruct_content.dict())) - + # Write requirements.txt requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt" requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) - + async def run(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 069f2a977..0580303e6 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -6,12 +6,16 @@ @File : search_google.py """ import pydantic +from typing import Optional, Any +from pydantic import BaseModel, Field from metagpt.actions import Action +from metagpt.llm import LLM from metagpt.config import Config from metagpt.logs import logger from metagpt.schema import Message from metagpt.tools.search_engine import SearchEngine +from pydantic import root_validator SEARCH_AND_SUMMARIZE_SYSTEM = """### Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. @@ -54,7 +58,6 @@ SEARCH_AND_SUMMARIZE_PROMPT = """ """ - SEARCH_AND_SUMMARIZE_SALES_SYSTEM = """## Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. - The context is for reference only. If it is irrelevant to the user's search request history, please reduce its reference and usage. @@ -101,23 +104,41 @@ You are a member of a professional butler team and will provide helpful suggesti class SearchAndSummarize(Action): - def __init__(self, name="", context=None, llm=None, engine=None, search_func=None): - self.config = Config() - self.engine = engine or self.config.search_engine + name: str = "" + content: Optional[str] = None + llm: None = Field(default_factory=LLM) + config: None = Field(default_factory=Config) + engine: Optional[str] = None + search_func: Optional[str] = None - try: - self.search_engine = SearchEngine(self.engine, run_func=search_func) - except pydantic.ValidationError: - self.search_engine = None + result = "" + - self.result = "" - super().__init__(name, context, llm) + @root_validator + def validate_engine_and_run_func(cls, values): + engine = values.get('engine') + search_func = values.get('search_func') + config = Config() + + if engine is None: + engine = config.search_engine + config_data = { + 'engine': engine, + 'run_func': search_func + } + search_engine = SearchEngine(**config_data) + values['search_engine'] = search_engine + return values + + + async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: + print(context) if self.search_engine is None: logger.warning("Configure one of SERPAPI_API_KEY, SERPER_API_KEY, GOOGLE_API_KEY to unlock full feature") return "" - + query = context[-1].content # logger.debug(query) rsp = await self.search_engine.run(query) @@ -126,9 +147,9 @@ class SearchAndSummarize(Action): logger.error("empty rsp...") return "" # logger.info(rsp) - + system_prompt = [system_text] - + prompt = SEARCH_AND_SUMMARIZE_PROMPT.format( # PREFIX = self.prefix, ROLE=self.profile, @@ -140,4 +161,7 @@ class SearchAndSummarize(Action): logger.debug(prompt) logger.debug(result) return result - \ No newline at end of file + + +if __name__ == "__main__": + action = SearchAndSummarize() diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index c000805c5..2dc240591 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -5,13 +5,18 @@ @Author : alexanderwu @File : write_code.py """ +from typing import List, Optional, Any + +from pydantic import Field +from tenacity import retry, stop_after_attempt, wait_fixed + from metagpt.actions import WriteDesign from metagpt.actions.action import Action +from metagpt.llm import LLM from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -43,9 +48,10 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): - def __init__(self, name="WriteCode", context: list[Message] = None, llm=None): - super().__init__(name, context, llm) - + name: str = "WriteCode" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + def _is_invalid(self, filename): return any(i in filename for i in ["mp3", "wav"]) @@ -79,4 +85,3 @@ class WriteCode(Action): # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) return code - \ No newline at end of file diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4ff4d6cf6..3d86d7c63 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -5,12 +5,15 @@ @Author : alexanderwu @File : write_code_review.py """ +from typing import List, Optional, Any +from pydantic import Field +from tenacity import retry, stop_after_attempt, wait_fixed +from metagpt.llm import LLM from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE @@ -62,9 +65,10 @@ FORMAT_EXAMPLE = """ class WriteCodeReview(Action): - def __init__(self, name="WriteCodeReview", context: list[Message] = None, llm=None): - super().__init__(name, context, llm) - + name: str = "WriteCodeReview" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) async def write_code(self, prompt): code_rsp = await self._aask(prompt) @@ -79,4 +83,3 @@ class WriteCodeReview(Action): # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) # self._save(context, filename, code) return code - \ No newline at end of file diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bd04ca79e..660d7fb95 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -5,9 +5,12 @@ @Author : alexanderwu @File : write_prd.py """ -from typing import List +from typing import List, Optional, Any + +from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput +from metagpt.llm import LLM from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.logs import logger @@ -219,18 +222,25 @@ OUTPUT_MAPPING = { class WritePRD(Action): - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) - + name: str = "" + content: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + assistant_search_action: Action = None + + def __init__(self, **kwargs): + super().__init__(**kwargs) + async def run(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: - sas = SearchAndSummarize() - # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) - rsp = "" - info = f"### Search Results\n{sas.result}\n\n### Search Summary\n{rsp}" - if sas.result: - logger.info(sas.result) + # self.assistant_search_action = SearchAndSummarize() + if self.assistant_search_action is None: + self.assistant_search_action = SearchAndSummarize() + # self.assistant_search_action = SearchAndSummarize() + rsp = await self.assistant_search_action.run(context=requirements) + info = f"### Search Results\n{self.assistant_search_action.result}\n\n### Search Summary\n{rsp}" + if self.assistant_search_action.result: + logger.info(self.assistant_search_action.result) logger.info(rsp) - + prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format( requirements=requirements, search_information=info, format_example=format_example diff --git a/metagpt/environment.py b/metagpt/environment.py index 24e6ada2f..88ff145e0 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -29,11 +29,12 @@ class Environment(BaseModel): arbitrary_types_allowed = True def add_role(self, role: Role): - """增加一个在当前环境的角色 + """增加一个在当前环境的角色, 默认为profile/role_profile Add a role in the current environment """ role.set_env(self) - self.roles[role.profile] = role + # use alias + self.roles[role.role_profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index 15d5fe5b1..face22a68 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -5,10 +5,11 @@ @Author : alexanderwu @File : architect.py """ +from pydantic import Field from metagpt.actions import WritePRD from metagpt.actions.design_api import WriteDesign -from metagpt.roles import Role +from metagpt.roles.role import Role class Architect(Role): @@ -21,17 +22,16 @@ class Architect(Role): goal (str): Primary goal or responsibility of the architect. constraints (str): Constraints or guidelines for the architect. """ + name: str = "Bob" + role_profile: str = Field(default="Architect" , alias='profile') + goal: str = "Design a concise, usable, complete python system" + constraints: str = "Try to specify good open source tools as much as possible" def __init__( - self, - name: str = "Bob", - profile: str = "Architect", - goal: str = "Design a concise, usable, complete python system", - constraints: str = "Try to specify good open source tools as much as possible", + self, + **kwargs ) -> None: - """Initializes the Architect with given attributes.""" - super().__init__(name, profile, goal, constraints) - + super().__init__(**kwargs) # Initialize actions specific to the Architect role self._init_actions([WriteDesign]) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 1f6685b38..129bedeb8 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -9,11 +9,12 @@ import asyncio import shutil from collections import OrderedDict from pathlib import Path +from pydantic import Field from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger -from metagpt.roles import Role +from metagpt.roles.role import Role from metagpt.schema import Message from metagpt.utils.common import CodeParser from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP @@ -23,7 +24,7 @@ async def gather_ordered_k(coros, k) -> list: tasks = OrderedDict() results = [None] * len(coros) done_queue = asyncio.Queue() - + for i, coro in enumerate(coros): if len(tasks) >= k: done, _ = await asyncio.wait(tasks.keys(), return_when=asyncio.FIRST_COMPLETED) @@ -32,17 +33,17 @@ async def gather_ordered_k(coros, k) -> list: await done_queue.put((index, task.result())) task = asyncio.create_task(coro) tasks[task] = i - + if tasks: done, _ = await asyncio.wait(tasks.keys()) for task in done: index = tasks[task] await done_queue.put((index, task.result())) - + while not done_queue.empty(): index, result = await done_queue.get() results[index] = result - + return results @@ -59,42 +60,42 @@ class Engineer(Role): use_code_review (bool): Whether to use code review. todos (list): List of tasks. """ - + name: str = "Alex" + role_profile: str = Field(default="Engineer", alias='profile') + goal: str = "Write elegant, readable, extensible, efficient code" + constraints: str = "The code should conform to standards like PEP8 and be modular and maintainable" + n_borg: int = 1 + use_code_review: bool = False + todos: list = [] + def __init__( - self, - name: str = "Alex", - profile: str = "Engineer", - goal: str = "Write elegant, readable, extensible, efficient code", - constraints: str = "The code should conform to standards like PEP8 and be modular and maintainable", - n_borg: int = 1, - use_code_review: bool = False, + self, + **kwargs ) -> None: - """Initializes the Engineer role with given attributes.""" - super().__init__(name, profile, goal, constraints) - self._init_actions([WriteCode]) - self.use_code_review = use_code_review + super().__init__(**kwargs) + + actions = [WriteCode] if self.use_code_review: - self._init_actions([WriteCode, WriteCodeReview]) + actions = [WriteCode, WriteCodeReview] + self._init_actions(actions) self._watch([WriteTasks]) - self.todos = [] - self.n_borg = n_borg - + @classmethod def parse_tasks(self, task_msg: Message) -> list[str]: if task_msg.instruct_content: return task_msg.instruct_content.dict().get("Task list") return CodeParser.parse_file_list(block="Task list", text=task_msg.content) - + @classmethod def parse_code(self, code_text: str) -> str: return CodeParser.parse_code(block="", text=code_text) - + @classmethod def parse_workspace(cls, system_design_msg: Message) -> str: if system_design_msg.instruct_content: return system_design_msg.instruct_content.dict().get("Python package name").strip().strip("'").strip('"') return CodeParser.parse_str(block="Python package name", text=system_design_msg.content) - + def get_workspace(self) -> Path: msg = self._rc.memory.get_by_action(WriteDesign)[-1] if not msg: @@ -102,7 +103,7 @@ class Engineer(Role): workspace = self.parse_workspace(msg) # Codes are written in workspace/{package_name}/{package_name} return WORKSPACE_ROOT / workspace / workspace - + def recreate_workspace(self): workspace = self.get_workspace() try: @@ -110,7 +111,7 @@ class Engineer(Role): except FileNotFoundError: pass # The folder does not exist, but we don't care workspace.mkdir(parents=True, exist_ok=True) - + def write_file(self, filename: str, code: str): workspace = self.get_workspace() filename = filename.replace('"', "").replace("\n", "") @@ -118,12 +119,12 @@ class Engineer(Role): file.parent.mkdir(parents=True, exist_ok=True) file.write_text(code) return file - + def recv(self, message: Message) -> None: self._rc.memory.add(message) if message in self._rc.important_memory: self.todos = self.parse_tasks(message) - + async def _act_mp(self) -> Message: # self.recreate_workspace() todo_coros = [] @@ -132,7 +133,7 @@ class Engineer(Role): context=self._rc.memory.get_by_actions([WriteTasks, WriteDesign]), filename=todo ) todo_coros.append(todo_coro) - + rsps = await gather_ordered_k(todo_coros, self.n_borg) for todo, code_rsp in zip(self.todos, rsps): _ = self.parse_code(code_rsp) @@ -142,11 +143,11 @@ class Engineer(Role): msg = Message(content=code_rsp, role=self.profile, cause_by=type(self._rc.todo)) self._rc.memory.add(msg) del self.todos[0] - + logger.info(f"Done {self.get_workspace()} generating.") msg = Message(content="all done.", role=self.profile, cause_by=type(self._rc.todo)) return msg - + async def _act_sp(self) -> Message: code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later for todo in self.todos: @@ -157,16 +158,16 @@ class Engineer(Role): file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=type(self._rc.todo)) self._rc.memory.add(msg) - + code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) - + logger.info(f"Done {self.get_workspace()} generating.") msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" ) return msg - + async def _act_sp_precision(self) -> Message: code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later for todo in self.todos: @@ -195,19 +196,18 @@ class Engineer(Role): file_path = self.write_file(todo, code) msg = Message(content=code, role=self.profile, cause_by=WriteCode) self._rc.memory.add(msg) - + code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) - + logger.info(f"Done {self.get_workspace()} generating.") msg = Message( content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" ) return msg - + async def _act(self) -> Message: """Determines the mode of action based on whether code review is used.""" - logger.info(f"{self._setting}: ready to WriteCode") if self.use_code_review: return await self._act_sp_precision() return await self._act_sp() diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a58ea5385..b099fb4d9 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -5,37 +5,33 @@ @Author : alexanderwu @File : product_manager.py """ +from pydantic import Field + from metagpt.actions import BossRequirement, WritePRD -from metagpt.roles import Role +from metagpt.roles.role import Role class ProductManager(Role): """ - Represents a Product Manager role responsible for product development and management. + Initializes the ProductManager role with given attributes. - Attributes: + Args: name (str): Name of the product manager. - profile (str): Role profile, default is 'Product Manager'. + profile (str): Role profile. goal (str): Goal of the product manager. constraints (str): Constraints or limitations for the product manager. """ - + name: str = "Alice" + role_profile: str = Field(default="Product Manager", alias='profile') + goal: str = "Efficiently create a successful product" + constraints: str = "" + """ + Represents a Product Manager role responsible for product development and management. + """ def __init__( - self, - name: str = "Alice", - profile: str = "Product Manager", - goal: str = "Efficiently create a successful product", - constraints: str = "", + self, + **kwargs ) -> None: - """ - Initializes the ProductManager role with given attributes. - - Args: - name (str): Name of the product manager. - profile (str): Role profile. - goal (str): Goal of the product manager. - constraints (str): Constraints or limitations for the product manager. - """ - super().__init__(name, profile, goal, constraints) + super().__init__(**kwargs) self._init_actions([WritePRD]) self._watch([BossRequirement]) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 7e7c5699d..a2b227f22 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -5,9 +5,11 @@ @Author : alexanderwu @File : project_manager.py """ +from pydantic import Field + from metagpt.actions import WriteTasks from metagpt.actions.design_api import WriteDesign -from metagpt.roles import Role +from metagpt.roles.role import Role class ProjectManager(Role): @@ -20,23 +22,16 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ + name: str = "Eve" + role_profile: str = Field(default="Project Manager", alias='profile') + + goal: str = "Improve team efficiency and deliver with quality and quantity" + constraints: str = "" def __init__( - self, - name: str = "Eve", - profile: str = "Project Manager", - goal: str = "Improve team efficiency and deliver with quality and quantity", - constraints: str = "", + self, + **kwargs ) -> None: - """ - Initializes the ProjectManager role with given attributes. - - Args: - name (str): Name of the project manager. - profile (str): Role profile. - goal (str): Goal of the project manager. - constraints (str): Constraints or limitations for the project manager. - """ - super().__init__(name, profile, goal, constraints) + super().__init__(**kwargs) self._init_actions([WriteTasks]) self._watch([WriteDesign]) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b96c361c0..9aae64188 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -5,17 +5,26 @@ @Author : alexanderwu @File : role.py """ + from __future__ import annotations -from typing import Iterable, Type, Union -from enum import Enum - +import sys +from types import SimpleNamespace +from typing import ( + Dict, + Optional, + Union, + Iterable, + Type +) +import re from pydantic import BaseModel, Field +from importlib import import_module # from metagpt.environment import Environment from metagpt.config import CONFIG from metagpt.actions import Action, ActionOutput -from metagpt.llm import LLM, HumanProvider +from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message @@ -28,14 +37,12 @@ Please note that only the text between the first and second "===" is information {history} === -Your previous stage: {previous_state} - -Now choose one of the following stages you need to go to in the next step: +You can now choose one of the following stages to decide the stage you need to go in the next step: {states} Just answer a number between 0-{n_states}, choose the most suitable stage according to the understanding of the conversation. Please note that the answer only needs a number, no need to add any other text. -If you think you have completed your goal and don't need to go to any of the stages, return -1. +If there is no conversation record, choose 0. Do not answer anything else, and do not add any other information in your answer. """ @@ -49,27 +56,18 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi {name}: {result} """ -class RoleReactMode(str, Enum): - REACT = "react" - BY_ORDER = "by_order" - PLAN_AND_ACT = "plan_and_act" - - @classmethod - def values(cls): - return [item.value for item in cls] class RoleSetting(BaseModel): """Role Settings""" - name: str - profile: str - goal: str - constraints: str - desc: str - is_human: bool - + name: str = "" + profile: str = "" + goal: str = "" + constraints: str = "" + desc: str = "" + def __str__(self): return f"{self.name}({self.profile})" - + def __repr__(self): return self.__str__() @@ -79,109 +77,128 @@ class RoleContext(BaseModel): env: 'Environment' = Field(default=None) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) - state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None + state: int = Field(default=0) todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[]) - react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes - max_react_loop: int = 1 - + class Config: arbitrary_types_allowed = True - + def check(self, role_id: str): if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: self.long_term_memory.recover_memory(role_id, self) self.memory = self.long_term_memory # use memory to act as long_term_memory for unify operation - + @property def important_memory(self) -> list[Message]: """Get the information corresponding to the watched actions""" return self.memory.get_by_actions(self.watch) - + @property def history(self) -> list[Message]: return self.memory.get() -class Role: +class Role(BaseModel): """Role/Agent""" - - def __init__(self, name="", profile="", goal="", constraints="", desc="", is_human=False): - self._llm = LLM() if not is_human else HumanProvider() - self._setting = RoleSetting(name=name, profile=profile, goal=goal, - constraints=constraints, desc=desc, is_human=is_human) - self._states = [] - self._actions = [] - self._role_id = str(self._setting) - self._rc = RoleContext() - + name: str = "" + profile: str = "" + goal: str = "" + constraints: str = "" + desc: str = "" + _setting: RoleSetting = Field(default_factory=RoleSetting, alias="_setting") + _setting = RoleSetting(name=name, profile=profile, goal=goal, constraints=constraints) + _role_id: str = "" + _states: list = Field(default=[]) + _actions: list = Field(default=[]) + _actions_type: list = Field(default=[]) + _rc: RoleContext = RoleContext() + + _private_attributes = { + '_setting': _setting, + '_role_id': _role_id, + '_states': [], + '_actions': [], + '_actions_type': [] # 用于记录和序列化 + } + + class Config: + arbitrary_types_allowed = True + + def __init__(self, **kwargs): + super().__init__(**kwargs) + # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 + for key in self._private_attributes.keys(): + if key in kwargs: + object.__setattr__(self, key, kwargs[key]) + if key =="_setting": + _setting = RoleSetting(**kwargs[key]) + object.__setattr__(self, '_setting', _setting) + elif key == "_rc": + _rc = RoleContext + object.__setattr__(self, '_rc', _rc) + else: + object.__setattr__(self, key, self._private_attributes[key]) + def _reset(self): - self._states = [] - self._actions = [] + object.__setattr__(self, '_states', []) + object.__setattr__(self, '_actions', []) + + + @staticmethod + def _process_class(class_str, module_name): + cleaned_string = re.sub(r"[<>']", "", class_str).replace("class ", "") + package_name = "metagpt" + file_name = cleaned_string.replace(package_name, "").replace("." + module_name, "") + print(file_name) + # print("\n", sys.modules) + module_file = import_module(file_name, package=package_name) + module = getattr(module_file, module_name) + return module + def _init_actions(self, actions): self._reset() for idx, action in enumerate(actions): if not isinstance(action, Action): - i = action("", llm=self._llm) + ## 默认初始化 + i = action() else: - if self._setting.is_human and not isinstance(action.llm, HumanProvider): - logger.warning(f"is_human attribute does not take effect," - f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances") i = action i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) self._states.append(f"{idx}. {action}") - - def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): - """Set strategy of the Role reacting to observed Message. Variation lies in how - this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. - - Args: - react_mode (str): Mode for choosing action during the _think stage, can be one of: - "react": standard think-act loop in the ReAct paper, alternating thinking and acting to solve the task, i.e. _think -> _act -> _think -> _act -> ... - Use llm to select actions in _think dynamically; - "by_order": switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...; - "plan_and_act": first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... - Use llm to come up with the plan dynamically. - Defaults to "react". - max_react_loop (int): Maximum react cycles to execute, used to prevent the agent from reacting forever. - Take effect only when react_mode is react, in which we use llm to choose actions, including termination. - Defaults to 1, i.e. _think -> _act (-> return result and end) - """ - assert react_mode in RoleReactMode.values(), f"react_mode must be one of {RoleReactMode.values()}" - self._rc.react_mode = react_mode - if react_mode == RoleReactMode.REACT: - self._rc.max_react_loop = max_react_loop - + action_title = action.schema()["title"] + self._actions_type.append(action_title) + def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" self._rc.watch.update(actions) # check RoleContext after adding watch actions self._rc.check(self._role_id) - - def _set_state(self, state: int): + + def _set_state(self, state): """Update the current state.""" self._rc.state = state logger.debug(self._actions) - self._rc.todo = self._actions[self._rc.state] if state >= 0 else None - + self._rc.todo = self._actions[self._rc.state] + def set_env(self, env: 'Environment'): """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env - + @property def profile(self): """Get the role description (position)""" return self._setting.profile - + def _get_prefix(self): """Get the role prefix""" if self._setting.desc: return self._setting.desc return PREFIX_TEMPLATE.format(**self._setting.dict()) - + async def _think(self) -> None: """Think about what to do and decide on the next action""" if len(self._actions) == 1: @@ -190,104 +207,60 @@ class Role: return prompt = self._get_prefix() prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), - n_states=len(self._states) - 1, previous_state=self._rc.state) - # print(prompt) + n_states=len(self._states) - 1) next_state = await self._llm.aask(prompt) logger.debug(f"{prompt=}") - if (not next_state.isdigit() and next_state != "-1") \ - or int(next_state) not in range(-1, len(self._states)): - logger.warning(f'Invalid answer of state, {next_state=}, will be set to -1') - next_state = -1 - else: - next_state = int(next_state) - if next_state == -1: - logger.info(f"End actions with {next_state=}") - self._set_state(next_state) - + if not next_state.isdigit() or int(next_state) not in range(len(self._states)): + logger.warning(f'Invalid answer of state, {next_state=}') + next_state = "0" + self._set_state(int(next_state)) + async def _act(self) -> Message: - # prompt = self.get_prefix() - # prompt += ROLE_TEMPLATE.format(name=self.profile, state=self.states[self.state], result=response, - # history=self.history) - logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) # logger.info(response) if isinstance(response, ActionOutput): msg = Message(content=response.content, instruct_content=response.instruct_content, - role=self.profile, cause_by=type(self._rc.todo)) + role=self.profile, cause_by=type(self._rc.todo)) else: msg = Message(content=response, role=self.profile, cause_by=type(self._rc.todo)) self._rc.memory.add(msg) # logger.debug(f"{response}") - + return msg - + async def _observe(self) -> int: """Observe from the environment, obtain important information, and add it to memory""" if not self._rc.env: return 0 env_msgs = self._rc.env.memory.get() - + observed = self._rc.env.memory.get_by_actions(self._rc.watch) - self._rc.news = self._rc.memory.find_news(observed) # find news (previously unseen messages) from observed messages - + self._rc.news = self._rc.memory.find_news( + observed) # find news (previously unseen messages) from observed messages + for i in env_msgs: self.recv(i) - + news_text = [f"{i.role}: {i.content[:20]}..." for i in self._rc.news] if news_text: logger.debug(f'{self._setting} observed: {news_text}') return len(self._rc.news) - + def _publish_message(self, msg): """If the role belongs to env, then the role's messages will be broadcast to env""" if not self._rc.env: # If env does not exist, do not publish the message return self._rc.env.publish_message(msg) - + async def _react(self) -> Message: - """Think first, then act, until the Role _think it is time to stop and requires no more todo. - This is the standard think-act loop in the ReAct paper, which alternates thinking and acting in task solving, i.e. _think -> _act -> _think -> _act -> ... - Use llm to select actions in _think dynamically - """ - actions_taken = 0 - rsp = Message("No actions taken yet") # will be overwritten after Role _act - while actions_taken < self._rc.max_react_loop: - # think - await self._think() - if self._rc.todo is None: - break - # act - logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") - rsp = await self._act() - actions_taken += 1 - return rsp # return output from the last action - - async def _act_by_order(self) -> Message: - """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" - for i in range(len(self._states)): - self._set_state(i) - rsp = await self._act() - return rsp # return output from the last action - - async def _plan_and_act(self) -> Message: - """first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... Use llm to come up with the plan dynamically.""" - # TODO: to be implemented - return Message("") - - async def react(self) -> Message: - """Entry to one of three strategies by which Role reacts to the observed Message""" - if self._rc.react_mode == RoleReactMode.REACT: - rsp = await self._react() - elif self._rc.react_mode == RoleReactMode.BY_ORDER: - rsp = await self._act_by_order() - elif self._rc.react_mode == RoleReactMode.PLAN_AND_ACT: - rsp = await self._plan_and_act() - self._set_state(state=-1) # current reaction is complete, reset state to -1 and todo back to None - return rsp - + """Think first, then act""" + await self._think() + logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") + return await self._act() + def recv(self, message: Message) -> None: """add message to history.""" # self._history += f"\n{message}" @@ -295,18 +268,14 @@ class Role: if message in self._rc.memory.get(): return self._rc.memory.add(message) - + async def handle(self, message: Message) -> Message: """Receive information and reply with actions""" # logger.debug(f"{self.name=}, {self.profile=}, {message.role=}") self.recv(message) - + return await self._react() - - def get_memories(self, k=0) -> list[Message]: - """A wrapper to return the most recent k memories of this role, return all when k=0""" - return self._rc.memory.get(k=k) - + async def run(self, message=None): """Observe, and think and act based on the results of the observation""" if message: @@ -320,8 +289,8 @@ class Role: # If there is no new information, suspend and wait logger.debug(f"{self._setting}: no news. waiting.") return - - rsp = await self.react() + + rsp = await self._react() # Publish the reply to the environment, waiting for the next subscriber to process self._publish_message(rsp) return rsp From 0dd63e4b2363d30d6c7e5db1705e749f00c9f82f Mon Sep 17 00:00:00 2001 From: stellahsr Date: Mon, 27 Nov 2023 21:13:19 +0800 Subject: [PATCH 190/413] update test cases for serialize_deserialize --- .../metagpt/serialize_deserialize/__init__.py | 4 ++ .../serialize_deserialize/test_actions.py | 24 ++++++++++ .../test_architect_deserialize.py | 26 ++++++++++ .../test_product_manager.py | 21 +++++++++ .../test_project_manager.py | 26 ++++++++++ .../serialize_deserialize/test_role.py | 41 ++++++++++++++++ .../serialize_deserialize/test_team.py | 47 +++++++++++++++++++ .../serialize_deserialize/test_wrire_prd.py | 28 +++++++++++ .../serialize_deserialize/test_write_code.py | 42 +++++++++++++++++ .../test_write_design.py | 39 +++++++++++++++ 10 files changed, 298 insertions(+) create mode 100644 tests/metagpt/serialize_deserialize/__init__.py create mode 100644 tests/metagpt/serialize_deserialize/test_actions.py create mode 100644 tests/metagpt/serialize_deserialize/test_architect_deserialize.py create mode 100644 tests/metagpt/serialize_deserialize/test_product_manager.py create mode 100644 tests/metagpt/serialize_deserialize/test_project_manager.py create mode 100644 tests/metagpt/serialize_deserialize/test_role.py create mode 100644 tests/metagpt/serialize_deserialize/test_team.py create mode 100644 tests/metagpt/serialize_deserialize/test_wrire_prd.py create mode 100644 tests/metagpt/serialize_deserialize/test_write_code.py create mode 100644 tests/metagpt/serialize_deserialize/test_write_design.py diff --git a/tests/metagpt/serialize_deserialize/__init__.py b/tests/metagpt/serialize_deserialize/__init__.py new file mode 100644 index 000000000..78f454fb5 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py new file mode 100644 index 000000000..e2efa982b --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_actions.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import Action +from metagpt.llm import LLM + +def test_action_serialize(): + action = Action() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = Action() + serialized_data = action.dict() + + new_action = Action(**serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action._aask("who are you")) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py new file mode 100644 index 000000000..cff1bbadd --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:04 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.architect import Architect +from metagpt.actions.action import Action + +def test_architect_serialize(): + role = Architect() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + +@pytest.mark.asyncio +async def test_architect_deserialize(): + role = Architect() + ser_role_dict = role.dict(by_alias=True) + new_role = Architect(**ser_role_dict) + # new_role = Architect.deserialize(ser_role_dict) + assert new_role.name == "Bob" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py new file mode 100644 index 000000000..978c50e5e --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:07 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.product_manager import ProductManager +from metagpt.actions.action import Action +from metagpt.schema import Message + +@pytest.mark.asyncio +async def test_product_manager_deserialize(): + role = ProductManager() + ser_role_dict = role.dict(by_alias=True) + new_role = ProductManager(**ser_role_dict) + # new_role = ProductManager().deserialize(ser_role_dict) + + assert new_role.name == "Alice" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run([Message(content="write a cli snake game")]) \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py new file mode 100644 index 000000000..590bd8109 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:06 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.project_manager import ProjectManager +from metagpt.actions.action import Action + +def test_project_manager_serialize(): + role = ProjectManager() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + +@pytest.mark.asyncio +async def test_project_manager_deserialize(): + role = ProjectManager() + ser_role_dict = role.dict(by_alias=True) + new_role = ProjectManager(**ser_role_dict) + # new_role = ProjectManager().deserialize(ser_role_dict) + assert new_role.name == "Eve" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py new file mode 100644 index 000000000..432c9acb7 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# @Date : 11/23/2023 4:49 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.role import Role +from metagpt.roles.engineer import Engineer + +from metagpt.actions.action import Action + + +def test_role_serialize(): + role = Role() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + + +def test_engineer_serialize(): + role = Engineer() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + + +@pytest.mark.asyncio +async def test_engineer_deserialize(): + role = Engineer(use_code_review=True) + ser_role_dict = role.dict(by_alias=True) + # new_role = Engineer().deserialize(ser_role_dict) + # also can be deserialized in this way: + new_role = Engineer(**ser_role_dict) + assert new_role.name == "Alex" + assert new_role.use_code_review == True + assert len(new_role._actions) == 2 + assert isinstance(new_role._actions[0], Action) + assert isinstance(new_role._actions[1], Action) + await new_role._actions[0].run(context="write a cli snake game", filename="test_code") diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py new file mode 100644 index 000000000..44a75d262 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# @Date : 11/27/2023 10:07 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.environment import Environment +from metagpt.schema import Message +from metagpt.software_company import SoftwareCompany +from metagpt.roles import ProjectManager, ProductManager, Architect + + +def test_env_serialize(): + env = Environment() + ser_env_dict = env.dict() + assert "roles" in ser_env_dict + assert "memory" in ser_env_dict + assert "memory" in ser_env_dict + + +def test_env_deserialize(): + env = Environment() + env.publish_message(message=Message(content="test env serialize")) + ser_env_dict = env.dict() + new_env = Environment(**ser_env_dict) + assert len(new_env.roles) == 0 + assert new_env.memory.storage[0].content == "test env serialize" + assert len(new_env.history) == 25 + + +def test_softwarecompany_deserialize(): + team = SoftwareCompany() + team.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) + assert len(team.environment.get_roles()) == 3 + ser_team_dict = team.dict() + new_team = SoftwareCompany(**ser_team_dict) + + assert len(new_team.environment.get_roles()) == 3 + assert new_team.environment.get_role('Product Manager') is not None + assert new_team.environment.get_role('Product Manager') is not None + assert new_team.environment.get_role('Architect') is not None diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py new file mode 100644 index 000000000..9b2653820 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 1:47 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WritePRD +from metagpt.llm import LLM +from metagpt.schema import Message + + +def test_action_serialize(): + action = WritePRD() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = WritePRD() + serialized_data = action.dict() + new_action = WritePRD(**serialized_data) + # new_action = WritePRD().deserialize(serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action.run([Message(content="write a cli snake game")]))>0 + diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py new file mode 100644 index 000000000..0b1f1dc7c --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# @Date : 11/23/2023 10:56 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.llm import LLM + +def test_write_design_serialize(): + action = WriteCode() + ser_action_dict = action.dict() + assert ser_action_dict["name"] == "WriteCode" + assert "llm" in ser_action_dict + +def test_write_task_serialize(): + action = WriteCodeReview() + ser_action_dict = action.dict() + assert ser_action_dict["name"] == "WriteCodeReview" + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_write_code_deserialize(): + action = WriteCode() + serialized_data = action.dict() + new_action = WriteCode(**serialized_data) + # new_action = WriteCode().deserialize(serialized_data) + assert new_action.name == "WriteCode" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game", filename="test_code") + +@pytest.mark.asyncio +async def test_write_code_review_deserialize(): + action = WriteCodeReview() + serialized_data = action.dict() + new_action = WriteCodeReview(**serialized_data) + # new_action = WriteCodeReview().deserialize(serialized_data) + code = await WriteCode().run(context="write a cli snake game", filename="test_code") + + assert new_action.name == "WriteCodeReview" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game", code =code, filename="test_rewrite_code") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py new file mode 100644 index 000000000..56bf78a63 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 8:19 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WriteDesign, WriteTasks +from metagpt.llm import LLM + +def test_write_design_serialize(): + action = WriteDesign() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +def test_write_task_serialize(): + action = WriteTasks() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_write_design_deserialize(): + action = WriteDesign() + serialized_data = action.dict() + new_action = WriteDesign().deserialize(serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game") + +@pytest.mark.asyncio +async def test_write_task_deserialize(): + action = WriteTasks() + serialized_data = action.dict() + new_action = WriteTasks(**serialized_data) + # new_action = WriteTasks().deserialize(serialized_data) + assert new_action.name == "CreateTasks" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game") \ No newline at end of file From 2cd7d266ddce6a4c0979e29363d38b6c58f9b15f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 27 Nov 2023 21:20:46 +0800 Subject: [PATCH 191/413] feat: merge Config class of send18:dev branch --- metagpt/actions/run_code.py | 6 +++-- metagpt/config.py | 52 ++++++++++++++++++++++++++++++------- metagpt/const.py | 3 +++ 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index 1e7010e52..fa13a0980 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -12,13 +12,15 @@ RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. 4. According to section 2.2.3.5.7 of RFC 135, change the method of transferring file content (code files, unit test files, log files) from using the message to using the file name. + 5. Merged the `Config` class of send18:dev branch to take over the set/get operations of the Environment + class. """ -import os import subprocess import traceback from typing import Tuple from metagpt.actions.action import Action +from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import RunCodeResult @@ -92,7 +94,7 @@ class RunCode(Action): additional_python_paths = [str(path) for path in additional_python_paths] # Copy the current environment variables - env = os.environ.copy() + env = CONFIG.new_environ() # Modify the PYTHONPATH environment variable additional_python_paths = [working_directory] + additional_python_paths diff --git a/metagpt/config.py b/metagpt/config.py index a20f58ec1..1b70d5fa6 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -7,11 +7,13 @@ Provide configuration, singleton 2. Add the parameter `src_workspace` for the old version project path. """ import os +from copy import deepcopy +from typing import Any import openai import yaml -from metagpt.const import PROJECT_ROOT +from metagpt.const import OPTIONS, PROJECT_ROOT from metagpt.logs import logger from metagpt.tools import SearchEngineType, WebBrowserEngineType from metagpt.utils.singleton import Singleton @@ -42,9 +44,11 @@ class Config(metaclass=Singleton): default_yaml_file = PROJECT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): - self._configs = {} - self._init_with_config_files_and_env(self._configs, yaml_file) + self._init_with_config_files_and_env(yaml_file) logger.info("Config loading done.") + self._update() + + def _update(self): self.global_proxy = self._get("GLOBAL_PROXY") self.openai_api_key = self._get("OPENAI_API_KEY") self.anthropic_api_key = self._get("Anthropic_API_KEY") @@ -96,12 +100,10 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.prompt_format = self._get("PROMPT_FORMAT", "markdown") - self.git_repo = None - self.src_workspace = None - def _init_with_config_files_and_env(self, configs: dict, yaml_file): + def _init_with_config_files_and_env(self, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" - configs.update(os.environ) + configs = dict(os.environ) for _yaml_file in [yaml_file, self.key_yaml_file]: if not _yaml_file.exists(): @@ -112,11 +114,13 @@ class Config(metaclass=Singleton): yaml_data = yaml.safe_load(file) if not yaml_data: continue - os.environ.update({k: v for k, v in yaml_data.items() if isinstance(v, str)}) configs.update(yaml_data) + OPTIONS.set(configs) - def _get(self, *args, **kwargs): - return self._configs.get(*args, **kwargs) + @staticmethod + def _get(*args, **kwargs): + m = OPTIONS.get() + return m.get(*args, **kwargs) def get(self, key, *args, **kwargs): """Search for a value in config/key.yaml, config/config.yaml, and env; raise an error if not found""" @@ -125,5 +129,33 @@ class Config(metaclass=Singleton): raise ValueError(f"Key '{key}' not found in environment variables or in the YAML file") return value + def __setattr__(self, name: str, value: Any) -> None: + OPTIONS.get()[name] = value + + def __getattr__(self, name: str) -> Any: + m = OPTIONS.get() + return m.get(name) + + def set_context(self, options: dict): + """Update current config""" + if not options: + return + opts = deepcopy(OPTIONS.get()) + opts.update(options) + OPTIONS.set(opts) + self._update() + + @property + def options(self): + """Return all key-values""" + return OPTIONS.get() + + def new_environ(self): + """Return a new os.environ object""" + env = os.environ.copy() + m = self.options + env.update({k: v for k, v in m.items() if isinstance(v, str)}) + return env + CONFIG = Config() diff --git a/metagpt/const.py b/metagpt/const.py index ce06655f1..9278a5d0e 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -8,6 +8,7 @@ common properties in the Message. @Modified By: mashenquan, 2023-11-27. Defines file repository paths according to Section 2.2.3.4 of RFC 135. """ +import contextvars from pathlib import Path @@ -27,6 +28,8 @@ def get_project_root(): current_path = parent_path +OPTIONS = contextvars.ContextVar("OPTIONS") + PROJECT_ROOT = get_project_root() DATA_PATH = PROJECT_ROOT / "data" WORKSPACE_ROOT = PROJECT_ROOT / "workspace" From d99b4c62e33d1c37cb832c04030697d37a90be66 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 28 Nov 2023 09:29:00 +0800 Subject: [PATCH 192/413] add mg ser&deser --- metagpt/actions/action.py | 32 ++++++++ metagpt/const.py | 1 + metagpt/environment.py | 38 +++++++++ metagpt/memory/memory.py | 30 +++++++ metagpt/roles/role.py | 115 ++++++++++++++++++++++++++- metagpt/schema.py | 44 ++++++++++ metagpt/team.py | 26 ++++++ metagpt/utils/serialize.py | 62 +++++++++++++-- metagpt/utils/utils.py | 41 ++++++++++ startup.py | 41 ++++++---- tests/metagpt/actions/test_action.py | 17 ++++ tests/metagpt/memory/test_memory.py | 42 ++++++++++ tests/metagpt/roles/test_role.py | 85 ++++++++++++++++++++ tests/metagpt/test_environment.py | 29 +++++-- tests/metagpt/test_schema.py | 42 ++++++++++ tests/metagpt/test_team.py | 27 +++++++ 16 files changed, 641 insertions(+), 31 deletions(-) create mode 100644 metagpt/utils/utils.py create mode 100644 tests/metagpt/memory/test_memory.py create mode 100644 tests/metagpt/roles/test_role.py create mode 100644 tests/metagpt/test_team.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 790295d55..a538baa77 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -9,6 +9,7 @@ import re from abc import ABC from typing import Optional +import importlib from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action_output import ActionOutput @@ -16,6 +17,7 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder +from metagpt.utils.utils import import_class class Action(ABC): @@ -42,6 +44,36 @@ class Action(ABC): def __repr__(self): return self.__str__() + def serialize(self): + return { + "action_class": self.__class__.__name__, + "module_name": self.__module__, + "name": self.name + } + + @classmethod + def deserialize(cls, action_dict: dict): + action_class_str = action_dict.pop("action_class") + module_name = action_dict.pop("module_name") + action_class = import_class(action_class_str, module_name) + return action_class(**action_dict) + + @classmethod + def ser_class(cls): + """ serialize class type""" + return { + "action_class": cls.__name__, + "module_name": cls.__module__ + } + + @classmethod + def deser_class(cls, action_dict: dict): + """ deserialize class type """ + action_class_str = action_dict.pop("action_class") + module_name = action_dict.pop("module_name") + action_class = import_class(action_class_str, module_name) + return action_class + async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str: """Append default prefix""" if not system_msgs: diff --git a/metagpt/const.py b/metagpt/const.py index 407ce803a..711546d03 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -42,6 +42,7 @@ TMP = PROJECT_ROOT / "tmp" RESEARCH_PATH = DATA_PATH / "research" TUTORIAL_PATH = DATA_PATH / "tutorial_docx" INVOICE_OCR_TABLE_PATH = DATA_PATH / "invoice_table" +SERDES_PATH = WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" diff --git a/metagpt/environment.py b/metagpt/environment.py index 24e6ada2f..d1fa561f0 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -7,12 +7,14 @@ """ import asyncio from typing import Iterable +from pathlib import Path from pydantic import BaseModel, Field from metagpt.memory import Memory from metagpt.roles import Role from metagpt.schema import Message +from metagpt.utils.utils import read_json_file, write_json_file class Environment(BaseModel): @@ -28,6 +30,42 @@ class Environment(BaseModel): class Config: arbitrary_types_allowed = True + def serialize(self, stg_path: Path): + roles_path = stg_path.joinpath("roles.json") + roles_info = [] + for role_key, role in self.roles.items(): + roles_info.append({ + "role_class": role.__class__.__name__, + "module_name": role.__module__, + "role_name": role.name + }) + role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) + write_json_file(roles_path, roles_info) + + self.memory.serialize(stg_path) + history_path = stg_path.joinpath("history.json") + write_json_file(history_path, {"content": self.history}) + + def deserialize(self, stg_path: Path): + """ stg_path: ./storage/team/environment/ """ + roles_path = stg_path.joinpath("roles.json") + roles_info = read_json_file(roles_path) + for role_info in roles_info: + role_class = role_info.get("role_class") + role_name = role_info.get("role_name") + + role_path = stg_path.joinpath(f"roles/{role_class}_{role_name}") + role = Role.deserialize(role_path) + + self.add_role(role) + + memory = Memory.deserialize(stg_path) + self.memory = memory + + history_path = stg_path.joinpath("history.json") + history = read_json_file(history_path) + self.history = history.get("content") + def add_role(self, role: Role): """增加一个在当前环境的角色 Add a role in the current environment diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index c818fa707..a839bb038 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -7,9 +7,12 @@ """ from collections import defaultdict from typing import Iterable, Type +from pathlib import Path from metagpt.actions import Action from metagpt.schema import Message +from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.serialize import serialize_general_message, deserialize_general_message class Memory: @@ -20,6 +23,33 @@ class Memory: self.storage: list[Message] = [] self.index: dict[Type[Action], list[Message]] = defaultdict(list) + def serialize(self, stg_path: Path): + """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ + memory_path = stg_path.joinpath("memory.json") + + storage = [] + for message in self.storage: + # msg_dict = message.serialize() + msg_dict = serialize_general_message(message) + storage.append(msg_dict) + + write_json_file(memory_path, storage) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Memory": + """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" + memory_path = stg_path.joinpath("memory.json") + + memory = Memory() + memory_list = read_json_file(memory_path) + for message in memory_list: + # distinguish instruct_content type in message + # msg = Message.deserialize(message) + msg = deserialize_general_message(message) + memory.add(msg) + + return memory + def add(self, message: Message): """Add a new message to storage, while updating the index""" if message in self.storage: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b96c361c0..9b0613fd5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -9,8 +9,9 @@ from __future__ import annotations from typing import Iterable, Type, Union from enum import Enum - +from pathlib import Path from pydantic import BaseModel, Field +import importlib # from metagpt.environment import Environment from metagpt.config import CONFIG @@ -19,6 +20,7 @@ from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message +from metagpt.utils.utils import read_json_file, write_json_file, import_class PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -115,11 +117,101 @@ class Role: self._actions = [] self._role_id = str(self._setting) self._rc = RoleContext() + self._recovered = False + + def serialize(self, stg_path: Path): + role_info_path = stg_path.joinpath("role_info.json") + role_info = { + "role_class": self.__class__.__name__, + "module_name": self.__module__ + } + setting = self._setting.dict() + setting.pop("desc") + setting.pop("is_human") # not all inherited roles have this atrr + role_info.update(setting) + write_json_file(role_info_path, role_info) + + actions_info_path = stg_path.joinpath("actions/actions_info.json") + actions_info = [] + for action in self._actions: + actions_info.append(action.serialize()) + write_json_file(actions_info_path, actions_info) + + watches_info_path = stg_path.joinpath("watches/watches_info.json") + watches_info = [] + for watch in self._rc.watch: + watches_info.append(watch.ser_class()) + write_json_file(watches_info_path, watches_info) + + actions_todo_path = stg_path.joinpath("actions/todo.json") + actions_todo = { + "cur_state": self._rc.state, + "react_mode": self._rc.react_mode.value, + "max_react_loop": self._rc.max_react_loop + } + write_json_file(actions_todo_path, actions_todo) + + self._rc.memory.serialize(stg_path) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Role": + """ stg_path = ./storage/team/environment/roles/{role_class}_{role_name}""" + role_info_path = stg_path.joinpath("role_info.json") + role_info = read_json_file(role_info_path) + + role_class_str = role_info.pop("role_class") + module_name = role_info.pop("module_name") + role_class = import_class(class_name=role_class_str, module_name=module_name) + + role = role_class(**role_info) # initiate particular Role + actions_info_path = stg_path.joinpath("actions/actions_info.json") + actions = [] + actions_info = read_json_file(actions_info_path) + for action_info in actions_info: + action = Action.deserialize(action_info) + actions.append(action) + + watches_info_path = stg_path.joinpath("watches/watches_info.json") + watches = [] + watches_info = read_json_file(watches_info_path) + for watch_info in watches_info: + action = Action.deser_class(watch_info) + watches.append(action) + + role.init_actions(actions) + role.watch(watches) + + actions_todo_path = stg_path.joinpath("actions/todo.json") + # recover self._rc.state + actions_todo = read_json_file(actions_todo_path) + max_react_loop = actions_todo.get("max_react_loop", 1) + cur_state = actions_todo.get("cur_state", -1) + role.set_state(cur_state) + role.set_recovered(True) + react_mode_str = actions_todo.get("react_mode", RoleReactMode.REACT.value) + if react_mode_str not in RoleReactMode.values(): + logger.warning(f"ReactMode: {react_mode_str} not in {RoleReactMode.values()}, use react as default") + react_mode_str = RoleReactMode.REACT.value + role.set_react_mode(RoleReactMode(react_mode_str), max_react_loop) + + role_memory = Memory.deserialize(stg_path) + role.set_memory(role_memory) + + return role def _reset(self): self._states = [] self._actions = [] + def set_recovered(self, recovered: bool = False): + self._recovered = recovered + + def set_memory(self, memory: Memory): + self._rc.memory = memory + + def init_actions(self, actions): + self._init_actions(actions) + def _init_actions(self, actions): self._reset() for idx, action in enumerate(actions): @@ -134,6 +226,9 @@ class Role: self._actions.append(i) self._states.append(f"{idx}. {action}") + def set_react_mode(self, react_mode: RoleReactMode, max_react_loop: int = 1): + self._set_react_mode(react_mode, max_react_loop) + def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): """Set strategy of the Role reacting to observed Message. Variation lies in how this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. @@ -155,12 +250,18 @@ class Role: if react_mode == RoleReactMode.REACT: self._rc.max_react_loop = max_react_loop + def watch(self, actions: Iterable[Type[Action]]): + self._watch(actions) + def _watch(self, actions: Iterable[Type[Action]]): """Listen to the corresponding behaviors""" self._rc.watch.update(actions) # check RoleContext after adding watch actions self._rc.check(self._role_id) + def set_state(self, state: int): + self._set_state(state) + def _set_state(self, state: int): """Update the current state.""" self._rc.state = state @@ -171,6 +272,10 @@ class Role: """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env + @property + def name(self): + return self._setting.name + @property def profile(self): """Get the role description (position)""" @@ -188,6 +293,11 @@ class Role: # If there is only one action, then only this one can be performed self._set_state(0) return + if self._recovered and self._rc.state >= 0: + self._set_state(self._rc.state) # action to run from recovered state + self._recovered = False # avoid max_react_loop out of work + return + prompt = self._get_prefix() prompt += STATE_TEMPLATE.format(history=self._rc.history, states="\n".join(self._states), n_states=len(self._states) - 1, previous_state=self._rc.state) @@ -267,7 +377,8 @@ class Role: async def _act_by_order(self) -> Message: """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" - for i in range(len(self._states)): + start_idx = self._rc.state if self._rc.state >= 0 else 0 # action to run from recovered state + for i in range(start_idx, len(self._states)): self._set_state(i) rsp = await self._act() return rsp # return output from the last action diff --git a/metagpt/schema.py b/metagpt/schema.py index bdca093c2..3374a7241 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -9,10 +9,14 @@ from __future__ import annotations from dataclasses import dataclass, field from typing import Type, TypedDict +import copy from pydantic import BaseModel from metagpt.logs import logger +# from metagpt.utils.serialize import actionoutout_schema_to_mapping +# from metagpt.actions.action_output import ActionOutput +# from metagpt.actions.action import Action class RawMessage(TypedDict): @@ -38,6 +42,46 @@ class Message: def __repr__(self): return self.__str__() + # def serialize(self): + # message_cp: Message = copy.deepcopy(self) + # ic = message_cp.instruct_content + # if ic: + # # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly + # schema = ic.schema() + # mapping = actionoutout_schema_to_mapping(schema) + # + # message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + # cb = message_cp.cause_by + # if cb: + # message_cp.cause_by = cb.serialize() + # + # return message_cp.dict() + # + # @classmethod + # def deserialize(cls, message_dict: dict): + # instruct_content = message_dict.get("instruct_content") + # if instruct_content: + # ic = instruct_content + # ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + # ic_new = ic_obj(**ic["value"]) + # message_dict.instruct_content = ic_new + # cause_by = message_dict.get("cause_by") + # if cause_by: + # message_dict.cause_by = Action.deserialize(cause_by) + # + # return Message(**message_dict) + + def dict(self): + return { + "content": self.content, + "instruct_content": self.instruct_content, + "role": self.role, + "cause_by": self.cause_by, + "sent_from": self.sent_from, + "send_to": self.send_to, + "restricted_to": self.restricted_to + } + def to_dict(self) -> dict: return { "role": self.role, diff --git a/metagpt/team.py b/metagpt/team.py index 67d3ecec8..3b76e5ff4 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -5,6 +5,7 @@ @Author : alexanderwu @File : software_company.py """ +from pathlib import Path from pydantic import BaseModel, Field from metagpt.actions import BossRequirement @@ -14,6 +15,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException +from metagpt.utils.utils import read_json_file, write_json_file class Team(BaseModel): @@ -28,6 +30,30 @@ class Team(BaseModel): class Config: arbitrary_types_allowed = True + def serialize(self, stg_path: Path): + team_info_path = stg_path.joinpath("team_info.json") + write_json_file(team_info_path, { + "idea": self.idea, + "investment": self.investment + }) + + self.environment.serialize(stg_path.joinpath("environment")) + + def deserialize(self, stg_path: Path): + """ stg_path = ./storage/team """ + # recover team_info + team_info_path = stg_path.joinpath("team_info.json") + if not team_info_path.exists(): + logger.error("recover storage not exist, not to recover and continue run the old project.") + team_info = read_json_file(team_info_path) + self.investment = team_info.get("investment", 10.0) + self.idea = team_info.get("idea", "") + + # recover environment + environment_path = stg_path.joinpath("environment") + self.environment = Environment() + self.environment.deserialize(stg_path=environment_path) + def hire(self, roles: list[Role]): """Hire roles to cooperate""" self.environment.add_roles(roles) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 124176fcb..56a866f2e 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -4,13 +4,13 @@ import copy import pickle -from typing import Dict, List from metagpt.actions.action_output import ActionOutput from metagpt.schema import Message +from metagpt.actions.action import Action -def actionoutout_schema_to_mapping(schema: Dict) -> Dict: +def actionoutout_schema_to_mapping(schema: dict) -> dict: """ directly traverse the `properties` in the first level. schema structure likes @@ -35,13 +35,47 @@ def actionoutout_schema_to_mapping(schema: Dict) -> Dict: if property["type"] == "string": mapping[field] = (str, ...) elif property["type"] == "array" and property["items"]["type"] == "string": - mapping[field] = (List[str], ...) + mapping[field] = (list[str], ...) elif property["type"] == "array" and property["items"]["type"] == "array": - # here only consider the `List[List[str]]` situation - mapping[field] = (List[List[str]], ...) + # here only consider the `list[list[str]]` situation + mapping[field] = (list[list[str]], ...) return mapping +def actionoutput_mapping_to_str(mapping: dict) -> dict: + new_mapping = {} + for key, value in mapping.items(): + new_mapping[key] = str(value) + return new_mapping + + +def actionoutput_str_to_mapping(mapping: dict) -> dict: + new_mapping = {} + for key, value in mapping.items(): + if value == "(, Ellipsis)": + new_mapping[key] = (str, ...) + else: + new_mapping[key] = eval(value) # `"'(list[str], Ellipsis)"` to `(list[str], ...)` + return new_mapping + + +def serialize_general_message(message: Message) -> dict: + """ serialize Message, not to save""" + message_cp = copy.deepcopy(message) + ic = message_cp.instruct_content + if ic: + # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly + schema = ic.schema() + mapping = actionoutout_schema_to_mapping(schema) + mapping = actionoutput_mapping_to_str(mapping) + + message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + cb = message_cp.cause_by + if cb: + message_cp.cause_by = cb.ser_class() + return message_cp.dict() + + def serialize_message(message: Message): message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference ic = message_cp.instruct_content @@ -56,6 +90,24 @@ def serialize_message(message: Message): return msg_ser +def deserialize_general_message(message_dict: dict) -> Message: + """ deserialize Message, not to load""" + instruct_content = message_dict.pop("instruct_content") + cause_by = message_dict.pop("cause_by") + + message = Message(**message_dict) + if instruct_content: + ic = instruct_content + mapping = actionoutput_str_to_mapping(ic["mapping"]) + ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=mapping) + ic_new = ic_obj(**ic["value"]) + message.instruct_content = ic_new + if cause_by: + message.cause_by = Action.deser_class(cause_by) + + return message + + def deserialize_message(message_ser: str) -> Message: message = pickle.loads(message_ser) if message.instruct_content: diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py new file mode 100644 index 000000000..81ceea884 --- /dev/null +++ b/metagpt/utils/utils.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : + +from typing import Any +import json +from pathlib import Path +import importlib + + +def read_json_file(json_file: str, encoding=None) -> list[Any]: + if not Path(json_file).exists(): + raise FileNotFoundError(f"json_file: {json_file} not exist, return []") + + with open(json_file, "r", encoding=encoding) as fin: + try: + data = json.load(fin) + except Exception as exp: + raise ValueError(f"read json file: {json_file} failed") + return data + + +def write_json_file(json_file: str, data: list, encoding=None): + folder_path = Path(json_file).parent + if not folder_path.exists(): + folder_path.mkdir(parents=True, exist_ok=True) + + with open(json_file, "w", encoding=encoding) as fout: + json.dump(data, fout, ensure_ascii=False, indent=4) + + +def import_class(class_name: str, module_name: str) -> type: + module = importlib.import_module(module_name) + a_class = getattr(module, class_name) + return a_class + + +def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> object: + a_class = import_class(class_name, module_name) + class_inst = a_class(*args, **kwargs) + return class_inst diff --git a/startup.py b/startup.py index e9fbf94d3..9f753d553 100644 --- a/startup.py +++ b/startup.py @@ -4,6 +4,7 @@ import asyncio import fire +from metagpt.const import SERDES_PATH from metagpt.roles import ( Architect, Engineer, @@ -21,26 +22,32 @@ async def startup( code_review: bool = False, run_tests: bool = False, implement: bool = True, + recover_path: bool = False, ): """Run a startup. Be a boss.""" company = Team() - company.hire( - [ - ProductManager(), - Architect(), - ProjectManager(), - ] - ) + if not recover_path: + company.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) - # if implement or code_review - if implement or code_review: - # developing features: implement the idea - company.hire([Engineer(n_borg=5, use_code_review=code_review)]) + # if implement or code_review + if implement or code_review: + # developing features: implement the idea + company.hire([Engineer(n_borg=5, use_code_review=code_review)]) - if run_tests: - # developing features: run tests on the spot and identify bugs - # (bug fixing capability comes soon!) - company.hire([QaEngineer()]) + if run_tests: + # developing features: run tests on the spot and identify bugs + # (bug fixing capability comes soon!) + company.hire([QaEngineer()]) + else: + stg_path = SERDES_PATH.joinpath("team") + company.deserialize(stg_path=stg_path) + idea = company.idea # use original idea company.invest(investment) company.start_project(idea) @@ -54,6 +61,7 @@ def main( code_review: bool = True, run_tests: bool = False, implement: bool = True, + recover_path: str = None, ): """ We are a software startup comprised of AI. By investing in us, @@ -63,9 +71,10 @@ def main( a certain dollar amount to this AI company. :param n_round: :param code_review: Whether to use code review. + :param recover_path: recover the project from existing serialized storage :return: """ - asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement)) + asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement, recover_path)) if __name__ == "__main__": diff --git a/tests/metagpt/actions/test_action.py b/tests/metagpt/actions/test_action.py index 9775630cc..4468a6f6f 100644 --- a/tests/metagpt/actions/test_action.py +++ b/tests/metagpt/actions/test_action.py @@ -11,3 +11,20 @@ from metagpt.actions import Action, WritePRD, WriteTest def test_action_repr(): actions = [Action(), WriteTest(), WritePRD()] assert "WriteTest" in str(actions) + + +def test_action_serdes(): + action_info = WriteTest.ser_class() + assert action_info["action_class"] == "WriteTest" + + action_class = Action.deser_class(action_info) + assert action_class == WriteTest + + +def test_action_class_serdes(): + name = "write test" + action_info = WriteTest(name=name).serialize() + assert action_info["name"] == name + + action = Action.deserialize(action_info) + assert action.name == name diff --git a/tests/metagpt/memory/test_memory.py b/tests/metagpt/memory/test_memory.py new file mode 100644 index 000000000..bda79ded1 --- /dev/null +++ b/tests/metagpt/memory/test_memory.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of memory + +from pathlib import Path + +from metagpt.schema import Message +from metagpt.memory.memory import Memory +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.design_api import WriteDesign +from metagpt.actions.add_requirement import BossRequirement + +serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") + + +def test_memory_serdes(): + msg1 = Message(role="User", + content="write a 2048 game", + cause_by=BossRequirement) + + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + msg2 = Message(role="Architect", + instruct_content=ic_obj(**out_data), + content="system design content", + cause_by=WriteDesign) + + memory = Memory() + memory.add_batch([msg1, msg2]) + + stg_path = serdes_path.joinpath("team/environment") + memory.serialize(stg_path) + assert stg_path.joinpath("memory.json").exists() + + new_memory = Memory.deserialize(stg_path) + assert new_memory.count() == 2 + new_msg2 = new_memory.get(1)[0] + assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] + assert new_msg2.cause_by == WriteDesign + + stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py new file mode 100644 index 000000000..a19ad9cb5 --- /dev/null +++ b/tests/metagpt/roles/test_role.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of Role + +from pathlib import Path +import shutil +import pytest + +from metagpt.roles.role import Role, RoleReactMode +from metagpt.actions.action import Action +from metagpt.schema import Message +from metagpt.actions.add_requirement import BossRequirement +from metagpt.roles.product_manager import ProductManager + +serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") + + +def test_role_serdes(): + stg_path_prefix = serdes_path.joinpath("team/environment/roles/") + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + pm = ProductManager() + role_tag = f"{pm.__class__.__name__}_{pm.name}" + stg_path = stg_path_prefix.joinpath(role_tag) + pm.serialize(stg_path) + assert stg_path.joinpath("actions/actions_info.json").exists() + + new_pm = Role.deserialize(stg_path) + assert new_pm.name == pm.name + assert len(new_pm.get_memories(1)) == 0 + + +class ActionOK(Action): + + async def run(self, messages: list["Message"]): + return "ok" + + +class ActionRaise(Action): + + async def run(self, messages: list["Message"]): + raise RuntimeError("parse error") + + +class RoleA(Role): + + def __init__(self, + name: str = "RoleA", + profile: str = "Role A", + goal: str = "", + constraints: str = ""): + super(RoleA, self).__init__(name=name, profile=profile, goal=goal, constraints=constraints) + self._init_actions([ActionOK, ActionRaise]) + self._watch([BossRequirement]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None, stg_path: str = None): + try: + await super(RoleA, self).run(message) + except Exception as exp: + print("exp ", exp) + self.serialize(stg_path) + + +@pytest.mark.asyncio +async def test_role_serdes_interrupt(): + role_a = RoleA() + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + stg_path = serdes_path.joinpath(f"team/environment/roles/{role_a.__class__.__name__}_{role_a.name}") + await role_a.run( + message=Message(content="demo", cause_by=BossRequirement), + stg_path=stg_path + ) + assert role_a._rc.memory.count() == 2 + + assert stg_path.joinpath("actions/todo.json").exists() + + new_role_a: Role = Role.deserialize(stg_path) + assert new_role_a._rc.state == 1 + await role_a.run( + message=Message(content="demo", cause_by=BossRequirement), + stg_path=stg_path + ) + diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index a0f1f6257..3cc2d8a7a 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -7,13 +7,18 @@ """ import pytest +from pathlib import Path +import shutil from metagpt.actions import BossRequirement from metagpt.environment import Environment from metagpt.logs import logger -from metagpt.manager import Manager from metagpt.roles import Architect, ProductManager, Role from metagpt.schema import Message +from tests.metagpt.roles.test_role import RoleA + + +serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") @pytest.fixture @@ -36,21 +41,29 @@ def test_get_roles(env: Environment): assert roles == {role1.profile: role1, role2.profile: role2} -def test_set_manager(env: Environment): - manager = Manager() - env.set_manager(manager) - assert env.manager == manager - - @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): product_manager = ProductManager("Alice", "Product Manager", "做AI Native产品", "资源有限") architect = Architect("Bob", "Architect", "设计一个可用、高效、较低成本的系统,包括数据结构与接口", "资源有限,需要节省成本") env.add_roles([product_manager, architect]) - env.set_manager(Manager()) env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement)) await env.run(k=2) logger.info(f"{env.history=}") assert len(env.history) > 10 + + +def test_environment_serdes(): + environment = Environment() + role_a = RoleA() + + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + stg_path = serdes_path.joinpath("team/environment") + environment.add_role(role_a) + environment.serialize(stg_path) + + new_env: Environment = Environment() + new_env.deserialize(stg_path) + assert len(new_env.roles) == 1 diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 12666e0d3..f515326e8 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -5,7 +5,11 @@ @Author : alexanderwu @File : test_schema.py """ + from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.write_code import WriteCode +from metagpt.utils.serialize import serialize_general_message, deserialize_general_message def test_messages(): @@ -19,3 +23,41 @@ def test_messages(): text = str(msgs) roles = ['user', 'system', 'assistant', 'QA'] assert all([i in text for i in roles]) + + +def test_message_serdes(): + out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} + out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + + message = Message( + content="code", + instruct_content=ic_obj(**out_data), + role="engineer", + cause_by=WriteCode + ) + message_dict = serialize_general_message(message) + assert message_dict["cause_by"] == {"action_class": "WriteCode"} + assert message_dict["instruct_content"] == { + "class": "code", + "mapping": { + "field3": "(, Ellipsis)", + "field4": "(list[str], Ellipsis)" + }, + "value": { + "field3": "field3 value3", + "field4": ["field4 value1", "field4 value2"] + } + } + + new_message = deserialize_general_message(message_dict) + assert new_message.content == message.content + assert new_message.instruct_content == message.instruct_content + assert new_message.cause_by == message.cause_by + assert new_message.instruct_content.field3 == out_data["field3"] + + message = Message(content="code") + message_dict = serialize_general_message(message) + new_message = deserialize_general_message(message_dict) + assert new_message.instruct_content is None + assert new_message.cause_by == "" diff --git a/tests/metagpt/test_team.py b/tests/metagpt/test_team.py new file mode 100644 index 000000000..ab201152c --- /dev/null +++ b/tests/metagpt/test_team.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of team + +from pathlib import Path +import shutil + +from metagpt.team import Team + +from tests.metagpt.roles.test_role import RoleA + +serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") + + +def test_team_serdes(): + company = Team() + company.hire([RoleA()]) + + stg_path = serdes_path.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company.serialize(stg_path=stg_path) + + new_company = Team() + new_company.deserialize(stg_path) + + assert len(new_company.environment.roles) == 1 From 39e4aa98ab6101ee1016cc8584c4f36977498077 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 28 Nov 2023 10:47:19 +0800 Subject: [PATCH 193/413] fix role and format ut of serialize_deserialize --- metagpt/roles/role.py | 29 +++++-------------- .../serialize_deserialize/test_actions.py | 2 ++ .../test_architect_deserialize.py | 2 ++ .../test_product_manager.py | 1 + .../test_project_manager.py | 2 ++ .../serialize_deserialize/test_role.py | 2 +- .../serialize_deserialize/test_wrire_prd.py | 4 +-- .../serialize_deserialize/test_write_code.py | 6 +++- .../test_write_design.py | 6 +++- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index eb5539f43..e9371c2c0 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -6,16 +6,11 @@ @File : role.py """ -import sys from enum import Enum -import importlib +from pathlib import Path from __future__ import annotations -from types import SimpleNamespace from typing import ( - Dict, - Optional, - Union, Iterable, Type ) @@ -30,6 +25,7 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message +from metagpt.provider.human_provider import HumanProvider from metagpt.utils.utils import read_json_file, write_json_file, import_class PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -133,11 +129,11 @@ class Role(BaseModel): _rc: RoleContext = RoleContext() _private_attributes = { - "_setting': _setting, - "_role_id': _role_id, - '_states': [], - '_actions': [], - '_actions_type': [] # 用于记录和序列化 + "_setting": _setting, + "_role_id": _role_id, + "_states": [], + "_actions": [], + "_actions_type": [] # 用于记录和序列化 } class Config: @@ -162,17 +158,6 @@ class Role(BaseModel): object.__setattr__(self, '_states', []) object.__setattr__(self, '_actions', []) - @staticmethod - def _process_class(class_str, module_name): - cleaned_string = re.sub(r"[<>']", "", class_str).replace("class ", "") - package_name = "metagpt" - file_name = cleaned_string.replace(package_name, "").replace("." + module_name, "") - print(file_name) - # print("\n", sys.modules) - module_file = import_module(file_name, package=package_name) - module = getattr(module_file, module_name) - return module - def serialize(self, stg_path: Path): role_info_path = stg_path.joinpath("role_info.json") role_info = { diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py index e2efa982b..2fec2121a 100644 --- a/tests/metagpt/serialize_deserialize/test_actions.py +++ b/tests/metagpt/serialize_deserialize/test_actions.py @@ -7,12 +7,14 @@ import pytest from metagpt.actions import Action from metagpt.llm import LLM + def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + @pytest.mark.asyncio async def test_action_deserialize(): action = Action() diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index cff1bbadd..d0ee3bc99 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -7,6 +7,7 @@ import pytest from metagpt.roles.architect import Architect from metagpt.actions.action import Action + def test_architect_serialize(): role = Architect() ser_role_dict = role.dict(by_alias=True) @@ -14,6 +15,7 @@ def test_architect_serialize(): assert "_states" in ser_role_dict assert "_actions" in ser_role_dict + @pytest.mark.asyncio async def test_architect_deserialize(): role = Architect() diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 978c50e5e..2aed87a28 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -8,6 +8,7 @@ from metagpt.roles.product_manager import ProductManager from metagpt.actions.action import Action from metagpt.schema import Message + @pytest.mark.asyncio async def test_product_manager_deserialize(): role = ProductManager() diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index 590bd8109..fbc0dcc08 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -7,6 +7,7 @@ import pytest from metagpt.roles.project_manager import ProjectManager from metagpt.actions.action import Action + def test_project_manager_serialize(): role = ProjectManager() ser_role_dict = role.dict(by_alias=True) @@ -14,6 +15,7 @@ def test_project_manager_serialize(): assert "_states" in ser_role_dict assert "_actions" in ser_role_dict + @pytest.mark.asyncio async def test_project_manager_deserialize(): role = ProjectManager() diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 432c9acb7..0e438d1a2 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -34,7 +34,7 @@ async def test_engineer_deserialize(): # also can be deserialized in this way: new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" - assert new_role.use_code_review == True + assert new_role.use_code_review is True assert len(new_role._actions) == 2 assert isinstance(new_role._actions[0], Action) assert isinstance(new_role._actions[1], Action) diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 9b2653820..baa08ed76 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -24,5 +24,5 @@ async def test_action_deserialize(): # new_action = WritePRD().deserialize(serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - assert len(await new_action.run([Message(content="write a cli snake game")]))>0 - + assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 + diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 0b1f1dc7c..9d659caaf 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -7,18 +7,21 @@ import pytest from metagpt.actions import WriteCode, WriteCodeReview from metagpt.llm import LLM + def test_write_design_serialize(): action = WriteCode() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCode" assert "llm" in ser_action_dict + def test_write_task_serialize(): action = WriteCodeReview() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCodeReview" assert "llm" in ser_action_dict - + + @pytest.mark.asyncio async def test_write_code_deserialize(): action = WriteCode() @@ -29,6 +32,7 @@ async def test_write_code_deserialize(): assert new_action.llm == LLM() await new_action.run(context="write a cli snake game", filename="test_code") + @pytest.mark.asyncio async def test_write_code_review_deserialize(): action = WriteCodeReview() diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 56bf78a63..e6e236676 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -7,18 +7,21 @@ import pytest from metagpt.actions import WriteDesign, WriteTasks from metagpt.llm import LLM + def test_write_design_serialize(): action = WriteDesign() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + def test_write_task_serialize(): action = WriteTasks() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + @pytest.mark.asyncio async def test_write_design_deserialize(): action = WriteDesign() @@ -28,6 +31,7 @@ async def test_write_design_deserialize(): assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") + @pytest.mark.asyncio async def test_write_task_deserialize(): action = WriteTasks() @@ -36,4 +40,4 @@ async def test_write_task_deserialize(): # new_action = WriteTasks().deserialize(serialized_data) assert new_action.name == "CreateTasks" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game") \ No newline at end of file + await new_action.run(context="write a cli snake game") From b794e5d73dc47722996508af4824b1e5496869a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 28 Nov 2023 16:22:40 +0800 Subject: [PATCH 194/413] feat: fix memory.add --- metagpt/roles/engineer.py | 4 ++-- metagpt/roles/role.py | 4 +++- metagpt/roles/sk_agent.py | 1 - 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index c0e1b8a10..ffd96849b 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -168,7 +168,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=self._rc.todo, - send_to="Edward", + send_to="Edward", # name of QaEngineer ) return msg @@ -209,7 +209,7 @@ class Engineer(Role): content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=self._rc.todo, - send_to="Edward", + send_to="Edward", # name of QaEngineer ) return msg diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index e1f43ef3a..dbf800c03 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -217,6 +217,7 @@ class Role: ) else: msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) + self._rc.memory.add(msg) return msg @@ -227,7 +228,8 @@ class Role: # Store the read messages in your own memory to prevent duplicate processing. self._rc.memory.add_batch(news) # Filter out messages of interest. - self._rc.news = [n for n in news if n.cause_by in self._rc.watch] + old_messages = self._rc.memory.get() + self._rc.news = [n for n in news if n.cause_by in self._rc.watch and n not in old_messages] # Design Rules: # If you need to further categorize Message objects, you can do so using the Message.set_meta function. diff --git a/metagpt/roles/sk_agent.py b/metagpt/roles/sk_agent.py index 15b18dd3e..2443b8b58 100644 --- a/metagpt/roles/sk_agent.py +++ b/metagpt/roles/sk_agent.py @@ -74,5 +74,4 @@ class SkAgent(Role): msg = Message(content=result, role=self.profile, cause_by=self._rc.todo) self._rc.memory.add(msg) - self.publish_message(msg) return msg From 9745dd12f6bdb27bc5bbc16401605c3f9bbe688c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 28 Nov 2023 16:28:53 +0800 Subject: [PATCH 195/413] feat: fix memory.add --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index dbf800c03..62c8b7708 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -226,9 +226,9 @@ class Role: # Read unprocessed messages from the msg buffer. news = self._rc.msg_buffer.pop_all() # Store the read messages in your own memory to prevent duplicate processing. + old_messages = self._rc.memory.get() self._rc.memory.add_batch(news) # Filter out messages of interest. - old_messages = self._rc.memory.get() self._rc.news = [n for n in news if n.cause_by in self._rc.watch and n not in old_messages] # Design Rules: From 49f0b5e9f140c5d1a9a1b88289a0488a465601b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 28 Nov 2023 16:41:32 +0800 Subject: [PATCH 196/413] feat: fix memory.add --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index f7de58d5a..1c9da7e6c 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -214,7 +214,7 @@ class Role: ) else: msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) - self._rc.memory.add(msg) + self._rc.memory.add(msg) return msg From f2de34fdad26a31def47a136f7ed7f73fa58ddf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 28 Nov 2023 16:42:15 +0800 Subject: [PATCH 197/413] feat: fix memory.add --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 62c8b7708..424a28c6f 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -217,7 +217,7 @@ class Role: ) else: msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) - self._rc.memory.add(msg) + self._rc.memory.add(msg) return msg From db9e900838b2c8eac4558fc858ce4a4e0cee6e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 09:52:26 +0800 Subject: [PATCH 198/413] feat: merge geekan:cli-etc --- metagpt/llm.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/llm.py b/metagpt/llm.py index 14bbad1b4..a35ba354b 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -8,10 +8,13 @@ from metagpt.config import CONFIG from metagpt.provider.anthropic_api import Claude2 as Claude +from metagpt.provider.human_provider import HumanProvider from metagpt.provider.openai_api import OpenAIGPTAPI from metagpt.provider.spark_api import SparkAPI from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI +_ = HumanProvider() + def LLM() -> "BaseGPTAPI": """initialize different LLM instance according to the key field existence""" From f564bb540a97cc76229a46d51d0cde21980770ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 09:52:55 +0800 Subject: [PATCH 199/413] feat: merge geekan:cli-etc --- metagpt/llm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index a35ba354b..d8d06c0a1 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -13,7 +13,7 @@ from metagpt.provider.openai_api import OpenAIGPTAPI from metagpt.provider.spark_api import SparkAPI from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI -_ = HumanProvider() +_ = HumanProvider() # Avoid pre-commit error def LLM() -> "BaseGPTAPI": From eff1cb7dc1ef842fc55d9118386052eadb98cf93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 10:14:04 +0800 Subject: [PATCH 200/413] feat: Add 'id' to 'Message' according to Section 2.2.3.1.1 of RFC 135. --- metagpt/schema.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/metagpt/schema.py b/metagpt/schema.py index 9e5854997..d1174799a 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -10,12 +10,14 @@ 1. Add `Document` and `Documents` for `FileRepository` in Section 2.2.3.4 of RFC 135. 2. Encapsulate the common key-values set to pydantic structures to standardize and unify parameter passing between actions. + 3. Add `id` to `Message` according to Section 2.2.3.1.1 of RFC 135. """ from __future__ import annotations import asyncio import json import os.path +import uuid from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path @@ -86,6 +88,7 @@ class Documents(BaseModel): class Message(BaseModel): """list[: ]""" + id: str # According to Section 2.2.3.1.1 of RFC 135 content: str instruct_content: BaseModel = Field(default=None) role: str = "user" # system / user / assistant @@ -113,6 +116,7 @@ class Message(BaseModel): :param role: Message meta info tells who sent this message. """ super().__init__( + id=uuid.uuid4().hex, content=content, instruct_content=instruct_content, role=role, From 7b44fccf8d826aa881b2fdd1765343a9a7207c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 16:22:05 +0800 Subject: [PATCH 201/413] feat: merge geekan:cli-etc --- metagpt/actions/prepare_documents.py | 7 ++- metagpt/startup.py | 16 +++++- metagpt/team.py | 11 ++-- metagpt/utils/git_repository.py | 4 +- startup.py | 80 ---------------------------- 5 files changed, 26 insertions(+), 92 deletions(-) delete mode 100644 startup.py diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 30558c93f..fe954b79c 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -12,7 +12,7 @@ from pathlib import Path from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME, WORKSPACE_ROOT +from metagpt.const import DEFAULT_WORKSPACE_ROOT, DOCS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.schema import Document from metagpt.utils.file_repository import FileRepository from metagpt.utils.git_repository import GitRepository @@ -28,8 +28,11 @@ class PrepareDocuments(Action): return ActionOutput(content=doc.json(exclue="content"), instruct_content=doc) # Create and initialize the workspace folder, initialize the Git environment. + default_workspace_root = CONFIG.project_path or DEFAULT_WORKSPACE_ROOT + default_project_name = CONFIG.project_name or FileRepository.new_filename() + default_workdir = Path(default_workspace_root) / default_project_name CONFIG.git_repo = GitRepository() - workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_filename() + workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else default_workdir CONFIG.git_repo.open(local_path=workdir, auto_init=True) # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. diff --git a/metagpt/startup.py b/metagpt/startup.py index 35b9b8b66..de348780b 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -4,6 +4,8 @@ import asyncio import typer +from metagpt.config import CONFIG + app = typer.Typer() @@ -17,6 +19,10 @@ def startup( implement: bool = typer.Option(True, help="Enable or disable code implementation."), project_name: str = typer.Option("", help="Unique project name, such as 'game_2048'."), inc: bool = typer.Option(False, help="Incremental mode. Use it to coop with existing repo."), + project_path: str = typer.Option( + help="Specify the directory path of the old version project to fulfill the " "incremental requirements." + ), + reqa_file: str = typer.Option(help="Specify the source file name for rewriting the quality test code."), ): """Run a startup. Be a boss.""" from metagpt.roles import ( @@ -28,6 +34,12 @@ def startup( ) from metagpt.team import Team + # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. + CONFIG.project_name = project_name + CONFIG.inc = inc + CONFIG.project_path = project_path + CONFIG.reqa_file = reqa_file + company = Team() company.hire( [ @@ -44,9 +56,9 @@ def startup( company.hire([QaEngineer()]) company.invest(investment) - company.run_project(idea, project_name=project_name, inc=inc) + company.run_project(idea) asyncio.run(company.run(n_round=n_round)) if __name__ == "__main__": - startup(idea="Make a 2048 game.") + app() diff --git a/metagpt/team.py b/metagpt/team.py index e252935c4..92f379c97 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -11,6 +11,7 @@ from pydantic import BaseModel, Field from metagpt.actions import UserRequirement from metagpt.config import CONFIG +from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Role @@ -45,16 +46,14 @@ class Team(BaseModel): if CONFIG.total_cost > CONFIG.max_budget: raise NoMoneyException(CONFIG.total_cost, f"Insufficient funds: {CONFIG.max_budget}") - def run_project(self, idea, send_to: str = "", project_name: str = "", inc: bool = False): + def run_project(self, idea, send_to: str = ""): """Start a project from publishing user requirement.""" self.idea = idea - # If user set project_name, then use it. - if project_name: - path = CONFIG.workspace_path / project_name - self.env.load_existing_repo(path, inc=inc) # Human requirement. - self.env.publish_message(Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to)) + self.env.publish_message( + Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to or MESSAGE_ROUTE_TO_ALL) + ) def _save(self): logger.info(self.json(ensure_ascii=False)) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index b8e35199b..b1cfe1ed2 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -16,7 +16,7 @@ from typing import Dict from git.repo import Repo from git.repo.fun import is_git_dir -from metagpt.const import WORKSPACE_ROOT +from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.utils.dependency_file import DependencyFile from metagpt.utils.file_repository import FileRepository @@ -201,7 +201,7 @@ class GitRepository: if __name__ == "__main__": - path = WORKSPACE_ROOT / "git" + path = DEFAULT_WORKSPACE_ROOT / "git" path.mkdir(exist_ok=True, parents=True) repo = GitRepository() diff --git a/startup.py b/startup.py deleted file mode 100644 index 1a59e7fa2..000000000 --- a/startup.py +++ /dev/null @@ -1,80 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import asyncio - -import fire - -from metagpt.config import CONFIG -from metagpt.roles import ( - Architect, - Engineer, - ProductManager, - ProjectManager, - QaEngineer, -) -from metagpt.software_company import SoftwareCompany - - -async def startup( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = False, - run_tests: bool = False, - implement: bool = True, -): - """Run a startup. Be a boss.""" - company = SoftwareCompany() - company.hire( - [ - ProductManager(), - Architect(), - ProjectManager(), - ] - ) - - # if implement or code_review - if implement or code_review: - # developing features: implement the idea - company.hire([Engineer(n_borg=5, use_code_review=code_review)]) - - if run_tests: - # developing features: run tests on the spot and identify bugs - # (bug fixing capability comes soon!) - company.hire([QaEngineer()]) - - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) - - -def main( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = True, - run_tests: bool = False, - implement: bool = True, - project_path: str = None, - reqa_file: str = None, -): - """ - We are a software startup comprised of AI. By investing in us, - you are empowering a future filled with limitless possibilities. - :param idea: Your innovative idea, such as "Creating a snake game." - :param investment: As an investor, you have the opportunity to contribute - a certain dollar amount to this AI company. - :param n_round: - :param code_review: Whether to use code review. - :param run_tests: Whether run unit tests. - :param implement: Whether to write codes. - :param project_path: The path of the old version project to improve. - :return: - """ - CONFIG.WORKDIR = project_path - CONFIG.REQA_FILENAME = reqa_file - asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement)) - - -if __name__ == "__main__": - fire.Fire(main) From 94043a89f41fa5da81d1fc56e0a1866423ae87d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 20:12:03 +0800 Subject: [PATCH 202/413] feat: merge geekan:cli-etc --- metagpt/actions/prepare_documents.py | 8 ++------ metagpt/startup.py | 19 ++++++++++--------- metagpt/utils/file_repository.py | 4 ++-- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index fe954b79c..71c94d25a 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -8,8 +8,6 @@ RFC 135 2.2.3.5.1. """ -from pathlib import Path - from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG from metagpt.const import DEFAULT_WORKSPACE_ROOT, DOCS_FILE_REPO, REQUIREMENT_FILENAME @@ -28,11 +26,9 @@ class PrepareDocuments(Action): return ActionOutput(content=doc.json(exclue="content"), instruct_content=doc) # Create and initialize the workspace folder, initialize the Git environment. - default_workspace_root = CONFIG.project_path or DEFAULT_WORKSPACE_ROOT - default_project_name = CONFIG.project_name or FileRepository.new_filename() - default_workdir = Path(default_workspace_root) / default_project_name + project_name = CONFIG.project_name or FileRepository.new_filename() + workdir = CONFIG.project_path or DEFAULT_WORKSPACE_ROOT / project_name CONFIG.git_repo = GitRepository() - workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else default_workdir CONFIG.git_repo.open(local_path=workdir, auto_init=True) # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. diff --git a/metagpt/startup.py b/metagpt/startup.py index de348780b..78f32d556 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -12,17 +12,18 @@ app = typer.Typer() @app.command() def startup( idea: str = typer.Argument(..., help="Your innovative idea, such as 'Create a 2048 game.'"), - investment: float = typer.Option(3.0, help="Dollar amount to invest in the AI company."), - n_round: int = typer.Option(5, help="Number of rounds for the simulation."), - code_review: bool = typer.Option(True, help="Whether to use code review."), - run_tests: bool = typer.Option(False, help="Whether to enable QA for adding & running tests."), - implement: bool = typer.Option(True, help="Enable or disable code implementation."), - project_name: str = typer.Option("", help="Unique project name, such as 'game_2048'."), - inc: bool = typer.Option(False, help="Incremental mode. Use it to coop with existing repo."), + investment: float = typer.Option(default=3.0, help="Dollar amount to invest in the AI company."), + n_round: int = typer.Option(default=5, help="Number of rounds for the simulation."), + code_review: bool = typer.Option(default=True, help="Whether to use code review."), + run_tests: bool = typer.Option(default=False, help="Whether to enable QA for adding & running tests."), + implement: bool = typer.Option(default=True, help="Enable or disable code implementation."), + project_name: str = typer.Option(default="", help="Unique project name, such as 'game_2048'."), + inc: bool = typer.Option(default=False, help="Incremental mode. Use it to coop with existing repo."), project_path: str = typer.Option( - help="Specify the directory path of the old version project to fulfill the " "incremental requirements." + default="", + help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", ), - reqa_file: str = typer.Option(help="Specify the source file name for rewriting the quality test code."), + reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), ): """Run a startup. Be a boss.""" from metagpt.roles import ( diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 846bfcd0c..0815bf90a 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -54,7 +54,7 @@ class FileRepository: """ pathname = self.workdir / filename pathname.parent.mkdir(parents=True, exist_ok=True) - async with aiofiles.open(str(pathname), mode="wb") as writer: + async with aiofiles.open(str(pathname), mode="w") as writer: await writer.write(content) logger.info(f"save to: {str(pathname)}") @@ -98,7 +98,7 @@ class FileRepository: if not path_name.exists(): return None try: - async with aiofiles.open(str(path_name), mode="rb") as reader: + async with aiofiles.open(str(path_name), mode="r") as reader: doc.content = await reader.read() except FileNotFoundError as e: logger.info(f"open {str(path_name)} failed:{e}") From 09b6d2df8377883335696c50f21d8956518f75ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 20:26:26 +0800 Subject: [PATCH 203/413] feat: merge geekan:cli-etc --- metagpt/actions/design_api.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 431879c25..2b9c20047 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -281,11 +281,7 @@ class WriteDesign(Action): # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" # contain space, have to use setattr - setattr( - system_design.instruct_content, - "project_name", - system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), - ) + self._rename_project_name(system_design=system_design) await self._rename_workspace(system_design) # ======= # # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" contain space, have to use setattr @@ -303,17 +299,29 @@ class WriteDesign(Action): system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python # package name" contain space, have to use setattr + self._rename_project_name(system_design=system_design) + system_design_doc.content = system_design.instruct_content.json(ensure_ascii=False) + return system_design_doc + + @staticmethod + def _rename_project_name(system_design): + if CONFIG.project_name: + setattr( + system_design.instruct_content, + "project_name", + CONFIG.project_name, + ) + return setattr( system_design.instruct_content, "project_name", system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), ) - system_design_doc.content = system_design.instruct_content.json(ensure_ascii=False) - return system_design_doc @staticmethod async def _rename_workspace(system_design): - if CONFIG.WORKDIR: # Updating on the old version has already been specified if it's valid. + if CONFIG.project_path: # Updating on the old version has already been specified if it's valid. According to + # Section 2.2.3.10 of RFC 135 return if isinstance(system_design, ActionOutput): From 6fa3deef00ad06c68c77d68e60b349e0d0137ebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 20:58:41 +0800 Subject: [PATCH 204/413] feat: merge geekan:cli-etc --- metagpt/actions/write_code.py | 28 ++++++++++++---------------- metagpt/utils/git_repository.py | 1 + 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index e373b1127..0cd41c52f 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -31,22 +31,6 @@ Role: You are a professional engineer; the main goal is to write PEP8 compliant, Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". ------ -# Context -{context} ------ - -## Code: {filename} Write code with triple quoto, based on the following list and context. -1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets -3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. -5. Think before writing: What should be implemented and provided in this document? -6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. -7. Do not use public member functions that do not exist in your design. -8. Before using a variable, make sure you reference it first -9. Write out EVERY DETAIL, DON'T LEAVE TODO. - ----- # Design ```json @@ -68,6 +52,18 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc {logs} ``` ----- + +## Code: {filename} Write code with triple quoto, based on the following list and context. +1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. +2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets +3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. +4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. +5. Think before writing: What should be implemented and provided in this document? +6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. +7. Do not use public member functions that do not exist in your design. +8. Before using a variable, make sure you reference it first +9. Write out EVERY DETAIL, DON'T LEAVE TODO. + ## Format example ----- ## Code: {filename} diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index b1cfe1ed2..7c9ec645f 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -59,6 +59,7 @@ class GitRepository: :param local_path: The local path to the Git repository. :param auto_init: If True, automatically initializes a new Git repository if the provided path is not a Git repository. """ + local_path = Path(local_path) if self.is_git_dir(local_path): self._repository = Repo(local_path) return From 810768a3505bb06dc9ae76024073432c671269ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 21:34:29 +0800 Subject: [PATCH 205/413] feat: merge geekan:cli-etc --- metagpt/actions/design_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 2b9c20047..3e17239b0 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -172,7 +172,7 @@ Max Output: 8192 chars or 2048 tokens. Try to use them up. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores +## Python package name: Provide as Python str with python triple quote, concise and clear, characters only use a combination of all lowercase and underscores ## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here From cd24931b65b4a6ca9e926174c0a86dbdc3b1856c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 22:06:40 +0800 Subject: [PATCH 206/413] feat: merge geekan:cli-etc --- metagpt/actions/design_api.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 3e17239b0..e31ea76a8 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -166,24 +166,22 @@ MERGE_PROMPT = """ {context} ----- -Role: You are an architect; The goal is to incrementally update the "Old Design" based on the information provided by the "Context," aiming to design a state-of-the-art (SOTA) Python system compliant with PEP8. Additionally, the objective is to optimize the use of high-quality open-source tools. -Requirement: Fill in the following missing information based on the context, each section name is a key in json -Max Output: 8192 chars or 2048 tokens. Try to use them up. +Role: You are an architect; The goal is to incrementally update the "Old Design" based on the information provided by the "Context," aiming to design a SOTA PEP8-compliant python system; make the best use of good open source tools +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. +Requirement: Fill in the following missing information based on the context, note that all sections are response with code form separately +ATTENTION: Output carefully referenced "Old Design" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Python str with python triple quote, concise and clear, characters only use a combination of all lowercase and underscores +## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores -## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here +## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here -## Data structures and interface definitions: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. +## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. ## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. -## Anything UNCLEAR: Provide as Plain text. Make clear here. - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Design" format, -and only output the json inside this tag, nothing else +## Anything UNCLEAR: Provide as Plain text. Try to clarify it. """ From e34b8bbf0bbb9d46757da1eb33883fd8b554df56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 22:13:10 +0800 Subject: [PATCH 207/413] feat: merge geekan:cli-etc --- metagpt/actions/write_prd.py | 47 ++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index ab216b7a0..c61684918 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -261,24 +261,41 @@ MERGE_PROMPT = """ {old_prd} ----- Role: You are a professional product manager; The goal is to merge the newly added requirements into the existing PRD in order to design a concise, usable, and efficient product. +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design +ATTENTION: Output carefully referenced "Old PRD" in format. -## Original Requirements: Provide as Plain text, place the polished complete original requirements here +## YOU NEED TO FULFILL THE BELOW JSON DOC -## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. If the requirement itself is simple, the goal should also be simple - -## User Stories: Provided as Python list[str], up to 5 scenario-based user stories, If the requirement itself is simple, the user stories should also be less - -## Competitive Analysis: Provided as Python list[str], up to 7 competitive product analyses, consider as similar competitors as possible - -## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. - -## Requirement Analysis: Provide as Plain text. Be simple. LESS IS MORE. Make your requirements less dumb. Delete the parts unnessasery. - -## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards; no more than 5 requirements and consider to make its difficulty lower - -## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. -## Anything UNCLEAR: Provide as Plain text. Make clear here. +{{ + "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. + "Original Requirements": "", # str, place the polished complete original requirements here + "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Search Information": "", + "Requirements": "", + "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. + "User Stories": [], # Provided as Python list[str], up to 5 scenario-based user stories + "Competitive Analysis": [], # Provided as Python list[str], up to 8 competitive product analyses + # Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. + "Competitive Quadrant Chart": "quadrantChart + title Reach and engagement of campaigns + x-axis Low Reach --> High Reach + y-axis Low Engagement --> High Engagement + quadrant-1 We should expand + quadrant-2 Need to promote + quadrant-3 Re-evaluate + quadrant-4 May be improved + Campaign A: [0.3, 0.6] + Campaign B: [0.45, 0.23] + Campaign C: [0.57, 0.69] + Campaign D: [0.78, 0.34] + Campaign E: [0.40, 0.34] + Campaign F: [0.35, 0.78]", + "Requirement Analysis": "", # Provide as Plain text. + "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], # Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards + "UI Design draft": "", # Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. + "Anything UNCLEAR": "", # Provide as Plain text. Try to clarify it. +}} output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old PRD" format, and only output the json inside this tag, nothing else From 6e0fc042258c005a8842cff66923d6bb76aa0e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 29 Nov 2023 22:25:47 +0800 Subject: [PATCH 208/413] feat: merge geekan:cli-etc --- metagpt/actions/design_api.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index e31ea76a8..7164cef26 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -182,6 +182,9 @@ ATTENTION: Output carefully referenced "Old Design" in format. ## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. ## Anything UNCLEAR: Provide as Plain text. Try to clarify it. + +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Design" format, +and only output the json inside this tag, nothing else """ From 5ef3076f20ee9d3591090da709e33f9667026711 Mon Sep 17 00:00:00 2001 From: Auster Cid Date: Wed, 29 Nov 2023 12:04:05 -0300 Subject: [PATCH 209/413] reimplemented retries following suggestions in OpenAI's cookbook --- metagpt/actions/action.py | 4 ++-- metagpt/actions/write_code.py | 4 ++-- metagpt/actions/write_code_review.py | 4 ++-- metagpt/provider/openai_api.py | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index f1a267468..6bdcc027d 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -9,7 +9,7 @@ import re from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_exponential +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM @@ -49,7 +49,7 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def _aask_v1( self, prompt: str, diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index b9b2ab228..a5dc8e059 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -11,7 +11,7 @@ from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_exponential, wait_exponential +from tenacity import retry, stop_after_attempt, wait_random_exponential PROMPT_TEMPLATE = """ NOTICE @@ -66,7 +66,7 @@ class WriteCode(Action): code_path.write_text(code) logger.info(f"Saving Code to {code_path}") - @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt): code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 84ccc96fc..06282411a 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -10,7 +10,7 @@ from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.schema import Message from metagpt.utils.common import CodeParser -from tenacity import retry, stop_after_attempt, wait_exponential +from tenacity import retry, stop_after_attempt, wait_random_exponential PROMPT_TEMPLATE = """ NOTICE @@ -65,7 +65,7 @@ class WriteCodeReview(Action): def __init__(self, name="WriteCodeReview", context: list[Message] = None, llm=None): super().__init__(name, context, llm) - @retry(stop=stop_after_attempt(4), wait=wait_exponential(10,60,3)) + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt): code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index fce19c16e..fa9397f20 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -15,7 +15,7 @@ from tenacity import ( retry, retry_if_exception_type, stop_after_attempt, - wait_exponential, + wait_random_exponential, ) from metagpt.config import CONFIG @@ -226,8 +226,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return await self._achat_completion(messages) @retry( - stop=stop_after_attempt(4), - wait=wait_exponential(10,60,3), + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), after=after_log(logger, logger.level("WARNING").name), retry=retry_if_exception_type(APIConnectionError), retry_error_callback=log_and_reraise, From 4928a896ca8b357bdfbc6d08d3b72d86f4598995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 10:16:34 +0800 Subject: [PATCH 210/413] feat: merge geekan:cli-etc --- metagpt/actions/prepare_documents.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 71c94d25a..b339d897d 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -7,6 +7,7 @@ @Desc: PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt. RFC 135 2.2.3.5.1. """ +import shutil from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG @@ -28,6 +29,8 @@ class PrepareDocuments(Action): # Create and initialize the workspace folder, initialize the Git environment. project_name = CONFIG.project_name or FileRepository.new_filename() workdir = CONFIG.project_path or DEFAULT_WORKSPACE_ROOT / project_name + if not CONFIG.inc and workdir.exists(): + shutil.rmtree(workdir) CONFIG.git_repo = GitRepository() CONFIG.git_repo.open(local_path=workdir, auto_init=True) From 6010ce70f651d514db06f0014f07d12ec2e7c354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 12:13:45 +0800 Subject: [PATCH 211/413] feat: merge geekan:cli-etc --- metagpt/actions/prepare_documents.py | 3 ++- metagpt/actions/write_prd.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index b339d897d..3d202e762 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -8,6 +8,7 @@ RFC 135 2.2.3.5.1. """ import shutil +from pathlib import Path from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG @@ -28,7 +29,7 @@ class PrepareDocuments(Action): # Create and initialize the workspace folder, initialize the Git environment. project_name = CONFIG.project_name or FileRepository.new_filename() - workdir = CONFIG.project_path or DEFAULT_WORKSPACE_ROOT / project_name + workdir = Path(CONFIG.project_path or DEFAULT_WORKSPACE_ROOT / project_name) if not CONFIG.inc and workdir.exists(): shutil.rmtree(workdir) CONFIG.git_repo = GitRepository() diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index c61684918..3967a0578 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -260,7 +260,7 @@ MERGE_PROMPT = """ ## Old PRD {old_prd} ----- -Role: You are a professional product manager; The goal is to merge the newly added requirements into the existing PRD in order to design a concise, usable, and efficient product. +Role: You are a professional product manager; The goal is to incorporate the newly added requirements from the "Original Requirements" into the existing Product Requirements Document (PRD) in the "Old PRD" in order to design a concise, usable, and efficient product. Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design ATTENTION: Output carefully referenced "Old PRD" in format. From 5351b50d1cf963eebe6473783eb1860bdf6266c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 12:47:48 +0800 Subject: [PATCH 212/413] feat: merge geekan:cli-etc --- metagpt/actions/design_api.py | 62 ++--------------------------------- 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 7164cef26..c5787ba20 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -227,72 +227,12 @@ class WriteDesign(Action): # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - # ======= - # def recreate_workspace(self, workspace: Path): - # try: - # shutil.rmtree(workspace) - # except FileNotFoundError: - # pass # Folder does not exist, but we don't care - # workspace.mkdir(parents=True, exist_ok=True) - - # async def _save_prd(self, docs_path, resources_path, context): - # prd_file = docs_path / "prd.md" - # if context[-1].instruct_content and context[-1].instruct_content.dict()["Competitive Quadrant Chart"]: - # quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"] - # await mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis") - # - # if context[-1].instruct_content: - # logger.info(f"Saving PRD to {prd_file}") - # prd_file.write_text(context[-1].instruct_content.json(ensure_ascii=False), encoding='utf-8') - - # async def _save_system_design(self, docs_path, resources_path, system_design): - # data_api_design = system_design.instruct_content.dict()[ - # "Data structures and interfaces" - # ] # CodeParser.parse_code(block="Data structures and interfaces", text=content) - # seq_flow = system_design.instruct_content.dict()[ - # "Program call flow" - # ] # CodeParser.parse_code(block="Program call flow", text=content) - # await mermaid_to_file(data_api_design, resources_path / "data_api_design") - # await mermaid_to_file(seq_flow, resources_path / "seq_flow") - # system_design_file = docs_path / "system_design.md" - # logger.info(f"Saving System Designs to {system_design_file}") - # system_design_file.write_text(system_design.instruct_content.json(ensure_ascii=False), encoding='utf-8') - - # async def _save(self, context, system_design): - # if isinstance(system_design, ActionOutput): - # project_name = system_design.instruct_content.dict()["project_name"] - # else: - # project_name = CodeParser.parse_str(block="project_name", text=system_design) - # workspace = CONFIG.workspace_path / project_name - # self.recreate_workspace(workspace) - # docs_path = workspace / "docs" - # resources_path = workspace / "resources" - # docs_path.mkdir(parents=True, exist_ok=True) - # resources_path.mkdir(parents=True, exist_ok=True) - # await self._save_prd(docs_path, resources_path, context) - # await self._save_system_design(docs_path, resources_path, system_design) - - # async def run(self, context, format=CONFIG.prompt_format): - async def _new_system_design(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) - # system_design = await self._aask(prompt) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - - # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" - # contain space, have to use setattr self._rename_project_name(system_design=system_design) await self._rename_workspace(system_design) - # ======= - # # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" contain space, have to use setattr - # # setattr( - # # system_design.instruct_content, - # # "project_name", - # # system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), - # # ) - # await self._save(context, system_design) - # >>>>>>> feature/geekan_cli_etc return system_design async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): @@ -306,6 +246,8 @@ class WriteDesign(Action): @staticmethod def _rename_project_name(system_design): + # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" + # contain space, have to use setattr if CONFIG.project_name: setattr( system_design.instruct_content, From 17bf646539ec2851c791c3b9cccabd8fdbf1753d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 12:53:02 +0800 Subject: [PATCH 213/413] feat: merge geekan:cli-etc --- metagpt/actions/project_management.py | 15 --------- metagpt/actions/write_code.py | 23 -------------- metagpt/actions/write_code_review.py | 17 ----------- metagpt/const.py | 28 ----------------- metagpt/environment.py | 44 --------------------------- metagpt/roles/engineer.py | 27 ---------------- metagpt/roles/qa_engineer.py | 26 ---------------- 7 files changed, 180 deletions(-) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index db856e55b..3d59daeed 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -258,21 +258,6 @@ class WriteTasks(Action): return task_doc async def _run_new_tasks(self, context, format=CONFIG.prompt_format): - # ======= - # def _save(self, context, rsp): - # if context[-1].instruct_content: - # ws_name = context[-1].instruct_content.dict()["project_name"] - # else: - # ws_name = CodeParser.parse_str(block="project_name", text=context[-1].content) - # file_path = CONFIG.workspace_path / ws_name / "docs/api_spec_and_tasks.md" - # file_path.write_text(rsp.instruct_content.json(ensure_ascii=False)) - # - # # Write requirements.txt - # requirements_path = CONFIG.workspace_path / ws_name / "requirements.txt" - # requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) - # - # async def run(self, context, format=CONFIG.prompt_format): - # >>>>>>> feature/geekan_cli_etc prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 0cd41c52f..59ccb49a5 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -79,29 +79,6 @@ class WriteCode(Action): def __init__(self, name="WriteCode", context=None, llm=None): super().__init__(name, context, llm) - # <<<<<<< HEAD - # ======= - # def _is_invalid(self, filename): - # return any(i in filename for i in ["mp3", "wav"]) - # - # def _save(self, context, filename, code): - # # logger.info(filename) - # # logger.info(code_rsp) - # if self._is_invalid(filename): - # return - # - # design = [i for i in context if i.cause_by == WriteDesign][0] - # - # ws_name = CodeParser.parse_str(block="project_name", text=design.content) - # ws_path = CONFIG.workspace_path / ws_name - # if f"{ws_name}/" not in filename and all(i not in filename for i in ["requirements.txt", ".md"]): - # ws_path = ws_path / ws_name - # code_path = ws_path / filename - # code_path.parent.mkdir(parents=True, exist_ok=True) - # code_path.write_text(code) - # logger.info(f"Saving Code to {code_path}") - # - # >>>>>>> feature/geekan_cli_etc @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) async def write_code(self, prompt) -> str: code_rsp = await self._aask(prompt) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 6d405029a..364f6af57 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -103,23 +103,6 @@ class WriteCodeReview(Action): code = CodeParser.parse_code(block="", text=code_rsp) return result, code - # <<<<<<< HEAD - # async def run(self, *args, **kwargs) -> CodingContext: - # format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) - # context = "\n----------\n".join( - # [self.context.design_doc.content, self.context.task_doc.content, self.context.code_doc.content] - # ) - # prompt = PROMPT_TEMPLATE.format( - # context=context, - # code=self.context.code_doc.content, - # filename=self.context.code_doc.filename, - # format_example=format_example, - # ) - # logger.info(f"Code review {self.context.code_doc.filename}..") - # code = await self.write_code(prompt) - # self.context.code_doc.content = code - # return self.context - # ======= async def run(self, *args, **kwargs) -> CodingContext: iterative_code = self.context.code_doc.content k = CONFIG.code_review_k_times or 1 diff --git a/metagpt/const.py b/metagpt/const.py index 6e616e820..a646cea7a 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -19,34 +19,6 @@ import metagpt OPTIONS = contextvars.ContextVar("OPTIONS") -# <<<<<<< HEAD -# def get_project_root(): -# """Search upwards to find the project root directory.""" -# current_path = Path.cwd() -# while True: -# if ( -# (current_path / ".git").exists() -# or (current_path / ".project_root").exists() -# or (current_path / ".gitignore").exists() -# ): -# return current_path -# parent_path = current_path.parent -# if parent_path == current_path: -# raise Exception("Project root not found.") -# current_path = parent_path -# -# -# PROJECT_ROOT = get_project_root() -# DATA_PATH = PROJECT_ROOT / "data" -# WORKSPACE_ROOT = PROJECT_ROOT / "workspace" -# PROMPT_PATH = PROJECT_ROOT / "metagpt/prompts" -# UT_PATH = PROJECT_ROOT / "data/ut" -# SWAGGER_PATH = UT_PATH / "files/api/" -# UT_PY_PATH = UT_PATH / "files/ut/" -# API_QUESTIONS_PATH = UT_PATH / "files/question/" -# YAPI_URL = "http://yapi.deepwisdomai.com/" -# TMP = PROJECT_ROOT / "tmp" -# ======= def get_metagpt_package_root(): """Get the root directory of the installed package.""" package_root = Path(metagpt.__file__).parent.parent diff --git a/metagpt/environment.py b/metagpt/environment.py index e8bdd25c7..02eb3d340 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -23,7 +23,6 @@ from metagpt.utils.common import is_subscribed class Environment(BaseModel): - # <<<<<<< HEAD """环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到 Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles @@ -32,17 +31,6 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) members: dict[Role, Set] = Field(default_factory=dict) history: str = Field(default="") # For debug - # ======= - # """ - # Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles - # """ - # - # roles: dict[str, Role] = Field(default_factory=dict) - # memory: Memory = Field(default_factory=Memory) # 已经私有化 - # history: str = Field(default='') - # repo: Repo = Field(default_factory=Repo) # 在CONFIG里 - # kv: dict = Field(default_factory=dict) # 在CONFIG里 - # >>>>>>> feature/geekan_cli_etc class Config: arbitrary_types_allowed = True @@ -83,38 +71,6 @@ class Environment(BaseModel): return True - # # Replaced by FileRepository.set_file - # def set_doc(self, content: str, filename: str): - # """向当前环境发布文档(包括代码)""" - # return self.repo.set(content, filename) - # - # # Replaced by FileRepository.get_file - # def get_doc(self, filename: str): - # return self.repo.get(filename) - # - # # Replaced by CONFIG.xx - # def set(self, k: str, v: str): - # self.kv[k] = v - # - # # Replaced by CONFIG.xx - # def get(self, k: str): - # return self.kv.get(k, None) - - # Replaced By 增量变更流程 - # def load_existing_repo(self, path: Path, inc: bool): - # self.repo = Repo.from_path(path) - # logger.info(self.repo.eda()) - # - # # Incremental mode: publish all docs to messages. Then roles can read the docs. - # if inc: - # docs = self.repo.get_text_documents() - # for doc in docs: - # msg = Message(content=doc.content) - # self.publish_message(msg) - # logger.info(f"Message from existing doc {doc.path}: {msg}") - # logger.info(f"Load {len(docs)} docs from existing repo.") - # raise NotImplementedError - async def run(self, k=1): """处理一次所有信息的运行 Process all Role runs at once diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 48262989c..78a7f3ba2 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -183,24 +183,6 @@ class Engineer(Role): msg = Message( content=coding_context.json(), instruct_content=coding_context, role=self.profile, cause_by=WriteCode ) - # ======= - # context = [] - # msg = self._rc.memory.get_by_actions([WriteDesign, WriteTasks, WriteCode]) - # for m in msg: - # context.append(m.content) - # context_str = "\n----------\n".join(context) - # # Write code - # code = await WriteCode().run(context=context_str, filename=todo) - # # Code review - # if self.use_code_review: - # # try: - # rewrite_code = await WriteCodeReview().run(context=context_str, code=code, filename=todo) - # code = rewrite_code - # # except Exception as e: - # # logger.error("code review failed!", e) - # file_path = self.write_file(todo, code) - # msg = Message(content=code, role=self.profile, cause_by=WriteCode) - # >>>>>>> feature/geekan_cli_etc self._rc.memory.add(msg) changed_files.add(coding_context.code_doc.filename) @@ -273,15 +255,6 @@ class Engineer(Role): coding_doc = Document(root_path=str(src_file_repo.root_path), filename=filename, content=context.json()) return coding_doc - # ======= - # async def _act(self) -> Message: - # """Determines the mode of action based on whether code review is used.""" - # logger.info(f"{self._setting}: ready to WriteCode") - # if self.use_code_review: - # return await self._act_sp_with_cr() - # return await self._act_sp() - # >>>>>>> feature/geekan_cli_etc - async def _new_code_actions(self): # Prepare file repos src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index fd2dcc786..ac5a280bb 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -44,32 +44,6 @@ class QaEngineer(Role): self.test_round = 0 self.test_round_allowed = test_round_allowed - # <<<<<<< HEAD - # ======= - # @classmethod - # def parse_workspace(cls, system_design_msg: Message) -> str: - # if system_design_msg.instruct_content: - # return system_design_msg.instruct_content.dict().get("project_name") - # return CodeParser.parse_str(block="project_name", text=system_design_msg.content) - # - # def get_workspace(self, return_proj_dir=True) -> Path: - # msg = self._rc.memory.get_by_action(WriteDesign)[-1] - # if not msg: - # return CONFIG.workspace_path / "src" - # workspace = self.parse_workspace(msg) - # # project directory: workspace/{package_name}, which contains package source code folder, tests folder, resources folder, etc. - # if return_proj_dir: - # return CONFIG.workspace_path / workspace - # # development codes directory: workspace/{package_name}/{package_name} - # return CONFIG.workspace_path / workspace / workspace - # - # def write_file(self, filename: str, code: str): - # workspace = self.get_workspace() / "tests" - # file = workspace / filename - # file.parent.mkdir(parents=True, exist_ok=True) - # file.write_text(code) - # - # >>>>>>> feature/geekan_cli_etc async def _write_test(self, message: Message) -> None: changed_files = message.content.splitlines() src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) From 6146d4dc7f352bc417bc2c720061613c7373ad39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 13:04:29 +0800 Subject: [PATCH 214/413] feat: merge RFC 135 --- metagpt/roles/engineer.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 2bb1f3ea2..d42835a1b 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -218,15 +218,15 @@ class Engineer(Role): src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) changed_src_files = src_file_repo.changed_files # Generate a SummarizeCode action for each pair of (system_design_doc, task_doc). - summerizations = {} + summarizations = {} for filename in changed_src_files: - depenencies = src_file_repo.get_dependency(filename=filename) - ctx = CodeSummarizeContext.loads(filenames=depenencies) - if ctx not in summerizations: - summerizations[ctx] = set() - srcs = summerizations.get(ctx) + dependencies = src_file_repo.get_dependency(filename=filename) + ctx = CodeSummarizeContext.loads(filenames=dependencies) + if ctx not in summarizations: + summarizations[ctx] = set() + srcs = summarizations.get(ctx) srcs.add(filename) - for ctx, filenames in summerizations.items(): + for ctx, filenames in summarizations.items(): ctx.codes_filenames = filenames self.summarize_todos.append(SummarizeCode(context=ctx, llm=self._llm)) if self.summarize_todos: From 6f3d1d6f5e6c6080bab47ad28184c698c5dd7913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 14:58:06 +0800 Subject: [PATCH 215/413] =?UTF-8?q?fixbug:=20=E5=A2=9E=E9=87=8F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=97=B6=E9=9C=80=E6=B1=82=E6=B2=A1=E5=86=99=E5=85=A5?= =?UTF-8?q?docs/requirement.txt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/actions/prepare_documents.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 30558c93f..8656de812 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -23,14 +23,11 @@ class PrepareDocuments(Action): super().__init__(name, context, llm) async def run(self, with_messages, **kwargs): - if CONFIG.git_repo: - doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) - return ActionOutput(content=doc.json(exclue="content"), instruct_content=doc) - - # Create and initialize the workspace folder, initialize the Git environment. - CONFIG.git_repo = GitRepository() - workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_filename() - CONFIG.git_repo.open(local_path=workdir, auto_init=True) + if not CONFIG.git_repo: + # Create and initialize the workspace folder, initialize the Git environment. + CONFIG.git_repo = GitRepository() + workdir = Path(CONFIG.WORKDIR) if CONFIG.WORKDIR else WORKSPACE_ROOT / FileRepository.new_filename() + CONFIG.git_repo.open(local_path=workdir, auto_init=True) # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. doc = Document(root_path=DOCS_FILE_REPO, filename=REQUIREMENT_FILENAME, content=with_messages[0].content) From 9e5c873d77754f24a7b36be0e697975d30efed04 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 15:10:38 +0800 Subject: [PATCH 216/413] update unittest of ser&deser --- tests/metagpt/actions/test_action.py | 17 --- tests/metagpt/roles/test_role.py | 84 +----------- .../serialize_deserialize/test_action.py | 49 +++++++ .../serialize_deserialize/test_actions.py | 26 ---- .../test_architect_deserialize.py | 2 +- .../serialize_deserialize/test_environment.py | 91 +++++++++++++ .../test_memory.py | 34 ++++- .../test_product_manager.py | 4 +- .../test_project_manager.py | 6 +- .../serialize_deserialize/test_role.py | 63 ++++++++- .../serialize_deserialize/test_schema.py | 49 +++++++ .../test_serdeser_base.py | 88 +++++++++++++ .../serialize_deserialize/test_team.py | 124 +++++++++++++----- .../serialize_deserialize/test_wrire_prd.py | 1 - .../serialize_deserialize/test_write_code.py | 2 +- tests/metagpt/test_environment.py | 44 +++---- tests/metagpt/test_role.py | 14 -- tests/metagpt/test_schema.py | 4 +- tests/metagpt/test_team.py | 22 +--- 19 files changed, 496 insertions(+), 228 deletions(-) create mode 100644 tests/metagpt/serialize_deserialize/test_action.py delete mode 100644 tests/metagpt/serialize_deserialize/test_actions.py create mode 100644 tests/metagpt/serialize_deserialize/test_environment.py rename tests/metagpt/{memory => serialize_deserialize}/test_memory.py (52%) create mode 100644 tests/metagpt/serialize_deserialize/test_schema.py create mode 100644 tests/metagpt/serialize_deserialize/test_serdeser_base.py delete mode 100644 tests/metagpt/test_role.py diff --git a/tests/metagpt/actions/test_action.py b/tests/metagpt/actions/test_action.py index 4468a6f6f..9775630cc 100644 --- a/tests/metagpt/actions/test_action.py +++ b/tests/metagpt/actions/test_action.py @@ -11,20 +11,3 @@ from metagpt.actions import Action, WritePRD, WriteTest def test_action_repr(): actions = [Action(), WriteTest(), WritePRD()] assert "WriteTest" in str(actions) - - -def test_action_serdes(): - action_info = WriteTest.ser_class() - assert action_info["action_class"] == "WriteTest" - - action_class = Action.deser_class(action_info) - assert action_class == WriteTest - - -def test_action_class_serdes(): - name = "write test" - action_info = WriteTest(name=name).serialize() - assert action_info["name"] == name - - action = Action.deserialize(action_info) - assert action.name == name diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py index a19ad9cb5..72cd84a9a 100644 --- a/tests/metagpt/roles/test_role.py +++ b/tests/metagpt/roles/test_role.py @@ -2,84 +2,10 @@ # -*- coding: utf-8 -*- # @Desc : unittest of Role -from pathlib import Path -import shutil -import pytest - -from metagpt.roles.role import Role, RoleReactMode -from metagpt.actions.action import Action -from metagpt.schema import Message -from metagpt.actions.add_requirement import BossRequirement -from metagpt.roles.product_manager import ProductManager - -serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") +from metagpt.roles.role import Role -def test_role_serdes(): - stg_path_prefix = serdes_path.joinpath("team/environment/roles/") - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - pm = ProductManager() - role_tag = f"{pm.__class__.__name__}_{pm.name}" - stg_path = stg_path_prefix.joinpath(role_tag) - pm.serialize(stg_path) - assert stg_path.joinpath("actions/actions_info.json").exists() - - new_pm = Role.deserialize(stg_path) - assert new_pm.name == pm.name - assert len(new_pm.get_memories(1)) == 0 - - -class ActionOK(Action): - - async def run(self, messages: list["Message"]): - return "ok" - - -class ActionRaise(Action): - - async def run(self, messages: list["Message"]): - raise RuntimeError("parse error") - - -class RoleA(Role): - - def __init__(self, - name: str = "RoleA", - profile: str = "Role A", - goal: str = "", - constraints: str = ""): - super(RoleA, self).__init__(name=name, profile=profile, goal=goal, constraints=constraints) - self._init_actions([ActionOK, ActionRaise]) - self._watch([BossRequirement]) - self._rc.react_mode = RoleReactMode.BY_ORDER - - async def run(self, message: "Message" = None, stg_path: str = None): - try: - await super(RoleA, self).run(message) - except Exception as exp: - print("exp ", exp) - self.serialize(stg_path) - - -@pytest.mark.asyncio -async def test_role_serdes_interrupt(): - role_a = RoleA() - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - stg_path = serdes_path.joinpath(f"team/environment/roles/{role_a.__class__.__name__}_{role_a.name}") - await role_a.run( - message=Message(content="demo", cause_by=BossRequirement), - stg_path=stg_path - ) - assert role_a._rc.memory.count() == 2 - - assert stg_path.joinpath("actions/todo.json").exists() - - new_role_a: Role = Role.deserialize(stg_path) - assert new_role_a._rc.state == 1 - await role_a.run( - message=Message(content="demo", cause_by=BossRequirement), - stg_path=stg_path - ) - +def test_role_desc(): + role = Role(profile="Sales", desc="Best Seller") + assert role.profile == "Sales" + assert role._setting.desc == "Best Seller" diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py new file mode 100644 index 000000000..b624dff5a --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import Action, WritePRD, WriteTest +from metagpt.llm import LLM +from metagpt.provider.openai_api import OpenAIGPTAPI + + +def test_action_serialize(): + action = Action() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = Action() + serialized_data = action.dict() + assert isinstance(serialized_data["llm"], OpenAIGPTAPI) + + new_action = Action(**serialized_data) + + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action._aask("who are you")) > 0 + + +def test_action_serdeser(): + action_info = WriteTest.ser_class() + assert action_info["action_class"] == "WriteTest" + + action_class = Action.deser_class(action_info) + assert action_class == WriteTest + + +def test_action_class_serdeser(): + name = "write test" + action_info = WriteTest(name=name).serialize() + assert action_info["name"] == name + + action_info = WriteTest(name=name, llm=LLM()).serialize() + assert action_info["name"] == name + + action = Action.deserialize(action_info) + assert action.name == name diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py deleted file mode 100644 index 2fec2121a..000000000 --- a/tests/metagpt/serialize_deserialize/test_actions.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- -# @Date : 11/22/2023 11:48 AM -# @Author : stellahong (stellahong@fuzhi.ai) -# @Desc : -import pytest - -from metagpt.actions import Action -from metagpt.llm import LLM - - -def test_action_serialize(): - action = Action() - ser_action_dict = action.dict() - assert "name" in ser_action_dict - assert "llm" in ser_action_dict - - -@pytest.mark.asyncio -async def test_action_deserialize(): - action = Action() - serialized_data = action.dict() - - new_action = Action(**serialized_data) - assert new_action.name == "" - assert new_action.llm == LLM() - assert len(await new_action._aask("who are you")) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index d0ee3bc99..fb58f0a3a 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -25,4 +25,4 @@ async def test_architect_deserialize(): assert new_role.name == "Bob" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file + await new_role._actions[0].run(context="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py new file mode 100644 index 000000000..15336eb6a --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : + +from pathlib import Path +import shutil + +from metagpt.schema import Message +from metagpt.actions.action_output import ActionOutput +from metagpt.roles.project_manager import ProjectManager +from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.project_management import WriteTasks +from metagpt.environment import Environment +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, ActionOK, serdeser_path + + +def test_env_serialize(): + env = Environment() + ser_env_dict = env.dict() + assert "roles" in ser_env_dict + assert "memory" in ser_env_dict + + +def test_env_deserialize(): + env = Environment() + env.publish_message(message=Message(content="test env serialize")) + ser_env_dict = env.dict() + new_env = Environment(**ser_env_dict) + assert len(new_env.roles) == 0 + assert new_env.memory.storage[0].content == "test env serialize" + assert len(new_env.history) == 25 + + +def test_environment_serdeser(): + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("prd", out_mapping) + + message = Message( + content="prd", + instruct_content=ic_obj(**out_data), + role="product manager", + cause_by=BossRequirement + ) + + environment = Environment() + role_c = RoleC() + environment.add_role(role_c) + environment.publish_message(message) + + ser_data = environment.dict() + assert ser_data["roles"]["Role C"]["name"] == "RoleC" + + new_env: Environment = Environment(**ser_data) + assert len(new_env.roles) == 1 + + assert new_env.memory.count() == 1 + assert new_env.memory.storage[0].instruct_content == ic_obj(**out_data) + assert list(new_env.roles.values())[0]._states == list(environment.roles.values())[0]._states + assert list(new_env.roles.values())[0]._actions == list(environment.roles.values())[0]._actions + assert isinstance(list(environment.roles.values())[0]._actions[0], ActionOK) + assert type(list(new_env.roles.values())[0]._actions[0]) == ActionOK + + +def test_environment_serdeser_v2(): + environment = Environment() + pm = ProjectManager() + environment.add_role(pm) + + ser_data = environment.dict() + + new_env: Environment = Environment(**ser_data) + role = new_env.get_role(pm.profile) + assert isinstance(role, ProjectManager) + assert isinstance(role._actions[0], WriteTasks) + assert isinstance(list(new_env.roles.values())[0]._actions[0], WriteTasks) + + +def test_environment_serdeser_save(): + environment = Environment() + role_c = RoleC() + + shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) + + stg_path = serdeser_path.joinpath("team/environment") + environment.add_role(role_c) + environment.serialize(stg_path) + + new_env: Environment = Environment.deserialize(stg_path) + assert len(new_env.roles) == 1 + assert type(list(new_env.roles.values())[0]._actions[0]) == ActionOK diff --git a/tests/metagpt/memory/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py similarity index 52% rename from tests/metagpt/memory/test_memory.py rename to tests/metagpt/serialize_deserialize/test_memory.py index bda79ded1..e24f31af3 100644 --- a/tests/metagpt/memory/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -3,6 +3,7 @@ # @Desc : unittest of memory from pathlib import Path +from pydantic import BaseModel from metagpt.schema import Message from metagpt.memory.memory import Memory @@ -10,10 +11,36 @@ from metagpt.actions.action_output import ActionOutput from metagpt.actions.design_api import WriteDesign from metagpt.actions.add_requirement import BossRequirement -serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") +from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path -def test_memory_serdes(): +def test_memory_serdeser(): + msg1 = Message(role="Boss", + content="write a snake game", + cause_by=BossRequirement) + + out_mapping = {"field2": (list[str], ...)} + out_data = {"field2": ["field2 value1", "field2 value2"]} + ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + msg2 = Message(role="Architect", + instruct_content=ic_obj(**out_data), + content="system design content", + cause_by=WriteDesign) + + memory = Memory() + memory.add_batch([msg1, msg2]) + ser_data = memory.dict() + + new_memory = Memory(**ser_data) + assert new_memory.count() == 2 + new_msg2 = new_memory.get(2)[0] + assert isinstance(new_msg2, BaseModel) + assert isinstance(new_memory.storage[-1], BaseModel) + assert new_memory.storage[-1].cause_by == WriteDesign + assert new_msg2.role == "Boss" + + +def test_memory_serdeser_save(): msg1 = Message(role="User", content="write a 2048 game", cause_by=BossRequirement) @@ -29,7 +56,7 @@ def test_memory_serdes(): memory = Memory() memory.add_batch([msg1, msg2]) - stg_path = serdes_path.joinpath("team/environment") + stg_path = serdeser_path.joinpath("team/environment") memory.serialize(stg_path) assert stg_path.joinpath("memory.json").exists() @@ -38,5 +65,6 @@ def test_memory_serdes(): new_msg2 = new_memory.get(1)[0] assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] assert new_msg2.cause_by == WriteDesign + assert len(new_memory.index) == 2 stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 2aed87a28..54584cf96 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -15,8 +15,8 @@ async def test_product_manager_deserialize(): ser_role_dict = role.dict(by_alias=True) new_role = ProductManager(**ser_role_dict) # new_role = ProductManager().deserialize(ser_role_dict) - + assert new_role.name == "Alice" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run([Message(content="write a cli snake game")]) \ No newline at end of file + await new_role._actions[0].run([Message(content="write a cli snake game")]) diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index fbc0dcc08..21fafa72e 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -6,6 +6,7 @@ import pytest from metagpt.roles.project_manager import ProjectManager from metagpt.actions.action import Action +from metagpt.actions.project_management import WriteTasks def test_project_manager_serialize(): @@ -20,9 +21,10 @@ def test_project_manager_serialize(): async def test_project_manager_deserialize(): role = ProjectManager() ser_role_dict = role.dict(by_alias=True) + new_role = ProjectManager(**ser_role_dict) - # new_role = ProjectManager().deserialize(ser_role_dict) assert new_role.name == "Eve" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file + assert isinstance(new_role._actions[0], WriteTasks) + # await new_role._actions[0].run(context="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 0e438d1a2..f260dea3a 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -2,12 +2,22 @@ # @Date : 11/23/2023 4:49 PM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + +from pathlib import Path +import shutil import pytest +from metagpt.logs import logger from metagpt.roles.role import Role +from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.schema import Message +from metagpt.actions.add_requirement import BossRequirement +from metagpt.roles.product_manager import ProductManager +from metagpt.const import SERDESER_PATH from metagpt.roles.engineer import Engineer +from metagpt.utils.utils import format_trackback_info -from metagpt.actions.action import Action +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, serdeser_path def test_role_serialize(): @@ -30,12 +40,53 @@ def test_engineer_serialize(): async def test_engineer_deserialize(): role = Engineer(use_code_review=True) ser_role_dict = role.dict(by_alias=True) - # new_role = Engineer().deserialize(ser_role_dict) - # also can be deserialized in this way: + new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" assert new_role.use_code_review is True assert len(new_role._actions) == 2 - assert isinstance(new_role._actions[0], Action) - assert isinstance(new_role._actions[1], Action) - await new_role._actions[0].run(context="write a cli snake game", filename="test_code") + assert isinstance(new_role._actions[0], WriteCode) + assert isinstance(new_role._actions[1], WriteCodeReview) + # await new_role._actions[0].run(context="write a cli snake game", filename="test_code") + + +def test_role_serdeser_save(): + stg_path_prefix = serdeser_path.joinpath("team/environment/roles/") + shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) + + pm = ProductManager() + role_tag = f"{pm.__class__.__name__}_{pm.name}" + stg_path = stg_path_prefix.joinpath(role_tag) + pm.serialize(stg_path) + assert stg_path.joinpath("actions/actions_info.json").exists() + + new_pm = Role.deserialize(stg_path) + assert new_pm.name == pm.name + assert len(new_pm.get_memories(1)) == 0 + + +@pytest.mark.asyncio +async def test_role_serdeser_interrupt(): + role_c = RoleC() + shutil.rmtree(SERDESER_PATH.joinpath("team"), ignore_errors=True) + + stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{role_c.__class__.__name__}_{role_c.name}") + try: + await role_c.run( + message=Message(content="demo", cause_by=BossRequirement) + ) + except Exception as exp: + logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") + role_c.serialize(stg_path) + + assert role_c._rc.memory.count() == 2 + + assert stg_path.joinpath("actions/todo.json").exists() + + new_role_a: Role = Role.deserialize(stg_path) + assert new_role_a._rc.state == 1 + + with pytest.raises(Exception): + await role_c.run( + message=Message(content="demo", cause_by=BossRequirement) + ) diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py new file mode 100644 index 000000000..74b134cad --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of schema ser&deser + +from metagpt.schema import Message +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.write_code import WriteCode + +from tests.metagpt.serialize_deserialize.test_serdeser_base import MockMessage + + +def test_message_serdeser(): + out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} + out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + + message = Message( + content="code", + instruct_content=ic_obj(**out_data), + role="engineer", + cause_by=WriteCode + ) + ser_data = message.dict() + assert ser_data["cause_by"] == { + "action_class": "WriteCode", + "module_name": "metagpt.actions.write_code" + } + assert ser_data["instruct_content"]["class"] == "code" + + new_message = Message(**ser_data) + assert new_message.cause_by == WriteCode + assert new_message.cause_by in [WriteCode] + assert new_message.instruct_content == ic_obj(**out_data) + + +def test_message_without_postprocess(): + """ to explain `instruct_content` should be postprocessed """ + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + message = MockMessage( + content="code", + instruct_content=ic_obj(**out_data) + ) + ser_data = message.dict() + assert ser_data["instruct_content"] == {"field1": ["field1 value1", "field1 value2"]} + + new_message = MockMessage(**ser_data) + assert new_message.instruct_content != ic_obj(**out_data) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py new file mode 100644 index 000000000..35bad6cd9 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : base test actions / roles used in unittest + +from pydantic import BaseModel, Field +from pathlib import Path + +from metagpt.actions.action import Action +from metagpt.roles.role import Role, RoleReactMode +from metagpt.actions.add_requirement import BossRequirement + + +serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage") + + +class MockMessage(BaseModel): + """ to test normal dict without postprocess """ + content: str = "" + instruct_content: BaseModel = Field(default=None) + + +class ActionPass(Action): + name: str = "ActionPass" + + async def run(self, messages: list["Message"]): + return "pass" + + +class ActionOK(Action): + name: str = "ActionOK" + + async def run(self, messages: list["Message"]): + return "ok" + + +class ActionRaise(Action): + name: str = "ActionRaise" + + async def run(self, messages: list["Message"]): + raise RuntimeError("parse error in ActionRaise") + + +class RoleA(Role): + + name: str = Field(default="RoleA") + profile: str = Field(default="Role A") + goal: str = "RoleA's goal" + constraints: str = "RoleA's constraints" + + def __init__(self, **kwargs): + super(RoleA, self).__init__(**kwargs) + self._init_actions([ActionPass]) + self._watch([BossRequirement]) + + async def run(self, message: "Message" = None): + await super(RoleA, self).run(message) + + +class RoleB(Role): + name: str = Field(default="RoleB") + profile: str = Field(default="Role B") + goal: str = "RoleB's goal" + constraints: str = "RoleB's constraints" + + def __init__(self, **kwargs): + super(RoleB, self).__init__(**kwargs) + self._init_actions([ActionOK, ActionRaise]) + self._watch([ActionPass]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None): + await super(RoleB, self).run(message) + + +class RoleC(Role): + name: str = Field(default="RoleC") + profile: str = Field(default="Role C") + goal: str = "RoleC's goal" + constraints: str = "RoleC's constraints" + + def __init__(self, **kwargs): + super(RoleC, self).__init__(**kwargs) + self._init_actions([ActionOK, ActionRaise]) + self._watch([BossRequirement]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None): + await super(RoleC, self).run(message) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 44a75d262..e9122ebc0 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -2,46 +2,104 @@ # @Date : 11/27/2023 10:07 AM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + +from pathlib import Path +import shutil import pytest -from metagpt.environment import Environment -from metagpt.schema import Message -from metagpt.software_company import SoftwareCompany from metagpt.roles import ProjectManager, ProductManager, Architect +from metagpt.team import Team +from metagpt.const import SERDESER_PATH + +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path -def test_env_serialize(): - env = Environment() - ser_env_dict = env.dict() - assert "roles" in ser_env_dict - assert "memory" in ser_env_dict - assert "memory" in ser_env_dict +def test_team_deserialize(): + company = Team() - -def test_env_deserialize(): - env = Environment() - env.publish_message(message=Message(content="test env serialize")) - ser_env_dict = env.dict() - new_env = Environment(**ser_env_dict) - assert len(new_env.roles) == 0 - assert new_env.memory.storage[0].content == "test env serialize" - assert len(new_env.history) == 25 - - -def test_softwarecompany_deserialize(): - team = SoftwareCompany() - team.hire( + pm = ProductManager() + arch = Architect() + company.hire( [ - ProductManager(), - Architect(), + pm, + arch, ProjectManager(), ] ) - assert len(team.environment.get_roles()) == 3 - ser_team_dict = team.dict() - new_team = SoftwareCompany(**ser_team_dict) - - assert len(new_team.environment.get_roles()) == 3 - assert new_team.environment.get_role('Product Manager') is not None - assert new_team.environment.get_role('Product Manager') is not None - assert new_team.environment.get_role('Architect') is not None + assert len(company.environment.get_roles()) == 3 + ser_company = company.dict() + new_company = Team(**ser_company) + + assert len(new_company.environment.get_roles()) == 3 + assert new_company.environment.get_role(pm.profile) is not None + + new_pm = new_company.environment.get_role(pm.profile) + assert type(new_pm) == ProductManager + assert new_company.environment.get_role(pm.profile) is not None + assert new_company.environment.get_role(arch.profile) is not None + + +def test_team_serdeser(): + company = Team() + company.hire([RoleC()]) + + stg_path = serdeser_path.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company.serialize(stg_path=stg_path) + + new_company = Team.deserialize(stg_path) + + assert len(new_company.environment.roles) == 1 + + +@pytest.mark.asyncio +async def test_team_recover(): + idea = "write a snake game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleC()]) + company.start_project(idea) + await company.run(n_round=4) + + ser_data = company.dict() + new_company = Team(**ser_data) + assert new_company.environment.memory.count() == 1 + assert type(list(new_company.environment.roles.values())[0]._actions[0]) == ActionOK + + new_company.start_project(idea) + await new_company.run(n_round=4) + + +@pytest.mark.asyncio +async def test_team_recover_save(): + idea = "write a 2048 web game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleC()]) + company.start_project(idea) + await company.run(n_round=4) + + new_company = Team.recover(stg_path) + new_company.start_project(idea) + await new_company.run(n_round=4) + + +@pytest.mark.asyncio +async def test_team_recover_multi_roles_save(): + idea = "write a snake game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleA(), RoleB()]) + company.start_project(idea) + await company.run(n_round=4) + + new_company = Team.recover(stg_path) + new_company.start_project(idea) + await new_company.run(n_round=4) diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index baa08ed76..96b4d19ad 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -25,4 +25,3 @@ async def test_action_deserialize(): assert new_action.name == "" assert new_action.llm == LLM() assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 - diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 9d659caaf..7f4799014 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -43,4 +43,4 @@ async def test_write_code_review_deserialize(): assert new_action.name == "WriteCodeReview" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game", code =code, filename="test_rewrite_code") \ No newline at end of file + await new_action.run(context="write a cli snake game", code=code, filename="test_rewrite_code") diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index 3cc2d8a7a..9f69e6189 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -8,17 +8,15 @@ import pytest from pathlib import Path -import shutil from metagpt.actions import BossRequirement from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Architect, ProductManager, Role from metagpt.schema import Message -from tests.metagpt.roles.test_role import RoleA -serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") +serdeser_path = Path(__file__).absolute().parent.joinpath("../data/serdeser_storage") @pytest.fixture @@ -27,14 +25,23 @@ def env(): def test_add_role(env: Environment): - role = ProductManager("Alice", "product manager", "create a new product", "limited resources") + role = ProductManager(name="Alice", + profile="product manager", + goal="create a new product", + constraints="limited resources") env.add_role(role) assert env.get_role(role.profile) == role def test_get_roles(env: Environment): - role1 = Role("Alice", "product manager", "create a new product", "limited resources") - role2 = Role("Bob", "engineer", "develop the new product", "short deadline") + role1 = Role(name="Alice", + profile="product manager", + goal="create a new product", + constraints="limited resources") + role2 = Role(name="Bob", + profile="engineer", + goal="develop the new product", + constraints="short deadline") env.add_role(role1) env.add_role(role2) roles = env.get_roles() @@ -43,8 +50,14 @@ def test_get_roles(env: Environment): @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): - product_manager = ProductManager("Alice", "Product Manager", "做AI Native产品", "资源有限") - architect = Architect("Bob", "Architect", "设计一个可用、高效、较低成本的系统,包括数据结构与接口", "资源有限,需要节省成本") + product_manager = ProductManager(name="Alice", + profile="Product Manager", + goal="做AI Native产品", + constraints="资源有限") + architect = Architect(name="Bob", + profile="Architect", + goal="设计一个可用、高效、较低成本的系统,包括数据结构与接口", + constraints="资源有限,需要节省成本") env.add_roles([product_manager, architect]) env.publish_message(Message(role="BOSS", content="需要一个基于LLM做总结的搜索引擎", cause_by=BossRequirement)) @@ -52,18 +65,3 @@ async def test_publish_and_process_message(env: Environment): await env.run(k=2) logger.info(f"{env.history=}") assert len(env.history) > 10 - - -def test_environment_serdes(): - environment = Environment() - role_a = RoleA() - - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - stg_path = serdes_path.joinpath("team/environment") - environment.add_role(role_a) - environment.serialize(stg_path) - - new_env: Environment = Environment() - new_env.deserialize(stg_path) - assert len(new_env.roles) == 1 diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py deleted file mode 100644 index 11fd804ec..000000000 --- a/tests/metagpt/test_role.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/11 14:44 -@Author : alexanderwu -@File : test_role.py -""" -from metagpt.roles import Role - - -def test_role_desc(): - i = Role(profile='Sales', desc='Best Seller') - assert i.profile == 'Sales' - assert i._setting.desc == 'Best Seller' diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index f515326e8..c70c93cfc 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -25,7 +25,7 @@ def test_messages(): assert all([i in text for i in roles]) -def test_message_serdes(): +def test_message_serdeser(): out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} ic_obj = ActionOutput.create_model_class("code", out_mapping) @@ -37,7 +37,7 @@ def test_message_serdes(): cause_by=WriteCode ) message_dict = serialize_general_message(message) - assert message_dict["cause_by"] == {"action_class": "WriteCode"} + assert message_dict["cause_by"] == {"action_class": "WriteCode", "module_name": "metagpt.actions.write_code"} assert message_dict["instruct_content"] == { "class": "code", "mapping": { diff --git a/tests/metagpt/test_team.py b/tests/metagpt/test_team.py index ab201152c..efd035bb2 100644 --- a/tests/metagpt/test_team.py +++ b/tests/metagpt/test_team.py @@ -2,26 +2,12 @@ # -*- coding: utf-8 -*- # @Desc : unittest of team -from pathlib import Path -import shutil - from metagpt.team import Team - -from tests.metagpt.roles.test_role import RoleA - -serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") +from metagpt.roles.project_manager import ProjectManager -def test_team_serdes(): +def test_team(): company = Team() - company.hire([RoleA()]) + company.hire([ProjectManager()]) - stg_path = serdes_path.joinpath("team") - shutil.rmtree(stg_path, ignore_errors=True) - - company.serialize(stg_path=stg_path) - - new_company = Team() - new_company.deserialize(stg_path) - - assert len(new_company.environment.roles) == 1 + assert len(company.environment.roles) == 1 From 5e3607f85bc4fec0ff97c57ff7d866f108e3c9c3 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 15:18:24 +0800 Subject: [PATCH 217/413] update environment/message to BaseModel, update the ser&deser of roles/actions --- metagpt/actions/action.py | 29 ++++- metagpt/actions/design_api.py | 8 +- metagpt/actions/project_management.py | 3 +- metagpt/actions/search_and_summarize.py | 15 ++- metagpt/actions/write_code.py | 3 +- metagpt/actions/write_code_review.py | 4 +- metagpt/actions/write_prd.py | 6 +- metagpt/actions/write_test.py | 11 +- metagpt/const.py | 2 +- metagpt/environment.py | 39 +++++-- metagpt/memory/longterm_memory.py | 14 ++- metagpt/memory/memory.py | 79 ++++++++++---- metagpt/roles/architect.py | 4 +- metagpt/roles/customer_service.py | 19 ++-- metagpt/roles/engineer.py | 4 +- metagpt/roles/product_manager.py | 5 +- metagpt/roles/project_manager.py | 4 +- metagpt/roles/qa_engineer.py | 16 +-- metagpt/roles/role.py | 130 +++++++++++++--------- metagpt/roles/sales.py | 31 +++--- metagpt/roles/seacher.py | 21 ++-- metagpt/schema.py | 138 ++++++++++++++---------- metagpt/team.py | 39 ++++--- metagpt/utils/serialize.py | 26 +++-- metagpt/utils/utils.py | 43 ++++++++ startup.py | 17 +-- 26 files changed, 458 insertions(+), 252 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index aefe6d39d..7a7f194f4 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -5,8 +5,9 @@ @Author : alexanderwu @File : action.py """ + +from __future__ import annotations import re -from abc import ABC from typing import Optional, Any from pydantic import BaseModel, Field @@ -14,25 +15,43 @@ from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger from metagpt.utils.common import OutputParser from metagpt.utils.custom_decoder import CustomDecoder from metagpt.utils.utils import import_class +action_subclass_registry = {} + + class Action(BaseModel): name: str = "" - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) context = "" prefix = "" profile = "" desc = "" content: Optional[str] = None instruct_content: Optional[str] = None + + # builtin variables + builtin_class_name: str = "" + + class Config: + arbitrary_types_allowed = True def __init__(self, **kwargs: Any): super().__init__(**kwargs) - + + # deserialize child classes dynamically for inherited `action` + object.__setattr__(self, "builtin_class_name", self.__class__.__name__) + self.__fields__["builtin_class_name"].default = self.__class__.__name__ + + def __init_subclass__(cls, **kwargs: Any) -> None: + super().__init_subclass__(**kwargs) + action_subclass_registry[cls.__name__] = cls + def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix @@ -52,14 +71,14 @@ class Action(BaseModel): } @classmethod - def deserialize(cls, action_dict: dict): + def deserialize(cls, action_dict: dict) -> "Action": action_class_str = action_dict.pop("action_class") module_name = action_dict.pop("module_name") action_class = import_class(action_class_str, module_name) return action_class(**action_dict) @classmethod - def ser_class(cls): + def ser_class(cls) -> dict: """ serialize class type""" return { "action_class": cls.__name__, diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 30df70ce7..015678baa 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -13,6 +13,7 @@ from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger @@ -155,12 +156,11 @@ OUTPUT_MAPPING = { class WriteDesign(Action): name: str = "" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " "data structures, library tables, processes, and paths. Please provide your design, feedback " "clearly and in detail." - def recreate_workspace(self, workspace: Path): try: shutil.rmtree(workspace) @@ -168,7 +168,6 @@ class WriteDesign(Action): pass # Folder does not exist, but we don't care workspace.mkdir(parents=True, exist_ok=True) - async def _save_prd(self, docs_path, resources_path, context): prd_file = docs_path / "prd.md" if context[-1].instruct_content and context[-1].instruct_content.dict()["Competitive Quadrant Chart"]: @@ -179,7 +178,6 @@ class WriteDesign(Action): logger.info(f"Saving PRD to {prd_file}") prd_file.write_text(json_to_markdown(context[-1].instruct_content.dict())) - async def _save_system_design(self, docs_path, resources_path, system_design): data_api_design = system_design.instruct_content.dict()[ "Data structures and interface definitions" @@ -193,7 +191,6 @@ class WriteDesign(Action): logger.info(f"Saving System Designs to {system_design_file}") system_design_file.write_text((json_to_markdown(system_design.instruct_content.dict()))) - async def _save(self, context, system_design): if isinstance(system_design, ActionOutput): ws_name = system_design.instruct_content.dict()["Python package name"] @@ -211,7 +208,6 @@ class WriteDesign(Action): logger.error(f"Failed to save PRD {e}") await self._save_system_design(docs_path, resources_path, system_design) - async def run(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) prompt = prompt_template.format(context=context, format_example=format_example) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index b72507ee3..cf44906cd 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -11,6 +11,7 @@ from pydantic import Field from metagpt.actions.action import Action from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import WORKSPACE_ROOT from metagpt.utils.common import CodeParser @@ -168,7 +169,7 @@ OUTPUT_MAPPING = { class WriteTasks(Action): name: str = "CreateTasks" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) def _save(self, context, rsp): try: diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 0580303e6..6b0c1f717 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -8,14 +8,15 @@ import pydantic from typing import Optional, Any from pydantic import BaseModel, Field +from pydantic import root_validator from metagpt.actions import Action from metagpt.llm import LLM -from metagpt.config import Config +from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.config import Config, CONFIG from metagpt.logs import logger from metagpt.schema import Message from metagpt.tools.search_engine import SearchEngine -from pydantic import root_validator SEARCH_AND_SUMMARIZE_SYSTEM = """### Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. @@ -106,13 +107,13 @@ You are a member of a professional butler team and will provide helpful suggesti class SearchAndSummarize(Action): name: str = "" content: Optional[str] = None - llm: None = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) config: None = Field(default_factory=Config) - engine: Optional[str] = None + engine: Optional[str] = CONFIG.search_engine search_func: Optional[str] = None + search_engine: SearchEngine = None result = "" - @root_validator def validate_engine_and_run_func(cls, values): @@ -130,9 +131,7 @@ class SearchAndSummarize(Action): values['search_engine'] = search_engine return values - - - + async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: print(context) if self.search_engine is None: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 2dc240591..10487e53a 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -13,6 +13,7 @@ from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions import WriteDesign from metagpt.actions.action import Action from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.const import WORKSPACE_ROOT from metagpt.logs import logger from metagpt.schema import Message @@ -50,7 +51,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): name: str = "WriteCode" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) def _is_invalid(self, filename): return any(i in filename for i in ["mp3", "wav"]) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 3d86d7c63..79e462f76 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -12,7 +12,7 @@ from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.llm import LLM from metagpt.actions.action import Action from metagpt.logs import logger -from metagpt.schema import Message +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -67,7 +67,7 @@ FORMAT_EXAMPLE = """ class WriteCodeReview(Action): name: str = "WriteCodeReview" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) async def write_code(self, prompt): diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 660d7fb95..450bed7e7 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -11,6 +11,7 @@ from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.logs import logger @@ -224,12 +225,9 @@ OUTPUT_MAPPING = { class WritePRD(Action): name: str = "" content: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) assistant_search_action: Action = None - def __init__(self, **kwargs): - super().__init__(**kwargs) - async def run(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: # self.assistant_search_action = SearchAndSummarize() if self.assistant_search_action is None: diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 35ff36dc2..6c902444a 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -5,6 +5,12 @@ @Author : alexanderwu @File : environment.py """ + +from typing import Optional +from pydantic import Field + +from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.actions.action import Action from metagpt.logs import logger from metagpt.utils.common import CodeParser @@ -31,8 +37,9 @@ you should correctly import the necessary classes based on these file locations! class WriteTest(Action): - def __init__(self, name="WriteTest", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "WriteTest" + context: Optional[str] = None + llm: BaseGPTAPI = Field(default_factory=LLM) async def write_code(self, prompt): code_rsp = await self._aask(prompt) diff --git a/metagpt/const.py b/metagpt/const.py index 711546d03..4b063a3dd 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -42,7 +42,7 @@ TMP = PROJECT_ROOT / "tmp" RESEARCH_PATH = DATA_PATH / "research" TUTORIAL_PATH = DATA_PATH / "tutorial_docx" INVOICE_OCR_TABLE_PATH = DATA_PATH / "invoice_table" -SERDES_PATH = WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project +SERDESER_PATH = WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project SKILL_DIRECTORY = PROJECT_ROOT / "metagpt/skills" diff --git a/metagpt/environment.py b/metagpt/environment.py index e867ad6fc..bade53f50 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,7 +12,7 @@ from pathlib import Path from pydantic import BaseModel, Field from metagpt.memory import Memory -from metagpt.roles import Role +from metagpt.roles.role import Role, role_subclass_registry from metagpt.schema import Message from metagpt.utils.utils import read_json_file, write_json_file @@ -30,6 +30,19 @@ class Environment(BaseModel): class Config: arbitrary_types_allowed = True + def __init__(self, **kwargs): + for role_key, role in kwargs.get("roles", {}).items(): + current_role = kwargs["roles"][role_key] + if isinstance(current_role, dict): + item_class_name = current_role.get("builtin_class_name", None) + for name, subclass in role_subclass_registry.items(): + registery_class_name = subclass.__fields__["builtin_class_name"].default + if item_class_name == registery_class_name: + current_role = subclass(**current_role) + break + kwargs["roles"][role_key] = current_role + super().__init__(**kwargs) + def serialize(self, stg_path: Path): roles_path = stg_path.joinpath("roles.json") roles_info = [] @@ -46,33 +59,39 @@ class Environment(BaseModel): history_path = stg_path.joinpath("history.json") write_json_file(history_path, {"content": self.history}) - def deserialize(self, stg_path: Path): + @classmethod + def deserialize(cls, stg_path: Path) -> "Environment": """ stg_path: ./storage/team/environment/ """ roles_path = stg_path.joinpath("roles.json") roles_info = read_json_file(roles_path) + roles = [] for role_info in roles_info: role_class = role_info.get("role_class") role_name = role_info.get("role_name") role_path = stg_path.joinpath(f"roles/{role_class}_{role_name}") role = Role.deserialize(role_path) - - self.add_role(role) + roles.append(role) memory = Memory.deserialize(stg_path) - self.memory = memory - history_path = stg_path.joinpath("history.json") - history = read_json_file(history_path) - self.history = history.get("content") + history = read_json_file(stg_path.joinpath("history.json")) + history = history.get("content") + + environment = Environment(**{ + "memory": memory, + "history": history + }) + environment.add_roles(roles) + return environment def add_role(self, role: Role): - """增加一个在当前环境的角色, 默认为profile/role_profile + """增加一个在当前环境的角色, 默认为profile Add a role in the current environment """ role.set_env(self) # use alias - self.roles[role.role_profile] = role + self.roles[role.profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index f8abea5f3..5d149ee7a 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -2,6 +2,9 @@ # -*- coding: utf-8 -*- # @Desc : the implement of Long-term memory +from typing import Optional +from pydantic import Field + from metagpt.logs import logger from metagpt.memory import Memory from metagpt.memory.memory_storage import MemoryStorage @@ -15,11 +18,12 @@ class LongTermMemory(Memory): - update memory when it changed """ - def __init__(self): - self.memory_storage: MemoryStorage = MemoryStorage() - super(LongTermMemory, self).__init__() - self.rc = None # RoleContext - self.msg_from_recover = False + memory_storage: MemoryStorage = Field(default_factory=MemoryStorage) + rc: Optional["RoleContext"] = None + msg_from_recover: bool = False + + class Config: + arbitrary_types_allowed = True def recover_memory(self, role_id: str, rc: "RoleContext"): messages = self.memory_storage.recover_memory(role_id) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index a839bb038..c88cc750e 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -5,34 +5,65 @@ @Author : alexanderwu @File : memory.py """ +import copy from collections import defaultdict -from typing import Iterable, Type +from typing import Iterable, Type, Union, Optional from pathlib import Path +from pydantic import BaseModel, Field +import json from metagpt.actions import Action from metagpt.schema import Message from metagpt.utils.utils import read_json_file, write_json_file -from metagpt.utils.serialize import serialize_general_message, deserialize_general_message +from metagpt.utils.utils import import_class -class Memory: +class Memory(BaseModel): """The most basic memory: super-memory""" - def __init__(self): - """Initialize an empty storage list and an empty index dictionary""" - self.storage: list[Message] = [] - self.index: dict[Type[Action], list[Message]] = defaultdict(list) + storage: list[Message] = Field(default=[]) + index: dict[Type[Action], list[Message]] = Field(default_factory=defaultdict(list)) + + def __init__(self, **kwargs): + index = kwargs.get("index", {}) + new_index = defaultdict(list) + for action_str, value in index.items(): + action_dict = json.loads(action_str) + action_class = import_class("Action", "metagpt.actions.action") + action_obj = action_class.deser_class(action_dict) + new_index[action_obj] = [Message(**item_dict) for item_dict in value] + kwargs["index"] = new_index + super(Memory, self).__init__(**kwargs) + self.index = new_index + + def dict(self, + *, + include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, + exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, + by_alias: bool = False, + skip_defaults: Optional[bool] = None, + exclude_unset: bool = False, + exclude_defaults: bool = False, + exclude_none: bool = False) -> "DictStrAny": + """ overwrite the `dict` to dump dynamic pydantic model""" + obj_dict = super(Memory, self).dict(include=include, + exclude=exclude, + by_alias=by_alias, + skip_defaults=skip_defaults, + exclude_unset=exclude_unset, + exclude_defaults=exclude_defaults, + exclude_none=exclude_none) + new_obj_dict = copy.deepcopy(obj_dict) + new_obj_dict["index"] = {} + for action, value in obj_dict["index"].items(): + action_ser = json.dumps(action.ser_class()) + new_obj_dict["index"][action_ser] = value + return new_obj_dict def serialize(self, stg_path: Path): """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ memory_path = stg_path.joinpath("memory.json") - - storage = [] - for message in self.storage: - # msg_dict = message.serialize() - msg_dict = serialize_general_message(message) - storage.append(msg_dict) - + storage = self.dict() write_json_file(memory_path, storage) @classmethod @@ -40,13 +71,8 @@ class Memory: """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" memory_path = stg_path.joinpath("memory.json") - memory = Memory() - memory_list = read_json_file(memory_path) - for message in memory_list: - # distinguish instruct_content type in message - # msg = Message.deserialize(message) - msg = deserialize_general_message(message) - memory.add(msg) + memory_dict = read_json_file(memory_path) + memory = Memory(**memory_dict) return memory @@ -70,6 +96,16 @@ class Memory: """Return all messages containing a specified content""" return [message for message in self.storage if content in message.content] + def delete_newest(self) -> "Message": + """ delete the newest message from the storage""" + if len(self.storage) > 0: + newest_msg = self.storage.pop() + if newest_msg.cause_by and newest_msg in self.index[newest_msg.cause_by]: + self.index[newest_msg.cause_by].remove(newest_msg) + else: + newest_msg = None + return newest_msg + def delete(self, message: Message): """Delete the specified message from storage, while updating the index""" self.storage.remove(message) @@ -115,4 +151,3 @@ class Memory: continue rsp += self.index[action] return rsp - \ No newline at end of file diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index face22a68..09d52edbe 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -22,8 +22,8 @@ class Architect(Role): goal (str): Primary goal or responsibility of the architect. constraints (str): Constraints or guidelines for the architect. """ - name: str = "Bob" - role_profile: str = Field(default="Architect" , alias='profile') + name: str = Field(default="Bob") + profile: str = Field(default="Architect") goal: str = "Design a concise, usable, complete python system" constraints: str = "Try to specify good open source tools as much as possible" diff --git a/metagpt/roles/customer_service.py b/metagpt/roles/customer_service.py index 4547f8190..62792696f 100644 --- a/metagpt/roles/customer_service.py +++ b/metagpt/roles/customer_service.py @@ -5,6 +5,9 @@ @Author : alexanderwu @File : sales.py """ +from typing import Optional +from pydantic import Field + from metagpt.roles import Sales # from metagpt.actions import SearchAndSummarize @@ -24,12 +27,14 @@ DESC = """ class CustomerService(Sales): + + name: str = Field(default="Xiaomei") + profile: str = Field(default="Human customer service") + desc: str = DESC, + + store: Optional[str] = None + def __init__( self, - name="Xiaomei", - profile="Human customer service", - desc=DESC, - store=None - ): - super().__init__(name, profile, desc=desc, store=store) - \ No newline at end of file + **kwargs): + super().__init__(**kwargs) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 129bedeb8..e90f586f0 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -60,8 +60,8 @@ class Engineer(Role): use_code_review (bool): Whether to use code review. todos (list): List of tasks. """ - name: str = "Alex" - role_profile: str = Field(default="Engineer", alias='profile') + name: str = Field(default="Alex") + profile: str = Field(default="Engineer") goal: str = "Write elegant, readable, extensible, efficient code" constraints: str = "The code should conform to standards like PEP8 and be modular and maintainable" n_borg: int = 1 diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index b099fb4d9..6f68fe5ba 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -21,10 +21,11 @@ class ProductManager(Role): goal (str): Goal of the product manager. constraints (str): Constraints or limitations for the product manager. """ - name: str = "Alice" - role_profile: str = Field(default="Product Manager", alias='profile') + name: str = Field(default="Alice") + profile: str = Field(default="Product Manager") goal: str = "Efficiently create a successful product" constraints: str = "" + """ Represents a Product Manager role responsible for product development and management. """ diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index a2b227f22..c8e785d85 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -22,8 +22,8 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ - name: str = "Eve" - role_profile: str = Field(default="Project Manager", alias='profile') + name: str = Field(default="Eve") + profile: str = Field(default="Project Manager") goal: str = "Improve team efficiency and deliver with quality and quantity" constraints: str = "" diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index a763c2ce8..bad3f2409 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -7,6 +7,7 @@ """ import os from pathlib import Path +from pydantic import Field from metagpt.actions import ( DebugError, @@ -25,21 +26,22 @@ from metagpt.utils.special_tokens import FILENAME_CODE_SEP, MSG_SEP class QaEngineer(Role): + name: str = Field(default="Edward") + profile: str = Field(default="QaEngineer") + goal: str = "Write comprehensive and robust tests to ensure codes will work as expected without bugs" + constraints: str = "The test code you write should conform to code standard like PEP8, be modular, easy to read and maintain" + test_round_allowed: int = 5 + def __init__( self, - name="Edward", - profile="QaEngineer", - goal="Write comprehensive and robust tests to ensure codes will work as expected without bugs", - constraints="The test code you write should conform to code standard like PEP8, be modular, easy to read and maintain", - test_round_allowed=5, + **kwargs ): - super().__init__(name, profile, goal, constraints) + super().__init__(**kwargs) self._init_actions( [WriteTest] ) # FIXME: a bit hack here, only init one action to circumvent _think() logic, will overwrite _think() in future updates self._watch([WriteCode, WriteCodeReview, WriteTest, RunCode, DebugError]) self.test_round = 0 - self.test_round_allowed = test_round_allowed @classmethod def parse_workspace(cls, system_design_msg: Message) -> str: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index e9371c2c0..b6332aa4c 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -6,27 +6,29 @@ @File : role.py """ +from __future__ import annotations from enum import Enum from pathlib import Path -from __future__ import annotations from typing import ( Iterable, - Type + Type, + Any ) -import re -from pydantic import BaseModel, Field -from importlib import import_module +from pydantic import BaseModel, Field, validator # from metagpt.environment import Environment from metagpt.config import CONFIG -from metagpt.actions import Action, ActionOutput +from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger from metagpt.memory import Memory, LongTermMemory from metagpt.schema import Message from metagpt.provider.human_provider import HumanProvider -from metagpt.utils.utils import read_json_file, write_json_file, import_class +from metagpt.utils.utils import read_json_file, write_json_file, import_class, role_raise_decorator +from metagpt.const import SERDESER_PATH + PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -57,6 +59,7 @@ ROLE_TEMPLATE = """Your response should be based on the previous conversation hi {name}: {result} """ + class RoleReactMode(str, Enum): REACT = "react" BY_ORDER = "by_order" @@ -74,6 +77,7 @@ class RoleSetting(BaseModel): goal: str = "" constraints: str = "" desc: str = "" + is_human: bool = False def __str__(self): return f"{self.name}({self.profile})" @@ -84,10 +88,10 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" - env: 'Environment' = Field(default=None) + env: "Environment" = Field(default=None) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) - state: int = Field(default=0) + state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[]) @@ -112,53 +116,86 @@ class RoleContext(BaseModel): return self.memory.get() +role_subclass_registry = {} + + class Role(BaseModel): """Role/Agent""" - name: str = "" profile: str = "" goal: str = "" constraints: str = "" desc: str = "" - _setting: RoleSetting = Field(default_factory=RoleSetting, alias="_setting") - _setting = RoleSetting(name=name, profile=profile, goal=goal, constraints=constraints) + is_human: bool = False + + _llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) + _setting: RoleSetting = Field(default_factory=RoleSetting, alias=True) _role_id: str = "" - _states: list = Field(default=[]) - _actions: list = Field(default=[]) - _actions_type: list = Field(default=[]) + _states: list[str] = Field(default=[]) + _actions: list[Action] = Field(default=[]) _rc: RoleContext = RoleContext() - + + # builtin variables + recovered: bool = False # to tag if a recovered role + builtin_class_name: str = "" + _private_attributes = { - "_setting": _setting, + "_llm": LLM() if not is_human else HumanProvider(), "_role_id": _role_id, "_states": [], - "_actions": [], - "_actions_type": [] # 用于记录和序列化 + "_actions": [] } - + class Config: arbitrary_types_allowed = True - - def __init__(self, **kwargs): + exclude = ["_llm"] + + def __init__(self, **kwargs: Any): + for index in range(len(kwargs.get("_actions", []))): + current_action = kwargs["_actions"][index] + if isinstance(current_action, dict): + item_class_name = current_action.get("builtin_class_name", None) + for name, subclass in action_subclass_registry.items(): + registery_class_name = subclass.__fields__["builtin_class_name"].default + if item_class_name == registery_class_name: + current_action = subclass(**current_action) + break + kwargs["_actions"][index] = current_action + super().__init__(**kwargs) + # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 + self._private_attributes["_llm"] = LLM() if not self.is_human else HumanProvider() + self._private_attributes["_setting"] = RoleSetting(name=self.name, profile=self.profile, goal=self.goal, + desc=self.desc, constraints=self.constraints, + is_human=self.is_human) for key in self._private_attributes.keys(): if key in kwargs: object.__setattr__(self, key, kwargs[key]) - if key =="_setting": - _setting = RoleSetting(**kwargs[key]) - object.__setattr__(self, '_setting', _setting) + if key == "_setting": + setting = RoleSetting(**kwargs[key]) + object.__setattr__(self, "_setting", setting) elif key == "_rc": _rc = RoleContext - object.__setattr__(self, '_rc', _rc) + object.__setattr__(self, "_rc", _rc) else: object.__setattr__(self, key, self._private_attributes[key]) + + # deserialize child classes dynamically for inherited `role` + object.__setattr__(self, "builtin_class_name", self.__class__.__name__) + self.__fields__["builtin_class_name"].default = self.__class__.__name__ + + def __init_subclass__(cls, **kwargs: Any) -> None: + super().__init_subclass__(**kwargs) + role_subclass_registry[cls.__name__] = cls def _reset(self): - object.__setattr__(self, '_states', []) - object.__setattr__(self, '_actions', []) + object.__setattr__(self, "_states", []) + object.__setattr__(self, "_actions", []) - def serialize(self, stg_path: Path): + def serialize(self, stg_path: Path = None): + stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ + if stg_path is None else stg_path role_info_path = stg_path.joinpath("role_info.json") role_info = { "role_class": self.__class__.__name__, @@ -207,7 +244,7 @@ class Role(BaseModel): actions = [] actions_info = read_json_file(actions_info_path) for action_info in actions_info: - action = Action.deserialize(action_info) + action = Action.deser_class(action_info) actions.append(action) watches_info_path = stg_path.joinpath("watches/watches_info.json") @@ -238,12 +275,8 @@ class Role(BaseModel): return role - def _reset(self): - self._states = [] - self._actions = [] - def set_recovered(self, recovered: bool = False): - self._recovered = recovered + self.recovered = recovered def set_memory(self, memory: Memory): self._rc.memory = memory @@ -256,7 +289,8 @@ class Role(BaseModel): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - i = action("", llm=self._llm) + # import pdb; pdb.set_trace() + i = action(name="", llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): logger.warning(f"is_human attribute does not take effect," @@ -265,8 +299,6 @@ class Role(BaseModel): i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) self._states.append(f"{idx}. {action}") - action_title = action.schema()["title"] - self._actions_type.append(action_title) def set_react_mode(self, react_mode: RoleReactMode, max_react_loop: int = 1): self._set_react_mode(react_mode, max_react_loop) @@ -310,19 +342,10 @@ class Role(BaseModel): logger.debug(self._actions) self._rc.todo = self._actions[self._rc.state] if state >= 0 else None - def set_env(self, env: 'Environment'): + def set_env(self, env: "Environment"): """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" self._rc.env = env - @property - def name(self): - return self._setting.name - - @property - def profile(self): - """Get the role description (position)""" - return self._setting.profile - def _get_prefix(self): """Get the role prefix""" if self._setting.desc: @@ -347,7 +370,7 @@ class Role(BaseModel): logger.debug(f"{prompt=}") if (not next_state.isdigit() and next_state != "-1") \ or int(next_state) not in range(-1, len(self._states)): - logger.warning(f'Invalid answer of state, {next_state=}, will be set to -1') + logger.warning(f"Invalid answer of state, {next_state=}, will be set to -1") next_state = -1 else: next_state = int(next_state) @@ -384,7 +407,7 @@ class Role(BaseModel): news_text = [f"{i.role}: {i.content[:20]}..." for i in self._rc.news] if news_text: - logger.debug(f'{self._setting} observed: {news_text}') + logger.debug(f"{self._setting} observed: {news_text}") return len(self._rc.news) def _publish_message(self, msg): @@ -400,7 +423,7 @@ class Role(BaseModel): Use llm to select actions in _think dynamically """ actions_taken = 0 - rsp = Message("No actions taken yet") # will be overwritten after Role _act + rsp = Message(content="No actions taken yet") # will be overwritten after Role _act while actions_taken < self._rc.max_react_loop: # think await self._think() @@ -410,7 +433,7 @@ class Role(BaseModel): logger.debug(f"{self._setting}: {self._rc.state=}, will do {self._rc.todo}") rsp = await self._act() actions_taken += 1 - return rsp # return output from the last action + return rsp # return output from the last action async def _act_by_order(self) -> Message: """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" @@ -454,7 +477,8 @@ class Role(BaseModel): def get_memories(self, k=0) -> list[Message]: """A wrapper to return the most recent k memories of this role, return all when k=0""" return self._rc.memory.get(k=k) - + + @role_raise_decorator async def run(self, message=None): """Observe, and think and act based on the results of the observation""" if message: diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index a45ad6f1b..dd360d82a 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -5,26 +5,34 @@ @Author : alexanderwu @File : sales.py """ + +from typing import Optional +from pydantic import Field + from metagpt.actions import SearchAndSummarize from metagpt.roles import Role from metagpt.tools import SearchEngineType class Sales(Role): + + name: str = Field(default="Xiaomei") + profile: str = Field(default="Retail sales guide") + desc: str = "I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " + "will answer questions only based on the information in the knowledge base." + "If I feel that you can't get the answer from the reference material, then I will directly reply that" + " I don't know, and I won't tell you that this is from the knowledge base," + "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " + "professional guide", + + store: Optional[str] = None + def __init__( self, - name="Xiaomei", - profile="Retail sales guide", - desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " - "will answer questions only based on the information in the knowledge base." - "If I feel that you can't get the answer from the reference material, then I will directly reply that" - " I don't know, and I won't tell you that this is from the knowledge base," - "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " - "professional guide", - store=None + **kwargs ): - super().__init__(name, profile, desc=desc) - self._set_store(store) + super().__init__(**kwargs) + self._set_store(self.store) def _set_store(self, store): if store: @@ -32,4 +40,3 @@ class Sales(Role): else: action = SearchAndSummarize() self._init_actions([action]) - \ No newline at end of file diff --git a/metagpt/roles/seacher.py b/metagpt/roles/seacher.py index 0b6e089da..e8f291d0d 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/seacher.py @@ -5,6 +5,9 @@ @Author : alexanderwu @File : seacher.py """ + +from pydantic import Field + from metagpt.actions import ActionOutput, SearchAndSummarize from metagpt.logs import logger from metagpt.roles import Role @@ -23,14 +26,14 @@ class Searcher(Role): constraints (str): Constraints or limitations for the searcher. engine (SearchEngineType): The type of search engine to use. """ + + name: str = Field(default="Alice") + profile: str = Field(default="Smart Assistant") + goal: str = "Provide search services for users" + constraints: str = "Answer is rich and complete" + engine: SearchEngineType = SearchEngineType.SERPAPI_GOOGLE - def __init__(self, - name: str = 'Alice', - profile: str = 'Smart Assistant', - goal: str = 'Provide search services for users', - constraints: str = 'Answer is rich and complete', - engine=SearchEngineType.SERPAPI_GOOGLE, - **kwargs) -> None: + def __init__(self, **kwargs) -> None: """ Initializes the Searcher role with given attributes. @@ -41,8 +44,8 @@ class Searcher(Role): constraints (str): Constraints or limitations for the searcher. engine (SearchEngineType): The type of search engine to use. """ - super().__init__(name, profile, goal, constraints, **kwargs) - self._init_actions([SearchAndSummarize(engine=engine)]) + super().__init__(**kwargs) + self._init_actions([SearchAndSummarize(engine=self.engine)]) def set_search_func(self, search_func): """Sets a custom search function for the searcher.""" diff --git a/metagpt/schema.py b/metagpt/schema.py index 3374a7241..60aa819b0 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -5,18 +5,17 @@ @Author : alexanderwu @File : schema.py """ -from __future__ import annotations from dataclasses import dataclass, field -from typing import Type, TypedDict -import copy +from typing import Type, TypedDict, Union, Optional -from pydantic import BaseModel +from pydantic import BaseModel, Field +from pydantic.main import ModelMetaclass from metagpt.logs import logger -# from metagpt.utils.serialize import actionoutout_schema_to_mapping -# from metagpt.actions.action_output import ActionOutput -# from metagpt.actions.action import Action +from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ + actionoutput_str_to_mapping +from metagpt.utils.utils import import_class class RawMessage(TypedDict): @@ -24,16 +23,72 @@ class RawMessage(TypedDict): role: str -@dataclass -class Message: - """list[: ]""" - content: str - instruct_content: BaseModel = field(default=None) - role: str = field(default='user') # system / user / assistant - cause_by: Type["Action"] = field(default="") - sent_from: str = field(default="") - send_to: str = field(default="") - restricted_to: str = field(default="") +class Message(BaseModel): + content: str = "" + instruct_content: BaseModel = Field(default=None) + role: str = "user" # system / user / assistant + cause_by: Type["Action"] = Field(default=None) + sent_from: str = "" + send_to: str = "" + restricted_to: str = "" + + def __init__(self, **kwargs): + instruct_content = kwargs.get("instruct_content", None) + cause_by = kwargs.get("cause_by", None) + if instruct_content and not isinstance(instruct_content, BaseModel): + ic = instruct_content + mapping = actionoutput_str_to_mapping(ic["mapping"]) + + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) + ic_new = ic_obj(**ic["value"]) + kwargs["instruct_content"] = ic_new + if cause_by and not isinstance(cause_by, ModelMetaclass): + action_class = import_class("Action", "metagpt.actions.action") + kwargs["cause_by"] = action_class.deser_class(cause_by) + super(Message, self).__init__(**kwargs) + + def dict(self, + *, + include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, + exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, + by_alias: bool = False, + skip_defaults: Optional[bool] = None, + exclude_unset: bool = False, + exclude_defaults: bool = False, + exclude_none: bool = False) -> "DictStrAny": + """ overwrite the `dict` to dump dynamic pydantic model""" + obj_dict = super(Message, self).dict(include=include, + exclude=exclude, + by_alias=by_alias, + skip_defaults=skip_defaults, + exclude_unset=exclude_unset, + exclude_defaults=exclude_defaults, + exclude_none=exclude_none) + ic = self.instruct_content # deal custom-defined action + if ic: + schema = ic.schema() + mapping = actionoutout_schema_to_mapping(schema) + mapping = actionoutput_mapping_to_str(mapping) + + obj_dict["instruct_content"] = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + cb = self.cause_by + if cb: + obj_dict["cause_by"] = cb.ser_class() + return obj_dict + +# +# +# @dataclass +# class Message: +# """list[: ]""" +# content: str +# instruct_content: BaseModel = field(default=None) +# role: str = field(default='user') # system / user / assistant +# cause_by: Type["Action"] = field(default="") +# sent_from: str = field(default="") +# send_to: str = field(default="") +# restricted_to: str = field(default="") def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) @@ -42,45 +97,16 @@ class Message: def __repr__(self): return self.__str__() - # def serialize(self): - # message_cp: Message = copy.deepcopy(self) - # ic = message_cp.instruct_content - # if ic: - # # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly - # schema = ic.schema() - # mapping = actionoutout_schema_to_mapping(schema) - # - # message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} - # cb = message_cp.cause_by - # if cb: - # message_cp.cause_by = cb.serialize() - # - # return message_cp.dict() - # - # @classmethod - # def deserialize(cls, message_dict: dict): - # instruct_content = message_dict.get("instruct_content") - # if instruct_content: - # ic = instruct_content - # ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) - # ic_new = ic_obj(**ic["value"]) - # message_dict.instruct_content = ic_new - # cause_by = message_dict.get("cause_by") - # if cause_by: - # message_dict.cause_by = Action.deserialize(cause_by) - # - # return Message(**message_dict) - - def dict(self): - return { - "content": self.content, - "instruct_content": self.instruct_content, - "role": self.role, - "cause_by": self.cause_by, - "sent_from": self.sent_from, - "send_to": self.send_to, - "restricted_to": self.restricted_to - } + # def dict(self): + # return { + # "content": self.content, + # "instruct_content": self.instruct_content, + # "role": self.role, + # "cause_by": self.cause_by, + # "sent_from": self.sent_from, + # "send_to": self.send_to, + # "restricted_to": self.restricted_to + # } def to_dict(self) -> dict: return { diff --git a/metagpt/team.py b/metagpt/team.py index 3b76e5ff4..795019b92 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -15,7 +15,8 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException -from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.utils import read_json_file, write_json_file, serialize_decorator +from metagpt.const import SERDESER_PATH class Team(BaseModel): @@ -30,29 +31,35 @@ class Team(BaseModel): class Config: arbitrary_types_allowed = True - def serialize(self, stg_path: Path): + def serialize(self, stg_path: Path = None): + stg_path = SERDESER_PATH.joinpath("team") if stg_path is None else stg_path + team_info_path = stg_path.joinpath("team_info.json") - write_json_file(team_info_path, { - "idea": self.idea, - "investment": self.investment - }) + write_json_file(team_info_path, self.dict(exclude={"environment": True})) - self.environment.serialize(stg_path.joinpath("environment")) + self.environment.serialize(stg_path.joinpath("environment")) # save environment alone - def deserialize(self, stg_path: Path): + @classmethod + def recover(cls, stg_path: Path) -> "Team": + return cls.deserialize(stg_path) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Team": """ stg_path = ./storage/team """ # recover team_info team_info_path = stg_path.joinpath("team_info.json") if not team_info_path.exists(): - logger.error("recover storage not exist, not to recover and continue run the old project.") - team_info = read_json_file(team_info_path) - self.investment = team_info.get("investment", 10.0) - self.idea = team_info.get("idea", "") + raise FileNotFoundError("recover storage meta file `team_info.json` not exist, " + "not to recover and please start a new project.") + + team_info: dict = read_json_file(team_info_path) # recover environment - environment_path = stg_path.joinpath("environment") - self.environment = Environment() - self.environment.deserialize(stg_path=environment_path) + environment = Environment.deserialize(stg_path=stg_path.joinpath("environment")) + team_info.update({"environment": environment}) + + team = Team(**team_info) + return team def hire(self, roles: list[Role]): """Hire roles to cooperate""" @@ -76,6 +83,7 @@ class Team(BaseModel): def _save(self): logger.info(self.json()) + @serialize_decorator async def run(self, n_round=3): """Run company until target round or no money""" while n_round > 0: @@ -85,4 +93,3 @@ class Team(BaseModel): self._check_balance() await self.environment.run() return self.environment.history - \ No newline at end of file diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 56a866f2e..9a7049214 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -5,9 +5,7 @@ import copy import pickle -from metagpt.actions.action_output import ActionOutput -from metagpt.schema import Message -from metagpt.actions.action import Action +from metagpt.utils.utils import import_class def actionoutout_schema_to_mapping(schema: dict) -> dict: @@ -59,7 +57,7 @@ def actionoutput_str_to_mapping(mapping: dict) -> dict: return new_mapping -def serialize_general_message(message: Message) -> dict: +def serialize_general_message(message: "Message") -> dict: """ serialize Message, not to save""" message_cp = copy.deepcopy(message) ic = message_cp.instruct_content @@ -76,7 +74,7 @@ def serialize_general_message(message: Message) -> dict: return message_cp.dict() -def serialize_message(message: Message): +def serialize_message(message: "Message"): message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference ic = message_cp.instruct_content if ic: @@ -90,29 +88,35 @@ def serialize_message(message: Message): return msg_ser -def deserialize_general_message(message_dict: dict) -> Message: +def deserialize_general_message(message_dict: dict) -> "Message": """ deserialize Message, not to load""" instruct_content = message_dict.pop("instruct_content") cause_by = message_dict.pop("cause_by") - message = Message(**message_dict) + message_cls = import_class("Message", "metagpt.schema") + message = message_cls(**message_dict) if instruct_content: ic = instruct_content mapping = actionoutput_str_to_mapping(ic["mapping"]) - ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=mapping) + + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new if cause_by: - message.cause_by = Action.deser_class(cause_by) + action_class = import_class("Action", "metagpt.actions.action") + message.cause_by = action_class.deser_class(cause_by) return message -def deserialize_message(message_ser: str) -> Message: +def deserialize_message(message_ser: str) -> "Message": message = pickle.loads(message_ser) if message.instruct_content: ic = message.instruct_content - ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 81ceea884..1cf618ba0 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -6,6 +6,9 @@ from typing import Any import json from pathlib import Path import importlib +import traceback + +from metagpt.logs import logger def read_json_file(json_file: str, encoding=None) -> list[Any]: @@ -39,3 +42,43 @@ def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> obj a_class = import_class(class_name, module_name) class_inst = a_class(*args, **kwargs) return class_inst + + +def format_trackback_info(limit: int = 2): + return traceback.format_exc(limit=limit) + + +def serialize_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + except Exception as exp: + logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + + return wrapper + + +def role_raise_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") + if self._rc.env: + newest_msgs = self._rc.env.memory.get(1) + if len(newest_msgs) > 0: + self._rc.memory.delete(newest_msgs[0]) + except Exception as exp: + if self._rc.env: + newest_msgs = self._rc.env.memory.get(1) + if len(newest_msgs) > 0: + logger.warning("There is a exception in role's execution, in order to resume, " + "we delete the newest role communication message in the role's memory.") + self._rc.memory.delete(newest_msgs[0]) # remove newest msg of the role to make it observed again + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside + + return wrapper diff --git a/startup.py b/startup.py index 9f753d553..c4928a1b5 100644 --- a/startup.py +++ b/startup.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- + +from typing import Optional import asyncio - import fire +from pathlib import Path -from metagpt.const import SERDES_PATH from metagpt.roles import ( Architect, Engineer, @@ -22,11 +23,11 @@ async def startup( code_review: bool = False, run_tests: bool = False, implement: bool = True, - recover_path: bool = False, + recover_path: Optional[str] = None, ): """Run a startup. Be a boss.""" - company = Team() if not recover_path: + company = Team() company.hire( [ ProductManager(), @@ -45,8 +46,12 @@ async def startup( # (bug fixing capability comes soon!) company.hire([QaEngineer()]) else: - stg_path = SERDES_PATH.joinpath("team") - company.deserialize(stg_path=stg_path) + # # stg_path = SERDESER_PATH.joinpath("team") + stg_path = Path(recover_path) + if not stg_path.exists() or not str(stg_path).endswith("team"): + raise FileNotFoundError(f"{recover_path} not exists or not endswith `team`") + + company = Team.recover(stg_path=stg_path) idea = company.idea # use original idea company.invest(investment) From 269eee4643728c5e5f0f6ce3efffa854f88c8f5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 19:20:53 +0800 Subject: [PATCH 218/413] fixbug: The assumption that messages in 'memory' have been processed has been revoked. --- metagpt/roles/product_manager.py | 3 +++ metagpt/roles/role.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index bc6771829..966115c0f 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -50,3 +50,6 @@ class ProductManager(Role): else: self._set_state(0) return self._rc.todo + + async def _observe(self, ignore_memory=False) -> int: + return await super(ProductManager, self)._observe(ignore_memory=True) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1c9da7e6c..fe121ed1a 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -218,12 +218,12 @@ class Role: return msg - async def _observe(self) -> int: + async def _observe(self, ignore_memory=False) -> int: """Prepare new messages for processing from the message buffer and other sources.""" # Read unprocessed messages from the msg buffer. news = self._rc.msg_buffer.pop_all() # Store the read messages in your own memory to prevent duplicate processing. - old_messages = self._rc.memory.get() + old_messages = [] if ignore_memory else self._rc.memory.get() self._rc.memory.add_batch(news) # Filter out messages of interest. self._rc.news = [n for n in news if n.cause_by in self._rc.watch and n not in old_messages] From caacfcff7a541b7e69928cb0ed078fd98f89b55b Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 19:30:02 +0800 Subject: [PATCH 219/413] fix ut of serialize_deserialize --- .../serialize_deserialize/test_action.py | 3 +-- .../test_product_manager.py | 1 - .../serialize_deserialize/test_role.py | 10 ++++++++- .../test_serdeser_base.py | 21 +++++++++++++------ .../serialize_deserialize/test_team.py | 2 +- .../serialize_deserialize/test_wrire_prd.py | 4 ++-- .../serialize_deserialize/test_write_code.py | 2 -- .../test_write_design.py | 3 +-- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index b624dff5a..0138d41ce 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -13,14 +13,13 @@ def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + assert "llm" not in ser_action_dict @pytest.mark.asyncio async def test_action_deserialize(): action = Action() serialized_data = action.dict() - assert isinstance(serialized_data["llm"], OpenAIGPTAPI) new_action = Action(**serialized_data) diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 54584cf96..25bc07a11 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -14,7 +14,6 @@ async def test_product_manager_deserialize(): role = ProductManager() ser_role_dict = role.dict(by_alias=True) new_role = ProductManager(**ser_role_dict) - # new_role = ProductManager().deserialize(ser_role_dict) assert new_role.name == "Alice" assert len(new_role._actions) == 1 diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index f260dea3a..c21b9cc2e 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -17,7 +17,15 @@ from metagpt.const import SERDESER_PATH from metagpt.roles.engineer import Engineer from metagpt.utils.utils import format_trackback_info -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path + + +def test_roles(): + role_a = RoleA() + assert len(role_a._rc.watch) == 1 + role_b = RoleB() + assert len(role_a._rc.watch) == 1 + assert len(role_b._rc.watch) == 1 def test_role_serialize(): diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 35bad6cd9..00d894b3d 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -8,6 +8,7 @@ from pathlib import Path from metagpt.actions.action import Action from metagpt.roles.role import Role, RoleReactMode from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.action_output import ActionOutput serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage") @@ -22,21 +23,27 @@ class MockMessage(BaseModel): class ActionPass(Action): name: str = "ActionPass" - async def run(self, messages: list["Message"]): - return "pass" + async def run(self, messages: list["Message"]) -> ActionOutput: + output_mapping = { + "result": (str, ...) + } + pass_class = ActionOutput.create_model_class("pass", output_mapping) + pass_output = ActionOutput("ActionPass run passed", pass_class(**{"result": "pass result"})) + + return pass_output class ActionOK(Action): name: str = "ActionOK" - async def run(self, messages: list["Message"]): + async def run(self, messages: list["Message"]) -> str: return "ok" class ActionRaise(Action): name: str = "ActionRaise" - async def run(self, messages: list["Message"]): + async def run(self, messages: list["Message"]) -> str: raise RuntimeError("parse error in ActionRaise") @@ -48,7 +55,8 @@ class RoleA(Role): constraints: str = "RoleA's constraints" def __init__(self, **kwargs): - super(RoleA, self).__init__(**kwargs) + # super(RoleA, self).__init__(**kwargs) + super().__init__(**kwargs) self._init_actions([ActionPass]) self._watch([BossRequirement]) @@ -63,7 +71,8 @@ class RoleB(Role): constraints: str = "RoleB's constraints" def __init__(self, **kwargs): - super(RoleB, self).__init__(**kwargs) + # super(RoleB, self).__init__(**kwargs) + super().__init__(**kwargs) self._init_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self._rc.react_mode = RoleReactMode.BY_ORDER diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index e9122ebc0..b8972135b 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -11,7 +11,7 @@ from metagpt.roles import ProjectManager, ProductManager, Architect from metagpt.team import Team from metagpt.const import SERDESER_PATH -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK def test_team_deserialize(): diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 96b4d19ad..05a86cb7f 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -21,7 +21,7 @@ async def test_action_deserialize(): action = WritePRD() serialized_data = action.dict() new_action = WritePRD(**serialized_data) - # new_action = WritePRD().deserialize(serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 + action_output = await new_action.run([Message(content="write a cli snake game")]) + assert len(action_output.content) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 7f4799014..4e3b712c0 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -27,7 +27,6 @@ async def test_write_code_deserialize(): action = WriteCode() serialized_data = action.dict() new_action = WriteCode(**serialized_data) - # new_action = WriteCode().deserialize(serialized_data) assert new_action.name == "WriteCode" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game", filename="test_code") @@ -38,7 +37,6 @@ async def test_write_code_review_deserialize(): action = WriteCodeReview() serialized_data = action.dict() new_action = WriteCodeReview(**serialized_data) - # new_action = WriteCodeReview().deserialize(serialized_data) code = await WriteCode().run(context="write a cli snake game", filename="test_code") assert new_action.name == "WriteCodeReview" diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index e6e236676..5b2a30ed3 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -26,7 +26,7 @@ def test_write_task_serialize(): async def test_write_design_deserialize(): action = WriteDesign() serialized_data = action.dict() - new_action = WriteDesign().deserialize(serialized_data) + new_action = WriteDesign(**serialized_data) assert new_action.name == "" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") @@ -37,7 +37,6 @@ async def test_write_task_deserialize(): action = WriteTasks() serialized_data = action.dict() new_action = WriteTasks(**serialized_data) - # new_action = WriteTasks().deserialize(serialized_data) assert new_action.name == "CreateTasks" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") From c70c8358d334d8297a0a33b95223d604c84096cd Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 19:31:26 +0800 Subject: [PATCH 220/413] fix actions/roles ser&deser --- metagpt/actions/search_and_summarize.py | 16 +++++++--------- metagpt/actions/write_prd.py | 15 ++++++--------- metagpt/roles/role.py | 20 ++++++++++++++------ metagpt/utils/utils.py | 4 +++- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 6b0c1f717..32444b302 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -117,23 +117,21 @@ class SearchAndSummarize(Action): @root_validator def validate_engine_and_run_func(cls, values): - engine = values.get('engine') - search_func = values.get('search_func') + engine = values.get("engine") + search_func = values.get("search_func") config = Config() if engine is None: engine = config.search_engine - config_data = { - 'engine': engine, - 'run_func': search_func - } - search_engine = SearchEngine(**config_data) + try: + search_engine = SearchEngine(engine=engine, run_func=search_func) + except pydantic.ValidationError: + search_engine = None - values['search_engine'] = search_engine + values["search_engine"] = search_engine return values async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: - print(context) if self.search_engine is None: logger.warning("Configure one of SERPAPI_API_KEY, SERPER_API_KEY, GOOGLE_API_KEY to unlock full feature") return "" diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 450bed7e7..86f0ad9a6 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -226,17 +226,14 @@ class WritePRD(Action): name: str = "" content: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) - assistant_search_action: Action = None async def run(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: - # self.assistant_search_action = SearchAndSummarize() - if self.assistant_search_action is None: - self.assistant_search_action = SearchAndSummarize() - # self.assistant_search_action = SearchAndSummarize() - rsp = await self.assistant_search_action.run(context=requirements) - info = f"### Search Results\n{self.assistant_search_action.result}\n\n### Search Summary\n{rsp}" - if self.assistant_search_action.result: - logger.info(self.assistant_search_action.result) + sas = SearchAndSummarize() + # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) + rsp = "" + info = f"### Search Results\n{sas.result}\n\n### Search Summary\n{rsp}" + if sas.result: + logger.info(sas.result) logger.info(rsp) prompt_template, format_example = get_template(templates, format) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b6332aa4c..38f564caa 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -88,7 +88,7 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" - env: "Environment" = Field(default=None) + env: "Environment" = Field(default=None, exclude=True) memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None @@ -133,7 +133,7 @@ class Role(BaseModel): _role_id: str = "" _states: list[str] = Field(default=[]) _actions: list[Action] = Field(default=[]) - _rc: RoleContext = RoleContext() + _rc: RoleContext = Field(default=RoleContext, exclude=True) # builtin variables recovered: bool = False # to tag if a recovered role @@ -143,7 +143,8 @@ class Role(BaseModel): "_llm": LLM() if not is_human else HumanProvider(), "_role_id": _role_id, "_states": [], - "_actions": [] + "_actions": [], + "_rc": RoleContext() } class Config: @@ -169,6 +170,8 @@ class Role(BaseModel): self._private_attributes["_setting"] = RoleSetting(name=self.name, profile=self.profile, goal=self.goal, desc=self.desc, constraints=self.constraints, is_human=self.is_human) + self._private_attributes["_role_id"] = str(self._setting) + for key in self._private_attributes.keys(): if key in kwargs: object.__setattr__(self, key, kwargs[key]) @@ -176,10 +179,15 @@ class Role(BaseModel): setting = RoleSetting(**kwargs[key]) object.__setattr__(self, "_setting", setting) elif key == "_rc": - _rc = RoleContext + _rc = RoleContext() object.__setattr__(self, "_rc", _rc) else: - object.__setattr__(self, key, self._private_attributes[key]) + if key == "_rc": + # # Warning, if use self._private_attributes["_rc"], + # # self._rc will be a shared object between roles, so init one or reset it inside `_reset` + object.__setattr__(self, key, RoleContext()) + else: + object.__setattr__(self, key, self._private_attributes[key]) # deserialize child classes dynamically for inherited `role` object.__setattr__(self, "builtin_class_name", self.__class__.__name__) @@ -192,6 +200,7 @@ class Role(BaseModel): def _reset(self): object.__setattr__(self, "_states", []) object.__setattr__(self, "_actions", []) + # object.__setattr__(self, "_rc", RoleContext()) def serialize(self, stg_path: Path = None): stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ @@ -289,7 +298,6 @@ class Role(BaseModel): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - # import pdb; pdb.set_trace() i = action(name="", llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 1cf618ba0..b72dabf7e 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -51,7 +51,9 @@ def format_trackback_info(limit: int = 2): def serialize_decorator(func): async def wrapper(self, *args, **kwargs): try: - return await func(self, *args, **kwargs) + result = await func(self, *args, **kwargs) + self.serialize() # Team.serialize + return result except KeyboardInterrupt as kbi: logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") self.serialize() # Team.serialize From 5c149efee77c6a3c90382de7221f1370eab7d94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 30 Nov 2023 19:33:27 +0800 Subject: [PATCH 221/413] fixbug: The assumption that messages in 'memory' have been processed has been revoked. --- metagpt/roles/qa_engineer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 763ab6a3f..de09cc4f0 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -167,3 +167,6 @@ class QaEngineer(Role): sent_from=self.profile, send_to=MESSAGE_ROUTE_TO_NONE, ) + + async def _observe(self, ignore_memory=False) -> int: + return await super(QaEngineer, self)._observe(ignore_memory=True) From 6208400f71ee926ed422aed9ed2cc160d7a0de4e Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 21:42:09 +0800 Subject: [PATCH 222/413] fix role._rc init --- metagpt/environment.py | 4 ++++ metagpt/roles/role.py | 11 ++++++----- .../serialize_deserialize/test_team.py | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index bade53f50..bff12210d 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -31,6 +31,7 @@ class Environment(BaseModel): arbitrary_types_allowed = True def __init__(self, **kwargs): + roles = [] for role_key, role in kwargs.get("roles", {}).items(): current_role = kwargs["roles"][role_key] if isinstance(current_role, dict): @@ -41,8 +42,11 @@ class Environment(BaseModel): current_role = subclass(**current_role) break kwargs["roles"][role_key] = current_role + roles.append(current_role) super().__init__(**kwargs) + self.add_roles(roles) # add_roles again to init the Role.set_env + def serialize(self, stg_path: Path): roles_path = stg_path.joinpath("roles.json") roles_info = [] diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 38f564caa..b78597d01 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -88,13 +88,14 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" + # # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison` env: "Environment" = Field(default=None, exclude=True) memory: Memory = Field(default_factory=Memory) - long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) + long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory, exclude=True) # TODO not used now state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None) watch: set[Type[Action]] = Field(default_factory=set) - news: list[Type[Message]] = Field(default=[]) + news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes max_react_loop: int = 1 @@ -128,12 +129,12 @@ class Role(BaseModel): desc: str = "" is_human: bool = False - _llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) + _llm: BaseGPTAPI = Field(default_factory=LLM) _setting: RoleSetting = Field(default_factory=RoleSetting, alias=True) _role_id: str = "" _states: list[str] = Field(default=[]) _actions: list[Action] = Field(default=[]) - _rc: RoleContext = Field(default=RoleContext, exclude=True) + _rc: RoleContext = Field(default=RoleContext) # builtin variables recovered: bool = False # to tag if a recovered role @@ -179,7 +180,7 @@ class Role(BaseModel): setting = RoleSetting(**kwargs[key]) object.__setattr__(self, "_setting", setting) elif key == "_rc": - _rc = RoleContext() + _rc = RoleContext(**kwargs["_rc"]) object.__setattr__(self, "_rc", _rc) else: if key == "_rc": diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index b8972135b..e5ec20f2e 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -39,7 +39,7 @@ def test_team_deserialize(): assert new_company.environment.get_role(arch.profile) is not None -def test_team_serdeser(): +def test_team_serdeser_save(): company = Team() company.hire([RoleC()]) @@ -60,12 +60,19 @@ async def test_team_recover(): shutil.rmtree(stg_path, ignore_errors=True) company = Team() - company.hire([RoleC()]) + role_c = RoleC() + company.hire([role_c]) company.start_project(idea) await company.run(n_round=4) ser_data = company.dict() new_company = Team(**ser_data) + + new_role_c = new_company.environment.get_role(role_c.profile) + assert new_role_c._rc.memory == role_c._rc.memory + assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env.memory == role_c._rc.env.memory + assert new_company.environment.memory.count() == 1 assert type(list(new_company.environment.roles.values())[0]._actions[0]) == ActionOK @@ -80,11 +87,17 @@ async def test_team_recover_save(): shutil.rmtree(stg_path, ignore_errors=True) company = Team() - company.hire([RoleC()]) + role_c = RoleC() + company.hire([role_c]) company.start_project(idea) await company.run(n_round=4) new_company = Team.recover(stg_path) + new_role_c = new_company.environment.get_role(role_c.profile) + assert new_role_c._rc.memory == role_c._rc.memory + assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env.memory == role_c._rc.env.memory + new_company.start_project(idea) await new_company.run(n_round=4) From 053eac62bcd990b748a4ce4578345880d882b276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 1 Dec 2023 13:01:43 +0800 Subject: [PATCH 223/413] feat: +annotation --- metagpt/roles/qa_engineer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index de09cc4f0..f2e011ffd 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -169,4 +169,6 @@ class QaEngineer(Role): ) async def _observe(self, ignore_memory=False) -> int: + # This role has events that trigger and execute themselves based on conditions, and cannot rely on the + # content of memory to activate. return await super(QaEngineer, self)._observe(ignore_memory=True) From f563b2c60809d45db87387956586acd18ddc9201 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 14:43:45 +0800 Subject: [PATCH 224/413] simplify some ser&desr code --- metagpt/actions/action.py | 20 ++----- metagpt/environment.py | 6 +- metagpt/memory/memory.py | 18 +----- metagpt/roles/role.py | 114 ++++++++++++++------------------------ metagpt/schema.py | 42 +------------- 5 files changed, 54 insertions(+), 146 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 7a7f194f4..692a2a6e5 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -52,6 +52,12 @@ class Action(BaseModel): super().__init_subclass__(**kwargs) action_subclass_registry[cls.__name__] = cls + def dict(self, *args, **kwargs) -> "DictStrAny": + obj_dict = super(Action, self).dict(*args, **kwargs) + if "llm" in obj_dict: + obj_dict.pop("llm") + return obj_dict + def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix @@ -63,20 +69,6 @@ class Action(BaseModel): def __repr__(self): return self.__str__() - def serialize(self): - return { - "action_class": self.__class__.__name__, - "module_name": self.__module__, - "name": self.name - } - - @classmethod - def deserialize(cls, action_dict: dict) -> "Action": - action_class_str = action_dict.pop("action_class") - module_name = action_dict.pop("module_name") - action_class = import_class(action_class_str, module_name) - return action_class(**action_dict) - @classmethod def ser_class(cls) -> dict: """ serialize class type""" diff --git a/metagpt/environment.py b/metagpt/environment.py index bff12210d..3174cfc10 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -70,10 +70,8 @@ class Environment(BaseModel): roles_info = read_json_file(roles_path) roles = [] for role_info in roles_info: - role_class = role_info.get("role_class") - role_name = role_info.get("role_name") - - role_path = stg_path.joinpath(f"roles/{role_class}_{role_name}") + # role stored in ./environment/roles/{role_class}_{role_name} + role_path = stg_path.joinpath(f'roles/{role_info.get("role_class")}_{role_info.get("role_name")}') role = Role.deserialize(role_path) roles.append(role) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index c88cc750e..ed30cde18 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -36,23 +36,9 @@ class Memory(BaseModel): super(Memory, self).__init__(**kwargs) self.index = new_index - def dict(self, - *, - include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, - exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, - by_alias: bool = False, - skip_defaults: Optional[bool] = None, - exclude_unset: bool = False, - exclude_defaults: bool = False, - exclude_none: bool = False) -> "DictStrAny": + def dict(self, *args, **kwargs) -> "DictStrAny": """ overwrite the `dict` to dump dynamic pydantic model""" - obj_dict = super(Memory, self).dict(include=include, - exclude=exclude, - by_alias=by_alias, - skip_defaults=skip_defaults, - exclude_unset=exclude_unset, - exclude_defaults=exclude_defaults, - exclude_none=exclude_none) + obj_dict = super(Memory, self).dict(*args, **kwargs) new_obj_dict = copy.deepcopy(obj_dict) new_obj_dict["index"] = {} for action, value in obj_dict["index"].items(): diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b78597d01..4e669772e 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -93,7 +93,7 @@ class RoleContext(BaseModel): memory: Memory = Field(default_factory=Memory) long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory, exclude=True) # TODO not used now state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None - todo: Action = Field(default=None) + todo: Action = Field(default=None, exclude=True) watch: set[Type[Action]] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes @@ -101,7 +101,25 @@ class RoleContext(BaseModel): class Config: arbitrary_types_allowed = True - + + def __init__(self, **kwargs): + watch_info = kwargs.get("watch", set()) + watch = set() + for item in watch_info: + action = Action.deser_class(item) + watch.update([action]) + kwargs["watch"] = watch + super(RoleContext, self).__init__(**kwargs) + + def dict(self, *args, **kwargs) -> "DictStrAny": + obj_dict = super(RoleContext, self).dict(*args, **kwargs) + watch = obj_dict.get("watch", set()) + watch_info = [] + for item in watch: + watch_info.append(item.ser_class()) + obj_dict["watch"] = watch_info + return obj_dict + def check(self, role_id: str): if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: self.long_term_memory.recover_memory(role_id, self) @@ -130,7 +148,6 @@ class Role(BaseModel): is_human: bool = False _llm: BaseGPTAPI = Field(default_factory=LLM) - _setting: RoleSetting = Field(default_factory=RoleSetting, alias=True) _role_id: str = "" _states: list[str] = Field(default=[]) _actions: list[Action] = Field(default=[]) @@ -168,18 +185,12 @@ class Role(BaseModel): # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 self._private_attributes["_llm"] = LLM() if not self.is_human else HumanProvider() - self._private_attributes["_setting"] = RoleSetting(name=self.name, profile=self.profile, goal=self.goal, - desc=self.desc, constraints=self.constraints, - is_human=self.is_human) self._private_attributes["_role_id"] = str(self._setting) for key in self._private_attributes.keys(): if key in kwargs: object.__setattr__(self, key, kwargs[key]) - if key == "_setting": - setting = RoleSetting(**kwargs[key]) - object.__setattr__(self, "_setting", setting) - elif key == "_rc": + if key == "_rc": _rc = RoleContext(**kwargs["_rc"]) object.__setattr__(self, "_rc", _rc) else: @@ -203,41 +214,23 @@ class Role(BaseModel): object.__setattr__(self, "_actions", []) # object.__setattr__(self, "_rc", RoleContext()) + @property + def _setting(self): + return f"{self.name}({self.profile})" + def serialize(self, stg_path: Path = None): stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ if stg_path is None else stg_path - role_info_path = stg_path.joinpath("role_info.json") - role_info = { + + role_info = self.dict(exclude={"_rc": {"memory": True}, "_llm": True}) + role_info.update({ "role_class": self.__class__.__name__, "module_name": self.__module__ - } - setting = self._setting.dict() - setting.pop("desc") - setting.pop("is_human") # not all inherited roles have this atrr - role_info.update(setting) + }) + role_info_path = stg_path.joinpath("role_info.json") write_json_file(role_info_path, role_info) - actions_info_path = stg_path.joinpath("actions/actions_info.json") - actions_info = [] - for action in self._actions: - actions_info.append(action.serialize()) - write_json_file(actions_info_path, actions_info) - - watches_info_path = stg_path.joinpath("watches/watches_info.json") - watches_info = [] - for watch in self._rc.watch: - watches_info.append(watch.ser_class()) - write_json_file(watches_info_path, watches_info) - - actions_todo_path = stg_path.joinpath("actions/todo.json") - actions_todo = { - "cur_state": self._rc.state, - "react_mode": self._rc.react_mode.value, - "max_react_loop": self._rc.max_react_loop - } - write_json_file(actions_todo_path, actions_todo) - - self._rc.memory.serialize(stg_path) + self._rc.memory.serialize(stg_path) # serialize role's memory alone @classmethod def deserialize(cls, stg_path: Path) -> "Role": @@ -250,35 +243,7 @@ class Role(BaseModel): role_class = import_class(class_name=role_class_str, module_name=module_name) role = role_class(**role_info) # initiate particular Role - actions_info_path = stg_path.joinpath("actions/actions_info.json") - actions = [] - actions_info = read_json_file(actions_info_path) - for action_info in actions_info: - action = Action.deser_class(action_info) - actions.append(action) - - watches_info_path = stg_path.joinpath("watches/watches_info.json") - watches = [] - watches_info = read_json_file(watches_info_path) - for watch_info in watches_info: - action = Action.deser_class(watch_info) - watches.append(action) - - role.init_actions(actions) - role.watch(watches) - - actions_todo_path = stg_path.joinpath("actions/todo.json") - # recover self._rc.state - actions_todo = read_json_file(actions_todo_path) - max_react_loop = actions_todo.get("max_react_loop", 1) - cur_state = actions_todo.get("cur_state", -1) - role.set_state(cur_state) - role.set_recovered(True) - react_mode_str = actions_todo.get("react_mode", RoleReactMode.REACT.value) - if react_mode_str not in RoleReactMode.values(): - logger.warning(f"ReactMode: {react_mode_str} not in {RoleReactMode.values()}, use react as default") - react_mode_str = RoleReactMode.REACT.value - role.set_react_mode(RoleReactMode(react_mode_str), max_react_loop) + role.set_recovered(True) # set True to make a tag role_memory = Memory.deserialize(stg_path) role.set_memory(role_memory) @@ -299,9 +264,9 @@ class Role(BaseModel): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - i = action(name="", llm=self._llm) + i = action(llm=self._llm) else: - if self._setting.is_human and not isinstance(action.llm, HumanProvider): + if self.is_human and not isinstance(action.llm, HumanProvider): logger.warning(f"is_human attribute does not take effect," f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances") i = action @@ -357,9 +322,14 @@ class Role(BaseModel): def _get_prefix(self): """Get the role prefix""" - if self._setting.desc: - return self._setting.desc - return PREFIX_TEMPLATE.format(**self._setting.dict()) + if self.desc: + return self.desc + return PREFIX_TEMPLATE.format(**{ + "profile": self.profile, + "name": self.name, + "goal": self.goal, + "constraints": self.constraints + }) async def _think(self) -> None: """Think about what to do and decide on the next action""" diff --git a/metagpt/schema.py b/metagpt/schema.py index 60aa819b0..3a5bea7e9 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -48,23 +48,9 @@ class Message(BaseModel): kwargs["cause_by"] = action_class.deser_class(cause_by) super(Message, self).__init__(**kwargs) - def dict(self, - *, - include: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, - exclude: Optional[Union["AbstractSetIntStr", "MappingIntStrAny"]] = None, - by_alias: bool = False, - skip_defaults: Optional[bool] = None, - exclude_unset: bool = False, - exclude_defaults: bool = False, - exclude_none: bool = False) -> "DictStrAny": + def dict(self, *args, **kwargs) -> "DictStrAny": """ overwrite the `dict` to dump dynamic pydantic model""" - obj_dict = super(Message, self).dict(include=include, - exclude=exclude, - by_alias=by_alias, - skip_defaults=skip_defaults, - exclude_unset=exclude_unset, - exclude_defaults=exclude_defaults, - exclude_none=exclude_none) + obj_dict = super(Message, self).dict(*args, **kwargs) ic = self.instruct_content # deal custom-defined action if ic: schema = ic.schema() @@ -77,19 +63,6 @@ class Message(BaseModel): obj_dict["cause_by"] = cb.ser_class() return obj_dict -# -# -# @dataclass -# class Message: -# """list[: ]""" -# content: str -# instruct_content: BaseModel = field(default=None) -# role: str = field(default='user') # system / user / assistant -# cause_by: Type["Action"] = field(default="") -# sent_from: str = field(default="") -# send_to: str = field(default="") -# restricted_to: str = field(default="") - def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) return f"{self.role}: {self.content}" @@ -97,17 +70,6 @@ class Message(BaseModel): def __repr__(self): return self.__str__() - # def dict(self): - # return { - # "content": self.content, - # "instruct_content": self.instruct_content, - # "role": self.role, - # "cause_by": self.cause_by, - # "sent_from": self.sent_from, - # "send_to": self.send_to, - # "restricted_to": self.restricted_to - # } - def to_dict(self) -> dict: return { "role": self.role, From 0e8eda683e991f8ea7f80ccb09da2fc9a208a265 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 14:45:06 +0800 Subject: [PATCH 225/413] update ut after simplification --- tests/metagpt/serialize_deserialize/test_action.py | 14 +------------- tests/metagpt/serialize_deserialize/test_role.py | 3 --- .../serialize_deserialize/test_serdeser_base.py | 6 +++--- tests/metagpt/serialize_deserialize/test_team.py | 2 +- .../serialize_deserialize/test_wrire_prd.py | 2 +- .../serialize_deserialize/test_write_code.py | 4 ++-- .../serialize_deserialize/test_write_design.py | 4 ++-- 7 files changed, 10 insertions(+), 25 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 0138d41ce..16369bb61 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -13,7 +13,7 @@ def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" not in ser_action_dict + # assert "llm" not in ser_action_dict # not export @pytest.mark.asyncio @@ -34,15 +34,3 @@ def test_action_serdeser(): action_class = Action.deser_class(action_info) assert action_class == WriteTest - - -def test_action_class_serdeser(): - name = "write test" - action_info = WriteTest(name=name).serialize() - assert action_info["name"] == name - - action_info = WriteTest(name=name, llm=LLM()).serialize() - assert action_info["name"] == name - - action = Action.deserialize(action_info) - assert action.name == name diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index c21b9cc2e..61684ba9d 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -66,7 +66,6 @@ def test_role_serdeser_save(): role_tag = f"{pm.__class__.__name__}_{pm.name}" stg_path = stg_path_prefix.joinpath(role_tag) pm.serialize(stg_path) - assert stg_path.joinpath("actions/actions_info.json").exists() new_pm = Role.deserialize(stg_path) assert new_pm.name == pm.name @@ -89,8 +88,6 @@ async def test_role_serdeser_interrupt(): assert role_c._rc.memory.count() == 2 - assert stg_path.joinpath("actions/todo.json").exists() - new_role_a: Role = Role.deserialize(stg_path) assert new_role_a._rc.state == 1 diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 00d894b3d..74f9fea87 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -21,7 +21,7 @@ class MockMessage(BaseModel): class ActionPass(Action): - name: str = "ActionPass" + name: str = Field(default="ActionPass") async def run(self, messages: list["Message"]) -> ActionOutput: output_mapping = { @@ -34,14 +34,14 @@ class ActionPass(Action): class ActionOK(Action): - name: str = "ActionOK" + name: str = Field(default="ActionOK") async def run(self, messages: list["Message"]) -> str: return "ok" class ActionRaise(Action): - name: str = "ActionRaise" + name: str = Field(default="ActionRaise") async def run(self, messages: list["Message"]) -> str: raise RuntimeError("parse error in ActionRaise") diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index e5ec20f2e..28728e1b5 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -70,7 +70,7 @@ async def test_team_recover(): new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env == role_c._rc.env # TODO check again assert new_role_c._rc.env.memory == role_c._rc.env.memory assert new_company.environment.memory.count() == 1 diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 05a86cb7f..0b9dfa9d8 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -13,7 +13,7 @@ def test_action_serialize(): action = WritePRD() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 4e3b712c0..5552ffd7f 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -12,14 +12,14 @@ def test_write_design_serialize(): action = WriteCode() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCode" - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export def test_write_task_serialize(): action = WriteCodeReview() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCodeReview" - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 5b2a30ed3..080896c98 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -12,14 +12,14 @@ def test_write_design_serialize(): action = WriteDesign() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export def test_write_task_serialize(): action = WriteTasks() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio From c7a5bea2b157d2fca2641369a14a415fd935f83f Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 15:30:28 +0800 Subject: [PATCH 226/413] update --- tests/metagpt/serialize_deserialize/test_team.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 28728e1b5..9c4eb8170 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -70,7 +70,7 @@ async def test_team_recover(): new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env == role_c._rc.env # TODO check again + assert new_role_c._rc.env == role_c._rc.env assert new_role_c._rc.env.memory == role_c._rc.env.memory assert new_company.environment.memory.count() == 1 @@ -95,7 +95,10 @@ async def test_team_recover_save(): new_company = Team.recover(stg_path) new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env != role_c._rc.env + assert new_role_c.recovered != role_c.recovered # here cause previous ut is `!=` + assert new_role_c._rc.todo != role_c._rc.todo # serialize exclude `_rc.todo` + assert new_role_c._rc.news != role_c._rc.news # serialize exclude `_rc.news` assert new_role_c._rc.env.memory == role_c._rc.env.memory new_company.start_project(idea) From dfc6e13ac3b2888e95574c2ff38b7a038cc9938d Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Fri, 1 Dec 2023 16:10:38 +0800 Subject: [PATCH 227/413] add agent subscription --- metagpt/subscription.py | 101 ++++++++++++++++++++++++++++ tests/conftest.py | 16 ++++- tests/metagpt/test_subscription.py | 102 +++++++++++++++++++++++++++++ 3 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 metagpt/subscription.py create mode 100644 tests/metagpt/test_subscription.py diff --git a/metagpt/subscription.py b/metagpt/subscription.py new file mode 100644 index 000000000..0d2b30821 --- /dev/null +++ b/metagpt/subscription.py @@ -0,0 +1,101 @@ +import asyncio +from typing import AsyncGenerator, Awaitable, Callable + +from pydantic import BaseModel, Field + +from metagpt.logs import logger +from metagpt.roles import Role +from metagpt.schema import Message + + +class SubscriptionRunner(BaseModel): + """A simple wrapper to manage subscription tasks for different roles using asyncio. + + Example: + >>> import asyncio + >>> from metagpt.subscription import SubscriptionRunner + >>> from metagpt.roles import Searcher + >>> from metagpt.schema import Message + + >>> async def trigger(): + ... while True: + ... yield Message("the latest news about OpenAI") + ... await asyncio.sleep(3600 * 24) + + >>> async def callback(msg: Message): + ... print(msg.content) + + >>> async def main(): + ... pb = SubscriptionRunner() + ... await pb.subscribe(Searcher(), trigger(), callback) + ... await pb.run() + + >>> asyncio.run(main()) + """ + + tasks: dict[Role, asyncio.Task] = Field(default_factory=dict) + + class Config: + arbitrary_types_allowed = True + + async def subscribe( + self, + role: Role, + trigger: AsyncGenerator[Message, None], + callback: Callable[ + [ + Message, + ], + Awaitable[None], + ], + ): + """Subscribes a role to a trigger and sets up a callback to be called with the role's response. + + Args: + role: The role to subscribe. + trigger: An asynchronous generator that yields Messages to be processed by the role. + callback: An asynchronous function to be called with the response from the role. + """ + loop = asyncio.get_running_loop() + + async def _start_role(): + async for msg in trigger: + resp = await role.run(msg) + await callback(resp) + + self.tasks[role] = loop.create_task(_start_role(), name=f"Subscription-{role}") + + async def unsubscribe(self, role: Role): + """Unsubscribes a role from its trigger and cancels the associated task. + + Args: + role: The role to unsubscribe. + """ + task = self.tasks.pop(role) + task.cancel() + + async def run(self, raise_exception: bool = True): + """Runs all subscribed tasks and handles their completion or exception. + + Args: + raise_exception: _description_. Defaults to True. + + Raises: + task.exception: _description_ + """ + while True: + for role, task in self.tasks.items(): + if task.done(): + if task.exception(): + if raise_exception: + raise task.exception() + logger.opt(exception=task.exception()).error(f"Task {task.get_name()} run error") + else: + logger.warning( + f"Task {task.get_name()} has completed. " + "If this is unexpected behavior, please check the trigger function." + ) + self.tasks.pop(role) + break + else: + await asyncio.sleep(1) diff --git a/tests/conftest.py b/tests/conftest.py index feecc7715..804c60e71 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,14 +6,15 @@ @File : conftest.py """ +import asyncio +import logging +import re from unittest.mock import Mock import pytest from metagpt.logs import logger from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI -import asyncio -import re class Context: @@ -68,3 +69,14 @@ def proxy(): server = asyncio.get_event_loop().run_until_complete(asyncio.start_server(handle_client, "127.0.0.1", 0)) return "http://{}:{}".format(*server.sockets[0].getsockname()) + + +# see https://github.com/Delgan/loguru/issues/59#issuecomment-466591978 +@pytest.fixture +def loguru_caplog(caplog): + class PropogateHandler(logging.Handler): + def emit(self, record): + logging.getLogger(record.name).handle(record) + + logger.add(PropogateHandler(), format="{message}") + yield caplog diff --git a/tests/metagpt/test_subscription.py b/tests/metagpt/test_subscription.py new file mode 100644 index 000000000..2e898424d --- /dev/null +++ b/tests/metagpt/test_subscription.py @@ -0,0 +1,102 @@ +import asyncio + +import pytest + +from metagpt.roles import Role +from metagpt.schema import Message +from metagpt.subscription import SubscriptionRunner + + +@pytest.mark.asyncio +async def test_subscription_run(): + callback_done = 0 + + async def trigger(): + while True: + yield Message("the latest news about OpenAI") + await asyncio.sleep(3600 * 24) + + class MockRole(Role): + async def run(self, message=None): + return Message("") + + async def callback(message): + nonlocal callback_done + callback_done += 1 + + runner = SubscriptionRunner() + + roles = [] + for _ in range(2): + role = MockRole() + roles.append(role) + await runner.subscribe(role, trigger(), callback) + + task = asyncio.get_running_loop().create_task(runner.run()) + + for _ in range(10): + if callback_done == 2: + break + await asyncio.sleep(0) + else: + raise TimeoutError("callback not call") + + role = roles[0] + assert role in runner.tasks + await runner.unsubscribe(roles[0]) + + for _ in range(10): + if role not in runner.tasks: + break + await asyncio.sleep(0) + else: + raise TimeoutError("callback not call") + + task.cancel() + for i in runner.tasks.values(): + i.cancel() + + +@pytest.mark.asyncio +async def test_subscription_run_error(loguru_caplog): + async def trigger1(): + while True: + yield Message("the latest news about OpenAI") + await asyncio.sleep(3600 * 24) + + async def trigger2(): + yield Message("the latest news about OpenAI") + + class MockRole1(Role): + async def run(self, message=None): + raise RuntimeError + + class MockRole2(Role): + async def run(self, message=None): + return Message("") + + async def callback(msg: Message): + print(msg) + + runner = SubscriptionRunner() + await runner.subscribe(MockRole1(), trigger1(), callback) + with pytest.raises(RuntimeError): + await runner.run() + + await runner.subscribe(MockRole2(), trigger2(), callback) + task = asyncio.get_running_loop().create_task(runner.run(False)) + + for _ in range(10): + if not runner.tasks: + break + await asyncio.sleep(0) + else: + raise TimeoutError("wait runner tasks empty timeout") + + task.cancel() + for i in runner.tasks.values(): + i.cancel() + assert len(loguru_caplog.records) >= 2 + logs = "".join(loguru_caplog.messages) + assert "run error" in logs + assert "has completed" in logs From bcba1393b4e1e3445031cd4779fcb441f1fad8d7 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 20:35:48 +0800 Subject: [PATCH 228/413] update asyncio.sleep to make it async --- .../test_serdeser_base.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 74f9fea87..298c13823 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -4,6 +4,7 @@ from pydantic import BaseModel, Field from pathlib import Path +import asyncio from metagpt.actions.action import Action from metagpt.roles.role import Role, RoleReactMode @@ -24,6 +25,7 @@ class ActionPass(Action): name: str = Field(default="ActionPass") async def run(self, messages: list["Message"]) -> ActionOutput: + await asyncio.sleep(5) # sleep to make other roles can watch the executed Message output_mapping = { "result": (str, ...) } @@ -37,6 +39,7 @@ class ActionOK(Action): name: str = Field(default="ActionOK") async def run(self, messages: list["Message"]) -> str: + await asyncio.sleep(5) return "ok" @@ -55,14 +58,10 @@ class RoleA(Role): constraints: str = "RoleA's constraints" def __init__(self, **kwargs): - # super(RoleA, self).__init__(**kwargs) - super().__init__(**kwargs) + super(RoleA, self).__init__(**kwargs) self._init_actions([ActionPass]) self._watch([BossRequirement]) - async def run(self, message: "Message" = None): - await super(RoleA, self).run(message) - class RoleB(Role): name: str = Field(default="RoleB") @@ -71,15 +70,11 @@ class RoleB(Role): constraints: str = "RoleB's constraints" def __init__(self, **kwargs): - # super(RoleB, self).__init__(**kwargs) - super().__init__(**kwargs) + super(RoleB, self).__init__(**kwargs) self._init_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self._rc.react_mode = RoleReactMode.BY_ORDER - async def run(self, message: "Message" = None): - await super(RoleB, self).run(message) - class RoleC(Role): name: str = Field(default="RoleC") @@ -92,6 +87,3 @@ class RoleC(Role): self._init_actions([ActionOK, ActionRaise]) self._watch([BossRequirement]) self._rc.react_mode = RoleReactMode.BY_ORDER - - async def run(self, message: "Message" = None): - await super(RoleC, self).run(message) From cb81561b69749596b16cdee7e6e3ed4128cd6685 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 21:07:47 +0800 Subject: [PATCH 229/413] fix when RoleReactMode=REACT --- metagpt/roles/role.py | 4 ++-- metagpt/utils/utils.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 4e669772e..5b998bf9a 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -337,9 +337,9 @@ class Role(BaseModel): # If there is only one action, then only this one can be performed self._set_state(0) return - if self._recovered and self._rc.state >= 0: + if self.recovered and self._rc.state >= 0: self._set_state(self._rc.state) # action to run from recovered state - self._recovered = False # avoid max_react_loop out of work + self.recovered = False # avoid max_react_loop out of work return prompt = self._get_prefix() diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index b72dabf7e..c1416c352 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -74,6 +74,7 @@ def role_raise_decorator(func): newest_msgs = self._rc.env.memory.get(1) if len(newest_msgs) > 0: self._rc.memory.delete(newest_msgs[0]) + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside except Exception as exp: if self._rc.env: newest_msgs = self._rc.env.memory.get(1) From 9f9b7ebe17b09d7bd952173e407dca565e064bb4 Mon Sep 17 00:00:00 2001 From: Stitch-z <284618289@qq.com> Date: Sat, 2 Dec 2023 14:39:51 +0800 Subject: [PATCH 230/413] update: optimize the action code for writing tutorials. --- examples/write_tutorial.py | 2 ++ metagpt/roles/tutorial_assistant.py | 30 +++++------------------------ 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/examples/write_tutorial.py b/examples/write_tutorial.py index 71ece5527..8d2b25103 100644 --- a/examples/write_tutorial.py +++ b/examples/write_tutorial.py @@ -1,10 +1,12 @@ #!/usr/bin/env python3 # _*_ coding: utf-8 _*_ + """ @Time : 2023/9/4 21:40:57 @Author : Stitch-z @File : tutorial_assistant.py """ + import asyncio from metagpt.roles.tutorial_assistant import TutorialAssistant diff --git a/metagpt/roles/tutorial_assistant.py b/metagpt/roles/tutorial_assistant.py index 9a7df4f4d..7c9450997 100644 --- a/metagpt/roles/tutorial_assistant.py +++ b/metagpt/roles/tutorial_assistant.py @@ -42,17 +42,7 @@ class TutorialAssistant(Role): self.main_title = "" self.total_content = "" self.language = language - - async def _think(self) -> None: - """Determine the next action to be taken by the role.""" - if self._rc.todo is None: - self._set_state(0) - return - - if self._rc.state + 1 < len(self._states): - self._set_state(self._rc.state + 1) - else: - self._rc.todo = None + self._set_react_mode(react_mode="by_order") async def _handle_directory(self, titles: Dict) -> Message: """Handle the directories for the tutorial document. @@ -75,8 +65,6 @@ class TutorialAssistant(Role): for second_dir in first_dir[key]: directory += f" - {second_dir}\n" self._init_actions(actions) - self._rc.todo = None - return Message(content=directory) async def _act(self) -> Message: """Perform an action as determined by the role. @@ -90,7 +78,8 @@ class TutorialAssistant(Role): self.topic = msg.content resp = await todo.run(topic=self.topic) logger.info(resp) - return await self._handle_directory(resp) + await self._handle_directory(resp) + return await super().react() resp = await todo.run(topic=self.topic) logger.info(resp) if self.total_content != "": @@ -98,17 +87,8 @@ class TutorialAssistant(Role): self.total_content += resp return Message(content=resp, role=self.profile) - async def _react(self) -> Message: - """Execute the assistant's think and actions. - - Returns: - A message containing the final result of the assistant's actions. - """ - while True: - await self._think() - if self._rc.todo is None: - break - msg = await self._act() + async def react(self) -> Message: + msg = await super().react() root_path = TUTORIAL_PATH / datetime.now().strftime("%Y-%m-%d_%H-%M-%S") await File.write(root_path, f"{self.main_title}.md", self.total_content.encode('utf-8')) return msg From 4845dafb94966a502f153a8e5d223b19f60be2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 4 Dec 2023 10:31:02 +0800 Subject: [PATCH 231/413] feat: +log --- tests/metagpt/test_gpt.py | 8 ++++++++ tests/metagpt/test_llm.py | 3 +++ 2 files changed, 11 insertions(+) diff --git a/tests/metagpt/test_gpt.py b/tests/metagpt/test_gpt.py index 285e8134c..431858d4c 100644 --- a/tests/metagpt/test_gpt.py +++ b/tests/metagpt/test_gpt.py @@ -15,6 +15,7 @@ from metagpt.logs import logger class TestGPT: def test_llm_api_ask(self, llm_api): answer = llm_api.ask("hello chatgpt") + logger.info(answer) assert len(answer) > 0 # def test_gptapi_ask_batch(self, llm_api): @@ -23,16 +24,19 @@ class TestGPT: def test_llm_api_ask_code(self, llm_api): answer = llm_api.ask_code(["请扮演一个Google Python专家工程师,如果理解,回复明白", "写一个hello world"]) + logger.info(answer) assert len(answer) > 0 @pytest.mark.asyncio async def test_llm_api_aask(self, llm_api): answer = await llm_api.aask("hello chatgpt") + logger.info(answer) assert len(answer) > 0 @pytest.mark.asyncio async def test_llm_api_aask_code(self, llm_api): answer = await llm_api.aask_code(["请扮演一个Google Python专家工程师,如果理解,回复明白", "写一个hello world"]) + logger.info(answer) assert len(answer) > 0 @pytest.mark.asyncio @@ -41,3 +45,7 @@ class TestGPT: costs = llm_api.get_costs() logger.info(costs) assert costs.total_cost > 0 + + +# if __name__ == "__main__": +# pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/test_llm.py b/tests/metagpt/test_llm.py index 03341212b..49969a2af 100644 --- a/tests/metagpt/test_llm.py +++ b/tests/metagpt/test_llm.py @@ -32,3 +32,6 @@ async def test_llm_acompletion(llm): assert len(await llm.acompletion(hello_msg)) > 0 assert len(await llm.acompletion_batch([hello_msg])) > 0 assert len(await llm.acompletion_batch_text([hello_msg])) > 0 + +# if __name__ == "__main__": +# pytest.main([__file__, "-s"]) From f1e01c5ba8b2246e763da3e7f850f0f4f8a30675 Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 4 Dec 2023 11:12:13 +0800 Subject: [PATCH 232/413] set config value not relay on key.yaml --- tests/metagpt/utils/test_repair_llm_raw_output.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index acacb3af3..a2dd18516 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -2,7 +2,9 @@ # -*- coding: utf-8 -*- # @Desc : unittest of repair_llm_raw_output -import pytest + +from metagpt.config import CONFIG +CONFIG.repair_llm_output = True from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType, repair_invalid_json,\ extract_content_from_output, retry_parse_json_text From fb69c107feaf866f704299b003072baa3f760ef7 Mon Sep 17 00:00:00 2001 From: better629 Date: Mon, 4 Dec 2023 21:56:43 +0800 Subject: [PATCH 233/413] rm useless deepcopy --- metagpt/utils/repair_llm_raw_output.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index f9e6d020d..124bcba89 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -82,8 +82,7 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - if left_key not in output: output = left_key + "\n" + output if right_key not in output: - def judge_potential_json(routput: str, left_key: str) -> Union[str]: - routput = copy.deepcopy(routput) + def judge_potential_json(routput: str, left_key: str) -> Union[str, None]: ridx = routput.rfind(left_key) if ridx < 0: return None From 7833e5767305153b4f2b0fb602f57ab72e6cf035 Mon Sep 17 00:00:00 2001 From: zeeland Date: Tue, 5 Dec 2023 16:30:46 +0800 Subject: [PATCH 234/413] pref: optimize log --- metagpt/logs.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/metagpt/logs.py b/metagpt/logs.py index b2052e9b8..471d57fe9 100644 --- a/metagpt/logs.py +++ b/metagpt/logs.py @@ -7,18 +7,24 @@ """ import sys +from datetime import datetime from loguru import logger as _logger from metagpt.const import PROJECT_ROOT + def define_log_level(print_level="INFO", logfile_level="DEBUG"): """调整日志级别到level之上 Adjust the log level to above level """ + current_date = datetime.now() + formatted_date = current_date.strftime("%Y%m%d") + _logger.remove() _logger.add(sys.stderr, level=print_level) - _logger.add(PROJECT_ROOT / 'logs/log.txt', level=logfile_level) + _logger.add(PROJECT_ROOT / f"logs/{formatted_date}.log", level=logfile_level) return _logger + logger = define_log_level() From 37703253a3d9a46cae573f1c23e44a5e4b342b59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 4 Dec 2023 23:04:07 +0800 Subject: [PATCH 235/413] feat: +SummarizeCode, refactor project_name --- metagpt/actions/design_api.py | 61 ++---- metagpt/actions/prepare_documents.py | 2 +- metagpt/actions/project_management.py | 10 +- metagpt/actions/summarize_code.py | 9 +- metagpt/actions/write_code.py | 20 +- metagpt/actions/write_code_review.py | 3 +- metagpt/actions/write_prd.py | 48 ++++- metagpt/actions/write_test.py | 7 +- metagpt/const.py | 3 + metagpt/provider/base_gpt_api.py | 4 +- metagpt/roles/engineer.py | 134 ++++++++---- metagpt/roles/qa_engineer.py | 12 +- metagpt/roles/role.py | 3 +- metagpt/schema.py | 20 +- metagpt/startup.py | 11 +- metagpt/utils/dependency_file.py | 5 +- metagpt/utils/file_repository.py | 33 +++ metagpt/utils/git_repository.py | 38 +++- tests/conftest.py | 16 ++ tests/metagpt/actions/mock.py | 2 +- tests/metagpt/actions/test_debug_error.py | 86 ++++---- tests/metagpt/actions/test_design_api.py | 26 +-- .../metagpt/actions/test_prepare_documents.py | 30 +++ tests/metagpt/actions/test_run_code.py | 62 +++--- tests/metagpt/actions/test_summarize_code.py | 195 ++++++++++++++++++ tests/metagpt/actions/test_write_code.py | 17 +- .../metagpt/actions/test_write_code_review.py | 12 +- tests/metagpt/actions/test_write_prd.py | 7 +- tests/metagpt/actions/test_write_test.py | 22 +- tests/metagpt/roles/mock.py | 2 +- tests/metagpt/utils/test_file_repository.py | 4 + 31 files changed, 662 insertions(+), 242 deletions(-) create mode 100644 tests/metagpt/actions/test_prepare_documents.py create mode 100644 tests/metagpt/actions/test_summarize_code.py diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index c5787ba20..eb73ed94f 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023/11/27. 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.3 of RFC 135, add incremental iteration functionality. +@Modified By: mashenquan, 2023/12/5. Move the generation logic of the project name to WritePRD. """ import json from pathlib import Path @@ -23,7 +24,6 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.schema import Document, Documents -from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file @@ -43,7 +43,7 @@ Requirement: Fill in the following missing information based on the context, eac ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select appropriate open-source frameworks. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text. ## File list: Provided as Python list[str], the list of files needed (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -58,15 +58,15 @@ and only output the json inside this tag, nothing else """, "FORMAT_EXAMPLE": """ [CONTENT] -{ +{{ "Implementation approach": "We will ...", - "project_name": "snake_game", + "Project name": "{project_name}", "File list": ["main.py"], "Data structures and interfaces": ' classDiagram - class Game{ + class Game{{ +int score - } + }} ... Game "1" -- "1" Food: has ', @@ -77,7 +77,7 @@ and only output the json inside this tag, nothing else G->>M: end game ', "Anything UNCLEAR": "The requirement is clear to me." -} +}} [/CONTENT] """, }, @@ -96,7 +96,7 @@ ATTENTION: Output carefully referenced "Format example" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text. ## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -112,9 +112,9 @@ ATTENTION: Output carefully referenced "Format example" in format. ## Implementation approach We will ... -## project_name +## Project name ```python -"snake_game" +"{project_name}" ``` ## File list @@ -151,7 +151,7 @@ The requirement is clear to me. OUTPUT_MAPPING = { "Implementation approach": (str, ...), - "project_name": (str, ...), + "Project name": (str, ...), "File list": (List[str], ...), "Data structures and interfaces": (str, ...), "Program call flow": (str, ...), @@ -173,7 +173,7 @@ ATTENTION: Output carefully referenced "Old Design" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text "{project_name}". ## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -229,50 +229,21 @@ class WriteDesign(Action): async def _new_system_design(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) + format_example = format_example.format(project_name=CONFIG.project_name) prompt = prompt_template.format(context=context, format_example=format_example) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - self._rename_project_name(system_design=system_design) - await self._rename_workspace(system_design) return system_design async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): - prompt = MERGE_PROMPT.format(old_design=system_design_doc.content, context=prd_doc.content) + prompt = MERGE_PROMPT.format( + old_design=system_design_doc.content, context=prd_doc.content, project_name=CONFIG.project_name + ) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python # package name" contain space, have to use setattr - self._rename_project_name(system_design=system_design) system_design_doc.content = system_design.instruct_content.json(ensure_ascii=False) return system_design_doc - @staticmethod - def _rename_project_name(system_design): - # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" - # contain space, have to use setattr - if CONFIG.project_name: - setattr( - system_design.instruct_content, - "project_name", - CONFIG.project_name, - ) - return - setattr( - system_design.instruct_content, - "project_name", - system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), - ) - - @staticmethod - async def _rename_workspace(system_design): - if CONFIG.project_path: # Updating on the old version has already been specified if it's valid. According to - # Section 2.2.3.10 of RFC 135 - return - - if isinstance(system_design, ActionOutput): - ws_name = system_design.instruct_content.dict()["project_name"] - else: - ws_name = CodeParser.parse_str(block="project_name", text=system_design) - CONFIG.git_repo.rename_root(ws_name) - async def _update_system_design(self, filename, prds_file_repo, system_design_file_repo) -> Document: prd = await prds_file_repo.get(filename) old_system_design_doc = await system_design_file_repo.get(filename) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index b751dc970..4a2082a07 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -3,7 +3,7 @@ """ @Time : 2023/11/20 @Author : mashenquan -@File : git_repository.py +@File : prepare_documents.py @Desc: PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt. RFC 135 2.2.3.5.1. """ diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 3d59daeed..95da0d65a 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -183,6 +183,10 @@ MERGE_PROMPT = """ ## Old Tasks {old_tasks} ----- + +## Format example +{format_example} +----- Role: You are a project manager; The goal is to merge the new PRD/technical design content from 'Context' into 'Old Tasks.' Based on this merged result, break down tasks, give a task list, and analyze task dependencies to start with the prerequisite modules. Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. @@ -201,7 +205,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Tasks" format, +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Format example" format, and only output the json inside this tag, nothing else """ @@ -264,7 +268,9 @@ class WriteTasks(Action): return rsp async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: - prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content) + _, format_example = get_template(templates, format) + prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content, + format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) task_doc.content = rsp.instruct_content.json(ensure_ascii=False) return task_doc diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index 88a37536b..d10cd6c55 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -3,12 +3,15 @@ """ @Author : alexanderwu @File : summarize_code.py +@Modified By: mashenquan, 2023/12/5. Archive the summarization content of issue discovery for use in WriteCode. """ +from pathlib import Path from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO from metagpt.logs import logger from metagpt.utils.file_repository import FileRepository @@ -95,8 +98,10 @@ class SummarizeCode(Action): return code_rsp async def run(self): - design_doc = await FileRepository.get_file(self.context.design_filename) - task_doc = await FileRepository.get_file(self.context.task_filename) + design_pathname = Path(self.context.design_filename) + design_doc = await FileRepository.get_file(filename=design_pathname.name, relative_path=SYSTEM_DESIGN_FILE_REPO) + task_pathname = Path(self.context.task_filename) + task_doc = await FileRepository.get_file(filename=task_pathname.name, relative_path=TASK_FILE_REPO) src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) code_blocks = [] for filename in self.context.codes_filenames: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 59ccb49a5..9b20843c7 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -15,13 +15,13 @@ RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ - from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action -from metagpt.const import TEST_OUTPUTS_FILE_REPO +from metagpt.config import CONFIG +from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger -from metagpt.schema import CodingContext, RunCodeResult +from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository @@ -50,6 +50,8 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc # Debug logs ```text {logs} + +{summary_log} ``` ----- @@ -90,18 +92,26 @@ class WriteCode(Action): test_doc = await FileRepository.get_file( filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO ) + summary_doc = None + if coding_context.design_doc.filename: + summary_doc = await FileRepository.get_file( + filename=coding_context.design_doc.filename, relative_path=CODE_SUMMARIES_FILE_REPO + ) logs = "" if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr prompt = PROMPT_TEMPLATE.format( design=coding_context.design_doc.content, - tasks=coding_context.task_doc.content, - code=coding_context.code_doc.content, + tasks=coding_context.task_doc.content if coding_context.task_doc else "", + code=coding_context.code_doc.content if coding_context.code_doc else "", logs=logs, filename=self.context.filename, + summary_log=summary_doc.content if summary_doc else "", ) logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) + if not coding_context.code_doc: + coding_context.code_doc = Document(filename=coding_context.filename, root_path=CONFIG.src_workspace) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 364f6af57..f7c6845d2 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -108,10 +108,11 @@ class WriteCodeReview(Action): k = CONFIG.code_review_k_times or 1 for i in range(k): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) + task_content = self.context.task_doc.content if self.context.task_doc else "" context = "\n----------\n".join( [ "```text\n" + self.context.design_doc.content + "```\n", - "```text\n" + self.context.task_doc.content + "```\n", + "```text\n" + task_content + "```\n", "```python\n" + self.context.code_doc.content + "```\n", ] ) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 3967a0578..530a22def 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -8,6 +8,7 @@ 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.2 of RFC 135, add incremental iteration functionality. 3. Move the document storage operations related to WritePRD from the save operation of WriteDesign. +@Modified By: mashenquan, 2023/12/5. Move the generation logic of the project name to WritePRD. """ from __future__ import annotations @@ -27,6 +28,7 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.schema import Document, Documents +from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file @@ -53,7 +55,7 @@ ATTENTION: Output carefully referenced "Format example" in format. {{ "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. "Original Requirements": "", # str, place the polished complete original requirements here - "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. "Search Information": "", "Requirements": "", "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. @@ -85,9 +87,10 @@ and only output the json inside this tag, nothing else """, "FORMAT_EXAMPLE": """ [CONTENT] -{ +{{ "Language": "", "Original Requirements": "", + "Project Name": "{project_name}", "Search Information": "", "Requirements": "", "Product Goals": [], @@ -111,7 +114,7 @@ and only output the json inside this tag, nothing else "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], "UI Design draft": "", "Anything UNCLEAR": "", -} +}} [/CONTENT] """, }, @@ -228,6 +231,7 @@ There are no unclear points. OUTPUT_MAPPING = { "Language": (str, ...), "Original Requirements": (str, ...), + "Project Name": (str, ...), "Product Goals": (List[str], ...), "User Stories": (List[str], ...), "Competitive Analysis": (List[str], ...), @@ -270,7 +274,7 @@ ATTENTION: Output carefully referenced "Old PRD" in format. {{ "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. "Original Requirements": "", # str, place the polished complete original requirements here - "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. "Search Information": "", "Requirements": "", "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. @@ -320,6 +324,7 @@ class WritePRD(Action): if not prd_doc: continue change_files.docs[prd_doc.filename] = prd_doc + logger.info(f"REWRITE PRD:{prd_doc.filename}") # If there is no existing PRD, generate one using 'docs/requirement.txt'. if not change_files.docs: prd_doc = await self._update_prd( @@ -327,6 +332,7 @@ class WritePRD(Action): ) if prd_doc: change_files.docs[prd_doc.filename] = prd_doc + logger.info(f"NEW PRD:{prd_doc.filename}") # Once all files under 'docs/prds/' have been compared with the newly added requirements, trigger the # 'publish' message to transition the workflow to the next stage. This design allows room for global # optimization in subsequent steps. @@ -343,32 +349,36 @@ class WritePRD(Action): # logger.info(format) prompt_template, format_example = get_template(templates, format) + project_name = CONFIG.project_name if CONFIG.project_name else "" + format_example = format_example.format(project_name=project_name) # logger.info(prompt_template) # logger.info(format_example) prompt = prompt_template.format( - requirements=requirements, search_information=info, format_example=format_example + requirements=requirements, search_information=info, format_example=format_example, project_name=project_name ) # logger.info(prompt) # prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) + await self._rename_workspace(prd) return prd async def _is_relative_to(self, new_requirement_doc, old_prd_doc) -> bool: - m = json.loads(old_prd_doc.content) - if m.get("Original Requirements") == new_requirement_doc.content: - # There have been no changes in the requirements, so they are considered unrelated. - return False prompt = IS_RELATIVE_PROMPT.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) res = await self._aask(prompt=prompt) - logger.info(f"[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") + logger.info(f"REQ-RELATIVE:[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") if "YES" in res: return True return False async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: - prompt = MERGE_PROMPT.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) + if not CONFIG.project_name: + CONFIG.project_name = Path(CONFIG.project_path).name + prompt = MERGE_PROMPT.format( + requirements=new_requirement_doc.content, old_prd=prd_doc.content, project_name=CONFIG.project_name + ) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) prd_doc.content = prd.instruct_content.json(ensure_ascii=False) + await self._rename_workspace(prd) return prd_doc async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: @@ -404,3 +414,19 @@ class WritePRD(Action): @staticmethod async def _save_pdf(prd_doc): await FileRepository.save_as(doc=prd_doc, with_suffix=".md", relative_path=PRD_PDF_FILE_REPO) + + @staticmethod + async def _rename_workspace(prd): + if CONFIG.project_path: # Updating on the old version has already been specified if it's valid. According to + # Section 2.2.3.10 of RFC 135 + if not CONFIG.project_name: + CONFIG.project_name = Path(CONFIG.project_path).name + return + + if not CONFIG.project_name: + if isinstance(prd, ActionOutput): + ws_name = prd.instruct_content.dict()["Project Name"] + else: + ws_name = CodeParser.parse_str(block="Project Name", text=prd) + CONFIG.project_name = ws_name + CONFIG.git_repo.rename_root(CONFIG.project_name) diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 7cbb42e1d..65673807f 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -9,8 +9,9 @@ """ from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.const import TEST_CODES_FILE_REPO from metagpt.logs import logger -from metagpt.schema import TestingContext +from metagpt.schema import Document, TestingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -52,6 +53,10 @@ class WriteTest(Action): return code async def run(self, *args, **kwargs) -> TestingContext: + if not self.context.test_doc: + self.context.test_doc = Document( + filename="test_" + self.context.code_doc.filename, root_path=TEST_CODES_FILE_REPO + ) prompt = PROMPT_TEMPLATE.format( code_to_test=self.context.code_doc.content, test_file_name=self.context.test_doc.filename, diff --git a/metagpt/const.py b/metagpt/const.py index a646cea7a..bd735a5e1 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023-11-1. According to Section 2.2.1 and 2.2.2 of RFC 116, added key definitions for common properties in the Message. @Modified By: mashenquan, 2023-11-27. Defines file repository paths according to Section 2.2.3.4 of RFC 135. +@Modified By: mashenquan, 2023/12/5. Add directories for code summarization.. """ import contextvars import os @@ -87,5 +88,7 @@ PRD_PDF_FILE_REPO = "resources/prd" TASK_PDF_FILE_REPO = "resources/api_spec_and_tasks" TEST_CODES_FILE_REPO = "tests" TEST_OUTPUTS_FILE_REPO = "test_outputs" +CODE_SUMMARIES_FILE_REPO = "docs/code_summaries" +CODE_SUMMARIES_PDF_FILE_REPO = "resources/code_summaries" YAPI_URL = "http://yapi.deepwisdomai.com/" diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index 565ae94f7..6c1dc8338 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -38,7 +38,7 @@ class BaseGPTAPI(BaseChatbot): rsp = self.completion(message) return self.get_choice_text(rsp) - async def aask(self, msg: str, system_msgs: Optional[list[str]] = None) -> str: + async def aask(self, msg: str, system_msgs: Optional[list[str]] = None, stream=True) -> str: if system_msgs: message = ( self._system_msgs(system_msgs) + [self._user_msg(msg)] @@ -49,7 +49,7 @@ class BaseGPTAPI(BaseChatbot): message = ( [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt else [self._user_msg(msg)] ) - rsp = await self.acompletion_text(message, stream=True) + rsp = await self.acompletion_text(message, stream=stream) logger.debug(message) # logger.debug(rsp) return rsp diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index d42835a1b..9f8eb6482 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -13,17 +13,25 @@ @Modified By: mashenquan, 2023-11-27. 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.5 of RFC 135, add incremental iteration functionality. +@Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results + of SummarizeCode. """ from __future__ import annotations import json +from collections import defaultdict from pathlib import Path from typing import Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG -from metagpt.const import MESSAGE_ROUTE_TO_NONE, SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO +from metagpt.const import ( + CODE_SUMMARIES_FILE_REPO, + CODE_SUMMARIES_PDF_FILE_REPO, + SYSTEM_DESIGN_FILE_REPO, + TASK_FILE_REPO, +) from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import ( @@ -33,6 +41,16 @@ from metagpt.schema import ( Documents, Message, ) +from metagpt.utils.common import any_to_str, any_to_str_set + +IS_PASS_PROMPT = """ +{context} + +---- +Does the above log indicate anything that needs to be done? +If there are any tasks to be completed, please answer 'NO' along with the to-do list in JSON format; +otherwise, answer 'YES' in JSON format. +""" class Engineer(Role): @@ -60,7 +78,7 @@ class Engineer(Role): """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) self.use_code_review = use_code_review - self._watch([WriteTasks]) + self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview]) self.code_todos = [] self.summarize_todos = [] self.n_borg = n_borg @@ -105,39 +123,88 @@ class Engineer(Role): if self._rc.todo is None: return None if isinstance(self._rc.todo, WriteCode): - changed_files = await self._act_sp_with_cr(review=self.use_code_review) - # Unit tests only. - if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files: - changed_files.add(CONFIG.REQA_FILENAME) - return Message( - content="\n".join(changed_files), - role=self.profile, - cause_by=WriteCodeReview if self.use_code_review else WriteCode, - send_to="Edward", # The name of QaEngineer - ) + return await self._act_write_code() if isinstance(self._rc.todo, SummarizeCode): - summaries = [] - for todo in self.summarize_todos: - summary = await todo.run() - summaries.append(summary.json(ensure_ascii=False)) + return await self._act_summarize() + return None + + async def _act_write_code(self): + changed_files = await self._act_sp_with_cr(review=self.use_code_review) + return Message( + content="\n".join(changed_files), + role=self.profile, + cause_by=WriteCodeReview if self.use_code_review else WriteCode, + send_to=self, + sent_from=self, + ) + + async def _act_summarize(self): + code_summaries_file_repo = CONFIG.git_repo.new_file_repository(CODE_SUMMARIES_FILE_REPO) + code_summaries_pdf_file_repo = CONFIG.git_repo.new_file_repository(CODE_SUMMARIES_PDF_FILE_REPO) + tasks = [] + src_relative_path = CONFIG.src_workspace.relative_to(CONFIG.git_repo.workdir) + for todo in self.summarize_todos: + summary = await todo.run() + summary_filename = Path(todo.context.design_filename).with_suffix(".md").name + dependencies = {todo.context.design_filename, todo.context.task_filename} + for filename in todo.context.codes_filenames: + rpath = src_relative_path / filename + dependencies.add(str(rpath)) + await code_summaries_pdf_file_repo.save( + filename=summary_filename, content=summary, dependencies=dependencies + ) + is_pass, reason = await self._is_pass(summary) + if not is_pass: + todo.context.reason = reason + tasks.append(todo.context.dict()) + await code_summaries_file_repo.save( + filename=Path(todo.context.design_filename).name, + content=todo.context.json(), + dependencies=dependencies, + ) + else: + await code_summaries_file_repo.delete(filename=Path(todo.context.design_filename).name) + + logger.info(f"--max-auto-summarize-code={CONFIG.max_auto_summarize_code}") + if not tasks or CONFIG.max_auto_summarize_code == 0: return Message( - content="\n".join(summaries), + content="", role=self.profile, cause_by=SummarizeCode, - send_to=MESSAGE_ROUTE_TO_NONE, + sent_from=self, + send_to="Edward", # The name of QaEngineer ) - return None + # The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. + # This parameter is used for debugging the workflow. + CONFIG.max_auto_summarize_code -= 1 if CONFIG.max_auto_summarize_code > 0 else 0 + return Message( + content=json.dumps(tasks), role=self.profile, cause_by=SummarizeCode, send_to=self, sent_from=self + ) + + async def _is_pass(self, summary) -> (str, str): + rsp = await self._llm.aask(msg=IS_PASS_PROMPT.format(context=summary), stream=False) + logger.info(rsp) + if "YES" in rsp: + return True, rsp + return False, rsp async def _think(self) -> Action | None: if not CONFIG.src_workspace: CONFIG.src_workspace = CONFIG.git_repo.workdir / CONFIG.git_repo.workdir.name - if not self.code_todos: - await self._new_code_actions() - elif not self.summarize_todos: - await self._new_summarize_actions() - else: + write_code_filters = any_to_str_set([WriteTasks, SummarizeCode]) + summarize_code_filters = any_to_str_set([WriteCode, WriteCodeReview]) + if not self._rc.news: return None - return self._rc.todo # For agent store + msg = self._rc.news[0] + if msg.cause_by in write_code_filters: + logger.info(f"TODO WriteCode:{msg.json()}") + await self._new_code_actions() + return self._rc.todo + if msg.cause_by in summarize_code_filters and msg.sent_from == any_to_str(self): + logger.info(f"TODO SummarizeCode:{msg.json()}") + await self._new_summarize_actions() + return self._rc.todo + return None @staticmethod async def _new_coding_context( @@ -151,9 +218,9 @@ class Engineer(Role): design_doc = None for i in dependencies: if str(i.parent) == TASK_FILE_REPO: - task_doc = task_file_repo.get(i.filename) + task_doc = await task_file_repo.get(i.name) elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO: - design_doc = design_file_repo.get(i.filename) + design_doc = await design_file_repo.get(i.name) context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc) return context @@ -216,16 +283,13 @@ class Engineer(Role): async def _new_summarize_actions(self): src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) - changed_src_files = src_file_repo.changed_files + src_files = src_file_repo.all_files # Generate a SummarizeCode action for each pair of (system_design_doc, task_doc). - summarizations = {} - for filename in changed_src_files: - dependencies = src_file_repo.get_dependency(filename=filename) + summarizations = defaultdict(list) + for filename in src_files: + dependencies = await src_file_repo.get_dependency(filename=filename) ctx = CodeSummarizeContext.loads(filenames=dependencies) - if ctx not in summarizations: - summarizations[ctx] = set() - srcs = summarizations.get(ctx) - srcs.add(filename) + summarizations[ctx].append(filename) for ctx, filenames in summarizations.items(): ctx.codes_filenames = filenames self.summarize_todos.append(SummarizeCode(context=ctx, llm=self._llm)) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 41a3213dc..15a01b9e9 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -11,10 +11,13 @@ WriteTest/RunCode/DebugError object, rather than passing them in when calling the run function. 2. According to Section 2.2.3.5.7 of RFC 135, change the method of transferring files from using the Message to using file references. +@Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results + of SummarizeCode. """ from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest # from metagpt.const import WORKSPACE_ROOT +from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_TO_NONE, @@ -40,13 +43,16 @@ class QaEngineer(Role): self._init_actions( [WriteTest] ) # FIXME: a bit hack here, only init one action to circumvent _think() logic, will overwrite _think() in future updates - self._watch([WriteCode, WriteCodeReview, WriteTest, RunCode, DebugError]) + self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 self.test_round_allowed = test_round_allowed async def _write_test(self, message: Message) -> None: - changed_files = message.content.splitlines() src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + changed_files = set(src_file_repo.changed_files.keys()) + # Unit tests only. + if CONFIG.reqa_file and CONFIG.reqa_file not in changed_files: + changed_files.add(CONFIG.reqa_file) tests_file_repo = CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO) for filename in changed_files: # write tests @@ -146,7 +152,7 @@ class QaEngineer(Role): ) return result_msg - code_filters = any_to_str_set({WriteCode, WriteCodeReview}) + code_filters = any_to_str_set({SummarizeCode}) test_filters = any_to_str_set({WriteTest, DebugError}) run_filters = any_to_str_set({RunCode}) for msg in self._rc.news: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1e99cc1ff..2651be7eb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -284,9 +284,10 @@ class Role: instruct_content=response.instruct_content, role=self.profile, cause_by=self._rc.todo, + sent_from=self, ) else: - msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) + msg = Message(content=response, role=self.profile, cause_by=self._rc.todo, sent_from=self) self._rc.memory.add(msg) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index d1174799a..a8c1b7726 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -48,9 +48,9 @@ class Document(BaseModel): Represents a document. """ - root_path: str - filename: str - content: Optional[str] = None + root_path: str = "" + filename: str = "" + content: str = "" def get_meta(self) -> Document: """Get metadata of the document. @@ -260,8 +260,8 @@ class MessageQueue: class CodingContext(BaseModel): filename: str design_doc: Document - task_doc: Document - code_doc: Document + task_doc: Optional[Document] + code_doc: Optional[Document] @staticmethod def loads(val: str) -> CodingContext | None: @@ -275,7 +275,7 @@ class CodingContext(BaseModel): class TestingContext(BaseModel): filename: str code_doc: Document - test_doc: Document + test_doc: Optional[Document] @staticmethod def loads(val: str) -> TestingContext | None: @@ -324,10 +324,11 @@ class RunCodeResult(BaseModel): class CodeSummarizeContext(BaseModel): design_filename: str = "" task_filename: str = "" - codes_filenames: Set[str] = Field(default_factory=set) + codes_filenames: List[str] = Field(default_factory=list) + reason: str = "" @staticmethod - def loads(filenames: Set) -> CodeSummarizeContext: + def loads(filenames: List) -> CodeSummarizeContext: ctx = CodeSummarizeContext() for filename in filenames: if Path(filename).is_relative_to(SYSTEM_DESIGN_FILE_REPO): @@ -337,3 +338,6 @@ class CodeSummarizeContext(BaseModel): ctx.task_filename = str(filename) continue return ctx + + def __hash__(self): + return hash((self.design_filename, self.task_filename)) diff --git a/metagpt/startup.py b/metagpt/startup.py index 78f32d556..f930c386b 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio +from pathlib import Path import typer @@ -24,6 +25,10 @@ def startup( help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", ), reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), + max_auto_summarize_code: int = typer.Option( + default=-1, + help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. This parameter is used for debugging the workflow.", + ), ): """Run a startup. Be a boss.""" from metagpt.roles import ( @@ -36,10 +41,14 @@ def startup( from metagpt.team import Team # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. + CONFIG.project_path = project_path + if project_path: + inc = True + project_name = project_name or Path(project_path).name CONFIG.project_name = project_name CONFIG.inc = inc - CONFIG.project_path = project_path CONFIG.reqa_file = reqa_file + CONFIG.max_auto_summarize_code = max_auto_summarize_code company = Team() company.hire( diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index 653e07ef9..e8347d567 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -14,6 +14,7 @@ from typing import Set import aiofiles +from metagpt.config import CONFIG from metagpt.logs import logger @@ -81,7 +82,7 @@ class DependencyFile: if persist: await self.save() - async def get(self, filename: Path | str, persist=False): + async def get(self, filename: Path | str, persist=True): """Get dependencies for a file asynchronously. :param filename: The filename or path. @@ -91,7 +92,7 @@ class DependencyFile: if persist: await self.load() - root = self._filename.parent + root = CONFIG.git_repo.workdir try: key = Path(filename).relative_to(root) except ValueError: diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 0815bf90a..2cace7232 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -151,6 +151,17 @@ class FileRepository: relative_files[str(rf)] = ct return relative_files + @property + def all_files(self) -> List: + """Get a dictionary of all files in the repository. + + The dictionary includes file paths relative to the current FileRepository. + + :return: A dictionary where keys are file paths and values are file information. + :rtype: List + """ + return self._git_repo.get_files(relative_path=self._relative_path) + def get_change_dir_files(self, dir: Path | str) -> List: """Get the files in a directory that have changed. @@ -259,3 +270,25 @@ class FileRepository: """ file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) return await file_repo.save_doc(doc=doc, with_suffix=with_suffix, dependencies=dependencies) + + async def delete(self, filename: Path | str): + """Delete a file from the file repository. + + This method deletes a file from the file repository based on the provided filename. + + :param filename: The name or path of the file to be deleted. + :type filename: Path or str + """ + pathname = self.workdir / filename + if not pathname.exists(): + return + pathname.unlink(missing_ok=True) + + dependency_file = await self._git_repo.get_dependency() + await dependency_file.update(filename=pathname, dependencies=None) + logger.info(f"remove dependency key: {str(pathname)}") + + @staticmethod + async def delete_file(filename: Path | str, relative_path: Path | str = "."): + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + await file_repo.delete(filename=filename) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 7c9ec645f..9a9ed0fce 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -8,10 +8,11 @@ """ from __future__ import annotations +import os import shutil from enum import Enum from pathlib import Path -from typing import Dict +from typing import Dict, List from git.repo import Repo from git.repo.fun import is_git_dir @@ -196,10 +197,43 @@ class GitRepository: if new_path.exists(): logger.info(f"Delete directory {str(new_path)}") shutil.rmtree(new_path) - self.workdir.rename(new_path) + os.rename(src=str(self.workdir), dst=str(new_path)) # self.workdir.rename(new_path) logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) + def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None) -> List: + """Retrieve a list of files in the specified relative path. + + The method returns a list of file paths relative to the current FileRepository. + + :param relative_path: The relative path within the repository. + :type relative_path: Path or str + :param root_relative_path: The root relative path within the repository. + :type root_relative_path: Path or str + :return: A list of file paths in the specified directory. + :rtype: List[str] + """ + try: + relative_path = Path(relative_path).relative_to(self.workdir) + except ValueError: + relative_path = Path(relative_path) + + if not root_relative_path: + root_relative_path = Path(self.workdir) / relative_path + files = [] + try: + directory_path = Path(self.workdir) / relative_path + for file_path in directory_path.iterdir(): + if file_path.is_file(): + rpath = file_path.relative_to(root_relative_path) + files.append(str(rpath)) + else: + subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path) + files.extend(subfolder_files) + except Exception as e: + logger.error(f"Error: {e}") + return files + if __name__ == "__main__": path = DEFAULT_WORKSPACE_ROOT / "git" diff --git a/tests/conftest.py b/tests/conftest.py index d2ac8304f..8e4422700 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,8 +12,11 @@ from unittest.mock import Mock import pytest +from metagpt.config import CONFIG +from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI +from metagpt.utils.git_repository import GitRepository class Context: @@ -68,3 +71,16 @@ def proxy(): server = asyncio.get_event_loop().run_until_complete(asyncio.start_server(handle_client, "127.0.0.1", 0)) return "http://{}:{}".format(*server.sockets[0].getsockname()) + + +# init & dispose git repo +@pytest.fixture(scope="session", autouse=True) +def setup_and_teardown_git_repo(request): + CONFIG.git_repo = GitRepository(local_path=DEFAULT_WORKSPACE_ROOT / "unittest") + + # Destroy git repo at the end of the test session. + def fin(): + CONFIG.git_repo.delete_repository() + + # Register the function for destroying the environment. + request.addfinalizer(fin) diff --git a/tests/metagpt/actions/mock.py b/tests/metagpt/actions/mock.py index c48913755..f6602a82b 100644 --- a/tests/metagpt/actions/mock.py +++ b/tests/metagpt/actions/mock.py @@ -90,7 +90,7 @@ Python's in-built data structures like lists and dictionaries will be used exten For testing, we can use the PyTest framework. This is a mature full-featured Python testing tool that helps you write better programs. -## project_name: +## Project Name: ```python "adventure_game" ``` diff --git a/tests/metagpt/actions/test_debug_error.py b/tests/metagpt/actions/test_debug_error.py index 2393d2cc9..8289fe41b 100644 --- a/tests/metagpt/actions/test_debug_error.py +++ b/tests/metagpt/actions/test_debug_error.py @@ -4,17 +4,19 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : test_debug_error.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ +import uuid + import pytest from metagpt.actions.debug_error import DebugError +from metagpt.config import CONFIG +from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO +from metagpt.schema import RunCodeContext, RunCodeResult +from metagpt.utils.file_repository import FileRepository -EXAMPLE_MSG_CONTENT = ''' ---- -## Development Code File Name -player.py -## Development Code -```python +CODE_CONTENT = ''' from typing import List from deck import Deck from card import Card @@ -58,12 +60,9 @@ class Player: if self.score > 21 and any(card.rank == 'A' for card in self.hand): self.score -= 10 return self.score +''' -``` -## Test File Name -test_player.py -## Test Code -```python +TEST_CONTENT = """ import unittest from blackjack_game.player import Player from blackjack_game.deck import Deck @@ -114,42 +113,41 @@ class TestPlayer(unittest.TestCase): if __name__ == '__main__': unittest.main() -``` -## Running Command -python tests/test_player.py -## Running Output -standard output: ; -standard errors: ..F.. -====================================================================== -FAIL: test_player_calculate_score_with_multiple_aces (__main__.TestPlayer) ----------------------------------------------------------------------- -Traceback (most recent call last): - File "tests/test_player.py", line 46, in test_player_calculate_score_with_multiple_aces - self.assertEqual(player.score, 12) -AssertionError: 22 != 12 - ----------------------------------------------------------------------- -Ran 5 tests in 0.007s - -FAILED (failures=1) -; -## instruction: -The error is in the development code, specifically in the calculate_score method of the Player class. The method is not correctly handling the case where there are multiple Aces in the player's hand. The current implementation only subtracts 10 from the score once if the score is over 21 and there's an Ace in the hand. However, in the case of multiple Aces, it should subtract 10 for each Ace until the score is 21 or less. -## File To Rewrite: -player.py -## Status: -FAIL -## Send To: -Engineer ---- -''' +""" @pytest.mark.asyncio async def test_debug_error(): - debug_error = DebugError("debug_error") + CONFIG.src_workspace = CONFIG.git_repo.workdir / uuid.uuid4().hex + ctx = RunCodeContext( + code_filename="player.py", + test_filename="test_player.py", + command=["python", "tests/test_player.py"], + output_filename="output.log", + ) - file_name, rewritten_code = await debug_error.run(context=EXAMPLE_MSG_CONTENT) + await FileRepository.save_file(filename=ctx.code_filename, content=CODE_CONTENT, relative_path=CONFIG.src_workspace) + await FileRepository.save_file(filename=ctx.test_filename, content=TEST_CONTENT, relative_path=TEST_CODES_FILE_REPO) + output_data = RunCodeResult( + stdout=";", + stderr="", + summary="======================================================================\n" + "FAIL: test_player_calculate_score_with_multiple_aces (__main__.TestPlayer)\n" + "----------------------------------------------------------------------\n" + "Traceback (most recent call last):\n" + ' File "tests/test_player.py", line 46, in test_player_calculate_score_' + "with_multiple_aces\n" + " self.assertEqual(player.score, 12)\nAssertionError: 22 != 12\n\n" + "----------------------------------------------------------------------\n" + "Ran 5 tests in 0.007s\n\nFAILED (failures=1)\n;\n", + ) + await FileRepository.save_file( + filename=ctx.output_filename, content=output_data.json(), relative_path=TEST_OUTPUTS_FILE_REPO + ) + debug_error = DebugError(context=ctx) - assert "class Player" in rewritten_code # rewrite the same class - assert "while self.score > 21" in rewritten_code # a key logic to rewrite to (original one is "if self.score > 12") + rsp = await debug_error.run() + + assert "class Player" in rsp # rewrite the same class + # a key logic to rewrite to (original one is "if self.score > 12") + assert "while self.score > 21" in rsp diff --git a/tests/metagpt/actions/test_design_api.py b/tests/metagpt/actions/test_design_api.py index 0add8fb74..e90707d1a 100644 --- a/tests/metagpt/actions/test_design_api.py +++ b/tests/metagpt/actions/test_design_api.py @@ -4,33 +4,27 @@ @Time : 2023/5/11 19:26 @Author : alexanderwu @File : test_design_api.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.design_api import WriteDesign +from metagpt.const import PRDS_FILE_REPO from metagpt.logs import logger from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository from tests.metagpt.actions.mock import PRD_SAMPLE @pytest.mark.asyncio async def test_design_api(): - prd = "我们需要一个音乐播放器,它应该有播放、暂停、上一曲、下一曲等功能。" + inputs = ["我们需要一个音乐播放器,它应该有播放、暂停、上一曲、下一曲等功能。", PRD_SAMPLE] + for prd in inputs: + await FileRepository.save_file("new_prd.txt", content=prd, relative_path=PRDS_FILE_REPO) - design_api = WriteDesign("design_api") + design_api = WriteDesign("design_api") - result = await design_api.run([Message(content=prd, instruct_content=None)]) - logger.info(result) + result = await design_api.run([Message(content=prd, instruct_content=None)]) + logger.info(result) - assert result - - -@pytest.mark.asyncio -async def test_design_api_calculator(): - prd = PRD_SAMPLE - - design_api = WriteDesign("design_api") - result = await design_api.run([Message(content=prd, instruct_content=None)]) - logger.info(result) - - assert result + assert result diff --git a/tests/metagpt/actions/test_prepare_documents.py b/tests/metagpt/actions/test_prepare_documents.py new file mode 100644 index 000000000..31c8bcb80 --- /dev/null +++ b/tests/metagpt/actions/test_prepare_documents.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/6 +@Author : mashenquan +@File : test_prepare_documents.py +@Desc: Unit test for prepare_documents.py +""" +import pytest + +from metagpt.actions.prepare_documents import PrepareDocuments +from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository + + +@pytest.mark.asyncio +async def test_prepare_documents(): + msg = Message(content="New user requirements balabala...") + + if CONFIG.git_repo: + CONFIG.git_repo.delete_repository() + CONFIG.git_repo = None + + await PrepareDocuments().run(with_messages=[msg]) + assert CONFIG.git_repo + doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) + assert doc + assert doc.content == msg.content diff --git a/tests/metagpt/actions/test_run_code.py b/tests/metagpt/actions/test_run_code.py index 1e451cb14..888418974 100644 --- a/tests/metagpt/actions/test_run_code.py +++ b/tests/metagpt/actions/test_run_code.py @@ -4,10 +4,12 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : test_run_code.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.run_code import RunCode +from metagpt.schema import RunCodeContext @pytest.mark.asyncio @@ -35,37 +37,29 @@ async def test_run_script(): @pytest.mark.asyncio async def test_run(): - action = RunCode() - result = await action.run(mode="text", code="print('Hello, World')") - assert "PASS" in result - - result = await action.run( - mode="script", - code="echo 'Hello World'", - code_file_name="", - test_code="", - test_file_name="", - command=["echo", "Hello World"], - working_directory=".", - additional_python_paths=[], - ) - assert "PASS" in result - - -@pytest.mark.asyncio -async def test_run_failure(): - action = RunCode() - result = await action.run(mode="text", code="result = 1 / 0") - assert "FAIL" in result - - result = await action.run( - mode="script", - code='python -c "print(1/0)"', - code_file_name="", - test_code="", - test_file_name="", - command=["python", "-c", "print(1/0)"], - working_directory=".", - additional_python_paths=[], - ) - assert "FAIL" in result + inputs = [ + (RunCodeContext(mode="text", code_filename="a.txt", code="print('Hello, World')"), "PASS"), + ( + RunCodeContext( + mode="script", + code_filename="a.sh", + code="echo 'Hello World'", + command=["echo", "Hello World"], + working_directory=".", + ), + "PASS", + ), + ( + RunCodeContext( + mode="script", + code_filename="a.py", + code='python -c "print(1/0)"', + command=["python", "-c", "print(1/0)"], + working_directory=".", + ), + "FAIL", + ), + ] + for ctx, result in inputs: + rsp = await RunCode(context=ctx).run() + assert result in rsp.summary diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py new file mode 100644 index 000000000..7ecb67afd --- /dev/null +++ b/tests/metagpt/actions/test_summarize_code.py @@ -0,0 +1,195 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/5/11 17:46 +@Author : mashenquan +@File : test_summarize_code.py +@Modifiled By: mashenquan, 2023-12-6. Unit test for summarize_code.py +""" +import pytest + +from metagpt.actions.summarize_code import SummarizeCode +from metagpt.config import CONFIG +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO +from metagpt.logs import logger +from metagpt.schema import CodeSummarizeContext +from metagpt.utils.file_repository import FileRepository + +DESIGN_CONTENT = """ +{"Implementation approach": "To develop this snake game, we will use the Python language and choose the Pygame library. Pygame is an open-source Python module collection specifically designed for writing video games. It provides functionalities such as displaying images and playing sounds, making it suitable for creating intuitive and responsive user interfaces. We will ensure efficient game logic to prevent any delays during gameplay. The scoring system will be simple, with the snake gaining points for each food it eats. We will use Pygame's event handling system to implement pause and resume functionality, as well as high-score tracking. The difficulty will increase by speeding up the snake's movement. In the initial version, we will focus on single-player mode and consider adding multiplayer mode and customizable skins in future updates. Based on the new requirement, we will also add a moving obstacle that appears randomly. If the snake eats this obstacle, the game will end. If the snake does not eat the obstacle, it will disappear after 5 seconds. For this, we need to add mechanisms for obstacle generation, movement, and disappearance in the game logic.", "Project_name": "snake_game", "File list": ["main.py", "game.py", "snake.py", "food.py", "obstacle.py", "scoreboard.py", "constants.py", "assets/styles.css", "assets/index.html"], "Data structures and interfaces": "```mermaid\n classDiagram\n class Game{\n +int score\n +int speed\n +bool game_over\n +bool paused\n +Snake snake\n +Food food\n +Obstacle obstacle\n +Scoreboard scoreboard\n +start_game() void\n +pause_game() void\n +resume_game() void\n +end_game() void\n +increase_difficulty() void\n +update() void\n +render() void\n Game()\n }\n class Snake{\n +list body_parts\n +str direction\n +bool grow\n +move() void\n +grow() void\n +check_collision() bool\n Snake()\n }\n class Food{\n +tuple position\n +spawn() void\n Food()\n }\n class Obstacle{\n +tuple position\n +int lifetime\n +bool active\n +spawn() void\n +move() void\n +check_collision() bool\n +disappear() void\n Obstacle()\n }\n class Scoreboard{\n +int high_score\n +update_score(int) void\n +reset_score() void\n +load_high_score() void\n +save_high_score() void\n Scoreboard()\n }\n class Constants{\n }\n Game \"1\" -- \"1\" Snake: has\n Game \"1\" -- \"1\" Food: has\n Game \"1\" -- \"1\" Obstacle: has\n Game \"1\" -- \"1\" Scoreboard: has\n ```", "Program call flow": "```sequenceDiagram\n participant M as Main\n participant G as Game\n participant S as Snake\n participant F as Food\n participant O as Obstacle\n participant SB as Scoreboard\n M->>G: start_game()\n loop game loop\n G->>S: move()\n G->>S: check_collision()\n G->>F: spawn()\n G->>O: spawn()\n G->>O: move()\n G->>O: check_collision()\n G->>O: disappear()\n G->>SB: update_score(score)\n G->>G: update()\n G->>G: render()\n alt if paused\n M->>G: pause_game()\n M->>G: resume_game()\n end\n alt if game_over\n G->>M: end_game()\n end\n end\n```", "Anything UNCLEAR": "There is no need for further clarification as the requirements are already clear."} +""" + +TASK_CONTENT = """ +{"Required Python third-party packages": ["pygame==2.0.1"], "Required Other language third-party packages": ["No third-party packages required for other languages."], "Full API spec": "\n openapi: 3.0.0\n info:\n title: Snake Game API\n version: \"1.0.0\"\n paths:\n /start:\n get:\n summary: Start the game\n responses:\n '200':\n description: Game started successfully\n /pause:\n get:\n summary: Pause the game\n responses:\n '200':\n description: Game paused successfully\n /resume:\n get:\n summary: Resume the game\n responses:\n '200':\n description: Game resumed successfully\n /end:\n get:\n summary: End the game\n responses:\n '200':\n description: Game ended successfully\n /score:\n get:\n summary: Get the current score\n responses:\n '200':\n description: Current score retrieved successfully\n /highscore:\n get:\n summary: Get the high score\n responses:\n '200':\n description: High score retrieved successfully\n components: {}\n ", "Logic Analysis": [["constants.py", "Contains all the constant values like screen size, colors, game speeds, etc. This should be implemented first as it provides the base values for other components."], ["snake.py", "Contains the Snake class with methods for movement, growth, and collision detection. It is dependent on constants.py for configuration values."], ["food.py", "Contains the Food class responsible for spawning food items on the screen. It is dependent on constants.py for configuration values."], ["obstacle.py", "Contains the Obstacle class with methods for spawning, moving, and disappearing of obstacles, as well as collision detection with the snake. It is dependent on constants.py for configuration values."], ["scoreboard.py", "Contains the Scoreboard class for updating, resetting, loading, and saving high scores. It may use constants.py for configuration values and depends on the game's scoring logic."], ["game.py", "Contains the main Game class which includes the game loop and methods for starting, pausing, resuming, and ending the game. It is dependent on snake.py, food.py, obstacle.py, and scoreboard.py."], ["main.py", "The entry point of the game that initializes the game and starts the game loop. It is dependent on game.py."]], "Task list": ["constants.py", "snake.py", "food.py", "obstacle.py", "scoreboard.py", "game.py", "main.py"], "Shared Knowledge": "\n 'constants.py' should contain all the necessary configurations for the game, such as screen dimensions, color definitions, and speed settings. These constants will be used across multiple files, ensuring consistency and ease of updates. Ensure that the Pygame library is initialized correctly in 'main.py' before starting the game loop. Also, make sure that the game's state is managed properly when pausing and resuming the game.\n ", "Anything UNCLEAR": "The interaction between the 'obstacle.py' and the game loop needs to be clearly defined to ensure obstacles appear and disappear correctly. The lifetime of the obstacle and its random movement should be implemented in a way that does not interfere with the game's performance."} +""" + +FOOD_PY = """ +## food.py +import random + +class Food: + def __init__(self): + self.position = (0, 0) + + def generate(self): + x = random.randint(0, 9) + y = random.randint(0, 9) + self.position = (x, y) + + def get_position(self): + return self.position + +""" + +GAME_PY = """ +## game.py +import pygame +from snake import Snake +from food import Food + +class Game: + def __init__(self): + self.score = 0 + self.level = 1 + self.snake = Snake() + self.food = Food() + + def start_game(self): + pygame.init() + self.initialize_game() + self.game_loop() + + def initialize_game(self): + self.score = 0 + self.level = 1 + self.snake.reset() + self.food.generate() + + def game_loop(self): + game_over = False + + while not game_over: + self.update() + self.draw() + self.handle_events() + self.check_collision() + self.increase_score() + self.increase_level() + + if self.snake.is_collision(): + game_over = True + self.game_over() + + def update(self): + self.snake.move() + + def draw(self): + self.snake.draw() + self.food.draw() + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.snake.change_direction("UP") + elif event.key == pygame.K_DOWN: + self.snake.change_direction("DOWN") + elif event.key == pygame.K_LEFT: + self.snake.change_direction("LEFT") + elif event.key == pygame.K_RIGHT: + self.snake.change_direction("RIGHT") + + def check_collision(self): + if self.snake.get_head() == self.food.get_position(): + self.snake.grow() + self.food.generate() + + def increase_score(self): + self.score += 1 + + def increase_level(self): + if self.score % 10 == 0: + self.level += 1 + + def game_over(self): + print("Game Over") + self.initialize_game() + +""" + +MAIN_PY = """ +## main.py +import pygame +from game import Game + +def main(): + pygame.init() + game = Game() + game.start_game() + +if __name__ == "__main__": + main() + +""" + +SNAKE_PY = """ +## snake.py +import pygame + +class Snake: + def __init__(self): + self.body = [(0, 0)] + self.direction = (1, 0) + + def move(self): + head = self.body[0] + dx, dy = self.direction + new_head = (head[0] + dx, head[1] + dy) + self.body.insert(0, new_head) + self.body.pop() + + def change_direction(self, direction): + if direction == "UP": + self.direction = (0, -1) + elif direction == "DOWN": + self.direction = (0, 1) + elif direction == "LEFT": + self.direction = (-1, 0) + elif direction == "RIGHT": + self.direction = (1, 0) + + def grow(self): + tail = self.body[-1] + dx, dy = self.direction + new_tail = (tail[0] - dx, tail[1] - dy) + self.body.append(new_tail) + + def get_head(self): + return self.body[0] + + def get_body(self): + return self.body[1:] + +""" + + +@pytest.mark.asyncio +async def test_summarize_code(): + CONFIG.src_workspace = CONFIG.git_repo.workdir / "src" + await FileRepository.save_file(filename="1.json", relative_path=SYSTEM_DESIGN_FILE_REPO, content=DESIGN_CONTENT) + await FileRepository.save_file(filename="1.json", relative_path=TASK_FILE_REPO, content=TASK_CONTENT) + await FileRepository.save_file(filename="food.py", relative_path=CONFIG.src_workspace, content=FOOD_PY) + await FileRepository.save_file(filename="game.py", relative_path=CONFIG.src_workspace, content=GAME_PY) + await FileRepository.save_file(filename="main.py", relative_path=CONFIG.src_workspace, content=MAIN_PY) + await FileRepository.save_file(filename="snake.py", relative_path=CONFIG.src_workspace, content=SNAKE_PY) + + src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) + all_files = src_file_repo.all_files + ctx = CodeSummarizeContext(design_filename="1.json", task_filename="1.json", codes_filenames=all_files) + action = SummarizeCode(context=ctx) + rsp = await action.run() + assert rsp + logger.info(rsp) diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index eb5e3de91..54229089c 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -4,26 +4,31 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : test_write_code.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.write_code import WriteCode from metagpt.llm import LLM from metagpt.logs import logger +from metagpt.schema import CodingContext, Document from tests.metagpt.actions.mock import TASKS_2, WRITE_CODE_PROMPT_SAMPLE @pytest.mark.asyncio async def test_write_code(): - api_design = "设计一个名为'add'的函数,该函数接受两个整数作为输入,并返回它们的和。" - write_code = WriteCode("write_code") + context = CodingContext( + filename="task_filename.py", design_doc=Document(content="设计一个名为'add'的函数,该函数接受两个整数作为输入,并返回它们的和。") + ) + doc = Document(content=context.json()) + write_code = WriteCode(context=doc) - code = await write_code.run(api_design) - logger.info(code) + code = await write_code.run() + logger.info(code.json()) # 我们不能精确地预测生成的代码,但我们可以检查某些关键字 - assert "def add" in code - assert "return" in code + assert "def add" in code.code_doc.content + assert "return" in code.code_doc.content @pytest.mark.asyncio diff --git a/tests/metagpt/actions/test_write_code_review.py b/tests/metagpt/actions/test_write_code_review.py index 21bc563ec..e16eb7348 100644 --- a/tests/metagpt/actions/test_write_code_review.py +++ b/tests/metagpt/actions/test_write_code_review.py @@ -8,6 +8,8 @@ import pytest from metagpt.actions.write_code_review import WriteCodeReview +from metagpt.document import Document +from metagpt.schema import CodingContext @pytest.mark.asyncio @@ -16,13 +18,15 @@ async def test_write_code_review(capfd): def add(a, b): return a + """ - # write_code_review = WriteCodeReview("write_code_review") + context = CodingContext( + filename="math.py", design_doc=Document(content="编写一个从a加b的函数,返回a+b"), code_doc=Document(content=code) + ) - code = await WriteCodeReview().run(context="编写一个从a加b的函数,返回a+b", code=code, filename="math.py") + context = await WriteCodeReview(context=context).run() # 我们不能精确地预测生成的代码评审,但我们可以检查返回的是否为字符串 - assert isinstance(code, str) - assert len(code) > 0 + assert isinstance(context.code_doc.content, str) + assert len(context.code_doc.content) > 0 captured = capfd.readouterr() print(f"输出内容: {captured.out}") diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 8f8ef84f5..08be3cf75 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -9,19 +9,24 @@ import pytest from metagpt.actions import UserRequirement +from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, PRDS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.logs import logger from metagpt.roles.product_manager import ProductManager from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository @pytest.mark.asyncio async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" + await FileRepository.save_file(filename=REQUIREMENT_FILENAME, content=requirements, relative_path=DOCS_FILE_REPO) prd = await product_manager.run(Message(content=requirements, cause_by=UserRequirement)) logger.info(requirements) logger.info(prd) # Assert the prd is not None or empty assert prd is not None - assert prd != "" + assert prd.content != "" + assert CONFIG.git_repo.new_file_repository(relative_path=PRDS_FILE_REPO).changed_files diff --git a/tests/metagpt/actions/test_write_test.py b/tests/metagpt/actions/test_write_test.py index e5acdff44..a3190fb0e 100644 --- a/tests/metagpt/actions/test_write_test.py +++ b/tests/metagpt/actions/test_write_test.py @@ -9,6 +9,7 @@ import pytest from metagpt.actions.write_test import WriteTest from metagpt.logs import logger +from metagpt.schema import Document, TestingContext @pytest.mark.asyncio @@ -24,22 +25,17 @@ async def test_write_test(): def generate(self, max_y: int, max_x: int): self.position = (random.randint(1, max_y - 1), random.randint(1, max_x - 1)) """ + context = TestingContext(filename="food.py", code_doc=Document(filename="food.py", content=code)) + write_test = WriteTest(context=context) - write_test = WriteTest() - - test_code = await write_test.run( - code_to_test=code, - test_file_name="test_food.py", - source_file_path="/some/dummy/path/cli_snake_game/cli_snake_game/food.py", - workspace="/some/dummy/path/cli_snake_game", - ) - logger.info(test_code) + context = await write_test.run() + logger.info(context.json()) # We cannot exactly predict the generated test cases, but we can check if it is a string and if it is not empty - assert isinstance(test_code, str) - assert "from cli_snake_game.food import Food" in test_code - assert "class TestFood(unittest.TestCase)" in test_code - assert "def test_generate" in test_code + assert isinstance(context.test_doc.content, str) + assert "from food import Food" in context.test_doc.content + assert "class TestFood(unittest.TestCase)" in context.test_doc.content + assert "def test_generate" in context.test_doc.content @pytest.mark.asyncio diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 5500b69f7..75f6b3b43 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -71,7 +71,7 @@ PRD = '''## 原始需求 ``` ''' -SYSTEM_DESIGN = """## project_name +SYSTEM_DESIGN = """## Project name ```python "smart_search_engine" ``` diff --git a/tests/metagpt/utils/test_file_repository.py b/tests/metagpt/utils/test_file_repository.py index a830b58aa..92e5204c5 100644 --- a/tests/metagpt/utils/test_file_repository.py +++ b/tests/metagpt/utils/test_file_repository.py @@ -43,6 +43,10 @@ async def test_file_repo(): assert {"a.txt"} == await file_repo.get_changed_dependency("b.txt") await file_repo.save("d/e.txt", "EEE") assert ["d/e.txt"] == file_repo.get_change_dir_files("d") + assert set(file_repo.all_files) == {"a.txt", "b.txt", "d/e.txt"} + await file_repo.delete("d/e.txt") + await file_repo.delete("d/e.txt") # delete twice + assert set(file_repo.all_files) == {"a.txt", "b.txt"} git_repo.delete_repository() From 2e0a847f63fc3c045dd296b8f84debee9cf6900f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 7 Dec 2023 15:08:38 +0800 Subject: [PATCH 236/413] fixbug --- metagpt/utils/git_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 9a9ed0fce..8b53ce7d2 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -197,7 +197,7 @@ class GitRepository: if new_path.exists(): logger.info(f"Delete directory {str(new_path)}") shutil.rmtree(new_path) - os.rename(src=str(self.workdir), dst=str(new_path)) # self.workdir.rename(new_path) + shutil.move(src=str(self.workdir), dst=str(new_path)) # self.workdir.rename(new_path) logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) From ef633b7c26f809daaccf27a850df52cb1e349a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 7 Dec 2023 17:09:21 +0800 Subject: [PATCH 237/413] fixbug: move dir --- metagpt/utils/git_repository.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 8b53ce7d2..5aec4509c 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -197,7 +197,10 @@ class GitRepository: if new_path.exists(): logger.info(f"Delete directory {str(new_path)}") shutil.rmtree(new_path) - shutil.move(src=str(self.workdir), dst=str(new_path)) # self.workdir.rename(new_path) + try: + shutil.move(src=str(self.workdir), dst=str(new_path)) + except Exception as e: + logger.warning(f"Move {str(self.workdir)} to {str(new_path)} error: {e}") logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) From 9d84c8f047b60455bbf9c679af3cfe4cf1b11b27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 4 Dec 2023 23:04:07 +0800 Subject: [PATCH 238/413] feat: +SummarizeCode, refactor project_name --- metagpt/actions/design_api.py | 65 ++---- metagpt/actions/prepare_documents.py | 7 +- metagpt/actions/project_management.py | 10 +- metagpt/actions/summarize_code.py | 9 +- metagpt/actions/write_code.py | 20 +- metagpt/actions/write_code_review.py | 3 +- metagpt/actions/write_prd.py | 48 ++++- metagpt/actions/write_test.py | 7 +- metagpt/const.py | 3 + metagpt/provider/base_gpt_api.py | 4 +- metagpt/roles/engineer.py | 134 ++++++++---- metagpt/roles/qa_engineer.py | 12 +- metagpt/roles/role.py | 3 +- metagpt/schema.py | 20 +- metagpt/startup.py | 11 +- metagpt/utils/dependency_file.py | 5 +- metagpt/utils/file_repository.py | 33 +++ metagpt/utils/git_repository.py | 41 +++- tests/conftest.py | 16 ++ tests/metagpt/actions/mock.py | 2 +- tests/metagpt/actions/test_debug_error.py | 86 ++++---- tests/metagpt/actions/test_design_api.py | 26 +-- .../metagpt/actions/test_prepare_documents.py | 30 +++ tests/metagpt/actions/test_run_code.py | 62 +++--- tests/metagpt/actions/test_summarize_code.py | 195 ++++++++++++++++++ tests/metagpt/actions/test_write_code.py | 17 +- .../metagpt/actions/test_write_code_review.py | 12 +- tests/metagpt/actions/test_write_prd.py | 7 +- tests/metagpt/actions/test_write_test.py | 22 +- tests/metagpt/roles/mock.py | 2 +- tests/metagpt/utils/test_file_repository.py | 4 + 31 files changed, 671 insertions(+), 245 deletions(-) create mode 100644 tests/metagpt/actions/test_prepare_documents.py create mode 100644 tests/metagpt/actions/test_summarize_code.py diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index c5787ba20..557ebcbbd 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023/11/27. 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.3 of RFC 135, add incremental iteration functionality. +@Modified By: mashenquan, 2023/12/5. Move the generation logic of the project name to WritePRD. """ import json from pathlib import Path @@ -23,7 +24,6 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.schema import Document, Documents -from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file @@ -43,7 +43,7 @@ Requirement: Fill in the following missing information based on the context, eac ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select appropriate open-source frameworks. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text. ## File list: Provided as Python list[str], the list of files needed (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -58,15 +58,15 @@ and only output the json inside this tag, nothing else """, "FORMAT_EXAMPLE": """ [CONTENT] -{ +{{ "Implementation approach": "We will ...", - "project_name": "snake_game", + "Project name": "{project_name}", "File list": ["main.py"], "Data structures and interfaces": ' classDiagram - class Game{ + class Game{{ +int score - } + }} ... Game "1" -- "1" Food: has ', @@ -77,7 +77,7 @@ and only output the json inside this tag, nothing else G->>M: end game ', "Anything UNCLEAR": "The requirement is clear to me." -} +}} [/CONTENT] """, }, @@ -96,7 +96,7 @@ ATTENTION: Output carefully referenced "Format example" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text. ## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -112,9 +112,9 @@ ATTENTION: Output carefully referenced "Format example" in format. ## Implementation approach We will ... -## project_name +## Project name ```python -"snake_game" +"{project_name}" ``` ## File list @@ -151,7 +151,7 @@ The requirement is clear to me. OUTPUT_MAPPING = { "Implementation approach": (str, ...), - "project_name": (str, ...), + "Project name": (str, ...), "File list": (List[str], ...), "Data structures and interfaces": (str, ...), "Program call flow": (str, ...), @@ -173,7 +173,7 @@ ATTENTION: Output carefully referenced "Old Design" in format. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## project_name: Provide as Plain text, concise and clear, characters only use a combination of all lowercase and underscores +## Project name: Constant text "{project_name}". ## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here @@ -229,50 +229,21 @@ class WriteDesign(Action): async def _new_system_design(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) + format_example = format_example.format(project_name=CONFIG.project_name) prompt = prompt_template.format(context=context, format_example=format_example) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - self._rename_project_name(system_design=system_design) - await self._rename_workspace(system_design) return system_design async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): - prompt = MERGE_PROMPT.format(old_design=system_design_doc.content, context=prd_doc.content) + prompt = MERGE_PROMPT.format( + old_design=system_design_doc.content, context=prd_doc.content, project_name=CONFIG.project_name + ) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python # package name" contain space, have to use setattr - self._rename_project_name(system_design=system_design) system_design_doc.content = system_design.instruct_content.json(ensure_ascii=False) return system_design_doc - @staticmethod - def _rename_project_name(system_design): - # fix project_name, we can't system_design.instruct_content.python_package_name = "xxx" since "project_name" - # contain space, have to use setattr - if CONFIG.project_name: - setattr( - system_design.instruct_content, - "project_name", - CONFIG.project_name, - ) - return - setattr( - system_design.instruct_content, - "project_name", - system_design.instruct_content.dict()["project_name"].strip().strip("'").strip('"'), - ) - - @staticmethod - async def _rename_workspace(system_design): - if CONFIG.project_path: # Updating on the old version has already been specified if it's valid. According to - # Section 2.2.3.10 of RFC 135 - return - - if isinstance(system_design, ActionOutput): - ws_name = system_design.instruct_content.dict()["project_name"] - else: - ws_name = CodeParser.parse_str(block="project_name", text=system_design) - CONFIG.git_repo.rename_root(ws_name) - async def _update_system_design(self, filename, prds_file_repo, system_design_file_repo) -> Document: prd = await prds_file_repo.get(filename) old_system_design_doc = await system_design_file_repo.get(filename) @@ -296,10 +267,10 @@ class WriteDesign(Action): @staticmethod async def _save_data_api_design(design_doc): m = json.loads(design_doc.content) - data_api_design = m.get("Data structures and interface definitions") + data_api_design = m.get("Data structures and interfaces") if not data_api_design: return - pathname = CONFIG.git_repo.workdir / Path(DATA_API_DESIGN_FILE_REPO) / Path(design_doc.filename).with_suffix("") + pathname = CONFIG.git_repo.workdir / DATA_API_DESIGN_FILE_REPO / Path(design_doc.filename).with_suffix("") await WriteDesign._save_mermaid_file(data_api_design, pathname) logger.info(f"Save class view to {str(pathname)}") diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index b751dc970..05255dcc5 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -3,7 +3,7 @@ """ @Time : 2023/11/20 @Author : mashenquan -@File : git_repository.py +@File : prepare_documents.py @Desc: PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt. RFC 135 2.2.3.5.1. """ @@ -26,7 +26,10 @@ class PrepareDocuments(Action): if not CONFIG.git_repo: # Create and initialize the workspace folder, initialize the Git environment. project_name = CONFIG.project_name or FileRepository.new_filename() - workdir = Path(CONFIG.project_path or DEFAULT_WORKSPACE_ROOT / project_name) + workdir = CONFIG.project_path + if not workdir and CONFIG.workspace: + workdir = Path(CONFIG.workspace) / project_name + workdir = Path(workdir or DEFAULT_WORKSPACE_ROOT / project_name) if not CONFIG.inc and workdir.exists(): shutil.rmtree(workdir) CONFIG.git_repo = GitRepository() diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 3d59daeed..95da0d65a 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -183,6 +183,10 @@ MERGE_PROMPT = """ ## Old Tasks {old_tasks} ----- + +## Format example +{format_example} +----- Role: You are a project manager; The goal is to merge the new PRD/technical design content from 'Context' into 'Old Tasks.' Based on this merged result, break down tasks, give a task list, and analyze task dependencies to start with the prerequisite modules. Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. @@ -201,7 +205,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Tasks" format, +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Format example" format, and only output the json inside this tag, nothing else """ @@ -264,7 +268,9 @@ class WriteTasks(Action): return rsp async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: - prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content) + _, format_example = get_template(templates, format) + prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content, + format_example=format_example) rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) task_doc.content = rsp.instruct_content.json(ensure_ascii=False) return task_doc diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index 88a37536b..d10cd6c55 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -3,12 +3,15 @@ """ @Author : alexanderwu @File : summarize_code.py +@Modified By: mashenquan, 2023/12/5. Archive the summarization content of issue discovery for use in WriteCode. """ +from pathlib import Path from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO from metagpt.logs import logger from metagpt.utils.file_repository import FileRepository @@ -95,8 +98,10 @@ class SummarizeCode(Action): return code_rsp async def run(self): - design_doc = await FileRepository.get_file(self.context.design_filename) - task_doc = await FileRepository.get_file(self.context.task_filename) + design_pathname = Path(self.context.design_filename) + design_doc = await FileRepository.get_file(filename=design_pathname.name, relative_path=SYSTEM_DESIGN_FILE_REPO) + task_pathname = Path(self.context.task_filename) + task_doc = await FileRepository.get_file(filename=task_pathname.name, relative_path=TASK_FILE_REPO) src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) code_blocks = [] for filename in self.context.codes_filenames: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 59ccb49a5..9b20843c7 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -15,13 +15,13 @@ RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ - from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action -from metagpt.const import TEST_OUTPUTS_FILE_REPO +from metagpt.config import CONFIG +from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO from metagpt.logs import logger -from metagpt.schema import CodingContext, RunCodeResult +from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository @@ -50,6 +50,8 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc # Debug logs ```text {logs} + +{summary_log} ``` ----- @@ -90,18 +92,26 @@ class WriteCode(Action): test_doc = await FileRepository.get_file( filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO ) + summary_doc = None + if coding_context.design_doc.filename: + summary_doc = await FileRepository.get_file( + filename=coding_context.design_doc.filename, relative_path=CODE_SUMMARIES_FILE_REPO + ) logs = "" if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr prompt = PROMPT_TEMPLATE.format( design=coding_context.design_doc.content, - tasks=coding_context.task_doc.content, - code=coding_context.code_doc.content, + tasks=coding_context.task_doc.content if coding_context.task_doc else "", + code=coding_context.code_doc.content if coding_context.code_doc else "", logs=logs, filename=self.context.filename, + summary_log=summary_doc.content if summary_doc else "", ) logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) + if not coding_context.code_doc: + coding_context.code_doc = Document(filename=coding_context.filename, root_path=CONFIG.src_workspace) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 364f6af57..f7c6845d2 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -108,10 +108,11 @@ class WriteCodeReview(Action): k = CONFIG.code_review_k_times or 1 for i in range(k): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) + task_content = self.context.task_doc.content if self.context.task_doc else "" context = "\n----------\n".join( [ "```text\n" + self.context.design_doc.content + "```\n", - "```text\n" + self.context.task_doc.content + "```\n", + "```text\n" + task_content + "```\n", "```python\n" + self.context.code_doc.content + "```\n", ] ) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 3967a0578..530a22def 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -8,6 +8,7 @@ 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.2 of RFC 135, add incremental iteration functionality. 3. Move the document storage operations related to WritePRD from the save operation of WriteDesign. +@Modified By: mashenquan, 2023/12/5. Move the generation logic of the project name to WritePRD. """ from __future__ import annotations @@ -27,6 +28,7 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.schema import Document, Documents +from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file @@ -53,7 +55,7 @@ ATTENTION: Output carefully referenced "Format example" in format. {{ "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. "Original Requirements": "", # str, place the polished complete original requirements here - "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. "Search Information": "", "Requirements": "", "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. @@ -85,9 +87,10 @@ and only output the json inside this tag, nothing else """, "FORMAT_EXAMPLE": """ [CONTENT] -{ +{{ "Language": "", "Original Requirements": "", + "Project Name": "{project_name}", "Search Information": "", "Requirements": "", "Product Goals": [], @@ -111,7 +114,7 @@ and only output the json inside this tag, nothing else "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], "UI Design draft": "", "Anything UNCLEAR": "", -} +}} [/CONTENT] """, }, @@ -228,6 +231,7 @@ There are no unclear points. OUTPUT_MAPPING = { "Language": (str, ...), "Original Requirements": (str, ...), + "Project Name": (str, ...), "Product Goals": (List[str], ...), "User Stories": (List[str], ...), "Competitive Analysis": (List[str], ...), @@ -270,7 +274,7 @@ ATTENTION: Output carefully referenced "Old PRD" in format. {{ "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. "Original Requirements": "", # str, place the polished complete original requirements here - "project_name": "", # str, name it like game_2048 / web_2048 / simple_crm etc. + "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. "Search Information": "", "Requirements": "", "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. @@ -320,6 +324,7 @@ class WritePRD(Action): if not prd_doc: continue change_files.docs[prd_doc.filename] = prd_doc + logger.info(f"REWRITE PRD:{prd_doc.filename}") # If there is no existing PRD, generate one using 'docs/requirement.txt'. if not change_files.docs: prd_doc = await self._update_prd( @@ -327,6 +332,7 @@ class WritePRD(Action): ) if prd_doc: change_files.docs[prd_doc.filename] = prd_doc + logger.info(f"NEW PRD:{prd_doc.filename}") # Once all files under 'docs/prds/' have been compared with the newly added requirements, trigger the # 'publish' message to transition the workflow to the next stage. This design allows room for global # optimization in subsequent steps. @@ -343,32 +349,36 @@ class WritePRD(Action): # logger.info(format) prompt_template, format_example = get_template(templates, format) + project_name = CONFIG.project_name if CONFIG.project_name else "" + format_example = format_example.format(project_name=project_name) # logger.info(prompt_template) # logger.info(format_example) prompt = prompt_template.format( - requirements=requirements, search_information=info, format_example=format_example + requirements=requirements, search_information=info, format_example=format_example, project_name=project_name ) # logger.info(prompt) # prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) + await self._rename_workspace(prd) return prd async def _is_relative_to(self, new_requirement_doc, old_prd_doc) -> bool: - m = json.loads(old_prd_doc.content) - if m.get("Original Requirements") == new_requirement_doc.content: - # There have been no changes in the requirements, so they are considered unrelated. - return False prompt = IS_RELATIVE_PROMPT.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) res = await self._aask(prompt=prompt) - logger.info(f"[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") + logger.info(f"REQ-RELATIVE:[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") if "YES" in res: return True return False async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: - prompt = MERGE_PROMPT.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) + if not CONFIG.project_name: + CONFIG.project_name = Path(CONFIG.project_path).name + prompt = MERGE_PROMPT.format( + requirements=new_requirement_doc.content, old_prd=prd_doc.content, project_name=CONFIG.project_name + ) prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) prd_doc.content = prd.instruct_content.json(ensure_ascii=False) + await self._rename_workspace(prd) return prd_doc async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: @@ -404,3 +414,19 @@ class WritePRD(Action): @staticmethod async def _save_pdf(prd_doc): await FileRepository.save_as(doc=prd_doc, with_suffix=".md", relative_path=PRD_PDF_FILE_REPO) + + @staticmethod + async def _rename_workspace(prd): + if CONFIG.project_path: # Updating on the old version has already been specified if it's valid. According to + # Section 2.2.3.10 of RFC 135 + if not CONFIG.project_name: + CONFIG.project_name = Path(CONFIG.project_path).name + return + + if not CONFIG.project_name: + if isinstance(prd, ActionOutput): + ws_name = prd.instruct_content.dict()["Project Name"] + else: + ws_name = CodeParser.parse_str(block="Project Name", text=prd) + CONFIG.project_name = ws_name + CONFIG.git_repo.rename_root(CONFIG.project_name) diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 7cbb42e1d..65673807f 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -9,8 +9,9 @@ """ from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.const import TEST_CODES_FILE_REPO from metagpt.logs import logger -from metagpt.schema import TestingContext +from metagpt.schema import Document, TestingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -52,6 +53,10 @@ class WriteTest(Action): return code async def run(self, *args, **kwargs) -> TestingContext: + if not self.context.test_doc: + self.context.test_doc = Document( + filename="test_" + self.context.code_doc.filename, root_path=TEST_CODES_FILE_REPO + ) prompt = PROMPT_TEMPLATE.format( code_to_test=self.context.code_doc.content, test_file_name=self.context.test_doc.filename, diff --git a/metagpt/const.py b/metagpt/const.py index a646cea7a..bd735a5e1 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023-11-1. According to Section 2.2.1 and 2.2.2 of RFC 116, added key definitions for common properties in the Message. @Modified By: mashenquan, 2023-11-27. Defines file repository paths according to Section 2.2.3.4 of RFC 135. +@Modified By: mashenquan, 2023/12/5. Add directories for code summarization.. """ import contextvars import os @@ -87,5 +88,7 @@ PRD_PDF_FILE_REPO = "resources/prd" TASK_PDF_FILE_REPO = "resources/api_spec_and_tasks" TEST_CODES_FILE_REPO = "tests" TEST_OUTPUTS_FILE_REPO = "test_outputs" +CODE_SUMMARIES_FILE_REPO = "docs/code_summaries" +CODE_SUMMARIES_PDF_FILE_REPO = "resources/code_summaries" YAPI_URL = "http://yapi.deepwisdomai.com/" diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index 565ae94f7..6c1dc8338 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -38,7 +38,7 @@ class BaseGPTAPI(BaseChatbot): rsp = self.completion(message) return self.get_choice_text(rsp) - async def aask(self, msg: str, system_msgs: Optional[list[str]] = None) -> str: + async def aask(self, msg: str, system_msgs: Optional[list[str]] = None, stream=True) -> str: if system_msgs: message = ( self._system_msgs(system_msgs) + [self._user_msg(msg)] @@ -49,7 +49,7 @@ class BaseGPTAPI(BaseChatbot): message = ( [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt else [self._user_msg(msg)] ) - rsp = await self.acompletion_text(message, stream=True) + rsp = await self.acompletion_text(message, stream=stream) logger.debug(message) # logger.debug(rsp) return rsp diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index d42835a1b..9f8eb6482 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -13,17 +13,25 @@ @Modified By: mashenquan, 2023-11-27. 1. According to Section 2.2.3.1 of RFC 135, replace file data in the message with the file name. 2. According to the design in Section 2.2.3.5.5 of RFC 135, add incremental iteration functionality. +@Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results + of SummarizeCode. """ from __future__ import annotations import json +from collections import defaultdict from pathlib import Path from typing import Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG -from metagpt.const import MESSAGE_ROUTE_TO_NONE, SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO +from metagpt.const import ( + CODE_SUMMARIES_FILE_REPO, + CODE_SUMMARIES_PDF_FILE_REPO, + SYSTEM_DESIGN_FILE_REPO, + TASK_FILE_REPO, +) from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import ( @@ -33,6 +41,16 @@ from metagpt.schema import ( Documents, Message, ) +from metagpt.utils.common import any_to_str, any_to_str_set + +IS_PASS_PROMPT = """ +{context} + +---- +Does the above log indicate anything that needs to be done? +If there are any tasks to be completed, please answer 'NO' along with the to-do list in JSON format; +otherwise, answer 'YES' in JSON format. +""" class Engineer(Role): @@ -60,7 +78,7 @@ class Engineer(Role): """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) self.use_code_review = use_code_review - self._watch([WriteTasks]) + self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview]) self.code_todos = [] self.summarize_todos = [] self.n_borg = n_borg @@ -105,39 +123,88 @@ class Engineer(Role): if self._rc.todo is None: return None if isinstance(self._rc.todo, WriteCode): - changed_files = await self._act_sp_with_cr(review=self.use_code_review) - # Unit tests only. - if CONFIG.REQA_FILENAME and CONFIG.REQA_FILENAME not in changed_files: - changed_files.add(CONFIG.REQA_FILENAME) - return Message( - content="\n".join(changed_files), - role=self.profile, - cause_by=WriteCodeReview if self.use_code_review else WriteCode, - send_to="Edward", # The name of QaEngineer - ) + return await self._act_write_code() if isinstance(self._rc.todo, SummarizeCode): - summaries = [] - for todo in self.summarize_todos: - summary = await todo.run() - summaries.append(summary.json(ensure_ascii=False)) + return await self._act_summarize() + return None + + async def _act_write_code(self): + changed_files = await self._act_sp_with_cr(review=self.use_code_review) + return Message( + content="\n".join(changed_files), + role=self.profile, + cause_by=WriteCodeReview if self.use_code_review else WriteCode, + send_to=self, + sent_from=self, + ) + + async def _act_summarize(self): + code_summaries_file_repo = CONFIG.git_repo.new_file_repository(CODE_SUMMARIES_FILE_REPO) + code_summaries_pdf_file_repo = CONFIG.git_repo.new_file_repository(CODE_SUMMARIES_PDF_FILE_REPO) + tasks = [] + src_relative_path = CONFIG.src_workspace.relative_to(CONFIG.git_repo.workdir) + for todo in self.summarize_todos: + summary = await todo.run() + summary_filename = Path(todo.context.design_filename).with_suffix(".md").name + dependencies = {todo.context.design_filename, todo.context.task_filename} + for filename in todo.context.codes_filenames: + rpath = src_relative_path / filename + dependencies.add(str(rpath)) + await code_summaries_pdf_file_repo.save( + filename=summary_filename, content=summary, dependencies=dependencies + ) + is_pass, reason = await self._is_pass(summary) + if not is_pass: + todo.context.reason = reason + tasks.append(todo.context.dict()) + await code_summaries_file_repo.save( + filename=Path(todo.context.design_filename).name, + content=todo.context.json(), + dependencies=dependencies, + ) + else: + await code_summaries_file_repo.delete(filename=Path(todo.context.design_filename).name) + + logger.info(f"--max-auto-summarize-code={CONFIG.max_auto_summarize_code}") + if not tasks or CONFIG.max_auto_summarize_code == 0: return Message( - content="\n".join(summaries), + content="", role=self.profile, cause_by=SummarizeCode, - send_to=MESSAGE_ROUTE_TO_NONE, + sent_from=self, + send_to="Edward", # The name of QaEngineer ) - return None + # The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. + # This parameter is used for debugging the workflow. + CONFIG.max_auto_summarize_code -= 1 if CONFIG.max_auto_summarize_code > 0 else 0 + return Message( + content=json.dumps(tasks), role=self.profile, cause_by=SummarizeCode, send_to=self, sent_from=self + ) + + async def _is_pass(self, summary) -> (str, str): + rsp = await self._llm.aask(msg=IS_PASS_PROMPT.format(context=summary), stream=False) + logger.info(rsp) + if "YES" in rsp: + return True, rsp + return False, rsp async def _think(self) -> Action | None: if not CONFIG.src_workspace: CONFIG.src_workspace = CONFIG.git_repo.workdir / CONFIG.git_repo.workdir.name - if not self.code_todos: - await self._new_code_actions() - elif not self.summarize_todos: - await self._new_summarize_actions() - else: + write_code_filters = any_to_str_set([WriteTasks, SummarizeCode]) + summarize_code_filters = any_to_str_set([WriteCode, WriteCodeReview]) + if not self._rc.news: return None - return self._rc.todo # For agent store + msg = self._rc.news[0] + if msg.cause_by in write_code_filters: + logger.info(f"TODO WriteCode:{msg.json()}") + await self._new_code_actions() + return self._rc.todo + if msg.cause_by in summarize_code_filters and msg.sent_from == any_to_str(self): + logger.info(f"TODO SummarizeCode:{msg.json()}") + await self._new_summarize_actions() + return self._rc.todo + return None @staticmethod async def _new_coding_context( @@ -151,9 +218,9 @@ class Engineer(Role): design_doc = None for i in dependencies: if str(i.parent) == TASK_FILE_REPO: - task_doc = task_file_repo.get(i.filename) + task_doc = await task_file_repo.get(i.name) elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO: - design_doc = design_file_repo.get(i.filename) + design_doc = await design_file_repo.get(i.name) context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc) return context @@ -216,16 +283,13 @@ class Engineer(Role): async def _new_summarize_actions(self): src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) - changed_src_files = src_file_repo.changed_files + src_files = src_file_repo.all_files # Generate a SummarizeCode action for each pair of (system_design_doc, task_doc). - summarizations = {} - for filename in changed_src_files: - dependencies = src_file_repo.get_dependency(filename=filename) + summarizations = defaultdict(list) + for filename in src_files: + dependencies = await src_file_repo.get_dependency(filename=filename) ctx = CodeSummarizeContext.loads(filenames=dependencies) - if ctx not in summarizations: - summarizations[ctx] = set() - srcs = summarizations.get(ctx) - srcs.add(filename) + summarizations[ctx].append(filename) for ctx, filenames in summarizations.items(): ctx.codes_filenames = filenames self.summarize_todos.append(SummarizeCode(context=ctx, llm=self._llm)) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 41a3213dc..15a01b9e9 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -11,10 +11,13 @@ WriteTest/RunCode/DebugError object, rather than passing them in when calling the run function. 2. According to Section 2.2.3.5.7 of RFC 135, change the method of transferring files from using the Message to using file references. +@Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results + of SummarizeCode. """ from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest # from metagpt.const import WORKSPACE_ROOT +from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_TO_NONE, @@ -40,13 +43,16 @@ class QaEngineer(Role): self._init_actions( [WriteTest] ) # FIXME: a bit hack here, only init one action to circumvent _think() logic, will overwrite _think() in future updates - self._watch([WriteCode, WriteCodeReview, WriteTest, RunCode, DebugError]) + self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 self.test_round_allowed = test_round_allowed async def _write_test(self, message: Message) -> None: - changed_files = message.content.splitlines() src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) + changed_files = set(src_file_repo.changed_files.keys()) + # Unit tests only. + if CONFIG.reqa_file and CONFIG.reqa_file not in changed_files: + changed_files.add(CONFIG.reqa_file) tests_file_repo = CONFIG.git_repo.new_file_repository(TEST_CODES_FILE_REPO) for filename in changed_files: # write tests @@ -146,7 +152,7 @@ class QaEngineer(Role): ) return result_msg - code_filters = any_to_str_set({WriteCode, WriteCodeReview}) + code_filters = any_to_str_set({SummarizeCode}) test_filters = any_to_str_set({WriteTest, DebugError}) run_filters = any_to_str_set({RunCode}) for msg in self._rc.news: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1e99cc1ff..2651be7eb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -284,9 +284,10 @@ class Role: instruct_content=response.instruct_content, role=self.profile, cause_by=self._rc.todo, + sent_from=self, ) else: - msg = Message(content=response, role=self.profile, cause_by=self._rc.todo) + msg = Message(content=response, role=self.profile, cause_by=self._rc.todo, sent_from=self) self._rc.memory.add(msg) return msg diff --git a/metagpt/schema.py b/metagpt/schema.py index d1174799a..a8c1b7726 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -48,9 +48,9 @@ class Document(BaseModel): Represents a document. """ - root_path: str - filename: str - content: Optional[str] = None + root_path: str = "" + filename: str = "" + content: str = "" def get_meta(self) -> Document: """Get metadata of the document. @@ -260,8 +260,8 @@ class MessageQueue: class CodingContext(BaseModel): filename: str design_doc: Document - task_doc: Document - code_doc: Document + task_doc: Optional[Document] + code_doc: Optional[Document] @staticmethod def loads(val: str) -> CodingContext | None: @@ -275,7 +275,7 @@ class CodingContext(BaseModel): class TestingContext(BaseModel): filename: str code_doc: Document - test_doc: Document + test_doc: Optional[Document] @staticmethod def loads(val: str) -> TestingContext | None: @@ -324,10 +324,11 @@ class RunCodeResult(BaseModel): class CodeSummarizeContext(BaseModel): design_filename: str = "" task_filename: str = "" - codes_filenames: Set[str] = Field(default_factory=set) + codes_filenames: List[str] = Field(default_factory=list) + reason: str = "" @staticmethod - def loads(filenames: Set) -> CodeSummarizeContext: + def loads(filenames: List) -> CodeSummarizeContext: ctx = CodeSummarizeContext() for filename in filenames: if Path(filename).is_relative_to(SYSTEM_DESIGN_FILE_REPO): @@ -337,3 +338,6 @@ class CodeSummarizeContext(BaseModel): ctx.task_filename = str(filename) continue return ctx + + def __hash__(self): + return hash((self.design_filename, self.task_filename)) diff --git a/metagpt/startup.py b/metagpt/startup.py index 78f32d556..f930c386b 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio +from pathlib import Path import typer @@ -24,6 +25,10 @@ def startup( help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", ), reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), + max_auto_summarize_code: int = typer.Option( + default=-1, + help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. This parameter is used for debugging the workflow.", + ), ): """Run a startup. Be a boss.""" from metagpt.roles import ( @@ -36,10 +41,14 @@ def startup( from metagpt.team import Team # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. + CONFIG.project_path = project_path + if project_path: + inc = True + project_name = project_name or Path(project_path).name CONFIG.project_name = project_name CONFIG.inc = inc - CONFIG.project_path = project_path CONFIG.reqa_file = reqa_file + CONFIG.max_auto_summarize_code = max_auto_summarize_code company = Team() company.hire( diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index 653e07ef9..e8347d567 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -14,6 +14,7 @@ from typing import Set import aiofiles +from metagpt.config import CONFIG from metagpt.logs import logger @@ -81,7 +82,7 @@ class DependencyFile: if persist: await self.save() - async def get(self, filename: Path | str, persist=False): + async def get(self, filename: Path | str, persist=True): """Get dependencies for a file asynchronously. :param filename: The filename or path. @@ -91,7 +92,7 @@ class DependencyFile: if persist: await self.load() - root = self._filename.parent + root = CONFIG.git_repo.workdir try: key = Path(filename).relative_to(root) except ValueError: diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 0815bf90a..2cace7232 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -151,6 +151,17 @@ class FileRepository: relative_files[str(rf)] = ct return relative_files + @property + def all_files(self) -> List: + """Get a dictionary of all files in the repository. + + The dictionary includes file paths relative to the current FileRepository. + + :return: A dictionary where keys are file paths and values are file information. + :rtype: List + """ + return self._git_repo.get_files(relative_path=self._relative_path) + def get_change_dir_files(self, dir: Path | str) -> List: """Get the files in a directory that have changed. @@ -259,3 +270,25 @@ class FileRepository: """ file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) return await file_repo.save_doc(doc=doc, with_suffix=with_suffix, dependencies=dependencies) + + async def delete(self, filename: Path | str): + """Delete a file from the file repository. + + This method deletes a file from the file repository based on the provided filename. + + :param filename: The name or path of the file to be deleted. + :type filename: Path or str + """ + pathname = self.workdir / filename + if not pathname.exists(): + return + pathname.unlink(missing_ok=True) + + dependency_file = await self._git_repo.get_dependency() + await dependency_file.update(filename=pathname, dependencies=None) + logger.info(f"remove dependency key: {str(pathname)}") + + @staticmethod + async def delete_file(filename: Path | str, relative_path: Path | str = "."): + file_repo = CONFIG.git_repo.new_file_repository(relative_path=relative_path) + await file_repo.delete(filename=filename) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 7c9ec645f..5aec4509c 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -8,10 +8,11 @@ """ from __future__ import annotations +import os import shutil from enum import Enum from pathlib import Path -from typing import Dict +from typing import Dict, List from git.repo import Repo from git.repo.fun import is_git_dir @@ -196,10 +197,46 @@ class GitRepository: if new_path.exists(): logger.info(f"Delete directory {str(new_path)}") shutil.rmtree(new_path) - self.workdir.rename(new_path) + try: + shutil.move(src=str(self.workdir), dst=str(new_path)) + except Exception as e: + logger.warning(f"Move {str(self.workdir)} to {str(new_path)} error: {e}") logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) + def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None) -> List: + """Retrieve a list of files in the specified relative path. + + The method returns a list of file paths relative to the current FileRepository. + + :param relative_path: The relative path within the repository. + :type relative_path: Path or str + :param root_relative_path: The root relative path within the repository. + :type root_relative_path: Path or str + :return: A list of file paths in the specified directory. + :rtype: List[str] + """ + try: + relative_path = Path(relative_path).relative_to(self.workdir) + except ValueError: + relative_path = Path(relative_path) + + if not root_relative_path: + root_relative_path = Path(self.workdir) / relative_path + files = [] + try: + directory_path = Path(self.workdir) / relative_path + for file_path in directory_path.iterdir(): + if file_path.is_file(): + rpath = file_path.relative_to(root_relative_path) + files.append(str(rpath)) + else: + subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path) + files.extend(subfolder_files) + except Exception as e: + logger.error(f"Error: {e}") + return files + if __name__ == "__main__": path = DEFAULT_WORKSPACE_ROOT / "git" diff --git a/tests/conftest.py b/tests/conftest.py index d2ac8304f..8e4422700 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,8 +12,11 @@ from unittest.mock import Mock import pytest +from metagpt.config import CONFIG +from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.provider.openai_api import OpenAIGPTAPI as GPTAPI +from metagpt.utils.git_repository import GitRepository class Context: @@ -68,3 +71,16 @@ def proxy(): server = asyncio.get_event_loop().run_until_complete(asyncio.start_server(handle_client, "127.0.0.1", 0)) return "http://{}:{}".format(*server.sockets[0].getsockname()) + + +# init & dispose git repo +@pytest.fixture(scope="session", autouse=True) +def setup_and_teardown_git_repo(request): + CONFIG.git_repo = GitRepository(local_path=DEFAULT_WORKSPACE_ROOT / "unittest") + + # Destroy git repo at the end of the test session. + def fin(): + CONFIG.git_repo.delete_repository() + + # Register the function for destroying the environment. + request.addfinalizer(fin) diff --git a/tests/metagpt/actions/mock.py b/tests/metagpt/actions/mock.py index c48913755..f6602a82b 100644 --- a/tests/metagpt/actions/mock.py +++ b/tests/metagpt/actions/mock.py @@ -90,7 +90,7 @@ Python's in-built data structures like lists and dictionaries will be used exten For testing, we can use the PyTest framework. This is a mature full-featured Python testing tool that helps you write better programs. -## project_name: +## Project Name: ```python "adventure_game" ``` diff --git a/tests/metagpt/actions/test_debug_error.py b/tests/metagpt/actions/test_debug_error.py index 2393d2cc9..8289fe41b 100644 --- a/tests/metagpt/actions/test_debug_error.py +++ b/tests/metagpt/actions/test_debug_error.py @@ -4,17 +4,19 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : test_debug_error.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ +import uuid + import pytest from metagpt.actions.debug_error import DebugError +from metagpt.config import CONFIG +from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO +from metagpt.schema import RunCodeContext, RunCodeResult +from metagpt.utils.file_repository import FileRepository -EXAMPLE_MSG_CONTENT = ''' ---- -## Development Code File Name -player.py -## Development Code -```python +CODE_CONTENT = ''' from typing import List from deck import Deck from card import Card @@ -58,12 +60,9 @@ class Player: if self.score > 21 and any(card.rank == 'A' for card in self.hand): self.score -= 10 return self.score +''' -``` -## Test File Name -test_player.py -## Test Code -```python +TEST_CONTENT = """ import unittest from blackjack_game.player import Player from blackjack_game.deck import Deck @@ -114,42 +113,41 @@ class TestPlayer(unittest.TestCase): if __name__ == '__main__': unittest.main() -``` -## Running Command -python tests/test_player.py -## Running Output -standard output: ; -standard errors: ..F.. -====================================================================== -FAIL: test_player_calculate_score_with_multiple_aces (__main__.TestPlayer) ----------------------------------------------------------------------- -Traceback (most recent call last): - File "tests/test_player.py", line 46, in test_player_calculate_score_with_multiple_aces - self.assertEqual(player.score, 12) -AssertionError: 22 != 12 - ----------------------------------------------------------------------- -Ran 5 tests in 0.007s - -FAILED (failures=1) -; -## instruction: -The error is in the development code, specifically in the calculate_score method of the Player class. The method is not correctly handling the case where there are multiple Aces in the player's hand. The current implementation only subtracts 10 from the score once if the score is over 21 and there's an Ace in the hand. However, in the case of multiple Aces, it should subtract 10 for each Ace until the score is 21 or less. -## File To Rewrite: -player.py -## Status: -FAIL -## Send To: -Engineer ---- -''' +""" @pytest.mark.asyncio async def test_debug_error(): - debug_error = DebugError("debug_error") + CONFIG.src_workspace = CONFIG.git_repo.workdir / uuid.uuid4().hex + ctx = RunCodeContext( + code_filename="player.py", + test_filename="test_player.py", + command=["python", "tests/test_player.py"], + output_filename="output.log", + ) - file_name, rewritten_code = await debug_error.run(context=EXAMPLE_MSG_CONTENT) + await FileRepository.save_file(filename=ctx.code_filename, content=CODE_CONTENT, relative_path=CONFIG.src_workspace) + await FileRepository.save_file(filename=ctx.test_filename, content=TEST_CONTENT, relative_path=TEST_CODES_FILE_REPO) + output_data = RunCodeResult( + stdout=";", + stderr="", + summary="======================================================================\n" + "FAIL: test_player_calculate_score_with_multiple_aces (__main__.TestPlayer)\n" + "----------------------------------------------------------------------\n" + "Traceback (most recent call last):\n" + ' File "tests/test_player.py", line 46, in test_player_calculate_score_' + "with_multiple_aces\n" + " self.assertEqual(player.score, 12)\nAssertionError: 22 != 12\n\n" + "----------------------------------------------------------------------\n" + "Ran 5 tests in 0.007s\n\nFAILED (failures=1)\n;\n", + ) + await FileRepository.save_file( + filename=ctx.output_filename, content=output_data.json(), relative_path=TEST_OUTPUTS_FILE_REPO + ) + debug_error = DebugError(context=ctx) - assert "class Player" in rewritten_code # rewrite the same class - assert "while self.score > 21" in rewritten_code # a key logic to rewrite to (original one is "if self.score > 12") + rsp = await debug_error.run() + + assert "class Player" in rsp # rewrite the same class + # a key logic to rewrite to (original one is "if self.score > 12") + assert "while self.score > 21" in rsp diff --git a/tests/metagpt/actions/test_design_api.py b/tests/metagpt/actions/test_design_api.py index 0add8fb74..e90707d1a 100644 --- a/tests/metagpt/actions/test_design_api.py +++ b/tests/metagpt/actions/test_design_api.py @@ -4,33 +4,27 @@ @Time : 2023/5/11 19:26 @Author : alexanderwu @File : test_design_api.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.design_api import WriteDesign +from metagpt.const import PRDS_FILE_REPO from metagpt.logs import logger from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository from tests.metagpt.actions.mock import PRD_SAMPLE @pytest.mark.asyncio async def test_design_api(): - prd = "我们需要一个音乐播放器,它应该有播放、暂停、上一曲、下一曲等功能。" + inputs = ["我们需要一个音乐播放器,它应该有播放、暂停、上一曲、下一曲等功能。", PRD_SAMPLE] + for prd in inputs: + await FileRepository.save_file("new_prd.txt", content=prd, relative_path=PRDS_FILE_REPO) - design_api = WriteDesign("design_api") + design_api = WriteDesign("design_api") - result = await design_api.run([Message(content=prd, instruct_content=None)]) - logger.info(result) + result = await design_api.run([Message(content=prd, instruct_content=None)]) + logger.info(result) - assert result - - -@pytest.mark.asyncio -async def test_design_api_calculator(): - prd = PRD_SAMPLE - - design_api = WriteDesign("design_api") - result = await design_api.run([Message(content=prd, instruct_content=None)]) - logger.info(result) - - assert result + assert result diff --git a/tests/metagpt/actions/test_prepare_documents.py b/tests/metagpt/actions/test_prepare_documents.py new file mode 100644 index 000000000..31c8bcb80 --- /dev/null +++ b/tests/metagpt/actions/test_prepare_documents.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/6 +@Author : mashenquan +@File : test_prepare_documents.py +@Desc: Unit test for prepare_documents.py +""" +import pytest + +from metagpt.actions.prepare_documents import PrepareDocuments +from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository + + +@pytest.mark.asyncio +async def test_prepare_documents(): + msg = Message(content="New user requirements balabala...") + + if CONFIG.git_repo: + CONFIG.git_repo.delete_repository() + CONFIG.git_repo = None + + await PrepareDocuments().run(with_messages=[msg]) + assert CONFIG.git_repo + doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) + assert doc + assert doc.content == msg.content diff --git a/tests/metagpt/actions/test_run_code.py b/tests/metagpt/actions/test_run_code.py index 1e451cb14..888418974 100644 --- a/tests/metagpt/actions/test_run_code.py +++ b/tests/metagpt/actions/test_run_code.py @@ -4,10 +4,12 @@ @Time : 2023/5/11 17:46 @Author : alexanderwu @File : test_run_code.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.run_code import RunCode +from metagpt.schema import RunCodeContext @pytest.mark.asyncio @@ -35,37 +37,29 @@ async def test_run_script(): @pytest.mark.asyncio async def test_run(): - action = RunCode() - result = await action.run(mode="text", code="print('Hello, World')") - assert "PASS" in result - - result = await action.run( - mode="script", - code="echo 'Hello World'", - code_file_name="", - test_code="", - test_file_name="", - command=["echo", "Hello World"], - working_directory=".", - additional_python_paths=[], - ) - assert "PASS" in result - - -@pytest.mark.asyncio -async def test_run_failure(): - action = RunCode() - result = await action.run(mode="text", code="result = 1 / 0") - assert "FAIL" in result - - result = await action.run( - mode="script", - code='python -c "print(1/0)"', - code_file_name="", - test_code="", - test_file_name="", - command=["python", "-c", "print(1/0)"], - working_directory=".", - additional_python_paths=[], - ) - assert "FAIL" in result + inputs = [ + (RunCodeContext(mode="text", code_filename="a.txt", code="print('Hello, World')"), "PASS"), + ( + RunCodeContext( + mode="script", + code_filename="a.sh", + code="echo 'Hello World'", + command=["echo", "Hello World"], + working_directory=".", + ), + "PASS", + ), + ( + RunCodeContext( + mode="script", + code_filename="a.py", + code='python -c "print(1/0)"', + command=["python", "-c", "print(1/0)"], + working_directory=".", + ), + "FAIL", + ), + ] + for ctx, result in inputs: + rsp = await RunCode(context=ctx).run() + assert result in rsp.summary diff --git a/tests/metagpt/actions/test_summarize_code.py b/tests/metagpt/actions/test_summarize_code.py new file mode 100644 index 000000000..7ecb67afd --- /dev/null +++ b/tests/metagpt/actions/test_summarize_code.py @@ -0,0 +1,195 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/5/11 17:46 +@Author : mashenquan +@File : test_summarize_code.py +@Modifiled By: mashenquan, 2023-12-6. Unit test for summarize_code.py +""" +import pytest + +from metagpt.actions.summarize_code import SummarizeCode +from metagpt.config import CONFIG +from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO +from metagpt.logs import logger +from metagpt.schema import CodeSummarizeContext +from metagpt.utils.file_repository import FileRepository + +DESIGN_CONTENT = """ +{"Implementation approach": "To develop this snake game, we will use the Python language and choose the Pygame library. Pygame is an open-source Python module collection specifically designed for writing video games. It provides functionalities such as displaying images and playing sounds, making it suitable for creating intuitive and responsive user interfaces. We will ensure efficient game logic to prevent any delays during gameplay. The scoring system will be simple, with the snake gaining points for each food it eats. We will use Pygame's event handling system to implement pause and resume functionality, as well as high-score tracking. The difficulty will increase by speeding up the snake's movement. In the initial version, we will focus on single-player mode and consider adding multiplayer mode and customizable skins in future updates. Based on the new requirement, we will also add a moving obstacle that appears randomly. If the snake eats this obstacle, the game will end. If the snake does not eat the obstacle, it will disappear after 5 seconds. For this, we need to add mechanisms for obstacle generation, movement, and disappearance in the game logic.", "Project_name": "snake_game", "File list": ["main.py", "game.py", "snake.py", "food.py", "obstacle.py", "scoreboard.py", "constants.py", "assets/styles.css", "assets/index.html"], "Data structures and interfaces": "```mermaid\n classDiagram\n class Game{\n +int score\n +int speed\n +bool game_over\n +bool paused\n +Snake snake\n +Food food\n +Obstacle obstacle\n +Scoreboard scoreboard\n +start_game() void\n +pause_game() void\n +resume_game() void\n +end_game() void\n +increase_difficulty() void\n +update() void\n +render() void\n Game()\n }\n class Snake{\n +list body_parts\n +str direction\n +bool grow\n +move() void\n +grow() void\n +check_collision() bool\n Snake()\n }\n class Food{\n +tuple position\n +spawn() void\n Food()\n }\n class Obstacle{\n +tuple position\n +int lifetime\n +bool active\n +spawn() void\n +move() void\n +check_collision() bool\n +disappear() void\n Obstacle()\n }\n class Scoreboard{\n +int high_score\n +update_score(int) void\n +reset_score() void\n +load_high_score() void\n +save_high_score() void\n Scoreboard()\n }\n class Constants{\n }\n Game \"1\" -- \"1\" Snake: has\n Game \"1\" -- \"1\" Food: has\n Game \"1\" -- \"1\" Obstacle: has\n Game \"1\" -- \"1\" Scoreboard: has\n ```", "Program call flow": "```sequenceDiagram\n participant M as Main\n participant G as Game\n participant S as Snake\n participant F as Food\n participant O as Obstacle\n participant SB as Scoreboard\n M->>G: start_game()\n loop game loop\n G->>S: move()\n G->>S: check_collision()\n G->>F: spawn()\n G->>O: spawn()\n G->>O: move()\n G->>O: check_collision()\n G->>O: disappear()\n G->>SB: update_score(score)\n G->>G: update()\n G->>G: render()\n alt if paused\n M->>G: pause_game()\n M->>G: resume_game()\n end\n alt if game_over\n G->>M: end_game()\n end\n end\n```", "Anything UNCLEAR": "There is no need for further clarification as the requirements are already clear."} +""" + +TASK_CONTENT = """ +{"Required Python third-party packages": ["pygame==2.0.1"], "Required Other language third-party packages": ["No third-party packages required for other languages."], "Full API spec": "\n openapi: 3.0.0\n info:\n title: Snake Game API\n version: \"1.0.0\"\n paths:\n /start:\n get:\n summary: Start the game\n responses:\n '200':\n description: Game started successfully\n /pause:\n get:\n summary: Pause the game\n responses:\n '200':\n description: Game paused successfully\n /resume:\n get:\n summary: Resume the game\n responses:\n '200':\n description: Game resumed successfully\n /end:\n get:\n summary: End the game\n responses:\n '200':\n description: Game ended successfully\n /score:\n get:\n summary: Get the current score\n responses:\n '200':\n description: Current score retrieved successfully\n /highscore:\n get:\n summary: Get the high score\n responses:\n '200':\n description: High score retrieved successfully\n components: {}\n ", "Logic Analysis": [["constants.py", "Contains all the constant values like screen size, colors, game speeds, etc. This should be implemented first as it provides the base values for other components."], ["snake.py", "Contains the Snake class with methods for movement, growth, and collision detection. It is dependent on constants.py for configuration values."], ["food.py", "Contains the Food class responsible for spawning food items on the screen. It is dependent on constants.py for configuration values."], ["obstacle.py", "Contains the Obstacle class with methods for spawning, moving, and disappearing of obstacles, as well as collision detection with the snake. It is dependent on constants.py for configuration values."], ["scoreboard.py", "Contains the Scoreboard class for updating, resetting, loading, and saving high scores. It may use constants.py for configuration values and depends on the game's scoring logic."], ["game.py", "Contains the main Game class which includes the game loop and methods for starting, pausing, resuming, and ending the game. It is dependent on snake.py, food.py, obstacle.py, and scoreboard.py."], ["main.py", "The entry point of the game that initializes the game and starts the game loop. It is dependent on game.py."]], "Task list": ["constants.py", "snake.py", "food.py", "obstacle.py", "scoreboard.py", "game.py", "main.py"], "Shared Knowledge": "\n 'constants.py' should contain all the necessary configurations for the game, such as screen dimensions, color definitions, and speed settings. These constants will be used across multiple files, ensuring consistency and ease of updates. Ensure that the Pygame library is initialized correctly in 'main.py' before starting the game loop. Also, make sure that the game's state is managed properly when pausing and resuming the game.\n ", "Anything UNCLEAR": "The interaction between the 'obstacle.py' and the game loop needs to be clearly defined to ensure obstacles appear and disappear correctly. The lifetime of the obstacle and its random movement should be implemented in a way that does not interfere with the game's performance."} +""" + +FOOD_PY = """ +## food.py +import random + +class Food: + def __init__(self): + self.position = (0, 0) + + def generate(self): + x = random.randint(0, 9) + y = random.randint(0, 9) + self.position = (x, y) + + def get_position(self): + return self.position + +""" + +GAME_PY = """ +## game.py +import pygame +from snake import Snake +from food import Food + +class Game: + def __init__(self): + self.score = 0 + self.level = 1 + self.snake = Snake() + self.food = Food() + + def start_game(self): + pygame.init() + self.initialize_game() + self.game_loop() + + def initialize_game(self): + self.score = 0 + self.level = 1 + self.snake.reset() + self.food.generate() + + def game_loop(self): + game_over = False + + while not game_over: + self.update() + self.draw() + self.handle_events() + self.check_collision() + self.increase_score() + self.increase_level() + + if self.snake.is_collision(): + game_over = True + self.game_over() + + def update(self): + self.snake.move() + + def draw(self): + self.snake.draw() + self.food.draw() + + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + quit() + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + self.snake.change_direction("UP") + elif event.key == pygame.K_DOWN: + self.snake.change_direction("DOWN") + elif event.key == pygame.K_LEFT: + self.snake.change_direction("LEFT") + elif event.key == pygame.K_RIGHT: + self.snake.change_direction("RIGHT") + + def check_collision(self): + if self.snake.get_head() == self.food.get_position(): + self.snake.grow() + self.food.generate() + + def increase_score(self): + self.score += 1 + + def increase_level(self): + if self.score % 10 == 0: + self.level += 1 + + def game_over(self): + print("Game Over") + self.initialize_game() + +""" + +MAIN_PY = """ +## main.py +import pygame +from game import Game + +def main(): + pygame.init() + game = Game() + game.start_game() + +if __name__ == "__main__": + main() + +""" + +SNAKE_PY = """ +## snake.py +import pygame + +class Snake: + def __init__(self): + self.body = [(0, 0)] + self.direction = (1, 0) + + def move(self): + head = self.body[0] + dx, dy = self.direction + new_head = (head[0] + dx, head[1] + dy) + self.body.insert(0, new_head) + self.body.pop() + + def change_direction(self, direction): + if direction == "UP": + self.direction = (0, -1) + elif direction == "DOWN": + self.direction = (0, 1) + elif direction == "LEFT": + self.direction = (-1, 0) + elif direction == "RIGHT": + self.direction = (1, 0) + + def grow(self): + tail = self.body[-1] + dx, dy = self.direction + new_tail = (tail[0] - dx, tail[1] - dy) + self.body.append(new_tail) + + def get_head(self): + return self.body[0] + + def get_body(self): + return self.body[1:] + +""" + + +@pytest.mark.asyncio +async def test_summarize_code(): + CONFIG.src_workspace = CONFIG.git_repo.workdir / "src" + await FileRepository.save_file(filename="1.json", relative_path=SYSTEM_DESIGN_FILE_REPO, content=DESIGN_CONTENT) + await FileRepository.save_file(filename="1.json", relative_path=TASK_FILE_REPO, content=TASK_CONTENT) + await FileRepository.save_file(filename="food.py", relative_path=CONFIG.src_workspace, content=FOOD_PY) + await FileRepository.save_file(filename="game.py", relative_path=CONFIG.src_workspace, content=GAME_PY) + await FileRepository.save_file(filename="main.py", relative_path=CONFIG.src_workspace, content=MAIN_PY) + await FileRepository.save_file(filename="snake.py", relative_path=CONFIG.src_workspace, content=SNAKE_PY) + + src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) + all_files = src_file_repo.all_files + ctx = CodeSummarizeContext(design_filename="1.json", task_filename="1.json", codes_filenames=all_files) + action = SummarizeCode(context=ctx) + rsp = await action.run() + assert rsp + logger.info(rsp) diff --git a/tests/metagpt/actions/test_write_code.py b/tests/metagpt/actions/test_write_code.py index eb5e3de91..54229089c 100644 --- a/tests/metagpt/actions/test_write_code.py +++ b/tests/metagpt/actions/test_write_code.py @@ -4,26 +4,31 @@ @Time : 2023/5/11 17:45 @Author : alexanderwu @File : test_write_code.py +@Modifiled By: mashenquan, 2023-12-6. According to RFC 135 """ import pytest from metagpt.actions.write_code import WriteCode from metagpt.llm import LLM from metagpt.logs import logger +from metagpt.schema import CodingContext, Document from tests.metagpt.actions.mock import TASKS_2, WRITE_CODE_PROMPT_SAMPLE @pytest.mark.asyncio async def test_write_code(): - api_design = "设计一个名为'add'的函数,该函数接受两个整数作为输入,并返回它们的和。" - write_code = WriteCode("write_code") + context = CodingContext( + filename="task_filename.py", design_doc=Document(content="设计一个名为'add'的函数,该函数接受两个整数作为输入,并返回它们的和。") + ) + doc = Document(content=context.json()) + write_code = WriteCode(context=doc) - code = await write_code.run(api_design) - logger.info(code) + code = await write_code.run() + logger.info(code.json()) # 我们不能精确地预测生成的代码,但我们可以检查某些关键字 - assert "def add" in code - assert "return" in code + assert "def add" in code.code_doc.content + assert "return" in code.code_doc.content @pytest.mark.asyncio diff --git a/tests/metagpt/actions/test_write_code_review.py b/tests/metagpt/actions/test_write_code_review.py index 21bc563ec..e16eb7348 100644 --- a/tests/metagpt/actions/test_write_code_review.py +++ b/tests/metagpt/actions/test_write_code_review.py @@ -8,6 +8,8 @@ import pytest from metagpt.actions.write_code_review import WriteCodeReview +from metagpt.document import Document +from metagpt.schema import CodingContext @pytest.mark.asyncio @@ -16,13 +18,15 @@ async def test_write_code_review(capfd): def add(a, b): return a + """ - # write_code_review = WriteCodeReview("write_code_review") + context = CodingContext( + filename="math.py", design_doc=Document(content="编写一个从a加b的函数,返回a+b"), code_doc=Document(content=code) + ) - code = await WriteCodeReview().run(context="编写一个从a加b的函数,返回a+b", code=code, filename="math.py") + context = await WriteCodeReview(context=context).run() # 我们不能精确地预测生成的代码评审,但我们可以检查返回的是否为字符串 - assert isinstance(code, str) - assert len(code) > 0 + assert isinstance(context.code_doc.content, str) + assert len(context.code_doc.content) > 0 captured = capfd.readouterr() print(f"输出内容: {captured.out}") diff --git a/tests/metagpt/actions/test_write_prd.py b/tests/metagpt/actions/test_write_prd.py index 8f8ef84f5..08be3cf75 100644 --- a/tests/metagpt/actions/test_write_prd.py +++ b/tests/metagpt/actions/test_write_prd.py @@ -9,19 +9,24 @@ import pytest from metagpt.actions import UserRequirement +from metagpt.config import CONFIG +from metagpt.const import DOCS_FILE_REPO, PRDS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.logs import logger from metagpt.roles.product_manager import ProductManager from metagpt.schema import Message +from metagpt.utils.file_repository import FileRepository @pytest.mark.asyncio async def test_write_prd(): product_manager = ProductManager() requirements = "开发一个基于大语言模型与私有知识库的搜索引擎,希望可以基于大语言模型进行搜索总结" + await FileRepository.save_file(filename=REQUIREMENT_FILENAME, content=requirements, relative_path=DOCS_FILE_REPO) prd = await product_manager.run(Message(content=requirements, cause_by=UserRequirement)) logger.info(requirements) logger.info(prd) # Assert the prd is not None or empty assert prd is not None - assert prd != "" + assert prd.content != "" + assert CONFIG.git_repo.new_file_repository(relative_path=PRDS_FILE_REPO).changed_files diff --git a/tests/metagpt/actions/test_write_test.py b/tests/metagpt/actions/test_write_test.py index e5acdff44..a3190fb0e 100644 --- a/tests/metagpt/actions/test_write_test.py +++ b/tests/metagpt/actions/test_write_test.py @@ -9,6 +9,7 @@ import pytest from metagpt.actions.write_test import WriteTest from metagpt.logs import logger +from metagpt.schema import Document, TestingContext @pytest.mark.asyncio @@ -24,22 +25,17 @@ async def test_write_test(): def generate(self, max_y: int, max_x: int): self.position = (random.randint(1, max_y - 1), random.randint(1, max_x - 1)) """ + context = TestingContext(filename="food.py", code_doc=Document(filename="food.py", content=code)) + write_test = WriteTest(context=context) - write_test = WriteTest() - - test_code = await write_test.run( - code_to_test=code, - test_file_name="test_food.py", - source_file_path="/some/dummy/path/cli_snake_game/cli_snake_game/food.py", - workspace="/some/dummy/path/cli_snake_game", - ) - logger.info(test_code) + context = await write_test.run() + logger.info(context.json()) # We cannot exactly predict the generated test cases, but we can check if it is a string and if it is not empty - assert isinstance(test_code, str) - assert "from cli_snake_game.food import Food" in test_code - assert "class TestFood(unittest.TestCase)" in test_code - assert "def test_generate" in test_code + assert isinstance(context.test_doc.content, str) + assert "from food import Food" in context.test_doc.content + assert "class TestFood(unittest.TestCase)" in context.test_doc.content + assert "def test_generate" in context.test_doc.content @pytest.mark.asyncio diff --git a/tests/metagpt/roles/mock.py b/tests/metagpt/roles/mock.py index 5500b69f7..75f6b3b43 100644 --- a/tests/metagpt/roles/mock.py +++ b/tests/metagpt/roles/mock.py @@ -71,7 +71,7 @@ PRD = '''## 原始需求 ``` ''' -SYSTEM_DESIGN = """## project_name +SYSTEM_DESIGN = """## Project name ```python "smart_search_engine" ``` diff --git a/tests/metagpt/utils/test_file_repository.py b/tests/metagpt/utils/test_file_repository.py index a830b58aa..92e5204c5 100644 --- a/tests/metagpt/utils/test_file_repository.py +++ b/tests/metagpt/utils/test_file_repository.py @@ -43,6 +43,10 @@ async def test_file_repo(): assert {"a.txt"} == await file_repo.get_changed_dependency("b.txt") await file_repo.save("d/e.txt", "EEE") assert ["d/e.txt"] == file_repo.get_change_dir_files("d") + assert set(file_repo.all_files) == {"a.txt", "b.txt", "d/e.txt"} + await file_repo.delete("d/e.txt") + await file_repo.delete("d/e.txt") # delete twice + assert set(file_repo.all_files) == {"a.txt", "b.txt"} git_repo.delete_repository() From 70fcf354925aa68f9260e06c7f84dc81f4ce233c Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Fri, 8 Dec 2023 15:29:11 +0800 Subject: [PATCH 239/413] openai requirement bug fix --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f0169d7fa..14a9f485d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ langchain==0.0.231 loguru==0.6.0 meilisearch==0.21.0 numpy==1.24.3 -openai>=0.28.0 +openai==0.28.0 openpyxl beautifulsoup4==4.12.2 pandas==2.0.3 From 4a9b85f268b3a0d0a01bbc7b1fc997862fe7449a Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Fri, 8 Dec 2023 15:30:02 +0800 Subject: [PATCH 240/413] update version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 239156ae3..494a4614d 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.3.0", + version="0.4.0", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From ec8c703c5a7b699880e73cba365fb41967489285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Fri, 8 Dec 2023 19:55:47 +0800 Subject: [PATCH 241/413] feat: merge geekan:main --- examples/agent_creator.py | 1 + examples/search_kb.py | 22 ++++- metagpt/actions/action.py | 4 +- metagpt/actions/summarize_code.py | 4 +- metagpt/actions/write_code.py | 4 +- metagpt/actions/write_code_review.py | 4 +- metagpt/provider/openai_api.py | 6 +- metagpt/provider/zhipuai_api.py | 4 +- metagpt/roles/__init__.py | 2 +- metagpt/roles/sales.py | 2 +- metagpt/roles/{seacher.py => searcher.py} | 2 +- metagpt/subscription.py | 101 +++++++++++++++++++++ tests/conftest.py | 11 +++ tests/metagpt/test_subscription.py | 102 ++++++++++++++++++++++ 14 files changed, 251 insertions(+), 18 deletions(-) rename metagpt/roles/{seacher.py => searcher.py} (99%) create mode 100644 metagpt/subscription.py create mode 100644 tests/metagpt/test_subscription.py diff --git a/examples/agent_creator.py b/examples/agent_creator.py index e724105a3..05417d24a 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -49,6 +49,7 @@ class CreateAgent(Action): pattern = r"```python(.*)```" match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" + CONFIG.workspace_path.mkdir(parents=True, exist_ok=True) with open(CONFIG.workspace_path / "agent_created_agent.py", "w") as f: f.write(code_text) return code_text diff --git a/examples/search_kb.py b/examples/search_kb.py index 0b5d59385..7a9911ca2 100644 --- a/examples/search_kb.py +++ b/examples/search_kb.py @@ -5,17 +5,35 @@ """ import asyncio +from metagpt.actions import Action from metagpt.const import DATA_PATH from metagpt.document_store import FaissStore from metagpt.logs import logger from metagpt.roles import Sales +from metagpt.schema import Message + +""" example.json, e.g. +[ + { + "source": "Which facial cleanser is good for oily skin?", + "output": "ABC cleanser is preferred by many with oily skin." + }, + { + "source": "Is L'Oreal good to use?", + "output": "L'Oreal is a popular brand with many positive reviews." + } +] +""" async def search(): store = FaissStore(DATA_PATH / "example.json") role = Sales(profile="Sales", store=store) - - queries = ["Which facial cleanser is good for oily skin?", "Is L'Oreal good to use?"] + role._watch({Action}) + queries = [ + Message("Which facial cleanser is good for oily skin?", cause_by=Action), + Message("Is L'Oreal good to use?", cause_by=Action), + ] for query in queries: logger.info(f"User: {query}") result = await role.run(query) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index f8016b8a2..dc96699a9 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -9,7 +9,7 @@ import re from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM @@ -53,7 +53,7 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry(stop=stop_after_attempt(3), wait=wait_fixed(1)) + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def _aask_v1( self, prompt: str, diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index d10cd6c55..413ac2a21 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -7,7 +7,7 @@ """ from pathlib import Path -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG @@ -92,7 +92,7 @@ class SummarizeCode(Action): def __init__(self, name="SummarizeCode", context=None, llm=None): super().__init__(name, context, llm) - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + @retry(stop=stop_after_attempt(2), wait=wait_random_exponential(min=1, max=60)) async def summarize_code(self, prompt): code_rsp = await self._aask(prompt) return code_rsp diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 9b20843c7..4c138a124 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -15,7 +15,7 @@ RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG @@ -81,7 +81,7 @@ class WriteCode(Action): def __init__(self, name="WriteCode", context=None, llm=None): super().__init__(name, context, llm) - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt) -> str: code_rsp = await self._aask(prompt) code = CodeParser.parse_code(block="", text=code_rsp) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index f7c6845d2..f9cebffac 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -8,7 +8,7 @@ WriteCode object, rather than passing them in when calling the run function. """ -from tenacity import retry, stop_after_attempt, wait_fixed +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG @@ -94,7 +94,7 @@ class WriteCodeReview(Action): def __init__(self, name="WriteCodeReview", context=None, llm=None): super().__init__(name, context, llm) - @retry(stop=stop_after_attempt(2), wait=wait_fixed(1)) + @retry(stop=stop_after_attempt(2), wait=wait_random_exponential(min=1, max=60)) async def write_code_review_and_rewrite(self, prompt): code_rsp = await self._aask(prompt) result = CodeParser.parse_block("Code Review Result", code_rsp) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 8ac0c4b21..a73bb0aa0 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -15,7 +15,7 @@ from tenacity import ( retry, retry_if_exception_type, stop_after_attempt, - wait_fixed, + wait_random_exponential, ) from metagpt.config import CONFIG @@ -231,8 +231,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return await self._achat_completion(messages) @retry( - stop=stop_after_attempt(3), - wait=wait_fixed(1), + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), after=after_log(logger, logger.level("WARNING").name), retry=retry_if_exception_type(APIConnectionError), retry_error_callback=log_and_reraise, diff --git a/metagpt/provider/zhipuai_api.py b/metagpt/provider/zhipuai_api.py index edd9084e3..92119b764 100644 --- a/metagpt/provider/zhipuai_api.py +++ b/metagpt/provider/zhipuai_api.py @@ -13,7 +13,7 @@ from tenacity import ( retry, retry_if_exception_type, stop_after_attempt, - wait_fixed, + wait_random_exponential, ) from metagpt.config import CONFIG @@ -122,7 +122,7 @@ class ZhiPuAIGPTAPI(BaseGPTAPI): @retry( stop=stop_after_attempt(3), - wait=wait_fixed(1), + wait=wait_random_exponential(min=1, max=60), after=after_log(logger, logger.level("WARNING").name), retry=retry_if_exception_type(ConnectionError), retry_error_callback=log_and_reraise, diff --git a/metagpt/roles/__init__.py b/metagpt/roles/__init__.py index 1768b786c..f033a5dfa 100644 --- a/metagpt/roles/__init__.py +++ b/metagpt/roles/__init__.py @@ -12,7 +12,7 @@ from metagpt.roles.project_manager import ProjectManager from metagpt.roles.product_manager import ProductManager from metagpt.roles.engineer import Engineer from metagpt.roles.qa_engineer import QaEngineer -from metagpt.roles.seacher import Searcher +from metagpt.roles.searcher import Searcher from metagpt.roles.sales import Sales from metagpt.roles.customer_service import CustomerService diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index 18282a494..d5aac1824 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -28,7 +28,7 @@ class Sales(Role): def _set_store(self, store): if store: - action = SearchAndSummarize("", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.search) + action = SearchAndSummarize("", engine=SearchEngineType.CUSTOM_ENGINE, search_func=store.asearch) else: action = SearchAndSummarize() self._init_actions([action]) diff --git a/metagpt/roles/seacher.py b/metagpt/roles/searcher.py similarity index 99% rename from metagpt/roles/seacher.py rename to metagpt/roles/searcher.py index 587698d1d..bee8d3986 100644 --- a/metagpt/roles/seacher.py +++ b/metagpt/roles/searcher.py @@ -3,7 +3,7 @@ """ @Time : 2023/5/23 17:25 @Author : alexanderwu -@File : seacher.py +@File : searcher.py @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ diff --git a/metagpt/subscription.py b/metagpt/subscription.py new file mode 100644 index 000000000..0d2b30821 --- /dev/null +++ b/metagpt/subscription.py @@ -0,0 +1,101 @@ +import asyncio +from typing import AsyncGenerator, Awaitable, Callable + +from pydantic import BaseModel, Field + +from metagpt.logs import logger +from metagpt.roles import Role +from metagpt.schema import Message + + +class SubscriptionRunner(BaseModel): + """A simple wrapper to manage subscription tasks for different roles using asyncio. + + Example: + >>> import asyncio + >>> from metagpt.subscription import SubscriptionRunner + >>> from metagpt.roles import Searcher + >>> from metagpt.schema import Message + + >>> async def trigger(): + ... while True: + ... yield Message("the latest news about OpenAI") + ... await asyncio.sleep(3600 * 24) + + >>> async def callback(msg: Message): + ... print(msg.content) + + >>> async def main(): + ... pb = SubscriptionRunner() + ... await pb.subscribe(Searcher(), trigger(), callback) + ... await pb.run() + + >>> asyncio.run(main()) + """ + + tasks: dict[Role, asyncio.Task] = Field(default_factory=dict) + + class Config: + arbitrary_types_allowed = True + + async def subscribe( + self, + role: Role, + trigger: AsyncGenerator[Message, None], + callback: Callable[ + [ + Message, + ], + Awaitable[None], + ], + ): + """Subscribes a role to a trigger and sets up a callback to be called with the role's response. + + Args: + role: The role to subscribe. + trigger: An asynchronous generator that yields Messages to be processed by the role. + callback: An asynchronous function to be called with the response from the role. + """ + loop = asyncio.get_running_loop() + + async def _start_role(): + async for msg in trigger: + resp = await role.run(msg) + await callback(resp) + + self.tasks[role] = loop.create_task(_start_role(), name=f"Subscription-{role}") + + async def unsubscribe(self, role: Role): + """Unsubscribes a role from its trigger and cancels the associated task. + + Args: + role: The role to unsubscribe. + """ + task = self.tasks.pop(role) + task.cancel() + + async def run(self, raise_exception: bool = True): + """Runs all subscribed tasks and handles their completion or exception. + + Args: + raise_exception: _description_. Defaults to True. + + Raises: + task.exception: _description_ + """ + while True: + for role, task in self.tasks.items(): + if task.done(): + if task.exception(): + if raise_exception: + raise task.exception() + logger.opt(exception=task.exception()).error(f"Task {task.get_name()} run error") + else: + logger.warning( + f"Task {task.get_name()} has completed. " + "If this is unexpected behavior, please check the trigger function." + ) + self.tasks.pop(role) + break + else: + await asyncio.sleep(1) diff --git a/tests/conftest.py b/tests/conftest.py index 8e4422700..0cef6a4c9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -73,6 +73,17 @@ def proxy(): return "http://{}:{}".format(*server.sockets[0].getsockname()) +# see https://github.com/Delgan/loguru/issues/59#issuecomment-466591978 +@pytest.fixture +def loguru_caplog(caplog): + class PropogateHandler(logging.Handler): + def emit(self, record): + logging.getLogger(record.name).handle(record) + + logger.add(PropogateHandler(), format="{message}") + yield caplog + + # init & dispose git repo @pytest.fixture(scope="session", autouse=True) def setup_and_teardown_git_repo(request): diff --git a/tests/metagpt/test_subscription.py b/tests/metagpt/test_subscription.py new file mode 100644 index 000000000..2e898424d --- /dev/null +++ b/tests/metagpt/test_subscription.py @@ -0,0 +1,102 @@ +import asyncio + +import pytest + +from metagpt.roles import Role +from metagpt.schema import Message +from metagpt.subscription import SubscriptionRunner + + +@pytest.mark.asyncio +async def test_subscription_run(): + callback_done = 0 + + async def trigger(): + while True: + yield Message("the latest news about OpenAI") + await asyncio.sleep(3600 * 24) + + class MockRole(Role): + async def run(self, message=None): + return Message("") + + async def callback(message): + nonlocal callback_done + callback_done += 1 + + runner = SubscriptionRunner() + + roles = [] + for _ in range(2): + role = MockRole() + roles.append(role) + await runner.subscribe(role, trigger(), callback) + + task = asyncio.get_running_loop().create_task(runner.run()) + + for _ in range(10): + if callback_done == 2: + break + await asyncio.sleep(0) + else: + raise TimeoutError("callback not call") + + role = roles[0] + assert role in runner.tasks + await runner.unsubscribe(roles[0]) + + for _ in range(10): + if role not in runner.tasks: + break + await asyncio.sleep(0) + else: + raise TimeoutError("callback not call") + + task.cancel() + for i in runner.tasks.values(): + i.cancel() + + +@pytest.mark.asyncio +async def test_subscription_run_error(loguru_caplog): + async def trigger1(): + while True: + yield Message("the latest news about OpenAI") + await asyncio.sleep(3600 * 24) + + async def trigger2(): + yield Message("the latest news about OpenAI") + + class MockRole1(Role): + async def run(self, message=None): + raise RuntimeError + + class MockRole2(Role): + async def run(self, message=None): + return Message("") + + async def callback(msg: Message): + print(msg) + + runner = SubscriptionRunner() + await runner.subscribe(MockRole1(), trigger1(), callback) + with pytest.raises(RuntimeError): + await runner.run() + + await runner.subscribe(MockRole2(), trigger2(), callback) + task = asyncio.get_running_loop().create_task(runner.run(False)) + + for _ in range(10): + if not runner.tasks: + break + await asyncio.sleep(0) + else: + raise TimeoutError("wait runner tasks empty timeout") + + task.cancel() + for i in runner.tasks.values(): + i.cancel() + assert len(loguru_caplog.records) >= 2 + logs = "".join(loguru_caplog.messages) + assert "run error" in logs + assert "has completed" in logs From d196bd0cc947aaf47520bfc3157df064a95d8ab5 Mon Sep 17 00:00:00 2001 From: paulaan Date: Sun, 10 Dec 2023 00:15:39 +0700 Subject: [PATCH 242/413] selenium config better performance --- metagpt/tools/web_browser_engine_selenium.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/metagpt/tools/web_browser_engine_selenium.py b/metagpt/tools/web_browser_engine_selenium.py index d727709b8..80b60a93c 100644 --- a/metagpt/tools/web_browser_engine_selenium.py +++ b/metagpt/tools/web_browser_engine_selenium.py @@ -104,6 +104,9 @@ def _gen_get_driver_func(browser_type, *args, executable_path=None): def _get_driver(): options = Options() options.add_argument("--headless") + options.add_argument("--no-sandbox") # This flag is important for running in a Docker container + options.add_argument("--disable-gpu") # This flag can help avoid renderer issue + options.add_argument("--disable-dev-shm-usage") # Overcome limited resource problems options.add_argument("--enable-javascript") if browser_type == "chrome": options.add_argument("--no-sandbox") From c92793c27ceae28cdc0fba67c39648b5cb42cabd Mon Sep 17 00:00:00 2001 From: paulaan Date: Sat, 9 Dec 2023 12:57:54 +0700 Subject: [PATCH 243/413] researcher allow override system prompt --- metagpt/roles/researcher.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index c5512121a..f954c60bb 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -46,7 +46,7 @@ class Researcher(Role): else: topic = msg.content - research_system_text = get_research_system_text(topic, self.language) + research_system_text = self.research_system_text(topic) if isinstance(todo, CollectLinks): links = await todo.run(topic, 4, 4) ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=type(todo)) @@ -64,6 +64,17 @@ class Researcher(Role): self._rc.memory.add(ret) return ret + def research_system_text(self, topic) -> str: + """ BACKWARD compatible + This allows sub-class able to define its own system prompt based on topic. + return the previous implementation to have backward compatible + Args: + topic: + language: + + Returns: str + """ + return get_research_system_text(topic, self.language) async def react(self) -> Message: msg = await super().react() report = msg.instruct_content From 6b2fb95e665064a53c5098f28c4771cd5d69d70b Mon Sep 17 00:00:00 2001 From: paulaan Date: Sat, 9 Dec 2023 12:58:51 +0700 Subject: [PATCH 244/413] reformat for code convention --- metagpt/roles/researcher.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index f954c60bb..c60d54486 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -75,6 +75,7 @@ class Researcher(Role): Returns: str """ return get_research_system_text(topic, self.language) + async def react(self) -> Message: msg = await super().react() report = msg.instruct_content From 9d0f19aeee7a713530217e19eac414a9354d5355 Mon Sep 17 00:00:00 2001 From: paulaan Date: Sat, 9 Dec 2023 22:01:47 +0700 Subject: [PATCH 245/413] current task might swith different sys prompt --- metagpt/roles/researcher.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/roles/researcher.py b/metagpt/roles/researcher.py index c60d54486..387999cff 100644 --- a/metagpt/roles/researcher.py +++ b/metagpt/roles/researcher.py @@ -4,7 +4,7 @@ import asyncio from pydantic import BaseModel -from metagpt.actions import CollectLinks, ConductResearch, WebBrowseAndSummarize +from metagpt.actions import Action, CollectLinks, ConductResearch, WebBrowseAndSummarize from metagpt.actions.research import get_research_system_text from metagpt.const import RESEARCH_PATH from metagpt.logs import logger @@ -46,7 +46,7 @@ class Researcher(Role): else: topic = msg.content - research_system_text = self.research_system_text(topic) + research_system_text = self.research_system_text(topic, todo) if isinstance(todo, CollectLinks): links = await todo.run(topic, 4, 4) ret = Message("", Report(topic=topic, links=links), role=self.profile, cause_by=type(todo)) @@ -64,7 +64,7 @@ class Researcher(Role): self._rc.memory.add(ret) return ret - def research_system_text(self, topic) -> str: + def research_system_text(self, topic, current_task: Action) -> str: """ BACKWARD compatible This allows sub-class able to define its own system prompt based on topic. return the previous implementation to have backward compatible From 00f8b47d3946c63d9e2da0045404509f1f440692 Mon Sep 17 00:00:00 2001 From: paulaan Date: Sun, 10 Dec 2023 00:42:38 +0700 Subject: [PATCH 246/413] move to chrome --- metagpt/tools/web_browser_engine_selenium.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metagpt/tools/web_browser_engine_selenium.py b/metagpt/tools/web_browser_engine_selenium.py index 80b60a93c..074943892 100644 --- a/metagpt/tools/web_browser_engine_selenium.py +++ b/metagpt/tools/web_browser_engine_selenium.py @@ -104,11 +104,10 @@ def _gen_get_driver_func(browser_type, *args, executable_path=None): def _get_driver(): options = Options() options.add_argument("--headless") - options.add_argument("--no-sandbox") # This flag is important for running in a Docker container - options.add_argument("--disable-gpu") # This flag can help avoid renderer issue - options.add_argument("--disable-dev-shm-usage") # Overcome limited resource problems options.add_argument("--enable-javascript") if browser_type == "chrome": + options.add_argument("--disable-gpu") # This flag can help avoid renderer issue + options.add_argument("--disable-dev-shm-usage") # Overcome limited resource problems options.add_argument("--no-sandbox") for i in args: options.add_argument(i) From d6cc0165fcc0862130388301d7ba04b74f937257 Mon Sep 17 00:00:00 2001 From: mo Date: Mon, 11 Dec 2023 09:21:55 +0800 Subject: [PATCH 247/413] fix prompts --- metagpt/prompts/generate_skill.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/prompts/generate_skill.md b/metagpt/prompts/generate_skill.md index 74948cd15..e96f8181a 100644 --- a/metagpt/prompts/generate_skill.md +++ b/metagpt/prompts/generate_skill.md @@ -10,7 +10,7 @@ from typing import Optional from abc import ABC from metagpt.llm import LLM # Large language model, similar to GPT -n + class Action(ABC): def __init__(self, name='', context=None, llm: LLM = LLM()): self.name = name From 6f1ff01e0c58c02107558bf9466e6167db05bfae Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Mon, 11 Dec 2023 11:52:50 +0800 Subject: [PATCH 248/413] roadmap update as of v0.4.0 --- docs/ROADMAP.md | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 005a59ab2..afc9ff445 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -16,12 +16,12 @@ ### Tasks To reach version v0.5, approximately 70% of the following tasks need to be completed. 1. Usability - 1. Release v0.01 pip package to try to solve issues like npm installation (though not necessarily successfully) + 1. ~~Release v0.01 pip package to try to solve issues like npm installation (though not necessarily successfully)~~ (v0.3.0) 2. Support for overall save and recovery of software companies - 3. Support human confirmation and modification during the process + 3. ~~Support human confirmation and modification during the process~~ (v0.3.0) New: Support human confirmation and modification with fewer constrainsts and a more user-friendly interface 4. Support process caching: Consider carefully whether to add server caching mechanism - 5. Resolve occasional failure to follow instruction under current prompts, causing code parsing errors, through stricter system prompts - 6. Write documentation, describing the current features and usage at all levels + 5. ~~Resolve occasional failure to follow instruction under current prompts, causing code parsing errors, through stricter system prompts~~ (v0.4.0, with function call) + 6. Write documentation, describing the current features and usage at all levels (ongoing, continuously adding contents to [documentation site](https://docs.deepwisdom.ai/guide/get_started/introduction.html)) 7. ~~Support Docker~~ 2. Features 1. Support a more standard and stable parser (need to analyze the format that the current LLM is better at) @@ -30,31 +30,33 @@ ### Tasks 4. Complete the design and implementation of module breakdown 5. Support various modes of memory: clearly distinguish between long-term and short-term memory 6. Perfect the test role, and carry out necessary interactions with humans - 7. Provide full mode instead of the current fast mode, allowing natural communication between roles - 8. Implement SkillManager and the process of incremental Skill learning + 7. Allowing natural communication between roles (expected v0.5.0) + 8. Implement SkillManager and the process of incremental Skill learning (experimentation done with game agents) 9. Automatically get RPM and configure it by calling the corresponding openai page, so that each key does not need to be manually configured + 10. IMPORTANT: Support incremental development (expected v0.5.0) 3. Strategies - 1. Support ReAct strategy - 2. Support CoT strategy + 1. Support ReAct strategy (experimentation done with game agents) + 2. Support CoT strategy (experimentation done with game agents) 3. Support ToT strategy - 4. Support Reflection strategy + 4. Support Reflection strategy (experimentation done with game agents) + 5. Support planning 4. Actions - 1. Implementation: Search + 1. ~~Implementation: Search~~ (v0.2.1) 2. Implementation: Knowledge search, supporting 10+ data formats - 3. Implementation: Data EDA + 3. Implementation: Data EDA (expected v0.6.0) 4. Implementation: Review - 5. Implementation: Add Document - 6. Implementation: Delete Document + 5. Implementation: Add Document (expected v0.5.0) + 6. Implementation: Delete Document (expected v0.5.0) 7. Implementation: Self-training - 8. Implementation: DebugError + 8. ~~Implementation: DebugError~~ (v0.2.1) 9. Implementation: Generate reliable unit tests based on YAPI 10. Implementation: Self-evaluation 11. Implementation: AI Invocation 12. Implementation: Learning and using third-party standard libraries 13. Implementation: Data collection 14. Implementation: AI training - 15. Implementation: Run code - 16. Implementation: Web access + 15. ~~Implementation: Run code~~ (v0.2.1) + 16. ~~Implementation: Web access~~ (v0.2.1) 5. Plugins: Compatibility with plugin system 6. Tools 1. ~~Support SERPER api~~ @@ -64,13 +66,13 @@ ### Tasks 1. Perfect the action pool/skill pool for each role 2. Red Book blogger 3. E-commerce seller - 4. Data analyst + 4. Data analyst (expected v0.6.0) 5. News observer - 6. Institutional researcher + 6. ~~Institutional researcher~~ (v0.2.1) 8. Evaluation - 1. Support an evaluation on a game dataset - 2. Reproduce papers, implement full skill acquisition for a single game role, achieving SOTA results - 3. Support an evaluation on a math dataset + 1. Support an evaluation on a game dataset (experimentation done with game agents) + 2. Reproduce papers, implement full skill acquisition for a single game role, achieving SOTA results (experimentation done with game agents) + 3. Support an evaluation on a math dataset (expected v0.6.0) 4. Reproduce papers, achieving SOTA results for current mathematical problem solving process 9. LLM 1. Support Claude underlying API From b4eb8e4f34ed3efa11c4fd817cc49a7679a2b50b Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 11 Dec 2023 14:58:54 +0800 Subject: [PATCH 249/413] use metagpt cli instead. update all related docs --- README.md | 7 ++++--- docs/FAQ-EN.md | 2 +- docs/README_CN.md | 6 +++--- docs/README_JA.md | 24 ++++++++++++------------ docs/install/docker_install.md | 6 +++--- docs/install/docker_install_cn.md | 6 +++--- docs/tutorial/usage.md | 12 ++++++------ docs/tutorial/usage_cn.md | 10 +++++----- metagpt/actions/write_docstring.py | 2 +- tests/metagpt/test_startup.py | 1 + 10 files changed, 39 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index e80082a3a..2ce768212 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ + # MetaGPT: The Multi-Agent Framework

@@ -50,9 +51,9 @@ # Step 2: Clone the repository to your local machine for latest version, and ins cd MetaGPT pip3 install -e. # or pip3 install metagpt # for stable version -# Step 3: run the startup.py +# Step 3: run metagpt cli # setup your OPENAI_API_KEY in key.yaml copy from config.yaml -python3 startup.py "Write a cli snake game" +metagpt "Write a cli snake game" # Step 4 [Optional]: If you want to save the artifacts like diagrams such as quadrant chart, system designs, sequence flow in the workspace, you can execute the step before Step 3. By default, the framework is compatible, and the entire process can be run completely without executing this step. # If executing, ensure that NPM is installed on your system. Then install mermaid-js. (If you don't have npm in your computer, please go to the Node.js official website to install Node.js https://nodejs.org/ and then you will have npm tool in your computer.) @@ -78,7 +79,7 @@ # Step 2: Run metagpt demo with container -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ - python startup.py "Write a cli snake game" + metagpt "Write a cli snake game" ``` detail installation please refer to [docker_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-with-docker) diff --git a/docs/FAQ-EN.md b/docs/FAQ-EN.md index f9df50caf..b87e5da1e 100644 --- a/docs/FAQ-EN.md +++ b/docs/FAQ-EN.md @@ -98,7 +98,7 @@ 1. How to change the investment amount? - 1. You can view all commands by typing `python startup.py --help` + 1. You can view all commands by typing `metagpt --help` 1. Which version of Python is more stable? diff --git a/docs/README_CN.md b/docs/README_CN.md index 038925184..1e0edc533 100644 --- a/docs/README_CN.md +++ b/docs/README_CN.md @@ -47,9 +47,9 @@ # 第 2 步:克隆最新仓库到您的本地机器,并进行安装。 cd MetaGPT pip3 install -e. # 或者 pip3 install metagpt # 安装稳定版本 -# 第 3 步:执行startup.py +# 第 3 步:执行metagpt # 拷贝config.yaml为key.yaml,并设置你自己的OPENAI_API_KEY -python3 startup.py "Write a cli snake game" +metagpt "Write a cli snake game" # 第 4 步【可选的】:如果你想在执行过程中保存像象限图、系统设计、序列流程等图表这些产物,可以在第3步前执行该步骤。默认的,框架做了兼容,在不执行该步的情况下,也可以完整跑完整个流程。 # 如果执行,确保您的系统上安装了 NPM。并使用npm安装mermaid-js @@ -75,7 +75,7 @@ # 步骤2: 使用容器运行metagpt演示 -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ - python startup.py "Write a cli snake game" + metagpt "Write a cli snake game" ``` 详细的安装请安装 [docker_install](https://docs.deepwisdom.ai/zhcn/guide/get_started/installation.html#%E4%BD%BF%E7%94%A8docker%E5%AE%89%E8%A3%85) diff --git a/docs/README_JA.md b/docs/README_JA.md index 411d190b4..210044ec2 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -41,7 +41,7 @@ ## MetaGPT の能力 ## 例(GPT-4 で完全生成) -例えば、`python startup.py "Toutiao のような RecSys をデザインする"`と入力すると、多くの出力が得られます +例えば、`metagpt "Toutiao のような RecSys をデザインする"`と入力すると、多くの出力が得られます ![Jinri Toutiao Recsys データと API デザイン](resources/workspace/content_rec_sys/resources/data_api_design.png) @@ -67,9 +67,9 @@ # ステップ 2: リポジトリをローカルマシンにクローンし、 cd MetaGPT pip install -e. -# ステップ 3: startup.py を実行する +# ステップ 3: metagpt を実行する # config.yaml を key.yaml にコピーし、独自の OPENAI_API_KEY を設定します -python3 startup.py "Write a cli snake game" +metagpt "Write a cli snake game" # ステップ 4 [オプション]: 実行中に PRD ファイルなどのアーティファクトを保存する場合は、ステップ 3 の前にこのステップを実行できます。デフォルトでは、フレームワークには互換性があり、この手順を実行しなくてもプロセス全体を完了できます。 # NPM がシステムにインストールされていることを確認してください。次に mermaid-js をインストールします。(お使いのコンピューターに npm がない場合は、Node.js 公式サイトで Node.js https://nodejs.org/ をインストールしてください。) @@ -178,7 +178,7 @@ # ステップ 2: コンテナで metagpt デモを実行する -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ - python startup.py "Write a cli snake game" + metagpt "Write a cli snake game" # コンテナを起動し、その中でコマンドを実行することもできます docker run --name metagpt -d \ @@ -188,7 +188,7 @@ # コンテナを起動し、その中でコマンドを実行することもで metagpt/metagpt:latest docker exec -it metagpt /bin/bash -$ python startup.py "Write a cli snake game" +$ metagpt "Write a cli snake game" ``` コマンド `docker run ...` は以下のことを行います: @@ -196,7 +196,7 @@ # コンテナを起動し、その中でコマンドを実行することもで - 特権モードで実行し、ブラウザの実行権限を得る - ホスト設定ファイル `/opt/metagpt/config/key.yaml` をコンテナ `/app/metagpt/config/key.yaml` にマップします - ホストディレクトリ `/opt/metagpt/workspace` をコンテナディレクトリ `/app/metagpt/workspace` にマップするs -- デモコマンド `python startup.py "Write a cli snake game"` を実行する +- デモコマンド `metagpt "Write a cli snake game"` を実行する ### 自分でイメージをビルドする @@ -225,11 +225,11 @@ ## チュートリアル: スタートアップの開始 ```shell # スクリプトの実行 -python startup.py "Write a cli snake game" +metagpt "Write a cli snake game" # プロジェクトの実施にエンジニアを雇わないこと -python startup.py "Write a cli snake game" --implement False +metagpt "Write a cli snake game" --implement False # エンジニアを雇い、コードレビューを行う -python startup.py "Write a cli snake game" --code_review True +metagpt "Write a cli snake game" --code_review True ``` スクリプトを実行すると、`workspace/` ディレクトリに新しいプロジェクトが見つかります。 @@ -239,17 +239,17 @@ ### プラットフォームまたはツールの設定 要件を述べるときに、どのプラットフォームまたはツールを使用するかを指定できます。 ```shell -python startup.py "pygame をベースとした cli ヘビゲームを書く" +metagpt "pygame をベースとした cli ヘビゲームを書く" ``` ### 使用方法 ``` 会社名 - startup.py - 私たちは AI で構成されたソフトウェア・スタートアップです。私たちに投資することは、無限の可能性に満ちた未来に力を与えることです。 + metagpt - 私たちは AI で構成されたソフトウェア・スタートアップです。私たちに投資することは、無限の可能性に満ちた未来に力を与えることです。 シノプシス - startup.py IDEA + metagpt IDEA 説明 私たちは AI で構成されたソフトウェア・スタートアップです。私たちに投資することは、無限の可能性に満ちた未来に力を与えることです。 diff --git a/docs/install/docker_install.md b/docs/install/docker_install.md index b803a5dae..37125bdbe 100644 --- a/docs/install/docker_install.md +++ b/docs/install/docker_install.md @@ -15,7 +15,7 @@ # Step 2: Run metagpt demo with container -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ - python3 startup.py "Write a cli snake game" + metagpt "Write a cli snake game" # You can also start a container and execute commands in it docker run --name metagpt -d \ @@ -25,7 +25,7 @@ # You can also start a container and execute commands in it metagpt/metagpt:latest docker exec -it metagpt /bin/bash -$ python3 startup.py "Write a cli snake game" +$ metagpt "Write a cli snake game" ``` The command `docker run ...` do the following things: @@ -33,7 +33,7 @@ # You can also start a container and execute commands in it - Run in privileged mode to have permission to run the browser - Map host configure file `/opt/metagpt/config/key.yaml` to container `/app/metagpt/config/key.yaml` - Map host directory `/opt/metagpt/workspace` to container `/app/metagpt/workspace` -- Execute the demo command `python3 startup.py "Write a cli snake game"` +- Execute the demo command `metagpt "Write a cli snake game"` ### Build image by yourself diff --git a/docs/install/docker_install_cn.md b/docs/install/docker_install_cn.md index 347fae10c..f360b49ed 100644 --- a/docs/install/docker_install_cn.md +++ b/docs/install/docker_install_cn.md @@ -15,7 +15,7 @@ # 步骤2: 使用容器运行metagpt演示 -v /opt/metagpt/config/key.yaml:/app/metagpt/config/key.yaml \ -v /opt/metagpt/workspace:/app/metagpt/workspace \ metagpt/metagpt:latest \ - python startup.py "Write a cli snake game" + metagpt "Write a cli snake game" # 您也可以启动一个容器并在其中执行命令 docker run --name metagpt -d \ @@ -25,7 +25,7 @@ # 您也可以启动一个容器并在其中执行命令 metagpt/metagpt:latest docker exec -it metagpt /bin/bash -$ python startup.py "Write a cli snake game" +$ metagpt "Write a cli snake game" ``` `docker run ...`做了以下事情: @@ -33,7 +33,7 @@ # 您也可以启动一个容器并在其中执行命令 - 以特权模式运行,有权限运行浏览器 - 将主机文件 `/opt/metagpt/config/key.yaml` 映射到容器文件 `/app/metagpt/config/key.yaml` - 将主机目录 `/opt/metagpt/workspace` 映射到容器目录 `/app/metagpt/workspace` -- 执行示例命令 `python startup.py "Write a cli snake game"` +- 执行示例命令 `metagpt "Write a cli snake game"` ### 自己构建镜像 diff --git a/docs/tutorial/usage.md b/docs/tutorial/usage.md index ee87b65c9..f3eb931f6 100644 --- a/docs/tutorial/usage.md +++ b/docs/tutorial/usage.md @@ -19,11 +19,11 @@ ### Initiating a startup ```shell # Run the script -python startup.py "Write a cli snake game" +metagpt "Write a cli snake game" # Do not hire an engineer to implement the project -python startup.py "Write a cli snake game" --implement False +metagpt "Write a cli snake game" --implement False # Hire an engineer and perform code reviews -python startup.py "Write a cli snake game" --code_review True +metagpt "Write a cli snake game" --code_review True ``` After running the script, you can find your new project in the `workspace/` directory. @@ -33,17 +33,17 @@ ### Preference of Platform or Tool You can tell which platform or tool you want to use when stating your requirements. ```shell -python startup.py "Write a cli snake game based on pygame" +metagpt "Write a cli snake game based on pygame" ``` ### Usage ``` NAME - startup.py - We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities. + metagpt - We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities. SYNOPSIS - startup.py IDEA + metagpt IDEA DESCRIPTION We are a software startup comprised of AI. By investing in us, you are empowering a future filled with limitless possibilities. diff --git a/docs/tutorial/usage_cn.md b/docs/tutorial/usage_cn.md index 4b3bdd2c3..18966acdc 100644 --- a/docs/tutorial/usage_cn.md +++ b/docs/tutorial/usage_cn.md @@ -18,9 +18,9 @@ # 复制配置文件并进行必要的修改 ### 示例:启动一个创业公司 ```shell -python startup.py "写一个命令行贪吃蛇" +metagpt "写一个命令行贪吃蛇" # 开启code review模式会花费更多的金钱, 但是会提升代码质量和成功率 -python startup.py "写一个命令行贪吃蛇" --code_review True +metagpt "写一个命令行贪吃蛇" --code_review True ``` 运行脚本后,您可以在 `workspace/` 目录中找到您的新项目。 @@ -29,17 +29,17 @@ ### 平台或工具的倾向性 可以在阐述需求时说明想要使用的平台或工具。 例如: ```shell -python startup.py "写一个基于pygame的命令行贪吃蛇" +metagpt "写一个基于pygame的命令行贪吃蛇" ``` ### 使用 ``` 名称 - startup.py - 我们是一家AI软件创业公司。通过投资我们,您将赋能一个充满无限可能的未来。 + metagpt - 我们是一家AI软件创业公司。通过投资我们,您将赋能一个充满无限可能的未来。 概要 - startup.py IDEA + metagpt IDEA 描述 我们是一家AI软件创业公司。通过投资我们,您将赋能一个充满无限可能的未来。 diff --git a/metagpt/actions/write_docstring.py b/metagpt/actions/write_docstring.py index dd3312bd5..0ad134157 100644 --- a/metagpt/actions/write_docstring.py +++ b/metagpt/actions/write_docstring.py @@ -16,7 +16,7 @@ Options: Default: 'google' Example: - python3 -m metagpt.actions.write_docstring startup.py --overwrite False --style=numpy + python3 -m metagpt.actions.write_docstring ./metagpt/startup.py --overwrite False --style=numpy This script uses the 'fire' library to create a command-line interface. It generates docstrings for the given Python code using the specified docstring style and adds them to the code. diff --git a/tests/metagpt/test_startup.py b/tests/metagpt/test_startup.py index 53d3509ed..c34fd2c31 100644 --- a/tests/metagpt/test_startup.py +++ b/tests/metagpt/test_startup.py @@ -16,6 +16,7 @@ runner = CliRunner() @pytest.mark.asyncio async def test_team(): + # FIXME: we're now using "metagpt" cli, so the entrance should be replaced instead. company = Team() company.run_project("做一个基础搜索引擎,可以支持知识库") history = await company.run(n_round=5) From 9a361593ea1d94081e244e73f8c11ebc24b3931a Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 11 Dec 2023 15:17:27 +0800 Subject: [PATCH 250/413] use metagpt cli instead. update all related docs --- docs/README_JA.md | 4 ++-- docs/tutorial/usage.md | 4 ++-- docs/tutorial/usage_cn.md | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/README_JA.md b/docs/README_JA.md index 210044ec2..63894647e 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -227,9 +227,9 @@ ## チュートリアル: スタートアップの開始 # スクリプトの実行 metagpt "Write a cli snake game" # プロジェクトの実施にエンジニアを雇わないこと -metagpt "Write a cli snake game" --implement False +metagpt "Write a cli snake game" --no-implement # エンジニアを雇い、コードレビューを行う -metagpt "Write a cli snake game" --code_review True +metagpt "Write a cli snake game" --code_review ``` スクリプトを実行すると、`workspace/` ディレクトリに新しいプロジェクトが見つかります。 diff --git a/docs/tutorial/usage.md b/docs/tutorial/usage.md index f3eb931f6..fbe4a8311 100644 --- a/docs/tutorial/usage.md +++ b/docs/tutorial/usage.md @@ -21,9 +21,9 @@ ### Initiating a startup # Run the script metagpt "Write a cli snake game" # Do not hire an engineer to implement the project -metagpt "Write a cli snake game" --implement False +metagpt "Write a cli snake game" --no-implement # Hire an engineer and perform code reviews -metagpt "Write a cli snake game" --code_review True +metagpt "Write a cli snake game" --code_review ``` After running the script, you can find your new project in the `workspace/` directory. diff --git a/docs/tutorial/usage_cn.md b/docs/tutorial/usage_cn.md index 18966acdc..1ef50d633 100644 --- a/docs/tutorial/usage_cn.md +++ b/docs/tutorial/usage_cn.md @@ -20,7 +20,7 @@ ### 示例:启动一个创业公司 ```shell metagpt "写一个命令行贪吃蛇" # 开启code review模式会花费更多的金钱, 但是会提升代码质量和成功率 -metagpt "写一个命令行贪吃蛇" --code_review True +metagpt "写一个命令行贪吃蛇" --code_review ``` 运行脚本后,您可以在 `workspace/` 目录中找到您的新项目。 From 292344cf40959bb6ddadfe2ae7862c48811dd838 Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 11 Dec 2023 15:23:55 +0800 Subject: [PATCH 251/413] change all mail address from fuzhi.ai to deepwisdom.ai --- README.md | 2 +- docs/README_CN.md | 2 +- docs/README_JA.md | 2 +- metagpt/tools/sd_engine.py | 2 +- setup.py | 2 +- tests/metagpt/actions/test_ui_design.py | 2 +- tests/metagpt/roles/test_ui.py | 2 +- tests/metagpt/roles/ui_role.py | 2 +- tests/metagpt/tools/test_sd_tool.py | 2 +- tests/metagpt/tools/test_web_browser_engine.py | 4 ++-- tests/metagpt/tools/test_web_browser_engine_playwright.py | 6 +++--- tests/metagpt/tools/test_web_browser_engine_selenium.py | 6 +++--- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2ce768212..b3473a12c 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ ### Contact Information If you have any questions or feedback about this project, please feel free to contact us. We highly appreciate your suggestions! -- **Email:** alexanderwu@fuzhi.ai +- **Email:** alexanderwu@deepwisdom.ai - **GitHub Issues:** For more technical inquiries, you can also create a new issue in our [GitHub repository](https://github.com/geekan/metagpt/issues). We will respond to all questions within 2-3 business days. diff --git a/docs/README_CN.md b/docs/README_CN.md index 1e0edc533..dd65c2a25 100644 --- a/docs/README_CN.md +++ b/docs/README_CN.md @@ -114,7 +114,7 @@ ### 联系信息 如果您对这个项目有任何问题或反馈,欢迎联系我们。我们非常欢迎您的建议! -- **邮箱:** alexanderwu@fuzhi.ai +- **邮箱:** alexanderwu@deepwisdom.ai - **GitHub 问题:** 对于更技术性的问题,您也可以在我们的 [GitHub 仓库](https://github.com/geekan/metagpt/issues) 中创建一个新的问题。 我们会在2-3个工作日内回复所有问题。 diff --git a/docs/README_JA.md b/docs/README_JA.md index 63894647e..482b42fa7 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -317,7 +317,7 @@ ## お問い合わせ先 このプロジェクトに関するご質問やご意見がございましたら、お気軽にお問い合わせください。皆様のご意見をお待ちしております! -- **Email:** alexanderwu@fuzhi.ai +- **Email:** alexanderwu@deepwisdom.ai - **GitHub Issues:** 技術的なお問い合わせについては、[GitHub リポジトリ](https://github.com/geekan/metagpt/issues) に新しい issue を作成することもできます。 ご質問には 2-3 営業日以内に回答いたします。 diff --git a/metagpt/tools/sd_engine.py b/metagpt/tools/sd_engine.py index c6676a247..a84812f7c 100644 --- a/metagpt/tools/sd_engine.py +++ b/metagpt/tools/sd_engine.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # @Date : 2023/7/19 16:28 -# @Author : stellahong (stellahong@fuzhi.ai) +# @Author : stellahong (stellahong@deepwisdom.ai) # @Desc : import asyncio import base64 diff --git a/setup.py b/setup.py index 6d3708c32..84e91ede8 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ setup( long_description_content_type="text/markdown", url="https://github.com/geekan/MetaGPT", author="Alexander Wu", - author_email="alexanderwu@fuzhi.ai", + author_email="alexanderwu@deepwisdom.ai", license="MIT", keywords="metagpt multi-role multi-agent programming gpt llm metaprogramming", packages=find_packages(exclude=["contrib", "docs", "examples", "tests*"]), diff --git a/tests/metagpt/actions/test_ui_design.py b/tests/metagpt/actions/test_ui_design.py index b8be914ae..83590ec7d 100644 --- a/tests/metagpt/actions/test_ui_design.py +++ b/tests/metagpt/actions/test_ui_design.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # @Date : 2023/7/22 02:40 -# @Author : stellahong (stellahong@fuzhi.ai) +# @Author : stellahong (stellahong@deepwisdom.ai) # from tests.metagpt.roles.ui_role import UIDesign diff --git a/tests/metagpt/roles/test_ui.py b/tests/metagpt/roles/test_ui.py index 5904bee8f..2038a1aee 100644 --- a/tests/metagpt/roles/test_ui.py +++ b/tests/metagpt/roles/test_ui.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # @Date : 2023/7/22 02:40 -# @Author : stellahong (stellahong@fuzhi.ai) +# @Author : stellahong (stellahong@deepwisdom.ai) # from metagpt.roles import ProductManager from metagpt.team import Team diff --git a/tests/metagpt/roles/ui_role.py b/tests/metagpt/roles/ui_role.py index ee36befbd..8ac799bf3 100644 --- a/tests/metagpt/roles/ui_role.py +++ b/tests/metagpt/roles/ui_role.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # @Date : 2023/7/15 16:40 -# @Author : stellahong (stellahong@fuzhi.ai) +# @Author : stellahong (stellahong@deepwisdom.ai) # @Desc : import os import re diff --git a/tests/metagpt/tools/test_sd_tool.py b/tests/metagpt/tools/test_sd_tool.py index edb23df42..e457101a9 100644 --- a/tests/metagpt/tools/test_sd_tool.py +++ b/tests/metagpt/tools/test_sd_tool.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # @Date : 2023/7/22 02:40 -# @Author : stellahong (stellahong@fuzhi.ai) +# @Author : stellahong (stellahong@deepwisdom.ai) # import os diff --git a/tests/metagpt/tools/test_web_browser_engine.py b/tests/metagpt/tools/test_web_browser_engine.py index b08d0ca10..28dd0e15c 100644 --- a/tests/metagpt/tools/test_web_browser_engine.py +++ b/tests/metagpt/tools/test_web_browser_engine.py @@ -7,8 +7,8 @@ from metagpt.tools import WebBrowserEngineType, web_browser_engine @pytest.mark.parametrize( "browser_type, url, urls", [ - (WebBrowserEngineType.PLAYWRIGHT, "https://fuzhi.ai", ("https://fuzhi.ai",)), - (WebBrowserEngineType.SELENIUM, "https://fuzhi.ai", ("https://fuzhi.ai",)), + (WebBrowserEngineType.PLAYWRIGHT, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), + (WebBrowserEngineType.SELENIUM, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), ], ids=["playwright", "selenium"], ) diff --git a/tests/metagpt/tools/test_web_browser_engine_playwright.py b/tests/metagpt/tools/test_web_browser_engine_playwright.py index 69e1339e7..e9ea80b10 100644 --- a/tests/metagpt/tools/test_web_browser_engine_playwright.py +++ b/tests/metagpt/tools/test_web_browser_engine_playwright.py @@ -8,9 +8,9 @@ from metagpt.tools import web_browser_engine_playwright @pytest.mark.parametrize( "browser_type, use_proxy, kwagrs, url, urls", [ - ("chromium", {"proxy": True}, {}, "https://fuzhi.ai", ("https://fuzhi.ai",)), - ("firefox", {}, {"ignore_https_errors": True}, "https://fuzhi.ai", ("https://fuzhi.ai",)), - ("webkit", {}, {"ignore_https_errors": True}, "https://fuzhi.ai", ("https://fuzhi.ai",)), + ("chromium", {"proxy": True}, {}, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), + ("firefox", {}, {"ignore_https_errors": True}, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), + ("webkit", {}, {"ignore_https_errors": True}, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), ], ids=["chromium-normal", "firefox-normal", "webkit-normal"], ) diff --git a/tests/metagpt/tools/test_web_browser_engine_selenium.py b/tests/metagpt/tools/test_web_browser_engine_selenium.py index ce322f7bd..ac6eafee7 100644 --- a/tests/metagpt/tools/test_web_browser_engine_selenium.py +++ b/tests/metagpt/tools/test_web_browser_engine_selenium.py @@ -8,9 +8,9 @@ from metagpt.tools import web_browser_engine_selenium @pytest.mark.parametrize( "browser_type, use_proxy, url, urls", [ - ("chrome", True, "https://fuzhi.ai", ("https://fuzhi.ai",)), - ("firefox", False, "https://fuzhi.ai", ("https://fuzhi.ai",)), - ("edge", False, "https://fuzhi.ai", ("https://fuzhi.ai",)), + ("chrome", True, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), + ("firefox", False, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), + ("edge", False, "https://deepwisdom.ai", ("https://deepwisdom.ai",)), ], ids=["chrome-normal", "firefox-normal", "edge-normal"], ) From 687e17367c9bcad10b54fd9af8afbcc74ef42433 Mon Sep 17 00:00:00 2001 From: geekan Date: Mon, 11 Dec 2023 16:07:53 +0800 Subject: [PATCH 252/413] use python3 instead of python --- docs/FAQ-EN.md | 2 +- docs/README_JA.md | 2 +- docs/install/cli_install_cn.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/FAQ-EN.md b/docs/FAQ-EN.md index b87e5da1e..af6868509 100644 --- a/docs/FAQ-EN.md +++ b/docs/FAQ-EN.md @@ -134,7 +134,7 @@ 1. Configuration instructions for SD Skills: The SD interface is currently deployed based on *https://github.com/AUTOMATIC1111/stable-diffusion-webui* **For environmental configurations and model downloads, please refer to the aforementioned GitHub repository. To initiate the SD service that supports API calls, run the command specified in cmd with the parameter nowebui, i.e., - 1. > python webui.py --enable-insecure-extension-access --port xxx --no-gradio-queue --nowebui + 1. > python3 webui.py --enable-insecure-extension-access --port xxx --no-gradio-queue --nowebui 1.     Once it runs without errors, the interface will be accessible after approximately 1 minute when the model finishes loading. 1. Configure SD_URL and SD_T2I_API in the config.yaml/key.yaml files. 1. ![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/065295a67b0b4feea665d1372722d49d~tplv-k3u1fbpfcp-zoom-1.image) diff --git a/docs/README_JA.md b/docs/README_JA.md index 482b42fa7..05f718635 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -60,7 +60,7 @@ ### 伝統的なインストール ```bash # ステップ 1: Python 3.9+ がシステムにインストールされていることを確認してください。これを確認するには: -python --version +python3 --version # ステップ 2: リポジトリをローカルマシンにクローンし、インストールする。 git clone https://github.com/geekan/MetaGPT.git diff --git a/docs/install/cli_install_cn.md b/docs/install/cli_install_cn.md index f351090ed..b1da1b813 100644 --- a/docs/install/cli_install_cn.md +++ b/docs/install/cli_install_cn.md @@ -15,7 +15,7 @@ # 第 1 步:确保您的系统上安装了 NPM。并使用npm安装mermaid-js sudo npm install -g @mermaid-js/mermaid-cli # 第 2 步:确保您的系统上安装了 Python 3.9+。您可以使用以下命令进行检查: -python --version +python3 --version # 第 3 步:克隆仓库到您的本地机器,并进行安装。 git clone https://github.com/geekan/MetaGPT.git From 9d922941cf468e89a3202ce230e20f878e22f072 Mon Sep 17 00:00:00 2001 From: Yi Lin Date: Mon, 11 Dec 2023 22:42:13 +0800 Subject: [PATCH 253/413] add gpt-4-turbo and gpt-3-turbo-1106 in token count Signed-off-by: Yi Lin --- metagpt/utils/token_counter.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index 1af96f272..ba63e90a9 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -16,11 +16,13 @@ TOKEN_COSTS = { "gpt-3.5-turbo-0613": {"prompt": 0.0015, "completion": 0.002}, "gpt-3.5-turbo-16k": {"prompt": 0.003, "completion": 0.004}, "gpt-3.5-turbo-16k-0613": {"prompt": 0.003, "completion": 0.004}, + "gpt-3.5-turbo-1106": {"prompt": 0.001, "completion": 0.002}, "gpt-4-0314": {"prompt": 0.03, "completion": 0.06}, "gpt-4": {"prompt": 0.03, "completion": 0.06}, "gpt-4-32k": {"prompt": 0.06, "completion": 0.12}, "gpt-4-32k-0314": {"prompt": 0.06, "completion": 0.12}, "gpt-4-0613": {"prompt": 0.06, "completion": 0.12}, + "gpt-4-1106-preview": {"prompt": 0.01, "completion": 0.03}, "text-embedding-ada-002": {"prompt": 0.0004, "completion": 0.0}, "chatglm_turbo": {"prompt": 0.0, "completion": 0.00069} # 32k version, prompt + completion tokens=0.005¥/k-tokens } @@ -32,11 +34,13 @@ TOKEN_MAX = { "gpt-3.5-turbo-0613": 4096, "gpt-3.5-turbo-16k": 16384, "gpt-3.5-turbo-16k-0613": 16384, + "gpt-3.5-turbo-1106": 16384, "gpt-4-0314": 8192, "gpt-4": 8192, "gpt-4-32k": 32768, "gpt-4-32k-0314": 32768, "gpt-4-0613": 8192, + "gpt-4-1106-preview": 128000, "text-embedding-ada-002": 8192, "chatglm_turbo": 32768 } @@ -52,10 +56,12 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): if model in { "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", + "gpt-3.5-turbo-1106", "gpt-4-0314", "gpt-4-32k-0314", "gpt-4-0613", "gpt-4-32k-0613", + "gpt-4-1106-preview", }: tokens_per_message = 3 tokens_per_name = 1 From 12208154ee0fc5f913fe104ec722cae074c95d42 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 12 Dec 2023 15:06:20 +0800 Subject: [PATCH 254/413] simplify code --- metagpt/utils/repair_llm_raw_output.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 124bcba89..0a461d360 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -222,10 +222,10 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R } """ if retry_state.outcome.failed: - if len(retry_state.args) > 0: + if retry_state.args: # # can't be used as args=retry_state.args func_param_output = retry_state.args[0] - elif len(retry_state.kwargs) > 0: + elif retry_state.kwargs: func_param_output = retry_state.kwargs.get("output", "") exp_str = str(retry_state.outcome.exception()) logger.warning(f"parse json from content inside [CONTENT][/CONTENT] failed at retry " From 697f790c837f248321d0c7705da6c5ba7d840897 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 12 Dec 2023 16:42:08 +0800 Subject: [PATCH 255/413] bugfix: write code add related code file context --- metagpt/actions/write_code.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 4c138a124..b20539e78 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -14,12 +14,13 @@ 3. Encapsulate the input of RunCode into RunCodeContext and encapsulate the output of RunCode into RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ +import json from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO +from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO, TASK_FILE_REPO from metagpt.logs import logger from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser @@ -101,10 +102,11 @@ class WriteCode(Action): if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr + code_context = await self._get_codes(coding_context.task_doc) prompt = PROMPT_TEMPLATE.format( design=coding_context.design_doc.content, tasks=coding_context.task_doc.content if coding_context.task_doc else "", - code=coding_context.code_doc.content if coding_context.code_doc else "", + code=code_context, logs=logs, filename=self.context.filename, summary_log=summary_doc.content if summary_doc else "", @@ -115,3 +117,21 @@ class WriteCode(Action): coding_context.code_doc = Document(filename=coding_context.filename, root_path=CONFIG.src_workspace) coding_context.code_doc.content = code return coding_context + + @staticmethod + async def _get_codes(task_doc) -> str: + if not task_doc: + return "" + if not task_doc.content: + task_doc.content = FileRepository.get_file(filename=task_doc.filename, relative_path=TASK_FILE_REPO) + m = json.loads(task_doc.content) + code_filenames = m.get("Task list", []) + codes = [] + src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) + for filename in code_filenames: + doc = await src_file_repo.get(filename=filename) + if not doc: + continue + codes.append(doc.content) + return "\n----------\n".join(codes) + From 97cd9cd98d1a53481307c6a7014b675e1c0321af Mon Sep 17 00:00:00 2001 From: 0aaryan Date: Tue, 12 Dec 2023 15:20:29 +0530 Subject: [PATCH 256/413] Fix: Spelling errors in words (quoto -> quote) #521 --- metagpt/actions/debug_error.py | 2 +- metagpt/actions/design_api.py | 4 ++-- metagpt/actions/write_code.py | 2 +- metagpt/actions/write_prd.py | 4 ++-- metagpt/actions/write_test.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index d69a22dba..101cc2025 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -22,7 +22,7 @@ The message is as follows: {context} --- Now you should start rewriting the code: -## file name of the code to rewrite: Write code with triple quoto. Do your best to implement THIS IN ONLY ONE FILE. +## file name of the code to rewrite: Write code with triple quote. Do your best to implement THIS IN ONLY ONE FILE. """ class DebugError(Action): def __init__(self, name="DebugError", context=None, llm=None): diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 75df8b909..fc3be602b 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -33,7 +33,7 @@ Max Output: 8192 chars or 2048 tokens. Try to use them up. ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores +## Python package name: Provide as Python str with python triple quote, concise and clear, characters only use a combination of all lowercase and underscores ## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here @@ -86,7 +86,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. -## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores +## Python package name: Provide as Python str with python triple quote, concise and clear, characters only use a combination of all lowercase and underscores ## File list: Provided as Python list[str], the list of ONLY REQUIRED files needed to write the program(LESS IS MORE!). Only need relative paths, comply with PEP8 standards. ALWAYS write a main.py or app.py here diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index a5dc8e059..a89bce60f 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -18,7 +18,7 @@ NOTICE Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language) ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". -## Code: {filename} Write code with triple quoto, based on the following list and context. +## Code: {filename} Write code with triple quote, based on the following list and context. 1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. 2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets 3. Attention1: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bd04ca79e..52a99dafc 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -23,7 +23,7 @@ templates = { ## Search Information {search_information} -## mermaid quadrantChart code syntax example. DONT USE QUOTO IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME +## mermaid quadrantChart code syntax example. DONT USE QUOTE IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME ```mermaid quadrantChart title Reach and engagement of campaigns @@ -108,7 +108,7 @@ and only output the json inside this tag, nothing else ## Search Information {search_information} -## mermaid quadrantChart code syntax example. DONT USE QUOTO IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME +## mermaid quadrantChart code syntax example. DONT USE QUOTE IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME ```mermaid quadrantChart title Reach and engagement of campaigns diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 35ff36dc2..e2352b641 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -26,7 +26,7 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W ``` Note that the code to test is at {source_file_path}, we will put your test code at {workspace}/tests/{test_file_name}, and run your test code from {workspace}, you should correctly import the necessary classes based on these file locations! -## {test_file_name}: Write test code with triple quoto. Do your best to implement THIS ONLY ONE FILE. +## {test_file_name}: Write test code with triple quote. Do your best to implement THIS ONLY ONE FILE. """ From 4cb3485c86bbfe3f96fb00b6bb0c15a6244a2282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 12 Dec 2023 21:32:03 +0800 Subject: [PATCH 257/413] feat: After users provide bug feedback, move directly to the WriteCode stage of the process. --- metagpt/actions/fix_bug.py | 14 +++++++ metagpt/actions/write_code.py | 11 ++++- metagpt/actions/write_prd.py | 49 +++++++++++++++++++--- metagpt/const.py | 1 + metagpt/roles/engineer.py | 11 ++--- metagpt/roles/role.py | 2 + metagpt/schema.py | 20 +++++---- metagpt/utils/git_repository.py | 42 ++++++++++++++++--- requirements.txt | 2 +- tests/metagpt/utils/test_git_repository.py | 7 ++++ 10 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 metagpt/actions/fix_bug.py diff --git a/metagpt/actions/fix_bug.py b/metagpt/actions/fix_bug.py new file mode 100644 index 000000000..6bd550d3d --- /dev/null +++ b/metagpt/actions/fix_bug.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +""" +@Time : 2023-12-12 +@Author : mashenquan +@File : fix_bug.py +""" +from metagpt.actions import Action + + +class FixBug(Action): + """Fix bug action without any implementation details""" + + async def run(self, *args, **kwargs): + raise NotImplementedError diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index b20539e78..1dda6466f 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -20,7 +20,8 @@ from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG -from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO, TASK_FILE_REPO +from metagpt.const import CODE_SUMMARIES_FILE_REPO, TEST_OUTPUTS_FILE_REPO, TASK_FILE_REPO, BUGFIX_FILENAME, \ + DOCS_FILE_REPO from metagpt.logs import logger from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser @@ -55,6 +56,12 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc {summary_log} ``` ----- +# Bug Feedback logs +```text +{feedback} +``` +----- + ## Code: {filename} Write code with triple quoto, based on the following list and context. 1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. @@ -89,6 +96,7 @@ class WriteCode(Action): return code async def run(self, *args, **kwargs) -> CodingContext: + bug_feedback = await FileRepository.get_file(filename=BUGFIX_FILENAME, relative_path=DOCS_FILE_REPO) coding_context = CodingContext.loads(self.context.content) test_doc = await FileRepository.get_file( filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO @@ -108,6 +116,7 @@ class WriteCode(Action): tasks=coding_context.task_doc.content if coding_context.task_doc else "", code=code_context, logs=logs, + feedback=bug_feedback.content if bug_feedback else "", filename=self.context.filename, summary_log=summary_doc.content if summary_doc else "", ) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 530a22def..aad2422ef 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -17,6 +17,7 @@ from pathlib import Path from typing import List from metagpt.actions import Action, ActionOutput +from metagpt.actions.fix_bug import FixBug from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.const import ( @@ -24,10 +25,10 @@ from metagpt.const import ( DOCS_FILE_REPO, PRD_PDF_FILE_REPO, PRDS_FILE_REPO, - REQUIREMENT_FILENAME, + REQUIREMENT_FILENAME, BUGFIX_FILENAME, ) from metagpt.logs import logger -from metagpt.schema import Document, Documents +from metagpt.schema import Document, Documents, Message, BugFixContext from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.get_template import get_template @@ -227,7 +228,6 @@ There are no unclear points. }, } - OUTPUT_MAPPING = { "Language": (str, ...), "Original Requirements": (str, ...), @@ -305,15 +305,44 @@ output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old P and only output the json inside this tag, nothing else """ +IS_BUGFIX_PROMPT = """ +{content} + +___ +You are a professional product manager; You need to determine whether the above content describes a requirement or provides feedback about a bug. +Respond with `YES` if it is a feedback about a bug, `NO` if it is not, and provide the reasons. Return the response in JSON format like below: + +```json +{{ + "is_bugfix": ..., # `YES` or `NO` + "reason": ..., # reason string +}} +``` +""" + class WritePRD(Action): def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: + async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput | Message: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are # related to the PRD. If they are related, rewrite the PRD. - requirement_doc = await FileRepository.get_file(filename=REQUIREMENT_FILENAME, relative_path=DOCS_FILE_REPO) + docs_file_repo = CONFIG.git_repo.new_file_repository(relative_path=DOCS_FILE_REPO) + requirement_doc = await docs_file_repo.get(filename=REQUIREMENT_FILENAME) + if await self._is_bugfix(requirement_doc.content): + await docs_file_repo.save(filename=BUGFIX_FILENAME, content=requirement_doc.content) + await docs_file_repo.save(filename=REQUIREMENT_FILENAME, content="") + bug_fix = BugFixContext(filename=BUGFIX_FILENAME) + return Message(content=bug_fix.json(), instruct_content=bug_fix, + role=self.profile, + cause_by=FixBug, + sent_from=self, + send_to="Alex", # the name of Engineer + ) + else: + await docs_file_repo.delete(filename=BUGFIX_FILENAME) + prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) prd_docs = await prds_file_repo.get_all() change_files = Documents() @@ -405,7 +434,7 @@ class WritePRD(Action): if not quadrant_chart: return pathname = ( - CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") + CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") ) if not pathname.parent.exists(): pathname.parent.mkdir(parents=True, exist_ok=True) @@ -430,3 +459,11 @@ class WritePRD(Action): ws_name = CodeParser.parse_str(block="Project Name", text=prd) CONFIG.project_name = ws_name CONFIG.git_repo.rename_root(CONFIG.project_name) + + async def _is_bugfix(self, content): + prompt = IS_BUGFIX_PROMPT.format(content=content) + res = await self._aask(prompt=prompt) + logger.info(f"IS_BUGFIX:{res}") + if "YES" in res: + return True + return False diff --git a/metagpt/const.py b/metagpt/const.py index bd735a5e1..f6f64a27d 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -74,6 +74,7 @@ MESSAGE_ROUTE_TO_ALL = "" MESSAGE_ROUTE_TO_NONE = "" REQUIREMENT_FILENAME = "requirement.txt" +BUGFIX_FILENAME = "bugfix.txt" PACKAGE_REQUIREMENTS_FILENAME = "requirements.txt" DOCS_FILE_REPO = "docs" diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 9f8eb6482..cedd2101f 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -24,6 +24,7 @@ from pathlib import Path from typing import Set from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks +from metagpt.actions.fix_bug import FixBug from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( @@ -78,7 +79,7 @@ class Engineer(Role): """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) self.use_code_review = use_code_review - self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview]) + self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug]) self.code_todos = [] self.summarize_todos = [] self.n_borg = n_borg @@ -191,14 +192,14 @@ class Engineer(Role): async def _think(self) -> Action | None: if not CONFIG.src_workspace: CONFIG.src_workspace = CONFIG.git_repo.workdir / CONFIG.git_repo.workdir.name - write_code_filters = any_to_str_set([WriteTasks, SummarizeCode]) + write_code_filters = any_to_str_set([WriteTasks, SummarizeCode, FixBug]) summarize_code_filters = any_to_str_set([WriteCode, WriteCodeReview]) if not self._rc.news: return None msg = self._rc.news[0] if msg.cause_by in write_code_filters: logger.info(f"TODO WriteCode:{msg.json()}") - await self._new_code_actions() + await self._new_code_actions(bug_fix=msg.cause_by == any_to_str(FixBug)) return self._rc.todo if msg.cause_by in summarize_code_filters and msg.sent_from == any_to_str(self): logger.info(f"TODO SummarizeCode:{msg.json()}") @@ -232,10 +233,10 @@ class Engineer(Role): coding_doc = Document(root_path=str(src_file_repo.root_path), filename=filename, content=context.json()) return coding_doc - async def _new_code_actions(self): + async def _new_code_actions(self, bug_fix=False): # Prepare file repos src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) - changed_src_files = src_file_repo.changed_files + changed_src_files = src_file_repo.all_files if bug_fix else src_file_repo.changed_files task_file_repo = CONFIG.git_repo.new_file_repository(TASK_FILE_REPO) changed_task_files = task_file_repo.changed_files design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 2651be7eb..52ac3cf28 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -286,6 +286,8 @@ class Role: cause_by=self._rc.todo, sent_from=self, ) + elif isinstance(response, Message): + msg = response else: msg = Message(content=response, role=self.profile, cause_by=self._rc.todo, sent_from=self) self._rc.memory.add(msg) diff --git a/metagpt/schema.py b/metagpt/schema.py index a8c1b7726..25281e399 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -97,14 +97,14 @@ class Message(BaseModel): send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL}) def __init__( - self, - content, - instruct_content=None, - role="user", - cause_by="", - sent_from="", - send_to=MESSAGE_ROUTE_TO_ALL, - **kwargs, + self, + content, + instruct_content=None, + role="user", + cause_by="", + sent_from="", + send_to=MESSAGE_ROUTE_TO_ALL, + **kwargs, ): """ Parameters not listed below will be stored as meta info, including custom parameters. @@ -341,3 +341,7 @@ class CodeSummarizeContext(BaseModel): def __hash__(self): return hash((self.design_filename, self.task_filename)) + + +class BugFixContext(BaseModel): + filename: str = "" diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 5aec4509c..d372fd22e 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -8,15 +8,13 @@ """ from __future__ import annotations -import os +from gitignore_parser import parse_gitignore, rule_from_pattern, handle_negation import shutil from enum import Enum from pathlib import Path from typing import Dict, List - from git.repo import Repo from git.repo.fun import is_git_dir - from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.utils.dependency_file import DependencyFile @@ -51,6 +49,7 @@ class GitRepository: """ self._repository = None self._dependency = None + self._gitignore_rules = None if local_path: self.open(local_path=local_path, auto_init=auto_init) @@ -63,6 +62,7 @@ class GitRepository: local_path = Path(local_path) if self.is_git_dir(local_path): self._repository = Repo(local_path) + self._gitignore_rules = parse_gitignore(full_path=str(local_path / ".gitignore")) return if not auto_init: return @@ -82,6 +82,7 @@ class GitRepository: writer.write("\n".join(ignores)) self._repository.index.add([".gitignore"]) self._repository.index.commit("Add .gitignore") + self._gitignore_rules = parse_gitignore(full_path=gitignore_filename) def add_change(self, files: Dict): """Add or remove files from the staging area based on the provided changes. @@ -204,8 +205,9 @@ class GitRepository: logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) - def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None) -> List: - """Retrieve a list of files in the specified relative path. + def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None, filter_ignored=True) -> List: + """ + Retrieve a list of files in the specified relative path. The method returns a list of file paths relative to the current FileRepository. @@ -213,6 +215,8 @@ class GitRepository: :type relative_path: Path or str :param root_relative_path: The root relative path within the repository. :type root_relative_path: Path or str + :param filter_ignored: Flag to indicate whether to filter files based on .gitignore rules. + :type filter_ignored: bool :return: A list of file paths in the specified directory. :rtype: List[str] """ @@ -231,10 +235,35 @@ class GitRepository: rpath = file_path.relative_to(root_relative_path) files.append(str(rpath)) else: - subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path) + subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path, + filter_ignored=False) files.extend(subfolder_files) except Exception as e: logger.error(f"Error: {e}") + if not filter_ignored: + return files + filtered_files = self.filter_gitignore(filenames=files, root_relative_path=root_relative_path) + return filtered_files + + def filter_gitignore(self, filenames: List[str], root_relative_path: Path | str = None) -> List[str]: + """ + Filter a list of filenames based on .gitignore rules. + + :param filenames: A list of filenames to be filtered. + :type filenames: List[str] + :param root_relative_path: The root relative path within the repository. + :type root_relative_path: Path or str + :return: A list of filenames that pass the .gitignore filtering. + :rtype: List[str] + """ + if root_relative_path is None: + root_relative_path = self.workdir + files = [] + for filename in filenames: + pathname = root_relative_path / filename + if self._gitignore_rules(str(pathname)): + continue + files.append(filename) return files @@ -244,6 +273,7 @@ if __name__ == "__main__": repo = GitRepository() repo.open(path, auto_init=True) + repo.filter_gitignore(filenames=["snake_game/snake_game/__pycache__", "snake_game/snake_game/game.py"]) changes = repo.changed_files print(changes) diff --git a/requirements.txt b/requirements.txt index 99f738448..515a4d88b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -48,4 +48,4 @@ websocket-client==0.58.0 aiofiles==23.2.1 gitpython==3.1.40 zhipuai==1.0.7 - +gitignore-parser==0.1.9 diff --git a/tests/metagpt/utils/test_git_repository.py b/tests/metagpt/utils/test_git_repository.py index 23bebba7f..d800e9594 100644 --- a/tests/metagpt/utils/test_git_repository.py +++ b/tests/metagpt/utils/test_git_repository.py @@ -73,6 +73,13 @@ async def test_git1(): repo1 = GitRepository(local_path=local_path, auto_init=False) assert repo1.changed_files + file_repo = repo1.new_file_repository("__pycache__") + await file_repo.save("a.pyc", content="") + all_files = repo1.get_files(relative_path=".", filter_ignored=False) + assert "__pycache__/a.pyc" in all_files + all_files = repo1.get_files(relative_path=".", filter_ignored=True) + assert "__pycache__/a.pyc" not in all_files + repo1.delete_repository() assert not local_path.exists() From 88bbc75d565a8549ed790c78d95fdd6759630085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Wed, 13 Dec 2023 22:19:55 +0800 Subject: [PATCH 258/413] fixbug: gitignore error after project renamed --- metagpt/utils/git_repository.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index d372fd22e..9827b8252 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -204,6 +204,7 @@ class GitRepository: logger.warning(f"Move {str(self.workdir)} to {str(new_path)} error: {e}") logger.info(f"Rename directory {str(self.workdir)} to {str(new_path)}") self._repository = Repo(new_path) + self._gitignore_rules = parse_gitignore(full_path=str(new_path / ".gitignore")) def get_files(self, relative_path: Path | str, root_relative_path: Path | str = None, filter_ignored=True) -> List: """ From ad0e5a6da83d6ded26f9b7f36b834c0bba78b8b9 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 12 Dec 2023 16:49:41 +0800 Subject: [PATCH 259/413] action_node: make it work at first step. --- metagpt/actions/action_node.py | 258 +++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 metagpt/actions/action_node.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py new file mode 100644 index 000000000..4fbd3ce7f --- /dev/null +++ b/metagpt/actions/action_node.py @@ -0,0 +1,258 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/11 18:45 +@Author : alexanderwu +@File : action_node.py +""" +from typing import Dict, Type, List, Any +import json + +from pydantic import BaseModel, create_model, root_validator, validator +# , model_validator, field_validator + +from metagpt.logs import logger + + +def dict_to_markdown(d, prefix="##", postfix="\n\n"): + markdown_str = "" + for key, value in d.items(): + markdown_str += f"{prefix} {key}: {value}{postfix}" + return markdown_str + + +class ActionNode: + """ActionNode is a tree of nodes.""" + + # Action Inputs + key: str # Product Requirement / File list / Code + expected_type: Type # such as str / int / float etc. + # context: str # everything in the history. + instruction: str # the instructions should be followed. + example: str # example for In Context-Learning. + + # Action Outputs + content: str + instruct_content: BaseModel + children: dict[str, "ActionNode"] + + def __init__(self, key, expected_type, instruction, example, content="", + children=None): + self.key = key + self.expected_type = expected_type + self.instruction = instruction + self.example = example + self.content = content + self.children = children if children is not None else {} + + def __str__(self): + return f"{self.key}, {self.expected_type}, {self.instruction}, {self.example}" \ + f", {self.content}, {self.children}" + + def __repr__(self): + return self.__str__() + + def add_child(self, node: "ActionNode"): + """增加子ActionNode""" + self.children[node.key] = node + + def add_childs(self, nodes: List["ActionNode"]): + """批量增加子ActionNode""" + for node in nodes: + self.add_child(node) + + def get_children_mapping(self) -> Dict[str, Type]: + """获得子ActionNode的字典,以key索引""" + return {k: v.expected_type for k, v in self.children.items()} + + @classmethod + def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): + """基于pydantic v1的模型动态生成,用来检验结果类型正确性""" + new_class = create_model(class_name, **mapping) + + @validator("*", allow_reuse=True) + def check_name(v, field): + if field.name not in mapping.keys(): + raise ValueError(f"Unrecognized block: {field.name}") + return v + + @root_validator(pre=True, allow_reuse=True) + def check_missing_fields(values): + required_fields = set(mapping.keys()) + missing_fields = required_fields - set(values.keys()) + if missing_fields: + raise ValueError(f"Missing fields: {missing_fields}") + return values + + new_class.__validator_check_name = classmethod(check_name) + new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) + return new_class + + @classmethod + def create_model_class_v2(cls, class_name: str, mapping: Dict[str, Type]): + """基于pydantic v2的模型动态生成,用来检验结果类型正确性,待验证""" + new_class = create_model(class_name, **mapping) + + @model_validator(mode='before') + def check_missing_fields(data): + required_fields = set(mapping.keys()) + missing_fields = required_fields - set(data.keys()) + if missing_fields: + raise ValueError(f"Missing fields: {missing_fields}") + return data + + @field_validator('*') + def check_name(v: Any, field: str) -> Any: + if field not in mapping.keys(): + raise ValueError(f"Unrecognized block: {field}") + return v + + new_class.__model_validator_check_missing_fields = classmethod(check_missing_fields) + new_class.__field_validator_check_name = classmethod(check_name) + return new_class + + def create_children_class(self): + """使用object内有的字段直接生成model_class""" + class_name = f"{self.key}_AN" + mapping = self.get_children_mapping() + return self.create_model_class(class_name, mapping) + + def to_dict(self, format_func=None, mode="all") -> Dict: + # 如果没有提供格式化函数,使用默认的格式化方式 + if format_func is None: + format_func = lambda node: f"{node.instruction}" + + # 使用提供的格式化函数来格式化当前节点的值 + formatted_value = format_func(self) + + # 创建当前节点的键值对 + if mode == "children": + node_dict = {} + else: + node_dict = {self.key: formatted_value} + + if mode == "root": + return node_dict + + # 遍历子节点并递归调用 to_dict 方法 + for child_key, child_node in self.children.items(): + node_dict.update(child_node.to_dict(format_func)) + + return node_dict + + def compile_to(self, i: Dict, to="raw") -> str: + if to == "json": + return json.dumps(i, indent=4) + elif to == "markdown": + return dict_to_markdown(i) + else: + return str(i) + + def compile_instruction(self, to="raw", mode="children") -> str: + """compile to raw/json/markdown template with all/root/children nodes""" + format_func = lambda i: f"{i.expected_type} # {i.instruction}" + nodes = self.to_dict(format_func=format_func, mode=mode) + return self.compile_to(nodes, to) + + def compile_example(self, to="raw", mode="all") -> str: + """compile to raw/json/markdown examples with all/root/children nodes""" + format_func = lambda i: f"{i.example}" + nodes = self.to_dict(format_func=format_func, mode=mode) + return self.compile_to(nodes, to) + + def compile(self, to="raw", mode="all") -> str: + pass + + def run(self): + """运行这个ActionNode,可以采用不同策略,比如只运行子节点""" + pass + + +IMPLEMENTATION_APPROACH = ActionNode( + key="implementation_approach", + expected_type=str, + instruction="Analyze the difficult points of the requirements, select the appropriate open-source framework", + example="We will ..." +) + +PROJECT_NAME = ActionNode( + key="project_name", + expected_type=str, + instruction="The project name with underline", + example="game_2048" +) + +FILE_LIST = ActionNode( + key="file_list", + expected_type=List[str], + instruction="Only need relative paths. ALWAYS write a main.py or app.py here", + example="['main.py', 'const.py', 'utils.py']" +) + +DATA_STRUCTURES_AND_INTERFACES = ActionNode( + key="data_structures_and_interfaces", + expected_type=str, + instruction="Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions " + "(with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " + "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", + example="""classDiagram +class Game{{ + +int score +}} +... +Game "1" -- "1" Food: has""" +) + +PROGRAM_CALL_FLOW = ActionNode( + key="program_call_flow", + expected_type=str, + instruction="Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE " + "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", + example="""sequenceDiagram +participant M as Main +... +G->>M: end game""" +) + +ANYTHING_UNCLEAR = ActionNode( + key="anything_unclear", + expected_type=str, + instruction="Mention unclear project aspects, then try to clarify it.", + example="Clarification needed on third-party API integration, ..." +) + + +ACTION_NODES = [ + IMPLEMENTATION_APPROACH, + PROJECT_NAME, + FILE_LIST, + DATA_STRUCTURES_AND_INTERFACES, + PROGRAM_CALL_FLOW, + ANYTHING_UNCLEAR +] + + +def action_node_from_tuple_example(): + # 示例:列表中包含元组 + list_of_tuples = [ + ("key1", str, "Instruction 1", "Example 1", "Content 1", {"child1": ...}), + ("key2", int, "Instruction 2", "Example 2", "Content 2"), + ("key3", int, "Instruction 3", "Example 3") + ] + + # 从列表中创建 ActionNode 实例 + nodes = [ActionNode(*data) for data in list_of_tuples] + for i in nodes: + logger.info(i) + + +def main(): + write_design_node = ActionNode("WriteDesign", str, "", "") + write_design_node.add_childs(ACTION_NODES) + instruction = write_design_node.compile_instruction(to="markdown") + logger.info(instruction) + logger.info(write_design_node.compile_example()) + + +if __name__ == '__main__': + main() From bfdb8415adc0c23ef7654402c862bf8302d34f92 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 13 Dec 2023 17:47:09 +0800 Subject: [PATCH 260/413] tuning action node code --- metagpt/actions/action.py | 6 +- metagpt/actions/action_node.py | 140 ++++++++++++------------------- metagpt/actions/design_api.py | 12 ++- metagpt/actions/write_prd.py | 4 +- metagpt/config.py | 2 +- metagpt/environment.py | 2 +- metagpt/utils/file_repository.py | 2 +- 7 files changed, 72 insertions(+), 96 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index dc96699a9..40faaad41 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -25,9 +25,9 @@ class Action(ABC): llm = LLM() self.llm = llm self.context = context - self.prefix = "" - self.profile = "" - self.desc = "" + self.prefix = "" # aask*时会加上prefix,作为system_message + self.profile = "" # FIXME: USELESS + self.desc = "" # FIXME: USELESS self.content = "" self.instruct_content = None self.env = None diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 4fbd3ce7f..35912446d 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -5,7 +5,7 @@ @Author : alexanderwu @File : action_node.py """ -from typing import Dict, Type, List, Any +from typing import Dict, Type, List, Any, Tuple import json from pydantic import BaseModel, create_model, root_validator, validator @@ -14,7 +14,16 @@ from pydantic import BaseModel, create_model, root_validator, validator from metagpt.logs import logger -def dict_to_markdown(d, prefix="##", postfix="\n\n"): +SIMPLE_TEMPLATE = """ +## example +{example} + +## instruction +{instruction} +""" + + +def dict_to_markdown(d, prefix="###", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" @@ -23,13 +32,17 @@ def dict_to_markdown(d, prefix="##", postfix="\n\n"): class ActionNode: """ActionNode is a tree of nodes.""" + # 应该是定义子任务,收集子任务结果,并且父任务同时执行吗? + # 初期只提供两种模式,一种是用父任务compile,一种是用子任务逐个执行 + # 1. context、example、instruction-nodes、instruction-action + # 2. context、example # Action Inputs key: str # Product Requirement / File list / Code expected_type: Type # such as str / int / float etc. # context: str # everything in the history. instruction: str # the instructions should be followed. - example: str # example for In Context-Learning. + example: Any # example for In Context-Learning. # Action Outputs content: str @@ -56,7 +69,7 @@ class ActionNode: """增加子ActionNode""" self.children[node.key] = node - def add_childs(self, nodes: List["ActionNode"]): + def add_children(self, nodes: List["ActionNode"]): """批量增加子ActionNode""" for node in nodes: self.add_child(node) @@ -140,7 +153,7 @@ class ActionNode: return node_dict - def compile_to(self, i: Dict, to="raw") -> str: + def compile_to(self, i: Dict, to) -> str: if to == "json": return json.dumps(i, indent=4) elif to == "markdown": @@ -148,88 +161,49 @@ class ActionNode: else: return str(i) - def compile_instruction(self, to="raw", mode="children") -> str: + def tagging(self, text, to, tag="") -> str: + if not tag: + return text + if to == "json": + return f"[{tag}]\n" + "{" + text + "}" + f"\n[/{tag}]" + else: + return f"[{tag}]\n" + text + f"\n[/{tag}]" + + def _compile_f(self, to, mode, tag, format_func) -> str: + nodes = self.to_dict(format_func=format_func, mode=mode) + text = self.compile_to(nodes, to) + return self.tagging(text, to, tag) + + def compile_instruction(self, to="raw", mode="children", tag="") -> str: """compile to raw/json/markdown template with all/root/children nodes""" format_func = lambda i: f"{i.expected_type} # {i.instruction}" - nodes = self.to_dict(format_func=format_func, mode=mode) - return self.compile_to(nodes, to) + return self._compile_f(to, mode, tag, format_func) - def compile_example(self, to="raw", mode="all") -> str: + def compile_example(self, to="raw", mode="children", tag="") -> str: """compile to raw/json/markdown examples with all/root/children nodes""" - format_func = lambda i: f"{i.example}" - nodes = self.to_dict(format_func=format_func, mode=mode) - return self.compile_to(nodes, to) - def compile(self, to="raw", mode="all") -> str: - pass + # 这里不能使用f-string,因为转译为str后再json.dumps会额外加上引号,无法作为有效的example + # 错误示例:"File list": "['main.py', 'const.py', 'game.py']", 注意这里值不是list,而是str + format_func = lambda i: i.example + return self._compile_f(to, mode, tag, format_func) + + def compile(self, mode="children") -> Tuple[str, str]: + """ + mode: all/root/children + mode="children": 编译所有子节点为一个统一模板,包括instruction与example + mode="all": NotImplemented + mode="root": NotImplemented + """ + self.instruction = self.compile_instruction(to="json", mode=mode) + self.example = self.compile_example(to="json", tag="CONTENT", mode=mode) + # prompt = template.format(example=self.example, instruction=self.instruction) + return self.instruction, self.example def run(self): """运行这个ActionNode,可以采用不同策略,比如只运行子节点""" - pass - -IMPLEMENTATION_APPROACH = ActionNode( - key="implementation_approach", - expected_type=str, - instruction="Analyze the difficult points of the requirements, select the appropriate open-source framework", - example="We will ..." -) - -PROJECT_NAME = ActionNode( - key="project_name", - expected_type=str, - instruction="The project name with underline", - example="game_2048" -) - -FILE_LIST = ActionNode( - key="file_list", - expected_type=List[str], - instruction="Only need relative paths. ALWAYS write a main.py or app.py here", - example="['main.py', 'const.py', 'utils.py']" -) - -DATA_STRUCTURES_AND_INTERFACES = ActionNode( - key="data_structures_and_interfaces", - expected_type=str, - instruction="Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions " - "(with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " - "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", - example="""classDiagram -class Game{{ - +int score -}} -... -Game "1" -- "1" Food: has""" -) - -PROGRAM_CALL_FLOW = ActionNode( - key="program_call_flow", - expected_type=str, - instruction="Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE " - "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", - example="""sequenceDiagram -participant M as Main -... -G->>M: end game""" -) - -ANYTHING_UNCLEAR = ActionNode( - key="anything_unclear", - expected_type=str, - instruction="Mention unclear project aspects, then try to clarify it.", - example="Clarification needed on third-party API integration, ..." -) - - -ACTION_NODES = [ - IMPLEMENTATION_APPROACH, - PROJECT_NAME, - FILE_LIST, - DATA_STRUCTURES_AND_INTERFACES, - PROGRAM_CALL_FLOW, - ANYTHING_UNCLEAR -] + # 需要传入llm,并且实际在ActionNode中执行。需要规划好具体的执行方法 + raise NotImplementedError def action_node_from_tuple_example(): @@ -246,13 +220,5 @@ def action_node_from_tuple_example(): logger.info(i) -def main(): - write_design_node = ActionNode("WriteDesign", str, "", "") - write_design_node.add_childs(ACTION_NODES) - instruction = write_design_node.compile_instruction(to="markdown") - logger.info(instruction) - logger.info(write_design_node.compile_example()) - - if __name__ == '__main__': - main() + action_node_from_tuple_example() diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 557ebcbbd..a6d559a4c 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -14,6 +14,7 @@ from pathlib import Path from typing import List from metagpt.actions import Action, ActionOutput +from metagpt.actions.design_api_an import DESIGN_API_NODE, SIMPLE_TEMPLATE from metagpt.config import CONFIG from metagpt.const import ( DATA_API_DESIGN_FILE_REPO, @@ -227,13 +228,22 @@ class WriteDesign(Action): # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - async def _new_system_design(self, context, format=CONFIG.prompt_format): + async def _new_system_design_bakup(self, context, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) format_example = format_example.format(project_name=CONFIG.project_name) prompt = prompt_template.format(context=context, format_example=format_example) system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) return system_design + async def _new_system_design(self, context, format=CONFIG.prompt_format): + instruction, example = DESIGN_API_NODE.compile() + prompt = SIMPLE_TEMPLATE.format(context=context, example=example, instruction=instruction) + # prompt_template, format_example = get_template(templates, format) + # format_example = format_example.format(project_name=CONFIG.project_name) + # prompt = prompt_template.format(context=context, format_example=format_example) + system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) + return system_design + async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): prompt = MERGE_PROMPT.format( old_design=system_design_doc.content, context=prd_doc.content, project_name=CONFIG.project_name diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index aad2422ef..0594d116e 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -361,7 +361,7 @@ class WritePRD(Action): ) if prd_doc: change_files.docs[prd_doc.filename] = prd_doc - logger.info(f"NEW PRD:{prd_doc.filename}") + logger.debug(f"new prd: {prd_doc.filename}") # Once all files under 'docs/prds/' have been compared with the newly added requirements, trigger the # 'publish' message to transition the workflow to the next stage. This design allows room for global # optimization in subsequent steps. @@ -394,7 +394,7 @@ class WritePRD(Action): async def _is_relative_to(self, new_requirement_doc, old_prd_doc) -> bool: prompt = IS_RELATIVE_PROMPT.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) res = await self._aask(prompt=prompt) - logger.info(f"REQ-RELATIVE:[{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") + logger.info(f"REQ-RELATIVE: [{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") if "YES" in res: return True return False diff --git a/metagpt/config.py b/metagpt/config.py index d04ae7291..d2390f704 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -108,7 +108,7 @@ class Config(metaclass=Singleton): def _ensure_workspace_exists(self): self.workspace_path.mkdir(parents=True, exist_ok=True) - logger.info(f"WORKSPACE_PATH set to {self.workspace_path}") + logger.debug(f"WORKSPACE_PATH set to {self.workspace_path}") def _init_with_config_files_and_env(self, yaml_file): """Load from config/key.yaml, config/config.yaml, and env in decreasing order of priority""" diff --git a/metagpt/environment.py b/metagpt/environment.py index 02eb3d340..7d1e307f3 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -58,7 +58,7 @@ class Environment(BaseModel): route the message to the message recipient is a problem addressed by the transport framework designed in RFC 113. """ - logger.info(f"publish_message: {message.dump()}") + logger.debug(f"publish_message: {message.dump()}") found = False # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 for role, subscription in self.members.items(): diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 2cace7232..2eca799a8 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -205,7 +205,7 @@ class FileRepository: m = json.loads(doc.content) filename = Path(doc.filename).with_suffix(with_suffix) if with_suffix is not None else Path(doc.filename) await self.save(filename=str(filename), content=json_to_markdown(m), dependencies=dependencies) - logger.info(f"File Saved: {str(filename)}") + logger.debug(f"File Saved: {str(filename)}") @staticmethod async def get_file(filename: Path | str, relative_path: Path | str = ".") -> Document | None: From 5d7c228539be3d50e1e97d8927cef34852117f82 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 13 Dec 2023 17:47:19 +0800 Subject: [PATCH 261/413] tuning action node code --- metagpt/actions/design_api_an.py | 146 +++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 metagpt/actions/design_api_an.py diff --git a/metagpt/actions/design_api_an.py b/metagpt/actions/design_api_an.py new file mode 100644 index 000000000..b4bd54849 --- /dev/null +++ b/metagpt/actions/design_api_an.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/12 22:24 +@Author : alexanderwu +@File : design_api_an.py +""" +from metagpt.actions.action_node import ActionNode +from metagpt.logs import logger + +IMPLEMENTATION_APPROACH = ActionNode( + key="Implementation approach", + expected_type=str, + instruction="Analyze the difficult points of the requirements, select the appropriate open-source framework", + example="We will ..." +) + +PROJECT_NAME = ActionNode( + key="Project name", + expected_type=str, + instruction="The project name with underline", + example="game_2048" +) + +FILE_LIST = ActionNode( + key="File list", + expected_type=list[str], + instruction="Only need relative paths. ALWAYS write a main.py or app.py here", + example=['main.py', 'game.py'] +) + +DATA_STRUCTURES_AND_INTERFACES = ActionNode( + key="Data structures and interfaces", + expected_type=str, + instruction="Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions " + "(with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " + "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", + example=""" classDiagram + class User { + +int id + +str username + +str email + +str password + __init__(id: int, username: str, email: str, password: str) + follow(user: User): void + like(content: Content): void + comment(content: Content, text: str): Comment + } + class Content { + +int id + +User author + +str title + +str body + +datetime created_at + +list likes + +list comments + __init__(id: int, author: User, title: str, body: str) + get_likes(): list + get_comments(): list + } + class Comment { + +int id + +User author + +str text + +datetime created_at + __init__(id: int, author: User, text: str) + } + class Leaderboard { + +list top_contents + update(): void + } + class SearchEngine { + +str query + search(): list + } + class RecommendationEngine { + +User user + recommend(): list + } + class TaskQueue { + +str task_name + enqueue(task: function): void + } + User "1" -- "*" Content: creates + Content "1" -- "*" Comment: includes + User "1" -- "*" Comment: writes + User "1" -- "*" User: follows + Content "1" -- "*" User: liked_by""" +) + +PROGRAM_CALL_FLOW = ActionNode( + key="Program call flow", + expected_type=str, + instruction="Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE " + "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", + example="""sequenceDiagram +participant M as Main +... +G->>M: end game""" +) + +ANYTHING_UNCLEAR = ActionNode( + key="Anything UNCLEAR", + expected_type=str, + instruction="Mention unclear project aspects, then try to clarify it.", + example="Clarification needed on third-party API integration, ..." +) + +ACTION_NODES = [ + IMPLEMENTATION_APPROACH, + PROJECT_NAME, + FILE_LIST, + DATA_STRUCTURES_AND_INTERFACES, + PROGRAM_CALL_FLOW, + ANYTHING_UNCLEAR +] + +DESIGN_API_NODE = ActionNode("DesignAPI", str, "", "") +DESIGN_API_NODE.add_children(ACTION_NODES) + +SIMPLE_TEMPLATE = """ +## context +{context} + +## example +{example} + +## instruction-nodes: ": # " +{instruction} + +## instruction-action +Role: You are an architect; the goal is to design a SOTA software system +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. +Requirement: Fill in the above missing instruction-nodes based on the context +now, output wrapped inside [CONTENT][/CONTENT] as example, nothing else. +""" + + +def main(): + instruction, example = DESIGN_API_NODE.compile() + text = SIMPLE_TEMPLATE.format(context="", example=example, instruction=instruction) + logger.info(text) + + +if __name__ == '__main__': + main() From c0bcf57caf134008ea5c8bd9a2df3cbdb3465759 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 15:58:05 +0800 Subject: [PATCH 262/413] Transfer Action usage to ActionNode for subsequent structured reasoning opportunities - Modifided actions: project_management / design_api / write_prd --- metagpt/actions/action.py | 20 +- metagpt/actions/action_node.py | 122 ++++++-- metagpt/actions/design_api.py | 189 +----------- metagpt/actions/design_api_an.py | 91 +----- metagpt/actions/project_management.py | 206 +------------ metagpt/actions/project_management_an.py | 82 +++++ metagpt/actions/write_prd.py | 366 +++-------------------- metagpt/actions/write_prd_an.py | 153 ++++++++++ metagpt/environment.py | 2 +- metagpt/llm.py | 3 +- metagpt/roles/architect.py | 4 +- metagpt/roles/engineer.py | 9 +- metagpt/roles/project_manager.py | 3 +- metagpt/roles/role.py | 5 +- metagpt/roles/searcher.py | 3 +- 15 files changed, 438 insertions(+), 820 deletions(-) create mode 100644 metagpt/actions/project_management_an.py create mode 100644 metagpt/actions/write_prd_an.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 40faaad41..2fd130cf5 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -27,18 +27,22 @@ class Action(ABC): self.context = context self.prefix = "" # aask*时会加上prefix,作为system_message self.profile = "" # FIXME: USELESS - self.desc = "" # FIXME: USELESS - self.content = "" - self.instruct_content = None - self.env = None + self.desc = "" # for skill manager + self.nodes = ... - def set_env(self, env): - self.env = env + # Output, useless + # self.content = "" + # self.instruct_content = None + # self.env = None + + # def set_env(self, env): + # self.env = env def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix self.profile = profile + self.llm.system_prompt = prefix def __str__(self): return self.__class__.__name__ @@ -62,10 +66,6 @@ class Action(ABC): system_msgs: Optional[list[str]] = None, format="markdown", # compatible to original format ) -> ActionOutput: - """Append default prefix""" - if not system_msgs: - system_msgs = [] - system_msgs.append(self.prefix) content = await self.llm.aask(prompt, system_msgs) logger.debug(content) output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 35912446d..178986ebe 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -5,25 +5,44 @@ @Author : alexanderwu @File : action_node.py """ -from typing import Dict, Type, List, Any, Tuple +import re +from typing import Dict, Type, List, Any, Tuple, Optional import json from pydantic import BaseModel, create_model, root_validator, validator # , model_validator, field_validator +from tenacity import wait_random_exponential, stop_after_attempt, retry +from metagpt.actions import ActionOutput +from metagpt.llm import BaseGPTAPI from metagpt.logs import logger +from metagpt.utils.common import OutputParser +from metagpt.utils.custom_decoder import CustomDecoder +CONSTRAINT = """ +- Language: Please use the same language as the user input. +- Format: output wrapped inside [CONTENT][/CONTENT] as format example, nothing else. +""" SIMPLE_TEMPLATE = """ -## example +## context +{context} + +## format example {example} -## instruction +## nodes: ": # " {instruction} + +## constraint +{constraint} + +## action +Fill in the above nodes based on the context. Answer in format example. """ -def dict_to_markdown(d, prefix="###", postfix="\n"): +def dict_to_markdown(d, prefix="-", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" @@ -32,22 +51,26 @@ def dict_to_markdown(d, prefix="###", postfix="\n"): class ActionNode: """ActionNode is a tree of nodes.""" - # 应该是定义子任务,收集子任务结果,并且父任务同时执行吗? - # 初期只提供两种模式,一种是用父任务compile,一种是用子任务逐个执行 - # 1. context、example、instruction-nodes、instruction-action - # 2. context、example + # Action Strgy + # - sop: 仅使用一级SOP + # - complex: 使用一级SOP+自定义策略填槽 + mode: str - # Action Inputs + # Action Context + context: str # all the context, including all necessary info + llm: BaseGPTAPI # LLM with aask interface + children: dict[str, "ActionNode"] + + # Action Input key: str # Product Requirement / File list / Code expected_type: Type # such as str / int / float etc. # context: str # everything in the history. instruction: str # the instructions should be followed. example: Any # example for In Context-Learning. - # Action Outputs + # Action Output content: str instruct_content: BaseModel - children: dict[str, "ActionNode"] def __init__(self, key, expected_type, instruction, example, content="", children=None): @@ -74,9 +97,16 @@ class ActionNode: for node in nodes: self.add_child(node) + @classmethod + def from_children(cls, key, nodes: List["ActionNode"]): + """直接从一系列的子nodes初始化""" + obj = cls(key, str, "", "") + obj.add_children(nodes) + return obj + def get_children_mapping(self) -> Dict[str, Type]: """获得子ActionNode的字典,以key索引""" - return {k: v.expected_type for k, v in self.children.items()} + return {k: (v.expected_type, ...) for k, v in self.children.items()} @classmethod def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): @@ -131,6 +161,8 @@ class ActionNode: return self.create_model_class(class_name, mapping) def to_dict(self, format_func=None, mode="all") -> Dict: + """将当前节点与子节点都按照node: format的格式组织称字典""" + # 如果没有提供格式化函数,使用默认的格式化方式 if format_func is None: format_func = lambda node: f"{node.instruction}" @@ -165,7 +197,7 @@ class ActionNode: if not tag: return text if to == "json": - return f"[{tag}]\n" + "{" + text + "}" + f"\n[/{tag}]" + return f"[{tag}]\n" + text + f"\n[/{tag}]" else: return f"[{tag}]\n" + text + f"\n[/{tag}]" @@ -187,31 +219,73 @@ class ActionNode: format_func = lambda i: i.example return self._compile_f(to, mode, tag, format_func) - def compile(self, mode="children") -> Tuple[str, str]: + def compile(self, context, to="json", mode="children", template=SIMPLE_TEMPLATE) -> str: """ mode: all/root/children mode="children": 编译所有子节点为一个统一模板,包括instruction与example mode="all": NotImplemented mode="root": NotImplemented """ - self.instruction = self.compile_instruction(to="json", mode=mode) - self.example = self.compile_example(to="json", tag="CONTENT", mode=mode) - # prompt = template.format(example=self.example, instruction=self.instruction) - return self.instruction, self.example - def run(self): - """运行这个ActionNode,可以采用不同策略,比如只运行子节点""" + # FIXME: json instruction会带来 "Project name": "web_2048 # 项目名称使用下划线", + self.instruction = self.compile_instruction(to="markdown", mode=mode) + self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) + prompt = template.format(context=context, example=self.example, instruction=self.instruction, + constraint=CONSTRAINT) + return prompt + @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) + async def _aask_v1( + self, + prompt: str, + output_class_name: str, + output_data_mapping: dict, + system_msgs: Optional[list[str]] = None, + format="markdown", # compatible to original format + ) -> ActionOutput: + content = await self.llm.aask(prompt, system_msgs) + logger.debug(content) + output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) + + if format == "json": + pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" + matches = re.findall(pattern, content, re.DOTALL) + + for match in matches: + if match: + content = match + break + + parsed_data = CustomDecoder(strict=False).decode(content) + + else: # using markdown parser + parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) + + logger.debug(parsed_data) + instruct_content = output_class(**parsed_data) + return ActionOutput(content, instruct_content) + + def get(self, key): + return self.instruct_content.dict()[key] + + async def fill(self, context, llm, to="json"): + """运行这个ActionNode,并且填槽,可以采用不同策略,比如只运行子节点""" + self.llm = llm + prompt = self.compile(context=context, to=to) + mapping = self.get_children_mapping() + + class_name = f"{self.key}_AN" # 需要传入llm,并且实际在ActionNode中执行。需要规划好具体的执行方法 - raise NotImplementedError + output = await self._aask_v1(prompt, class_name, mapping, format=to) + self.content = output.content + self.instruct_content = output.instruct_content + return self def action_node_from_tuple_example(): # 示例:列表中包含元组 list_of_tuples = [ - ("key1", str, "Instruction 1", "Example 1", "Content 1", {"child1": ...}), - ("key2", int, "Instruction 2", "Example 2", "Content 2"), - ("key3", int, "Instruction 3", "Example 3") + ("key1", int, "Instruction 1", "Example 1") ] # 从列表中创建 ActionNode 实例 diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index a6d559a4c..fd58e0ca8 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -11,10 +11,10 @@ """ import json from pathlib import Path -from typing import List +# from typing import List from metagpt.actions import Action, ActionOutput -from metagpt.actions.design_api_an import DESIGN_API_NODE, SIMPLE_TEMPLATE +from metagpt.actions.design_api_an import DESIGN_API_NODE from metagpt.config import CONFIG from metagpt.const import ( DATA_API_DESIGN_FILE_REPO, @@ -26,166 +26,15 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository -from metagpt.utils.get_template import get_template +# from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file -templates = { - "json": { - "PROMPT_TEMPLATE": """ -# Context -{context} - -## Format example -{format_example} ------ -Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirement: Fill in the following missing information based on the context, each section name is a key in json - -## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select appropriate open-source frameworks. - -## Project name: Constant text. - -## File list: Provided as Python list[str], the list of files needed (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here - -## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. - -## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, -and only output the json inside this tag, nothing else -""", - "FORMAT_EXAMPLE": """ -[CONTENT] -{{ - "Implementation approach": "We will ...", - "Project name": "{project_name}", - "File list": ["main.py"], - "Data structures and interfaces": ' - classDiagram - class Game{{ - +int score - }} - ... - Game "1" -- "1" Food: has - ', - "Program call flow": ' - sequenceDiagram - participant M as Main - ... - G->>M: end game - ', - "Anything UNCLEAR": "The requirement is clear to me." -}} -[/CONTENT] -""", - }, - "markdown": { - "PROMPT_TEMPLATE": """ -# Context -{context} - -## Format example -{format_example} ------ -Role: You are an architect; the goal is to design a SOTA PEP8-compliant python system; make the best use of good open source tools -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirement: Fill in the following missing information based on the context, note that all sections are response with code form separately -ATTENTION: Output carefully referenced "Format example" in format. - -## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. - -## Project name: Constant text. - -## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here - -## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. - -## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. - -""", - "FORMAT_EXAMPLE": """ ---- -## Implementation approach -We will ... - -## Project name -```python -"{project_name}" -``` - -## File list -```python -[ - "main.py", -] -``` - -## Data structures and interfaces -```mermaid -classDiagram - class Game{ - +int score - } - ... - Game "1" -- "1" Food: has -``` - -## Program call flow -```mermaid -sequenceDiagram - participant M as Main - ... - G->>M: end game -``` - -## Anything UNCLEAR -The requirement is clear to me. ---- -""", - }, -} - -OUTPUT_MAPPING = { - "Implementation approach": (str, ...), - "Project name": (str, ...), - "File list": (List[str], ...), - "Data structures and interfaces": (str, ...), - "Program call flow": (str, ...), - "Anything UNCLEAR": (str, ...), -} - -MERGE_PROMPT = """ -## Old Design +NEW_REQ_TEMPLATE = """ +### Legacy Content {old_design} -## Context +### New Requirements {context} - ------ -Role: You are an architect; The goal is to incrementally update the "Old Design" based on the information provided by the "Context," aiming to design a SOTA PEP8-compliant python system; make the best use of good open source tools -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirement: Fill in the following missing information based on the context, note that all sections are response with code form separately -ATTENTION: Output carefully referenced "Old Design" in format. - -## Implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. - -## Project name: Constant text "{project_name}". - -## File list: Provided as Python list[str], the list of code files (including HTML & CSS IF NEEDED) to write the program. Only need relative paths. ALWAYS write a main.py or app.py here - -## Data structures and interfaces: Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions (with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design. - -## Program call flow: Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old Design" format, -and only output the json inside this tag, nothing else """ @@ -228,30 +77,16 @@ class WriteDesign(Action): # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - async def _new_system_design_bakup(self, context, format=CONFIG.prompt_format): - prompt_template, format_example = get_template(templates, format) - format_example = format_example.format(project_name=CONFIG.project_name) - prompt = prompt_template.format(context=context, format_example=format_example) - system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - return system_design - async def _new_system_design(self, context, format=CONFIG.prompt_format): - instruction, example = DESIGN_API_NODE.compile() - prompt = SIMPLE_TEMPLATE.format(context=context, example=example, instruction=instruction) - # prompt_template, format_example = get_template(templates, format) - # format_example = format_example.format(project_name=CONFIG.project_name) - # prompt = prompt_template.format(context=context, format_example=format_example) - system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - return system_design + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + return node async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): - prompt = MERGE_PROMPT.format( - old_design=system_design_doc.content, context=prd_doc.content, project_name=CONFIG.project_name + context = NEW_REQ_TEMPLATE.format( + old_design=system_design_doc.content, context=prd_doc.content ) - system_design = await self._aask_v1(prompt, "system_design", OUTPUT_MAPPING, format=format) - # fix Python package name, we can't system_design.instruct_content.python_package_name = "xxx" since "Python - # package name" contain space, have to use setattr - system_design_doc.content = system_design.instruct_content.json(ensure_ascii=False) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + system_design_doc.content = node.instruct_content.json(ensure_ascii=False) return system_design_doc async def _update_system_design(self, filename, prds_file_repo, system_design_file_repo) -> Document: diff --git a/metagpt/actions/design_api_an.py b/metagpt/actions/design_api_an.py index b4bd54849..2db203606 100644 --- a/metagpt/actions/design_api_an.py +++ b/metagpt/actions/design_api_an.py @@ -6,6 +6,7 @@ @File : design_api_an.py """ from metagpt.actions.action_node import ActionNode +from metagpt.utils.mermaid import MMC1, MMC2 from metagpt.logs import logger IMPLEMENTATION_APPROACH = ActionNode( @@ -32,60 +33,10 @@ FILE_LIST = ActionNode( DATA_STRUCTURES_AND_INTERFACES = ActionNode( key="Data structures and interfaces", expected_type=str, - instruction="Use mermaid classDiagram code syntax, including classes (INCLUDING __init__ method) and functions " - "(with type annotations), CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " + instruction="Use mermaid classDiagram code syntax, including classes, method(__init__ etc.) and functions with type" + " annotations, CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", - example=""" classDiagram - class User { - +int id - +str username - +str email - +str password - __init__(id: int, username: str, email: str, password: str) - follow(user: User): void - like(content: Content): void - comment(content: Content, text: str): Comment - } - class Content { - +int id - +User author - +str title - +str body - +datetime created_at - +list likes - +list comments - __init__(id: int, author: User, title: str, body: str) - get_likes(): list - get_comments(): list - } - class Comment { - +int id - +User author - +str text - +datetime created_at - __init__(id: int, author: User, text: str) - } - class Leaderboard { - +list top_contents - update(): void - } - class SearchEngine { - +str query - search(): list - } - class RecommendationEngine { - +User user - recommend(): list - } - class TaskQueue { - +str task_name - enqueue(task: function): void - } - User "1" -- "*" Content: creates - Content "1" -- "*" Comment: includes - User "1" -- "*" Comment: writes - User "1" -- "*" User: follows - Content "1" -- "*" User: liked_by""" + example=MMC1 ) PROGRAM_CALL_FLOW = ActionNode( @@ -93,10 +44,7 @@ PROGRAM_CALL_FLOW = ActionNode( expected_type=str, instruction="Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE " "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", - example="""sequenceDiagram -participant M as Main -... -G->>M: end game""" + example=MMC2 ) ANYTHING_UNCLEAR = ActionNode( @@ -106,40 +54,21 @@ ANYTHING_UNCLEAR = ActionNode( example="Clarification needed on third-party API integration, ..." ) -ACTION_NODES = [ +NODES = [ IMPLEMENTATION_APPROACH, - PROJECT_NAME, + # PROJECT_NAME, FILE_LIST, DATA_STRUCTURES_AND_INTERFACES, PROGRAM_CALL_FLOW, ANYTHING_UNCLEAR ] -DESIGN_API_NODE = ActionNode("DesignAPI", str, "", "") -DESIGN_API_NODE.add_children(ACTION_NODES) - -SIMPLE_TEMPLATE = """ -## context -{context} - -## example -{example} - -## instruction-nodes: ": # " -{instruction} - -## instruction-action -Role: You are an architect; the goal is to design a SOTA software system -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirement: Fill in the above missing instruction-nodes based on the context -now, output wrapped inside [CONTENT][/CONTENT] as example, nothing else. -""" +DESIGN_API_NODE = ActionNode.from_children("DesignAPI", NODES) def main(): - instruction, example = DESIGN_API_NODE.compile() - text = SIMPLE_TEMPLATE.format(context="", example=example, instruction=instruction) - logger.info(text) + prompt = DESIGN_API_NODE.compile(context="") + logger.info(prompt) if __name__ == '__main__': diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 95da0d65a..29e3bed3e 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -10,10 +10,11 @@ 3. According to the design in Section 2.2.3.5.4 of RFC 135, add incremental iteration functionality. """ import json -from typing import List +# from typing import List from metagpt.actions import ActionOutput from metagpt.actions.action import Action +from metagpt.actions.project_management_an import PM_NODE from metagpt.config import CONFIG from metagpt.const import ( PACKAGE_REQUIREMENTS_FILENAME, @@ -24,189 +25,14 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository -from metagpt.utils.get_template import get_template +# from metagpt.utils.get_template import get_template -templates = { - "json": { - "PROMPT_TEMPLATE": """ -# Context -{context} - -## Format example -{format_example} ------ -Role: You are a project manager; the goal is to break down tasks according to PRD/technical design, give a task list, and analyze task dependencies to start with the prerequisite modules -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them -ATTENTION: Output carefully referenced "Format example" in format. - -## Required Python third-party packages: Provide Python list[str] in requirements.txt format - -## Required Other language third-party packages: Provide Python list[str] in requirements.txt format - -## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first - -## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first - -## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. - -## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, -and only output the json inside this tag, nothing else -""", - "FORMAT_EXAMPLE": ''' -{ - "Required Python third-party packages": [ - "flask==1.1.2", - "bcrypt==3.2.0" - ], - "Required Other language third-party packages": [ - "No third-party ..." - ], - "Logic Analysis": [ - ["game.py", "Contains..."] - ], - "Task list": [ - "game.py" - ], - "Full API spec": """ - openapi: 3.0.0 - ... - description: A JSON object ... - """, - "Shared Knowledge": """ - 'game.py' contains ... - """, - "Anything UNCLEAR": "We need ... how to start." -} -''', - }, - "markdown": { - "PROMPT_TEMPLATE": """ -# Context -{context} - -## Format example -{format_example} ------ -Role: You are a project manager; the goal is to break down tasks according to PRD/technical design, give a task list, and analyze task dependencies to start with the prerequisite modules -Requirements: Based on the context, fill in the following missing information, note that all sections are returned in Python code triple quote form seperatedly. Here the granularity of the task is a file, if there are any missing files, you can supplement them -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. - -## Required Python third-party packages: Provided in requirements.txt format - -## Required Other language third-party packages: Provided in requirements.txt format - -## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first - -## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first - -## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. - -## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. - -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. For example, don't forget a main entry. don't forget to init 3rd party libs. - -""", - "FORMAT_EXAMPLE": ''' ---- -## Required Python third-party packages -```python -""" -flask==1.1.2 -bcrypt==3.2.0 -""" -``` - -## Required Other language third-party packages -```python -""" -No third-party ... -""" -``` - -## Full API spec -```python -""" -openapi: 3.0.0 -... -description: A JSON object ... -""" -``` - -## Logic Analysis -```python -[ - ["index.js", "Contains ..."], - ["main.py", "Contains ..."], -] -``` - -## Task list -```python -[ - "index.js", - "main.py", -] -``` - -## Shared Knowledge -```python -""" -'game.py' contains ... -""" -``` - -## Anything UNCLEAR -We need ... how to start. ---- -''', - }, -} -OUTPUT_MAPPING = { - "Required Python third-party packages": (List[str], ...), - "Required Other language third-party packages": (List[str], ...), - "Full API spec": (str, ...), - "Logic Analysis": (List[List[str]], ...), - "Task list": (List[str], ...), - "Shared Knowledge": (str, ...), - "Anything UNCLEAR": (str, ...), -} - -MERGE_PROMPT = """ -# Context -{context} - -## Old Tasks +NEW_REQ_TEMPLATE = """ +### Legacy Content {old_tasks} ------ -## Format example -{format_example} ------ -Role: You are a project manager; The goal is to merge the new PRD/technical design content from 'Context' into 'Old Tasks.' Based on this merged result, break down tasks, give a task list, and analyze task dependencies to start with the prerequisite modules. -Requirements: Based on the context, fill in the following missing information, each section name is a key in json. Here the granularity of the task is a file, if there are any missing files, you can supplement them -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. - -## Required Python third-party packages: Provided in requirements.txt format - -## Required Other language third-party packages: Provided in requirements.txt format - -## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend. - -## Logic Analysis: Provided as a Python list[list[str]. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first - -## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first - -## Shared Knowledge: Anything that should be public like utils' functions, config's variables details that should make clear first. - -## Anything UNCLEAR: Provide as Plain text. Make clear here. For example, don't forget a main entry. don't forget to init 3rd party libs. - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Format example" format, -and only output the json inside this tag, nothing else +### New Requirements +{context} """ @@ -262,18 +88,16 @@ class WriteTasks(Action): return task_doc async def _run_new_tasks(self, context, format=CONFIG.prompt_format): - prompt_template, format_example = get_template(templates, format) - prompt = prompt_template.format(context=context, format_example=format_example) - rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) - return rsp + node = await PM_NODE.fill(context, self.llm, format) + # prompt_template, format_example = get_template(templates, format) + # prompt = prompt_template.format(context=context, format_example=format_example) + # rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) + return node async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: - _, format_example = get_template(templates, format) - prompt = MERGE_PROMPT.format(context=system_design_doc.content, old_tasks=task_doc.content, - format_example=format_example) - rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) - task_doc.content = rsp.instruct_content.json(ensure_ascii=False) - return task_doc + context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content) + node = await PM_NODE.fill(context, self.llm, format) + return node @staticmethod async def _update_requirements(doc): diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py new file mode 100644 index 000000000..aa7cdcde2 --- /dev/null +++ b/metagpt/actions/project_management_an.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/14 15:28 +@Author : alexanderwu +@File : project_management_an.py +""" +from metagpt.actions.action_node import ActionNode +from metagpt.logs import logger + +REQUIRED_PYTHON_PACKAGES = ActionNode( + key="Required Python packages", + expected_type=list[str], + instruction="Provide required Python packages in requirements.txt format.", + example=["flask==1.1.2", "bcrypt==3.2.0"] +) + +REQUIRED_OTHER_LANGUAGE_PACKAGES = ActionNode( + key="Required Other language third-party packages", + expected_type=list[str], + instruction="List down the required packages for languages other than Python.", + example=["No third-party dependencies required"] +) + +LOGIC_ANALYSIS = ActionNode( + key="Logic Analysis", + expected_type=list[list[str]], + instruction="Provide a list of files with the classes/methods/functions to be implemented, " + "including dependency analysis and imports.", + example=[["game.py", "Contains Game class and ... functions"], + ["main.py", "Contains main function, from game import Game"]] +) + +TASK_LIST = ActionNode( + key="Task list", + expected_type=list[str], + instruction="Break down the tasks into a list of filenames, prioritized by dependency order.", + example=["game.py", "main.py"] +) + +FULL_API_SPEC = ActionNode( + key="Full API spec", + expected_type=str, + instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend.", + example="openapi: 3.0.0 ..." +) + +SHARED_KNOWLEDGE = ActionNode( + key="Shared Knowledge", + expected_type=str, + instruction="Detail any shared knowledge, like common utility functions or configuration variables.", + example="'game.py' contains functions shared across the project." +) + +ANYTHING_UNCLEAR_PM = ActionNode( + key="Anything UNCLEAR", + expected_type=str, + instruction="Mention any unclear aspects in the project management context and try to clarify them.", + example="Clarification needed on how to start and initialize third-party libraries." +) + +NODES = [ + REQUIRED_PYTHON_PACKAGES, + REQUIRED_OTHER_LANGUAGE_PACKAGES, + LOGIC_ANALYSIS, + TASK_LIST, + FULL_API_SPEC, + SHARED_KNOWLEDGE, + ANYTHING_UNCLEAR_PM +] + + +PM_NODE = ActionNode.from_children("PM_NODE", NODES) + + +def main(): + prompt = PM_NODE.compile(context="") + logger.info(prompt) + + +if __name__ == '__main__': + main() diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 0594d116e..e61743e7f 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -14,9 +14,11 @@ from __future__ import annotations import json from pathlib import Path -from typing import List +# from typing import List from metagpt.actions import Action, ActionOutput +from metagpt.actions.action_node import ActionNode +from metagpt.actions.write_prd_an import WRITE_PRD_NODE, WP_ISSUE_TYPE_NODE, WP_IS_RELATIVE_NODE from metagpt.actions.fix_bug import FixBug from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG @@ -31,293 +33,26 @@ from metagpt.logs import logger from metagpt.schema import Document, Documents, Message, BugFixContext from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository -from metagpt.utils.get_template import get_template +# from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file -templates = { - "json": { - "PROMPT_TEMPLATE": """ -# Context -{{ - "Original Requirements": "{requirements}", - "Search Information": "" -}} +CONTEXT_TEMPLATE = """ +### Project Name +{project_name} -## Format example -{format_example} ------ -Role: You are a professional product manager; the goal is to design a concise, usable, efficient product -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. -ATTENTION: Output carefully referenced "Format example" in format. - -## YOU NEED TO FULFILL THE BELOW JSON DOC - -{{ - "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. - "Original Requirements": "", # str, place the polished complete original requirements here - "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. - "Search Information": "", - "Requirements": "", - "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. - "User Stories": [], # Provided as Python list[str], up to 5 scenario-based user stories - "Competitive Analysis": [], # Provided as Python list[str], up to 8 competitive product analyses - # Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. - "Competitive Quadrant Chart": "quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - Campaign A: [0.3, 0.6] - Campaign B: [0.45, 0.23] - Campaign C: [0.57, 0.69] - Campaign D: [0.78, 0.34] - Campaign E: [0.40, 0.34] - Campaign F: [0.35, 0.78]", - "Requirement Analysis": "", # Provide as Plain text. - "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], # Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards - "UI Design draft": "", # Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. - "Anything UNCLEAR": "", # Provide as Plain text. Try to clarify it. -}} - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, -and only output the json inside this tag, nothing else -""", - "FORMAT_EXAMPLE": """ -[CONTENT] -{{ - "Language": "", - "Original Requirements": "", - "Project Name": "{project_name}", - "Search Information": "", - "Requirements": "", - "Product Goals": [], - "User Stories": [], - "Competitive Analysis": [], - "Competitive Quadrant Chart": "quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - Campaign A: [0.3, 0.6] - Campaign B: [0.45, 0.23] - Campaign C: [0.57, 0.69] - Campaign D: [0.78, 0.34] - Campaign E: [0.40, 0.34] - Campaign F: [0.35, 0.78]", - "Requirement Analysis": "", - "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], - "UI Design draft": "", - "Anything UNCLEAR": "", -}} -[/CONTENT] -""", - }, - "markdown": { - "PROMPT_TEMPLATE": """ -# Context -## Original Requirements +### Original Requirements {requirements} -## Search Information -{search_information} - -## mermaid quadrantChart code syntax example. DONT USE QUOTO IN CODE DUE TO INVALID SYNTAX. Replace the with REAL COMPETITOR NAME -```mermaid -quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - "Campaign: A": [0.3, 0.6] - "Campaign B": [0.45, 0.23] - "Campaign C": [0.57, 0.69] - "Campaign D": [0.78, 0.34] - "Campaign E": [0.40, 0.34] - "Campaign F": [0.35, 0.78] - "Our Target Product": [0.5, 0.6] -``` - -## Format example -{format_example} ------ -Role: You are a professional product manager; the goal is to design a concise, usable, efficient product -Language: Please use the same language as the user requirement to answer, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. -ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. AND '## ' SHOULD WRITE BEFORE the code and triple quote. Output carefully referenced "Format example" in format. - -## Language: Provide as Plain text, use the same language as the user requirement. - -## Original Requirements: Provide as Plain text, place the polished complete original requirements here - -## Product Goals: Provided as Python list[str], up to 3 clear, orthogonal product goals. - -## User Stories: Provided as Python list[str], up to 5 scenario-based user stories - -## Competitive Analysis: Provided as Python list[str], up to 7 competitive product analyses, consider as similar competitors as possible - -## Competitive Quadrant Chart: Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. - -## Requirement Analysis: Provide as Plain text. - -## Requirement Pool: Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards - -## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. -## Anything UNCLEAR: Provide as Plain text. Try to clarify it. -""", - "FORMAT_EXAMPLE": """ ---- -## Original Requirements -The user ... - -## Product Goals -```python -[ - "Create a ...", -] -``` - -## User Stories -```python -[ - "As a user, ...", -] -``` - -## Competitive Analysis -```python -[ - "Python Snake Game: ...", -] -``` - -## Competitive Quadrant Chart -```mermaid -quadrantChart - title Reach and engagement of campaigns - ... - "Our Target Product": [0.6, 0.7] -``` - -## Requirement Analysis -The product should be a ... - -## Requirement Pool -```python -[ - ["End game ...", "P0"] -] -``` - -## UI Design draft -Give a basic function description, and a draft - -## Anything UNCLEAR -There are no unclear points. ---- -""", - }, -} - -OUTPUT_MAPPING = { - "Language": (str, ...), - "Original Requirements": (str, ...), - "Project Name": (str, ...), - "Product Goals": (List[str], ...), - "User Stories": (List[str], ...), - "Competitive Analysis": (List[str], ...), - "Competitive Quadrant Chart": (str, ...), - "Requirement Analysis": (str, ...), - "Requirement Pool": (List[List[str]], ...), - "UI Design draft": (str, ...), - "Anything UNCLEAR": (str, ...), -} - -IS_RELATIVE_PROMPT = """ -## PRD: -{old_prd} - -## New Requirement: -{requirements} - -___ -You are a professional product manager; You need to assess whether the new requirements are relevant to the existing PRD to determine whether to merge the new requirements into this PRD. -Is the newly added requirement in "New Requirement" related to the PRD? -Respond with `YES` if it is related, `NO` if it is not, and provide the reasons. Return the response in JSON format. +### Search Information +- """ -MERGE_PROMPT = """ -# Context -## Original Requirements -{requirements} - - -## Old PRD +NEW_REQ_TEMPLATE = """ +### Legacy Content {old_prd} ------ -Role: You are a professional product manager; The goal is to incorporate the newly added requirements from the "Original Requirements" into the existing Product Requirements Document (PRD) in the "Old PRD" in order to design a concise, usable, and efficient product. -Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. -Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design -ATTENTION: Output carefully referenced "Old PRD" in format. -## YOU NEED TO FULFILL THE BELOW JSON DOC - -{{ - "Language": "", # str, use the same language as the user requirement. en_us / zh_cn etc. - "Original Requirements": "", # str, place the polished complete original requirements here - "Project Name": "{project_name}", # str, if it's empty, name it with snake case style, like game_2048 / web_2048 / simple_crm etc. - "Search Information": "", - "Requirements": "", - "Product Goals": [], # Provided as Python list[str], up to 3 clear, orthogonal product goals. - "User Stories": [], # Provided as Python list[str], up to 5 scenario-based user stories - "Competitive Analysis": [], # Provided as Python list[str], up to 8 competitive product analyses - # Use mermaid quadrantChart code syntax. up to 14 competitive products. Translation: Distribute these competitor scores evenly between 0 and 1, trying to conform to a normal distribution centered around 0.5 as much as possible. - "Competitive Quadrant Chart": "quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - Campaign A: [0.3, 0.6] - Campaign B: [0.45, 0.23] - Campaign C: [0.57, 0.69] - Campaign D: [0.78, 0.34] - Campaign E: [0.40, 0.34] - Campaign F: [0.35, 0.78]", - "Requirement Analysis": "", # Provide as Plain text. - "Requirement Pool": [["P0","P0 requirement"],["P1","P1 requirement"]], # Provided as Python list[list[str], the parameters are requirement description, priority(P0/P1/P2), respectively, comply with PEP standards - "UI Design draft": "", # Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. - "Anything UNCLEAR": "", # Provide as Plain text. Try to clarify it. -}} - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like "Old PRD" format, -and only output the json inside this tag, nothing else -""" - -IS_BUGFIX_PROMPT = """ -{content} - -___ -You are a professional product manager; You need to determine whether the above content describes a requirement or provides feedback about a bug. -Respond with `YES` if it is a feedback about a bug, `NO` if it is not, and provide the reasons. Return the response in JSON format like below: - -```json -{{ - "is_bugfix": ..., # `YES` or `NO` - "reason": ..., # reason string -}} -``` +### New Requirements +{requirements} """ @@ -335,7 +70,7 @@ class WritePRD(Action): await docs_file_repo.save(filename=REQUIREMENT_FILENAME, content="") bug_fix = BugFixContext(filename=BUGFIX_FILENAME) return Message(content=bug_fix.json(), instruct_content=bug_fix, - role=self.profile, + role="", cause_by=FixBug, sent_from=self, send_to="Alex", # the name of Engineer @@ -353,7 +88,7 @@ class WritePRD(Action): if not prd_doc: continue change_files.docs[prd_doc.filename] = prd_doc - logger.info(f"REWRITE PRD:{prd_doc.filename}") + logger.info(f"rewrite prd: {prd_doc.filename}") # If there is no existing PRD, generate one using 'docs/requirement.txt'. if not change_files.docs: prd_doc = await self._update_prd( @@ -367,47 +102,32 @@ class WritePRD(Action): # optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) - async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput: - sas = SearchAndSummarize() - # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) - rsp = "" - info = f"### Search Results\n{sas.result}\n\n### Search Summary\n{rsp}" - if sas.result: - logger.info(sas.result) - logger.info(rsp) - - # logger.info(format) - prompt_template, format_example = get_template(templates, format) + async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format) -> ActionOutput: + # sas = SearchAndSummarize() + # # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) + # rsp = "" + # info = f"### Search Results\n{sas.result}\n\n### Search Summary\n{rsp}" + # if sas.result: + # logger.info(sas.result) + # logger.info(rsp) project_name = CONFIG.project_name if CONFIG.project_name else "" - format_example = format_example.format(project_name=project_name) - # logger.info(prompt_template) - # logger.info(format_example) - prompt = prompt_template.format( - requirements=requirements, search_information=info, format_example=format_example, project_name=project_name - ) - # logger.info(prompt) - # prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING) - prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) - await self._rename_workspace(prd) - return prd + context = CONTEXT_TEMPLATE.format(requirements=requirements, project_name=project_name) + node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=format) + await self._rename_workspace(node) + return node - async def _is_relative_to(self, new_requirement_doc, old_prd_doc) -> bool: - prompt = IS_RELATIVE_PROMPT.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) - res = await self._aask(prompt=prompt) - logger.info(f"REQ-RELATIVE: [{new_requirement_doc.root_relative_path}, {old_prd_doc.root_relative_path}]: {res}") - if "YES" in res: - return True - return False + async def _is_relative(self, new_requirement_doc, old_prd_doc) -> bool: + context = NEW_REQ_TEMPLATE.format(old_prd=old_prd_doc.content, requirements=new_requirement_doc.content) + node = await WP_IS_RELATIVE_NODE.fill(context, self.llm) + return node.get("is_relative") == "YES" async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: if not CONFIG.project_name: CONFIG.project_name = Path(CONFIG.project_path).name - prompt = MERGE_PROMPT.format( - requirements=new_requirement_doc.content, old_prd=prd_doc.content, project_name=CONFIG.project_name - ) - prd = await self._aask_v1(prompt, "prd", OUTPUT_MAPPING, format=format) - prd_doc.content = prd.instruct_content.json(ensure_ascii=False) - await self._rename_workspace(prd) + prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) + node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=format) + prd_doc.content = node.instruct_content.json(ensure_ascii=False) + await self._rename_workspace(node) return prd_doc async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: @@ -418,7 +138,7 @@ class WritePRD(Action): filename=FileRepository.new_filename() + ".json", content=prd.instruct_content.json(ensure_ascii=False), ) - elif await self._is_relative_to(requirement_doc, prd_doc): + elif await self._is_relative(requirement_doc, prd_doc): new_prd_doc = await self._merge(requirement_doc, prd_doc) else: return None @@ -453,17 +173,13 @@ class WritePRD(Action): return if not CONFIG.project_name: - if isinstance(prd, ActionOutput): + if isinstance(prd, ActionOutput) or isinstance(prd, ActionNode): ws_name = prd.instruct_content.dict()["Project Name"] else: ws_name = CodeParser.parse_str(block="Project Name", text=prd) CONFIG.project_name = ws_name CONFIG.git_repo.rename_root(CONFIG.project_name) - async def _is_bugfix(self, content): - prompt = IS_BUGFIX_PROMPT.format(content=content) - res = await self._aask(prompt=prompt) - logger.info(f"IS_BUGFIX:{res}") - if "YES" in res: - return True - return False + async def _is_bugfix(self, context) -> bool: + node = await WP_ISSUE_TYPE_NODE.fill(context, self.llm) + return node.get("issue_type") == "BUG" diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py new file mode 100644 index 000000000..7368621ea --- /dev/null +++ b/metagpt/actions/write_prd_an.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/14 11:40 +@Author : alexanderwu +@File : write_prd_an.py +""" + +from metagpt.actions.action_node import ActionNode +from metagpt.logs import logger + +LANGUAGE = ActionNode( + key="Language", + expected_type=str, + instruction="Provide the language used in the project, typically matching the user's requirement language.", + example="en_us" +) + +ORIGINAL_REQUIREMENTS = ActionNode( + key="Original Requirements", + expected_type=str, + instruction="Place the polished, complete original requirements here.", + example="The game should have a leaderboard and multiple difficulty levels." +) + +PROJECT_NAME = ActionNode( + key="Project Name", + expected_type=str, + instruction="Name the project using snake case style, like 'game_2048' or 'simple_crm'.", + example="game_2048" +) + +PRODUCT_GOALS = ActionNode( + key="Product Goals", + expected_type=list[str], + instruction="Provide up to three clear, orthogonal product goals.", + example=["Create an engaging user experience", + "Ensure high performance", + "Provide customizable features"] +) + +USER_STORIES = ActionNode( + key="User Stories", + expected_type=list[str], + instruction="Provide up to five scenario-based user stories.", + example=["As a user, I want to be able to choose difficulty levels", + "As a player, I want to see my score after each game"] +) + +COMPETITIVE_ANALYSIS = ActionNode( + key="Competitive Analysis", + expected_type=list[str], + instruction="Provide analyses for up to seven competitive products.", + example=["Python Snake Game: Simple interface, lacks advanced features"] +) + +COMPETITIVE_QUADRANT_CHART = ActionNode( + key="Competitive Quadrant Chart", + expected_type=str, + instruction="Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1", + example="""quadrantChart + title Reach and engagement of campaigns + x-axis Low Reach --> High Reach + y-axis Low Engagement --> High Engagement + quadrant-1 We should expand + quadrant-2 Need to promote + quadrant-3 Re-evaluate + quadrant-4 May be improved + "Campaign: A": [0.3, 0.6] + "Campaign B": [0.45, 0.23] + "Campaign C": [0.57, 0.69] + "Campaign D": [0.78, 0.34] + "Campaign E": [0.40, 0.34] + "Campaign F": [0.35, 0.78] + "Our Target Product": [0.5, 0.6]""" +) + +REQUIREMENT_ANALYSIS = ActionNode( + key="Requirement Analysis", + expected_type=str, + instruction="Provide a detailed analysis of the requirements.", + example="The product should be user-friendly and performance-optimized." +) + +REQUIREMENT_POOL = ActionNode( + key="Requirement Pool", + expected_type=list[list[str]], + instruction="List down the requirements with their priority (P0, P1, P2).", + example=[["P0", "High priority requirement"], ["P1", "Medium priority requirement"]] +) + +UI_DESIGN_DRAFT = ActionNode( + key="UI Design draft", + expected_type=str, + instruction="Provide a simple description of UI elements, functions, style, and layout.", + example="Basic function description with a simple style and layout." +) + +ANYTHING_UNCLEAR = ActionNode( + key="Anything UNCLEAR", + expected_type=str, + instruction="Mention any aspects of the project that are unclear and try to clarify them.", + example="..." +) + +ISSUE_TYPE = ActionNode( + key="issue_type", + expected_type=str, + instruction="Answer BUG/REQUIREMENT. If it is a bugfix, answer Bug, otherwise answer Requirement", + example="BUG" +) + +IS_RELATIVE = ActionNode( + key="is_relative", + expected_type=str, + instruction="Answer YES/NO. If the requirement is related to the old PRD, answer YES, otherwise NO", + example="YES" +) + +REASON = ActionNode( + key="reason", + expected_type=str, + instruction="Explain the reasoning process from question to answer", + example="..." +) + + +NODES = [ + LANGUAGE, + ORIGINAL_REQUIREMENTS, + PROJECT_NAME, + PRODUCT_GOALS, + USER_STORIES, + COMPETITIVE_ANALYSIS, + COMPETITIVE_QUADRANT_CHART, + REQUIREMENT_ANALYSIS, + REQUIREMENT_POOL, + UI_DESIGN_DRAFT, + ANYTHING_UNCLEAR +] + +WRITE_PRD_NODE = ActionNode.from_children("WritePRD", NODES) +WP_ISSUE_TYPE_NODE = ActionNode.from_children("WP_ISSUE_TYPE", [ISSUE_TYPE, REASON]) +WP_IS_RELATIVE_NODE = ActionNode.from_children("WP_IS_RELATIVE", [IS_RELATIVE, REASON]) + + +def main(): + prompt = WRITE_PRD_NODE.compile(context="") + logger.info(prompt) + + +if __name__ == '__main__': + main() diff --git a/metagpt/environment.py b/metagpt/environment.py index 7d1e307f3..89b6f9d46 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -82,7 +82,7 @@ class Environment(BaseModel): futures.append(future) await asyncio.gather(*futures) - logger.info(f"is idle: {self.is_idle}") + logger.debug(f"is idle: {self.is_idle}") def get_roles(self) -> dict[str, Role]: """获得环境内的所有角色 diff --git a/metagpt/llm.py b/metagpt/llm.py index d8d06c0a1..eaa4880a5 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -12,11 +12,12 @@ from metagpt.provider.human_provider import HumanProvider from metagpt.provider.openai_api import OpenAIGPTAPI from metagpt.provider.spark_api import SparkAPI from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI +from metagpt.provider.base_gpt_api import BaseGPTAPI _ = HumanProvider() # Avoid pre-commit error -def LLM() -> "BaseGPTAPI": +def LLM() -> BaseGPTAPI: """initialize different LLM instance according to the key field existence""" # TODO a little trick, can use registry to initialize LLM instance further if CONFIG.openai_api_key: diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index 15d5fe5b1..b80ef85be 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -26,8 +26,8 @@ class Architect(Role): self, name: str = "Bob", profile: str = "Architect", - goal: str = "Design a concise, usable, complete python system", - constraints: str = "Try to specify good open source tools as much as possible", + goal: str = "design a concise, usable, complete software system", + constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries" ) -> None: """Initializes the Architect with given attributes.""" super().__init__(name, profile, goal, constraints) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index cedd2101f..844f3589d 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -71,14 +71,15 @@ class Engineer(Role): self, name: str = "Alex", profile: str = "Engineer", - goal: str = "Write elegant, readable, extensible, efficient code", - constraints: str = "The code should conform to standards like PEP8 and be modular and maintainable", + goal: str = "write elegant, readable, extensible, efficient code", + constraints: str = "the code should conform to standards like PEP8 and be modular and maintainable", n_borg: int = 1, use_code_review: bool = False, ) -> None: """Initializes the Engineer role with given attributes.""" super().__init__(name, profile, goal, constraints) self.use_code_review = use_code_review + self._init_actions([WriteCode]) self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug]) self.code_todos = [] self.summarize_todos = [] @@ -198,11 +199,11 @@ class Engineer(Role): return None msg = self._rc.news[0] if msg.cause_by in write_code_filters: - logger.info(f"TODO WriteCode:{msg.json()}") + logger.debug(f"TODO WriteCode:{msg.json()}") await self._new_code_actions(bug_fix=msg.cause_by == any_to_str(FixBug)) return self._rc.todo if msg.cause_by in summarize_code_filters and msg.sent_from == any_to_str(self): - logger.info(f"TODO SummarizeCode:{msg.json()}") + logger.debug(f"TODO SummarizeCode:{msg.json()}") await self._new_summarize_actions() return self._rc.todo return None diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 7e7c5699d..37090b24f 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -25,7 +25,8 @@ class ProjectManager(Role): self, name: str = "Eve", profile: str = "Project Manager", - goal: str = "Improve team efficiency and deliver with quality and quantity", + goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " + "dependencies to start with the prerequisite modules", constraints: str = "", ) -> None: """ diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 52ac3cf28..7c9341adb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -26,6 +26,7 @@ from typing import Iterable, Set, Type from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.config import CONFIG from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger @@ -156,7 +157,7 @@ class Role: f"as Role's {str(action)} was initialized using LLM, try passing in Action classes instead of initialized instances" ) i = action - i.set_env(self._rc.env) + # i.set_env(self._rc.env) i.set_prefix(self._get_prefix(), self.profile) self._actions.append(i) self._states.append(f"{idx}. {action}") @@ -278,7 +279,7 @@ class Role: async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) - if isinstance(response, ActionOutput): + if isinstance(response, ActionOutput) or isinstance(response, ActionNode): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index bee8d3986..5760202ff 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -8,6 +8,7 @@ the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ from metagpt.actions import ActionOutput, SearchAndSummarize +from metagpt.actions.action_node import ActionNode from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message @@ -58,7 +59,7 @@ class Searcher(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - if isinstance(response, ActionOutput): + if isinstance(response, ActionOutput) or isinstance(response, ActionNode): msg = Message( content=response.content, instruct_content=response.instruct_content, From 39cb66359505edef07b9e1fb5f5c1f341372bcec Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 16:21:56 +0800 Subject: [PATCH 263/413] fix typo --- metagpt/actions/action_node.py | 2 +- metagpt/actions/write_prd_an.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 178986ebe..96c175ccb 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -285,7 +285,7 @@ class ActionNode: def action_node_from_tuple_example(): # 示例:列表中包含元组 list_of_tuples = [ - ("key1", int, "Instruction 1", "Example 1") + ("key1", str, "Instruction 1", "Example 1") ] # 从列表中创建 ActionNode 实例 diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 7368621ea..0781760ba 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -106,7 +106,7 @@ ANYTHING_UNCLEAR = ActionNode( ISSUE_TYPE = ActionNode( key="issue_type", expected_type=str, - instruction="Answer BUG/REQUIREMENT. If it is a bugfix, answer Bug, otherwise answer Requirement", + instruction="Answer BUG/REQUIREMENT. If it is a bugfix, answer BUG, otherwise answer Requirement", example="BUG" ) From 609d75a07eba441dcba4c3c2ea0644f9836f6d5a Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 18:06:43 +0800 Subject: [PATCH 264/413] add programming language as input, add complex strgy to ActionNode.fill method, fix quadrantChart in chinese etc. --- metagpt/actions/action_node.py | 74 ++++++++++++++++++++---- metagpt/actions/project_management_an.py | 2 +- metagpt/actions/write_prd_an.py | 24 +++++--- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 96c175ccb..b1fbdaae9 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -28,6 +28,8 @@ SIMPLE_TEMPLATE = """ ## context {context} +----- + ## format example {example} @@ -38,7 +40,7 @@ SIMPLE_TEMPLATE = """ {constraint} ## action -Fill in the above nodes based on the context. Answer in format example. +Fill in the above nodes based on the format example. """ @@ -108,6 +110,16 @@ class ActionNode: """获得子ActionNode的字典,以key索引""" return {k: (v.expected_type, ...) for k, v in self.children.items()} + def get_self_mapping(self) -> Dict[str, Type]: + """get self key: type mapping""" + return {self.key: (self.expected_type, ...)} + + def get_mapping(self, mode="children") -> Dict[str, Type]: + """get key: type mapping under mode""" + if mode == "children" or (mode=="auto" and self.children): + return self.get_children_mapping() + return self.get_self_mapping() + @classmethod def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): """基于pydantic v1的模型动态生成,用来检验结果类型正确性""" @@ -160,8 +172,8 @@ class ActionNode: mapping = self.get_children_mapping() return self.create_model_class(class_name, mapping) - def to_dict(self, format_func=None, mode="all") -> Dict: - """将当前节点与子节点都按照node: format的格式组织称字典""" + def to_dict(self, format_func=None, mode="auto") -> Dict: + """将当前节点与子节点都按照node: format的格式组织成字典""" # 如果没有提供格式化函数,使用默认的格式化方式 if format_func is None: @@ -171,7 +183,7 @@ class ActionNode: formatted_value = format_func(self) # 创建当前节点的键值对 - if mode == "children": + if mode == "children" or (mode == "auto" and self.children): node_dict = {} else: node_dict = {self.key: formatted_value} @@ -227,7 +239,7 @@ class ActionNode: mode="root": NotImplemented """ - # FIXME: json instruction会带来 "Project name": "web_2048 # 项目名称使用下划线", + # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", self.instruction = self.compile_instruction(to="markdown", mode=mode) self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) prompt = template.format(context=context, example=self.example, instruction=self.instruction, @@ -268,19 +280,59 @@ class ActionNode: def get(self, key): return self.instruct_content.dict()[key] - async def fill(self, context, llm, to="json"): - """运行这个ActionNode,并且填槽,可以采用不同策略,比如只运行子节点""" - self.llm = llm - prompt = self.compile(context=context, to=to) - mapping = self.get_children_mapping() + def set_recursive(self, name, value): + setattr(self, name, value) + for _, i in self.children.items(): + i.set_recursive(name, value) + + def set_llm(self, llm): + self.set_recursive("llm", llm) + + def set_context(self, context): + self.set_recursive("context", context) + + async def simple_fill(self, to, mode): + prompt = self.compile(context=self.context, to=to, mode=mode) + mapping = self.get_mapping(mode) class_name = f"{self.key}_AN" - # 需要传入llm,并且实际在ActionNode中执行。需要规划好具体的执行方法 output = await self._aask_v1(prompt, class_name, mapping, format=to) self.content = output.content self.instruct_content = output.instruct_content return self + async def fill(self, context, llm, to="json", mode="auto", strgy="simple"): + """ Fill the node(s) with mode. + + :param context: Everything we should know when filling node. + :param llm: Large Language Model with pre-defined system message. + :param to: json/markdown, determine example and output format. + - json: it's easy to open source LLM with json format + - markdown: when generating code, markdown is always better + :param mode: auto/children/root + - auto: automated fill children's nodes and gather outputs, if no children, fill itself + - children: fill children's nodes and gather outputs + - root: fill root's node and gather output + :param strgy: simple/complex + - simple: run only once + - complex: run each node + :return: self + """ + self.set_llm(llm) + self.set_context(context) + + if strgy == "simple": + return await self.simple_fill(to, mode) + elif strgy == "complex": + # 这里隐式假设了拥有children + tmp = {} + for _, i in self.children.items(): + child = await i.simple_fill(to, mode) + tmp.update(child.instruct_content.dict()) + cls = self.create_children_class() + self.instruct_content = cls(**tmp) + return self + def action_node_from_tuple_example(): # 示例:列表中包含元组 diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index aa7cdcde2..9849cb7b3 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -28,7 +28,7 @@ LOGIC_ANALYSIS = ActionNode( instruction="Provide a list of files with the classes/methods/functions to be implemented, " "including dependency analysis and imports.", example=[["game.py", "Contains Game class and ... functions"], - ["main.py", "Contains main function, from game import Game"]] + ["main.py", "Contains main function, depends on game.py"]] ) TASK_LIST = ActionNode( diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 0781760ba..cbcf920b9 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -16,6 +16,13 @@ LANGUAGE = ActionNode( example="en_us" ) +PROGRAMMING_LANGUAGE = ActionNode( + key="Programming Language", + expected_type=str, + instruction="Python/JavaScript or other mainstream programming language.", + example="Python" +) + ORIGINAL_REQUIREMENTS = ActionNode( key="Original Requirements", expected_type=str, @@ -59,14 +66,14 @@ COMPETITIVE_QUADRANT_CHART = ActionNode( expected_type=str, instruction="Use mermaid quadrantChart syntax. Distribute scores evenly between 0 and 1", example="""quadrantChart - title Reach and engagement of campaigns - x-axis Low Reach --> High Reach - y-axis Low Engagement --> High Engagement - quadrant-1 We should expand - quadrant-2 Need to promote - quadrant-3 Re-evaluate - quadrant-4 May be improved - "Campaign: A": [0.3, 0.6] + title "Reach and engagement of campaigns" + x-axis "Low Reach" --> "High Reach" + y-axis "Low Engagement" --> "High Engagement" + quadrant-1 "We should expand" + quadrant-2 "Need to promote" + quadrant-3 "Re-evaluate" + quadrant-4 "May be improved" + "Campaign A": [0.3, 0.6] "Campaign B": [0.45, 0.23] "Campaign C": [0.57, 0.69] "Campaign D": [0.78, 0.34] @@ -127,6 +134,7 @@ REASON = ActionNode( NODES = [ LANGUAGE, + PROGRAMMING_LANGUAGE, ORIGINAL_REQUIREMENTS, PROJECT_NAME, PRODUCT_GOALS, From 290fb8b8d053a4d1441ac64fff60550f0b9e18e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 14 Dec 2023 20:44:27 +0800 Subject: [PATCH 265/413] refactor: format --- .gitignore | 1 + metagpt/actions/action_node.py | 31 ++- metagpt/actions/design_api_an.py | 27 +-- metagpt/actions/project_management.py | 7 +- metagpt/actions/project_management_an.py | 24 +- metagpt/actions/write_prd_an.py | 41 ++-- metagpt/provider/fireworks_api.py | 3 +- metagpt/provider/open_llm_api.py | 7 +- .../postprecess/base_postprecess_plugin.py | 22 +- .../postprecess/llm_output_postprecess.py | 11 +- metagpt/roles/architect.py | 2 +- metagpt/roles/project_manager.py | 2 +- metagpt/roles/qa_engineer.py | 4 +- metagpt/schema.py | 16 +- metagpt/utils/ahttp_client.py | 56 ++--- metagpt/utils/git_repository.py | 9 +- metagpt/utils/repair_llm_raw_output.py | 31 +-- metagpt/utils/utils.py | 11 +- tests/metagpt/test_llm.py | 1 + tests/metagpt/utils/test_ahttp_client.py | 17 +- .../utils/test_repair_llm_raw_output.py | 223 +++++++++--------- 21 files changed, 262 insertions(+), 284 deletions(-) diff --git a/.gitignore b/.gitignore index e03eab3d3..0ac318ff5 100644 --- a/.gitignore +++ b/.gitignore @@ -59,6 +59,7 @@ cover/ # Django stuff: *.log +logs local_settings.py db.sqlite3 db.sqlite3-journal diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 96c175ccb..ae40913e0 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -5,13 +5,12 @@ @Author : alexanderwu @File : action_node.py """ -import re -from typing import Dict, Type, List, Any, Tuple, Optional import json +import re +from typing import Any, Dict, List, Optional, Type from pydantic import BaseModel, create_model, root_validator, validator -# , model_validator, field_validator -from tenacity import wait_random_exponential, stop_after_attempt, retry +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions import ActionOutput from metagpt.llm import BaseGPTAPI @@ -51,6 +50,7 @@ def dict_to_markdown(d, prefix="-", postfix="\n"): class ActionNode: """ActionNode is a tree of nodes.""" + # Action Strgy # - sop: 仅使用一级SOP # - complex: 使用一级SOP+自定义策略填槽 @@ -72,8 +72,7 @@ class ActionNode: content: str instruct_content: BaseModel - def __init__(self, key, expected_type, instruction, example, content="", - children=None): + def __init__(self, key, expected_type, instruction, example, content="", children=None): self.key = key self.expected_type = expected_type self.instruction = instruction @@ -82,8 +81,9 @@ class ActionNode: self.children = children if children is not None else {} def __str__(self): - return f"{self.key}, {self.expected_type}, {self.instruction}, {self.example}" \ - f", {self.content}, {self.children}" + return ( + f"{self.key}, {self.expected_type}, {self.instruction}, {self.example}" f", {self.content}, {self.children}" + ) def __repr__(self): return self.__str__() @@ -136,7 +136,7 @@ class ActionNode: """基于pydantic v2的模型动态生成,用来检验结果类型正确性,待验证""" new_class = create_model(class_name, **mapping) - @model_validator(mode='before') + @model_validator(mode="before") def check_missing_fields(data): required_fields = set(mapping.keys()) missing_fields = required_fields - set(data.keys()) @@ -144,7 +144,7 @@ class ActionNode: raise ValueError(f"Missing fields: {missing_fields}") return data - @field_validator('*') + @field_validator("*") def check_name(v: Any, field: str) -> Any: if field not in mapping.keys(): raise ValueError(f"Unrecognized block: {field}") @@ -230,8 +230,9 @@ class ActionNode: # FIXME: json instruction会带来 "Project name": "web_2048 # 项目名称使用下划线", self.instruction = self.compile_instruction(to="markdown", mode=mode) self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) - prompt = template.format(context=context, example=self.example, instruction=self.instruction, - constraint=CONSTRAINT) + prompt = template.format( + context=context, example=self.example, instruction=self.instruction, constraint=CONSTRAINT + ) return prompt @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) @@ -284,9 +285,7 @@ class ActionNode: def action_node_from_tuple_example(): # 示例:列表中包含元组 - list_of_tuples = [ - ("key1", str, "Instruction 1", "Example 1") - ] + list_of_tuples = [("key1", str, "Instruction 1", "Example 1")] # 从列表中创建 ActionNode 实例 nodes = [ActionNode(*data) for data in list_of_tuples] @@ -294,5 +293,5 @@ def action_node_from_tuple_example(): logger.info(i) -if __name__ == '__main__': +if __name__ == "__main__": action_node_from_tuple_example() diff --git a/metagpt/actions/design_api_an.py b/metagpt/actions/design_api_an.py index 2db203606..0a303cdd5 100644 --- a/metagpt/actions/design_api_an.py +++ b/metagpt/actions/design_api_an.py @@ -6,52 +6,49 @@ @File : design_api_an.py """ from metagpt.actions.action_node import ActionNode -from metagpt.utils.mermaid import MMC1, MMC2 from metagpt.logs import logger +from metagpt.utils.mermaid import MMC1, MMC2 IMPLEMENTATION_APPROACH = ActionNode( key="Implementation approach", expected_type=str, instruction="Analyze the difficult points of the requirements, select the appropriate open-source framework", - example="We will ..." + example="We will ...", ) PROJECT_NAME = ActionNode( - key="Project name", - expected_type=str, - instruction="The project name with underline", - example="game_2048" + key="Project name", expected_type=str, instruction="The project name with underline", example="game_2048" ) FILE_LIST = ActionNode( key="File list", expected_type=list[str], instruction="Only need relative paths. ALWAYS write a main.py or app.py here", - example=['main.py', 'game.py'] + example=["main.py", "game.py"], ) DATA_STRUCTURES_AND_INTERFACES = ActionNode( key="Data structures and interfaces", expected_type=str, instruction="Use mermaid classDiagram code syntax, including classes, method(__init__ etc.) and functions with type" - " annotations, CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " - "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", - example=MMC1 + " annotations, CLEARLY MARK the RELATIONSHIPS between classes, and comply with PEP8 standards. " + "The data structures SHOULD BE VERY DETAILED and the API should be comprehensive with a complete design.", + example=MMC1, ) PROGRAM_CALL_FLOW = ActionNode( key="Program call flow", expected_type=str, instruction="Use sequenceDiagram code syntax, COMPLETE and VERY DETAILED, using CLASSES AND API DEFINED ABOVE " - "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", - example=MMC2 + "accurately, covering the CRUD AND INIT of each object, SYNTAX MUST BE CORRECT.", + example=MMC2, ) ANYTHING_UNCLEAR = ActionNode( key="Anything UNCLEAR", expected_type=str, instruction="Mention unclear project aspects, then try to clarify it.", - example="Clarification needed on third-party API integration, ..." + example="Clarification needed on third-party API integration, ...", ) NODES = [ @@ -60,7 +57,7 @@ NODES = [ FILE_LIST, DATA_STRUCTURES_AND_INTERFACES, PROGRAM_CALL_FLOW, - ANYTHING_UNCLEAR + ANYTHING_UNCLEAR, ] DESIGN_API_NODE = ActionNode.from_children("DesignAPI", NODES) @@ -71,5 +68,5 @@ def main(): logger.info(prompt) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 29e3bed3e..c95be4012 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -10,7 +10,6 @@ 3. According to the design in Section 2.2.3.5.4 of RFC 135, add incremental iteration functionality. """ import json -# from typing import List from metagpt.actions import ActionOutput from metagpt.actions.action import Action @@ -25,6 +24,9 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository + +# from typing import List + # from metagpt.utils.get_template import get_template NEW_REQ_TEMPLATE = """ @@ -97,7 +99,8 @@ class WriteTasks(Action): async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content) node = await PM_NODE.fill(context, self.llm, format) - return node + task_doc.content = node.content + return task_doc @staticmethod async def _update_requirements(doc): diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index aa7cdcde2..e03af36d7 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -12,51 +12,53 @@ REQUIRED_PYTHON_PACKAGES = ActionNode( key="Required Python packages", expected_type=list[str], instruction="Provide required Python packages in requirements.txt format.", - example=["flask==1.1.2", "bcrypt==3.2.0"] + example=["flask==1.1.2", "bcrypt==3.2.0"], ) REQUIRED_OTHER_LANGUAGE_PACKAGES = ActionNode( key="Required Other language third-party packages", expected_type=list[str], instruction="List down the required packages for languages other than Python.", - example=["No third-party dependencies required"] + example=["No third-party dependencies required"], ) LOGIC_ANALYSIS = ActionNode( key="Logic Analysis", expected_type=list[list[str]], instruction="Provide a list of files with the classes/methods/functions to be implemented, " - "including dependency analysis and imports.", - example=[["game.py", "Contains Game class and ... functions"], - ["main.py", "Contains main function, from game import Game"]] + "including dependency analysis and imports.", + example=[ + ["game.py", "Contains Game class and ... functions"], + ["main.py", "Contains main function, from game import Game"], + ], ) TASK_LIST = ActionNode( key="Task list", expected_type=list[str], instruction="Break down the tasks into a list of filenames, prioritized by dependency order.", - example=["game.py", "main.py"] + example=["game.py", "main.py"], ) FULL_API_SPEC = ActionNode( key="Full API spec", expected_type=str, instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend.", - example="openapi: 3.0.0 ..." + example="openapi: 3.0.0 ...", ) SHARED_KNOWLEDGE = ActionNode( key="Shared Knowledge", expected_type=str, instruction="Detail any shared knowledge, like common utility functions or configuration variables.", - example="'game.py' contains functions shared across the project." + example="'game.py' contains functions shared across the project.", ) ANYTHING_UNCLEAR_PM = ActionNode( key="Anything UNCLEAR", expected_type=str, instruction="Mention any unclear aspects in the project management context and try to clarify them.", - example="Clarification needed on how to start and initialize third-party libraries." + example="Clarification needed on how to start and initialize third-party libraries.", ) NODES = [ @@ -66,7 +68,7 @@ NODES = [ TASK_LIST, FULL_API_SPEC, SHARED_KNOWLEDGE, - ANYTHING_UNCLEAR_PM + ANYTHING_UNCLEAR_PM, ] @@ -78,5 +80,5 @@ def main(): logger.info(prompt) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 0781760ba..849150f6c 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -13,45 +13,45 @@ LANGUAGE = ActionNode( key="Language", expected_type=str, instruction="Provide the language used in the project, typically matching the user's requirement language.", - example="en_us" + example="en_us", ) ORIGINAL_REQUIREMENTS = ActionNode( key="Original Requirements", expected_type=str, instruction="Place the polished, complete original requirements here.", - example="The game should have a leaderboard and multiple difficulty levels." + example="The game should have a leaderboard and multiple difficulty levels.", ) PROJECT_NAME = ActionNode( key="Project Name", expected_type=str, instruction="Name the project using snake case style, like 'game_2048' or 'simple_crm'.", - example="game_2048" + example="game_2048", ) PRODUCT_GOALS = ActionNode( key="Product Goals", expected_type=list[str], instruction="Provide up to three clear, orthogonal product goals.", - example=["Create an engaging user experience", - "Ensure high performance", - "Provide customizable features"] + example=["Create an engaging user experience", "Ensure high performance", "Provide customizable features"], ) USER_STORIES = ActionNode( key="User Stories", expected_type=list[str], instruction="Provide up to five scenario-based user stories.", - example=["As a user, I want to be able to choose difficulty levels", - "As a player, I want to see my score after each game"] + example=[ + "As a user, I want to be able to choose difficulty levels", + "As a player, I want to see my score after each game", + ], ) COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", expected_type=list[str], instruction="Provide analyses for up to seven competitive products.", - example=["Python Snake Game: Simple interface, lacks advanced features"] + example=["Python Snake Game: Simple interface, lacks advanced features"], ) COMPETITIVE_QUADRANT_CHART = ActionNode( @@ -72,56 +72,53 @@ COMPETITIVE_QUADRANT_CHART = ActionNode( "Campaign D": [0.78, 0.34] "Campaign E": [0.40, 0.34] "Campaign F": [0.35, 0.78] - "Our Target Product": [0.5, 0.6]""" + "Our Target Product": [0.5, 0.6]""", ) REQUIREMENT_ANALYSIS = ActionNode( key="Requirement Analysis", expected_type=str, instruction="Provide a detailed analysis of the requirements.", - example="The product should be user-friendly and performance-optimized." + example="The product should be user-friendly and performance-optimized.", ) REQUIREMENT_POOL = ActionNode( key="Requirement Pool", expected_type=list[list[str]], instruction="List down the requirements with their priority (P0, P1, P2).", - example=[["P0", "High priority requirement"], ["P1", "Medium priority requirement"]] + example=[["P0", "High priority requirement"], ["P1", "Medium priority requirement"]], ) UI_DESIGN_DRAFT = ActionNode( key="UI Design draft", expected_type=str, instruction="Provide a simple description of UI elements, functions, style, and layout.", - example="Basic function description with a simple style and layout." + example="Basic function description with a simple style and layout.", ) ANYTHING_UNCLEAR = ActionNode( key="Anything UNCLEAR", expected_type=str, instruction="Mention any aspects of the project that are unclear and try to clarify them.", - example="..." + example="...", ) ISSUE_TYPE = ActionNode( key="issue_type", expected_type=str, instruction="Answer BUG/REQUIREMENT. If it is a bugfix, answer BUG, otherwise answer Requirement", - example="BUG" + example="BUG", ) IS_RELATIVE = ActionNode( key="is_relative", expected_type=str, instruction="Answer YES/NO. If the requirement is related to the old PRD, answer YES, otherwise NO", - example="YES" + example="YES", ) REASON = ActionNode( - key="reason", - expected_type=str, - instruction="Explain the reasoning process from question to answer", - example="..." + key="reason", expected_type=str, instruction="Explain the reasoning process from question to answer", example="..." ) @@ -136,7 +133,7 @@ NODES = [ REQUIREMENT_ANALYSIS, REQUIREMENT_POOL, UI_DESIGN_DRAFT, - ANYTHING_UNCLEAR + ANYTHING_UNCLEAR, ] WRITE_PRD_NODE = ActionNode.from_children("WritePRD", NODES) @@ -149,5 +146,5 @@ def main(): logger.info(prompt) -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/metagpt/provider/fireworks_api.py b/metagpt/provider/fireworks_api.py index 23126af2d..47ac9cf61 100644 --- a/metagpt/provider/fireworks_api.py +++ b/metagpt/provider/fireworks_api.py @@ -5,11 +5,10 @@ import openai from metagpt.config import CONFIG -from metagpt.provider.openai_api import OpenAIGPTAPI, CostManager, RateLimiter +from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter class FireWorksGPTAPI(OpenAIGPTAPI): - def __init__(self): self.__init_fireworks(CONFIG) self.llm = openai diff --git a/metagpt/provider/open_llm_api.py b/metagpt/provider/open_llm_api.py index a6820b42b..f421e30c8 100644 --- a/metagpt/provider/open_llm_api.py +++ b/metagpt/provider/open_llm_api.py @@ -4,13 +4,13 @@ import openai -from metagpt.logs import logger from metagpt.config import CONFIG -from metagpt.provider.openai_api import OpenAIGPTAPI, CostManager, RateLimiter +from metagpt.logs import logger +from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter class OpenLLMCostManager(CostManager): - """ open llm model is self-host, it's free and without cost""" + """open llm model is self-host, it's free and without cost""" def update_cost(self, prompt_tokens, completion_tokens, model): """ @@ -32,7 +32,6 @@ class OpenLLMCostManager(CostManager): class OpenLLMGPTAPI(OpenAIGPTAPI): - def __init__(self): self.__init_openllm(CONFIG) self.llm = openai diff --git a/metagpt/provider/postprecess/base_postprecess_plugin.py b/metagpt/provider/postprecess/base_postprecess_plugin.py index 702a03194..0d1cfbb11 100644 --- a/metagpt/provider/postprecess/base_postprecess_plugin.py +++ b/metagpt/provider/postprecess/base_postprecess_plugin.py @@ -5,13 +5,15 @@ from typing import Union from metagpt.logs import logger -from metagpt.utils.repair_llm_raw_output import RepairType -from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, extract_content_from_output, \ - retry_parse_json_text +from metagpt.utils.repair_llm_raw_output import ( + RepairType, + extract_content_from_output, + repair_llm_raw_output, + retry_parse_json_text, +) class BasePostPrecessPlugin(object): - model = None # the plugin of the `model`, use to judge in `llm_postprecess` def run_repair_llm_output(self, output: str, schema: dict, req_key: str = "[/CONTENT]") -> Union[dict, list]: @@ -33,15 +35,15 @@ class BasePostPrecessPlugin(object): return parsed_data def run_repair_llm_raw_output(self, content: str, req_keys: list[str], repair_type: str = None) -> str: - """ inherited class can re-implement the function""" + """inherited class can re-implement the function""" return repair_llm_raw_output(content, req_keys=req_keys, repair_type=repair_type) def run_extract_content_from_output(self, content: str, right_key: str) -> str: - """ inherited class can re-implement the function""" + """inherited class can re-implement the function""" return extract_content_from_output(content, right_key=right_key) def run_retry_parse_json_text(self, content: str) -> Union[dict, list]: - """ inherited class can re-implement the function""" + """inherited class can re-implement the function""" logger.info(f"extracted json CONTENT from output:\n{content}") parsed_data = retry_parse_json_text(output=content) # should use output=content return parsed_data @@ -64,9 +66,5 @@ class BasePostPrecessPlugin(object): assert "/" in req_key # current, postprocess only deal the repair_llm_raw_output - new_output = self.run_repair_llm_output( - output=output, - schema=schema, - req_key=req_key - ) + new_output = self.run_repair_llm_output(output=output, schema=schema, req_key=req_key) return new_output diff --git a/metagpt/provider/postprecess/llm_output_postprecess.py b/metagpt/provider/postprecess/llm_output_postprecess.py index 4b5955061..85405543d 100644 --- a/metagpt/provider/postprecess/llm_output_postprecess.py +++ b/metagpt/provider/postprecess/llm_output_postprecess.py @@ -7,17 +7,14 @@ from typing import Union from metagpt.provider.postprecess.base_postprecess_plugin import BasePostPrecessPlugin -def llm_output_postprecess(output: str, schema: dict, req_key: str = "[/CONTENT]", - model_name: str = None) -> Union[dict, str]: +def llm_output_postprecess( + output: str, schema: dict, req_key: str = "[/CONTENT]", model_name: str = None +) -> Union[dict, str]: """ default use BasePostPrecessPlugin if there is not matched plugin. """ # TODO choose different model's plugin according to the model_name postprecess_plugin = BasePostPrecessPlugin() - result = postprecess_plugin.run( - output=output, - schema=schema, - req_key=req_key - ) + result = postprecess_plugin.run(output=output, schema=schema, req_key=req_key) return result diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index b80ef85be..2c0bdd1d6 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -27,7 +27,7 @@ class Architect(Role): name: str = "Bob", profile: str = "Architect", goal: str = "design a concise, usable, complete software system", - constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries" + constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries", ) -> None: """Initializes the Architect with given attributes.""" super().__init__(name, profile, goal, constraints) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 37090b24f..bfe1be251 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -26,7 +26,7 @@ class ProjectManager(Role): name: str = "Eve", profile: str = "Project Manager", goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " - "dependencies to start with the prerequisite modules", + "dependencies to start with the prerequisite modules", constraints: str = "", ) -> None: """ diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 15a01b9e9..c1573e63b 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -14,9 +14,7 @@ @Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results of SummarizeCode. """ -from metagpt.actions import DebugError, RunCode, WriteCode, WriteCodeReview, WriteTest - -# from metagpt.const import WORKSPACE_ROOT +from metagpt.actions import DebugError, RunCode, WriteTest from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( diff --git a/metagpt/schema.py b/metagpt/schema.py index 25281e399..baed5582b 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -97,14 +97,14 @@ class Message(BaseModel): send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL}) def __init__( - self, - content, - instruct_content=None, - role="user", - cause_by="", - sent_from="", - send_to=MESSAGE_ROUTE_TO_ALL, - **kwargs, + self, + content, + instruct_content=None, + role="user", + cause_by="", + sent_from="", + send_to=MESSAGE_ROUTE_TO_ALL, + **kwargs, ): """ Parameters not listed below will be stored as meta info, including custom parameters. diff --git a/metagpt/utils/ahttp_client.py b/metagpt/utils/ahttp_client.py index d4f9f94e5..b4a33e9d7 100644 --- a/metagpt/utils/ahttp_client.py +++ b/metagpt/utils/ahttp_client.py @@ -2,29 +2,24 @@ # -*- coding: utf-8 -*- # @Desc : pure async http_client -from typing import Optional, Any, Mapping, Union +from typing import Any, Mapping, Optional, Union -from aiohttp.client import DEFAULT_TIMEOUT import aiohttp +from aiohttp.client import DEFAULT_TIMEOUT -async def apost(url: str, - params: Optional[Mapping[str, str]] = None, - json: Any = None, - data: Any = None, - headers: Optional[dict] = None, - as_json: bool = False, - encoding: str = "utf-8", - timeout: int = DEFAULT_TIMEOUT.total) -> Union[str, dict]: +async def apost( + url: str, + params: Optional[Mapping[str, str]] = None, + json: Any = None, + data: Any = None, + headers: Optional[dict] = None, + as_json: bool = False, + encoding: str = "utf-8", + timeout: int = DEFAULT_TIMEOUT.total, +) -> Union[str, dict]: async with aiohttp.ClientSession() as session: - async with session.post( - url=url, - params=params, - json=json, - data=data, - headers=headers, - timeout=timeout - ) as resp: + async with session.post(url=url, params=params, json=json, data=data, headers=headers, timeout=timeout) as resp: if as_json: data = await resp.json() else: @@ -33,13 +28,15 @@ async def apost(url: str, return data -async def apost_stream(url: str, - params: Optional[Mapping[str, str]] = None, - json: Any = None, - data: Any = None, - headers: Optional[dict] = None, - encoding: str = "utf-8", - timeout: int = DEFAULT_TIMEOUT.total) -> Any: +async def apost_stream( + url: str, + params: Optional[Mapping[str, str]] = None, + json: Any = None, + data: Any = None, + headers: Optional[dict] = None, + encoding: str = "utf-8", + timeout: int = DEFAULT_TIMEOUT.total, +) -> Any: """ usage: result = astream(url="xx") @@ -47,13 +44,6 @@ async def apost_stream(url: str, deal_with(line) """ async with aiohttp.ClientSession() as session: - async with session.post( - url=url, - params=params, - json=json, - data=data, - headers=headers, - timeout=timeout - ) as resp: + async with session.post(url=url, params=params, json=json, data=data, headers=headers, timeout=timeout) as resp: async for line in resp.content: yield line.decode(encoding) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 9827b8252..1340b1768 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -8,13 +8,15 @@ """ from __future__ import annotations -from gitignore_parser import parse_gitignore, rule_from_pattern, handle_negation import shutil from enum import Enum from pathlib import Path from typing import Dict, List + from git.repo import Repo from git.repo.fun import is_git_dir +from gitignore_parser import parse_gitignore + from metagpt.const import DEFAULT_WORKSPACE_ROOT from metagpt.logs import logger from metagpt.utils.dependency_file import DependencyFile @@ -236,8 +238,9 @@ class GitRepository: rpath = file_path.relative_to(root_relative_path) files.append(str(rpath)) else: - subfolder_files = self.get_files(relative_path=file_path, root_relative_path=root_relative_path, - filter_ignored=False) + subfolder_files = self.get_files( + relative_path=file_path, root_relative_path=root_relative_path, filter_ignored=False + ) files.extend(subfolder_files) except Exception as e: logger.error(f"Error: {e}") diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 0a461d360..4aafd8e66 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -4,12 +4,13 @@ import copy from enum import Enum -from typing import Union, Callable -import regex as re -from tenacity import retry, stop_after_attempt, wait_fixed, after_log, RetryCallState +from typing import Callable, Union + +import regex as re +from tenacity import RetryCallState, retry, stop_after_attempt, wait_fixed -from metagpt.logs import logger from metagpt.config import CONFIG +from metagpt.logs import logger from metagpt.utils.custom_decoder import CustomDecoder @@ -33,7 +34,7 @@ def repair_case_sensitivity(output: str, req_key: str) -> str: if req_key_lower in output_lower: # find the sub-part index, and replace it with raw req_key lidx = output_lower.find(req_key_lower) - source = output[lidx: lidx + len(req_key_lower)] + source = output[lidx : lidx + len(req_key_lower)] output = output.replace(source, req_key) logger.info(f"repair_case_sensitivity: {req_key}") @@ -73,7 +74,7 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - sc = "/" # special char if req_key.startswith("[") and req_key.endswith("]"): if sc in req_key: - left_key = req_key.replace(sc, "") # `[/req_key]` -> `[req_key]` + left_key = req_key.replace(sc, "") # `[/req_key]` -> `[req_key]` right_key = req_key else: left_key = req_key @@ -82,6 +83,7 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - if left_key not in output: output = left_key + "\n" + output if right_key not in output: + def judge_potential_json(routput: str, left_key: str) -> Union[str, None]: ridx = routput.rfind(left_key) if ridx < 0: @@ -90,7 +92,7 @@ def repair_required_key_pair_missing(output: str, req_key: str = "[/CONTENT]") - idx1 = sub_output.rfind("}") idx2 = sub_output.rindex("]") idx = idx1 if idx1 >= idx2 else idx2 - sub_output = sub_output[: idx+1] + sub_output = sub_output[: idx + 1] return sub_output if output.strip().endswith("}") or (output.strip().endswith("]") and not output.strip().endswith(left_key)): @@ -155,9 +157,7 @@ def repair_llm_raw_output(output: str, req_keys: list[str], repair_type: RepairT # do the repairation usually for non-openai models for req_key in req_keys: - output = _repair_llm_raw_output(output=output, - req_key=req_key, - repair_type=repair_type) + output = _repair_llm_raw_output(output=output, req_key=req_key, repair_type=repair_type) return output @@ -187,7 +187,7 @@ def repair_invalid_json(output: str, error: str) -> str: new_line = line.replace("}", "") elif line.endswith("},") and output.endswith("},"): new_line = line[:-1] - elif '",' not in line and ',' not in line: + elif '",' not in line and "," not in line: new_line = f'{line}",' elif "," not in line: # problem, miss char `,` at the end. @@ -228,8 +228,10 @@ def run_after_exp_and_passon_next_retry(logger: "loguru.Logger") -> Callable[["R elif retry_state.kwargs: func_param_output = retry_state.kwargs.get("output", "") exp_str = str(retry_state.outcome.exception()) - logger.warning(f"parse json from content inside [CONTENT][/CONTENT] failed at retry " - f"{retry_state.attempt_number}, try to fix it, exp: {exp_str}") + logger.warning( + f"parse json from content inside [CONTENT][/CONTENT] failed at retry " + f"{retry_state.attempt_number}, try to fix it, exp: {exp_str}" + ) repaired_output = repair_invalid_json(func_param_output, exp_str) retry_state.kwargs["output"] = repaired_output @@ -260,7 +262,8 @@ def retry_parse_json_text(output: str) -> Union[list, dict]: def extract_content_from_output(content: str, right_key: str = "[/CONTENT]"): - """ extract xxx from [CONTENT](xxx)[/CONTENT] using regex pattern """ + """extract xxx from [CONTENT](xxx)[/CONTENT] using regex pattern""" + def re_extract_content(cont: str, pattern: str) -> str: matches = re.findall(pattern, cont, re.DOTALL) for match in matches: diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index f479ec3b8..5ceed65d9 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -4,7 +4,7 @@ import typing -from tenacity import after_log, _utils +from tenacity import _utils def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: @@ -13,7 +13,10 @@ def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typ fn_name = "" else: fn_name = _utils.get_callback_name(retry_state.fn) - logger.error(f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " - f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " - f"exp: {retry_state.outcome.exception()}") + logger.error( + f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " + f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " + f"exp: {retry_state.outcome.exception()}" + ) + return log_it diff --git a/tests/metagpt/test_llm.py b/tests/metagpt/test_llm.py index 49969a2af..408fd3162 100644 --- a/tests/metagpt/test_llm.py +++ b/tests/metagpt/test_llm.py @@ -33,5 +33,6 @@ async def test_llm_acompletion(llm): assert len(await llm.acompletion_batch([hello_msg])) > 0 assert len(await llm.acompletion_batch_text([hello_msg])) > 0 + # if __name__ == "__main__": # pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/utils/test_ahttp_client.py b/tests/metagpt/utils/test_ahttp_client.py index 15159423a..a595d645f 100644 --- a/tests/metagpt/utils/test_ahttp_client.py +++ b/tests/metagpt/utils/test_ahttp_client.py @@ -9,30 +9,21 @@ from metagpt.utils.ahttp_client import apost, apost_stream @pytest.mark.asyncio async def test_apost(): - result = await apost( - url="https://www.baidu.com/" - ) + result = await apost(url="https://www.baidu.com/") assert "百度一下" in result result = await apost( - url="http://aider.meizu.com/app/weather/listWeather", - data={"cityIds": "101240101"}, - as_json=True + url="http://aider.meizu.com/app/weather/listWeather", data={"cityIds": "101240101"}, as_json=True ) assert result["code"] == "200" @pytest.mark.asyncio async def test_apost_stream(): - result = apost_stream( - url="https://www.baidu.com/" - ) + result = apost_stream(url="https://www.baidu.com/") async for line in result: assert len(line) >= 0 - result = apost_stream( - url="http://aider.meizu.com/app/weather/listWeather", - data={"cityIds": "101240101"} - ) + result = apost_stream(url="http://aider.meizu.com/app/weather/listWeather", data={"cityIds": "101240101"}) async for line in result: assert len(line) >= 0 diff --git a/tests/metagpt/utils/test_repair_llm_raw_output.py b/tests/metagpt/utils/test_repair_llm_raw_output.py index a2dd18516..21bbee921 100644 --- a/tests/metagpt/utils/test_repair_llm_raw_output.py +++ b/tests/metagpt/utils/test_repair_llm_raw_output.py @@ -4,10 +4,15 @@ from metagpt.config import CONFIG -CONFIG.repair_llm_output = True +from metagpt.utils.repair_llm_raw_output import ( + RepairType, + extract_content_from_output, + repair_invalid_json, + repair_llm_raw_output, + retry_parse_json_text, +) -from metagpt.utils.repair_llm_raw_output import repair_llm_raw_output, RepairType, repair_invalid_json,\ - extract_content_from_output, retry_parse_json_text +CONFIG.repair_llm_output = True def test_repair_case_sensitivity(): @@ -26,8 +31,7 @@ def test_repair_case_sensitivity(): "Requirement Analysis": "The 2048 game should be simple to play" }""" req_keys = ["Original Requirements", "Search Information", "Competitive Quadrant Chart", "Requirement Analysis"] - output = repair_llm_raw_output(output=raw_output, - req_keys=req_keys) + output = repair_llm_raw_output(output=raw_output, req_keys=req_keys) assert output == target_output @@ -40,8 +44,7 @@ def test_repair_special_character_missing(): "Anything UNCLEAR": "No unclear requirements or information." [/CONTENT]""" req_keys = ["[/CONTENT]"] - output = repair_llm_raw_output(output=raw_output, - req_keys=req_keys) + output = repair_llm_raw_output(output=raw_output, req_keys=req_keys) assert output == target_output raw_output = """[CONTENT] tag @@ -56,15 +59,13 @@ def test_repair_special_character_missing(): "Anything UNCLEAR": "No unclear requirements or information." } [/CONTENT]""" - output = repair_llm_raw_output(output=raw_output, - req_keys=req_keys) + output = repair_llm_raw_output(output=raw_output, req_keys=req_keys) assert output == target_output raw_output = '[CONTENT] {"a": "b"} [CONTENT]' target_output = '[CONTENT] {"a": "b"} [/CONTENT]' - output = repair_llm_raw_output(output=raw_output, - req_keys=["[/CONTENT]"]) + output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) print("output\n", output) assert output == target_output @@ -73,38 +74,35 @@ def test_required_key_pair_missing(): raw_output = '[CONTENT] {"a": "b"}' target_output = '[CONTENT] {"a": "b"}\n[/CONTENT]' - output = repair_llm_raw_output(output=raw_output, - req_keys=["[/CONTENT]"]) + output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) assert output == target_output - raw_output = '''[CONTENT] + raw_output = """[CONTENT] { "key": "value" -]''' - target_output = '''[CONTENT] +]""" + target_output = """[CONTENT] { "key": "value" ] -[/CONTENT]''' +[/CONTENT]""" - output = repair_llm_raw_output(output=raw_output, - req_keys=["[/CONTENT]"]) + output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) assert output == target_output - raw_output = '''[CONTENT] tag + raw_output = """[CONTENT] tag [CONTENT] { "key": "value" } xxx -''' - target_output = '''[CONTENT] +""" + target_output = """[CONTENT] { "key": "value" } -[/CONTENT]''' - output = repair_llm_raw_output(output=raw_output, - req_keys=["[/CONTENT]"]) +[/CONTENT]""" + output = repair_llm_raw_output(output=raw_output, req_keys=["[/CONTENT]"]) assert output == target_output @@ -112,25 +110,19 @@ def test_repair_json_format(): raw_output = "{ xxx }]" target_output = "{ xxx }" - output = repair_llm_raw_output(output=raw_output, - req_keys=[None], - repair_type=RepairType.JSON) + output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON) assert output == target_output raw_output = "[{ xxx }" target_output = "{ xxx }" - output = repair_llm_raw_output(output=raw_output, - req_keys=[None], - repair_type=RepairType.JSON) + output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON) assert output == target_output raw_output = "{ xxx ]" target_output = "{ xxx }" - output = repair_llm_raw_output(output=raw_output, - req_keys=[None], - repair_type=RepairType.JSON) + output = repair_llm_raw_output(output=raw_output, req_keys=[None], repair_type=RepairType.JSON) assert output == target_output @@ -186,7 +178,7 @@ def test_retry_parse_json_text(): target_json = { "Original Requirements": "Create a 2048 game", "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", - "Requirement Analysis": "The requirements are clear and well-defined" + "Requirement Analysis": "The requirements are clear and well-defined", } output = retry_parse_json_text(output=invalid_json_text) assert output == target_json @@ -200,7 +192,7 @@ def test_retry_parse_json_text(): target_json = { "Original Requirements": "Create a 2048 game", "Competitive Quadrant Chart": "quadrantChart\n\ttitle Reach and engagement of campaigns\n\t\tx-axis", - "Requirement Analysis": "The requirements are clear and well-defined" + "Requirement Analysis": "The requirements are clear and well-defined", } output = retry_parse_json_text(output=invalid_json_text) assert output == target_json @@ -214,84 +206,88 @@ def test_extract_content_from_output(): xxx [CONTENT] xxxx [/CONTENT] xxx [CONTENT][/CONTENT] xxx [CONTENT][/CONTENT] # target pair is the last one """ - output = 'Sure! Here is the properly formatted JSON output based on the given context:\n\n[CONTENT]\n{\n"' \ - 'Required Python third-party packages": [\n"pygame==2.0.4",\n"pytest"\n],\n"Required Other language ' \ - 'third-party packages": [\n"No third-party packages are required."\n],\n"Full API spec": "\nopenapi: ' \ - '3.0.0\n\ndescription: A JSON object representing the game state.\n\npaths:\n game:\n get:\n ' \ - 'summary: Get the current game state.\n responses:\n 200:\n description: Game state.' \ - '\n\n moves:\n post:\n summary: Make a move.\n requestBody:\n description: Move to be ' \ - 'made.\n content:\n applicationjson:\n schema:\n type: object\n ' \ - ' properties:\n x:\n type: integer\n y:\n ' \ - ' type: integer\n tile:\n type: object\n ' \ - 'properties:\n value:\n type: integer\n x:\n ' \ - ' type: integer\n y:\n type: integer\n\n ' \ - 'undo-move:\n post:\n summary: Undo the last move.\n responses:\n 200:\n ' \ - ' description: Undone move.\n\n end-game:\n post:\n summary: End the game.\n responses:\n ' \ - ' 200:\n description: Game ended.\n\n start-game:\n post:\n summary: Start a new ' \ - 'game.\n responses:\n 200:\n description: Game started.\n\n game-over:\n get:\n ' \ - ' summary: Check if the game is over.\n responses:\n 200:\n description: Game ' \ - 'over.\n 404:\n description: Game not over.\n\n score:\n get:\n summary: Get the ' \ - 'current score.\n responses:\n 200:\n description: Score.\n\n tile:\n get:\n ' \ - 'summary: Get a specific tile.\n parameters:\n tile_id:\n type: integer\n ' \ - 'description: ID of the tile to get.\n responses:\n 200:\n description: Tile.\n\n ' \ - 'tiles:\n get:\n summary: Get all tiles.\n responses:\n 200:\n description: ' \ - 'Tiles.\n\n level:\n get:\n summary: Get the current level.\n responses:\n 200:\n ' \ - ' description: Level.\n\n level-up:\n post:\n summary: Level up.\n responses:\n ' \ - '200:\n description: Level up successful.\n\n level-down:\n post:\n summary: Level ' \ - 'down.\n responses:\n 200:\n description: Level down successful.\n\n restart:\n ' \ - 'post:\n summary: Restart the game.\n responses:\n 200:\n description: Game ' \ - 'restarted.\n\n help:\n get:\n summary: Get help.\n responses:\n 200:\n ' \ - 'description: Help.\n\n version:\n get:\n summary: Get the version of the game.\n ' \ - 'responses:\n 200:\n description: Version.\n\n}\n\n"Logic Analysis": [\n"game.py",' \ - '\n"Contains the game logic."\n],\n"Task list": [\n"game.py",\n"Contains the game logic and should be ' \ - 'done first."\n],\n"Shared Knowledge": "\n\'game.py\' contains the game logic.\n",\n"Anything ' \ - 'UNCLEAR": "How to start the game."\n]\n\n[/CONTENT] Great! Your JSON output is properly formatted ' \ - 'and correctly includes all the required sections. Here\'s a breakdown of what each section ' \ - 'contains:\n\nRequired Python third-party packages:\n\n* pygame==2.0.4\n* pytest\n\nRequired Other ' \ - 'language third-party packages:\n\n* No third-party packages are required.\n\nFull API spec:\n\n* ' \ - 'openapi: 3.0.0\n* description: A JSON object representing the game state.\n* paths:\n + game: ' \ - 'Get the current game state.\n + moves: Make a move.\n + undo-move: Undo the last move.\n + ' \ - 'end-game: End the game.\n + start-game: Start a new game.\n + game-over: Check if the game is ' \ - 'over.\n + score: Get the current score.\n + tile: Get a specific tile.\n + tiles: Get all tiles.\n ' \ - '+ level: Get the current level.\n + level-up: Level up.\n + level-down: Level down.\n + restart: ' \ - 'Restart the game.\n + help: Get help.\n + version: Get the version of the game.\n\nLogic ' \ - 'Analysis:\n\n* game.py contains the game logic.\n\nTask list:\n\n* game.py contains the game logic ' \ - 'and should be done first.\n\nShared Knowledge:\n\n* \'game.py\' contains the game logic.\n\nAnything ' \ - 'UNCLEAR:\n\n* How to start the game.\n\nGreat job! This JSON output should provide a clear and ' \ - 'comprehensive overview of the project\'s requirements and dependencies.' + output = ( + 'Sure! Here is the properly formatted JSON output based on the given context:\n\n[CONTENT]\n{\n"' + 'Required Python third-party packages": [\n"pygame==2.0.4",\n"pytest"\n],\n"Required Other language ' + 'third-party packages": [\n"No third-party packages are required."\n],\n"Full API spec": "\nopenapi: ' + "3.0.0\n\ndescription: A JSON object representing the game state.\n\npaths:\n game:\n get:\n " + "summary: Get the current game state.\n responses:\n 200:\n description: Game state." + "\n\n moves:\n post:\n summary: Make a move.\n requestBody:\n description: Move to be " + "made.\n content:\n applicationjson:\n schema:\n type: object\n " + " properties:\n x:\n type: integer\n y:\n " + " type: integer\n tile:\n type: object\n " + "properties:\n value:\n type: integer\n x:\n " + " type: integer\n y:\n type: integer\n\n " + "undo-move:\n post:\n summary: Undo the last move.\n responses:\n 200:\n " + " description: Undone move.\n\n end-game:\n post:\n summary: End the game.\n responses:\n " + " 200:\n description: Game ended.\n\n start-game:\n post:\n summary: Start a new " + "game.\n responses:\n 200:\n description: Game started.\n\n game-over:\n get:\n " + " summary: Check if the game is over.\n responses:\n 200:\n description: Game " + "over.\n 404:\n description: Game not over.\n\n score:\n get:\n summary: Get the " + "current score.\n responses:\n 200:\n description: Score.\n\n tile:\n get:\n " + "summary: Get a specific tile.\n parameters:\n tile_id:\n type: integer\n " + "description: ID of the tile to get.\n responses:\n 200:\n description: Tile.\n\n " + "tiles:\n get:\n summary: Get all tiles.\n responses:\n 200:\n description: " + "Tiles.\n\n level:\n get:\n summary: Get the current level.\n responses:\n 200:\n " + " description: Level.\n\n level-up:\n post:\n summary: Level up.\n responses:\n " + "200:\n description: Level up successful.\n\n level-down:\n post:\n summary: Level " + "down.\n responses:\n 200:\n description: Level down successful.\n\n restart:\n " + "post:\n summary: Restart the game.\n responses:\n 200:\n description: Game " + "restarted.\n\n help:\n get:\n summary: Get help.\n responses:\n 200:\n " + "description: Help.\n\n version:\n get:\n summary: Get the version of the game.\n " + 'responses:\n 200:\n description: Version.\n\n}\n\n"Logic Analysis": [\n"game.py",' + '\n"Contains the game logic."\n],\n"Task list": [\n"game.py",\n"Contains the game logic and should be ' + 'done first."\n],\n"Shared Knowledge": "\n\'game.py\' contains the game logic.\n",\n"Anything ' + 'UNCLEAR": "How to start the game."\n]\n\n[/CONTENT] Great! Your JSON output is properly formatted ' + "and correctly includes all the required sections. Here's a breakdown of what each section " + "contains:\n\nRequired Python third-party packages:\n\n* pygame==2.0.4\n* pytest\n\nRequired Other " + "language third-party packages:\n\n* No third-party packages are required.\n\nFull API spec:\n\n* " + "openapi: 3.0.0\n* description: A JSON object representing the game state.\n* paths:\n + game: " + "Get the current game state.\n + moves: Make a move.\n + undo-move: Undo the last move.\n + " + "end-game: End the game.\n + start-game: Start a new game.\n + game-over: Check if the game is " + "over.\n + score: Get the current score.\n + tile: Get a specific tile.\n + tiles: Get all tiles.\n " + "+ level: Get the current level.\n + level-up: Level up.\n + level-down: Level down.\n + restart: " + "Restart the game.\n + help: Get help.\n + version: Get the version of the game.\n\nLogic " + "Analysis:\n\n* game.py contains the game logic.\n\nTask list:\n\n* game.py contains the game logic " + "and should be done first.\n\nShared Knowledge:\n\n* 'game.py' contains the game logic.\n\nAnything " + "UNCLEAR:\n\n* How to start the game.\n\nGreat job! This JSON output should provide a clear and " + "comprehensive overview of the project's requirements and dependencies." + ) output = extract_content_from_output(output) - assert output.startswith('{\n"Required Python third-party packages') and \ - output.endswith('UNCLEAR": "How to start the game."\n]') + assert output.startswith('{\n"Required Python third-party packages') and output.endswith( + 'UNCLEAR": "How to start the game."\n]' + ) - output = 'Sure, I would be happy to help! Here is the information you provided, formatted as a JSON object ' \ - 'inside the [CONTENT] tag:\n\n[CONTENT]\n{\n"Original Requirements": "Create a 2048 game",\n"Search ' \ - 'Information": "Search results for 2048 game",\n"Requirements": [\n"Create a game with the same rules ' \ - 'as the original 2048 game",\n"Implement a user interface that is easy to use and understand",\n"Add a ' \ - 'scoreboard to track the player progress",\n"Allow the player to undo and redo moves",\n"Implement a ' \ - 'game over screen to display the final score"\n],\n"Product Goals": [\n"Create a fun and engaging game ' \ - 'experience for the player",\n"Design a user interface that is visually appealing and easy to use",\n"' \ - 'Optimize the game for performance and responsiveness"\n],\n"User Stories": [\n"As a player, I want to ' \ - 'be able to move tiles around the board to combine numbers",\n"As a player, I want to be able to undo ' \ - 'and redo moves to correct mistakes",\n"As a player, I want to see the final score and game over screen' \ - ' when I win"\n],\n"Competitive Analysis": [\n"Competitor A: 2048 game with a simple user interface and' \ - ' basic graphics",\n"Competitor B: 2048 game with a more complex user interface and better graphics",' \ - '\n"Competitor C: 2048 game with a unique twist on the rules and a more challenging gameplay experience"' \ - '\n],\n"Competitive Quadrant Chart": "quadrantChart\\n\ttitle Reach and engagement of campaigns\\n\t\t' \ - 'x-axis Low Reach --> High Reach\\n\t\ty-axis Low Engagement --> High Engagement\\n\tquadrant-1 We ' \ - 'should expand\\n\tquadrant-2 Need to promote\\n\tquadrant-3 Re-evaluate\\n\tquadrant-4 May be ' \ - 'improved\\n\tCampaign A: [0.3, 0.6]\\n\tCampaign B: [0.45, 0.23]\\n\tCampaign C: [0.57, 0.69]\\n\t' \ - 'Campaign D: [0.78, 0.34]\\n\tCampaign E: [0.40, 0.34]\\n\tCampaign F: [0.35, 0.78]"\n],\n"Requirement ' \ - 'Analysis": "The requirements are clear and well-defined, but there may be some ambiguity around the ' \ - 'specific implementation details",\n"Requirement Pool": [\n["P0", "Implement a game with the same ' \ - 'rules as the original 2048 game"],\n["P1", "Add a scoreboard to track the player progress"],\n["P2", ' \ - '"Allow the player to undo and redo moves"]\n],\n"UI Design draft": "The UI should be simple and easy ' \ - 'to use, with a clean and visually appealing design. The game board should be the main focus of the ' \ - 'UI, with clear and concise buttons for the player to interact with.",\n"Anything UNCLEAR": ""\n}\n' \ - '[/CONTENT]\n\nI hope this helps! Let me know if you have any further questions or if there anything ' \ - 'else I can do to assist you.' + output = ( + "Sure, I would be happy to help! Here is the information you provided, formatted as a JSON object " + 'inside the [CONTENT] tag:\n\n[CONTENT]\n{\n"Original Requirements": "Create a 2048 game",\n"Search ' + 'Information": "Search results for 2048 game",\n"Requirements": [\n"Create a game with the same rules ' + 'as the original 2048 game",\n"Implement a user interface that is easy to use and understand",\n"Add a ' + 'scoreboard to track the player progress",\n"Allow the player to undo and redo moves",\n"Implement a ' + 'game over screen to display the final score"\n],\n"Product Goals": [\n"Create a fun and engaging game ' + 'experience for the player",\n"Design a user interface that is visually appealing and easy to use",\n"' + 'Optimize the game for performance and responsiveness"\n],\n"User Stories": [\n"As a player, I want to ' + 'be able to move tiles around the board to combine numbers",\n"As a player, I want to be able to undo ' + 'and redo moves to correct mistakes",\n"As a player, I want to see the final score and game over screen' + ' when I win"\n],\n"Competitive Analysis": [\n"Competitor A: 2048 game with a simple user interface and' + ' basic graphics",\n"Competitor B: 2048 game with a more complex user interface and better graphics",' + '\n"Competitor C: 2048 game with a unique twist on the rules and a more challenging gameplay experience"' + '\n],\n"Competitive Quadrant Chart": "quadrantChart\\n\ttitle Reach and engagement of campaigns\\n\t\t' + "x-axis Low Reach --> High Reach\\n\t\ty-axis Low Engagement --> High Engagement\\n\tquadrant-1 We " + "should expand\\n\tquadrant-2 Need to promote\\n\tquadrant-3 Re-evaluate\\n\tquadrant-4 May be " + "improved\\n\tCampaign A: [0.3, 0.6]\\n\tCampaign B: [0.45, 0.23]\\n\tCampaign C: [0.57, 0.69]\\n\t" + 'Campaign D: [0.78, 0.34]\\n\tCampaign E: [0.40, 0.34]\\n\tCampaign F: [0.35, 0.78]"\n],\n"Requirement ' + 'Analysis": "The requirements are clear and well-defined, but there may be some ambiguity around the ' + 'specific implementation details",\n"Requirement Pool": [\n["P0", "Implement a game with the same ' + 'rules as the original 2048 game"],\n["P1", "Add a scoreboard to track the player progress"],\n["P2", ' + '"Allow the player to undo and redo moves"]\n],\n"UI Design draft": "The UI should be simple and easy ' + "to use, with a clean and visually appealing design. The game board should be the main focus of the " + 'UI, with clear and concise buttons for the player to interact with.",\n"Anything UNCLEAR": ""\n}\n' + "[/CONTENT]\n\nI hope this helps! Let me know if you have any further questions or if there anything " + "else I can do to assist you." + ) output = extract_content_from_output(output) - assert output.startswith('{\n"Original Requirements"') and \ - output.endswith('"Anything UNCLEAR": ""\n}') + assert output.startswith('{\n"Original Requirements"') and output.endswith('"Anything UNCLEAR": ""\n}') output = """ Sure, I'd be happy to help! Here's the JSON output for the given context:\n\n[CONTENT]\n{ "Implementation approach": "We will use the open-source framework PyGame to create a 2D game engine, which will @@ -316,5 +312,6 @@ def test_extract_content_from_output(): information for a developer to understand the design and implementation of the 2048 game. """ output = extract_content_from_output(output) - assert output.startswith('{\n"Implementation approach"') and \ - output.endswith('"Anything UNCLEAR": "The requirement is clear to me."\n}') + assert output.startswith('{\n"Implementation approach"') and output.endswith( + '"Anything UNCLEAR": "The requirement is clear to me."\n}' + ) From ce1895a40bfde64af82d6a5cde5c90c1fcef41b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Thu, 14 Dec 2023 21:28:11 +0800 Subject: [PATCH 266/413] feat: Assume it's new requirements if the code directory does not exist --- metagpt/actions/write_prd.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index b9bad2233..bb0cf8fb9 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -190,5 +190,9 @@ class WritePRD(Action): CONFIG.git_repo.rename_root(CONFIG.project_name) async def _is_bugfix(self, context) -> bool: + src_workspace_path = CONFIG.git_repo.workdir / CONFIG.git_repo.workdir.name + code_files = CONFIG.git_repo.get_files(relative_path=src_workspace_path) + if not code_files: + return False node = await WP_ISSUE_TYPE_NODE.fill(context, self.llm) return node.get("issue_type") == "BUG" From 84357651e53a82669238ae91ed98610810ddcd89 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 23:54:00 +0800 Subject: [PATCH 267/413] resolve conflicts --- metagpt/actions/action_node.py | 4 ---- metagpt/actions/project_management_an.py | 3 ++- metagpt/actions/write_prd_an.py | 2 +- metagpt/roles/architect.py | 3 ++- metagpt/roles/engineer.py | 3 ++- metagpt/roles/product_manager.py | 4 ++-- metagpt/roles/project_manager.py | 4 ++-- metagpt/team.py | 2 +- 8 files changed, 12 insertions(+), 13 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index f5009f345..9fb10f35c 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -52,10 +52,6 @@ def dict_to_markdown(d, prefix="-", postfix="\n"): class ActionNode: """ActionNode is a tree of nodes.""" - - # Action Strgy - # - sop: 仅使用一级SOP - # - complex: 使用一级SOP+自定义策略填槽 mode: str # Action Context diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index e03af36d7..970cb0594 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -43,7 +43,8 @@ TASK_LIST = ActionNode( FULL_API_SPEC = ActionNode( key="Full API spec", expected_type=str, - instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend.", + instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend. If front-end " + "and back-end communication is not required, leave it blank.", example="openapi: 3.0.0 ...", ) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 2c81bdb6e..68402e504 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -86,7 +86,7 @@ REQUIREMENT_ANALYSIS = ActionNode( key="Requirement Analysis", expected_type=str, instruction="Provide a detailed analysis of the requirements.", - example="The product should be user-friendly and performance-optimized.", + example="The product should be user-friendly.", ) REQUIREMENT_POOL = ActionNode( diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index 2c0bdd1d6..fa91d393d 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -27,7 +27,8 @@ class Architect(Role): name: str = "Bob", profile: str = "Architect", goal: str = "design a concise, usable, complete software system", - constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries", + constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries." + "Use same language as user requirement" ) -> None: """Initializes the Architect with given attributes.""" super().__init__(name, profile, goal, constraints) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 844f3589d..2f99d132e 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -72,7 +72,8 @@ class Engineer(Role): name: str = "Alex", profile: str = "Engineer", goal: str = "write elegant, readable, extensible, efficient code", - constraints: str = "the code should conform to standards like PEP8 and be modular and maintainable", + constraints: str = "the code should conform to standards like PEP8 and be modular and maintainable. " + "Use same language as user requirement", n_borg: int = 1, use_code_review: bool = False, ) -> None: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 017feade7..e5e9f2b5e 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -28,8 +28,8 @@ class ProductManager(Role): self, name: str = "Alice", profile: str = "Product Manager", - goal: str = "Efficiently create a successful product", - constraints: str = "", + goal: str = "efficiently create a successful product", + constraints: str = "use same language as user requirement", ) -> None: """ Initializes the ProductManager role with given attributes. diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index bfe1be251..5a2b9be50 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -26,8 +26,8 @@ class ProjectManager(Role): name: str = "Eve", profile: str = "Project Manager", goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " - "dependencies to start with the prerequisite modules", - constraints: str = "", + "dependencies to start with the prerequisite modules", + constraints: str = "use same language as user requirement", ) -> None: """ Initializes the ProjectManager role with given attributes. diff --git a/metagpt/team.py b/metagpt/team.py index 92f379c97..e1b2a9ffc 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -63,7 +63,7 @@ class Team(BaseModel): while n_round > 0: # self._save() n_round -= 1 - logger.debug(f"{n_round=}") + logger.info(f"max {n_round=} left.") self._check_balance() await self.env.run() if CONFIG.git_repo: From ad0ac940936e089058842f953426b25533d7614f Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 20:27:18 +0800 Subject: [PATCH 268/413] fix code review performance drop --- metagpt/actions/write_code.py | 6 ++++-- metagpt/actions/write_code_review.py | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index a2501db2a..b759f4e2a 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -115,7 +115,7 @@ class WriteCode(Action): if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr - code_context = await self._get_codes(coding_context.task_doc) + code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) prompt = PROMPT_TEMPLATE.format( design=coding_context.design_doc.content, tasks=coding_context.task_doc.content if coding_context.task_doc else "", @@ -133,7 +133,7 @@ class WriteCode(Action): return coding_context @staticmethod - async def _get_codes(task_doc) -> str: + async def get_codes(task_doc, exclude) -> str: if not task_doc: return "" if not task_doc.content: @@ -143,6 +143,8 @@ class WriteCode(Action): codes = [] src_file_repo = CONFIG.git_repo.new_file_repository(relative_path=CONFIG.src_workspace) for filename in code_filenames: + if filename == exclude: + continue doc = await src_file_repo.get(filename=filename) if not doc: continue diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index e0a538fc8..75313fea5 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -10,6 +10,7 @@ from tenacity import retry, stop_after_attempt, wait_random_exponential +from metagpt.actions import WriteCode from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger @@ -109,11 +110,12 @@ class WriteCodeReview(Action): for i in range(k): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) task_content = self.context.task_doc.content if self.context.task_doc else "" + code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) context = "\n----------\n".join( [ "```text\n" + self.context.design_doc.content + "```\n", "```text\n" + task_content + "```\n", - "```python\n" + self.context.code_doc.content + "```\n", + "```python\n" + code_context + "```\n", ] ) prompt = PROMPT_TEMPLATE.format( From ccecb45b13f5786c5ff842ee27516f67ec97b7f4 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 23:54:38 +0800 Subject: [PATCH 269/413] resolve conflicts --- metagpt/actions/action.py | 1 + metagpt/actions/action_node.py | 2 +- metagpt/actions/write_code.py | 61 ++++++++---------- metagpt/actions/write_code_review.py | 95 ++++++++++++++++------------ metagpt/actions/write_prd_an.py | 2 +- metagpt/provider/base_gpt_api.py | 2 +- metagpt/roles/engineer.py | 7 +- metagpt/roles/role.py | 6 +- metagpt/schema.py | 2 +- 9 files changed, 95 insertions(+), 83 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1d9be60e0..6c1f63f45 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -44,6 +44,7 @@ class Action(ABC): self.prefix = prefix self.profile = profile self.llm.system_prompt = prefix + return self def __str__(self): return self.__class__.__name__ diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 9fb10f35c..1d808ec70 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -243,7 +243,7 @@ class ActionNode: ) return prompt - @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) + @retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(6)) async def _aask_v1( self, prompt: str, diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index b759f4e2a..a91e4ee1e 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -34,59 +34,52 @@ from metagpt.utils.file_repository import FileRepository PROMPT_TEMPLATE = """ NOTICE -Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language) +Role: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". ------ -# Design -```json +# Context +## Design {design} -``` ------ -# Tasks -```json + +## Tasks {tasks} -``` ------ -# Legacy Code -```python + +## Legacy Code +```Code {code} ``` ------ -# Debug logs + +## Debug logs ```text {logs} {summary_log} ``` ------ -# Bug Feedback logs + +## Bug Feedback logs ```text {feedback} ``` ------ - -## Code: {filename} Write code with triple quoto, based on the following list and context. -1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets -3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. -5. Think before writing: What should be implemented and provided in this document? -6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. -7. Do not use public member functions that do not exist in your design. -8. Before using a variable, make sure you reference it first -9. Write out EVERY DETAIL, DON'T LEAVE TODO. - -## Format example ------ +# Format example ## Code: {filename} ```python ## {filename} ... ``` ------ + +# Instruction: Based on the context, follow "Format example", write code. + +## Code: {filename} Write code with triple quoto, based on the following attentions and context. +1. Only One file: do your best to implement THIS ONLY ONE FILE. +2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets. +3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import. +4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design. +5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. +6. Before using a external variable/module, make sure you import it first. +7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO. + """ @@ -148,5 +141,5 @@ class WriteCode(Action): doc = await src_file_repo.get(filename=filename) if not doc: continue - codes.append(doc.content) - return "\n----------\n".join(codes) + codes.append(f"----- {filename}\n" + doc.content) + return "\n".join(codes) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 75313fea5..f63a399a9 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -18,8 +18,8 @@ from metagpt.schema import CodingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ -NOTICE -Role: You are a professional software engineer, and your main task is to review the code. You need to ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). +# System +Role: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain. Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". @@ -27,53 +27,52 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc {context} ## Code to be Reviewed: {filename} -``` +```Code {code} ``` +""" ------ -## Code Review: Based on the "Code to be Reviewed", provide key, clear, concise, and specific code modification suggestions, up to 5. +EXAMPLE_AND_INSTRUCTION = """ + +{format_example} + + +# Instruction: Based on the actual code situation, follow one of the "Format example". + +## Code Review: Ordered List. Based on the "Code to be Reviewed", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step. 1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step. 2. Is the code logic completely correct? If there are errors, please indicate how to correct them. 3. Does the existing code follow the "Data structures and interfaces"? 4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step. 5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported -6. Is the code implemented concisely enough? Are methods from other files being reused correctly? +6. Are methods from other files being reused correctly? -## Code Review Result: If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM. +## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B + +## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM. LGTM/LBTM -## Rewrite Code: if it still has some bugs, rewrite {filename} based on "Code Review" with triple quotes, try to get LGTM. Do your utmost to optimize THIS SINGLE FILE. Implement ALL TODO. RETURN ALL CODE, NEVER OMIT ANYTHING. 以任何方式省略代码都是不允许的。 -``` -``` - -## Format example -{format_example} - """ FORMAT_EXAMPLE = """ ------ -# EXAMPLE 1 +# Format example 1 ## Code Review: {filename} -1. No, we should add the logic of ... +1. No, we should fix the logic of class A due to ... 2. ... 3. ... -4. ... +4. No, function B is not implemented, ... 5. ... 6. ... -## Code Review Result: {filename} +## Actions +1. fix class A +2. implement function B + +## Code Review Result LBTM -## Rewrite Code: {filename} -```python -## {filename} -... -``` ------ -# EXAMPLE 2 +# Format example 2 ## Code Review: {filename} 1. Yes. 2. Yes. @@ -82,12 +81,20 @@ LBTM 5. Yes. 6. Yes. -## Code Review Result: {filename} -LGTM - -## Rewrite Code: {filename} +## Actions pass ------ + +## Code Review Result +LGTM +""" + +REWRITE_CODE_TEMPLATE = """ +# Instruction: rewrite code based on the Code Review and Actions +## Rewrite Code: CodeBlock. If it still has some bugs, rewrite {filename} with triple quotes. Do your utmost to optimize THIS SINGLE FILE. Return all completed codes and prohibit the return of unfinished codes. +```Code +## {filename} +... +``` """ @@ -96,11 +103,15 @@ class WriteCodeReview(Action): super().__init__(name, context, llm) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) - async def write_code_review_and_rewrite(self, prompt): - code_rsp = await self._aask(prompt) - result = CodeParser.parse_block("Code Review Result", code_rsp) + async def write_code_review_and_rewrite(self, context_prompt, cr_prompt, filename): + cr_rsp = await self._aask(context_prompt + cr_prompt) + result = CodeParser.parse_block("Code Review Result", cr_rsp) if "LGTM" in result: return result, None + + # if LBTM, rewrite code + rewrite_prompt = f"{context_prompt}\n{cr_rsp}\n{REWRITE_CODE_TEMPLATE.format(filename=filename)}" + code_rsp = await self._aask(rewrite_prompt) code = CodeParser.parse_code(block="", text=code_rsp) return result, code @@ -111,23 +122,23 @@ class WriteCodeReview(Action): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) task_content = self.context.task_doc.content if self.context.task_doc else "" code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) - context = "\n----------\n".join( + context = "\n".join( [ - "```text\n" + self.context.design_doc.content + "```\n", - "```text\n" + task_content + "```\n", - "```python\n" + code_context + "```\n", + "## System Design\n" + self.context.design_doc.content + "\n", + "## Tasks\n" + task_content + "\n", + "## Code Files\n" + code_context + "\n", ] ) - prompt = PROMPT_TEMPLATE.format( + context_prompt = PROMPT_TEMPLATE.format( context=context, code=iterative_code, filename=self.context.code_doc.filename, - format_example=format_example, ) + cr_prompt = EXAMPLE_AND_INSTRUCTION.format(format_example=format_example, ) logger.info( - f"Code review and rewrite {self.context.code_doc.filename,}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" + f"Code review and rewrite {self.context.code_doc.filename}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" ) - result, rewrited_code = await self.write_code_review_and_rewrite(prompt) + result, rewrited_code = await self.write_code_review_and_rewrite(context_prompt, cr_prompt, self.context.code_doc.filename) if "LBTM" in result: iterative_code = rewrited_code elif "LGTM" in result: diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 68402e504..d96c0aeac 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -93,7 +93,7 @@ REQUIREMENT_POOL = ActionNode( key="Requirement Pool", expected_type=list[list[str]], instruction="List down the requirements with their priority (P0, P1, P2).", - example=[["P0", "High priority requirement"], ["P1", "Medium priority requirement"]], + example=[["P0", "..."], ["P1", "..."]], ) UI_DESIGN_DRAFT = ActionNode( diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index 6c1dc8338..c38576806 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -49,8 +49,8 @@ class BaseGPTAPI(BaseChatbot): message = ( [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt else [self._user_msg(msg)] ) - rsp = await self.acompletion_text(message, stream=stream) logger.debug(message) + rsp = await self.acompletion_text(message, stream=stream) # logger.debug(rsp) return rsp diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 2f99d132e..f1e65b177 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -72,7 +72,7 @@ class Engineer(Role): name: str = "Alex", profile: str = "Engineer", goal: str = "write elegant, readable, extensible, efficient code", - constraints: str = "the code should conform to standards like PEP8 and be modular and maintainable. " + constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " "Use same language as user requirement", n_borg: int = 1, use_code_review: bool = False, @@ -105,7 +105,9 @@ class Engineer(Role): coding_context = await todo.run() # Code review if review: - coding_context = await WriteCodeReview(context=coding_context, llm=self._llm).run() + action = WriteCodeReview(context=coding_context, llm=self._llm) + self._init_action_system_message(action) + coding_context = await action.run() await src_file_repo.save( coding_context.filename, dependencies={coding_context.design_doc.root_relative_path, coding_context.task_doc.root_relative_path}, @@ -224,6 +226,7 @@ class Engineer(Role): task_doc = await task_file_repo.get(i.name) elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO: design_doc = await design_file_repo.get(i.name) + # FIXME: design doc没有加载进来,是None context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc) return context diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 633ad6051..66475da72 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -134,6 +134,7 @@ class Role: self._setting = RoleSetting( name=name, profile=profile, goal=goal, constraints=constraints, desc=desc, is_human=is_human ) + self._llm.system_prompt = self._get_prefix() self._states = [] self._actions = [] self._role_id = str(self._setting) @@ -144,6 +145,9 @@ class Role: self._states = [] self._actions = [] + def _init_action_system_message(self, action: Action): + action.set_prefix(self._get_prefix(), self.profile) + def _init_actions(self, actions): self._reset() for idx, action in enumerate(actions): @@ -158,7 +162,7 @@ class Role: ) i = action # i.set_env(self._rc.env) - i.set_prefix(self._get_prefix(), self.profile) + self._init_action_system_message(i) self._actions.append(i) self._states.append(f"{idx}. {action}") diff --git a/metagpt/schema.py b/metagpt/schema.py index baed5582b..799bb9253 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -259,7 +259,7 @@ class MessageQueue: class CodingContext(BaseModel): filename: str - design_doc: Document + design_doc: Optional[Document] task_doc: Optional[Document] code_doc: Optional[Document] From 222694c329d5bddc412317d4e20c774d391776b3 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 00:37:10 +0800 Subject: [PATCH 270/413] fix bugs --- metagpt/actions/write_code.py | 13 +++++++++---- metagpt/actions/write_code_review.py | 2 +- metagpt/schema.py | 6 ++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index a91e4ee1e..5960e2621 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -71,7 +71,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc # Instruction: Based on the context, follow "Format example", write code. -## Code: {filename} Write code with triple quoto, based on the following attentions and context. +## Code: {filename}. Write code with triple quoto, based on the following attentions and context. 1. Only One file: do your best to implement THIS ONLY ONE FILE. 2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets. 3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import. @@ -100,7 +100,7 @@ class WriteCode(Action): filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO ) summary_doc = None - if coding_context.design_doc.filename: + if coding_context.design_doc and coding_context.design_doc.filename: summary_doc = await FileRepository.get_file( filename=coding_context.design_doc.filename, relative_path=CODE_SUMMARIES_FILE_REPO ) @@ -108,9 +108,14 @@ class WriteCode(Action): if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr - code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) + + if bug_feedback: + code_context = coding_context.code_doc.content + else: + code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) + prompt = PROMPT_TEMPLATE.format( - design=coding_context.design_doc.content, + design=coding_context.design_doc.content if coding_context.design_doc else "", tasks=coding_context.task_doc.content if coding_context.task_doc else "", code=code_context, logs=logs, diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index f63a399a9..62e96acd8 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -124,7 +124,7 @@ class WriteCodeReview(Action): code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) context = "\n".join( [ - "## System Design\n" + self.context.design_doc.content + "\n", + "## System Design\n" + str(self.context.design_doc) + "\n", "## Tasks\n" + task_content + "\n", "## Code Files\n" + code_context + "\n", ] diff --git a/metagpt/schema.py b/metagpt/schema.py index 799bb9253..758149efa 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -74,6 +74,12 @@ class Document(BaseModel): return None return str(CONFIG.git_repo.workdir / self.root_path / self.filename) + def __str__(self): + return self.content + + def __repr__(self): + return self.content + class Documents(BaseModel): """A class representing a collection of documents. From 126bcdafb966ef694dcf764dc98302bc57497f27 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 10:44:18 +0800 Subject: [PATCH 271/413] fix error msg --- metagpt/utils/git_repository.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 1340b1768..d2bdf5d85 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -233,6 +233,8 @@ class GitRepository: files = [] try: directory_path = Path(self.workdir) / relative_path + if not directory_path.exists(): + return [] for file_path in directory_path.iterdir(): if file_path.is_file(): rpath = file_path.relative_to(root_relative_path) From 862707d4b7bd319873e550010253a8df0844f6b8 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 10:56:08 +0800 Subject: [PATCH 272/413] use react instead of _react --- metagpt/roles/role.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 66475da72..b673c330d 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -412,7 +412,7 @@ class Role: logger.debug(f"{self._setting}: no news. waiting.") return - rsp = await self._react() + rsp = await self.react() # Reset the next action to be taken. self._rc.todo = None From b97ca3af7ecf980d3ce00675a632c66b9d0989f0 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 14 Dec 2023 23:54:38 +0800 Subject: [PATCH 273/413] feat: resolve conflicts --- metagpt/actions/action.py | 1 + metagpt/actions/action_node.py | 2 +- metagpt/actions/project_management.py | 2 +- metagpt/actions/write_code.py | 72 ++++++++++---------- metagpt/actions/write_code_review.py | 95 +++++++++++++++------------ metagpt/actions/write_prd_an.py | 2 +- metagpt/provider/base_gpt_api.py | 2 +- metagpt/roles/engineer.py | 7 +- metagpt/roles/role.py | 8 ++- metagpt/schema.py | 8 ++- metagpt/utils/common.py | 9 ++- metagpt/utils/git_repository.py | 2 + 12 files changed, 120 insertions(+), 90 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1d9be60e0..6c1f63f45 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -44,6 +44,7 @@ class Action(ABC): self.prefix = prefix self.profile = profile self.llm.system_prompt = prefix + return self def __str__(self): return self.__class__.__name__ diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 9fb10f35c..1d808ec70 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -243,7 +243,7 @@ class ActionNode: ) return prompt - @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) + @retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(6)) async def _aask_v1( self, prompt: str, diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index c95be4012..1f14e7944 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -99,7 +99,7 @@ class WriteTasks(Action): async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content) node = await PM_NODE.fill(context, self.llm, format) - task_doc.content = node.content + task_doc.content = node.instruct_content.json(ensure_ascii=False) return task_doc @staticmethod diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index b759f4e2a..5960e2621 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -34,59 +34,52 @@ from metagpt.utils.file_repository import FileRepository PROMPT_TEMPLATE = """ NOTICE -Role: You are a professional engineer; the main goal is to write PEP8 compliant, elegant, modular, easy to read and maintain Python 3.9 code (but you can also use other programming language) +Role: You are a professional engineer; the main goal is to write google-style, elegant, modular, easy to read and maintain code Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". ------ -# Design -```json +# Context +## Design {design} -``` ------ -# Tasks -```json + +## Tasks {tasks} -``` ------ -# Legacy Code -```python + +## Legacy Code +```Code {code} ``` ------ -# Debug logs + +## Debug logs ```text {logs} {summary_log} ``` ------ -# Bug Feedback logs + +## Bug Feedback logs ```text {feedback} ``` ------ - -## Code: {filename} Write code with triple quoto, based on the following list and context. -1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Requirement: Based on the context, implement one following code file, note to return only in code form, your code will be part of the entire project, so please implement complete, reliable, reusable code snippets -3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. -5. Think before writing: What should be implemented and provided in this document? -6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. -7. Do not use public member functions that do not exist in your design. -8. Before using a variable, make sure you reference it first -9. Write out EVERY DETAIL, DON'T LEAVE TODO. - -## Format example ------ +# Format example ## Code: {filename} ```python ## {filename} ... ``` ------ + +# Instruction: Based on the context, follow "Format example", write code. + +## Code: {filename}. Write code with triple quoto, based on the following attentions and context. +1. Only One file: do your best to implement THIS ONLY ONE FILE. +2. COMPLETE CODE: Your code will be part of the entire project, so please implement complete, reliable, reusable code snippets. +3. Set default value: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. AVOID circular import. +4. Follow design: YOU MUST FOLLOW "Data structures and interfaces". DONT CHANGE ANY DESIGN. Do not use public member functions that do not exist in your design. +5. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. +6. Before using a external variable/module, make sure you import it first. +7. Write out EVERY CODE DETAIL, DON'T LEAVE TODO. + """ @@ -107,7 +100,7 @@ class WriteCode(Action): filename="test_" + coding_context.filename + ".json", relative_path=TEST_OUTPUTS_FILE_REPO ) summary_doc = None - if coding_context.design_doc.filename: + if coding_context.design_doc and coding_context.design_doc.filename: summary_doc = await FileRepository.get_file( filename=coding_context.design_doc.filename, relative_path=CODE_SUMMARIES_FILE_REPO ) @@ -115,9 +108,14 @@ class WriteCode(Action): if test_doc: test_detail = RunCodeResult.loads(test_doc.content) logs = test_detail.stderr - code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) + + if bug_feedback: + code_context = coding_context.code_doc.content + else: + code_context = await self.get_codes(coding_context.task_doc, exclude=self.context.filename) + prompt = PROMPT_TEMPLATE.format( - design=coding_context.design_doc.content, + design=coding_context.design_doc.content if coding_context.design_doc else "", tasks=coding_context.task_doc.content if coding_context.task_doc else "", code=code_context, logs=logs, @@ -148,5 +146,5 @@ class WriteCode(Action): doc = await src_file_repo.get(filename=filename) if not doc: continue - codes.append(doc.content) - return "\n----------\n".join(codes) + codes.append(f"----- {filename}\n" + doc.content) + return "\n".join(codes) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 75313fea5..62e96acd8 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -18,8 +18,8 @@ from metagpt.schema import CodingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ -NOTICE -Role: You are a professional software engineer, and your main task is to review the code. You need to ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). +# System +Role: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain. Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". @@ -27,53 +27,52 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc {context} ## Code to be Reviewed: {filename} -``` +```Code {code} ``` +""" ------ -## Code Review: Based on the "Code to be Reviewed", provide key, clear, concise, and specific code modification suggestions, up to 5. +EXAMPLE_AND_INSTRUCTION = """ + +{format_example} + + +# Instruction: Based on the actual code situation, follow one of the "Format example". + +## Code Review: Ordered List. Based on the "Code to be Reviewed", provide key, clear, concise, and specific answer. If any answer is no, explain how to fix it step by step. 1. Is the code implemented as per the requirements? If not, how to achieve it? Analyse it step by step. 2. Is the code logic completely correct? If there are errors, please indicate how to correct them. 3. Does the existing code follow the "Data structures and interfaces"? 4. Are all functions implemented? If there is no implementation, please indicate how to achieve it step by step. 5. Have all necessary pre-dependencies been imported? If not, indicate which ones need to be imported -6. Is the code implemented concisely enough? Are methods from other files being reused correctly? +6. Are methods from other files being reused correctly? -## Code Review Result: If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM. +## Actions: Ordered List. Things that should be done after CR, such as implementing class A and function B + +## Code Review Result: str. If the code doesn't have bugs, we don't need to rewrite it, so answer LGTM and stop. ONLY ANSWER LGTM/LBTM. LGTM/LBTM -## Rewrite Code: if it still has some bugs, rewrite {filename} based on "Code Review" with triple quotes, try to get LGTM. Do your utmost to optimize THIS SINGLE FILE. Implement ALL TODO. RETURN ALL CODE, NEVER OMIT ANYTHING. 以任何方式省略代码都是不允许的。 -``` -``` - -## Format example -{format_example} - """ FORMAT_EXAMPLE = """ ------ -# EXAMPLE 1 +# Format example 1 ## Code Review: {filename} -1. No, we should add the logic of ... +1. No, we should fix the logic of class A due to ... 2. ... 3. ... -4. ... +4. No, function B is not implemented, ... 5. ... 6. ... -## Code Review Result: {filename} +## Actions +1. fix class A +2. implement function B + +## Code Review Result LBTM -## Rewrite Code: {filename} -```python -## {filename} -... -``` ------ -# EXAMPLE 2 +# Format example 2 ## Code Review: {filename} 1. Yes. 2. Yes. @@ -82,12 +81,20 @@ LBTM 5. Yes. 6. Yes. -## Code Review Result: {filename} -LGTM - -## Rewrite Code: {filename} +## Actions pass ------ + +## Code Review Result +LGTM +""" + +REWRITE_CODE_TEMPLATE = """ +# Instruction: rewrite code based on the Code Review and Actions +## Rewrite Code: CodeBlock. If it still has some bugs, rewrite {filename} with triple quotes. Do your utmost to optimize THIS SINGLE FILE. Return all completed codes and prohibit the return of unfinished codes. +```Code +## {filename} +... +``` """ @@ -96,11 +103,15 @@ class WriteCodeReview(Action): super().__init__(name, context, llm) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) - async def write_code_review_and_rewrite(self, prompt): - code_rsp = await self._aask(prompt) - result = CodeParser.parse_block("Code Review Result", code_rsp) + async def write_code_review_and_rewrite(self, context_prompt, cr_prompt, filename): + cr_rsp = await self._aask(context_prompt + cr_prompt) + result = CodeParser.parse_block("Code Review Result", cr_rsp) if "LGTM" in result: return result, None + + # if LBTM, rewrite code + rewrite_prompt = f"{context_prompt}\n{cr_rsp}\n{REWRITE_CODE_TEMPLATE.format(filename=filename)}" + code_rsp = await self._aask(rewrite_prompt) code = CodeParser.parse_code(block="", text=code_rsp) return result, code @@ -111,23 +122,23 @@ class WriteCodeReview(Action): format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) task_content = self.context.task_doc.content if self.context.task_doc else "" code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) - context = "\n----------\n".join( + context = "\n".join( [ - "```text\n" + self.context.design_doc.content + "```\n", - "```text\n" + task_content + "```\n", - "```python\n" + code_context + "```\n", + "## System Design\n" + str(self.context.design_doc) + "\n", + "## Tasks\n" + task_content + "\n", + "## Code Files\n" + code_context + "\n", ] ) - prompt = PROMPT_TEMPLATE.format( + context_prompt = PROMPT_TEMPLATE.format( context=context, code=iterative_code, filename=self.context.code_doc.filename, - format_example=format_example, ) + cr_prompt = EXAMPLE_AND_INSTRUCTION.format(format_example=format_example, ) logger.info( - f"Code review and rewrite {self.context.code_doc.filename,}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" + f"Code review and rewrite {self.context.code_doc.filename}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" ) - result, rewrited_code = await self.write_code_review_and_rewrite(prompt) + result, rewrited_code = await self.write_code_review_and_rewrite(context_prompt, cr_prompt, self.context.code_doc.filename) if "LBTM" in result: iterative_code = rewrited_code elif "LGTM" in result: diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 68402e504..d96c0aeac 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -93,7 +93,7 @@ REQUIREMENT_POOL = ActionNode( key="Requirement Pool", expected_type=list[list[str]], instruction="List down the requirements with their priority (P0, P1, P2).", - example=[["P0", "High priority requirement"], ["P1", "Medium priority requirement"]], + example=[["P0", "..."], ["P1", "..."]], ) UI_DESIGN_DRAFT = ActionNode( diff --git a/metagpt/provider/base_gpt_api.py b/metagpt/provider/base_gpt_api.py index 6c1dc8338..c38576806 100644 --- a/metagpt/provider/base_gpt_api.py +++ b/metagpt/provider/base_gpt_api.py @@ -49,8 +49,8 @@ class BaseGPTAPI(BaseChatbot): message = ( [self._default_system_msg(), self._user_msg(msg)] if self.use_system_prompt else [self._user_msg(msg)] ) - rsp = await self.acompletion_text(message, stream=stream) logger.debug(message) + rsp = await self.acompletion_text(message, stream=stream) # logger.debug(rsp) return rsp diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 2f99d132e..f1e65b177 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -72,7 +72,7 @@ class Engineer(Role): name: str = "Alex", profile: str = "Engineer", goal: str = "write elegant, readable, extensible, efficient code", - constraints: str = "the code should conform to standards like PEP8 and be modular and maintainable. " + constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " "Use same language as user requirement", n_borg: int = 1, use_code_review: bool = False, @@ -105,7 +105,9 @@ class Engineer(Role): coding_context = await todo.run() # Code review if review: - coding_context = await WriteCodeReview(context=coding_context, llm=self._llm).run() + action = WriteCodeReview(context=coding_context, llm=self._llm) + self._init_action_system_message(action) + coding_context = await action.run() await src_file_repo.save( coding_context.filename, dependencies={coding_context.design_doc.root_relative_path, coding_context.task_doc.root_relative_path}, @@ -224,6 +226,7 @@ class Engineer(Role): task_doc = await task_file_repo.get(i.name) elif str(i.parent) == SYSTEM_DESIGN_FILE_REPO: design_doc = await design_file_repo.get(i.name) + # FIXME: design doc没有加载进来,是None context = CodingContext(filename=filename, design_doc=design_doc, task_doc=task_doc, code_doc=old_code_doc) return context diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 633ad6051..b673c330d 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -134,6 +134,7 @@ class Role: self._setting = RoleSetting( name=name, profile=profile, goal=goal, constraints=constraints, desc=desc, is_human=is_human ) + self._llm.system_prompt = self._get_prefix() self._states = [] self._actions = [] self._role_id = str(self._setting) @@ -144,6 +145,9 @@ class Role: self._states = [] self._actions = [] + def _init_action_system_message(self, action: Action): + action.set_prefix(self._get_prefix(), self.profile) + def _init_actions(self, actions): self._reset() for idx, action in enumerate(actions): @@ -158,7 +162,7 @@ class Role: ) i = action # i.set_env(self._rc.env) - i.set_prefix(self._get_prefix(), self.profile) + self._init_action_system_message(i) self._actions.append(i) self._states.append(f"{idx}. {action}") @@ -408,7 +412,7 @@ class Role: logger.debug(f"{self._setting}: no news. waiting.") return - rsp = await self._react() + rsp = await self.react() # Reset the next action to be taken. self._rc.todo = None diff --git a/metagpt/schema.py b/metagpt/schema.py index baed5582b..758149efa 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -74,6 +74,12 @@ class Document(BaseModel): return None return str(CONFIG.git_repo.workdir / self.root_path / self.filename) + def __str__(self): + return self.content + + def __repr__(self): + return self.content + class Documents(BaseModel): """A class representing a collection of documents. @@ -259,7 +265,7 @@ class MessageQueue: class CodingContext(BaseModel): filename: str - design_doc: Document + design_doc: Optional[Document] task_doc: Optional[Document] code_doc: Optional[Document] diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index f08519f8e..a9bdd6e2d 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -223,10 +223,15 @@ class CodeParser: # 遍历所有的block for block in blocks: # 如果block不为空,则继续处理 - if block.strip() != "": + if block.strip() == "": + continue + if "\n" not in block: + block_title = block + block_content = "" + else: # 将block的标题和内容分开,并分别去掉前后的空白字符 block_title, block_content = block.split("\n", 1) - block_dict[block_title.strip()] = block_content.strip() + block_dict[block_title.strip()] = block_content.strip() return block_dict diff --git a/metagpt/utils/git_repository.py b/metagpt/utils/git_repository.py index 1340b1768..d2bdf5d85 100644 --- a/metagpt/utils/git_repository.py +++ b/metagpt/utils/git_repository.py @@ -233,6 +233,8 @@ class GitRepository: files = [] try: directory_path = Path(self.workdir) / relative_path + if not directory_path.exists(): + return [] for file_path in directory_path.iterdir(): if file_path.is_file(): rpath = file_path.relative_to(root_relative_path) From 60957372fcb3a810f931443b3a8d7bbcbf1d4e2a Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 11:36:24 +0800 Subject: [PATCH 274/413] tuning log level --- metagpt/config.py | 2 +- metagpt/team.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index d04da1d91..8ad42c99f 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -46,7 +46,7 @@ class Config(metaclass=Singleton): def __init__(self, yaml_file=default_yaml_file): self._init_with_config_files_and_env(yaml_file) - logger.info("Config loading done.") + logger.debug("Config loading done.") self._update() def _update(self): diff --git a/metagpt/team.py b/metagpt/team.py index e1b2a9ffc..a5c405f80 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -63,7 +63,7 @@ class Team(BaseModel): while n_round > 0: # self._save() n_round -= 1 - logger.info(f"max {n_round=} left.") + logger.debug(f"max {n_round=} left.") self._check_balance() await self.env.run() if CONFIG.git_repo: From 3a448a7bb48fefea3a2e377ab42e44a3ddd4deb4 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 11:54:30 +0800 Subject: [PATCH 275/413] config: adjust default values --- config/config.yaml | 10 +++++----- metagpt/config.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index ef8575e43..8fd208c59 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -77,8 +77,8 @@ RPM: 10 #### for Stable Diffusion ## Use SD service, based on https://github.com/AUTOMATIC1111/stable-diffusion-webui -SD_URL: "YOUR_SD_URL" -SD_T2I_API: "/sdapi/v1/txt2img" +#SD_URL: "YOUR_SD_URL" +#SD_T2I_API: "/sdapi/v1/txt2img" #### for Execution #LONG_TERM_MEMORY: false @@ -93,8 +93,8 @@ SD_T2I_API: "/sdapi/v1/txt2img" # CALC_USAGE: false ### for Research -MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo -MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k +# MODEL_FOR_RESEARCHER_SUMMARY: gpt-3.5-turbo +# MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k ### choose the engine for mermaid conversion, # default is nodejs, you can change it to playwright,pyppeteer or ink @@ -108,4 +108,4 @@ MODEL_FOR_RESEARCHER_REPORT: gpt-3.5-turbo-16k ### repair operation on the content extracted from LLM's raw output. Warning, it improves the result but not fix all cases. # REPAIR_LLM_OUTPUT: false -PROMPT_FORMAT: json #json or markdown \ No newline at end of file +# PROMPT_FORMAT: json #json or markdown \ No newline at end of file diff --git a/metagpt/config.py b/metagpt/config.py index 8ad42c99f..19bd02c87 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -114,7 +114,7 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.repair_llm_output = self._get("REPAIR_LLM_OUTPUT", False) - self.prompt_format = self._get("PROMPT_FORMAT", "markdown") + self.prompt_format = self._get("PROMPT_FORMAT", "json") self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() From 2c68b42432a86f1b1de95bb5e8ede2ba79efcc03 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 12:06:27 +0800 Subject: [PATCH 276/413] action: add example --- metagpt/actions/write_code_review.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 62e96acd8..4b3e9aece 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -66,8 +66,28 @@ FORMAT_EXAMPLE = """ 6. ... ## Actions -1. fix class A -2. implement function B +1. Fix the `handle_events` method to update the game state only if a move is successful. + ```python + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + if event.type == pygame.KEYDOWN: + moved = False + if event.key == pygame.K_UP: + moved = self.game.move('UP') + elif event.key == pygame.K_DOWN: + moved = self.game.move('DOWN') + elif event.key == pygame.K_LEFT: + moved = self.game.move('LEFT') + elif event.key == pygame.K_RIGHT: + moved = self.game.move('RIGHT') + if moved: + # Update the game state only if a move was successful + self.render() + return True + ``` +2. Implement function B ## Code Review Result LBTM From bc9f0f190269c23050a4ebf54b3ac6e23af0d68e Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 12:17:26 +0800 Subject: [PATCH 277/413] workspace path update --- metagpt/actions/prepare_documents.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 05255dcc5..8d3445ae4 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -27,8 +27,8 @@ class PrepareDocuments(Action): # Create and initialize the workspace folder, initialize the Git environment. project_name = CONFIG.project_name or FileRepository.new_filename() workdir = CONFIG.project_path - if not workdir and CONFIG.workspace: - workdir = Path(CONFIG.workspace) / project_name + if not workdir and CONFIG.workspace_path: + workdir = Path(CONFIG.workspace_path) / project_name workdir = Path(workdir or DEFAULT_WORKSPACE_ROOT / project_name) if not CONFIG.inc and workdir.exists(): shutil.rmtree(workdir) From a3d7b0f380c8305ce51f0675af74d2e438b7e2b0 Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 13:19:04 +0800 Subject: [PATCH 278/413] CR update --- metagpt/actions/action.py | 1 - metagpt/actions/action_node.py | 3 ++- metagpt/actions/research.py | 2 +- metagpt/actions/summarize_code.py | 14 +++++++------- metagpt/roles/role.py | 16 ---------------- 5 files changed, 10 insertions(+), 26 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 6c1f63f45..1534b1f4d 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -43,7 +43,6 @@ class Action(ABC): """Set prefix for later usage""" self.prefix = prefix self.profile = profile - self.llm.system_prompt = prefix return self def __str__(self): diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 1d808ec70..fb7d621d8 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -70,7 +70,8 @@ class ActionNode: content: str instruct_content: BaseModel - def __init__(self, key, expected_type, instruction, example, content="", children=None): + def __init__(self, key: str, expected_type: Type, instruction: str, example: str, content: str = "", + children: dict[str, "ActionNode"] = None): self.key = key self.expected_type = expected_type self.instruction = instruction diff --git a/metagpt/actions/research.py b/metagpt/actions/research.py index d7a2a7e38..a70038c51 100644 --- a/metagpt/actions/research.py +++ b/metagpt/actions/research.py @@ -114,7 +114,7 @@ class CollectLinks(Action): keywords = OutputParser.extract_struct(keywords, list) keywords = parse_obj_as(list[str], keywords) except Exception as e: - logger.exception(f'fail to get keywords related to the research topic "{topic}" for {e}') + logger.exception(f"fail to get keywords related to the research topic '{topic}' for {e}") keywords = [topic] results = await asyncio.gather(*(self.search_engine.run(i, as_string=False) for i in keywords)) diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index 413ac2a21..f8d8d2b47 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -34,13 +34,13 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc ----- {code_blocks} -## Code Review All: 请你对历史所有文件进行阅读,在文件中找到可能的bug,如函数未实现、调用错误、未引用等 +## Code Review All: Please read all historical files and find possible bugs in the files, such as unimplemented functions, calling errors, unreferences, etc. -## Call flow: mermaid代码,根据实现的函数,使用mermaid绘制完整的调用链 +## Call flow: mermaid code, based on the implemented function, use mermaid to draw a complete call chain -## Summary: 根据历史文件的实现情况进行总结 +## Summary: Summary based on the implementation of historical files -## TODOs: Python dict[str, str],这里写出需要修改的文件列表与理由,我们会在之后进行修改 +## TODOs: Python dict[str, str], write down the list of files that need to be modified and the reasons. We will modify them later. """ @@ -49,9 +49,9 @@ FORMAT_EXAMPLE = """ ## Code Review All ### a.py -- 它少实现了xxx需求... -- 字段yyy没有给出... -- ... +- It fulfills less of xxx requirements... +- Field yyy is not given... +-... ### b.py ... diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b673c330d..b07541b09 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -218,22 +218,6 @@ class Role: if env: env.set_subscription(self, self._subscription) - # # Replaced by FileRepository.set_file - # def set_doc(self, content: str, filename: str): - # return self._rc.env.set_doc(content, filename) - # - # # Replaced by FileRepository.get_file - # def get_doc(self, filename: str): - # return self._rc.env.get_doc(filename) - # - # # Replaced by CONFIG.xx - # def set(self, k, v): - # return self._rc.env.set(k, v) - # - # # Replaced by CONFIG.xx - # def get(self, k): - # return self._rc.env.get(k) - @property def profile(self): """Get the role description (position)""" From df2e9a12be0f9c891405b54fb17c23640d404aae Mon Sep 17 00:00:00 2001 From: geekan Date: Fri, 15 Dec 2023 12:17:26 +0800 Subject: [PATCH 279/413] workspace path update --- metagpt/actions/debug_error.py | 2 +- metagpt/actions/prepare_documents.py | 4 ++-- metagpt/document.py | 1 + metagpt/document_store/document.py | 1 + metagpt/roles/qa_engineer.py | 5 +++-- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index df60c2e61..39f3bc1bc 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -62,7 +62,7 @@ class DebugError(Action): if matches: return "" - logger.info(f"Debug and rewrite {self.context.code_filename}") + logger.info(f"Debug and rewrite {self.context.test_filename}") code_doc = await FileRepository.get_file( filename=self.context.code_filename, relative_path=CONFIG.src_workspace ) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 05255dcc5..8d3445ae4 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -27,8 +27,8 @@ class PrepareDocuments(Action): # Create and initialize the workspace folder, initialize the Git environment. project_name = CONFIG.project_name or FileRepository.new_filename() workdir = CONFIG.project_path - if not workdir and CONFIG.workspace: - workdir = Path(CONFIG.workspace) / project_name + if not workdir and CONFIG.workspace_path: + workdir = Path(CONFIG.workspace_path) / project_name workdir = Path(workdir or DEFAULT_WORKSPACE_ROOT / project_name) if not CONFIG.inc and workdir.exists(): shutil.rmtree(workdir) diff --git a/metagpt/document.py b/metagpt/document.py index 6ac4834aa..0af3a915c 100644 --- a/metagpt/document.py +++ b/metagpt/document.py @@ -4,6 +4,7 @@ @Time : 2023/6/8 14:03 @Author : alexanderwu @File : document.py +@Desc : Classes and Operations Related to Files in the File System. """ from enum import Enum from pathlib import Path diff --git a/metagpt/document_store/document.py b/metagpt/document_store/document.py index c59056312..90abc54de 100644 --- a/metagpt/document_store/document.py +++ b/metagpt/document_store/document.py @@ -4,6 +4,7 @@ @Time : 2023/6/8 14:03 @Author : alexanderwu @File : document.py +@Desc : Classes and Operations Related to Vector Files in the Vector Database. Still under design. """ from pathlib import Path diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index c1573e63b..4439b9b19 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -26,6 +26,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext from metagpt.utils.common import any_to_str_set, parse_recipient +from metagpt.utils.file_repository import FileRepository class QaEngineer(Role): @@ -125,8 +126,8 @@ class QaEngineer(Role): async def _debug_error(self, msg): run_code_context = RunCodeContext.loads(msg.content) code = await DebugError(context=run_code_context, llm=self._llm).run() - await CONFIG.git_repo.new_file_repository(CONFIG.src_workspace).save( - filename=run_code_context.code_filename, content=code + await FileRepository.save_file( + filename=run_code_context.test_filename, content=code, relative_path=TEST_CODES_FILE_REPO ) run_code_context.output = None self.publish_message( From 493224439e47cdcf3170d495a6ec35e481404ae8 Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Fri, 15 Dec 2023 18:48:43 +0800 Subject: [PATCH 280/413] version preparation --- README.md | 3 ++- config/config.yaml | 3 +++ setup.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b3473a12c..b0faf85c7 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ # MetaGPT: The Multi-Agent Framework

Software Company Multi-Role Schematic (Gradually Implementing)

- +## News +- Dec 15: v0.5.0 is released! We introduce **incremental development**, facilitating agents to build up larger projects on top of their previous efforts or exisiting human codebase. We also launch a whole collection of important features, including multilingual support (experimental), multiple programming languages support (experimental), incremental development (experimental), CLI support, pip support, enhanced code review, documentation mechanism, and optimized messaging mechanism! ## Install diff --git a/config/config.yaml b/config/config.yaml index 8fd208c59..dc4c4ea5a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,6 +1,9 @@ # DO NOT MODIFY THIS FILE, create a new key.yaml, define OPENAI_API_KEY. # The configuration of key.yaml has a higher priority and will not enter git +#### Project Path Setting +# WORKSPACE_PATH: "Path for placing output files" + #### if OpenAI ## The official OPENAI_API_BASE is https://api.openai.com/v1 ## If the official OPENAI_API_BASE is not available, we recommend using the [openai-forward](https://github.com/beidongjiedeguang/openai-forward). diff --git a/setup.py b/setup.py index 4dd453b3d..730fffd35 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.4.0", + version="0.5.0", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From 335a025c030db55f563e5dd21aaa0f2a7e632018 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Fri, 15 Dec 2023 19:38:48 +0800 Subject: [PATCH 281/413] fix cContextVar OPTIONS LookupError --- metagpt/config.py | 2 ++ metagpt/const.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/metagpt/config.py b/metagpt/config.py index 19bd02c87..d7f5c1249 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -45,9 +45,11 @@ class Config(metaclass=Singleton): default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): + golbal_options = OPTIONS.get() self._init_with_config_files_and_env(yaml_file) logger.debug("Config loading done.") self._update() + golbal_options.update(OPTIONS.get()) def _update(self): # logger.info("Config loading done.") diff --git a/metagpt/const.py b/metagpt/const.py index f6f64a27d..47864d134 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -17,7 +17,7 @@ from loguru import logger import metagpt -OPTIONS = contextvars.ContextVar("OPTIONS") +OPTIONS = contextvars.ContextVar("OPTIONS", default={}) def get_metagpt_package_root(): From 68f3865893140f93f2f38fc5591d0b9cb340c871 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Sun, 17 Dec 2023 00:21:43 +0800 Subject: [PATCH 282/413] Add UserRequirement to watch in default if the role is not set to watch --- metagpt/roles/role.py | 14 +++++++++++++- metagpt/schema.py | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index b07541b09..1e7ebf711 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -27,6 +27,7 @@ from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode +from metagpt.actions.add_requirement import UserRequirement from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory @@ -126,7 +127,17 @@ class RoleContext(BaseModel): return self.memory.get() -class Role: +class _RoleInjector(type): + def __call__(cls, *args, **kwargs): + instance = super().__call__(*args, **kwargs) + + if not instance._rc.watch: + instance._watch([UserRequirement]) + + return instance + + +class Role(metaclass=_RoleInjector): """Role/Agent""" def __init__(self, name="", profile="", goal="", constraints="", desc="", is_human=False): @@ -141,6 +152,7 @@ class Role: self._rc = RoleContext() self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} + def _reset(self): self._states = [] self._actions = [] diff --git a/metagpt/schema.py b/metagpt/schema.py index 758149efa..5aec378e4 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -121,6 +121,10 @@ class Message(BaseModel): :param send_to: Specifies the target recipient or consumer for message delivery in the environment. :param role: Message meta info tells who sent this message. """ + if not cause_by: + from metagpt.actions import UserRequirement + cause_by = UserRequirement + super().__init__( id=uuid.uuid4().hex, content=content, From 355ee8faa8cf4b2edd0fbeccf2084890da780d6d Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Sun, 17 Dec 2023 00:23:21 +0800 Subject: [PATCH 283/413] Set current working directory (cwd) to default project root in PyPI mode --- metagpt/const.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/metagpt/const.py b/metagpt/const.py index 47864d134..10de0ff66 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -23,6 +23,12 @@ OPTIONS = contextvars.ContextVar("OPTIONS", default={}) def get_metagpt_package_root(): """Get the root directory of the installed package.""" package_root = Path(metagpt.__file__).parent.parent + for i in (".git", ".project_root", ".gitignore"): + if (package_root / i).exists(): + break + else: + package_root = Path.cwd() + logger.info(f"Package root set to {str(package_root)}") return package_root From feef54ba3bcbd0d6fe7943f6e9aaf318b9cc396a Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Sun, 17 Dec 2023 13:52:37 +0800 Subject: [PATCH 284/413] patch release v0.5.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 730fffd35..73a05eeae 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.5.0", + version="0.5.1", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From 097c6e09d2ff7b0f5487f6068ff2185801eb950e Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Sun, 17 Dec 2023 14:41:59 +0800 Subject: [PATCH 285/413] add deprecated warnings for the start_project method --- metagpt/team.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/metagpt/team.py b/metagpt/team.py index a5c405f80..5ce07ef13 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -3,10 +3,11 @@ """ @Time : 2023/5/12 00:30 @Author : alexanderwu -@File : software_company.py +@File : team.py @Modified By: mashenquan, 2023/11/27. Add an archiving operation after completing the project, as specified in Section 2.2.3.3 of RFC 135. """ +import warnings from pydantic import BaseModel, Field from metagpt.actions import UserRequirement @@ -47,7 +48,7 @@ class Team(BaseModel): raise NoMoneyException(CONFIG.total_cost, f"Insufficient funds: {CONFIG.max_budget}") def run_project(self, idea, send_to: str = ""): - """Start a project from publishing user requirement.""" + """Run a project from publishing user requirement.""" self.idea = idea # Human requirement. @@ -55,6 +56,16 @@ class Team(BaseModel): Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to or MESSAGE_ROUTE_TO_ALL) ) + def start_project(self, idea, send_to: str = ""): + """ + Deprecated: This method will be removed in the future. + Please use the `run_project` method instead. + """ + warnings.warn("The 'start_project' method is deprecated and will be removed in the future. " + "Please use the 'run_project' method instead.", + DeprecationWarning, stacklevel=2) + return self.run_project(idea=idea, send_to=send_to) + def _save(self): logger.info(self.json(ensure_ascii=False)) From f9111e009ee132b0e30ba2070bfe0f6cac986f1c Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Sun, 17 Dec 2023 15:01:54 +0800 Subject: [PATCH 286/413] update the docs link --- README.md | 22 +++++++++++----------- docs/README_CN.md | 20 ++++++++++---------- docs/ROADMAP.md | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index b0faf85c7..7538824c5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ # If executing, ensure that NPM is installed on your system. Then install mermai sudo npm install -g @mermaid-js/mermaid-cli ``` -detail installation please refer to [cli_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-stable-version) +detail installation please refer to [cli_install](https://docs.deepwisdom.ai/main/en/guide/get_started/installation.html#install-stable-version) ### Docker installation > Note: In the Windows, you need to replace "/opt/metagpt" with a directory that Docker has permission to create, such as "D:\Users\x\metagpt" @@ -83,7 +83,7 @@ # Step 2: Run metagpt demo with container metagpt "Write a cli snake game" ``` -detail installation please refer to [docker_install](https://docs.deepwisdom.ai/guide/get_started/installation.html#install-with-docker) +detail installation please refer to [docker_install](https://docs.deepwisdom.ai/main/en/guide/get_started/installation.html#install-with-docker) ### QuickStart & Demo Video - Try it on [MetaGPT Huggingface Space](https://huggingface.co/spaces/deepwisdom/MetaGPT) @@ -94,19 +94,19 @@ ### QuickStart & Demo Video ## Tutorial -- 🗒 [Online Document](https://docs.deepwisdom.ai/) -- 💻 [Usage](https://docs.deepwisdom.ai/guide/get_started/quickstart.html) -- 🔎 [What can MetaGPT do?](https://docs.deepwisdom.ai/guide/get_started/introduction.html) +- 🗒 [Online Document](https://docs.deepwisdom.ai/main/en/) +- 💻 [Usage](https://docs.deepwisdom.ai/main/en/guide/get_started/quickstart.html) +- 🔎 [What can MetaGPT do?](https://docs.deepwisdom.ai/main/en/guide/get_started/introduction.html) - 🛠 How to build your own agents? - - [MetaGPT Usage & Development Guide | Agent 101](https://docs.deepwisdom.ai/guide/tutorials/agent_101.html) - - [MetaGPT Usage & Development Guide | MultiAgent 101](https://docs.deepwisdom.ai/guide/tutorials/multi_agent_101.html) + - [MetaGPT Usage & Development Guide | Agent 101](https://docs.deepwisdom.ai/main/en/guide/tutorials/agent_101.html) + - [MetaGPT Usage & Development Guide | MultiAgent 101](https://docs.deepwisdom.ai/main/en/guide/tutorials/multi_agent_101.html) - 🧑‍💻 Contribution - [Develop Roadmap](docs/ROADMAP.md) - 🔖 Use Cases - - [Debate](https://docs.deepwisdom.ai/guide/use_cases/multi_agent/debate.html) - - [Researcher](https://docs.deepwisdom.ai/guide/use_cases/agent/researcher.html) - - [Recepit Assistant](https://docs.deepwisdom.ai/guide/use_cases/agent/receipt_assistant.html) -- ❓ [FAQs](https://docs.deepwisdom.ai/guide/faq.html) + - [Debate](https://docs.deepwisdom.ai/main/en/guide/use_cases/multi_agent/debate.html) + - [Researcher](https://docs.deepwisdom.ai/main/en/guide/use_cases/agent/researcher.html) + - [Recepit Assistant](https://docs.deepwisdom.ai/main/en/guide/use_cases/agent/receipt_assistant.html) +- ❓ [FAQs](https://docs.deepwisdom.ai/main/en/guide/faq.html) ## Support diff --git a/docs/README_CN.md b/docs/README_CN.md index dd65c2a25..2855b5500 100644 --- a/docs/README_CN.md +++ b/docs/README_CN.md @@ -78,7 +78,7 @@ # 步骤2: 使用容器运行metagpt演示 metagpt "Write a cli snake game" ``` -详细的安装请安装 [docker_install](https://docs.deepwisdom.ai/zhcn/guide/get_started/installation.html#%E4%BD%BF%E7%94%A8docker%E5%AE%89%E8%A3%85) +详细的安装请安装 [docker_install](https://docs.deepwisdom.ai/main/zh/guide/get_started/installation.html#%E4%BD%BF%E7%94%A8docker%E5%AE%89%E8%A3%85) ### 快速开始的演示视频 - 在 [MetaGPT Huggingface Space](https://huggingface.co/spaces/deepwisdom/MetaGPT) 上进行体验 @@ -88,19 +88,19 @@ ### 快速开始的演示视频 https://github.com/geekan/MetaGPT/assets/34952977/34345016-5d13-489d-b9f9-b82ace413419 ## 教程 -- 🗒 [在线文档](https://docs.deepwisdom.ai/zhcn/) -- 💻 [如何使用](https://docs.deepwisdom.ai/zhcn/guide/get_started/quickstart.html) -- 🔎 [MetaGPT的能力及应用场景](https://docs.deepwisdom.ai/zhcn/guide/get_started/introduction.html) +- 🗒 [在线文档](https://docs.deepwisdom.ai/main/zh/) +- 💻 [如何使用](https://docs.deepwisdom.ai/main/zh/guide/get_started/quickstart.html) +- 🔎 [MetaGPT的能力及应用场景](https://docs.deepwisdom.ai/main/zh/guide/get_started/introduction.html) - 🛠 如何构建你自己的智能体? - - [MetaGPT的使用和开发教程 | 智能体入门](https://docs.deepwisdom.ai/zhcn/guide/tutorials/agent_101.html) - - [MetaGPT的使用和开发教程 | 多智能体入门](https://docs.deepwisdom.ai/zhcn/guide/tutorials/multi_agent_101.html) + - [MetaGPT的使用和开发教程 | 智能体入门](https://docs.deepwisdom.ai/main/zh/guide/tutorials/agent_101.html) + - [MetaGPT的使用和开发教程 | 多智能体入门](https://docs.deepwisdom.ai/main/zh/guide/tutorials/multi_agent_101.html) - 🧑‍💻 贡献 - [开发路线图](ROADMAP.md) - 🔖 示例 - - [辩论](https://docs.deepwisdom.ai/zhcn/guide/use_cases/multi_agent/debate.html) - - [调研员](https://docs.deepwisdom.ai/zhcn/guide/use_cases/agent/researcher.html) - - [票据助手](https://docs.deepwisdom.ai/zhcn/guide/use_cases/agent/receipt_assistant.html) -- ❓ [常见问题解答](https://docs.deepwisdom.ai/zhcn/guide/faq.html) + - [辩论](https://docs.deepwisdom.ai/main/zh/guide/use_cases/multi_agent/debate.html) + - [调研员](https://docs.deepwisdom.ai/main/zh/guide/use_cases/agent/researcher.html) + - [票据助手](https://docs.deepwisdom.ai/main/zh/guide/use_cases/agent/receipt_assistant.html) +- ❓ [常见问题解答](https://docs.deepwisdom.ai/main/zh/guide/faq.html) ## 支持 diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index afc9ff445..25eb4e3a1 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -21,7 +21,7 @@ ### Tasks 3. ~~Support human confirmation and modification during the process~~ (v0.3.0) New: Support human confirmation and modification with fewer constrainsts and a more user-friendly interface 4. Support process caching: Consider carefully whether to add server caching mechanism 5. ~~Resolve occasional failure to follow instruction under current prompts, causing code parsing errors, through stricter system prompts~~ (v0.4.0, with function call) - 6. Write documentation, describing the current features and usage at all levels (ongoing, continuously adding contents to [documentation site](https://docs.deepwisdom.ai/guide/get_started/introduction.html)) + 6. Write documentation, describing the current features and usage at all levels (ongoing, continuously adding contents to [documentation site](https://docs.deepwisdom.ai/main/en/guide/get_started/introduction.html)) 7. ~~Support Docker~~ 2. Features 1. Support a more standard and stable parser (need to analyze the format that the current LLM is better at) From 949bc747f92c368f47bd73966e0eba205d4f7a40 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 28 Nov 2023 09:29:00 +0800 Subject: [PATCH 287/413] add mg ser&deser --- metagpt/actions/action.py | 31 +++++++ metagpt/const.py | 2 + metagpt/environment.py | 38 +++++++++ metagpt/memory/memory.py | 30 +++++++ metagpt/roles/role.py | 117 ++++++++++++++++++++++++++- metagpt/schema.py | 44 +++++++++- metagpt/team.py | 26 ++++++ metagpt/utils/serialize.py | 62 ++++++++++++-- metagpt/utils/utils.py | 38 ++++++++- startup.py | 81 +++++++++++++++++++ tests/metagpt/actions/test_action.py | 17 ++++ tests/metagpt/memory/test_memory.py | 42 ++++++++++ tests/metagpt/roles/test_role.py | 85 +++++++++++++++++++ tests/metagpt/test_environment.py | 27 +++++-- tests/metagpt/test_schema.py | 42 ++++++++++ tests/metagpt/test_team.py | 27 +++++++ 16 files changed, 693 insertions(+), 16 deletions(-) create mode 100644 startup.py create mode 100644 tests/metagpt/memory/test_memory.py create mode 100644 tests/metagpt/roles/test_role.py create mode 100644 tests/metagpt/test_team.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1534b1f4d..3bfb69de4 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -17,6 +17,7 @@ from metagpt.logs import logger from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess from metagpt.utils.common import OutputParser from metagpt.utils.utils import general_after_log +from metagpt.utils.utils import import_class class Action(ABC): @@ -51,6 +52,36 @@ class Action(ABC): def __repr__(self): return self.__str__() + def serialize(self): + return { + "action_class": self.__class__.__name__, + "module_name": self.__module__, + "name": self.name + } + + @classmethod + def deserialize(cls, action_dict: dict): + action_class_str = action_dict.pop("action_class") + module_name = action_dict.pop("module_name") + action_class = import_class(action_class_str, module_name) + return action_class(**action_dict) + + @classmethod + def ser_class(cls): + """ serialize class type""" + return { + "action_class": cls.__name__, + "module_name": cls.__module__ + } + + @classmethod + def deser_class(cls, action_dict: dict): + """ deserialize class type """ + action_class_str = action_dict.pop("action_class") + module_name = action_dict.pop("module_name") + action_class = import_class(action_class_str, module_name) + return action_class + async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str: """Append default prefix""" if not system_msgs: diff --git a/metagpt/const.py b/metagpt/const.py index 10de0ff66..b46bc15a4 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -60,6 +60,8 @@ SWAGGER_PATH = UT_PATH / "files/api/" UT_PY_PATH = UT_PATH / "files/ut/" API_QUESTIONS_PATH = UT_PATH / "files/question/" +SERDES_PATH = DEFAULT_WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project + TMP = METAGPT_ROOT / "tmp" SOURCE_ROOT = METAGPT_ROOT / "metagpt" diff --git a/metagpt/environment.py b/metagpt/environment.py index 89b6f9d46..14da6cd95 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -13,6 +13,7 @@ """ import asyncio from typing import Iterable, Set +from pathlib import Path from pydantic import BaseModel, Field @@ -20,6 +21,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import is_subscribed +from metagpt.utils.utils import read_json_file, write_json_file class Environment(BaseModel): @@ -35,6 +37,42 @@ class Environment(BaseModel): class Config: arbitrary_types_allowed = True + def serialize(self, stg_path: Path): + roles_path = stg_path.joinpath("roles.json") + roles_info = [] + for role_key, role in self.roles.items(): + roles_info.append({ + "role_class": role.__class__.__name__, + "module_name": role.__module__, + "role_name": role.name + }) + role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) + write_json_file(roles_path, roles_info) + + self.memory.serialize(stg_path) + history_path = stg_path.joinpath("history.json") + write_json_file(history_path, {"content": self.history}) + + def deserialize(self, stg_path: Path): + """ stg_path: ./storage/team/environment/ """ + roles_path = stg_path.joinpath("roles.json") + roles_info = read_json_file(roles_path) + for role_info in roles_info: + role_class = role_info.get("role_class") + role_name = role_info.get("role_name") + + role_path = stg_path.joinpath(f"roles/{role_class}_{role_name}") + role = Role.deserialize(role_path) + + self.add_role(role) + + memory = Memory.deserialize(stg_path) + self.memory = memory + + history_path = stg_path.joinpath("history.json") + history = read_json_file(history_path) + self.history = history.get("content") + def add_role(self, role: Role): """增加一个在当前环境的角色 Add a role in the current environment diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 53b65fcf7..43bd33e59 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -8,9 +8,12 @@ """ from collections import defaultdict from typing import Iterable, Set +from pathlib import Path from metagpt.schema import Message from metagpt.utils.common import any_to_str, any_to_str_set +from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.serialize import serialize_general_message, deserialize_general_message class Memory: @@ -21,6 +24,33 @@ class Memory: self.storage: list[Message] = [] self.index: dict[str, list[Message]] = defaultdict(list) + def serialize(self, stg_path: Path): + """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ + memory_path = stg_path.joinpath("memory.json") + + storage = [] + for message in self.storage: + # msg_dict = message.serialize() + msg_dict = serialize_general_message(message) + storage.append(msg_dict) + + write_json_file(memory_path, storage) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Memory": + """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" + memory_path = stg_path.joinpath("memory.json") + + memory = Memory() + memory_list = read_json_file(memory_path) + for message in memory_list: + # distinguish instruct_content type in message + # msg = Message.deserialize(message) + msg = deserialize_general_message(message) + memory.add(msg) + + return memory + def add(self, message: Message): """Add a new message to storage, while updating the index""" if message in self.storage: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1e7ebf711..bb3b2acfe 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -22,7 +22,7 @@ from __future__ import annotations from enum import Enum from typing import Iterable, Set, Type - +from pathlib import Path from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput @@ -30,10 +30,12 @@ from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger -from metagpt.memory import Memory from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output +from metagpt.memory import Memory +from metagpt.utils.utils import read_json_file, write_json_file, import_class + PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -152,6 +154,87 @@ class Role(metaclass=_RoleInjector): self._rc = RoleContext() self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} + self._recovered = False + + def serialize(self, stg_path: Path): + role_info_path = stg_path.joinpath("role_info.json") + role_info = { + "role_class": self.__class__.__name__, + "module_name": self.__module__ + } + setting = self._setting.dict() + setting.pop("desc") + setting.pop("is_human") # not all inherited roles have this atrr + role_info.update(setting) + write_json_file(role_info_path, role_info) + + actions_info_path = stg_path.joinpath("actions/actions_info.json") + actions_info = [] + for action in self._actions: + actions_info.append(action.serialize()) + write_json_file(actions_info_path, actions_info) + + watches_info_path = stg_path.joinpath("watches/watches_info.json") + watches_info = [] + for watch in self._rc.watch: + watches_info.append(watch.ser_class()) + write_json_file(watches_info_path, watches_info) + + actions_todo_path = stg_path.joinpath("actions/todo.json") + actions_todo = { + "cur_state": self._rc.state, + "react_mode": self._rc.react_mode.value, + "max_react_loop": self._rc.max_react_loop + } + write_json_file(actions_todo_path, actions_todo) + + self._rc.memory.serialize(stg_path) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Role": + """ stg_path = ./storage/team/environment/roles/{role_class}_{role_name}""" + role_info_path = stg_path.joinpath("role_info.json") + role_info = read_json_file(role_info_path) + + role_class_str = role_info.pop("role_class") + module_name = role_info.pop("module_name") + role_class = import_class(class_name=role_class_str, module_name=module_name) + + role = role_class(**role_info) # initiate particular Role + actions_info_path = stg_path.joinpath("actions/actions_info.json") + actions = [] + actions_info = read_json_file(actions_info_path) + for action_info in actions_info: + action = Action.deserialize(action_info) + actions.append(action) + + watches_info_path = stg_path.joinpath("watches/watches_info.json") + watches = [] + watches_info = read_json_file(watches_info_path) + for watch_info in watches_info: + action = Action.deser_class(watch_info) + watches.append(action) + + role.init_actions(actions) + role.watch(watches) + + actions_todo_path = stg_path.joinpath("actions/todo.json") + # recover self._rc.state + actions_todo = read_json_file(actions_todo_path) + max_react_loop = actions_todo.get("max_react_loop", 1) + cur_state = actions_todo.get("cur_state", -1) + role.set_state(cur_state) + role.set_recovered(True) + react_mode_str = actions_todo.get("react_mode", RoleReactMode.REACT.value) + if react_mode_str not in RoleReactMode.values(): + logger.warning(f"ReactMode: {react_mode_str} not in {RoleReactMode.values()}, use react as default") + react_mode_str = RoleReactMode.REACT.value + role.set_react_mode(RoleReactMode(react_mode_str), max_react_loop) + + role_memory = Memory.deserialize(stg_path) + role.set_memory(role_memory) + + return role def _reset(self): self._states = [] @@ -160,6 +243,15 @@ class Role(metaclass=_RoleInjector): def _init_action_system_message(self, action: Action): action.set_prefix(self._get_prefix(), self.profile) + def set_recovered(self, recovered: bool = False): + self._recovered = recovered + + def set_memory(self, memory: Memory): + self._rc.memory = memory + + def init_actions(self, actions): + self._init_actions(actions) + def _init_actions(self, actions): self._reset() for idx, action in enumerate(actions): @@ -178,6 +270,9 @@ class Role(metaclass=_RoleInjector): self._actions.append(i) self._states.append(f"{idx}. {action}") + def set_react_mode(self, react_mode: RoleReactMode, max_react_loop: int = 1): + self._set_react_mode(react_mode, max_react_loop) + def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): """Set strategy of the Role reacting to observed Message. Variation lies in how this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. @@ -199,6 +294,9 @@ class Role(metaclass=_RoleInjector): if react_mode == RoleReactMode.REACT: self._rc.max_react_loop = max_react_loop + def watch(self, actions: Iterable[Type[Action]]): + self._watch(actions) + def _watch(self, actions: Iterable[Type[Action]]): """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe. @@ -217,6 +315,9 @@ class Role(metaclass=_RoleInjector): if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 self._rc.env.set_subscription(self, self._subscription) + def set_state(self, state: int): + self._set_state(state) + def _set_state(self, state: int): """Update the current state.""" self._rc.state = state @@ -230,6 +331,10 @@ class Role(metaclass=_RoleInjector): if env: env.set_subscription(self, self._subscription) + @property + def name(self): + return self._setting.name + @property def profile(self): """Get the role description (position)""" @@ -257,6 +362,11 @@ class Role(metaclass=_RoleInjector): # If there is only one action, then only this one can be performed self._set_state(0) return + if self._recovered and self._rc.state >= 0: + self._set_state(self._rc.state) # action to run from recovered state + self._recovered = False # avoid max_react_loop out of work + return + prompt = self._get_prefix() prompt += STATE_TEMPLATE.format( history=self._rc.history, @@ -349,7 +459,8 @@ class Role(metaclass=_RoleInjector): async def _act_by_order(self) -> Message: """switch action each time by order defined in _init_actions, i.e. _act (Action1) -> _act (Action2) -> ...""" - for i in range(len(self._states)): + start_idx = self._rc.state if self._rc.state >= 0 else 0 # action to run from recovered state + for i in range(start_idx, len(self._states)): self._set_state(i) rsp = await self._act() return rsp # return output from the last action diff --git a/metagpt/schema.py b/metagpt/schema.py index 5aec378e4..78e4a6031 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -22,7 +22,6 @@ from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path from typing import Dict, List, Optional, Set, TypedDict - from pydantic import BaseModel, Field from metagpt.config import CONFIG @@ -36,6 +35,9 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.utils.common import any_to_str, any_to_str_set +# from metagpt.utils.serialize import actionoutout_schema_to_mapping +# from metagpt.actions.action_output import ActionOutput +# from metagpt.actions.action import Action class RawMessage(TypedDict): @@ -155,6 +157,46 @@ class Message(BaseModel): def __repr__(self): return self.__str__() + # def serialize(self): + # message_cp: Message = copy.deepcopy(self) + # ic = message_cp.instruct_content + # if ic: + # # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly + # schema = ic.schema() + # mapping = actionoutout_schema_to_mapping(schema) + # + # message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + # cb = message_cp.cause_by + # if cb: + # message_cp.cause_by = cb.serialize() + # + # return message_cp.dict() + # + # @classmethod + # def deserialize(cls, message_dict: dict): + # instruct_content = message_dict.get("instruct_content") + # if instruct_content: + # ic = instruct_content + # ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + # ic_new = ic_obj(**ic["value"]) + # message_dict.instruct_content = ic_new + # cause_by = message_dict.get("cause_by") + # if cause_by: + # message_dict.cause_by = Action.deserialize(cause_by) + # + # return Message(**message_dict) + + def dict(self): + return { + "content": self.content, + "instruct_content": self.instruct_content, + "role": self.role, + "cause_by": self.cause_by, + "sent_from": self.sent_from, + "send_to": self.send_to, + "restricted_to": self.restricted_to + } + def to_dict(self) -> dict: """Return a dict containing `role` and `content` for the LLM call.l""" return {"role": self.role, "content": self.content} diff --git a/metagpt/team.py b/metagpt/team.py index a5c405f80..02c48a138 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -7,6 +7,7 @@ @Modified By: mashenquan, 2023/11/27. Add an archiving operation after completing the project, as specified in Section 2.2.3.3 of RFC 135. """ +from pathlib import Path from pydantic import BaseModel, Field from metagpt.actions import UserRequirement @@ -17,6 +18,7 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException +from metagpt.utils.utils import read_json_file, write_json_file class Team(BaseModel): @@ -32,6 +34,30 @@ class Team(BaseModel): class Config: arbitrary_types_allowed = True + def serialize(self, stg_path: Path): + team_info_path = stg_path.joinpath("team_info.json") + write_json_file(team_info_path, { + "idea": self.idea, + "investment": self.investment + }) + + self.environment.serialize(stg_path.joinpath("environment")) + + def deserialize(self, stg_path: Path): + """ stg_path = ./storage/team """ + # recover team_info + team_info_path = stg_path.joinpath("team_info.json") + if not team_info_path.exists(): + logger.error("recover storage not exist, not to recover and continue run the old project.") + team_info = read_json_file(team_info_path) + self.investment = team_info.get("investment", 10.0) + self.idea = team_info.get("idea", "") + + # recover environment + environment_path = stg_path.joinpath("environment") + self.environment = Environment() + self.environment.deserialize(stg_path=environment_path) + def hire(self, roles: list[Role]): """Hire roles to cooperate""" self.env.add_roles(roles) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 124176fcb..56a866f2e 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -4,13 +4,13 @@ import copy import pickle -from typing import Dict, List from metagpt.actions.action_output import ActionOutput from metagpt.schema import Message +from metagpt.actions.action import Action -def actionoutout_schema_to_mapping(schema: Dict) -> Dict: +def actionoutout_schema_to_mapping(schema: dict) -> dict: """ directly traverse the `properties` in the first level. schema structure likes @@ -35,13 +35,47 @@ def actionoutout_schema_to_mapping(schema: Dict) -> Dict: if property["type"] == "string": mapping[field] = (str, ...) elif property["type"] == "array" and property["items"]["type"] == "string": - mapping[field] = (List[str], ...) + mapping[field] = (list[str], ...) elif property["type"] == "array" and property["items"]["type"] == "array": - # here only consider the `List[List[str]]` situation - mapping[field] = (List[List[str]], ...) + # here only consider the `list[list[str]]` situation + mapping[field] = (list[list[str]], ...) return mapping +def actionoutput_mapping_to_str(mapping: dict) -> dict: + new_mapping = {} + for key, value in mapping.items(): + new_mapping[key] = str(value) + return new_mapping + + +def actionoutput_str_to_mapping(mapping: dict) -> dict: + new_mapping = {} + for key, value in mapping.items(): + if value == "(, Ellipsis)": + new_mapping[key] = (str, ...) + else: + new_mapping[key] = eval(value) # `"'(list[str], Ellipsis)"` to `(list[str], ...)` + return new_mapping + + +def serialize_general_message(message: Message) -> dict: + """ serialize Message, not to save""" + message_cp = copy.deepcopy(message) + ic = message_cp.instruct_content + if ic: + # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly + schema = ic.schema() + mapping = actionoutout_schema_to_mapping(schema) + mapping = actionoutput_mapping_to_str(mapping) + + message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + cb = message_cp.cause_by + if cb: + message_cp.cause_by = cb.ser_class() + return message_cp.dict() + + def serialize_message(message: Message): message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference ic = message_cp.instruct_content @@ -56,6 +90,24 @@ def serialize_message(message: Message): return msg_ser +def deserialize_general_message(message_dict: dict) -> Message: + """ deserialize Message, not to load""" + instruct_content = message_dict.pop("instruct_content") + cause_by = message_dict.pop("cause_by") + + message = Message(**message_dict) + if instruct_content: + ic = instruct_content + mapping = actionoutput_str_to_mapping(ic["mapping"]) + ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=mapping) + ic_new = ic_obj(**ic["value"]) + message.instruct_content = ic_new + if cause_by: + message.cause_by = Action.deser_class(cause_by) + + return message + + def deserialize_message(message_ser: str) -> Message: message = pickle.loads(message_ser) if message.instruct_content: diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 5ceed65d9..220e228c3 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -3,7 +3,10 @@ # @Desc : import typing - +from typing import Any +import json +from pathlib import Path +import importlib from tenacity import _utils @@ -20,3 +23,36 @@ def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typ ) return log_it + + +def read_json_file(json_file: str, encoding=None) -> list[Any]: + if not Path(json_file).exists(): + raise FileNotFoundError(f"json_file: {json_file} not exist, return []") + + with open(json_file, "r", encoding=encoding) as fin: + try: + data = json.load(fin) + except Exception as exp: + raise ValueError(f"read json file: {json_file} failed") + return data + + +def write_json_file(json_file: str, data: list, encoding=None): + folder_path = Path(json_file).parent + if not folder_path.exists(): + folder_path.mkdir(parents=True, exist_ok=True) + + with open(json_file, "w", encoding=encoding) as fout: + json.dump(data, fout, ensure_ascii=False, indent=4) + + +def import_class(class_name: str, module_name: str) -> type: + module = importlib.import_module(module_name) + a_class = getattr(module, class_name) + return a_class + + +def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> object: + a_class = import_class(class_name, module_name) + class_inst = a_class(*args, **kwargs) + return class_inst diff --git a/startup.py b/startup.py new file mode 100644 index 000000000..9f753d553 --- /dev/null +++ b/startup.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import asyncio + +import fire + +from metagpt.const import SERDES_PATH +from metagpt.roles import ( + Architect, + Engineer, + ProductManager, + ProjectManager, + QaEngineer, +) +from metagpt.team import Team + + +async def startup( + idea: str, + investment: float = 3.0, + n_round: int = 5, + code_review: bool = False, + run_tests: bool = False, + implement: bool = True, + recover_path: bool = False, +): + """Run a startup. Be a boss.""" + company = Team() + if not recover_path: + company.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) + + # if implement or code_review + if implement or code_review: + # developing features: implement the idea + company.hire([Engineer(n_borg=5, use_code_review=code_review)]) + + if run_tests: + # developing features: run tests on the spot and identify bugs + # (bug fixing capability comes soon!) + company.hire([QaEngineer()]) + else: + stg_path = SERDES_PATH.joinpath("team") + company.deserialize(stg_path=stg_path) + idea = company.idea # use original idea + + company.invest(investment) + company.start_project(idea) + await company.run(n_round=n_round) + + +def main( + idea: str, + investment: float = 3.0, + n_round: int = 5, + code_review: bool = True, + run_tests: bool = False, + implement: bool = True, + recover_path: str = None, +): + """ + We are a software startup comprised of AI. By investing in us, + you are empowering a future filled with limitless possibilities. + :param idea: Your innovative idea, such as "Creating a snake game." + :param investment: As an investor, you have the opportunity to contribute + a certain dollar amount to this AI company. + :param n_round: + :param code_review: Whether to use code review. + :param recover_path: recover the project from existing serialized storage + :return: + """ + asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement, recover_path)) + + +if __name__ == "__main__": + fire.Fire(main) diff --git a/tests/metagpt/actions/test_action.py b/tests/metagpt/actions/test_action.py index 9775630cc..4468a6f6f 100644 --- a/tests/metagpt/actions/test_action.py +++ b/tests/metagpt/actions/test_action.py @@ -11,3 +11,20 @@ from metagpt.actions import Action, WritePRD, WriteTest def test_action_repr(): actions = [Action(), WriteTest(), WritePRD()] assert "WriteTest" in str(actions) + + +def test_action_serdes(): + action_info = WriteTest.ser_class() + assert action_info["action_class"] == "WriteTest" + + action_class = Action.deser_class(action_info) + assert action_class == WriteTest + + +def test_action_class_serdes(): + name = "write test" + action_info = WriteTest(name=name).serialize() + assert action_info["name"] == name + + action = Action.deserialize(action_info) + assert action.name == name diff --git a/tests/metagpt/memory/test_memory.py b/tests/metagpt/memory/test_memory.py new file mode 100644 index 000000000..bda79ded1 --- /dev/null +++ b/tests/metagpt/memory/test_memory.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of memory + +from pathlib import Path + +from metagpt.schema import Message +from metagpt.memory.memory import Memory +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.design_api import WriteDesign +from metagpt.actions.add_requirement import BossRequirement + +serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") + + +def test_memory_serdes(): + msg1 = Message(role="User", + content="write a 2048 game", + cause_by=BossRequirement) + + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + msg2 = Message(role="Architect", + instruct_content=ic_obj(**out_data), + content="system design content", + cause_by=WriteDesign) + + memory = Memory() + memory.add_batch([msg1, msg2]) + + stg_path = serdes_path.joinpath("team/environment") + memory.serialize(stg_path) + assert stg_path.joinpath("memory.json").exists() + + new_memory = Memory.deserialize(stg_path) + assert new_memory.count() == 2 + new_msg2 = new_memory.get(1)[0] + assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] + assert new_msg2.cause_by == WriteDesign + + stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py new file mode 100644 index 000000000..a19ad9cb5 --- /dev/null +++ b/tests/metagpt/roles/test_role.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of Role + +from pathlib import Path +import shutil +import pytest + +from metagpt.roles.role import Role, RoleReactMode +from metagpt.actions.action import Action +from metagpt.schema import Message +from metagpt.actions.add_requirement import BossRequirement +from metagpt.roles.product_manager import ProductManager + +serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") + + +def test_role_serdes(): + stg_path_prefix = serdes_path.joinpath("team/environment/roles/") + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + pm = ProductManager() + role_tag = f"{pm.__class__.__name__}_{pm.name}" + stg_path = stg_path_prefix.joinpath(role_tag) + pm.serialize(stg_path) + assert stg_path.joinpath("actions/actions_info.json").exists() + + new_pm = Role.deserialize(stg_path) + assert new_pm.name == pm.name + assert len(new_pm.get_memories(1)) == 0 + + +class ActionOK(Action): + + async def run(self, messages: list["Message"]): + return "ok" + + +class ActionRaise(Action): + + async def run(self, messages: list["Message"]): + raise RuntimeError("parse error") + + +class RoleA(Role): + + def __init__(self, + name: str = "RoleA", + profile: str = "Role A", + goal: str = "", + constraints: str = ""): + super(RoleA, self).__init__(name=name, profile=profile, goal=goal, constraints=constraints) + self._init_actions([ActionOK, ActionRaise]) + self._watch([BossRequirement]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None, stg_path: str = None): + try: + await super(RoleA, self).run(message) + except Exception as exp: + print("exp ", exp) + self.serialize(stg_path) + + +@pytest.mark.asyncio +async def test_role_serdes_interrupt(): + role_a = RoleA() + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + stg_path = serdes_path.joinpath(f"team/environment/roles/{role_a.__class__.__name__}_{role_a.name}") + await role_a.run( + message=Message(content="demo", cause_by=BossRequirement), + stg_path=stg_path + ) + assert role_a._rc.memory.count() == 2 + + assert stg_path.joinpath("actions/todo.json").exists() + + new_role_a: Role = Role.deserialize(stg_path) + assert new_role_a._rc.state == 1 + await role_a.run( + message=Message(content="demo", cause_by=BossRequirement), + stg_path=stg_path + ) + diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index b27bc3da7..03236a08b 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -7,6 +7,8 @@ """ import pytest +from pathlib import Path +import shutil from metagpt.actions import UserRequirement from metagpt.environment import Environment @@ -14,6 +16,10 @@ from metagpt.logs import logger from metagpt.manager import Manager from metagpt.roles import Architect, ProductManager, Role from metagpt.schema import Message +from tests.metagpt.roles.test_role import RoleA + + +serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") @pytest.fixture @@ -36,12 +42,6 @@ def test_get_roles(env: Environment): assert roles == {role1.profile: role1, role2.profile: role2} -def test_set_manager(env: Environment): - manager = Manager() - env.set_manager(manager) - assert env.manager == manager - - @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): product_manager = ProductManager("Alice", "Product Manager", "做AI Native产品", "资源有限") @@ -54,3 +54,18 @@ async def test_publish_and_process_message(env: Environment): await env.run(k=2) logger.info(f"{env.history=}") assert len(env.history) > 10 + + +def test_environment_serdes(): + environment = Environment() + role_a = RoleA() + + shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) + + stg_path = serdes_path.joinpath("team/environment") + environment.add_role(role_a) + environment.serialize(stg_path) + + new_env: Environment = Environment() + new_env.deserialize(stg_path) + assert len(new_env.roles) == 1 diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 51ebd5baa..4a6f518b1 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -7,12 +7,16 @@ @Modified By: mashenquan, 2023-11-1. In line with Chapter 2.2.1 and 2.2.2 of RFC 116, introduce unit tests for the utilization of the new feature of `Message` class. """ + import json import pytest from metagpt.actions import Action from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.write_code import WriteCode +from metagpt.utils.serialize import serialize_general_message, deserialize_general_message from metagpt.utils.common import get_class_name @@ -70,5 +74,43 @@ def test_routes(): assert m.send_to == {"e", get_class_name(Action)} +def test_message_serdes(): + out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} + out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + + message = Message( + content="code", + instruct_content=ic_obj(**out_data), + role="engineer", + cause_by=WriteCode + ) + message_dict = serialize_general_message(message) + assert message_dict["cause_by"] == {"action_class": "WriteCode"} + assert message_dict["instruct_content"] == { + "class": "code", + "mapping": { + "field3": "(, Ellipsis)", + "field4": "(list[str], Ellipsis)" + }, + "value": { + "field3": "field3 value3", + "field4": ["field4 value1", "field4 value2"] + } + } + + new_message = deserialize_general_message(message_dict) + assert new_message.content == message.content + assert new_message.instruct_content == message.instruct_content + assert new_message.cause_by == message.cause_by + assert new_message.instruct_content.field3 == out_data["field3"] + + message = Message(content="code") + message_dict = serialize_general_message(message) + new_message = deserialize_general_message(message_dict) + assert new_message.instruct_content is None + assert new_message.cause_by == "" + + if __name__ == "__main__": pytest.main([__file__, "-s"]) diff --git a/tests/metagpt/test_team.py b/tests/metagpt/test_team.py new file mode 100644 index 000000000..ab201152c --- /dev/null +++ b/tests/metagpt/test_team.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of team + +from pathlib import Path +import shutil + +from metagpt.team import Team + +from tests.metagpt.roles.test_role import RoleA + +serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") + + +def test_team_serdes(): + company = Team() + company.hire([RoleA()]) + + stg_path = serdes_path.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company.serialize(stg_path=stg_path) + + new_company = Team() + new_company.deserialize(stg_path) + + assert len(new_company.environment.roles) == 1 From c8570036fc92be30d2513a95c72ed9d0dc73bc55 Mon Sep 17 00:00:00 2001 From: stellahsr Date: Mon, 27 Nov 2023 21:12:50 +0800 Subject: [PATCH 288/413] update basic code for serialize --- metagpt/actions/action.py | 57 ++++--- metagpt/actions/design_api.py | 20 ++- metagpt/actions/project_management.py | 13 +- metagpt/actions/search_and_summarize.py | 44 ++++-- metagpt/actions/write_code.py | 14 +- metagpt/actions/write_code_review.py | 8 +- metagpt/actions/write_prd.py | 14 +- metagpt/const.py | 2 +- metagpt/environment.py | 26 ++-- metagpt/roles/architect.py | 21 ++- metagpt/roles/engineer.py | 33 ++-- metagpt/roles/product_manager.py | 41 +++-- metagpt/roles/project_manager.py | 30 ++-- metagpt/roles/role.py | 193 +++++++++++++----------- 14 files changed, 270 insertions(+), 246 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 3bfb69de4..e890ef76a 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -6,10 +6,9 @@ @File : action.py """ -from abc import ABC -from typing import Optional - +from typing import Optional, Any from tenacity import retry, stop_after_attempt, wait_random_exponential +from pydantic import BaseModel, Field from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM @@ -20,25 +19,22 @@ from metagpt.utils.utils import general_after_log from metagpt.utils.utils import import_class -class Action(ABC): - def __init__(self, name: str = "", context=None, llm: LLM = None): - self.name: str = name - if llm is None: - llm = LLM() - self.llm = llm - self.context = context - self.prefix = "" # aask*时会加上prefix,作为system_message - self.profile = "" # FIXME: USELESS - self.desc = "" # for skill manager - self.nodes = ... +action_subclass_registry = {} - # Output, useless - # self.content = "" - # self.instruct_content = None - # self.env = None - # def set_env(self, env): - # self.env = env +class Action(BaseModel): + name: str = "" + llm: LLM = Field(default_factory=LLM) + context = None + prefix = "" # aask*时会加上prefix,作为system_message + profile = "" # FIXME: USELESS + desc = "" # for skill manager + nodes = None + # content: Optional[str] = None + # instruct_content: Optional[str] = None + + def __init__(self, **kwargs: Any): + super().__init__(**kwargs) def set_prefix(self, prefix, profile): """Set prefix for later usage""" @@ -95,27 +91,26 @@ class Action(ABC): after=general_after_log(logger), ) async def _aask_v1( - self, - prompt: str, - output_class_name: str, - output_data_mapping: dict, - system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format + self, + prompt: str, + output_class_name: str, + output_data_mapping: dict, + system_msgs: Optional[list[str]] = None, + format="markdown", # compatible to original format ) -> ActionOutput: content = await self.llm.aask(prompt, system_msgs) logger.debug(f"llm raw output:\n{content}") output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - + if format == "json": parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") - else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - - logger.debug(f"parsed_data:\n{parsed_data}") + + logger.debug(parsed_data) instruct_content = output_class(**parsed_data) return ActionOutput(content, instruct_content) - + async def run(self, *args, **kwargs): """Run action""" raise NotImplementedError("The run method should be implemented in a subclass.") diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 5a5f52de7..a10ff1c9a 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -11,9 +11,12 @@ """ import json from pathlib import Path +from typing import Optional +from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.actions.design_api_an import DESIGN_API_NODE +from metagpt.llm import LLM from metagpt.config import CONFIG from metagpt.const import ( DATA_API_DESIGN_FILE_REPO, @@ -25,12 +28,8 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository - -# from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file -# from typing import List - NEW_REQ_TEMPLATE = """ ### Legacy Content @@ -42,13 +41,12 @@ NEW_REQ_TEMPLATE = """ class WriteDesign(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = ( - "Based on the PRD, think about the system design, and design the corresponding APIs, " - "data structures, library tables, processes, and paths. Please provide your design, feedback " - "clearly and in detail." - ) + name: str = "" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) + desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " + "data structures, library tables, processes, and paths. Please provide your design, feedback " + "clearly and in detail." async def run(self, with_messages, format=CONFIG.prompt_format): # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 1f14e7944..d830a4c15 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -9,11 +9,15 @@ 2. Move the document storage operations related to WritePRD from the save operation of WriteDesign. 3. According to the design in Section 2.2.3.5.4 of RFC 135, add incremental iteration functionality. """ + import json +from typing import List, Optional, Any +from pydantic import Field from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.actions.project_management_an import PM_NODE +from metagpt.llm import LLM from metagpt.config import CONFIG from metagpt.const import ( PACKAGE_REQUIREMENTS_FILENAME, @@ -24,10 +28,8 @@ from metagpt.const import ( from metagpt.logs import logger from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository +from metagpt.provider.base_gpt_api import BaseGPTAPI -# from typing import List - -# from metagpt.utils.get_template import get_template NEW_REQ_TEMPLATE = """ ### Legacy Content @@ -39,8 +41,9 @@ NEW_REQ_TEMPLATE = """ class WriteTasks(Action): - def __init__(self, name="CreateTasks", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "CreateTasks" + context: Optional[str] = None + llm: BaseGPTAPI = Field(default_factory=LLM) async def run(self, with_messages, format=CONFIG.prompt_format): system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 5e4cdaea0..7b549518e 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -6,12 +6,16 @@ @File : search_google.py """ import pydantic +from typing import Optional, Any +from pydantic import BaseModel, Field from metagpt.actions import Action +from metagpt.llm import LLM from metagpt.config import Config from metagpt.logs import logger from metagpt.schema import Message from metagpt.tools.search_engine import SearchEngine +from pydantic import root_validator SEARCH_AND_SUMMARIZE_SYSTEM = """### Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. @@ -54,7 +58,6 @@ SEARCH_AND_SUMMARIZE_PROMPT = """ """ - SEARCH_AND_SUMMARIZE_SALES_SYSTEM = """## Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. - The context is for reference only. If it is irrelevant to the user's search request history, please reduce its reference and usage. @@ -101,23 +104,38 @@ You are a member of a professional butler team and will provide helpful suggesti class SearchAndSummarize(Action): - def __init__(self, name="", context=None, llm=None, engine=None, search_func=None): - self.config = Config() - self.engine = engine or self.config.search_engine + name: str = "" + content: Optional[str] = None + llm: None = Field(default_factory=LLM) + config: None = Field(default_factory=Config) + engine: Optional[str] = None + search_func: Optional[str] = None + search_engine: SearchEngine = None - try: - self.search_engine = SearchEngine(self.engine, run_func=search_func) - except pydantic.ValidationError: - self.search_engine = None + result = "" - self.result = "" - super().__init__(name, context, llm) + @root_validator + def validate_engine_and_run_func(cls, values): + engine = values.get('engine') + search_func = values.get('search_func') + config = Config() + + if engine is None: + engine = config.search_engine + config_data = { + 'engine': engine, + 'run_func': search_func + } + search_engine = SearchEngine(**config_data) + + values['search_engine'] = search_engine + return values async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: if self.search_engine is None: logger.warning("Configure one of SERPAPI_API_KEY, SERPER_API_KEY, GOOGLE_API_KEY to unlock full feature") return "" - + query = context[-1].content # logger.debug(query) rsp = await self.search_engine.run(query) @@ -126,9 +144,9 @@ class SearchAndSummarize(Action): logger.error("empty rsp...") return "" # logger.info(rsp) - + system_prompt = [system_text] - + prompt = SEARCH_AND_SUMMARIZE_PROMPT.format( # PREFIX = self.prefix, ROLE=self.profile, diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 5960e2621..2d155e6bf 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -14,10 +14,17 @@ 3. Encapsulate the input of RunCode into RunCodeContext and encapsulate the output of RunCode into RunCodeResult to standardize and unify parameter passing between WriteCode, RunCode, and DebugError. """ + import json from tenacity import retry, stop_after_attempt, wait_random_exponential + + +from typing import List, Optional, Any +from pydantic import Field +from tenacity import retry, stop_after_attempt, wait_fixed + from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.const import ( @@ -27,6 +34,8 @@ from metagpt.const import ( TASK_FILE_REPO, TEST_OUTPUTS_FILE_REPO, ) +from metagpt.actions import WriteDesign +from metagpt.llm import LLM from metagpt.logs import logger from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser @@ -84,8 +93,9 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): - def __init__(self, name="WriteCode", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "WriteCode" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt) -> str: diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4b3e9aece..bf07d0a93 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -8,9 +8,12 @@ WriteCode object, rather than passing them in when calling the run function. """ +from typing import List, Optional, Any +from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions import WriteCode +from metagpt.llm import LLM from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger @@ -119,8 +122,9 @@ REWRITE_CODE_TEMPLATE = """ class WriteCodeReview(Action): - def __init__(self, name="WriteCodeReview", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "WriteCodeReview" + context: Optional[str] = None + llm: LLM = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code_review_and_rewrite(self, context_prompt, cr_prompt, filename): diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bb0cf8fb9..7f9089763 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -10,10 +10,13 @@ 3. Move the document storage operations related to WritePRD from the save operation of WriteDesign. @Modified By: mashenquan, 2023/12/5. Move the generation logic of the project name to WritePRD. """ + from __future__ import annotations import json from pathlib import Path +from typing import List, Optional, Any +from pydantic import BaseModel, Field from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode @@ -23,6 +26,8 @@ from metagpt.actions.write_prd_an import ( WP_ISSUE_TYPE_NODE, WRITE_PRD_NODE, ) +from metagpt.llm import LLM +from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.const import ( BUGFIX_FILENAME, @@ -36,12 +41,8 @@ from metagpt.logs import logger from metagpt.schema import BugFixContext, Document, Documents, Message from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository - -# from metagpt.utils.get_template import get_template from metagpt.utils.mermaid import mermaid_to_file -# from typing import List - CONTEXT_TEMPLATE = """ ### Project Name @@ -64,8 +65,9 @@ NEW_REQ_TEMPLATE = """ class WritePRD(Action): - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "" + content: Optional[str] = None + llm: LLM = Field(default_factory=LLM) async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput | Message: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are diff --git a/metagpt/const.py b/metagpt/const.py index b46bc15a4..9cf9726fc 100644 --- a/metagpt/const.py +++ b/metagpt/const.py @@ -60,7 +60,7 @@ SWAGGER_PATH = UT_PATH / "files/api/" UT_PY_PATH = UT_PATH / "files/ut/" API_QUESTIONS_PATH = UT_PATH / "files/question/" -SERDES_PATH = DEFAULT_WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project +SERDESER_PATH = DEFAULT_WORKSPACE_ROOT / "storage" # TODO to store `storage` under the individual generated project TMP = METAGPT_ROOT / "tmp" diff --git a/metagpt/environment.py b/metagpt/environment.py index 14da6cd95..19197bd10 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -54,31 +54,33 @@ class Environment(BaseModel): write_json_file(history_path, {"content": self.history}) def deserialize(self, stg_path: Path): + """ stg_path: ./storage/team/environment/ """ """ stg_path: ./storage/team/environment/ """ roles_path = stg_path.joinpath("roles.json") roles_info = read_json_file(roles_path) + roles = [] for role_info in roles_info: - role_class = role_info.get("role_class") - role_name = role_info.get("role_name") - - role_path = stg_path.joinpath(f"roles/{role_class}_{role_name}") + # role stored in ./environment/roles/{role_class}_{role_name} + role_path = stg_path.joinpath(f'roles/{role_info.get("role_class")}_{role_info.get("role_name")}') role = Role.deserialize(role_path) + roles.append(role) - self.add_role(role) + history = read_json_file(stg_path.joinpath("history.json")) + history = history.get("content") - memory = Memory.deserialize(stg_path) - self.memory = memory - - history_path = stg_path.joinpath("history.json") - history = read_json_file(history_path) - self.history = history.get("content") + environment = Environment(**{ + "history": history + }) + environment.add_roles(roles) + return environment def add_role(self, role: Role): """增加一个在当前环境的角色 Add a role in the current environment """ role.set_env(self) - self.roles[role.profile] = role + # use alias + self.roles[role.role_profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index fa91d393d..377531c8d 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -5,10 +5,11 @@ @Author : alexanderwu @File : architect.py """ +from pydantic import Field from metagpt.actions import WritePRD from metagpt.actions.design_api import WriteDesign -from metagpt.roles import Role +from metagpt.roles.role import Role class Architect(Role): @@ -21,18 +22,14 @@ class Architect(Role): goal (str): Primary goal or responsibility of the architect. constraints (str): Constraints or guidelines for the architect. """ + name: str = "Bob" + profile: str = Field(default="Architect", alias='profile') + goal: str = "design a concise, usable, complete software system" + constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries." \ + "Use same language as user requirement" - def __init__( - self, - name: str = "Bob", - profile: str = "Architect", - goal: str = "design a concise, usable, complete software system", - constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries." - "Use same language as user requirement" - ) -> None: - """Initializes the Architect with given attributes.""" - super().__init__(name, profile, goal, constraints) - + def __init__(self, **kwargs) -> None: + super().__init__(**kwargs) # Initialize actions specific to the Architect role self._init_actions([WriteDesign]) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index f1e65b177..59ca18a17 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -16,8 +16,9 @@ @Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results of SummarizeCode. """ -from __future__ import annotations +from __future__ import annotations +from pydantic import Field import json from collections import defaultdict from pathlib import Path @@ -44,9 +45,11 @@ from metagpt.schema import ( ) from metagpt.utils.common import any_to_str, any_to_str_set + IS_PASS_PROMPT = """ {context} +<<<<<<< HEAD ---- Does the above log indicate anything that needs to be done? If there are any tasks to be completed, please answer 'NO' along with the to-do list in JSON format; @@ -66,25 +69,21 @@ class Engineer(Role): n_borg (int): Number of borgs. use_code_review (bool): Whether to use code review. """ + name: str = "Alex" + role_profile: str = Field(default="Engineer", alias='profile') + goal: str = "write elegant, readable, extensible, efficient code" + constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " \ + "Use same language as user requirement", + n_borg: int = 1 + use_code_review: bool = False + code_todos: list = [] + summarize_todos = [] + + def __init__(self, **kwargs) -> None: + super().__init__(**kwargs) - def __init__( - self, - name: str = "Alex", - profile: str = "Engineer", - goal: str = "write elegant, readable, extensible, efficient code", - constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " - "Use same language as user requirement", - n_borg: int = 1, - use_code_review: bool = False, - ) -> None: - """Initializes the Engineer role with given attributes.""" - super().__init__(name, profile, goal, constraints) - self.use_code_review = use_code_review self._init_actions([WriteCode]) self._watch([WriteTasks, SummarizeCode, WriteCode, WriteCodeReview, FixBug]) - self.code_todos = [] - self.summarize_todos = [] - self.n_borg = n_borg @staticmethod def _parse_tasks(task_msg: Document) -> list[str]: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index e5e9f2b5e..a49459fca 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -7,40 +7,33 @@ @Modified By: mashenquan, 2023/11/27. Add `PrepareDocuments` action according to Section 2.2.3.5.1 of RFC 135. """ +from pydantic import Field + from metagpt.actions import UserRequirement, WritePRD from metagpt.actions.prepare_documents import PrepareDocuments from metagpt.config import CONFIG -from metagpt.roles import Role +from metagpt.roles.role import Role class ProductManager(Role): """ - Represents a Product Manager role responsible for product development and management. + Represents a Project Manager role responsible for overseeing project execution and team efficiency. Attributes: - name (str): Name of the product manager. - profile (str): Role profile, default is 'Product Manager'. - goal (str): Goal of the product manager. - constraints (str): Constraints or limitations for the product manager. + name (str): Name of the project manager. + profile (str): Role profile, default is 'Project Manager'. + goal (str): Goal of the project manager. + constraints (str): Constraints or limitations for the project manager. """ - - def __init__( - self, - name: str = "Alice", - profile: str = "Product Manager", - goal: str = "efficiently create a successful product", - constraints: str = "use same language as user requirement", - ) -> None: - """ - Initializes the ProductManager role with given attributes. - - Args: - name (str): Name of the product manager. - profile (str): Role profile. - goal (str): Goal of the product manager. - constraints (str): Constraints or limitations for the product manager. - """ - super().__init__(name, profile, goal, constraints) + name: str = "Alice" + role_profile: str = Field(default="Product Manager", alias='profile') + goal: str = "efficiently create a successful product" + constraints: str = "use same language as user requiremen" + """ + Represents a Product Manager role responsible for product development and management. + """ + def __init__(self, **kwargs) -> None: + super().__init__(**kwargs) self._init_actions([PrepareDocuments, WritePRD]) self._watch([UserRequirement, PrepareDocuments]) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 5a2b9be50..211e41d3b 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -5,9 +5,11 @@ @Author : alexanderwu @File : project_manager.py """ +from pydantic import Field + from metagpt.actions import WriteTasks from metagpt.actions.design_api import WriteDesign -from metagpt.roles import Role +from metagpt.roles.role import Role class ProjectManager(Role): @@ -20,24 +22,14 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ + name: str = "Eve" + profile: str = Field(default="Project Manager") + + goal: str = "reak down tasks according to PRD/technical design, generate a task list, and analyze task " \ + "dependencies to start with the prerequisite modules" + constraints: str = "use same language as user requirement" - def __init__( - self, - name: str = "Eve", - profile: str = "Project Manager", - goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " - "dependencies to start with the prerequisite modules", - constraints: str = "use same language as user requirement", - ) -> None: - """ - Initializes the ProjectManager role with given attributes. - - Args: - name (str): Name of the project manager. - profile (str): Role profile. - goal (str): Goal of the project manager. - constraints (str): Constraints or limitations for the project manager. - """ - super().__init__(name, profile, goal, constraints) + def __init__(self, **kwargs) -> None: + super().__init__(**kwargs) self._init_actions([WriteTasks]) self._watch([WriteDesign]) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index bb3b2acfe..07a78e4bb 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -18,14 +18,16 @@ @Modified By: mashenquan, 2023-11-4. According to the routing feature plan in Chapter 2.2.3.2 of RFC 113, the routing functionality is to be consolidated into the `Environment` class. """ + from __future__ import annotations + from enum import Enum from typing import Iterable, Set, Type from pathlib import Path from pydantic import BaseModel, Field -from metagpt.actions import Action, ActionOutput +from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.llm import LLM, HumanProvider @@ -35,6 +37,8 @@ from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output from metagpt.memory import Memory from metagpt.utils.utils import read_json_file, write_json_file, import_class +from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.const import SERDESER_PATH PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -45,14 +49,12 @@ Please note that only the text between the first and second "===" is information {history} === -Your previous stage: {previous_state} - -Now choose one of the following stages you need to go to in the next step: +You can now choose one of the following stages to decide the stage you need to go in the next step: {states} Just answer a number between 0-{n_states}, choose the most suitable stage according to the understanding of the conversation. Please note that the answer only needs a number, no need to add any other text. -If you think you have completed your goal and don't need to go to any of the stages, return -1. +If there is no conversation record, choose 0. Do not answer anything else, and do not add any other information in your answer. """ @@ -89,7 +91,7 @@ class RoleSetting(BaseModel): def __str__(self): return f"{self.name}({self.profile})" - + def __repr__(self): return self.__str__() @@ -112,7 +114,7 @@ class RoleContext(BaseModel): class Config: arbitrary_types_allowed = True - + def check(self, role_id: str): # if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: # self.long_term_memory.recover_memory(role_id, self) @@ -123,7 +125,7 @@ class RoleContext(BaseModel): def important_memory(self) -> list[Message]: """Get the information corresponding to the watched actions""" return self.memory.get_by_actions(self.watch) - + @property def history(self) -> list[Message]: return self.memory.get() @@ -139,56 +141,99 @@ class _RoleInjector(type): return instance -class Role(metaclass=_RoleInjector): - """Role/Agent""" +role_subclass_registry = {} - def __init__(self, name="", profile="", goal="", constraints="", desc="", is_human=False): - self._llm = LLM() if not is_human else HumanProvider() - self._setting = RoleSetting( - name=name, profile=profile, goal=goal, constraints=constraints, desc=desc, is_human=is_human - ) - self._llm.system_prompt = self._get_prefix() - self._states = [] - self._actions = [] - self._role_id = str(self._setting) - self._rc = RoleContext() + +class Role(BaseModel): + """Role/Agent""" + name: str = "" + profile: str = "" + goal: str = "" + constraints: str = "" + desc: str = "" + is_human: bool = False + + _llm: BaseGPTAPI = Field(default_factory=LLM) + _role_id: str = "" + _states: list[str] = Field(default=[]) + _actions: list[Action] = Field(default=[]) + _rc: RoleContext = Field(default=RoleContext) + _subscription: tuple = set() + + # builtin variables + recovered: bool = False # to tag if a recovered role + builtin_class_name: str = "" + + _private_attributes = { + "_llm": LLM() if not is_human else HumanProvider(), + "_role_id": _role_id, + "_states": [], + "_actions": [], + "_rc": RoleContext() + } + + class Config: + arbitrary_types_allowed = True + exclude = ["_llm"] + + def __init__(self, **kwargs): + for index in range(len(kwargs.get("_actions", []))): + current_action = kwargs["_actions"][index] + if isinstance(current_action, dict): + item_class_name = current_action.get("builtin_class_name", None) + for name, subclass in action_subclass_registry.items(): + registery_class_name = subclass.__fields__["builtin_class_name"].default + if item_class_name == registery_class_name: + current_action = subclass(**current_action) + break + kwargs["_actions"][index] = current_action + + super().__init__(**kwargs) + + # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 + self._private_attributes["_llm"] = LLM() if not self.is_human else HumanProvider() + self._private_attributes["_role_id"] = str(self._setting) self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} - self._recovered = False + for key in self._private_attributes.keys(): + if key in kwargs: + object.__setattr__(self, key, kwargs[key]) + if key == "_rc": + _rc = RoleContext(**kwargs["_rc"]) + object.__setattr__(self, "_rc", _rc) + else: + if key == "_rc": + # # Warning, if use self._private_attributes["_rc"], + # # self._rc will be a shared object between roles, so init one or reset it inside `_reset` + object.__setattr__(self, key, RoleContext()) + else: + object.__setattr__(self, key, self._private_attributes[key]) + + # deserialize child classes dynamically for inherited `role` + object.__setattr__(self, "builtin_class_name", self.__class__.__name__) + self.__fields__["builtin_class_name"].default = self.__class__.__name__ + + def _reset(self): + object.__setattr__(self, '_states', []) + object.__setattr__(self, '_actions', []) + + @property + def _setting(self): + return f"{self.name}({self.profile})" def serialize(self, stg_path: Path): - role_info_path = stg_path.joinpath("role_info.json") - role_info = { + stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ + if stg_path is None else stg_path + + role_info = self.dict(exclude={"_rc": {"memory": True}, "_llm": True}) + role_info.update({ "role_class": self.__class__.__name__, "module_name": self.__module__ - } - setting = self._setting.dict() - setting.pop("desc") - setting.pop("is_human") # not all inherited roles have this atrr - role_info.update(setting) + }) + role_info_path = stg_path.joinpath("role_info.json") write_json_file(role_info_path, role_info) - actions_info_path = stg_path.joinpath("actions/actions_info.json") - actions_info = [] - for action in self._actions: - actions_info.append(action.serialize()) - write_json_file(actions_info_path, actions_info) - - watches_info_path = stg_path.joinpath("watches/watches_info.json") - watches_info = [] - for watch in self._rc.watch: - watches_info.append(watch.ser_class()) - write_json_file(watches_info_path, watches_info) - - actions_todo_path = stg_path.joinpath("actions/todo.json") - actions_todo = { - "cur_state": self._rc.state, - "react_mode": self._rc.react_mode.value, - "max_react_loop": self._rc.max_react_loop - } - write_json_file(actions_todo_path, actions_todo) - - self._rc.memory.serialize(stg_path) + self._rc.memory.serialize(stg_path) # serialize role's memory alone @classmethod def deserialize(cls, stg_path: Path) -> "Role": @@ -201,45 +246,13 @@ class Role(metaclass=_RoleInjector): role_class = import_class(class_name=role_class_str, module_name=module_name) role = role_class(**role_info) # initiate particular Role - actions_info_path = stg_path.joinpath("actions/actions_info.json") - actions = [] - actions_info = read_json_file(actions_info_path) - for action_info in actions_info: - action = Action.deserialize(action_info) - actions.append(action) - - watches_info_path = stg_path.joinpath("watches/watches_info.json") - watches = [] - watches_info = read_json_file(watches_info_path) - for watch_info in watches_info: - action = Action.deser_class(watch_info) - watches.append(action) - - role.init_actions(actions) - role.watch(watches) - - actions_todo_path = stg_path.joinpath("actions/todo.json") - # recover self._rc.state - actions_todo = read_json_file(actions_todo_path) - max_react_loop = actions_todo.get("max_react_loop", 1) - cur_state = actions_todo.get("cur_state", -1) - role.set_state(cur_state) - role.set_recovered(True) - react_mode_str = actions_todo.get("react_mode", RoleReactMode.REACT.value) - if react_mode_str not in RoleReactMode.values(): - logger.warning(f"ReactMode: {react_mode_str} not in {RoleReactMode.values()}, use react as default") - react_mode_str = RoleReactMode.REACT.value - role.set_react_mode(RoleReactMode(react_mode_str), max_react_loop) + role.set_recovered(True) # set True to make a tag role_memory = Memory.deserialize(stg_path) role.set_memory(role_memory) return role - def _reset(self): - self._states = [] - self._actions = [] - def _init_action_system_message(self, action: Action): action.set_prefix(self._get_prefix(), self.profile) @@ -256,7 +269,8 @@ class Role(metaclass=_RoleInjector): self._reset() for idx, action in enumerate(actions): if not isinstance(action, Action): - i = action("", llm=self._llm) + ## 默认初始化 + i = action() else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): logger.warning( @@ -331,10 +345,6 @@ class Role(metaclass=_RoleInjector): if env: env.set_subscription(self, self._subscription) - @property - def name(self): - return self._setting.name - @property def profile(self): """Get the role description (position)""" @@ -355,7 +365,7 @@ class Role(metaclass=_RoleInjector): if self._setting.desc: return self._setting.desc return PREFIX_TEMPLATE.format(**self._setting.dict()) - + async def _think(self) -> None: """Think about what to do and decide on the next action""" if len(self._actions) == 1: @@ -378,6 +388,7 @@ class Role(metaclass=_RoleInjector): next_state = await self._llm.aask(prompt) next_state = extract_state_value_from_output(next_state) logger.debug(f"{prompt=}") + if (not next_state.isdigit() and next_state != "-1") or int(next_state) not in range(-1, len(self._states)): logger.warning(f"Invalid answer of state, {next_state=}, will be set to -1") next_state = -1 @@ -423,8 +434,8 @@ class Role(metaclass=_RoleInjector): if news_text: logger.debug(f"{self._setting} observed: {news_text}") return len(self._rc.news) - - def publish_message(self, msg): + + def _publish_message(self, msg): """If the role belongs to env, then the role's messages will be broadcast to env""" if not msg: return @@ -501,7 +512,7 @@ class Role(metaclass=_RoleInjector): def get_memories(self, k=0) -> list[Message]: """A wrapper to return the most recent k memories of this role, return all when k=0""" return self._rc.memory.get(k=k) - + async def run(self, with_message=None): """Observe, and think and act based on the results of the observation""" if with_message: From 9608a20c7127f3034e58293343249401d61a59ac Mon Sep 17 00:00:00 2001 From: stellahsr Date: Mon, 27 Nov 2023 21:13:19 +0800 Subject: [PATCH 289/413] update test cases for serialize_deserialize --- .../metagpt/serialize_deserialize/__init__.py | 4 ++ .../serialize_deserialize/test_actions.py | 24 ++++++++++ .../test_architect_deserialize.py | 26 ++++++++++ .../test_product_manager.py | 21 +++++++++ .../test_project_manager.py | 26 ++++++++++ .../serialize_deserialize/test_role.py | 41 ++++++++++++++++ .../serialize_deserialize/test_team.py | 47 +++++++++++++++++++ .../serialize_deserialize/test_wrire_prd.py | 28 +++++++++++ .../serialize_deserialize/test_write_code.py | 42 +++++++++++++++++ .../test_write_design.py | 39 +++++++++++++++ 10 files changed, 298 insertions(+) create mode 100644 tests/metagpt/serialize_deserialize/__init__.py create mode 100644 tests/metagpt/serialize_deserialize/test_actions.py create mode 100644 tests/metagpt/serialize_deserialize/test_architect_deserialize.py create mode 100644 tests/metagpt/serialize_deserialize/test_product_manager.py create mode 100644 tests/metagpt/serialize_deserialize/test_project_manager.py create mode 100644 tests/metagpt/serialize_deserialize/test_role.py create mode 100644 tests/metagpt/serialize_deserialize/test_team.py create mode 100644 tests/metagpt/serialize_deserialize/test_wrire_prd.py create mode 100644 tests/metagpt/serialize_deserialize/test_write_code.py create mode 100644 tests/metagpt/serialize_deserialize/test_write_design.py diff --git a/tests/metagpt/serialize_deserialize/__init__.py b/tests/metagpt/serialize_deserialize/__init__.py new file mode 100644 index 000000000..78f454fb5 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py new file mode 100644 index 000000000..e2efa982b --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_actions.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import Action +from metagpt.llm import LLM + +def test_action_serialize(): + action = Action() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = Action() + serialized_data = action.dict() + + new_action = Action(**serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action._aask("who are you")) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py new file mode 100644 index 000000000..cff1bbadd --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:04 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.architect import Architect +from metagpt.actions.action import Action + +def test_architect_serialize(): + role = Architect() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + +@pytest.mark.asyncio +async def test_architect_deserialize(): + role = Architect() + ser_role_dict = role.dict(by_alias=True) + new_role = Architect(**ser_role_dict) + # new_role = Architect.deserialize(ser_role_dict) + assert new_role.name == "Bob" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py new file mode 100644 index 000000000..978c50e5e --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:07 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.product_manager import ProductManager +from metagpt.actions.action import Action +from metagpt.schema import Message + +@pytest.mark.asyncio +async def test_product_manager_deserialize(): + role = ProductManager() + ser_role_dict = role.dict(by_alias=True) + new_role = ProductManager(**ser_role_dict) + # new_role = ProductManager().deserialize(ser_role_dict) + + assert new_role.name == "Alice" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run([Message(content="write a cli snake game")]) \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py new file mode 100644 index 000000000..590bd8109 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# @Date : 11/26/2023 2:06 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.project_manager import ProjectManager +from metagpt.actions.action import Action + +def test_project_manager_serialize(): + role = ProjectManager() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + +@pytest.mark.asyncio +async def test_project_manager_deserialize(): + role = ProjectManager() + ser_role_dict = role.dict(by_alias=True) + new_role = ProjectManager(**ser_role_dict) + # new_role = ProjectManager().deserialize(ser_role_dict) + assert new_role.name == "Eve" + assert len(new_role._actions) == 1 + assert isinstance(new_role._actions[0], Action) + await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py new file mode 100644 index 000000000..432c9acb7 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# @Date : 11/23/2023 4:49 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.roles.role import Role +from metagpt.roles.engineer import Engineer + +from metagpt.actions.action import Action + + +def test_role_serialize(): + role = Role() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + + +def test_engineer_serialize(): + role = Engineer() + ser_role_dict = role.dict(by_alias=True) + assert "name" in ser_role_dict + assert "_states" in ser_role_dict + assert "_actions" in ser_role_dict + + +@pytest.mark.asyncio +async def test_engineer_deserialize(): + role = Engineer(use_code_review=True) + ser_role_dict = role.dict(by_alias=True) + # new_role = Engineer().deserialize(ser_role_dict) + # also can be deserialized in this way: + new_role = Engineer(**ser_role_dict) + assert new_role.name == "Alex" + assert new_role.use_code_review == True + assert len(new_role._actions) == 2 + assert isinstance(new_role._actions[0], Action) + assert isinstance(new_role._actions[1], Action) + await new_role._actions[0].run(context="write a cli snake game", filename="test_code") diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py new file mode 100644 index 000000000..44a75d262 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# @Date : 11/27/2023 10:07 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.environment import Environment +from metagpt.schema import Message +from metagpt.software_company import SoftwareCompany +from metagpt.roles import ProjectManager, ProductManager, Architect + + +def test_env_serialize(): + env = Environment() + ser_env_dict = env.dict() + assert "roles" in ser_env_dict + assert "memory" in ser_env_dict + assert "memory" in ser_env_dict + + +def test_env_deserialize(): + env = Environment() + env.publish_message(message=Message(content="test env serialize")) + ser_env_dict = env.dict() + new_env = Environment(**ser_env_dict) + assert len(new_env.roles) == 0 + assert new_env.memory.storage[0].content == "test env serialize" + assert len(new_env.history) == 25 + + +def test_softwarecompany_deserialize(): + team = SoftwareCompany() + team.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) + assert len(team.environment.get_roles()) == 3 + ser_team_dict = team.dict() + new_team = SoftwareCompany(**ser_team_dict) + + assert len(new_team.environment.get_roles()) == 3 + assert new_team.environment.get_role('Product Manager') is not None + assert new_team.environment.get_role('Product Manager') is not None + assert new_team.environment.get_role('Architect') is not None diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py new file mode 100644 index 000000000..9b2653820 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 1:47 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WritePRD +from metagpt.llm import LLM +from metagpt.schema import Message + + +def test_action_serialize(): + action = WritePRD() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = WritePRD() + serialized_data = action.dict() + new_action = WritePRD(**serialized_data) + # new_action = WritePRD().deserialize(serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action.run([Message(content="write a cli snake game")]))>0 + diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py new file mode 100644 index 000000000..0b1f1dc7c --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# @Date : 11/23/2023 10:56 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.llm import LLM + +def test_write_design_serialize(): + action = WriteCode() + ser_action_dict = action.dict() + assert ser_action_dict["name"] == "WriteCode" + assert "llm" in ser_action_dict + +def test_write_task_serialize(): + action = WriteCodeReview() + ser_action_dict = action.dict() + assert ser_action_dict["name"] == "WriteCodeReview" + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_write_code_deserialize(): + action = WriteCode() + serialized_data = action.dict() + new_action = WriteCode(**serialized_data) + # new_action = WriteCode().deserialize(serialized_data) + assert new_action.name == "WriteCode" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game", filename="test_code") + +@pytest.mark.asyncio +async def test_write_code_review_deserialize(): + action = WriteCodeReview() + serialized_data = action.dict() + new_action = WriteCodeReview(**serialized_data) + # new_action = WriteCodeReview().deserialize(serialized_data) + code = await WriteCode().run(context="write a cli snake game", filename="test_code") + + assert new_action.name == "WriteCodeReview" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game", code =code, filename="test_rewrite_code") \ No newline at end of file diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py new file mode 100644 index 000000000..56bf78a63 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 8:19 PM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import WriteDesign, WriteTasks +from metagpt.llm import LLM + +def test_write_design_serialize(): + action = WriteDesign() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +def test_write_task_serialize(): + action = WriteTasks() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + +@pytest.mark.asyncio +async def test_write_design_deserialize(): + action = WriteDesign() + serialized_data = action.dict() + new_action = WriteDesign().deserialize(serialized_data) + assert new_action.name == "" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game") + +@pytest.mark.asyncio +async def test_write_task_deserialize(): + action = WriteTasks() + serialized_data = action.dict() + new_action = WriteTasks(**serialized_data) + # new_action = WriteTasks().deserialize(serialized_data) + assert new_action.name == "CreateTasks" + assert new_action.llm == LLM() + await new_action.run(context="write a cli snake game") \ No newline at end of file From c08f6d83d792bc66eafea7d0d1dca61db41b1916 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 28 Nov 2023 10:47:19 +0800 Subject: [PATCH 290/413] fix role and format ut of serialize_deserialize --- metagpt/roles/role.py | 5 ++--- tests/metagpt/serialize_deserialize/test_actions.py | 2 ++ .../serialize_deserialize/test_architect_deserialize.py | 2 ++ tests/metagpt/serialize_deserialize/test_product_manager.py | 1 + tests/metagpt/serialize_deserialize/test_project_manager.py | 2 ++ tests/metagpt/serialize_deserialize/test_role.py | 2 +- tests/metagpt/serialize_deserialize/test_wrire_prd.py | 4 ++-- tests/metagpt/serialize_deserialize/test_write_code.py | 6 +++++- tests/metagpt/serialize_deserialize/test_write_design.py | 6 +++++- 9 files changed, 22 insertions(+), 8 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 07a78e4bb..f1d7df5e7 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -20,8 +20,6 @@ """ from __future__ import annotations - - from enum import Enum from typing import Iterable, Set, Type from pathlib import Path @@ -30,12 +28,13 @@ from pydantic import BaseModel, Field from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement -from metagpt.llm import LLM, HumanProvider +from metagpt.llm import LLM from metagpt.logs import logger from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output from metagpt.memory import Memory +from metagpt.provider.human_provider import HumanProvider from metagpt.utils.utils import read_json_file, write_json_file, import_class from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.const import SERDESER_PATH diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py index e2efa982b..2fec2121a 100644 --- a/tests/metagpt/serialize_deserialize/test_actions.py +++ b/tests/metagpt/serialize_deserialize/test_actions.py @@ -7,12 +7,14 @@ import pytest from metagpt.actions import Action from metagpt.llm import LLM + def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + @pytest.mark.asyncio async def test_action_deserialize(): action = Action() diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index cff1bbadd..d0ee3bc99 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -7,6 +7,7 @@ import pytest from metagpt.roles.architect import Architect from metagpt.actions.action import Action + def test_architect_serialize(): role = Architect() ser_role_dict = role.dict(by_alias=True) @@ -14,6 +15,7 @@ def test_architect_serialize(): assert "_states" in ser_role_dict assert "_actions" in ser_role_dict + @pytest.mark.asyncio async def test_architect_deserialize(): role = Architect() diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 978c50e5e..2aed87a28 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -8,6 +8,7 @@ from metagpt.roles.product_manager import ProductManager from metagpt.actions.action import Action from metagpt.schema import Message + @pytest.mark.asyncio async def test_product_manager_deserialize(): role = ProductManager() diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index 590bd8109..fbc0dcc08 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -7,6 +7,7 @@ import pytest from metagpt.roles.project_manager import ProjectManager from metagpt.actions.action import Action + def test_project_manager_serialize(): role = ProjectManager() ser_role_dict = role.dict(by_alias=True) @@ -14,6 +15,7 @@ def test_project_manager_serialize(): assert "_states" in ser_role_dict assert "_actions" in ser_role_dict + @pytest.mark.asyncio async def test_project_manager_deserialize(): role = ProjectManager() diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 432c9acb7..0e438d1a2 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -34,7 +34,7 @@ async def test_engineer_deserialize(): # also can be deserialized in this way: new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" - assert new_role.use_code_review == True + assert new_role.use_code_review is True assert len(new_role._actions) == 2 assert isinstance(new_role._actions[0], Action) assert isinstance(new_role._actions[1], Action) diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 9b2653820..baa08ed76 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -24,5 +24,5 @@ async def test_action_deserialize(): # new_action = WritePRD().deserialize(serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - assert len(await new_action.run([Message(content="write a cli snake game")]))>0 - + assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 + diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 0b1f1dc7c..9d659caaf 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -7,18 +7,21 @@ import pytest from metagpt.actions import WriteCode, WriteCodeReview from metagpt.llm import LLM + def test_write_design_serialize(): action = WriteCode() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCode" assert "llm" in ser_action_dict + def test_write_task_serialize(): action = WriteCodeReview() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCodeReview" assert "llm" in ser_action_dict - + + @pytest.mark.asyncio async def test_write_code_deserialize(): action = WriteCode() @@ -29,6 +32,7 @@ async def test_write_code_deserialize(): assert new_action.llm == LLM() await new_action.run(context="write a cli snake game", filename="test_code") + @pytest.mark.asyncio async def test_write_code_review_deserialize(): action = WriteCodeReview() diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 56bf78a63..e6e236676 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -7,18 +7,21 @@ import pytest from metagpt.actions import WriteDesign, WriteTasks from metagpt.llm import LLM + def test_write_design_serialize(): action = WriteDesign() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + def test_write_task_serialize(): action = WriteTasks() ser_action_dict = action.dict() assert "name" in ser_action_dict assert "llm" in ser_action_dict + @pytest.mark.asyncio async def test_write_design_deserialize(): action = WriteDesign() @@ -28,6 +31,7 @@ async def test_write_design_deserialize(): assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") + @pytest.mark.asyncio async def test_write_task_deserialize(): action = WriteTasks() @@ -36,4 +40,4 @@ async def test_write_task_deserialize(): # new_action = WriteTasks().deserialize(serialized_data) assert new_action.name == "CreateTasks" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game") \ No newline at end of file + await new_action.run(context="write a cli snake game") From f7d5102fa62b06ad728f86b32e68023f7c4baa3c Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 15:10:38 +0800 Subject: [PATCH 291/413] update unittest of ser&deser --- tests/metagpt/actions/test_action.py | 17 --- tests/metagpt/roles/test_role.py | 84 +----------- .../serialize_deserialize/test_action.py | 49 +++++++ .../serialize_deserialize/test_actions.py | 26 ---- .../test_architect_deserialize.py | 2 +- .../serialize_deserialize/test_environment.py | 91 +++++++++++++ .../test_memory.py | 34 ++++- .../test_product_manager.py | 4 +- .../test_project_manager.py | 6 +- .../serialize_deserialize/test_role.py | 63 ++++++++- .../serialize_deserialize/test_schema.py | 49 +++++++ .../test_serdeser_base.py | 88 +++++++++++++ .../serialize_deserialize/test_team.py | 124 +++++++++++++----- .../serialize_deserialize/test_wrire_prd.py | 1 - .../serialize_deserialize/test_write_code.py | 2 +- tests/metagpt/test_environment.py | 44 +++---- tests/metagpt/test_schema.py | 4 +- tests/metagpt/test_team.py | 22 +--- 18 files changed, 496 insertions(+), 214 deletions(-) create mode 100644 tests/metagpt/serialize_deserialize/test_action.py delete mode 100644 tests/metagpt/serialize_deserialize/test_actions.py create mode 100644 tests/metagpt/serialize_deserialize/test_environment.py rename tests/metagpt/{memory => serialize_deserialize}/test_memory.py (52%) create mode 100644 tests/metagpt/serialize_deserialize/test_schema.py create mode 100644 tests/metagpt/serialize_deserialize/test_serdeser_base.py diff --git a/tests/metagpt/actions/test_action.py b/tests/metagpt/actions/test_action.py index 4468a6f6f..9775630cc 100644 --- a/tests/metagpt/actions/test_action.py +++ b/tests/metagpt/actions/test_action.py @@ -11,20 +11,3 @@ from metagpt.actions import Action, WritePRD, WriteTest def test_action_repr(): actions = [Action(), WriteTest(), WritePRD()] assert "WriteTest" in str(actions) - - -def test_action_serdes(): - action_info = WriteTest.ser_class() - assert action_info["action_class"] == "WriteTest" - - action_class = Action.deser_class(action_info) - assert action_class == WriteTest - - -def test_action_class_serdes(): - name = "write test" - action_info = WriteTest(name=name).serialize() - assert action_info["name"] == name - - action = Action.deserialize(action_info) - assert action.name == name diff --git a/tests/metagpt/roles/test_role.py b/tests/metagpt/roles/test_role.py index a19ad9cb5..72cd84a9a 100644 --- a/tests/metagpt/roles/test_role.py +++ b/tests/metagpt/roles/test_role.py @@ -2,84 +2,10 @@ # -*- coding: utf-8 -*- # @Desc : unittest of Role -from pathlib import Path -import shutil -import pytest - -from metagpt.roles.role import Role, RoleReactMode -from metagpt.actions.action import Action -from metagpt.schema import Message -from metagpt.actions.add_requirement import BossRequirement -from metagpt.roles.product_manager import ProductManager - -serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") +from metagpt.roles.role import Role -def test_role_serdes(): - stg_path_prefix = serdes_path.joinpath("team/environment/roles/") - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - pm = ProductManager() - role_tag = f"{pm.__class__.__name__}_{pm.name}" - stg_path = stg_path_prefix.joinpath(role_tag) - pm.serialize(stg_path) - assert stg_path.joinpath("actions/actions_info.json").exists() - - new_pm = Role.deserialize(stg_path) - assert new_pm.name == pm.name - assert len(new_pm.get_memories(1)) == 0 - - -class ActionOK(Action): - - async def run(self, messages: list["Message"]): - return "ok" - - -class ActionRaise(Action): - - async def run(self, messages: list["Message"]): - raise RuntimeError("parse error") - - -class RoleA(Role): - - def __init__(self, - name: str = "RoleA", - profile: str = "Role A", - goal: str = "", - constraints: str = ""): - super(RoleA, self).__init__(name=name, profile=profile, goal=goal, constraints=constraints) - self._init_actions([ActionOK, ActionRaise]) - self._watch([BossRequirement]) - self._rc.react_mode = RoleReactMode.BY_ORDER - - async def run(self, message: "Message" = None, stg_path: str = None): - try: - await super(RoleA, self).run(message) - except Exception as exp: - print("exp ", exp) - self.serialize(stg_path) - - -@pytest.mark.asyncio -async def test_role_serdes_interrupt(): - role_a = RoleA() - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - stg_path = serdes_path.joinpath(f"team/environment/roles/{role_a.__class__.__name__}_{role_a.name}") - await role_a.run( - message=Message(content="demo", cause_by=BossRequirement), - stg_path=stg_path - ) - assert role_a._rc.memory.count() == 2 - - assert stg_path.joinpath("actions/todo.json").exists() - - new_role_a: Role = Role.deserialize(stg_path) - assert new_role_a._rc.state == 1 - await role_a.run( - message=Message(content="demo", cause_by=BossRequirement), - stg_path=stg_path - ) - +def test_role_desc(): + role = Role(profile="Sales", desc="Best Seller") + assert role.profile == "Sales" + assert role._setting.desc == "Best Seller" diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py new file mode 100644 index 000000000..b624dff5a --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# @Date : 11/22/2023 11:48 AM +# @Author : stellahong (stellahong@fuzhi.ai) +# @Desc : +import pytest + +from metagpt.actions import Action, WritePRD, WriteTest +from metagpt.llm import LLM +from metagpt.provider.openai_api import OpenAIGPTAPI + + +def test_action_serialize(): + action = Action() + ser_action_dict = action.dict() + assert "name" in ser_action_dict + assert "llm" in ser_action_dict + + +@pytest.mark.asyncio +async def test_action_deserialize(): + action = Action() + serialized_data = action.dict() + assert isinstance(serialized_data["llm"], OpenAIGPTAPI) + + new_action = Action(**serialized_data) + + assert new_action.name == "" + assert new_action.llm == LLM() + assert len(await new_action._aask("who are you")) > 0 + + +def test_action_serdeser(): + action_info = WriteTest.ser_class() + assert action_info["action_class"] == "WriteTest" + + action_class = Action.deser_class(action_info) + assert action_class == WriteTest + + +def test_action_class_serdeser(): + name = "write test" + action_info = WriteTest(name=name).serialize() + assert action_info["name"] == name + + action_info = WriteTest(name=name, llm=LLM()).serialize() + assert action_info["name"] == name + + action = Action.deserialize(action_info) + assert action.name == name diff --git a/tests/metagpt/serialize_deserialize/test_actions.py b/tests/metagpt/serialize_deserialize/test_actions.py deleted file mode 100644 index 2fec2121a..000000000 --- a/tests/metagpt/serialize_deserialize/test_actions.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- -# @Date : 11/22/2023 11:48 AM -# @Author : stellahong (stellahong@fuzhi.ai) -# @Desc : -import pytest - -from metagpt.actions import Action -from metagpt.llm import LLM - - -def test_action_serialize(): - action = Action() - ser_action_dict = action.dict() - assert "name" in ser_action_dict - assert "llm" in ser_action_dict - - -@pytest.mark.asyncio -async def test_action_deserialize(): - action = Action() - serialized_data = action.dict() - - new_action = Action(**serialized_data) - assert new_action.name == "" - assert new_action.llm == LLM() - assert len(await new_action._aask("who are you")) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index d0ee3bc99..fb58f0a3a 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -25,4 +25,4 @@ async def test_architect_deserialize(): assert new_role.name == "Bob" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file + await new_role._actions[0].run(context="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py new file mode 100644 index 000000000..15336eb6a --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : + +from pathlib import Path +import shutil + +from metagpt.schema import Message +from metagpt.actions.action_output import ActionOutput +from metagpt.roles.project_manager import ProjectManager +from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.project_management import WriteTasks +from metagpt.environment import Environment +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, ActionOK, serdeser_path + + +def test_env_serialize(): + env = Environment() + ser_env_dict = env.dict() + assert "roles" in ser_env_dict + assert "memory" in ser_env_dict + + +def test_env_deserialize(): + env = Environment() + env.publish_message(message=Message(content="test env serialize")) + ser_env_dict = env.dict() + new_env = Environment(**ser_env_dict) + assert len(new_env.roles) == 0 + assert new_env.memory.storage[0].content == "test env serialize" + assert len(new_env.history) == 25 + + +def test_environment_serdeser(): + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("prd", out_mapping) + + message = Message( + content="prd", + instruct_content=ic_obj(**out_data), + role="product manager", + cause_by=BossRequirement + ) + + environment = Environment() + role_c = RoleC() + environment.add_role(role_c) + environment.publish_message(message) + + ser_data = environment.dict() + assert ser_data["roles"]["Role C"]["name"] == "RoleC" + + new_env: Environment = Environment(**ser_data) + assert len(new_env.roles) == 1 + + assert new_env.memory.count() == 1 + assert new_env.memory.storage[0].instruct_content == ic_obj(**out_data) + assert list(new_env.roles.values())[0]._states == list(environment.roles.values())[0]._states + assert list(new_env.roles.values())[0]._actions == list(environment.roles.values())[0]._actions + assert isinstance(list(environment.roles.values())[0]._actions[0], ActionOK) + assert type(list(new_env.roles.values())[0]._actions[0]) == ActionOK + + +def test_environment_serdeser_v2(): + environment = Environment() + pm = ProjectManager() + environment.add_role(pm) + + ser_data = environment.dict() + + new_env: Environment = Environment(**ser_data) + role = new_env.get_role(pm.profile) + assert isinstance(role, ProjectManager) + assert isinstance(role._actions[0], WriteTasks) + assert isinstance(list(new_env.roles.values())[0]._actions[0], WriteTasks) + + +def test_environment_serdeser_save(): + environment = Environment() + role_c = RoleC() + + shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) + + stg_path = serdeser_path.joinpath("team/environment") + environment.add_role(role_c) + environment.serialize(stg_path) + + new_env: Environment = Environment.deserialize(stg_path) + assert len(new_env.roles) == 1 + assert type(list(new_env.roles.values())[0]._actions[0]) == ActionOK diff --git a/tests/metagpt/memory/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py similarity index 52% rename from tests/metagpt/memory/test_memory.py rename to tests/metagpt/serialize_deserialize/test_memory.py index bda79ded1..e24f31af3 100644 --- a/tests/metagpt/memory/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -3,6 +3,7 @@ # @Desc : unittest of memory from pathlib import Path +from pydantic import BaseModel from metagpt.schema import Message from metagpt.memory.memory import Memory @@ -10,10 +11,36 @@ from metagpt.actions.action_output import ActionOutput from metagpt.actions.design_api import WriteDesign from metagpt.actions.add_requirement import BossRequirement -serdes_path = Path(__file__).absolute().parent.joinpath("../../data/serdes_storage") +from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path -def test_memory_serdes(): +def test_memory_serdeser(): + msg1 = Message(role="Boss", + content="write a snake game", + cause_by=BossRequirement) + + out_mapping = {"field2": (list[str], ...)} + out_data = {"field2": ["field2 value1", "field2 value2"]} + ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + msg2 = Message(role="Architect", + instruct_content=ic_obj(**out_data), + content="system design content", + cause_by=WriteDesign) + + memory = Memory() + memory.add_batch([msg1, msg2]) + ser_data = memory.dict() + + new_memory = Memory(**ser_data) + assert new_memory.count() == 2 + new_msg2 = new_memory.get(2)[0] + assert isinstance(new_msg2, BaseModel) + assert isinstance(new_memory.storage[-1], BaseModel) + assert new_memory.storage[-1].cause_by == WriteDesign + assert new_msg2.role == "Boss" + + +def test_memory_serdeser_save(): msg1 = Message(role="User", content="write a 2048 game", cause_by=BossRequirement) @@ -29,7 +56,7 @@ def test_memory_serdes(): memory = Memory() memory.add_batch([msg1, msg2]) - stg_path = serdes_path.joinpath("team/environment") + stg_path = serdeser_path.joinpath("team/environment") memory.serialize(stg_path) assert stg_path.joinpath("memory.json").exists() @@ -38,5 +65,6 @@ def test_memory_serdes(): new_msg2 = new_memory.get(1)[0] assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] assert new_msg2.cause_by == WriteDesign + assert len(new_memory.index) == 2 stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 2aed87a28..54584cf96 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -15,8 +15,8 @@ async def test_product_manager_deserialize(): ser_role_dict = role.dict(by_alias=True) new_role = ProductManager(**ser_role_dict) # new_role = ProductManager().deserialize(ser_role_dict) - + assert new_role.name == "Alice" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run([Message(content="write a cli snake game")]) \ No newline at end of file + await new_role._actions[0].run([Message(content="write a cli snake game")]) diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index fbc0dcc08..21fafa72e 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -6,6 +6,7 @@ import pytest from metagpt.roles.project_manager import ProjectManager from metagpt.actions.action import Action +from metagpt.actions.project_management import WriteTasks def test_project_manager_serialize(): @@ -20,9 +21,10 @@ def test_project_manager_serialize(): async def test_project_manager_deserialize(): role = ProjectManager() ser_role_dict = role.dict(by_alias=True) + new_role = ProjectManager(**ser_role_dict) - # new_role = ProjectManager().deserialize(ser_role_dict) assert new_role.name == "Eve" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run(context="write a cli snake game") \ No newline at end of file + assert isinstance(new_role._actions[0], WriteTasks) + # await new_role._actions[0].run(context="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 0e438d1a2..f260dea3a 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -2,12 +2,22 @@ # @Date : 11/23/2023 4:49 PM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + +from pathlib import Path +import shutil import pytest +from metagpt.logs import logger from metagpt.roles.role import Role +from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.schema import Message +from metagpt.actions.add_requirement import BossRequirement +from metagpt.roles.product_manager import ProductManager +from metagpt.const import SERDESER_PATH from metagpt.roles.engineer import Engineer +from metagpt.utils.utils import format_trackback_info -from metagpt.actions.action import Action +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, serdeser_path def test_role_serialize(): @@ -30,12 +40,53 @@ def test_engineer_serialize(): async def test_engineer_deserialize(): role = Engineer(use_code_review=True) ser_role_dict = role.dict(by_alias=True) - # new_role = Engineer().deserialize(ser_role_dict) - # also can be deserialized in this way: + new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" assert new_role.use_code_review is True assert len(new_role._actions) == 2 - assert isinstance(new_role._actions[0], Action) - assert isinstance(new_role._actions[1], Action) - await new_role._actions[0].run(context="write a cli snake game", filename="test_code") + assert isinstance(new_role._actions[0], WriteCode) + assert isinstance(new_role._actions[1], WriteCodeReview) + # await new_role._actions[0].run(context="write a cli snake game", filename="test_code") + + +def test_role_serdeser_save(): + stg_path_prefix = serdeser_path.joinpath("team/environment/roles/") + shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) + + pm = ProductManager() + role_tag = f"{pm.__class__.__name__}_{pm.name}" + stg_path = stg_path_prefix.joinpath(role_tag) + pm.serialize(stg_path) + assert stg_path.joinpath("actions/actions_info.json").exists() + + new_pm = Role.deserialize(stg_path) + assert new_pm.name == pm.name + assert len(new_pm.get_memories(1)) == 0 + + +@pytest.mark.asyncio +async def test_role_serdeser_interrupt(): + role_c = RoleC() + shutil.rmtree(SERDESER_PATH.joinpath("team"), ignore_errors=True) + + stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{role_c.__class__.__name__}_{role_c.name}") + try: + await role_c.run( + message=Message(content="demo", cause_by=BossRequirement) + ) + except Exception as exp: + logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") + role_c.serialize(stg_path) + + assert role_c._rc.memory.count() == 2 + + assert stg_path.joinpath("actions/todo.json").exists() + + new_role_a: Role = Role.deserialize(stg_path) + assert new_role_a._rc.state == 1 + + with pytest.raises(Exception): + await role_c.run( + message=Message(content="demo", cause_by=BossRequirement) + ) diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py new file mode 100644 index 000000000..74b134cad --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of schema ser&deser + +from metagpt.schema import Message +from metagpt.actions.action_output import ActionOutput +from metagpt.actions.write_code import WriteCode + +from tests.metagpt.serialize_deserialize.test_serdeser_base import MockMessage + + +def test_message_serdeser(): + out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} + out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + + message = Message( + content="code", + instruct_content=ic_obj(**out_data), + role="engineer", + cause_by=WriteCode + ) + ser_data = message.dict() + assert ser_data["cause_by"] == { + "action_class": "WriteCode", + "module_name": "metagpt.actions.write_code" + } + assert ser_data["instruct_content"]["class"] == "code" + + new_message = Message(**ser_data) + assert new_message.cause_by == WriteCode + assert new_message.cause_by in [WriteCode] + assert new_message.instruct_content == ic_obj(**out_data) + + +def test_message_without_postprocess(): + """ to explain `instruct_content` should be postprocessed """ + out_mapping = {"field1": (list[str], ...)} + out_data = {"field1": ["field1 value1", "field1 value2"]} + ic_obj = ActionOutput.create_model_class("code", out_mapping) + message = MockMessage( + content="code", + instruct_content=ic_obj(**out_data) + ) + ser_data = message.dict() + assert ser_data["instruct_content"] == {"field1": ["field1 value1", "field1 value2"]} + + new_message = MockMessage(**ser_data) + assert new_message.instruct_content != ic_obj(**out_data) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py new file mode 100644 index 000000000..35bad6cd9 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : base test actions / roles used in unittest + +from pydantic import BaseModel, Field +from pathlib import Path + +from metagpt.actions.action import Action +from metagpt.roles.role import Role, RoleReactMode +from metagpt.actions.add_requirement import BossRequirement + + +serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage") + + +class MockMessage(BaseModel): + """ to test normal dict without postprocess """ + content: str = "" + instruct_content: BaseModel = Field(default=None) + + +class ActionPass(Action): + name: str = "ActionPass" + + async def run(self, messages: list["Message"]): + return "pass" + + +class ActionOK(Action): + name: str = "ActionOK" + + async def run(self, messages: list["Message"]): + return "ok" + + +class ActionRaise(Action): + name: str = "ActionRaise" + + async def run(self, messages: list["Message"]): + raise RuntimeError("parse error in ActionRaise") + + +class RoleA(Role): + + name: str = Field(default="RoleA") + profile: str = Field(default="Role A") + goal: str = "RoleA's goal" + constraints: str = "RoleA's constraints" + + def __init__(self, **kwargs): + super(RoleA, self).__init__(**kwargs) + self._init_actions([ActionPass]) + self._watch([BossRequirement]) + + async def run(self, message: "Message" = None): + await super(RoleA, self).run(message) + + +class RoleB(Role): + name: str = Field(default="RoleB") + profile: str = Field(default="Role B") + goal: str = "RoleB's goal" + constraints: str = "RoleB's constraints" + + def __init__(self, **kwargs): + super(RoleB, self).__init__(**kwargs) + self._init_actions([ActionOK, ActionRaise]) + self._watch([ActionPass]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None): + await super(RoleB, self).run(message) + + +class RoleC(Role): + name: str = Field(default="RoleC") + profile: str = Field(default="Role C") + goal: str = "RoleC's goal" + constraints: str = "RoleC's constraints" + + def __init__(self, **kwargs): + super(RoleC, self).__init__(**kwargs) + self._init_actions([ActionOK, ActionRaise]) + self._watch([BossRequirement]) + self._rc.react_mode = RoleReactMode.BY_ORDER + + async def run(self, message: "Message" = None): + await super(RoleC, self).run(message) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 44a75d262..e9122ebc0 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -2,46 +2,104 @@ # @Date : 11/27/2023 10:07 AM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + +from pathlib import Path +import shutil import pytest -from metagpt.environment import Environment -from metagpt.schema import Message -from metagpt.software_company import SoftwareCompany from metagpt.roles import ProjectManager, ProductManager, Architect +from metagpt.team import Team +from metagpt.const import SERDESER_PATH + +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path -def test_env_serialize(): - env = Environment() - ser_env_dict = env.dict() - assert "roles" in ser_env_dict - assert "memory" in ser_env_dict - assert "memory" in ser_env_dict +def test_team_deserialize(): + company = Team() - -def test_env_deserialize(): - env = Environment() - env.publish_message(message=Message(content="test env serialize")) - ser_env_dict = env.dict() - new_env = Environment(**ser_env_dict) - assert len(new_env.roles) == 0 - assert new_env.memory.storage[0].content == "test env serialize" - assert len(new_env.history) == 25 - - -def test_softwarecompany_deserialize(): - team = SoftwareCompany() - team.hire( + pm = ProductManager() + arch = Architect() + company.hire( [ - ProductManager(), - Architect(), + pm, + arch, ProjectManager(), ] ) - assert len(team.environment.get_roles()) == 3 - ser_team_dict = team.dict() - new_team = SoftwareCompany(**ser_team_dict) - - assert len(new_team.environment.get_roles()) == 3 - assert new_team.environment.get_role('Product Manager') is not None - assert new_team.environment.get_role('Product Manager') is not None - assert new_team.environment.get_role('Architect') is not None + assert len(company.environment.get_roles()) == 3 + ser_company = company.dict() + new_company = Team(**ser_company) + + assert len(new_company.environment.get_roles()) == 3 + assert new_company.environment.get_role(pm.profile) is not None + + new_pm = new_company.environment.get_role(pm.profile) + assert type(new_pm) == ProductManager + assert new_company.environment.get_role(pm.profile) is not None + assert new_company.environment.get_role(arch.profile) is not None + + +def test_team_serdeser(): + company = Team() + company.hire([RoleC()]) + + stg_path = serdeser_path.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company.serialize(stg_path=stg_path) + + new_company = Team.deserialize(stg_path) + + assert len(new_company.environment.roles) == 1 + + +@pytest.mark.asyncio +async def test_team_recover(): + idea = "write a snake game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleC()]) + company.start_project(idea) + await company.run(n_round=4) + + ser_data = company.dict() + new_company = Team(**ser_data) + assert new_company.environment.memory.count() == 1 + assert type(list(new_company.environment.roles.values())[0]._actions[0]) == ActionOK + + new_company.start_project(idea) + await new_company.run(n_round=4) + + +@pytest.mark.asyncio +async def test_team_recover_save(): + idea = "write a 2048 web game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleC()]) + company.start_project(idea) + await company.run(n_round=4) + + new_company = Team.recover(stg_path) + new_company.start_project(idea) + await new_company.run(n_round=4) + + +@pytest.mark.asyncio +async def test_team_recover_multi_roles_save(): + idea = "write a snake game" + stg_path = SERDESER_PATH.joinpath("team") + shutil.rmtree(stg_path, ignore_errors=True) + + company = Team() + company.hire([RoleA(), RoleB()]) + company.start_project(idea) + await company.run(n_round=4) + + new_company = Team.recover(stg_path) + new_company.start_project(idea) + await new_company.run(n_round=4) diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index baa08ed76..96b4d19ad 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -25,4 +25,3 @@ async def test_action_deserialize(): assert new_action.name == "" assert new_action.llm == LLM() assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 - diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 9d659caaf..7f4799014 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -43,4 +43,4 @@ async def test_write_code_review_deserialize(): assert new_action.name == "WriteCodeReview" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game", code =code, filename="test_rewrite_code") \ No newline at end of file + await new_action.run(context="write a cli snake game", code=code, filename="test_rewrite_code") diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index 03236a08b..8aacdd77b 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -8,7 +8,6 @@ import pytest from pathlib import Path -import shutil from metagpt.actions import UserRequirement from metagpt.environment import Environment @@ -16,10 +15,9 @@ from metagpt.logs import logger from metagpt.manager import Manager from metagpt.roles import Architect, ProductManager, Role from metagpt.schema import Message -from tests.metagpt.roles.test_role import RoleA -serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") +serdeser_path = Path(__file__).absolute().parent.joinpath("../data/serdeser_storage") @pytest.fixture @@ -28,14 +26,23 @@ def env(): def test_add_role(env: Environment): - role = ProductManager("Alice", "product manager", "create a new product", "limited resources") + role = ProductManager(name="Alice", + profile="product manager", + goal="create a new product", + constraints="limited resources") env.add_role(role) assert env.get_role(role.profile) == role def test_get_roles(env: Environment): - role1 = Role("Alice", "product manager", "create a new product", "limited resources") - role2 = Role("Bob", "engineer", "develop the new product", "short deadline") + role1 = Role(name="Alice", + profile="product manager", + goal="create a new product", + constraints="limited resources") + role2 = Role(name="Bob", + profile="engineer", + goal="develop the new product", + constraints="short deadline") env.add_role(role1) env.add_role(role2) roles = env.get_roles() @@ -44,8 +51,14 @@ def test_get_roles(env: Environment): @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): - product_manager = ProductManager("Alice", "Product Manager", "做AI Native产品", "资源有限") - architect = Architect("Bob", "Architect", "设计一个可用、高效、较低成本的系统,包括数据结构与接口", "资源有限,需要节省成本") + product_manager = ProductManager(name="Alice", + profile="Product Manager", + goal="做AI Native产品", + constraints="资源有限") + architect = Architect(name="Bob", + profile="Architect", + goal="设计一个可用、高效、较低成本的系统,包括数据结构与接口", + constraints="资源有限,需要节省成本") env.add_roles([product_manager, architect]) env.set_manager(Manager()) @@ -54,18 +67,3 @@ async def test_publish_and_process_message(env: Environment): await env.run(k=2) logger.info(f"{env.history=}") assert len(env.history) > 10 - - -def test_environment_serdes(): - environment = Environment() - role_a = RoleA() - - shutil.rmtree(serdes_path.joinpath("team"), ignore_errors=True) - - stg_path = serdes_path.joinpath("team/environment") - environment.add_role(role_a) - environment.serialize(stg_path) - - new_env: Environment = Environment() - new_env.deserialize(stg_path) - assert len(new_env.roles) == 1 diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 4a6f518b1..5eea789ea 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -74,7 +74,7 @@ def test_routes(): assert m.send_to == {"e", get_class_name(Action)} -def test_message_serdes(): +def test_message_serdeser(): out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} ic_obj = ActionOutput.create_model_class("code", out_mapping) @@ -86,7 +86,7 @@ def test_message_serdes(): cause_by=WriteCode ) message_dict = serialize_general_message(message) - assert message_dict["cause_by"] == {"action_class": "WriteCode"} + assert message_dict["cause_by"] == {"action_class": "WriteCode", "module_name": "metagpt.actions.write_code"} assert message_dict["instruct_content"] == { "class": "code", "mapping": { diff --git a/tests/metagpt/test_team.py b/tests/metagpt/test_team.py index ab201152c..efd035bb2 100644 --- a/tests/metagpt/test_team.py +++ b/tests/metagpt/test_team.py @@ -2,26 +2,12 @@ # -*- coding: utf-8 -*- # @Desc : unittest of team -from pathlib import Path -import shutil - from metagpt.team import Team - -from tests.metagpt.roles.test_role import RoleA - -serdes_path = Path(__file__).absolute().parent.joinpath("../data/serdes_storage") +from metagpt.roles.project_manager import ProjectManager -def test_team_serdes(): +def test_team(): company = Team() - company.hire([RoleA()]) + company.hire([ProjectManager()]) - stg_path = serdes_path.joinpath("team") - shutil.rmtree(stg_path, ignore_errors=True) - - company.serialize(stg_path=stg_path) - - new_company = Team() - new_company.deserialize(stg_path) - - assert len(new_company.environment.roles) == 1 + assert len(company.environment.roles) == 1 From 2abe99cf45ec07bf69c44ec4c374704a798fd4c6 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 15:18:24 +0800 Subject: [PATCH 292/413] update environment/message to BaseModel, update the ser&deser of roles/actions --- metagpt/actions/action.py | 28 ++++- metagpt/actions/design_api.py | 3 +- metagpt/actions/project_management.py | 1 + metagpt/actions/search_and_summarize.py | 7 +- metagpt/actions/write_code.py | 9 +- metagpt/actions/write_code_review.py | 3 +- metagpt/actions/write_prd.py | 3 +- metagpt/actions/write_test.py | 11 +- metagpt/environment.py | 20 +++- metagpt/memory/longterm_memory.py | 14 ++- metagpt/memory/memory.py | 64 +++++++---- metagpt/roles/customer_service.py | 16 ++- metagpt/roles/product_manager.py | 1 + metagpt/roles/project_manager.py | 2 +- metagpt/roles/qa_engineer.py | 24 +++-- metagpt/roles/role.py | 52 ++++++--- metagpt/roles/sales.py | 33 +++--- metagpt/roles/searcher.py | 23 ++-- metagpt/schema.py | 134 ++++++++++-------------- metagpt/team.py | 38 ++++--- metagpt/utils/serialize.py | 26 +++-- metagpt/utils/utils.py | 40 +++++++ startup.py | 17 +-- 23 files changed, 361 insertions(+), 208 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index e890ef76a..499b5e794 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -6,12 +6,17 @@ @File : action.py """ +from __future__ import annotations +import re +from typing import Optional, Any + from typing import Optional, Any from tenacity import retry, stop_after_attempt, wait_random_exponential from pydantic import BaseModel, Field from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess from metagpt.utils.common import OutputParser @@ -24,18 +29,31 @@ action_subclass_registry = {} class Action(BaseModel): name: str = "" - llm: LLM = Field(default_factory=LLM) - context = None + llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) + context = "" prefix = "" # aask*时会加上prefix,作为system_message profile = "" # FIXME: USELESS desc = "" # for skill manager - nodes = None # content: Optional[str] = None # instruct_content: Optional[str] = None + + # builtin variables + builtin_class_name: str = "" + + class Config: + arbitrary_types_allowed = True def __init__(self, **kwargs: Any): super().__init__(**kwargs) + # deserialize child classes dynamically for inherited `action` + object.__setattr__(self, "builtin_class_name", self.__class__.__name__) + self.__fields__["builtin_class_name"].default = self.__class__.__name__ + + def __init_subclass__(cls, **kwargs: Any) -> None: + super().__init_subclass__(**kwargs) + action_subclass_registry[cls.__name__] = cls + def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix @@ -56,14 +74,14 @@ class Action(BaseModel): } @classmethod - def deserialize(cls, action_dict: dict): + def deserialize(cls, action_dict: dict) -> "Action": action_class_str = action_dict.pop("action_class") module_name = action_dict.pop("module_name") action_class = import_class(action_class_str, module_name) return action_class(**action_dict) @classmethod - def ser_class(cls): + def ser_class(cls) -> dict: """ serialize class type""" return { "action_class": cls.__name__, diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index a10ff1c9a..504328582 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -17,6 +17,7 @@ from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.actions.design_api_an import DESIGN_API_NODE from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import ( DATA_API_DESIGN_FILE_REPO, @@ -43,7 +44,7 @@ NEW_REQ_TEMPLATE = """ class WriteDesign(Action): name: str = "" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " "data structures, library tables, processes, and paths. Please provide your design, feedback " "clearly and in detail." diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index d830a4c15..98a948b64 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -18,6 +18,7 @@ from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.actions.project_management_an import PM_NODE from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import ( PACKAGE_REQUIREMENTS_FILENAME, diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 7b549518e..7bff1c113 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -11,7 +11,8 @@ from pydantic import BaseModel, Field from metagpt.actions import Action from metagpt.llm import LLM -from metagpt.config import Config +from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.config import Config, CONFIG from metagpt.logs import logger from metagpt.schema import Message from metagpt.tools.search_engine import SearchEngine @@ -106,9 +107,9 @@ You are a member of a professional butler team and will provide helpful suggesti class SearchAndSummarize(Action): name: str = "" content: Optional[str] = None - llm: None = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) config: None = Field(default_factory=Config) - engine: Optional[str] = None + engine: Optional[str] = CONFIG.search_engine search_func: Optional[str] = None search_engine: SearchEngine = None diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 2d155e6bf..bad9a0890 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -16,14 +16,9 @@ """ import json - from tenacity import retry, stop_after_attempt, wait_random_exponential - - - from typing import List, Optional, Any from pydantic import Field -from tenacity import retry, stop_after_attempt, wait_fixed from metagpt.actions.action import Action from metagpt.config import CONFIG @@ -34,8 +29,8 @@ from metagpt.const import ( TASK_FILE_REPO, TEST_OUTPUTS_FILE_REPO, ) -from metagpt.actions import WriteDesign from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser @@ -95,7 +90,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): name: str = "WriteCode" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code(self, prompt) -> str: diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index bf07d0a93..83225060a 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -18,6 +18,7 @@ from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import CodingContext +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -124,7 +125,7 @@ REWRITE_CODE_TEMPLATE = """ class WriteCodeReview(Action): name: str = "WriteCodeReview" context: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) async def write_code_review_and_rewrite(self, context_prompt, cr_prompt, filename): diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 7f9089763..8510733ac 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -27,6 +27,7 @@ from metagpt.actions.write_prd_an import ( WRITE_PRD_NODE, ) from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.const import ( @@ -67,7 +68,7 @@ NEW_REQ_TEMPLATE = """ class WritePRD(Action): name: str = "" content: Optional[str] = None - llm: LLM = Field(default_factory=LLM) + llm: BaseGPTAPI = Field(default_factory=LLM) async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput | Message: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index 9dd967788..fa3931ba6 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -7,6 +7,12 @@ @Modified By: mashenquan, 2023-11-27. Following the think-act principle, solidify the task parameters when creating the WriteTest object, rather than passing them in when calling the run function. """ + +from typing import Optional +from pydantic import Field + +from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.const import TEST_CODES_FILE_REPO @@ -36,8 +42,9 @@ you should correctly import the necessary classes based on these file locations! class WriteTest(Action): - def __init__(self, name="WriteTest", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "WriteTest" + context: Optional[str] = None + llm: BaseGPTAPI = Field(default_factory=LLM) async def write_code(self, prompt): code_rsp = await self._aask(prompt) diff --git a/metagpt/environment.py b/metagpt/environment.py index 19197bd10..242581e17 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -19,6 +19,8 @@ from pydantic import BaseModel, Field from metagpt.logs import logger from metagpt.roles import Role +from metagpt.memory import Memory +from metagpt.roles.role import Role, role_subclass_registry from metagpt.schema import Message from metagpt.utils.common import is_subscribed from metagpt.utils.utils import read_json_file, write_json_file @@ -37,6 +39,19 @@ class Environment(BaseModel): class Config: arbitrary_types_allowed = True + def __init__(self, **kwargs): + for role_key, role in kwargs.get("roles", {}).items(): + current_role = kwargs["roles"][role_key] + if isinstance(current_role, dict): + item_class_name = current_role.get("builtin_class_name", None) + for name, subclass in role_subclass_registry.items(): + registery_class_name = subclass.__fields__["builtin_class_name"].default + if item_class_name == registery_class_name: + current_role = subclass(**current_role) + break + kwargs["roles"][role_key] = current_role + super().__init__(**kwargs) + def serialize(self, stg_path: Path): roles_path = stg_path.joinpath("roles.json") roles_info = [] @@ -53,7 +68,8 @@ class Environment(BaseModel): history_path = stg_path.joinpath("history.json") write_json_file(history_path, {"content": self.history}) - def deserialize(self, stg_path: Path): + @classmethod + def deserialize(cls, stg_path: Path) -> "Environment": """ stg_path: ./storage/team/environment/ """ """ stg_path: ./storage/team/environment/ """ roles_path = stg_path.joinpath("roles.json") @@ -80,7 +96,7 @@ class Environment(BaseModel): """ role.set_env(self) # use alias - self.roles[role.role_profile] = role + self.roles[role.profile] = role def add_roles(self, roles: Iterable[Role]): """增加一批在当前环境的角色 diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index 22032a86e..e8a5be395 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -4,6 +4,9 @@ @Desc : the implement of Long-term memory """ +from typing import Optional +from pydantic import Field + from metagpt.logs import logger from metagpt.memory import Memory from metagpt.memory.memory_storage import MemoryStorage @@ -17,11 +20,12 @@ class LongTermMemory(Memory): - update memory when it changed """ - def __init__(self): - self.memory_storage: MemoryStorage = MemoryStorage() - super(LongTermMemory, self).__init__() - self.rc = None # RoleContext - self.msg_from_recover = False + memory_storage: MemoryStorage = Field(default_factory=MemoryStorage) + rc: Optional["RoleContext"] = None + msg_from_recover: bool = False + + class Config: + arbitrary_types_allowed = True def recover_memory(self, role_id: str, rc: "RoleContext"): messages = self.memory_storage.recover_memory(role_id) diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 43bd33e59..adef0d283 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -6,34 +6,51 @@ @File : memory.py @Modified By: mashenquan, 2023-11-1. According to RFC 116: Updated the type of index key. """ +import copy from collections import defaultdict -from typing import Iterable, Set +from typing import Iterable, Type, Union, Optional, Set from pathlib import Path +from pydantic import BaseModel, Field +import json from metagpt.schema import Message from metagpt.utils.common import any_to_str, any_to_str_set from metagpt.utils.utils import read_json_file, write_json_file -from metagpt.utils.serialize import serialize_general_message, deserialize_general_message +from metagpt.utils.utils import import_class -class Memory: +class Memory(BaseModel): """The most basic memory: super-memory""" - def __init__(self): - """Initialize an empty storage list and an empty index dictionary""" - self.storage: list[Message] = [] - self.index: dict[str, list[Message]] = defaultdict(list) + storage: list[Message] = Field(default=[]) + index: dict[str, list[Message]] = Field(default_factory=defaultdict(list)) + + def __init__(self, **kwargs): + index = kwargs.get("index", {}) + new_index = defaultdict(list) + for action_str, value in index.items(): + action_dict = json.loads(action_str) + action_class = import_class("Action", "metagpt.actions.action") + action_obj = action_class.deser_class(action_dict) + new_index[action_obj] = [Message(**item_dict) for item_dict in value] + kwargs["index"] = new_index + super(Memory, self).__init__(**kwargs) + self.index = new_index + + def dict(self, *args, **kwargs) -> "DictStrAny": + """ overwrite the `dict` to dump dynamic pydantic model""" + obj_dict = super(Memory, self).dict(*args, **kwargs) + new_obj_dict = copy.deepcopy(obj_dict) + new_obj_dict["index"] = {} + for action, value in obj_dict["index"].items(): + action_ser = json.dumps(action.ser_class()) + new_obj_dict["index"][action_ser] = value + return new_obj_dict def serialize(self, stg_path: Path): """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ memory_path = stg_path.joinpath("memory.json") - - storage = [] - for message in self.storage: - # msg_dict = message.serialize() - msg_dict = serialize_general_message(message) - storage.append(msg_dict) - + storage = self.dict() write_json_file(memory_path, storage) @classmethod @@ -41,13 +58,8 @@ class Memory: """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" memory_path = stg_path.joinpath("memory.json") - memory = Memory() - memory_list = read_json_file(memory_path) - for message in memory_list: - # distinguish instruct_content type in message - # msg = Message.deserialize(message) - msg = deserialize_general_message(message) - memory.add(msg) + memory_dict = read_json_file(memory_path) + memory = Memory(**memory_dict) return memory @@ -71,6 +83,16 @@ class Memory: """Return all messages containing a specified content""" return [message for message in self.storage if content in message.content] + def delete_newest(self) -> "Message": + """ delete the newest message from the storage""" + if len(self.storage) > 0: + newest_msg = self.storage.pop() + if newest_msg.cause_by and newest_msg in self.index[newest_msg.cause_by]: + self.index[newest_msg.cause_by].remove(newest_msg) + else: + newest_msg = None + return newest_msg + def delete(self, message: Message): """Delete the specified message from storage, while updating the index""" self.storage.remove(message) diff --git a/metagpt/roles/customer_service.py b/metagpt/roles/customer_service.py index 188182d47..62792696f 100644 --- a/metagpt/roles/customer_service.py +++ b/metagpt/roles/customer_service.py @@ -5,6 +5,9 @@ @Author : alexanderwu @File : sales.py """ +from typing import Optional +from pydantic import Field + from metagpt.roles import Sales # from metagpt.actions import SearchAndSummarize @@ -24,5 +27,14 @@ DESC = """ class CustomerService(Sales): - def __init__(self, name="Xiaomei", profile="Human customer service", desc=DESC, store=None): - super().__init__(name, profile, desc=desc, store=store) + + name: str = Field(default="Xiaomei") + profile: str = Field(default="Human customer service") + desc: str = DESC, + + store: Optional[str] = None + + def __init__( + self, + **kwargs): + super().__init__(**kwargs) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index a49459fca..30017b60d 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -29,6 +29,7 @@ class ProductManager(Role): role_profile: str = Field(default="Product Manager", alias='profile') goal: str = "efficiently create a successful product" constraints: str = "use same language as user requiremen" + """ Represents a Product Manager role responsible for product development and management. """ diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 211e41d3b..b7ee1ed53 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -22,7 +22,7 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ - name: str = "Eve" + name: str = Field(default="Eve") profile: str = Field(default="Project Manager") goal: str = "reak down tasks according to PRD/technical design, generate a task list, and analyze task " \ diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 4439b9b19..ec404570c 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -14,7 +14,9 @@ @Modified By: mashenquan, 2023-12-5. Enhance the workflow to navigate to WriteCode or QaEngineer based on the results of SummarizeCode. """ -from metagpt.actions import DebugError, RunCode, WriteTest + +from pydantic import Field + from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( @@ -22,6 +24,11 @@ from metagpt.const import ( TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO, ) +from metagpt.actions import ( + DebugError, + RunCode, + WriteTest, +) from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext @@ -30,21 +37,22 @@ from metagpt.utils.file_repository import FileRepository class QaEngineer(Role): + name: str = Field(default="Edward") + profile: str = Field(default="QaEngineer") + goal: str = "Write comprehensive and robust tests to ensure codes will work as expected without bugs" + constraints: str = "The test code you write should conform to code standard like PEP8, be modular, easy to read and maintain" + test_round_allowed: int = 5 + def __init__( self, - name="Edward", - profile="QaEngineer", - goal="Write comprehensive and robust tests to ensure codes will work as expected without bugs", - constraints="The test code you write should conform to code standard like PEP8, be modular, easy to read and maintain", - test_round_allowed=5, + **kwargs ): - super().__init__(name, profile, goal, constraints) + super().__init__(**kwargs) self._init_actions( [WriteTest] ) # FIXME: a bit hack here, only init one action to circumvent _think() logic, will overwrite _think() in future updates self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 - self.test_round_allowed = test_round_allowed async def _write_test(self, message: Message) -> None: src_file_repo = CONFIG.git_repo.new_file_repository(CONFIG.src_workspace) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index f1d7df5e7..114e9e599 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -28,15 +28,32 @@ from pydantic import BaseModel, Field from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement + +from pathlib import Path + +from typing import ( + Iterable, + Type, + Any +) +from pydantic import BaseModel, Field, validator + +# from metagpt.environment import Environment +from metagpt.config import CONFIG +from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output from metagpt.memory import Memory from metagpt.provider.human_provider import HumanProvider + from metagpt.utils.utils import read_json_file, write_json_file, import_class from metagpt.provider.base_gpt_api import BaseGPTAPI + +from metagpt.utils.utils import read_json_file, write_json_file, import_class, role_raise_decorator from metagpt.const import SERDESER_PATH @@ -80,13 +97,12 @@ class RoleReactMode(str, Enum): class RoleSetting(BaseModel): """Role Settings""" - - name: str - profile: str - goal: str - constraints: str - desc: str - is_human: bool + name: str = "" + profile: str = "" + goal: str = "" + constraints: str = "" + desc: str = "" + is_human: bool = False def __str__(self): return f"{self.name}({self.profile})" @@ -174,8 +190,8 @@ class Role(BaseModel): class Config: arbitrary_types_allowed = True exclude = ["_llm"] - - def __init__(self, **kwargs): + + def __init__(self, **kwargs: Any): for index in range(len(kwargs.get("_actions", []))): current_action = kwargs["_actions"][index] if isinstance(current_action, dict): @@ -212,15 +228,19 @@ class Role(BaseModel): object.__setattr__(self, "builtin_class_name", self.__class__.__name__) self.__fields__["builtin_class_name"].default = self.__class__.__name__ + def __init_subclass__(cls, **kwargs: Any) -> None: + super().__init_subclass__(**kwargs) + role_subclass_registry[cls.__name__] = cls + def _reset(self): - object.__setattr__(self, '_states', []) - object.__setattr__(self, '_actions', []) + object.__setattr__(self, "_states", []) + object.__setattr__(self, "_actions", []) @property def _setting(self): return f"{self.name}({self.profile})" - def serialize(self, stg_path: Path): + def serialize(self, stg_path: Path = None): stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ if stg_path is None else stg_path @@ -256,7 +276,7 @@ class Role(BaseModel): action.set_prefix(self._get_prefix(), self.profile) def set_recovered(self, recovered: bool = False): - self._recovered = recovered + self.recovered = recovered def set_memory(self, memory: Memory): self._rc.memory = memory @@ -269,7 +289,7 @@ class Role(BaseModel): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - i = action() + i = action(name="", llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): logger.warning( @@ -358,6 +378,10 @@ class Role(BaseModel): def subscription(self) -> Set: """The labels for messages to be consumed by the Role object.""" return self._subscription + + def set_env(self, env: "Environment"): + """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" + self._rc.env = env def _get_prefix(self): """Get the role prefix""" diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index d5aac1824..826413dc8 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -5,26 +5,31 @@ @Author : alexanderwu @File : sales.py """ + +from typing import Optional +from pydantic import Field + from metagpt.actions import SearchAndSummarize from metagpt.roles import Role from metagpt.tools import SearchEngineType class Sales(Role): - def __init__( - self, - name="Xiaomei", - profile="Retail sales guide", - desc="I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " - "will answer questions only based on the information in the knowledge base." - "If I feel that you can't get the answer from the reference material, then I will directly reply that" - " I don't know, and I won't tell you that this is from the knowledge base," - "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " - "professional guide", - store=None, - ): - super().__init__(name, profile, desc=desc) - self._set_store(store) + + name: str = Field(default="Xiaomei") + profile: str = Field(default="Retail sales guide") + desc: str = "I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " + "will answer questions only based on the information in the knowledge base." + "If I feel that you can't get the answer from the reference material, then I will directly reply that" + " I don't know, and I won't tell you that this is from the knowledge base," + "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " + "professional guide", + + store: Optional[str] = None + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self._set_store(self.store) def _set_store(self, store): if store: diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index 5760202ff..7d58ad922 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -7,6 +7,9 @@ @Modified By: mashenquan, 2023-11-1. According to Chapter 2.2.1 and 2.2.2 of RFC 116, change the data type of the `cause_by` value in the `Message` to a string to support the new message distribution feature. """ + +from pydantic import Field + from metagpt.actions import ActionOutput, SearchAndSummarize from metagpt.actions.action_node import ActionNode from metagpt.logs import logger @@ -27,15 +30,13 @@ class Searcher(Role): engine (SearchEngineType): The type of search engine to use. """ - def __init__( - self, - name: str = "Alice", - profile: str = "Smart Assistant", - goal: str = "Provide search services for users", - constraints: str = "Answer is rich and complete", - engine=SearchEngineType.SERPAPI_GOOGLE, - **kwargs, - ) -> None: + name: str = Field(default="Alice") + profile: str = Field(default="Smart Assistant") + goal: str = "Provide search services for users" + constraints: str = "Answer is rich and complete" + engine: SearchEngineType = SearchEngineType.SERPAPI_GOOGLE + + def __init__(self, **kwargs) -> None: """ Initializes the Searcher role with given attributes. @@ -46,8 +47,8 @@ class Searcher(Role): constraints (str): Constraints or limitations for the searcher. engine (SearchEngineType): The type of search engine to use. """ - super().__init__(name, profile, goal, constraints, **kwargs) - self._init_actions([SearchAndSummarize(engine=engine)]) + super().__init__(**kwargs) + self._init_actions([SearchAndSummarize(engine=self.engine)]) def set_search_func(self, search_func): """Sets a custom search function for the searcher.""" diff --git a/metagpt/schema.py b/metagpt/schema.py index 78e4a6031..a872481bb 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -12,7 +12,6 @@ between actions. 3. Add `id` to `Message` according to Section 2.2.3.1.1 of RFC 135. """ -from __future__ import annotations import asyncio import json @@ -24,6 +23,12 @@ from pathlib import Path from typing import Dict, List, Optional, Set, TypedDict from pydantic import BaseModel, Field +from dataclasses import dataclass, field +from typing import Type, TypedDict, Union, Optional + +from pydantic import BaseModel, Field +from pydantic.main import ModelMetaclass + from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, @@ -34,11 +39,16 @@ from metagpt.const import ( TASK_FILE_REPO, ) from metagpt.logs import logger + from metagpt.utils.common import any_to_str, any_to_str_set # from metagpt.utils.serialize import actionoutout_schema_to_mapping # from metagpt.actions.action_output import ActionOutput # from metagpt.actions.action import Action +from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ + actionoutput_str_to_mapping +from metagpt.utils.utils import import_class + class RawMessage(TypedDict): content: str @@ -54,7 +64,7 @@ class Document(BaseModel): filename: str = "" content: str = "" - def get_meta(self) -> Document: + def get_meta(self) -> "Document"": """Get metadata of the document. :return: A new Document instance with the same root path and filename. @@ -104,39 +114,21 @@ class Message(BaseModel): sent_from: str = "" send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL}) - def __init__( - self, - content, - instruct_content=None, - role="user", - cause_by="", - sent_from="", - send_to=MESSAGE_ROUTE_TO_ALL, - **kwargs, - ): - """ - Parameters not listed below will be stored as meta info, including custom parameters. - :param content: Message content. - :param instruct_content: Message content struct. - :param cause_by: Message producer - :param sent_from: Message route info tells who sent this message. - :param send_to: Specifies the target recipient or consumer for message delivery in the environment. - :param role: Message meta info tells who sent this message. - """ - if not cause_by: - from metagpt.actions import UserRequirement - cause_by = UserRequirement + def __init__(self, **kwargs): + instruct_content = kwargs.get("instruct_content", None) + cause_by = kwargs.get("cause_by", None) + if instruct_content and not isinstance(instruct_content, BaseModel): + ic = instruct_content + mapping = actionoutput_str_to_mapping(ic["mapping"]) - super().__init__( - id=uuid.uuid4().hex, - content=content, - instruct_content=instruct_content, - role=role, - cause_by=any_to_str(cause_by), - sent_from=any_to_str(sent_from), - send_to=any_to_str_set(send_to), - **kwargs, - ) + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) + ic_new = ic_obj(**ic["value"]) + kwargs["instruct_content"] = ic_new + if cause_by and not isinstance(cause_by, ModelMetaclass): + action_class = import_class("Action", "metagpt.actions.action") + kwargs["cause_by"] = action_class.deser_class(cause_by) + super(Message, self).__init__(**kwargs) def __setattr__(self, key, val): """Override `@property.setter`, convert non-string parameters into string parameters.""" @@ -150,6 +142,21 @@ class Message(BaseModel): new_val = val super().__setattr__(key, new_val) + def dict(self, *args, **kwargs) -> "DictStrAny": + """ overwrite the `dict` to dump dynamic pydantic model""" + obj_dict = super(Message, self).dict(*args, **kwargs) + ic = self.instruct_content # deal custom-defined action + if ic: + schema = ic.schema() + mapping = actionoutout_schema_to_mapping(schema) + mapping = actionoutput_mapping_to_str(mapping) + + obj_dict["instruct_content"] = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + cb = self.cause_by + if cb: + obj_dict["cause_by"] = cb.ser_class() + return obj_dict + def __str__(self): # prefix = '-'.join([self.role, str(self.cause_by)]) return f"{self.role}: {self.content}" @@ -157,45 +164,16 @@ class Message(BaseModel): def __repr__(self): return self.__str__() - # def serialize(self): - # message_cp: Message = copy.deepcopy(self) - # ic = message_cp.instruct_content - # if ic: - # # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly - # schema = ic.schema() - # mapping = actionoutout_schema_to_mapping(schema) - # - # message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} - # cb = message_cp.cause_by - # if cb: - # message_cp.cause_by = cb.serialize() - # - # return message_cp.dict() - # - # @classmethod - # def deserialize(cls, message_dict: dict): - # instruct_content = message_dict.get("instruct_content") - # if instruct_content: - # ic = instruct_content - # ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) - # ic_new = ic_obj(**ic["value"]) - # message_dict.instruct_content = ic_new - # cause_by = message_dict.get("cause_by") - # if cause_by: - # message_dict.cause_by = Action.deserialize(cause_by) - # - # return Message(**message_dict) - - def dict(self): - return { - "content": self.content, - "instruct_content": self.instruct_content, - "role": self.role, - "cause_by": self.cause_by, - "sent_from": self.sent_from, - "send_to": self.send_to, - "restricted_to": self.restricted_to - } + # def dict(self): + # return { + # "content": self.content, + # "instruct_content": self.instruct_content, + # "role": self.role, + # "cause_by": self.cause_by, + # "sent_from": self.sent_from, + # "send_to": self.send_to, + # "restricted_to": self.restricted_to + # } def to_dict(self) -> dict: """Return a dict containing `role` and `content` for the LLM call.l""" @@ -316,7 +294,7 @@ class CodingContext(BaseModel): code_doc: Optional[Document] @staticmethod - def loads(val: str) -> CodingContext | None: + def loads(val: str) -> "CodingContext" | None: try: m = json.loads(val) return CodingContext(**m) @@ -330,7 +308,7 @@ class TestingContext(BaseModel): test_doc: Optional[Document] @staticmethod - def loads(val: str) -> TestingContext | None: + def loads(val: str) -> "TestingContext" | None: try: m = json.loads(val) return TestingContext(**m) @@ -351,7 +329,7 @@ class RunCodeContext(BaseModel): output: Optional[str] @staticmethod - def loads(val: str) -> RunCodeContext | None: + def loads(val: str) -> "RunCodeContext" | None: try: m = json.loads(val) return RunCodeContext(**m) @@ -365,7 +343,7 @@ class RunCodeResult(BaseModel): stderr: str @staticmethod - def loads(val: str) -> RunCodeResult | None: + def loads(val: str) -> "RunCodeResult" | None: try: m = json.loads(val) return RunCodeResult(**m) @@ -380,7 +358,7 @@ class CodeSummarizeContext(BaseModel): reason: str = "" @staticmethod - def loads(filenames: List) -> CodeSummarizeContext: + def loads(filenames: List) -> "CodeSummarizeContext": ctx = CodeSummarizeContext() for filename in filenames: if Path(filename).is_relative_to(SYSTEM_DESIGN_FILE_REPO): diff --git a/metagpt/team.py b/metagpt/team.py index 02c48a138..87a6766f6 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -18,7 +18,8 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException -from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.utils import read_json_file, write_json_file, serialize_decorator +from metagpt.const import SERDESER_PATH class Team(BaseModel): @@ -34,29 +35,35 @@ class Team(BaseModel): class Config: arbitrary_types_allowed = True - def serialize(self, stg_path: Path): + def serialize(self, stg_path: Path = None): + stg_path = SERDESER_PATH.joinpath("team") if stg_path is None else stg_path + team_info_path = stg_path.joinpath("team_info.json") - write_json_file(team_info_path, { - "idea": self.idea, - "investment": self.investment - }) + write_json_file(team_info_path, self.dict(exclude={"environment": True})) - self.environment.serialize(stg_path.joinpath("environment")) + self.environment.serialize(stg_path.joinpath("environment")) # save environment alone - def deserialize(self, stg_path: Path): + @classmethod + def recover(cls, stg_path: Path) -> "Team": + return cls.deserialize(stg_path) + + @classmethod + def deserialize(cls, stg_path: Path) -> "Team": """ stg_path = ./storage/team """ # recover team_info team_info_path = stg_path.joinpath("team_info.json") if not team_info_path.exists(): - logger.error("recover storage not exist, not to recover and continue run the old project.") - team_info = read_json_file(team_info_path) - self.investment = team_info.get("investment", 10.0) - self.idea = team_info.get("idea", "") + raise FileNotFoundError("recover storage meta file `team_info.json` not exist, " + "not to recover and please start a new project.") + + team_info: dict = read_json_file(team_info_path) # recover environment - environment_path = stg_path.joinpath("environment") - self.environment = Environment() - self.environment.deserialize(stg_path=environment_path) + environment = Environment.deserialize(stg_path=stg_path.joinpath("environment")) + team_info.update({"environment": environment}) + + team = Team(**team_info) + return team def hire(self, roles: list[Role]): """Hire roles to cooperate""" @@ -84,6 +91,7 @@ class Team(BaseModel): def _save(self): logger.info(self.json(ensure_ascii=False)) + @serialize_decorator async def run(self, n_round=3): """Run company until target round or no money""" while n_round > 0: diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 56a866f2e..9a7049214 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -5,9 +5,7 @@ import copy import pickle -from metagpt.actions.action_output import ActionOutput -from metagpt.schema import Message -from metagpt.actions.action import Action +from metagpt.utils.utils import import_class def actionoutout_schema_to_mapping(schema: dict) -> dict: @@ -59,7 +57,7 @@ def actionoutput_str_to_mapping(mapping: dict) -> dict: return new_mapping -def serialize_general_message(message: Message) -> dict: +def serialize_general_message(message: "Message") -> dict: """ serialize Message, not to save""" message_cp = copy.deepcopy(message) ic = message_cp.instruct_content @@ -76,7 +74,7 @@ def serialize_general_message(message: Message) -> dict: return message_cp.dict() -def serialize_message(message: Message): +def serialize_message(message: "Message"): message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference ic = message_cp.instruct_content if ic: @@ -90,29 +88,35 @@ def serialize_message(message: Message): return msg_ser -def deserialize_general_message(message_dict: dict) -> Message: +def deserialize_general_message(message_dict: dict) -> "Message": """ deserialize Message, not to load""" instruct_content = message_dict.pop("instruct_content") cause_by = message_dict.pop("cause_by") - message = Message(**message_dict) + message_cls = import_class("Message", "metagpt.schema") + message = message_cls(**message_dict) if instruct_content: ic = instruct_content mapping = actionoutput_str_to_mapping(ic["mapping"]) - ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=mapping) + + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new if cause_by: - message.cause_by = Action.deser_class(cause_by) + action_class = import_class("Action", "metagpt.actions.action") + message.cause_by = action_class.deser_class(cause_by) return message -def deserialize_message(message_ser: str) -> Message: +def deserialize_message(message_ser: str) -> "Message": message = pickle.loads(message_ser) if message.instruct_content: ic = message.instruct_content - ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + + actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") + ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 220e228c3..ad5c7626a 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -56,3 +56,43 @@ def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> obj a_class = import_class(class_name, module_name) class_inst = a_class(*args, **kwargs) return class_inst + + +def format_trackback_info(limit: int = 2): + return traceback.format_exc(limit=limit) + + +def serialize_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + except Exception as exp: + logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + + return wrapper + + +def role_raise_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") + if self._rc.env: + newest_msgs = self._rc.env.memory.get(1) + if len(newest_msgs) > 0: + self._rc.memory.delete(newest_msgs[0]) + except Exception as exp: + if self._rc.env: + newest_msgs = self._rc.env.memory.get(1) + if len(newest_msgs) > 0: + logger.warning("There is a exception in role's execution, in order to resume, " + "we delete the newest role communication message in the role's memory.") + self._rc.memory.delete(newest_msgs[0]) # remove newest msg of the role to make it observed again + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside + + return wrapper diff --git a/startup.py b/startup.py index 9f753d553..c4928a1b5 100644 --- a/startup.py +++ b/startup.py @@ -1,10 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- + +from typing import Optional import asyncio - import fire +from pathlib import Path -from metagpt.const import SERDES_PATH from metagpt.roles import ( Architect, Engineer, @@ -22,11 +23,11 @@ async def startup( code_review: bool = False, run_tests: bool = False, implement: bool = True, - recover_path: bool = False, + recover_path: Optional[str] = None, ): """Run a startup. Be a boss.""" - company = Team() if not recover_path: + company = Team() company.hire( [ ProductManager(), @@ -45,8 +46,12 @@ async def startup( # (bug fixing capability comes soon!) company.hire([QaEngineer()]) else: - stg_path = SERDES_PATH.joinpath("team") - company.deserialize(stg_path=stg_path) + # # stg_path = SERDESER_PATH.joinpath("team") + stg_path = Path(recover_path) + if not stg_path.exists() or not str(stg_path).endswith("team"): + raise FileNotFoundError(f"{recover_path} not exists or not endswith `team`") + + company = Team.recover(stg_path=stg_path) idea = company.idea # use original idea company.invest(investment) From a01766ae72d9d2ac7a113f51afbfd6e2d30e85e1 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 19:30:02 +0800 Subject: [PATCH 293/413] fix ut of serialize_deserialize --- .../serialize_deserialize/test_action.py | 3 +-- .../test_product_manager.py | 1 - .../serialize_deserialize/test_role.py | 10 ++++++++- .../test_serdeser_base.py | 21 +++++++++++++------ .../serialize_deserialize/test_team.py | 2 +- .../serialize_deserialize/test_wrire_prd.py | 4 ++-- .../serialize_deserialize/test_write_code.py | 2 -- .../test_write_design.py | 3 +-- 8 files changed, 29 insertions(+), 17 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index b624dff5a..0138d41ce 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -13,14 +13,13 @@ def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + assert "llm" not in ser_action_dict @pytest.mark.asyncio async def test_action_deserialize(): action = Action() serialized_data = action.dict() - assert isinstance(serialized_data["llm"], OpenAIGPTAPI) new_action = Action(**serialized_data) diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 54584cf96..25bc07a11 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -14,7 +14,6 @@ async def test_product_manager_deserialize(): role = ProductManager() ser_role_dict = role.dict(by_alias=True) new_role = ProductManager(**ser_role_dict) - # new_role = ProductManager().deserialize(ser_role_dict) assert new_role.name == "Alice" assert len(new_role._actions) == 1 diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index f260dea3a..c21b9cc2e 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -17,7 +17,15 @@ from metagpt.const import SERDESER_PATH from metagpt.roles.engineer import Engineer from metagpt.utils.utils import format_trackback_info -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path + + +def test_roles(): + role_a = RoleA() + assert len(role_a._rc.watch) == 1 + role_b = RoleB() + assert len(role_a._rc.watch) == 1 + assert len(role_b._rc.watch) == 1 def test_role_serialize(): diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 35bad6cd9..00d894b3d 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -8,6 +8,7 @@ from pathlib import Path from metagpt.actions.action import Action from metagpt.roles.role import Role, RoleReactMode from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.action_output import ActionOutput serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage") @@ -22,21 +23,27 @@ class MockMessage(BaseModel): class ActionPass(Action): name: str = "ActionPass" - async def run(self, messages: list["Message"]): - return "pass" + async def run(self, messages: list["Message"]) -> ActionOutput: + output_mapping = { + "result": (str, ...) + } + pass_class = ActionOutput.create_model_class("pass", output_mapping) + pass_output = ActionOutput("ActionPass run passed", pass_class(**{"result": "pass result"})) + + return pass_output class ActionOK(Action): name: str = "ActionOK" - async def run(self, messages: list["Message"]): + async def run(self, messages: list["Message"]) -> str: return "ok" class ActionRaise(Action): name: str = "ActionRaise" - async def run(self, messages: list["Message"]): + async def run(self, messages: list["Message"]) -> str: raise RuntimeError("parse error in ActionRaise") @@ -48,7 +55,8 @@ class RoleA(Role): constraints: str = "RoleA's constraints" def __init__(self, **kwargs): - super(RoleA, self).__init__(**kwargs) + # super(RoleA, self).__init__(**kwargs) + super().__init__(**kwargs) self._init_actions([ActionPass]) self._watch([BossRequirement]) @@ -63,7 +71,8 @@ class RoleB(Role): constraints: str = "RoleB's constraints" def __init__(self, **kwargs): - super(RoleB, self).__init__(**kwargs) + # super(RoleB, self).__init__(**kwargs) + super().__init__(**kwargs) self._init_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self._rc.react_mode = RoleReactMode.BY_ORDER diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index e9122ebc0..b8972135b 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -11,7 +11,7 @@ from metagpt.roles import ProjectManager, ProductManager, Architect from metagpt.team import Team from metagpt.const import SERDESER_PATH -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK def test_team_deserialize(): diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 96b4d19ad..05a86cb7f 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -21,7 +21,7 @@ async def test_action_deserialize(): action = WritePRD() serialized_data = action.dict() new_action = WritePRD(**serialized_data) - # new_action = WritePRD().deserialize(serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - assert len(await new_action.run([Message(content="write a cli snake game")])) > 0 + action_output = await new_action.run([Message(content="write a cli snake game")]) + assert len(action_output.content) > 0 diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 7f4799014..4e3b712c0 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -27,7 +27,6 @@ async def test_write_code_deserialize(): action = WriteCode() serialized_data = action.dict() new_action = WriteCode(**serialized_data) - # new_action = WriteCode().deserialize(serialized_data) assert new_action.name == "WriteCode" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game", filename="test_code") @@ -38,7 +37,6 @@ async def test_write_code_review_deserialize(): action = WriteCodeReview() serialized_data = action.dict() new_action = WriteCodeReview(**serialized_data) - # new_action = WriteCodeReview().deserialize(serialized_data) code = await WriteCode().run(context="write a cli snake game", filename="test_code") assert new_action.name == "WriteCodeReview" diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index e6e236676..5b2a30ed3 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -26,7 +26,7 @@ def test_write_task_serialize(): async def test_write_design_deserialize(): action = WriteDesign() serialized_data = action.dict() - new_action = WriteDesign().deserialize(serialized_data) + new_action = WriteDesign(**serialized_data) assert new_action.name == "" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") @@ -37,7 +37,6 @@ async def test_write_task_deserialize(): action = WriteTasks() serialized_data = action.dict() new_action = WriteTasks(**serialized_data) - # new_action = WriteTasks().deserialize(serialized_data) assert new_action.name == "CreateTasks" assert new_action.llm == LLM() await new_action.run(context="write a cli snake game") From a6510c44fcb14eaecb42224d3398acdacbc13d30 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 19:31:26 +0800 Subject: [PATCH 294/413] fix actions/roles ser&deser --- metagpt/actions/search_and_summarize.py | 15 +++++++-------- metagpt/roles/role.py | 4 ++-- metagpt/utils/utils.py | 4 +++- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 7bff1c113..aa4d0f654 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -117,19 +117,18 @@ class SearchAndSummarize(Action): @root_validator def validate_engine_and_run_func(cls, values): - engine = values.get('engine') - search_func = values.get('search_func') + engine = values.get("engine") + search_func = values.get("search_func") config = Config() if engine is None: engine = config.search_engine - config_data = { - 'engine': engine, - 'run_func': search_func - } - search_engine = SearchEngine(**config_data) + try: + search_engine = SearchEngine(engine=engine, run_func=search_func) + except pydantic.ValidationError: + search_engine = None - values['search_engine'] = search_engine + values["search_engine"] = search_engine return values async def run(self, context: list[Message], system_text=SEARCH_AND_SUMMARIZE_SYSTEM) -> str: diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 114e9e599..e407003f5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -113,8 +113,7 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" - - env: "Environment" = Field(default=None) + env: "Environment" = Field(default=None, exclude=True) msg_buffer: MessageQueue = Field(default_factory=MessageQueue) # Message Buffer with Asynchronous Updates memory: Memory = Field(default_factory=Memory) # long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) @@ -235,6 +234,7 @@ class Role(BaseModel): def _reset(self): object.__setattr__(self, "_states", []) object.__setattr__(self, "_actions", []) + # object.__setattr__(self, "_rc", RoleContext()) @property def _setting(self): diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index ad5c7626a..b9a8dcb53 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -65,7 +65,9 @@ def format_trackback_info(limit: int = 2): def serialize_decorator(func): async def wrapper(self, *args, **kwargs): try: - return await func(self, *args, **kwargs) + result = await func(self, *args, **kwargs) + self.serialize() # Team.serialize + return result except KeyboardInterrupt as kbi: logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") self.serialize() # Team.serialize From 0a80752908deae92906f4b0337972790ada79756 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 30 Nov 2023 21:42:09 +0800 Subject: [PATCH 295/413] fix role._rc init --- metagpt/environment.py | 4 ++++ metagpt/roles/role.py | 1 + .../serialize_deserialize/test_team.py | 19 ++++++++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index 242581e17..19c77a03d 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -40,6 +40,7 @@ class Environment(BaseModel): arbitrary_types_allowed = True def __init__(self, **kwargs): + roles = [] for role_key, role in kwargs.get("roles", {}).items(): current_role = kwargs["roles"][role_key] if isinstance(current_role, dict): @@ -50,8 +51,11 @@ class Environment(BaseModel): current_role = subclass(**current_role) break kwargs["roles"][role_key] = current_role + roles.append(current_role) super().__init__(**kwargs) + self.add_roles(roles) # add_roles again to init the Role.set_env + def serialize(self, stg_path: Path): roles_path = stg_path.joinpath("roles.json") roles_info = [] diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index e407003f5..6be800789 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -113,6 +113,7 @@ class RoleSetting(BaseModel): class RoleContext(BaseModel): """Role Runtime Context""" + # # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison` env: "Environment" = Field(default=None, exclude=True) msg_buffer: MessageQueue = Field(default_factory=MessageQueue) # Message Buffer with Asynchronous Updates memory: Memory = Field(default_factory=Memory) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index b8972135b..e5ec20f2e 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -39,7 +39,7 @@ def test_team_deserialize(): assert new_company.environment.get_role(arch.profile) is not None -def test_team_serdeser(): +def test_team_serdeser_save(): company = Team() company.hire([RoleC()]) @@ -60,12 +60,19 @@ async def test_team_recover(): shutil.rmtree(stg_path, ignore_errors=True) company = Team() - company.hire([RoleC()]) + role_c = RoleC() + company.hire([role_c]) company.start_project(idea) await company.run(n_round=4) ser_data = company.dict() new_company = Team(**ser_data) + + new_role_c = new_company.environment.get_role(role_c.profile) + assert new_role_c._rc.memory == role_c._rc.memory + assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env.memory == role_c._rc.env.memory + assert new_company.environment.memory.count() == 1 assert type(list(new_company.environment.roles.values())[0]._actions[0]) == ActionOK @@ -80,11 +87,17 @@ async def test_team_recover_save(): shutil.rmtree(stg_path, ignore_errors=True) company = Team() - company.hire([RoleC()]) + role_c = RoleC() + company.hire([role_c]) company.start_project(idea) await company.run(n_round=4) new_company = Team.recover(stg_path) + new_role_c = new_company.environment.get_role(role_c.profile) + assert new_role_c._rc.memory == role_c._rc.memory + assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env.memory == role_c._rc.env.memory + new_company.start_project(idea) await new_company.run(n_round=4) From 26ddddaadd8dada086d8bc6199320863ca7d3f51 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 14:43:45 +0800 Subject: [PATCH 296/413] simplify some ser&desr code --- metagpt/actions/action.py | 20 ++++++------------ metagpt/roles/role.py | 43 +++++++++++++++++++++++++++++---------- metagpt/schema.py | 13 +----------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 499b5e794..8b28ffd8e 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -54,6 +54,12 @@ class Action(BaseModel): super().__init_subclass__(**kwargs) action_subclass_registry[cls.__name__] = cls + def dict(self, *args, **kwargs) -> "DictStrAny": + obj_dict = super(Action, self).dict(*args, **kwargs) + if "llm" in obj_dict: + obj_dict.pop("llm") + return obj_dict + def set_prefix(self, prefix, profile): """Set prefix for later usage""" self.prefix = prefix @@ -66,20 +72,6 @@ class Action(BaseModel): def __repr__(self): return self.__str__() - def serialize(self): - return { - "action_class": self.__class__.__name__, - "module_name": self.__module__, - "name": self.name - } - - @classmethod - def deserialize(cls, action_dict: dict) -> "Action": - action_class_str = action_dict.pop("action_class") - module_name = action_dict.pop("module_name") - action_class = import_class(action_class_str, module_name) - return action_class(**action_dict) - @classmethod def ser_class(cls) -> dict: """ serialize class type""" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 6be800789..59b0f9cd6 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -119,17 +119,33 @@ class RoleContext(BaseModel): memory: Memory = Field(default_factory=Memory) # long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None - todo: Action = Field(default=None) - watch: set[str] = Field(default_factory=set) - news: list[Type[Message]] = Field(default=[]) - react_mode: RoleReactMode = ( - RoleReactMode.REACT - ) # see `Role._set_react_mode` for definitions of the following two attributes + todo: Action = Field(default=None, exclude=True) + watch: set[Type[Action]] = Field(default_factory=set) + news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used + react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes max_react_loop: int = 1 class Config: arbitrary_types_allowed = True - + + def __init__(self, **kwargs): + watch_info = kwargs.get("watch", set()) + watch = set() + for item in watch_info: + action = Action.deser_class(item) + watch.update([action]) + kwargs["watch"] = watch + super(RoleContext, self).__init__(**kwargs) + + def dict(self, *args, **kwargs) -> "DictStrAny": + obj_dict = super(RoleContext, self).dict(*args, **kwargs) + watch = obj_dict.get("watch", set()) + watch_info = [] + for item in watch: + watch_info.append(item.ser_class()) + obj_dict["watch"] = watch_info + return obj_dict + def check(self, role_id: str): # if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: # self.long_term_memory.recover_memory(role_id, self) @@ -290,7 +306,7 @@ class Role(BaseModel): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - i = action(name="", llm=self._llm) + i = action(llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): logger.warning( @@ -386,9 +402,14 @@ class Role(BaseModel): def _get_prefix(self): """Get the role prefix""" - if self._setting.desc: - return self._setting.desc - return PREFIX_TEMPLATE.format(**self._setting.dict()) + if self.desc: + return self.desc + return PREFIX_TEMPLATE.format(**{ + "profile": self.profile, + "name": self.name, + "goal": self.goal, + "constraints": self.constraints + }) async def _think(self) -> None: """Think about what to do and decide on the next action""" diff --git a/metagpt/schema.py b/metagpt/schema.py index a872481bb..15dfb579c 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -64,7 +64,7 @@ class Document(BaseModel): filename: str = "" content: str = "" - def get_meta(self) -> "Document"": + def get_meta(self) -> "Document": """Get metadata of the document. :return: A new Document instance with the same root path and filename. @@ -164,17 +164,6 @@ class Message(BaseModel): def __repr__(self): return self.__str__() - # def dict(self): - # return { - # "content": self.content, - # "instruct_content": self.instruct_content, - # "role": self.role, - # "cause_by": self.cause_by, - # "sent_from": self.sent_from, - # "send_to": self.send_to, - # "restricted_to": self.restricted_to - # } - def to_dict(self) -> dict: """Return a dict containing `role` and `content` for the LLM call.l""" return {"role": self.role, "content": self.content} From 1514942d1d0058f85569fffdca10db2e9281613c Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 14:45:06 +0800 Subject: [PATCH 297/413] update ut after simplification --- tests/metagpt/serialize_deserialize/test_action.py | 14 +------------- tests/metagpt/serialize_deserialize/test_role.py | 3 --- .../serialize_deserialize/test_serdeser_base.py | 6 +++--- tests/metagpt/serialize_deserialize/test_team.py | 2 +- .../serialize_deserialize/test_wrire_prd.py | 2 +- .../serialize_deserialize/test_write_code.py | 4 ++-- .../serialize_deserialize/test_write_design.py | 4 ++-- 7 files changed, 10 insertions(+), 25 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 0138d41ce..16369bb61 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -13,7 +13,7 @@ def test_action_serialize(): action = Action() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" not in ser_action_dict + # assert "llm" not in ser_action_dict # not export @pytest.mark.asyncio @@ -34,15 +34,3 @@ def test_action_serdeser(): action_class = Action.deser_class(action_info) assert action_class == WriteTest - - -def test_action_class_serdeser(): - name = "write test" - action_info = WriteTest(name=name).serialize() - assert action_info["name"] == name - - action_info = WriteTest(name=name, llm=LLM()).serialize() - assert action_info["name"] == name - - action = Action.deserialize(action_info) - assert action.name == name diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index c21b9cc2e..61684ba9d 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -66,7 +66,6 @@ def test_role_serdeser_save(): role_tag = f"{pm.__class__.__name__}_{pm.name}" stg_path = stg_path_prefix.joinpath(role_tag) pm.serialize(stg_path) - assert stg_path.joinpath("actions/actions_info.json").exists() new_pm = Role.deserialize(stg_path) assert new_pm.name == pm.name @@ -89,8 +88,6 @@ async def test_role_serdeser_interrupt(): assert role_c._rc.memory.count() == 2 - assert stg_path.joinpath("actions/todo.json").exists() - new_role_a: Role = Role.deserialize(stg_path) assert new_role_a._rc.state == 1 diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 00d894b3d..74f9fea87 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -21,7 +21,7 @@ class MockMessage(BaseModel): class ActionPass(Action): - name: str = "ActionPass" + name: str = Field(default="ActionPass") async def run(self, messages: list["Message"]) -> ActionOutput: output_mapping = { @@ -34,14 +34,14 @@ class ActionPass(Action): class ActionOK(Action): - name: str = "ActionOK" + name: str = Field(default="ActionOK") async def run(self, messages: list["Message"]) -> str: return "ok" class ActionRaise(Action): - name: str = "ActionRaise" + name: str = Field(default="ActionRaise") async def run(self, messages: list["Message"]) -> str: raise RuntimeError("parse error in ActionRaise") diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index e5ec20f2e..28728e1b5 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -70,7 +70,7 @@ async def test_team_recover(): new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env == role_c._rc.env # TODO check again assert new_role_c._rc.env.memory == role_c._rc.env.memory assert new_company.environment.memory.count() == 1 diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_wrire_prd.py index 05a86cb7f..0b9dfa9d8 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_wrire_prd.py @@ -13,7 +13,7 @@ def test_action_serialize(): action = WritePRD() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 4e3b712c0..5552ffd7f 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -12,14 +12,14 @@ def test_write_design_serialize(): action = WriteCode() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCode" - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export def test_write_task_serialize(): action = WriteCodeReview() ser_action_dict = action.dict() assert ser_action_dict["name"] == "WriteCodeReview" - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 5b2a30ed3..080896c98 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -12,14 +12,14 @@ def test_write_design_serialize(): action = WriteDesign() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export def test_write_task_serialize(): action = WriteTasks() ser_action_dict = action.dict() assert "name" in ser_action_dict - assert "llm" in ser_action_dict + # assert "llm" in ser_action_dict # not export @pytest.mark.asyncio From a11096ef02efb43f056f77d21707dff97f8d72a3 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 15:30:28 +0800 Subject: [PATCH 298/413] update --- tests/metagpt/serialize_deserialize/test_team.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 28728e1b5..9c4eb8170 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -70,7 +70,7 @@ async def test_team_recover(): new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env == role_c._rc.env # TODO check again + assert new_role_c._rc.env == role_c._rc.env assert new_role_c._rc.env.memory == role_c._rc.env.memory assert new_company.environment.memory.count() == 1 @@ -95,7 +95,10 @@ async def test_team_recover_save(): new_company = Team.recover(stg_path) new_role_c = new_company.environment.get_role(role_c.profile) assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env != role_c._rc.env # due to Action raise, role's memory has been changed. + assert new_role_c._rc.env != role_c._rc.env + assert new_role_c.recovered != role_c.recovered # here cause previous ut is `!=` + assert new_role_c._rc.todo != role_c._rc.todo # serialize exclude `_rc.todo` + assert new_role_c._rc.news != role_c._rc.news # serialize exclude `_rc.news` assert new_role_c._rc.env.memory == role_c._rc.env.memory new_company.start_project(idea) From 0f2d96a7e2ad1028fe1f8baa3495be8c2e1fd5c7 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 20:35:48 +0800 Subject: [PATCH 299/413] update asyncio.sleep to make it async --- .../test_serdeser_base.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 74f9fea87..298c13823 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -4,6 +4,7 @@ from pydantic import BaseModel, Field from pathlib import Path +import asyncio from metagpt.actions.action import Action from metagpt.roles.role import Role, RoleReactMode @@ -24,6 +25,7 @@ class ActionPass(Action): name: str = Field(default="ActionPass") async def run(self, messages: list["Message"]) -> ActionOutput: + await asyncio.sleep(5) # sleep to make other roles can watch the executed Message output_mapping = { "result": (str, ...) } @@ -37,6 +39,7 @@ class ActionOK(Action): name: str = Field(default="ActionOK") async def run(self, messages: list["Message"]) -> str: + await asyncio.sleep(5) return "ok" @@ -55,14 +58,10 @@ class RoleA(Role): constraints: str = "RoleA's constraints" def __init__(self, **kwargs): - # super(RoleA, self).__init__(**kwargs) - super().__init__(**kwargs) + super(RoleA, self).__init__(**kwargs) self._init_actions([ActionPass]) self._watch([BossRequirement]) - async def run(self, message: "Message" = None): - await super(RoleA, self).run(message) - class RoleB(Role): name: str = Field(default="RoleB") @@ -71,15 +70,11 @@ class RoleB(Role): constraints: str = "RoleB's constraints" def __init__(self, **kwargs): - # super(RoleB, self).__init__(**kwargs) - super().__init__(**kwargs) + super(RoleB, self).__init__(**kwargs) self._init_actions([ActionOK, ActionRaise]) self._watch([ActionPass]) self._rc.react_mode = RoleReactMode.BY_ORDER - async def run(self, message: "Message" = None): - await super(RoleB, self).run(message) - class RoleC(Role): name: str = Field(default="RoleC") @@ -92,6 +87,3 @@ class RoleC(Role): self._init_actions([ActionOK, ActionRaise]) self._watch([BossRequirement]) self._rc.react_mode = RoleReactMode.BY_ORDER - - async def run(self, message: "Message" = None): - await super(RoleC, self).run(message) From 3679d77f0df68eeb7bd9d325eb671a20430a81c7 Mon Sep 17 00:00:00 2001 From: better629 Date: Fri, 1 Dec 2023 21:07:47 +0800 Subject: [PATCH 300/413] fix when RoleReactMode=REACT --- metagpt/roles/role.py | 4 ++-- metagpt/utils/utils.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 59b0f9cd6..e63404939 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -417,9 +417,9 @@ class Role(BaseModel): # If there is only one action, then only this one can be performed self._set_state(0) return - if self._recovered and self._rc.state >= 0: + if self.recovered and self._rc.state >= 0: self._set_state(self._rc.state) # action to run from recovered state - self._recovered = False # avoid max_react_loop out of work + self.recovered = False # avoid max_react_loop out of work return prompt = self._get_prefix() diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index b9a8dcb53..33ca16944 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -88,6 +88,7 @@ def role_raise_decorator(func): newest_msgs = self._rc.env.memory.get(1) if len(newest_msgs) > 0: self._rc.memory.delete(newest_msgs[0]) + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside except Exception as exp: if self._rc.env: newest_msgs = self._rc.env.memory.get(1) From 43e35fe929d165f6f5c36b8f683d85825fa78684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 18 Dec 2023 16:13:21 +0800 Subject: [PATCH 301/413] fixbug: recursive user requirement dead loop --- metagpt/roles/role.py | 23 ++++++----------------- metagpt/schema.py | 4 ---- tests/metagpt/test_role.py | 6 +++--- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 1e7ebf711..48688ad5f 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -25,9 +25,8 @@ from typing import Iterable, Set, Type from pydantic import BaseModel, Field -from metagpt.actions import Action, ActionOutput +from metagpt.actions import Action, ActionOutput, UserRequirement from metagpt.actions.action_node import ActionNode -from metagpt.actions.add_requirement import UserRequirement from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory @@ -127,17 +126,7 @@ class RoleContext(BaseModel): return self.memory.get() -class _RoleInjector(type): - def __call__(cls, *args, **kwargs): - instance = super().__call__(*args, **kwargs) - - if not instance._rc.watch: - instance._watch([UserRequirement]) - - return instance - - -class Role(metaclass=_RoleInjector): +class Role: """Role/Agent""" def __init__(self, name="", profile="", goal="", constraints="", desc="", is_human=False): @@ -149,10 +138,9 @@ class Role(metaclass=_RoleInjector): self._states = [] self._actions = [] self._role_id = str(self._setting) - self._rc = RoleContext() + self._rc = RoleContext(watch={any_to_str(UserRequirement)}) self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} - def _reset(self): self._states = [] self._actions = [] @@ -203,8 +191,7 @@ class Role(metaclass=_RoleInjector): """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe. """ - tags = {any_to_str(t) for t in actions} - self._rc.watch.update(tags) + self._rc.watch = {any_to_str(t) for t in actions} # check RoleContext after adding watch actions self._rc.check(self._role_id) @@ -401,6 +388,8 @@ class Role(metaclass=_RoleInjector): msg = with_message elif isinstance(with_message, list): msg = Message("\n".join(with_message)) + if not msg.cause_by: + msg.cause_by = UserRequirement self.put_message(msg) if not await self._observe(): diff --git a/metagpt/schema.py b/metagpt/schema.py index 5aec378e4..758149efa 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -121,10 +121,6 @@ class Message(BaseModel): :param send_to: Specifies the target recipient or consumer for message delivery in the environment. :param role: Message meta info tells who sent this message. """ - if not cause_by: - from metagpt.actions import UserRequirement - cause_by = UserRequirement - super().__init__( id=uuid.uuid4().hex, content=content, diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 8fac2503c..611d321fc 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -14,11 +14,11 @@ import uuid import pytest from pydantic import BaseModel -from metagpt.actions import Action, ActionOutput +from metagpt.actions import Action, ActionOutput, UserRequirement from metagpt.environment import Environment from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_class_name +from metagpt.utils.common import any_to_str, get_class_name class MockAction(Action): @@ -60,7 +60,7 @@ async def test_react(): name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc ) role.subscribe({seed.subscription}) - assert role._rc.watch == set({}) + assert role._rc.watch == {any_to_str(UserRequirement)} assert role.name == seed.name assert role.profile == seed.profile assert role._setting.goal == seed.goal From a88f931fe9a93dc7d883c0a260310f9aee9942e0 Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Mon, 18 Dec 2023 19:26:38 +0800 Subject: [PATCH 302/413] update version and roadmap --- docs/ROADMAP.md | 8 ++++---- setup.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index afc9ff445..3cb03f374 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -30,10 +30,10 @@ ### Tasks 4. Complete the design and implementation of module breakdown 5. Support various modes of memory: clearly distinguish between long-term and short-term memory 6. Perfect the test role, and carry out necessary interactions with humans - 7. Allowing natural communication between roles (expected v0.5.0) + 7. ~~Allowing natural communication between roles~~ (v0.5.0) 8. Implement SkillManager and the process of incremental Skill learning (experimentation done with game agents) 9. Automatically get RPM and configure it by calling the corresponding openai page, so that each key does not need to be manually configured - 10. IMPORTANT: Support incremental development (expected v0.5.0) + 10. ~~IMPORTANT: Support incremental development~~ (v0.5.0) 3. Strategies 1. Support ReAct strategy (experimentation done with game agents) 2. Support CoT strategy (experimentation done with game agents) @@ -45,8 +45,8 @@ ### Tasks 2. Implementation: Knowledge search, supporting 10+ data formats 3. Implementation: Data EDA (expected v0.6.0) 4. Implementation: Review - 5. Implementation: Add Document (expected v0.5.0) - 6. Implementation: Delete Document (expected v0.5.0) + 5. ~~Implementation~~: Add Document (v0.5.0) + 6. ~~Implementation~~: Delete Document (v0.5.0) 7. Implementation: Self-training 8. ~~Implementation: DebugError~~ (v0.2.1) 9. Implementation: Generate reliable unit tests based on YAPI diff --git a/setup.py b/setup.py index 73a05eeae..57290f4cd 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.5.1", + version="0.5.2", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From 5022e2e713adaefe0cfce44939f918174026509a Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 10:52:16 +0800 Subject: [PATCH 303/413] remove requirements-ocr.txt and place the optional setup to setup.py --- requirements-ocr.txt | 4 ---- setup.py | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 requirements-ocr.txt diff --git a/requirements-ocr.txt b/requirements-ocr.txt deleted file mode 100644 index cf6103afc..000000000 --- a/requirements-ocr.txt +++ /dev/null @@ -1,4 +0,0 @@ -paddlepaddle==2.4.2 -paddleocr>=2.0.1 -tabulate==0.9.0 --r requirements.txt diff --git a/setup.py b/setup.py index 4dd453b3d..64d34f1e9 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ setup( "search-google": ["google-api-python-client==2.94.0"], "search-ddg": ["duckduckgo-search==3.8.5"], "pyppeteer": ["pyppeteer>=1.0.2"], + "ocr": ["paddlepaddle==2.4.2", "paddleocr>=2.0.1", "tabulate==0.9.0"], }, cmdclass={ "install_mermaid": InstallMermaidCLI, From caac36b83f795ba556e063574fa85a5cfea8fb3c Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:01:20 +0800 Subject: [PATCH 304/413] use pre-commit --- metagpt/actions/action_node.py | 12 ++++++++++-- metagpt/actions/project_management_an.py | 2 +- metagpt/actions/write_code_review.py | 8 ++++++-- metagpt/roles/architect.py | 2 +- metagpt/roles/engineer.py | 2 +- metagpt/roles/project_manager.py | 2 +- 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index fb7d621d8..9bb12fc84 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -52,6 +52,7 @@ def dict_to_markdown(d, prefix="-", postfix="\n"): class ActionNode: """ActionNode is a tree of nodes.""" + mode: str # Action Context @@ -70,8 +71,15 @@ class ActionNode: content: str instruct_content: BaseModel - def __init__(self, key: str, expected_type: Type, instruction: str, example: str, content: str = "", - children: dict[str, "ActionNode"] = None): + def __init__( + self, + key: str, + expected_type: Type, + instruction: str, + example: str, + content: str = "", + children: dict[str, "ActionNode"] = None, + ): self.key = key self.expected_type = expected_type self.instruction = instruction diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index 970cb0594..6208c1051 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -44,7 +44,7 @@ FULL_API_SPEC = ActionNode( key="Full API spec", expected_type=str, instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend. If front-end " - "and back-end communication is not required, leave it blank.", + "and back-end communication is not required, leave it blank.", example="openapi: 3.0.0 ...", ) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 4b3e9aece..365c87063 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -154,11 +154,15 @@ class WriteCodeReview(Action): code=iterative_code, filename=self.context.code_doc.filename, ) - cr_prompt = EXAMPLE_AND_INSTRUCTION.format(format_example=format_example, ) + cr_prompt = EXAMPLE_AND_INSTRUCTION.format( + format_example=format_example, + ) logger.info( f"Code review and rewrite {self.context.code_doc.filename}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" ) - result, rewrited_code = await self.write_code_review_and_rewrite(context_prompt, cr_prompt, self.context.code_doc.filename) + result, rewrited_code = await self.write_code_review_and_rewrite( + context_prompt, cr_prompt, self.context.code_doc.filename + ) if "LBTM" in result: iterative_code = rewrited_code elif "LGTM" in result: diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index fa91d393d..fce6c3425 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -28,7 +28,7 @@ class Architect(Role): profile: str = "Architect", goal: str = "design a concise, usable, complete software system", constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries." - "Use same language as user requirement" + "Use same language as user requirement", ) -> None: """Initializes the Architect with given attributes.""" super().__init__(name, profile, goal, constraints) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index f1e65b177..2620fe4e5 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -73,7 +73,7 @@ class Engineer(Role): profile: str = "Engineer", goal: str = "write elegant, readable, extensible, efficient code", constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " - "Use same language as user requirement", + "Use same language as user requirement", n_borg: int = 1, use_code_review: bool = False, ) -> None: diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 5a2b9be50..657737513 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -26,7 +26,7 @@ class ProjectManager(Role): name: str = "Eve", profile: str = "Project Manager", goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " - "dependencies to start with the prerequisite modules", + "dependencies to start with the prerequisite modules", constraints: str = "use same language as user requirement", ) -> None: """ From bef1071c5ab2928ac4df4448870e53d97206fc33 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:10:17 +0800 Subject: [PATCH 305/413] setup.py: update --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 64d34f1e9..320230a7f 100644 --- a/setup.py +++ b/setup.py @@ -30,15 +30,15 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.4.0", - description="The Multi-Role Meta Programming Framework", + version="0.5.1", + description="The Multi-Agent Framework", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/geekan/MetaGPT", author="Alexander Wu", author_email="alexanderwu@deepwisdom.ai", license="MIT", - keywords="metagpt multi-role multi-agent programming gpt llm metaprogramming", + keywords="metagpt multi-agent multi-role programming gpt llm metaprogramming", packages=find_packages(exclude=["contrib", "docs", "examples", "tests*"]), python_requires=">=3.9", install_requires=requirements, From 8b8ee5c56d778468141f4f6e007fcf9fb10e1bc7 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:22:21 +0800 Subject: [PATCH 306/413] delete inspect_module.py because we have ast tree parser --- metagpt/inspect_module.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 metagpt/inspect_module.py diff --git a/metagpt/inspect_module.py b/metagpt/inspect_module.py deleted file mode 100644 index 48ceffc57..000000000 --- a/metagpt/inspect_module.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/28 14:54 -@Author : alexanderwu -@File : inspect_module.py -""" - -import inspect - -import metagpt # replace with your module - - -def print_classes_and_functions(module): - """FIXME: NOT WORK..""" - for name, obj in inspect.getmembers(module): - if inspect.isclass(obj): - print(f"Class: {name}") - elif inspect.isfunction(obj): - print(f"Function: {name}") - else: - print(name) - - print(dir(module)) - - -if __name__ == "__main__": - print_classes_and_functions(metagpt) From ad8f7ebfb9c74cb8ef258563ad9c71666a178596 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:49:06 +0800 Subject: [PATCH 307/413] token_counter: add gpt-3.5-turbo-16k in list and add comment for them --- metagpt/utils/token_counter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index 266a53268..ebfb85de7 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -56,6 +56,7 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): if model in { "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", + "gpt-3.5-turbo-16k", "gpt-3.5-turbo-1106", "gpt-4-0314", "gpt-4-32k-0314", @@ -63,7 +64,7 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): "gpt-4-32k-0613", "gpt-4-1106-preview", }: - tokens_per_message = 3 + tokens_per_message = 3 # # every reply is primed with <|start|>assistant<|message|> tokens_per_name = 1 elif model == "gpt-3.5-turbo-0301": tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n From 602818e2baf9ffc4b2108d8a8c23fd7dc1a19522 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:52:23 +0800 Subject: [PATCH 308/413] openai_api: refine logic --- metagpt/provider/openai_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index a73bb0aa0..86054881e 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -329,7 +329,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): usage["completion_tokens"] = completion_tokens return usage except Exception as e: - logger.error("usage calculation failed!", e) + logger.error(f"{self.model} usage calculation failed!", e) + return {} else: return usage @@ -360,7 +361,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return results def _update_costs(self, usage: dict): - if CONFIG.calc_usage: + if CONFIG.calc_usage and usage: try: prompt_tokens = int(usage["prompt_tokens"]) completion_tokens = int(usage["completion_tokens"]) From e8f45c4072a6987975bed97375f66161e3273c43 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 13:40:54 +0800 Subject: [PATCH 309/413] delete utils.py, move function to common.py --- metagpt/actions/action.py | 3 +-- metagpt/utils/common.py | 18 ++++++++++++++++++ metagpt/utils/utils.py | 22 ---------------------- 3 files changed, 19 insertions(+), 24 deletions(-) delete mode 100644 metagpt/utils/utils.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1534b1f4d..7bb26ea91 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -15,8 +15,7 @@ from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM from metagpt.logs import logger from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess -from metagpt.utils.common import OutputParser -from metagpt.utils.utils import general_after_log +from metagpt.utils.common import OutputParser, general_after_log class Action(ABC): diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index a9bdd6e2d..e9061d548 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -17,8 +17,11 @@ import inspect import os import platform import re +import typing from typing import List, Tuple, Union +from tenacity import _utils + from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger @@ -363,3 +366,18 @@ def is_subscribed(message, tags): if t in message.send_to: return True return False + + +def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + def log_it(retry_state: "RetryCallState") -> None: + if retry_state.fn is None: + fn_name = "" + else: + fn_name = _utils.get_callback_name(retry_state.fn) + logger.error( + f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " + f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " + f"exp: {retry_state.outcome.exception()}" + ) + + return log_it diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py deleted file mode 100644 index 5ceed65d9..000000000 --- a/metagpt/utils/utils.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# @Desc : - -import typing - -from tenacity import _utils - - -def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: - def log_it(retry_state: "RetryCallState") -> None: - if retry_state.fn is None: - fn_name = "" - else: - fn_name = _utils.get_callback_name(retry_state.fn) - logger.error( - f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " - f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " - f"exp: {retry_state.outcome.exception()}" - ) - - return log_it From 6f166603c4e574d6983d5a2879cc3ad3a539b2ab Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 13:51:51 +0800 Subject: [PATCH 310/413] add function import, avoid "import" --- metagpt/utils/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index e9061d548..d0528544b 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -20,7 +20,8 @@ import re import typing from typing import List, Tuple, Union -from tenacity import _utils +import loguru +from tenacity import RetryCallState, _utils from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger From b4322bca54b62bf32498af28f607f0413ae0fe0e Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 19 Dec 2023 13:55:45 +0800 Subject: [PATCH 311/413] update tests of serialize_deserialize --- .../serialize_deserialize/test_action.py | 3 +- .../test_architect_deserialize.py | 2 +- .../serialize_deserialize/test_environment.py | 12 +++--- .../serialize_deserialize/test_memory.py | 13 +++--- .../test_product_manager.py | 2 +- .../serialize_deserialize/test_role.py | 16 ++++--- .../serialize_deserialize/test_schema.py | 10 ++--- .../test_serdeser_base.py | 8 ++-- .../serialize_deserialize/test_team.py | 42 +++++++++---------- .../serialize_deserialize/test_write_code.py | 31 ++++---------- .../test_write_code_review.py | 37 ++++++++++++++++ .../test_write_design.py | 4 +- .../{test_wrire_prd.py => test_write_prd.py} | 3 +- tests/metagpt/test_schema.py | 8 +--- 14 files changed, 100 insertions(+), 91 deletions(-) create mode 100644 tests/metagpt/serialize_deserialize/test_write_code_review.py rename tests/metagpt/serialize_deserialize/{test_wrire_prd.py => test_write_prd.py} (87%) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 16369bb61..2db5d223c 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -4,9 +4,8 @@ # @Desc : import pytest -from metagpt.actions import Action, WritePRD, WriteTest +from metagpt.actions import Action, WriteTest from metagpt.llm import LLM -from metagpt.provider.openai_api import OpenAIGPTAPI def test_action_serialize(): diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index fb58f0a3a..66fba6167 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -25,4 +25,4 @@ async def test_architect_deserialize(): assert new_role.name == "Bob" assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], Action) - await new_role._actions[0].run(context="write a cli snake game") + await new_role._actions[0].run(with_messages="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py index 15336eb6a..4e3445047 100644 --- a/tests/metagpt/serialize_deserialize/test_environment.py +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -8,9 +8,11 @@ import shutil from metagpt.schema import Message from metagpt.actions.action_output import ActionOutput from metagpt.roles.project_manager import ProjectManager -from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.project_management import WriteTasks from metagpt.environment import Environment +from metagpt.utils.common import any_to_str + from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, ActionOK, serdeser_path @@ -18,7 +20,6 @@ def test_env_serialize(): env = Environment() ser_env_dict = env.dict() assert "roles" in ser_env_dict - assert "memory" in ser_env_dict def test_env_deserialize(): @@ -27,7 +28,6 @@ def test_env_deserialize(): ser_env_dict = env.dict() new_env = Environment(**ser_env_dict) assert len(new_env.roles) == 0 - assert new_env.memory.storage[0].content == "test env serialize" assert len(new_env.history) == 25 @@ -40,7 +40,7 @@ def test_environment_serdeser(): content="prd", instruct_content=ic_obj(**out_data), role="product manager", - cause_by=BossRequirement + cause_by=any_to_str(UserRequirement) ) environment = Environment() @@ -54,8 +54,6 @@ def test_environment_serdeser(): new_env: Environment = Environment(**ser_data) assert len(new_env.roles) == 1 - assert new_env.memory.count() == 1 - assert new_env.memory.storage[0].instruct_content == ic_obj(**out_data) assert list(new_env.roles.values())[0]._states == list(environment.roles.values())[0]._states assert list(new_env.roles.values())[0]._actions == list(environment.roles.values())[0]._actions assert isinstance(list(environment.roles.values())[0]._actions[0], ActionOK) @@ -82,7 +80,7 @@ def test_environment_serdeser_save(): shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) - stg_path = serdeser_path.joinpath("team/environment") + stg_path = serdeser_path.joinpath("team", "environment") environment.add_role(role_c) environment.serialize(stg_path) diff --git a/tests/metagpt/serialize_deserialize/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py index e24f31af3..50d30a94d 100644 --- a/tests/metagpt/serialize_deserialize/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -9,7 +9,8 @@ from metagpt.schema import Message from metagpt.memory.memory import Memory from metagpt.actions.action_output import ActionOutput from metagpt.actions.design_api import WriteDesign -from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.add_requirement import UserRequirement +from metagpt.utils.common import any_to_str from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path @@ -17,7 +18,7 @@ from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path def test_memory_serdeser(): msg1 = Message(role="Boss", content="write a snake game", - cause_by=BossRequirement) + cause_by=UserRequirement) out_mapping = {"field2": (list[str], ...)} out_data = {"field2": ["field2 value1", "field2 value2"]} @@ -36,14 +37,14 @@ def test_memory_serdeser(): new_msg2 = new_memory.get(2)[0] assert isinstance(new_msg2, BaseModel) assert isinstance(new_memory.storage[-1], BaseModel) - assert new_memory.storage[-1].cause_by == WriteDesign + assert new_memory.storage[-1].cause_by == any_to_str(WriteDesign) assert new_msg2.role == "Boss" def test_memory_serdeser_save(): msg1 = Message(role="User", content="write a 2048 game", - cause_by=BossRequirement) + cause_by=UserRequirement) out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} @@ -56,7 +57,7 @@ def test_memory_serdeser_save(): memory = Memory() memory.add_batch([msg1, msg2]) - stg_path = serdeser_path.joinpath("team/environment") + stg_path = serdeser_path.joinpath("team", "environment") memory.serialize(stg_path) assert stg_path.joinpath("memory.json").exists() @@ -64,7 +65,7 @@ def test_memory_serdeser_save(): assert new_memory.count() == 2 new_msg2 = new_memory.get(1)[0] assert new_msg2.instruct_content.field1 == ["field1 value1", "field1 value2"] - assert new_msg2.cause_by == WriteDesign + assert new_msg2.cause_by == any_to_str(WriteDesign) assert len(new_memory.index) == 2 stg_path.joinpath("memory.json").unlink() diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 25bc07a11..1d721282f 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -16,6 +16,6 @@ async def test_product_manager_deserialize(): new_role = ProductManager(**ser_role_dict) assert new_role.name == "Alice" - assert len(new_role._actions) == 1 + assert len(new_role._actions) == 2 assert isinstance(new_role._actions[0], Action) await new_role._actions[0].run([Message(content="write a cli snake game")]) diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 61684ba9d..fe7b63ef3 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -3,15 +3,14 @@ # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : -from pathlib import Path import shutil import pytest from metagpt.logs import logger from metagpt.roles.role import Role -from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.actions import WriteCode from metagpt.schema import Message -from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.add_requirement import UserRequirement from metagpt.roles.product_manager import ProductManager from metagpt.const import SERDESER_PATH from metagpt.roles.engineer import Engineer @@ -52,14 +51,13 @@ async def test_engineer_deserialize(): new_role = Engineer(**ser_role_dict) assert new_role.name == "Alex" assert new_role.use_code_review is True - assert len(new_role._actions) == 2 + assert len(new_role._actions) == 1 assert isinstance(new_role._actions[0], WriteCode) - assert isinstance(new_role._actions[1], WriteCodeReview) # await new_role._actions[0].run(context="write a cli snake game", filename="test_code") def test_role_serdeser_save(): - stg_path_prefix = serdeser_path.joinpath("team/environment/roles/") + stg_path_prefix = serdeser_path.joinpath("team", "environment", "roles") shutil.rmtree(serdeser_path.joinpath("team"), ignore_errors=True) pm = ProductManager() @@ -77,10 +75,10 @@ async def test_role_serdeser_interrupt(): role_c = RoleC() shutil.rmtree(SERDESER_PATH.joinpath("team"), ignore_errors=True) - stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{role_c.__class__.__name__}_{role_c.name}") + stg_path = SERDESER_PATH.joinpath(f"team", "environment", "roles", "{role_c.__class__.__name__}_{role_c.name}") try: await role_c.run( - message=Message(content="demo", cause_by=BossRequirement) + with_message=Message(content="demo", cause_by=UserRequirement) ) except Exception as exp: logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") @@ -93,5 +91,5 @@ async def test_role_serdeser_interrupt(): with pytest.raises(Exception): await role_c.run( - message=Message(content="demo", cause_by=BossRequirement) + with_message=Message(content="demo", cause_by=UserRequirement) ) diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py index 74b134cad..97ca4ea0c 100644 --- a/tests/metagpt/serialize_deserialize/test_schema.py +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -5,6 +5,7 @@ from metagpt.schema import Message from metagpt.actions.action_output import ActionOutput from metagpt.actions.write_code import WriteCode +from metagpt.utils.common import any_to_str from tests.metagpt.serialize_deserialize.test_serdeser_base import MockMessage @@ -21,15 +22,12 @@ def test_message_serdeser(): cause_by=WriteCode ) ser_data = message.dict() - assert ser_data["cause_by"] == { - "action_class": "WriteCode", - "module_name": "metagpt.actions.write_code" - } + assert ser_data["cause_by"] == "metagpt.actions.write_code.WriteCode" assert ser_data["instruct_content"]["class"] == "code" new_message = Message(**ser_data) - assert new_message.cause_by == WriteCode - assert new_message.cause_by in [WriteCode] + assert new_message.cause_by == any_to_str(WriteCode) + assert new_message.cause_by in [any_to_str(WriteCode)] assert new_message.instruct_content == ic_obj(**out_data) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 298c13823..0363c519b 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -8,11 +8,11 @@ import asyncio from metagpt.actions.action import Action from metagpt.roles.role import Role, RoleReactMode -from metagpt.actions.add_requirement import BossRequirement +from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.action_output import ActionOutput -serdeser_path = Path(__file__).absolute().parent.joinpath("../../data/serdeser_storage") +serdeser_path = Path(__file__).absolute().parent.joinpath("..", "..", "data", "serdeser_storage") class MockMessage(BaseModel): @@ -60,7 +60,7 @@ class RoleA(Role): def __init__(self, **kwargs): super(RoleA, self).__init__(**kwargs) self._init_actions([ActionPass]) - self._watch([BossRequirement]) + self._watch([UserRequirement]) class RoleB(Role): @@ -85,5 +85,5 @@ class RoleC(Role): def __init__(self, **kwargs): super(RoleC, self).__init__(**kwargs) self._init_actions([ActionOK, ActionRaise]) - self._watch([BossRequirement]) + self._watch([UserRequirement]) self._rc.react_mode = RoleReactMode.BY_ORDER diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 9c4eb8170..777f0f381 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -26,17 +26,17 @@ def test_team_deserialize(): ProjectManager(), ] ) - assert len(company.environment.get_roles()) == 3 + assert len(company.env.get_roles()) == 3 ser_company = company.dict() new_company = Team(**ser_company) - assert len(new_company.environment.get_roles()) == 3 - assert new_company.environment.get_role(pm.profile) is not None + assert len(new_company.env.get_roles()) == 3 + assert new_company.env.get_role(pm.profile) is not None - new_pm = new_company.environment.get_role(pm.profile) + new_pm = new_company.env.get_role(pm.profile) assert type(new_pm) == ProductManager - assert new_company.environment.get_role(pm.profile) is not None - assert new_company.environment.get_role(arch.profile) is not None + assert new_company.env.get_role(pm.profile) is not None + assert new_company.env.get_role(arch.profile) is not None def test_team_serdeser_save(): @@ -50,7 +50,7 @@ def test_team_serdeser_save(): new_company = Team.deserialize(stg_path) - assert len(new_company.environment.roles) == 1 + assert len(new_company.env.roles) == 1 @pytest.mark.asyncio @@ -62,21 +62,18 @@ async def test_team_recover(): company = Team() role_c = RoleC() company.hire([role_c]) - company.start_project(idea) + company.run_project(idea) await company.run(n_round=4) ser_data = company.dict() new_company = Team(**ser_data) - new_role_c = new_company.environment.get_role(role_c.profile) - assert new_role_c._rc.memory == role_c._rc.memory - assert new_role_c._rc.env == role_c._rc.env - assert new_role_c._rc.env.memory == role_c._rc.env.memory + new_role_c = new_company.env.get_role(role_c.profile) + # assert new_role_c._rc.memory == role_c._rc.memory # TODO + assert new_role_c._rc.env != role_c._rc.env # TODO + assert type(list(new_company.env.roles.values())[0]._actions[0]) == ActionOK - assert new_company.environment.memory.count() == 1 - assert type(list(new_company.environment.roles.values())[0]._actions[0]) == ActionOK - - new_company.start_project(idea) + new_company.run_project(idea) await new_company.run(n_round=4) @@ -89,19 +86,18 @@ async def test_team_recover_save(): company = Team() role_c = RoleC() company.hire([role_c]) - company.start_project(idea) + company.run_project(idea) await company.run(n_round=4) new_company = Team.recover(stg_path) - new_role_c = new_company.environment.get_role(role_c.profile) - assert new_role_c._rc.memory == role_c._rc.memory + new_role_c = new_company.env.get_role(role_c.profile) + # assert new_role_c._rc.memory == role_c._rc.memory assert new_role_c._rc.env != role_c._rc.env assert new_role_c.recovered != role_c.recovered # here cause previous ut is `!=` assert new_role_c._rc.todo != role_c._rc.todo # serialize exclude `_rc.todo` assert new_role_c._rc.news != role_c._rc.news # serialize exclude `_rc.news` - assert new_role_c._rc.env.memory == role_c._rc.env.memory - new_company.start_project(idea) + new_company.run_project(idea) await new_company.run(n_round=4) @@ -113,9 +109,9 @@ async def test_team_recover_multi_roles_save(): company = Team() company.hire([RoleA(), RoleB()]) - company.start_project(idea) + company.run_project(idea) await company.run(n_round=4) new_company = Team.recover(stg_path) - new_company.start_project(idea) + new_company.run_project(idea) await new_company.run(n_round=4) diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 5552ffd7f..0114c48da 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -2,10 +2,12 @@ # @Date : 11/23/2023 10:56 AM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + import pytest -from metagpt.actions import WriteCode, WriteCodeReview +from metagpt.actions import WriteCode from metagpt.llm import LLM +from metagpt.schema import CodingContext, Document def test_write_design_serialize(): @@ -15,30 +17,15 @@ def test_write_design_serialize(): # assert "llm" in ser_action_dict # not export -def test_write_task_serialize(): - action = WriteCodeReview() - ser_action_dict = action.dict() - assert ser_action_dict["name"] == "WriteCodeReview" - # assert "llm" in ser_action_dict # not export - - @pytest.mark.asyncio async def test_write_code_deserialize(): - action = WriteCode() + context = CodingContext(filename="test_code.py", + design_doc=Document(content="write add function to calculate two numbers")) + doc = Document(content=context.json()) + action = WriteCode(context=doc) serialized_data = action.dict() new_action = WriteCode(**serialized_data) + assert new_action.name == "WriteCode" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game", filename="test_code") - - -@pytest.mark.asyncio -async def test_write_code_review_deserialize(): - action = WriteCodeReview() - serialized_data = action.dict() - new_action = WriteCodeReview(**serialized_data) - code = await WriteCode().run(context="write a cli snake game", filename="test_code") - - assert new_action.name == "WriteCodeReview" - assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game", code=code, filename="test_rewrite_code") + await action.run() diff --git a/tests/metagpt/serialize_deserialize/test_write_code_review.py b/tests/metagpt/serialize_deserialize/test_write_code_review.py new file mode 100644 index 000000000..6ca4c6027 --- /dev/null +++ b/tests/metagpt/serialize_deserialize/test_write_code_review.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# @Desc : unittest of WriteCodeReview SerDeser + +import pytest + +from metagpt.actions import WriteCodeReview +from metagpt.llm import LLM +from metagpt.schema import CodingContext, Document + + +def test_write_task_serialize(): + action = WriteCodeReview() + ser_action_dict = action.dict() + assert ser_action_dict["name"] == "WriteCodeReview" + # assert "llm" in ser_action_dict # not export + + +@pytest.mark.asyncio +async def test_write_code_review_deserialize(): + code_content = """ +def div(a: int, b: int = 0): + return a / b +""" + context = CodingContext( + filename="test_op.py", + design_doc=Document(content="divide two numbers"), + code_doc=Document(content=code_content) + ) + + action = WriteCodeReview(context=context) + serialized_data = action.dict() + new_action = WriteCodeReview(**serialized_data) + + assert new_action.name == "WriteCodeReview" + assert new_action.llm == LLM() + await new_action.run() diff --git a/tests/metagpt/serialize_deserialize/test_write_design.py b/tests/metagpt/serialize_deserialize/test_write_design.py index 080896c98..4e768ddd7 100644 --- a/tests/metagpt/serialize_deserialize/test_write_design.py +++ b/tests/metagpt/serialize_deserialize/test_write_design.py @@ -29,7 +29,7 @@ async def test_write_design_deserialize(): new_action = WriteDesign(**serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game") + await new_action.run(with_messages="write a cli snake game") @pytest.mark.asyncio @@ -39,4 +39,4 @@ async def test_write_task_deserialize(): new_action = WriteTasks(**serialized_data) assert new_action.name == "CreateTasks" assert new_action.llm == LLM() - await new_action.run(context="write a cli snake game") + await new_action.run(with_messages="write a cli snake game") diff --git a/tests/metagpt/serialize_deserialize/test_wrire_prd.py b/tests/metagpt/serialize_deserialize/test_write_prd.py similarity index 87% rename from tests/metagpt/serialize_deserialize/test_wrire_prd.py rename to tests/metagpt/serialize_deserialize/test_write_prd.py index 0b9dfa9d8..d6d14f99a 100644 --- a/tests/metagpt/serialize_deserialize/test_wrire_prd.py +++ b/tests/metagpt/serialize_deserialize/test_write_prd.py @@ -2,6 +2,7 @@ # @Date : 11/22/2023 1:47 PM # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : + import pytest from metagpt.actions import WritePRD @@ -23,5 +24,5 @@ async def test_action_deserialize(): new_action = WritePRD(**serialized_data) assert new_action.name == "" assert new_action.llm == LLM() - action_output = await new_action.run([Message(content="write a cli snake game")]) + action_output = await new_action.run(with_messages=Message(content="write a cli snake game")) assert len(action_output.content) > 0 diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index ca8b9043f..10343c192 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -8,22 +8,16 @@ the utilization of the new feature of `Message` class. """ -<<<<<<< HEAD import json - import pytest from metagpt.actions import Action -======= ->>>>>>> a69be36abf7beef1a989a707d1aa027948c07fee from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.actions.action_output import ActionOutput from metagpt.actions.write_code import WriteCode from metagpt.utils.serialize import serialize_general_message, deserialize_general_message -<<<<<<< HEAD + from metagpt.utils.common import get_class_name -======= ->>>>>>> a69be36abf7beef1a989a707d1aa027948c07fee @pytest.mark.asyncio From 35ac28c30eae3ef9728bfd10c84bb3ae212c653e Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 19 Dec 2023 14:04:09 +0800 Subject: [PATCH 312/413] format serialize_deserialize tests code --- .../test_architect_deserialize.py | 2 +- .../metagpt/serialize_deserialize/test_environment.py | 6 ++---- tests/metagpt/serialize_deserialize/test_memory.py | 8 +++----- .../serialize_deserialize/test_product_manager.py | 2 +- .../serialize_deserialize/test_project_manager.py | 2 +- tests/metagpt/serialize_deserialize/test_role.py | 10 +++++----- tests/metagpt/serialize_deserialize/test_schema.py | 3 +-- .../serialize_deserialize/test_serdeser_base.py | 11 +++++------ tests/metagpt/serialize_deserialize/test_team.py | 5 ++--- 9 files changed, 21 insertions(+), 28 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py index 66fba6167..b92eba8a1 100644 --- a/tests/metagpt/serialize_deserialize/test_architect_deserialize.py +++ b/tests/metagpt/serialize_deserialize/test_architect_deserialize.py @@ -4,8 +4,8 @@ # @Desc : import pytest -from metagpt.roles.architect import Architect from metagpt.actions.action import Action +from metagpt.roles.architect import Architect def test_architect_serialize(): diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py index 4e3445047..3a374460c 100644 --- a/tests/metagpt/serialize_deserialize/test_environment.py +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -2,17 +2,15 @@ # -*- coding: utf-8 -*- # @Desc : -from pathlib import Path import shutil -from metagpt.schema import Message from metagpt.actions.action_output import ActionOutput -from metagpt.roles.project_manager import ProjectManager from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.project_management import WriteTasks from metagpt.environment import Environment +from metagpt.roles.project_manager import ProjectManager +from metagpt.schema import Message from metagpt.utils.common import any_to_str - from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, ActionOK, serdeser_path diff --git a/tests/metagpt/serialize_deserialize/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py index 50d30a94d..47410c615 100644 --- a/tests/metagpt/serialize_deserialize/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -2,16 +2,14 @@ # -*- coding: utf-8 -*- # @Desc : unittest of memory -from pathlib import Path from pydantic import BaseModel -from metagpt.schema import Message -from metagpt.memory.memory import Memory from metagpt.actions.action_output import ActionOutput -from metagpt.actions.design_api import WriteDesign from metagpt.actions.add_requirement import UserRequirement +from metagpt.actions.design_api import WriteDesign +from metagpt.memory.memory import Memory +from metagpt.schema import Message from metagpt.utils.common import any_to_str - from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path diff --git a/tests/metagpt/serialize_deserialize/test_product_manager.py b/tests/metagpt/serialize_deserialize/test_product_manager.py index 1d721282f..b65e329d1 100644 --- a/tests/metagpt/serialize_deserialize/test_product_manager.py +++ b/tests/metagpt/serialize_deserialize/test_product_manager.py @@ -4,8 +4,8 @@ # @Desc : import pytest -from metagpt.roles.product_manager import ProductManager from metagpt.actions.action import Action +from metagpt.roles.product_manager import ProductManager from metagpt.schema import Message diff --git a/tests/metagpt/serialize_deserialize/test_project_manager.py b/tests/metagpt/serialize_deserialize/test_project_manager.py index 21fafa72e..e52e3f247 100644 --- a/tests/metagpt/serialize_deserialize/test_project_manager.py +++ b/tests/metagpt/serialize_deserialize/test_project_manager.py @@ -4,9 +4,9 @@ # @Desc : import pytest -from metagpt.roles.project_manager import ProjectManager from metagpt.actions.action import Action from metagpt.actions.project_management import WriteTasks +from metagpt.roles.project_manager import ProjectManager def test_project_manager_serialize(): diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index fe7b63ef3..f25403dc0 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -4,18 +4,18 @@ # @Desc : import shutil + import pytest -from metagpt.logs import logger -from metagpt.roles.role import Role from metagpt.actions import WriteCode -from metagpt.schema import Message from metagpt.actions.add_requirement import UserRequirement -from metagpt.roles.product_manager import ProductManager from metagpt.const import SERDESER_PATH +from metagpt.logs import logger from metagpt.roles.engineer import Engineer +from metagpt.roles.product_manager import ProductManager +from metagpt.roles.role import Role +from metagpt.schema import Message from metagpt.utils.utils import format_trackback_info - from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py index 97ca4ea0c..02afa762d 100644 --- a/tests/metagpt/serialize_deserialize/test_schema.py +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -2,11 +2,10 @@ # -*- coding: utf-8 -*- # @Desc : unittest of schema ser&deser -from metagpt.schema import Message from metagpt.actions.action_output import ActionOutput from metagpt.actions.write_code import WriteCode +from metagpt.schema import Message from metagpt.utils.common import any_to_str - from tests.metagpt.serialize_deserialize.test_serdeser_base import MockMessage diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 0363c519b..20f708e30 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -2,15 +2,15 @@ # -*- coding: utf-8 -*- # @Desc : base test actions / roles used in unittest -from pydantic import BaseModel, Field -from pathlib import Path import asyncio +from pathlib import Path + +from pydantic import BaseModel, Field from metagpt.actions.action import Action -from metagpt.roles.role import Role, RoleReactMode -from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.action_output import ActionOutput - +from metagpt.actions.add_requirement import UserRequirement +from metagpt.roles.role import Role, RoleReactMode serdeser_path = Path(__file__).absolute().parent.joinpath("..", "..", "data", "serdeser_storage") @@ -51,7 +51,6 @@ class ActionRaise(Action): class RoleA(Role): - name: str = Field(default="RoleA") profile: str = Field(default="Role A") goal: str = "RoleA's goal" diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 777f0f381..01e0a6c70 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -3,14 +3,13 @@ # @Author : stellahong (stellahong@fuzhi.ai) # @Desc : -from pathlib import Path import shutil + import pytest +from metagpt.const import SERDESER_PATH from metagpt.roles import ProjectManager, ProductManager, Architect from metagpt.team import Team -from metagpt.const import SERDESER_PATH - from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK From d3c135edff1e5e9d34fc8414d84d4e34e3963054 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 14:17:54 +0800 Subject: [PATCH 313/413] refine utils code --- metagpt/utils/common.py | 51 ++++++++++++++++++++++++------------ tests/metagpt/test_role.py | 8 +++--- tests/metagpt/test_schema.py | 8 +++--- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index d0528544b..cdabe96a3 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -295,9 +295,6 @@ class NoMoneyException(Exception): def print_members(module, indent=0): """ https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python - :param module: - :param indent: - :return: """ prefix = " " * indent for name, obj in inspect.getmembers(module): @@ -315,6 +312,7 @@ def print_members(module, indent=0): def parse_recipient(text): + # FIXME: use ActionNode instead. pattern = r"## Send To:\s*([A-Za-z]+)\s*?" # hard code for now recipient = re.search(pattern, text) if recipient: @@ -331,18 +329,12 @@ def get_class_name(cls) -> str: return f"{cls.__module__}.{cls.__name__}" -def get_object_name(obj) -> str: - """Return class name of the object""" - cls = type(obj) - return f"{cls.__module__}.{cls.__name__}" - - -def any_to_str(val) -> str: +def any_to_str(val: str | typing.Callable) -> str: """Return the class name or the class name of the object, or 'val' if it's a string type.""" if isinstance(val, str): return val if not callable(val): - return get_object_name(val) + return get_class_name(type(val)) return get_class_name(val) @@ -350,32 +342,57 @@ def any_to_str(val) -> str: def any_to_str_set(val) -> set: """Convert any type to string set.""" res = set() - if isinstance(val, dict) or isinstance(val, list) or isinstance(val, set) or isinstance(val, tuple): + + # Check if the value is iterable, but not a string (since strings are technically iterable) + if isinstance(val, (dict, list, set, tuple)): + # Special handling for dictionaries to iterate over values + if isinstance(val, dict): + val = val.values() + for i in val: res.add(any_to_str(i)) else: res.add(any_to_str(val)) + return res -def is_subscribed(message, tags): +def is_subscribed(message: "Message", tags: set): """Return whether it's consumer""" if MESSAGE_ROUTE_TO_ALL in message.send_to: return True - for t in tags: - if t in message.send_to: + for i in tags: + if i in message.send_to: return True return False -def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: +def general_after_log(i: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + """ + Generates a logging function to be used after a call is retried. + + This generated function logs an error message with the outcome of the retried function call. It includes + the name of the function, the time taken for the call in seconds (formatted according to `sec_format`), + the number of attempts made, and the exception raised, if any. + + :param i: A Logger instance from the loguru library used to log the error message. + :param sec_format: A string format specifier for how to format the number of seconds since the start of the call. + Defaults to three decimal places. + :return: A callable that accepts a RetryCallState object and returns None. This callable logs the details + of the retried call. + """ + def log_it(retry_state: "RetryCallState") -> None: + # If the function name is not known, default to "" if retry_state.fn is None: fn_name = "" else: + # Retrieve the callable's name using a utility function fn_name = _utils.get_callback_name(retry_state.fn) - logger.error( + + # Log an error message with the function name, time since start, attempt number, and the exception + i.error( f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " f"exp: {retry_state.outcome.exception()}" diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 8fac2503c..3328607cf 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -18,7 +18,7 @@ from metagpt.actions import Action, ActionOutput from metagpt.environment import Environment from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_class_name +from metagpt.utils.common import any_to_str class MockAction(Action): @@ -88,13 +88,13 @@ async def test_react(): @pytest.mark.asyncio async def test_msg_to(): m = Message(content="a", send_to=["a", MockRole, Message]) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} m = Message(content="a", cause_by=MockAction, send_to={"a", MockRole, Message}) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} m = Message(content="a", send_to=("a", MockRole, Message)) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} if __name__ == "__main__": diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 51ebd5baa..79421fab2 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -13,7 +13,7 @@ import pytest from metagpt.actions import Action from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage -from metagpt.utils.common import get_class_name +from metagpt.utils.common import any_to_str @pytest.mark.asyncio @@ -54,9 +54,9 @@ def test_message(): m.cause_by = "Message" assert m.cause_by == "Message" m.cause_by = Action - assert m.cause_by == get_class_name(Action) + assert m.cause_by == any_to_str(Action) m.cause_by = Action() - assert m.cause_by == get_class_name(Action) + assert m.cause_by == any_to_str(Action) m.content = "b" assert m.content == "b" @@ -67,7 +67,7 @@ def test_routes(): m.send_to = "b" assert m.send_to == {"b"} m.send_to = {"e", Action} - assert m.send_to == {"e", get_class_name(Action)} + assert m.send_to == {"e", any_to_str(Action)} if __name__ == "__main__": From ebc4fe4b179acfe8c373afb8e2ee922e15fb06c6 Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 19 Dec 2023 14:22:52 +0800 Subject: [PATCH 314/413] update ser&deser after env_refactor --- metagpt/actions/action.py | 24 ++--- metagpt/actions/prepare_documents.py | 2 - metagpt/actions/write_code.py | 13 +-- metagpt/actions/write_code_review.py | 38 ++++---- metagpt/actions/write_prd.py | 18 ++-- metagpt/environment.py | 9 +- metagpt/memory/memory.py | 16 ++-- metagpt/roles/architect.py | 10 +-- metagpt/roles/engineer.py | 13 +-- metagpt/roles/product_manager.py | 3 +- metagpt/roles/project_manager.py | 2 - metagpt/roles/role.py | 129 +++++++++------------------ metagpt/schema.py | 63 +++++++------ metagpt/team.py | 9 +- metagpt/utils/utils.py | 3 +- 15 files changed, 152 insertions(+), 200 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index c941d44b6..a21f575ea 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -7,23 +7,21 @@ """ from __future__ import annotations -import re -from typing import Optional, Any from typing import Optional, Any -from tenacity import retry, stop_after_attempt, wait_random_exponential + from pydantic import BaseModel, Field +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action_output import ActionOutput from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess from metagpt.utils.common import OutputParser from metagpt.utils.utils import general_after_log from metagpt.utils.utils import import_class - action_subclass_registry = {} @@ -31,9 +29,10 @@ class Action(BaseModel): name: str = "" llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) context = "" - prefix = "" # aask*时会加上prefix,作为system_message + prefix = "" # aask*时会加上prefix,作为system_message profile = "" # FIXME: USELESS - desc = "" # for skill manager + desc = "" # for skill manager + nodes = [] # content: Optional[str] = None # instruct_content: Optional[str] = None @@ -42,7 +41,7 @@ class Action(BaseModel): class Config: arbitrary_types_allowed = True - + def __init__(self, **kwargs: Any): super().__init__(**kwargs) @@ -64,10 +63,11 @@ class Action(BaseModel): """Set prefix for later usage""" self.prefix = prefix self.profile = profile + return self def __str__(self): return self.__class__.__name__ - + def __repr__(self): return self.__str__() @@ -110,16 +110,16 @@ class Action(BaseModel): content = await self.llm.aask(prompt, system_msgs) logger.debug(f"llm raw output:\n{content}") output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - + if format == "json": parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - + logger.debug(parsed_data) instruct_content = output_class(**parsed_data) return ActionOutput(content, instruct_content) - + async def run(self, *args, **kwargs): """Run action""" raise NotImplementedError("The run method should be implemented in a subclass.") diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 8d3445ae4..af38b7eae 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -19,8 +19,6 @@ from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) async def run(self, with_messages, **kwargs): if not CONFIG.git_repo: diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index bad9a0890..046f9f456 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -16,9 +16,10 @@ """ import json -from tenacity import retry, stop_after_attempt, wait_random_exponential -from typing import List, Optional, Any +from typing import Optional + from pydantic import Field +from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG @@ -30,8 +31,8 @@ from metagpt.const import ( TEST_OUTPUTS_FILE_REPO, ) from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import CodingContext, Document, RunCodeResult from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository @@ -89,7 +90,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): name: str = "WriteCode" - context: Optional[str] = None + context: Optional[Document] = None llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) @@ -131,7 +132,9 @@ class WriteCode(Action): logger.info(f"Writing {coding_context.filename}..") code = await self.write_code(prompt) if not coding_context.code_doc: - coding_context.code_doc = Document(filename=coding_context.filename, root_path=CONFIG.src_workspace) + # avoid root_path pydantic ValidationError if use WriteCode alone + root_path = CONFIG.src_workspace if CONFIG.src_workspace else "" + coding_context.code_doc = Document(filename=coding_context.filename, root_path=root_path) coding_context.code_doc.content = code return coding_context diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 636f3f12a..f4ab0adfe 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -7,21 +7,19 @@ @Modified By: mashenquan, 2023/11/27. Following the think-act principle, solidify the task parameters when creating the WriteCode object, rather than passing them in when calling the run function. """ -from typing import List, Optional, Any -from pydantic import Field -from tenacity import retry, stop_after_attempt, wait_fixed -from typing import List, Optional, Any +from typing import Optional + from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions import WriteCode -from metagpt.llm import LLM from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.schema import CodingContext from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.schema import CodingContext from metagpt.utils.common import CodeParser PROMPT_TEMPLATE = """ @@ -39,7 +37,6 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc ``` """ - EXAMPLE_AND_INSTRUCTION = """ {format_example} @@ -127,7 +124,7 @@ REWRITE_CODE_TEMPLATE = """ class WriteCodeReview(Action): name: str = "WriteCodeReview" - context: Optional[str] = None + context: Optional[CodingContext] = None llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) @@ -147,9 +144,15 @@ class WriteCodeReview(Action): iterative_code = self.context.code_doc.content k = CONFIG.code_review_k_times or 1 for i in range(k): - format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) - task_content = self.context.task_doc.content if self.context.task_doc else "" - code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) + format_example = FORMAT_EXAMPLE.format( + filename=self.context.code_doc.filename + ) + task_content = ( + self.context.task_doc.content if self.context.task_doc else "" + ) + code_context = await WriteCode.get_codes( + self.context.task_doc, exclude=self.context.filename + ) context = "\n".join( [ "## System Design\n" + str(self.context.design_doc) + "\n", @@ -162,11 +165,16 @@ class WriteCodeReview(Action): code=iterative_code, filename=self.context.code_doc.filename, ) - cr_prompt = EXAMPLE_AND_INSTRUCTION.format(format_example=format_example, ) - logger.info( - f"Code review and rewrite {self.context.code_doc.filename}: {i+1}/{k} | {len(iterative_code)=}, {len(self.context.code_doc.content)=}" + cr_prompt = EXAMPLE_AND_INSTRUCTION.format( + format_example=format_example, + ) + logger.info( + f"Code review and rewrite {self.context.code_doc.filename}: {i + 1}/{k} | {len(iterative_code)=}, " + f"{len(self.context.code_doc.content)=}" + ) + result, rewrited_code = await self.write_code_review_and_rewrite( + context_prompt, cr_prompt, self.context.code_doc.filename ) - result, rewrited_code = await self.write_code_review_and_rewrite(context_prompt, cr_prompt, self.context.code_doc.filename) if "LBTM" in result: iterative_code = rewrited_code elif "LGTM" in result: diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 8510733ac..e76e91272 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -15,8 +15,9 @@ from __future__ import annotations import json from pathlib import Path -from typing import List, Optional, Any -from pydantic import BaseModel, Field +from typing import Optional + +from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.actions.action_node import ActionNode @@ -26,9 +27,6 @@ from metagpt.actions.write_prd_an import ( WP_ISSUE_TYPE_NODE, WRITE_PRD_NODE, ) -from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.actions.search_and_summarize import SearchAndSummarize from metagpt.config import CONFIG from metagpt.const import ( BUGFIX_FILENAME, @@ -38,13 +36,14 @@ from metagpt.const import ( PRDS_FILE_REPO, REQUIREMENT_FILENAME, ) +from metagpt.llm import LLM from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import BugFixContext, Document, Documents, Message from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository from metagpt.utils.mermaid import mermaid_to_file - CONTEXT_TEMPLATE = """ ### Project Name {project_name} @@ -75,7 +74,7 @@ class WritePRD(Action): # related to the PRD. If they are related, rewrite the PRD. docs_file_repo = CONFIG.git_repo.new_file_repository(relative_path=DOCS_FILE_REPO) requirement_doc = await docs_file_repo.get(filename=REQUIREMENT_FILENAME) - if await self._is_bugfix(requirement_doc.content): + if requirement_doc and await self._is_bugfix(requirement_doc.content): await docs_file_repo.save(filename=BUGFIX_FILENAME, content=requirement_doc.content) await docs_file_repo.save(filename=REQUIREMENT_FILENAME, content="") bug_fix = BugFixContext(filename=BUGFIX_FILENAME) @@ -144,7 +143,8 @@ class WritePRD(Action): async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: if not prd_doc: - prd = await self._run_new_requirement(requirements=[requirement_doc.content], *args, **kwargs) + prd = await self._run_new_requirement(requirements=[requirement_doc.content if requirement_doc else ""], + *args, **kwargs) new_prd_doc = Document( root_path=PRDS_FILE_REPO, filename=FileRepository.new_filename() + ".json", @@ -166,7 +166,7 @@ class WritePRD(Action): if not quadrant_chart: return pathname = ( - CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") + CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") ) if not pathname.parent.exists(): pathname.parent.mkdir(parents=True, exist_ok=True) diff --git a/metagpt/environment.py b/metagpt/environment.py index 19c77a03d..4c8d7d5e5 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -12,14 +12,12 @@ functionality is to be consolidated into the `Environment` class. """ import asyncio -from typing import Iterable, Set from pathlib import Path +from typing import Iterable, Set from pydantic import BaseModel, Field from metagpt.logs import logger -from metagpt.roles import Role -from metagpt.memory import Memory from metagpt.roles.role import Role, role_subclass_registry from metagpt.schema import Message from metagpt.utils.common import is_subscribed @@ -29,7 +27,6 @@ from metagpt.utils.utils import read_json_file, write_json_file class Environment(BaseModel): """环境,承载一批角色,角色可以向环境发布消息,可以被其他角色观察到 Environment, hosting a batch of roles, roles can publish messages to the environment, and can be observed by other roles - """ roles: dict[str, Role] = Field(default_factory=dict) @@ -63,12 +60,11 @@ class Environment(BaseModel): roles_info.append({ "role_class": role.__class__.__name__, "module_name": role.__module__, - "role_name": role.name + "role_name": role.name, }) role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) write_json_file(roles_path, roles_info) - self.memory.serialize(stg_path) history_path = stg_path.joinpath("history.json") write_json_file(history_path, {"content": self.history}) @@ -92,6 +88,7 @@ class Environment(BaseModel): "history": history }) environment.add_roles(roles) + return environment def add_role(self, role: Role): diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index b647198e3..fe70358c9 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -8,16 +8,14 @@ """ import copy from collections import defaultdict - -from typing import Iterable, Type, Union, Optional, Set from pathlib import Path +from typing import Iterable, Set + from pydantic import BaseModel, Field -import json from metagpt.schema import Message from metagpt.utils.common import any_to_str, any_to_str_set from metagpt.utils.utils import read_json_file, write_json_file -from metagpt.utils.utils import import_class class Memory(BaseModel): @@ -30,10 +28,7 @@ class Memory(BaseModel): index = kwargs.get("index", {}) new_index = defaultdict(list) for action_str, value in index.items(): - action_dict = json.loads(action_str) - action_class = import_class("Action", "metagpt.actions.action") - action_obj = action_class.deser_class(action_dict) - new_index[action_obj] = [Message(**item_dict) for item_dict in value] + new_index[action_str] = [Message(**item_dict) for item_dict in value] kwargs["index"] = new_index super(Memory, self).__init__(**kwargs) self.index = new_index @@ -43,9 +38,8 @@ class Memory(BaseModel): obj_dict = super(Memory, self).dict(*args, **kwargs) new_obj_dict = copy.deepcopy(obj_dict) new_obj_dict["index"] = {} - for action, value in obj_dict["index"].items(): - action_ser = json.dumps(action.ser_class()) - new_obj_dict["index"][action_ser] = value + for action_str, value in obj_dict["index"].items(): + new_obj_dict["index"][action_str] = value return new_obj_dict def serialize(self, stg_path: Path): diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index 266ffc256..9edfe33d9 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -23,11 +23,11 @@ class Architect(Role): constraints (str): Constraints or guidelines for the architect. """ - name: str = "Bob" - profile: str = Field(default="Architect", alias='profile') - goal: str = "design a concise, usable, complete software system" - constraints: str = "make sure the architecture is simple enough and use appropriate open source libraries." \ - "Use same language as user requirement" + name: str = Field(default="Bob") + profile: str = Field(default="Architect") + goal: str = Field(default="design a concise, usable, complete software system") + constraints: str = Field(default="make sure the architecture is simple enough and use appropriate open source " + "libraries. Use same language as user requirement") def __init__(self, **kwargs) -> None: super().__init__(**kwargs) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index ad3d0f66a..206afb38c 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -18,12 +18,14 @@ """ from __future__ import annotations -from pydantic import Field + import json from collections import defaultdict from pathlib import Path from typing import Set +from pydantic import Field + from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.fix_bug import FixBug from metagpt.actions.summarize_code import SummarizeCode @@ -45,7 +47,6 @@ from metagpt.schema import ( ) from metagpt.utils.common import any_to_str, any_to_str_set - IS_PASS_PROMPT = """ {context} @@ -69,15 +70,15 @@ class Engineer(Role): use_code_review (bool): Whether to use code review. """ name: str = "Alex" - role_profile: str = Field(default="Engineer", alias='profile') + profile: str = Field(default="Engineer") goal: str = "write elegant, readable, extensible, efficient code" constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " \ - "Use same language as user requirement", + "Use same language as user requirement" n_borg: int = 1 use_code_review: bool = False code_todos: list = [] summarize_todos = [] - + def __init__(self, **kwargs) -> None: super().__init__(**kwargs) @@ -211,7 +212,7 @@ class Engineer(Role): @staticmethod async def _new_coding_context( - filename, src_file_repo, task_file_repo, design_file_repo, dependency + filename, src_file_repo, task_file_repo, design_file_repo, dependency ) -> CodingContext: old_code_doc = await src_file_repo.get(filename) if not old_code_doc: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 30017b60d..d054b94f5 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -26,13 +26,14 @@ class ProductManager(Role): constraints (str): Constraints or limitations for the project manager. """ name: str = "Alice" - role_profile: str = Field(default="Product Manager", alias='profile') + profile: str = Field(default="Product Manager") goal: str = "efficiently create a successful product" constraints: str = "use same language as user requiremen" """ Represents a Product Manager role responsible for product development and management. """ + def __init__(self, **kwargs) -> None: super().__init__(**kwargs) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index d885f2ee6..ec93e609b 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -24,8 +24,6 @@ class ProjectManager(Role): """ name: str = Field(default="Eve") profile: str = Field(default="Project Manager") - - goal: str = "reak down tasks according to PRD/technical design, generate a task list, and analyze task " \ "dependencies to start with the prerequisite modules" constraints: str = "use same language as user requirement" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index bed5a38e7..dbbaf8713 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -20,42 +20,26 @@ """ from __future__ import annotations + from enum import Enum -from typing import Iterable, Set, Type from pathlib import Path +from typing import Iterable, Set, Type, Any + from pydantic import BaseModel, Field from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement - -from pathlib import Path - -from typing import ( - Iterable, - Type, - Any -) -from pydantic import BaseModel, Field, validator - -# from metagpt.environment import Environment -from metagpt.config import CONFIG -from metagpt.actions.action import Action, ActionOutput, action_subclass_registry +from metagpt.const import SERDESER_PATH from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.logs import logger +from metagpt.memory import Memory +from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.human_provider import HumanProvider from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output -from metagpt.memory import Memory -from metagpt.provider.human_provider import HumanProvider - from metagpt.utils.utils import read_json_file, write_json_file, import_class -from metagpt.provider.base_gpt_api import BaseGPTAPI - -from metagpt.utils.utils import read_json_file, write_json_file, import_class, role_raise_decorator -from metagpt.const import SERDESER_PATH - PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -65,12 +49,14 @@ Please note that only the text between the first and second "===" is information {history} === -You can now choose one of the following stages to decide the stage you need to go in the next step: +Your previous stage: {previous_state} + +Now choose one of the following stages you need to go to in the next step: {states} Just answer a number between 0-{n_states}, choose the most suitable stage according to the understanding of the conversation. Please note that the answer only needs a number, no need to add any other text. -If there is no conversation record, choose 0. +If you think you have completed your goal and don't need to go to any of the stages, return -1. Do not answer anything else, and do not add any other information in your answer. """ @@ -106,7 +92,7 @@ class RoleSetting(BaseModel): def __str__(self): return f"{self.name}({self.profile})" - + def __repr__(self): return self.__str__() @@ -115,37 +101,21 @@ class RoleContext(BaseModel): """Role Runtime Context""" # # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison` env: "Environment" = Field(default=None, exclude=True) - msg_buffer: MessageQueue = Field(default_factory=MessageQueue) # Message Buffer with Asynchronous Updates + # TODO judge if ser&deser + msg_buffer: MessageQueue = Field(default_factory=MessageQueue, + exclude=True) # Message Buffer with Asynchronous Updates memory: Memory = Field(default_factory=Memory) # long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None, exclude=True) - watch: set[Type[Action]] = Field(default_factory=set) + watch: set[str] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used - react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes + react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes max_react_loop: int = 1 - + class Config: arbitrary_types_allowed = True - def __init__(self, **kwargs): - watch_info = kwargs.get("watch", set()) - watch = set() - for item in watch_info: - action = Action.deser_class(item) - watch.update([action]) - kwargs["watch"] = watch - super(RoleContext, self).__init__(**kwargs) - - def dict(self, *args, **kwargs) -> "DictStrAny": - obj_dict = super(RoleContext, self).dict(*args, **kwargs) - watch = obj_dict.get("watch", set()) - watch_info = [] - for item in watch: - watch_info.append(item.ser_class()) - obj_dict["watch"] = watch_info - return obj_dict - def check(self, role_id: str): # if hasattr(CONFIG, "long_term_memory") and CONFIG.long_term_memory: # self.long_term_memory.recover_memory(role_id, self) @@ -156,26 +126,16 @@ class RoleContext(BaseModel): def important_memory(self) -> list[Message]: """Get the information corresponding to the watched actions""" return self.memory.get_by_actions(self.watch) - + @property def history(self) -> list[Message]: return self.memory.get() -class _RoleInjector(type): - def __call__(cls, *args, **kwargs): - instance = super().__call__(*args, **kwargs) - - if not instance._rc.watch: - instance._watch([UserRequirement]) - - return instance - - role_subclass_registry = {} -class Role(BaseModel, metaclass=_RoleInjector): +class Role(BaseModel): """Role/Agent""" name: str = "" profile: str = "" @@ -189,7 +149,7 @@ class Role(BaseModel, metaclass=_RoleInjector): _states: list[str] = Field(default=[]) _actions: list[Action] = Field(default=[]) _rc: RoleContext = Field(default=RoleContext) - _subscription: tuple = set() + _subscription: tuple[str] = set() # builtin variables recovered: bool = False # to tag if a recovered role @@ -203,6 +163,8 @@ class Role(BaseModel, metaclass=_RoleInjector): "_rc": RoleContext() } + __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` + class Config: arbitrary_types_allowed = True exclude = ["_llm"] @@ -240,6 +202,9 @@ class Role(BaseModel, metaclass=_RoleInjector): else: object.__setattr__(self, key, self._private_attributes[key]) + if not self._rc.watch: + self._watch([UserRequirement]) + # deserialize child classes dynamically for inherited `role` object.__setattr__(self, "builtin_class_name", self.__class__.__name__) self.__fields__["builtin_class_name"].default = self.__class__.__name__ @@ -303,7 +268,7 @@ class Role(BaseModel, metaclass=_RoleInjector): def __init_subclass__(cls, **kwargs: Any) -> None: super().__init_subclass__(**kwargs) role_subclass_registry[cls.__name__] = cls - + def _reset(self): object.__setattr__(self, "_states", []) object.__setattr__(self, "_actions", []) @@ -338,7 +303,7 @@ class Role(BaseModel, metaclass=_RoleInjector): role_class = import_class(class_name=role_class_str, module_name=module_name) role = role_class(**role_info) # initiate particular Role - role.set_recovered(True) # set True to make a tag + role.set_recovered(True) # set True to make a tag role_memory = Memory.deserialize(stg_path) role.set_memory(role_memory) @@ -362,7 +327,7 @@ class Role(BaseModel, metaclass=_RoleInjector): for idx, action in enumerate(actions): if not isinstance(action, Action): ## 默认初始化 - i = action(llm=self._llm) + i = action(name="", llm=self._llm) else: if self._setting.is_human and not isinstance(action.llm, HumanProvider): logger.warning( @@ -437,24 +402,10 @@ class Role(BaseModel, metaclass=_RoleInjector): if env: env.set_subscription(self, self._subscription) - @property - def profile(self): - """Get the role description (position)""" - return self._setting.profile - - @property - def name(self): - """Get virtual user name""" - return self._setting.name - @property def subscription(self) -> Set: """The labels for messages to be consumed by the Role object.""" return self._subscription - - def set_env(self, env: "Environment"): - """Set the environment in which the role works. The role can talk to the environment and can also receive messages by observing.""" - self._rc.env = env def _get_prefix(self): """Get the role prefix""" @@ -466,7 +417,7 @@ class Role(BaseModel, metaclass=_RoleInjector): "goal": self.goal, "constraints": self.constraints }) - + async def _think(self) -> None: """Think about what to do and decide on the next action""" if len(self._actions) == 1: @@ -475,7 +426,7 @@ class Role(BaseModel, metaclass=_RoleInjector): return if self.recovered and self._rc.state >= 0: self._set_state(self._rc.state) # action to run from recovered state - self.recovered = False # avoid max_react_loop out of work + self.recovered = False # avoid max_react_loop out of work return prompt = self._get_prefix() @@ -498,7 +449,7 @@ class Role(BaseModel, metaclass=_RoleInjector): if next_state == -1: logger.info(f"End actions with {next_state=}") self._set_state(next_state) - + async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) @@ -535,8 +486,8 @@ class Role(BaseModel, metaclass=_RoleInjector): if news_text: logger.debug(f"{self._setting} observed: {news_text}") return len(self._rc.news) - - def _publish_message(self, msg): + + def publish_message(self, msg): """If the role belongs to env, then the role's messages will be broadcast to env""" if not msg: return @@ -557,7 +508,7 @@ class Role(BaseModel, metaclass=_RoleInjector): Use llm to select actions in _think dynamically """ actions_taken = 0 - rsp = Message("No actions taken yet") # will be overwritten after Role _act + rsp = Message(content="No actions taken yet") # will be overwritten after Role _act while actions_taken < self._rc.max_react_loop: # think await self._think() @@ -580,7 +531,7 @@ class Role(BaseModel, metaclass=_RoleInjector): async def _plan_and_act(self) -> Message: """first plan, then execute an action sequence, i.e. _think (of a plan) -> _act -> _act -> ... Use llm to come up with the plan dynamically.""" # TODO: to be implemented - return Message("") + return Message(content="") async def react(self) -> Message: """Entry to one of three strategies by which Role reacts to the observed Message""" @@ -613,24 +564,24 @@ class Role(BaseModel, metaclass=_RoleInjector): def get_memories(self, k=0) -> list[Message]: """A wrapper to return the most recent k memories of this role, return all when k=0""" return self._rc.memory.get(k=k) - + async def run(self, with_message=None): """Observe, and think and act based on the results of the observation""" if with_message: msg = None if isinstance(with_message, str): - msg = Message(with_message) + msg = Message(content=with_message) elif isinstance(with_message, Message): msg = with_message elif isinstance(with_message, list): - msg = Message("\n".join(with_message)) + msg = Message(content="\n".join(with_message)) self.put_message(msg) if not await self._observe(): # If there is no new information, suspend and wait logger.debug(f"{self._setting}: no news. waiting.") return - + rsp = await self.react() # Reset the next action to be taken. diff --git a/metagpt/schema.py b/metagpt/schema.py index 962850547..690f64128 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -13,6 +13,8 @@ 3. Add `id` to `Message` according to Section 2.2.3.1.1 of RFC 135. """ +from __future__ import annotations + import asyncio import json import os.path @@ -20,14 +22,9 @@ import uuid from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path -from typing import Dict, List, Optional, Set, TypedDict -from pydantic import BaseModel, Field - -from dataclasses import dataclass, field -from typing import Type, TypedDict, Union, Optional +from typing import Dict, List, Set, TypedDict, Optional, Any from pydantic import BaseModel, Field -from pydantic.main import ModelMetaclass from metagpt.config import CONFIG from metagpt.const import ( @@ -39,15 +36,7 @@ from metagpt.const import ( TASK_FILE_REPO, ) from metagpt.logs import logger -from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ - actionoutput_str_to_mapping -from metagpt.utils.utils import import_class - from metagpt.utils.common import any_to_str, any_to_str_set -# from metagpt.utils.serialize import actionoutout_schema_to_mapping -# from metagpt.actions.action_output import ActionOutput -# from metagpt.actions.action import Action - from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ actionoutput_str_to_mapping from metagpt.utils.utils import import_class @@ -58,7 +47,6 @@ class RawMessage(TypedDict): role: str - class Document(BaseModel): """ Represents a document. @@ -68,7 +56,7 @@ class Document(BaseModel): filename: str = "" content: str = "" - def get_meta(self) -> "Document": + def get_meta(self) -> Document: """Get metadata of the document. :return: A new Document instance with the same root path and filename. @@ -120,7 +108,6 @@ class Message(BaseModel): def __init__(self, **kwargs): instruct_content = kwargs.get("instruct_content", None) - cause_by = kwargs.get("cause_by", None) if instruct_content and not isinstance(instruct_content, BaseModel): ic = instruct_content mapping = actionoutput_str_to_mapping(ic["mapping"]) @@ -129,9 +116,11 @@ class Message(BaseModel): ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) kwargs["instruct_content"] = ic_new - if cause_by and not isinstance(cause_by, ModelMetaclass): - action_class = import_class("Action", "metagpt.actions.action") - kwargs["cause_by"] = action_class.deser_class(cause_by) + + kwargs["id"] = uuid.uuid4().hex + kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", "")) + kwargs["sent_from"] = any_to_str(kwargs.get("sent_from", "")) + kwargs["send_to"] = any_to_str_set(kwargs.get("send_to", {MESSAGE_ROUTE_TO_ALL})) super(Message, self).__init__(**kwargs) def __setattr__(self, key, val): @@ -156,9 +145,6 @@ class Message(BaseModel): mapping = actionoutput_mapping_to_str(mapping) obj_dict["instruct_content"] = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} - cb = self.cause_by - if cb: - obj_dict["cause_by"] = cb.ser_class() return obj_dict def __str__(self): @@ -214,11 +200,24 @@ class AIMessage(Message): super().__init__(content=content, role="assistant") -class MessageQueue: +class MessageQueue(BaseModel): """Message queue which supports asynchronous updates.""" - def __init__(self): - self._queue = Queue() + _queue: Queue = Field(default_factory=Queue) + + _private_attributes = { + "_queue": Queue() + } + + class Config: + arbitrary_types_allowed = True + + def __init__(self, **kwargs: Any): + for key in self._private_attributes.keys(): + if key in kwargs: + object.__setattr__(self, key, kwargs[key]) + else: + object.__setattr__(self, key, self._private_attributes[key]) def pop(self) -> Message | None: """Pop one message from the queue.""" @@ -266,7 +265,7 @@ class MessageQueue: return json.dumps(lst) @staticmethod - def load(self, v) -> "MessageQueue": + def load(self, v) -> MessageQueue: """Convert the json string to the `MessageQueue` object.""" q = MessageQueue() try: @@ -287,7 +286,7 @@ class CodingContext(BaseModel): code_doc: Optional[Document] @staticmethod - def loads(val: str) -> "CodingContext" | None: + def loads(val: str) -> CodingContext | None: try: m = json.loads(val) return CodingContext(**m) @@ -301,7 +300,7 @@ class TestingContext(BaseModel): test_doc: Optional[Document] @staticmethod - def loads(val: str) -> "TestingContext" | None: + def loads(val: str) -> TestingContext | None: try: m = json.loads(val) return TestingContext(**m) @@ -322,7 +321,7 @@ class RunCodeContext(BaseModel): output: Optional[str] @staticmethod - def loads(val: str) -> "RunCodeContext" | None: + def loads(val: str) -> RunCodeContext | None: try: m = json.loads(val) return RunCodeContext(**m) @@ -336,7 +335,7 @@ class RunCodeResult(BaseModel): stderr: str @staticmethod - def loads(val: str) -> "RunCodeResult" | None: + def loads(val: str) -> RunCodeResult | None: try: m = json.loads(val) return RunCodeResult(**m) @@ -351,7 +350,7 @@ class CodeSummarizeContext(BaseModel): reason: str = "" @staticmethod - def loads(filenames: List) -> "CodeSummarizeContext": + def loads(filenames: List) -> CodeSummarizeContext: ctx = CodeSummarizeContext() for filename in filenames: if Path(filename).is_relative_to(SYSTEM_DESIGN_FILE_REPO): diff --git a/metagpt/team.py b/metagpt/team.py index bd02508c4..30e3dc618 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -8,18 +8,19 @@ Section 2.2.3.3 of RFC 135. """ from pathlib import Path + from pydantic import BaseModel, Field from metagpt.actions import UserRequirement from metagpt.config import CONFIG from metagpt.const import MESSAGE_ROUTE_TO_ALL +from metagpt.const import SERDESER_PATH from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message from metagpt.utils.common import NoMoneyException from metagpt.utils.utils import read_json_file, write_json_file, serialize_decorator -from metagpt.const import SERDESER_PATH class Team(BaseModel): @@ -39,9 +40,9 @@ class Team(BaseModel): stg_path = SERDESER_PATH.joinpath("team") if stg_path is None else stg_path team_info_path = stg_path.joinpath("team_info.json") - write_json_file(team_info_path, self.dict(exclude={"environment": True})) + write_json_file(team_info_path, self.dict(exclude={"env": True})) - self.environment.serialize(stg_path.joinpath("environment")) # save environment alone + self.env.serialize(stg_path.joinpath("environment")) # save environment alone @classmethod def recover(cls, stg_path: Path) -> "Team": @@ -60,7 +61,7 @@ class Team(BaseModel): # recover environment environment = Environment.deserialize(stg_path=stg_path.joinpath("environment")) - team_info.update({"environment": environment}) + team_info.update({"env": environment}) team = Team(**team_info) return team diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 35df654d7..57da57b00 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -9,6 +9,7 @@ from pathlib import Path import importlib from tenacity import _utils import traceback +from pydantic.json import pydantic_encoder from metagpt.logs import logger @@ -46,7 +47,7 @@ def write_json_file(json_file: str, data: list, encoding=None): folder_path.mkdir(parents=True, exist_ok=True) with open(json_file, "w", encoding=encoding) as fout: - json.dump(data, fout, ensure_ascii=False, indent=4) + json.dump(data, fout, ensure_ascii=False, indent=4, default=pydantic_encoder) def import_class(class_name: str, module_name: str) -> type: From 57121ef395c2659f8b67be025e7e7fbcd621434e Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 19 Dec 2023 15:53:14 +0800 Subject: [PATCH 315/413] remove useless code and format code --- metagpt/actions/action.py | 16 ---- metagpt/actions/design_api.py | 21 ++--- metagpt/actions/prepare_documents.py | 8 ++ metagpt/actions/project_management.py | 9 +- metagpt/actions/write_prd.py | 2 +- metagpt/actions/write_prd_review.py | 26 ++++-- metagpt/environment.py | 5 +- metagpt/memory/memory.py | 10 -- metagpt/roles/product_manager.py | 8 +- metagpt/roles/project_manager.py | 2 +- metagpt/roles/role.py | 91 +------------------ metagpt/schema.py | 3 +- metagpt/utils/serialize.py | 6 -- .../serialize_deserialize/test_action.py | 8 -- 14 files changed, 50 insertions(+), 165 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index a21f575ea..570863388 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -71,22 +71,6 @@ class Action(BaseModel): def __repr__(self): return self.__str__() - @classmethod - def ser_class(cls) -> dict: - """ serialize class type""" - return { - "action_class": cls.__name__, - "module_name": cls.__module__ - } - - @classmethod - def deser_class(cls, action_dict: dict): - """ deserialize class type """ - action_class_str = action_dict.pop("action_class") - module_name = action_dict.pop("module_name") - action_class = import_class(action_class_str, module_name) - return action_class - async def _aask(self, prompt: str, system_msgs: Optional[list[str]] = None) -> str: """Append default prefix""" if not system_msgs: diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index a13c5873a..c1778d53f 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -12,17 +12,11 @@ import json from pathlib import Path from typing import Optional + from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.actions.design_api_an import DESIGN_API_NODE -from typing import List, Optional, Any - -from pydantic import Field - -from metagpt.actions import Action, ActionOutput -from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import ( DATA_API_DESIGN_FILE_REPO, @@ -31,12 +25,13 @@ from metagpt.const import ( SYSTEM_DESIGN_FILE_REPO, SYSTEM_DESIGN_PDF_FILE_REPO, ) +from metagpt.llm import LLM from metagpt.logs import logger -from metagpt.schema import Document, Documents +from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.schema import Document, Documents, Message from metagpt.utils.file_repository import FileRepository from metagpt.utils.mermaid import mermaid_to_file - NEW_REQ_TEMPLATE = """ ### Legacy Content {old_design} @@ -50,11 +45,11 @@ class WriteDesign(Action): name: str = "" context: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) - desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " - "data structures, library tables, processes, and paths. Please provide your design, feedback " - "clearly and in detail." + desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " \ + "data structures, library tables, processes, and paths. Please provide your design, feedback " \ + "clearly and in detail." - async def run(self, with_messages, format=CONFIG.prompt_format): + async def run(self, with_messages: Message, format: str = CONFIG.prompt_format): # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) changed_prds = prds_file_repo.changed_files diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index af38b7eae..6bb18be7b 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -9,16 +9,24 @@ """ import shutil from pathlib import Path +from typing import Optional + +from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG from metagpt.const import DEFAULT_WORKSPACE_ROOT, DOCS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Document from metagpt.utils.file_repository import FileRepository from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): + name: str = "PrepareDocuments" + context: Optional[str] = None + llm: BaseGPTAPI = Field(default_factory=LLM) async def run(self, with_messages, **kwargs): if not CONFIG.git_repo: diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 98a948b64..2727f7e7f 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -11,14 +11,13 @@ """ import json -from typing import List, Optional, Any +from typing import Optional + from pydantic import Field from metagpt.actions import ActionOutput from metagpt.actions.action import Action from metagpt.actions.project_management_an import PM_NODE -from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.config import CONFIG from metagpt.const import ( PACKAGE_REQUIREMENTS_FILENAME, @@ -26,11 +25,11 @@ from metagpt.const import ( TASK_FILE_REPO, TASK_PDF_FILE_REPO, ) +from metagpt.llm import LLM from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Document, Documents from metagpt.utils.file_repository import FileRepository -from metagpt.provider.base_gpt_api import BaseGPTAPI - NEW_REQ_TEMPLATE = """ ### Legacy Content diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index e76e91272..f087d8650 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -166,7 +166,7 @@ class WritePRD(Action): if not quadrant_chart: return pathname = ( - CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") + CONFIG.git_repo.workdir / Path(COMPETITIVE_ANALYSIS_FILE_REPO) / Path(prd_doc.filename).with_suffix("") ) if not pathname.parent.exists(): pathname.parent.mkdir(parents=True, exist_ok=True) diff --git a/metagpt/actions/write_prd_review.py b/metagpt/actions/write_prd_review.py index 5ff9624c5..6ed73b6a2 100644 --- a/metagpt/actions/write_prd_review.py +++ b/metagpt/actions/write_prd_review.py @@ -5,20 +5,28 @@ @Author : alexanderwu @File : write_prd_review.py """ + +from typing import Optional + +from pydantic import Field + from metagpt.actions.action import Action +from metagpt.llm import LLM +from metagpt.provider.base_gpt_api import BaseGPTAPI class WritePRDReview(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.prd = None - self.desc = "Based on the PRD, conduct a PRD Review, providing clear and detailed feedback" - self.prd_review_prompt_template = """ - Given the following Product Requirement Document (PRD): - {prd} + name: str = "" + context: Optional[str] = None + llm: BaseGPTAPI = Field(default_factory=LLM) + prd: Optional[str] = None + desc: str = "Based on the PRD, conduct a PRD Review, providing clear and detailed feedback" + prd_review_prompt_template: str = """ +Given the following Product Requirement Document (PRD): +{prd} - As a project manager, please review it and provide your feedback and suggestions. - """ +As a project manager, please review it and provide your feedback and suggestions. +""" async def run(self, prd): self.prd = prd diff --git a/metagpt/environment.py b/metagpt/environment.py index 4c8d7d5e5..9108cdf06 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -61,6 +61,7 @@ class Environment(BaseModel): "role_class": role.__class__.__name__, "module_name": role.__module__, "role_name": role.name, + "role_sub_tags": list(self.members.get(role)) }) role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) write_json_file(roles_path, roles_info) @@ -70,14 +71,13 @@ class Environment(BaseModel): @classmethod def deserialize(cls, stg_path: Path) -> "Environment": - """ stg_path: ./storage/team/environment/ """ """ stg_path: ./storage/team/environment/ """ roles_path = stg_path.joinpath("roles.json") roles_info = read_json_file(roles_path) roles = [] for role_info in roles_info: # role stored in ./environment/roles/{role_class}_{role_name} - role_path = stg_path.joinpath(f'roles/{role_info.get("role_class")}_{role_info.get("role_name")}') + role_path = stg_path.joinpath(f"roles/{role_info.get('role_class')}_{role_info.get('role_name')}") role = Role.deserialize(role_path) roles.append(role) @@ -96,7 +96,6 @@ class Environment(BaseModel): Add a role in the current environment """ role.set_env(self) - # use alias self.roles[role.profile] = role def add_roles(self, roles: Iterable[Role]): diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index fe70358c9..198c0970d 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -20,7 +20,6 @@ from metagpt.utils.utils import read_json_file, write_json_file class Memory(BaseModel): """The most basic memory: super-memory""" - storage: list[Message] = Field(default=[]) index: dict[str, list[Message]] = Field(default_factory=defaultdict(list)) @@ -33,15 +32,6 @@ class Memory(BaseModel): super(Memory, self).__init__(**kwargs) self.index = new_index - def dict(self, *args, **kwargs) -> "DictStrAny": - """ overwrite the `dict` to dump dynamic pydantic model""" - obj_dict = super(Memory, self).dict(*args, **kwargs) - new_obj_dict = copy.deepcopy(obj_dict) - new_obj_dict["index"] = {} - for action_str, value in obj_dict["index"].items(): - new_obj_dict["index"][action_str] = value - return new_obj_dict - def serialize(self, stg_path: Path): """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ memory_path = stg_path.joinpath("memory.json") diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index d054b94f5..11bda2127 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -17,7 +17,7 @@ from metagpt.roles.role import Role class ProductManager(Role): """ - Represents a Project Manager role responsible for overseeing project execution and team efficiency. + Represents a Product Manager role responsible for product development and management. Attributes: name (str): Name of the project manager. @@ -28,11 +28,7 @@ class ProductManager(Role): name: str = "Alice" profile: str = Field(default="Product Manager") goal: str = "efficiently create a successful product" - constraints: str = "use same language as user requiremen" - - """ - Represents a Product Manager role responsible for product development and management. - """ + constraints: str = "use same language as user requirement" def __init__(self, **kwargs) -> None: super().__init__(**kwargs) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index ec93e609b..f98d28cb7 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -24,7 +24,7 @@ class ProjectManager(Role): """ name: str = Field(default="Eve") profile: str = Field(default="Project Manager") - goal: str = "reak down tasks according to PRD/technical design, generate a task list, and analyze task " \ + goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " \ "dependencies to start with the prerequisite modules" constraints: str = "use same language as user requirement" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index dbbaf8713..9b1e0bf94 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -81,22 +81,6 @@ class RoleReactMode(str, Enum): return [item.value for item in cls] -class RoleSetting(BaseModel): - """Role Settings""" - name: str = "" - profile: str = "" - goal: str = "" - constraints: str = "" - desc: str = "" - is_human: bool = False - - def __str__(self): - return f"{self.name}({self.profile})" - - def __repr__(self): - return self.__str__() - - class RoleContext(BaseModel): """Role Runtime Context""" # # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison` @@ -160,7 +144,8 @@ class Role(BaseModel): "_role_id": _role_id, "_states": [], "_actions": [], - "_rc": RoleContext() + "_rc": RoleContext(), + "_subscription": set() } __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` @@ -186,7 +171,7 @@ class Role(BaseModel): # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 self._private_attributes["_llm"] = LLM() if not self.is_human else HumanProvider() self._private_attributes["_role_id"] = str(self._setting) - self._subscription = {any_to_str(self), name} if name else {any_to_str(self)} + self._private_attributes["_subscription"] = {any_to_str(self), self.name} if self.name else {any_to_str(self)} for key in self._private_attributes.keys(): if key in kwargs: @@ -202,64 +187,7 @@ class Role(BaseModel): else: object.__setattr__(self, key, self._private_attributes[key]) - if not self._rc.watch: - self._watch([UserRequirement]) - - # deserialize child classes dynamically for inherited `role` - object.__setattr__(self, "builtin_class_name", self.__class__.__name__) - self.__fields__["builtin_class_name"].default = self.__class__.__name__ - - def __init_subclass__(cls, **kwargs: Any) -> None: - super().__init_subclass__(**kwargs) - role_subclass_registry[cls.__name__] = cls - - # builtin variables - recovered: bool = False # to tag if a recovered role - builtin_class_name: str = "" - - _private_attributes = { - "_llm": LLM() if not is_human else HumanProvider(), - "_role_id": _role_id, - "_states": [], - "_actions": [], - "_rc": RoleContext() - } - - class Config: - arbitrary_types_allowed = True - exclude = ["_llm"] - - def __init__(self, **kwargs: Any): - for index in range(len(kwargs.get("_actions", []))): - current_action = kwargs["_actions"][index] - if isinstance(current_action, dict): - item_class_name = current_action.get("builtin_class_name", None) - for name, subclass in action_subclass_registry.items(): - registery_class_name = subclass.__fields__["builtin_class_name"].default - if item_class_name == registery_class_name: - current_action = subclass(**current_action) - break - kwargs["_actions"][index] = current_action - - super().__init__(**kwargs) - - # 关于私有变量的初始化 https://github.com/pydantic/pydantic/issues/655 - self._private_attributes["_llm"] = LLM() if not self.is_human else HumanProvider() - self._private_attributes["_role_id"] = str(self._setting) - - for key in self._private_attributes.keys(): - if key in kwargs: - object.__setattr__(self, key, kwargs[key]) - if key == "_rc": - _rc = RoleContext(**kwargs["_rc"]) - object.__setattr__(self, "_rc", _rc) - else: - if key == "_rc": - # # Warning, if use self._private_attributes["_rc"], - # # self._rc will be a shared object between roles, so init one or reset it inside `_reset` - object.__setattr__(self, key, RoleContext()) - else: - object.__setattr__(self, key, self._private_attributes[key]) + self._llm.system_prompt = self._get_prefix() # deserialize child classes dynamically for inherited `role` object.__setattr__(self, "builtin_class_name", self.__class__.__name__) @@ -341,9 +269,6 @@ class Role(BaseModel): self._actions.append(i) self._states.append(f"{idx}. {action}") - def set_react_mode(self, react_mode: RoleReactMode, max_react_loop: int = 1): - self._set_react_mode(react_mode, max_react_loop) - def _set_react_mode(self, react_mode: str, max_react_loop: int = 1): """Set strategy of the Role reacting to observed Message. Variation lies in how this Role elects action to perform during the _think stage, especially if it is capable of multiple Actions. @@ -365,9 +290,6 @@ class Role(BaseModel): if react_mode == RoleReactMode.REACT: self._rc.max_react_loop = max_react_loop - def watch(self, actions: Iterable[Type[Action]]): - self._watch(actions) - def _watch(self, actions: Iterable[Type[Action]]): """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe. @@ -386,9 +308,6 @@ class Role(BaseModel): if self._rc.env: # According to the routing feature plan in Chapter 2.2.3.2 of RFC 113 self._rc.env.set_subscription(self, self._subscription) - def set_state(self, state: int): - self._set_state(state) - def _set_state(self, state: int): """Update the current state.""" self._rc.state = state @@ -436,7 +355,7 @@ class Role(BaseModel): n_states=len(self._states) - 1, previous_state=self._rc.state, ) - # print(prompt) + next_state = await self._llm.aask(prompt) next_state = extract_state_value_from_output(next_state) logger.debug(f"{prompt=}") diff --git a/metagpt/schema.py b/metagpt/schema.py index 690f64128..0ec9b5c60 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -26,6 +26,7 @@ from typing import Dict, List, Set, TypedDict, Optional, Any from pydantic import BaseModel, Field +from metagpt.actions import UserRequirement from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, @@ -118,7 +119,7 @@ class Message(BaseModel): kwargs["instruct_content"] = ic_new kwargs["id"] = uuid.uuid4().hex - kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", "")) + kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", UserRequirement)) kwargs["sent_from"] = any_to_str(kwargs.get("sent_from", "")) kwargs["send_to"] = any_to_str_set(kwargs.get("send_to", {MESSAGE_ROUTE_TO_ALL})) super(Message, self).__init__(**kwargs) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 9a7049214..93f584057 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -68,9 +68,6 @@ def serialize_general_message(message: "Message") -> dict: mapping = actionoutput_mapping_to_str(mapping) message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} - cb = message_cp.cause_by - if cb: - message_cp.cause_by = cb.ser_class() return message_cp.dict() @@ -103,9 +100,6 @@ def deserialize_general_message(message_dict: dict) -> "Message": ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new - if cause_by: - action_class = import_class("Action", "metagpt.actions.action") - message.cause_by = action_class.deser_class(cause_by) return message diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 2db5d223c..63d8e7b7c 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -25,11 +25,3 @@ async def test_action_deserialize(): assert new_action.name == "" assert new_action.llm == LLM() assert len(await new_action._aask("who are you")) > 0 - - -def test_action_serdeser(): - action_info = WriteTest.ser_class() - assert action_info["action_class"] == "WriteTest" - - action_class = Action.deser_class(action_info) - assert action_class == WriteTest From f1c6a7ebfbad1c571574ca4f9b85c14e24221e33 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:16:52 +0800 Subject: [PATCH 316/413] refine code: use handle_exception function instead of in-function duplicate code frags --- metagpt/actions/action_node.py | 2 +- metagpt/actions/run_code.py | 30 ++++----- metagpt/config.py | 1 + metagpt/repo_parser.py | 19 ++++-- metagpt/schema.py | 78 ++++++++-------------- metagpt/tools/search_engine_meilisearch.py | 12 ++-- metagpt/utils/common.py | 10 +++ metagpt/utils/custom_decoder.py | 2 +- metagpt/utils/dependency_file.py | 20 ++---- metagpt/utils/exceptions.py | 59 ++++++++++++++++ metagpt/utils/file.py | 45 ++++++------- metagpt/utils/file_repository.py | 11 +-- 12 files changed, 159 insertions(+), 130 deletions(-) create mode 100644 metagpt/utils/exceptions.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 9bb12fc84..6f1215920 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -43,7 +43,7 @@ Fill in the above nodes based on the format example. """ -def dict_to_markdown(d, prefix="-", postfix="\n"): +def dict_to_markdown(d, prefix="###", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index fa13a0980..1b9fd252f 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -16,13 +16,13 @@ class. """ import subprocess -import traceback from typing import Tuple from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import RunCodeResult +from metagpt.utils.exceptions import handle_exception PROMPT_TEMPLATE = """ Role: You are a senior development and qa engineer, your role is summarize the code running result. @@ -78,15 +78,12 @@ class RunCode(Action): super().__init__(name, context, llm) @classmethod + @handle_exception async def run_text(cls, code) -> Tuple[str, str]: - try: - # We will document_store the result in this dictionary - namespace = {} - exec(code, namespace) - return namespace.get("result", ""), "" - except Exception: - # If there is an error in the code, return the error message - return "", traceback.format_exc() + # We will document_store the result in this dictionary + namespace = {} + exec(code, namespace) + return namespace.get("result", ""), "" @classmethod async def run_script(cls, working_directory, additional_python_paths=[], command=[]) -> Tuple[str, str]: @@ -145,18 +142,17 @@ class RunCode(Action): rsp = await self._aask(prompt) return RunCodeResult(summary=rsp, stdout=outs, stderr=errs) + @staticmethod + @handle_exception(exception_type=subprocess.CalledProcessError) + def _install_via_subprocess(cmd, check, cwd, env): + return subprocess.run(cmd, check=check, cwd=cwd, env=env) + @staticmethod def _install_dependencies(working_directory, env): install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"] logger.info(" ".join(install_command)) - try: - subprocess.run(install_command, check=True, cwd=working_directory, env=env) - except subprocess.CalledProcessError as e: - logger.warning(f"{e}") + RunCode._install_via_subprocess(install_command, check=True, cwd=working_directory, env=env) install_pytest_command = ["python", "-m", "pip", "install", "pytest"] logger.info(" ".join(install_pytest_command)) - try: - subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env) - except subprocess.CalledProcessError as e: - logger.warning(f"{e}") + RunCode._install_via_subprocess(install_pytest_command, check=True, cwd=working_directory, env=env) diff --git a/metagpt/config.py b/metagpt/config.py index 19bd02c87..45a560209 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -137,6 +137,7 @@ class Config(metaclass=Singleton): continue configs.update(yaml_data) OPTIONS.set(configs) + logger.info(f"Default OpenAI API Model: {self.openai_api_model}") @staticmethod def _get(*args, **kwargs): diff --git a/metagpt/repo_parser.py b/metagpt/repo_parser.py index b84dbab9a..9a1218ef1 100644 --- a/metagpt/repo_parser.py +++ b/metagpt/repo_parser.py @@ -15,17 +15,17 @@ from pydantic import BaseModel, Field from metagpt.config import CONFIG from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception class RepoParser(BaseModel): base_directory: Path = Field(default=None) - def parse_file(self, file_path): + @classmethod + @handle_exception(exception_type=Exception, default_return=[]) + def _parse_file(cls, file_path: Path) -> list: """Parse a Python file in the repository.""" - try: - return ast.parse(file_path.read_text()).body - except: - return [] + return ast.parse(file_path.read_text()).body def extract_class_and_function_info(self, tree, file_path): """Extract class, function, and global variable information from the AST.""" @@ -52,7 +52,7 @@ class RepoParser(BaseModel): files_classes = [] directory = self.base_directory for path in directory.rglob("*.py"): - tree = self.parse_file(path) + tree = self._parse_file(path) file_info = self.extract_class_and_function_info(tree, path) files_classes.append(file_info) @@ -90,5 +90,10 @@ def main(): logger.info(pformat(symbols)) +def error(): + """raise Exception and logs it""" + RepoParser._parse_file(Path("test.py")) + + if __name__ == "__main__": - main() + error() diff --git a/metagpt/schema.py b/metagpt/schema.py index 758149efa..7359084f5 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -21,7 +21,7 @@ import uuid from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path -from typing import Dict, List, Optional, Set, TypedDict +from typing import Dict, List, Optional, Set, Type, TypedDict, TypeVar from pydantic import BaseModel, Field @@ -36,6 +36,7 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.utils.common import any_to_str, any_to_str_set +from metagpt.utils.exceptions import handle_exception class RawMessage(TypedDict): @@ -160,14 +161,11 @@ class Message(BaseModel): return self.json(exclude_none=True) @staticmethod + @handle_exception(exception_type=JSONDecodeError, default_return=None) def load(val): """Convert the json string to object.""" - try: - d = json.loads(val) - return Message(**d) - except JSONDecodeError as err: - logger.error(f"parse json failed: {val}, error:{err}") - return None + d = json.loads(val) + return Message(**d) class UserMessage(Message): @@ -249,50 +247,46 @@ class MessageQueue: return json.dumps(lst) @staticmethod - def load(self, v) -> "MessageQueue": + def load(i) -> "MessageQueue": """Convert the json string to the `MessageQueue` object.""" - q = MessageQueue() + queue = MessageQueue() try: - lst = json.loads(v) + lst = json.loads(i) for i in lst: msg = Message(**i) - q.push(msg) + queue.push(msg) except JSONDecodeError as e: - logger.warning(f"JSON load failed: {v}, error:{e}") + logger.warning(f"JSON load failed: {i}, error:{e}") - return q + return queue -class CodingContext(BaseModel): +# 定义一个泛型类型变量 +T = TypeVar("T", bound="BaseModel") + + +class BaseContext(BaseModel): + @staticmethod + @handle_exception + def loads(val: str, cls: Type[T]) -> Optional[T]: + m = json.loads(val) + return cls(**m) + + +class CodingContext(BaseContext): filename: str design_doc: Optional[Document] task_doc: Optional[Document] code_doc: Optional[Document] - @staticmethod - def loads(val: str) -> CodingContext | None: - try: - m = json.loads(val) - return CodingContext(**m) - except Exception: - return None - -class TestingContext(BaseModel): +class TestingContext(BaseContext): filename: str code_doc: Document test_doc: Optional[Document] - @staticmethod - def loads(val: str) -> TestingContext | None: - try: - m = json.loads(val) - return TestingContext(**m) - except Exception: - return None - -class RunCodeContext(BaseModel): +class RunCodeContext(BaseContext): mode: str = "script" code: Optional[str] code_filename: str = "" @@ -304,28 +298,12 @@ class RunCodeContext(BaseModel): output_filename: Optional[str] output: Optional[str] - @staticmethod - def loads(val: str) -> RunCodeContext | None: - try: - m = json.loads(val) - return RunCodeContext(**m) - except Exception: - return None - -class RunCodeResult(BaseModel): +class RunCodeResult(BaseContext): summary: str stdout: str stderr: str - @staticmethod - def loads(val: str) -> RunCodeResult | None: - try: - m = json.loads(val) - return RunCodeResult(**m) - except Exception: - return None - class CodeSummarizeContext(BaseModel): design_filename: str = "" @@ -349,5 +327,5 @@ class CodeSummarizeContext(BaseModel): return hash((self.design_filename, self.task_filename)) -class BugFixContext(BaseModel): +class BugFixContext(BaseContext): filename: str = "" diff --git a/metagpt/tools/search_engine_meilisearch.py b/metagpt/tools/search_engine_meilisearch.py index f7c1c685a..ea6db4dbd 100644 --- a/metagpt/tools/search_engine_meilisearch.py +++ b/metagpt/tools/search_engine_meilisearch.py @@ -11,6 +11,8 @@ from typing import List import meilisearch from meilisearch.index import Index +from metagpt.utils.exceptions import handle_exception + class DataSource: def __init__(self, name: str, url: str): @@ -34,11 +36,7 @@ class MeilisearchEngine: index.add_documents(documents) self.set_index(index) + @handle_exception(exception_type=Exception, default_return=[]) def search(self, query): - try: - search_results = self._index.search(query) - return search_results["hits"] - except Exception as e: - # Handle MeiliSearch API errors - print(f"MeiliSearch API error: {e}") - return [] + search_results = self._index.search(query) + return search_results["hits"] diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index cdabe96a3..bf435b74f 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -20,11 +20,13 @@ import re import typing from typing import List, Tuple, Union +import aiofiles import loguru from tenacity import RetryCallState, _utils from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception def check_cmd_exists(command) -> int: @@ -399,3 +401,11 @@ def general_after_log(i: "loguru.Logger", sec_format: str = "%0.3f") -> typing.C ) return log_it + + +@handle_exception +async def aread(file_path: str) -> str: + """Read file asynchronously.""" + async with aiofiles.open(str(file_path), mode="r") as reader: + content = await reader.read() + return content diff --git a/metagpt/utils/custom_decoder.py b/metagpt/utils/custom_decoder.py index 373d16356..eb01a1115 100644 --- a/metagpt/utils/custom_decoder.py +++ b/metagpt/utils/custom_decoder.py @@ -25,7 +25,7 @@ def py_make_scanner(context): except IndexError: raise StopIteration(idx) from None - if nextchar == '"' or nextchar == "'": + if nextchar in ("'", '"'): if idx + 2 < len(string) and string[idx + 1] == nextchar and string[idx + 2] == nextchar: # Handle the case where the next two characters are the same as nextchar return parse_string(string, idx + 3, strict, delimiter=nextchar * 3) # triple quote diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index e8347d567..d03444f0e 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -15,7 +15,8 @@ from typing import Set import aiofiles from metagpt.config import CONFIG -from metagpt.logs import logger +from metagpt.utils.common import aread +from metagpt.utils.exceptions import handle_exception class DependencyFile: @@ -36,21 +37,14 @@ class DependencyFile: """Load dependencies from the file asynchronously.""" if not self._filename.exists(): return - try: - async with aiofiles.open(str(self._filename), mode="r") as reader: - data = await reader.read() - self._dependencies = json.loads(data) - except Exception as e: - logger.error(f"Failed to load {str(self._filename)}, error:{e}") + self._dependencies = await aread(self._filename) + @handle_exception async def save(self): """Save dependencies to the file asynchronously.""" - try: - data = json.dumps(self._dependencies) - async with aiofiles.open(str(self._filename), mode="w") as writer: - await writer.write(data) - except Exception as e: - logger.error(f"Failed to save {str(self._filename)}, error:{e}") + data = json.dumps(self._dependencies) + async with aiofiles.open(str(self._filename), mode="w") as writer: + await writer.write(data) async def update(self, filename: Path | str, dependencies: Set[Path | str], persist=True): """Update dependencies for a file asynchronously. diff --git a/metagpt/utils/exceptions.py b/metagpt/utils/exceptions.py new file mode 100644 index 000000000..b4b5aa590 --- /dev/null +++ b/metagpt/utils/exceptions.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/19 14:46 +@Author : alexanderwu +@File : exceptions.py +""" + + +import asyncio +import functools +import traceback +from typing import Any, Callable, Tuple, Type, TypeVar, Union + +from metagpt.logs import logger + +ReturnType = TypeVar("ReturnType") + + +def handle_exception( + _func: Callable[..., ReturnType] = None, + *, + exception_type: Union[Type[Exception], Tuple[Type[Exception], ...]] = Exception, + default_return: Any = None, +) -> Callable[..., ReturnType]: + """handle exception, return default value""" + + def decorator(func: Callable[..., ReturnType]) -> Callable[..., ReturnType]: + @functools.wraps(func) + async def async_wrapper(*args: Any, **kwargs: Any) -> ReturnType: + try: + return await func(*args, **kwargs) + except exception_type as e: + logger.opt(depth=1).error( + f"Calling {func.__name__} with args: {args}, kwargs: {kwargs} failed: {e}, " + f"stack: {traceback.format_exc()}" + ) + return default_return + + @functools.wraps(func) + def sync_wrapper(*args: Any, **kwargs: Any) -> ReturnType: + try: + return func(*args, **kwargs) + except exception_type as e: + logger.opt(depth=1).error( + f"Calling {func.__name__} with args: {args}, kwargs: {kwargs} failed: {e}, " + f"stack: {traceback.format_exc()}" + ) + return default_return + + if asyncio.iscoroutinefunction(func): + return async_wrapper + else: + return sync_wrapper + + if _func is None: + return decorator + else: + return decorator(_func) diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index 6bb9a1a97..f62b44eb8 100644 --- a/metagpt/utils/file.py +++ b/metagpt/utils/file.py @@ -11,6 +11,7 @@ from pathlib import Path import aiofiles from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception class File: @@ -19,6 +20,7 @@ class File: CHUNK_SIZE = 64 * 1024 @classmethod + @handle_exception async def write(cls, root_path: Path, filename: str, content: bytes) -> Path: """Write the file content to the local specified path. @@ -33,18 +35,15 @@ class File: Raises: Exception: If an unexpected error occurs during the file writing process. """ - try: - root_path.mkdir(parents=True, exist_ok=True) - full_path = root_path / filename - async with aiofiles.open(full_path, mode="wb") as writer: - await writer.write(content) - logger.debug(f"Successfully write file: {full_path}") - return full_path - except Exception as e: - logger.error(f"Error writing file: {e}") - raise e + root_path.mkdir(parents=True, exist_ok=True) + full_path = root_path / filename + async with aiofiles.open(full_path, mode="wb") as writer: + await writer.write(content) + logger.debug(f"Successfully write file: {full_path}") + return full_path @classmethod + @handle_exception async def read(cls, file_path: Path, chunk_size: int = None) -> bytes: """Partitioning read the file content from the local specified path. @@ -58,18 +57,14 @@ class File: Raises: Exception: If an unexpected error occurs during the file reading process. """ - try: - chunk_size = chunk_size or cls.CHUNK_SIZE - async with aiofiles.open(file_path, mode="rb") as reader: - chunks = list() - while True: - chunk = await reader.read(chunk_size) - if not chunk: - break - chunks.append(chunk) - content = b"".join(chunks) - logger.debug(f"Successfully read file, the path of file: {file_path}") - return content - except Exception as e: - logger.error(f"Error reading file: {e}") - raise e + chunk_size = chunk_size or cls.CHUNK_SIZE + async with aiofiles.open(file_path, mode="rb") as reader: + chunks = list() + while True: + chunk = await reader.read(chunk_size) + if not chunk: + break + chunks.append(chunk) + content = b"".join(chunks) + logger.debug(f"Successfully read file, the path of file: {file_path}") + return content diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 2eca799a8..099556a6b 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -19,6 +19,7 @@ import aiofiles from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import Document +from metagpt.utils.common import aread from metagpt.utils.json_to_markdown import json_to_markdown @@ -97,15 +98,7 @@ class FileRepository: path_name = self.workdir / filename if not path_name.exists(): return None - try: - async with aiofiles.open(str(path_name), mode="r") as reader: - doc.content = await reader.read() - except FileNotFoundError as e: - logger.info(f"open {str(path_name)} failed:{e}") - return None - except Exception as e: - logger.info(f"open {str(path_name)} failed:{e}") - return None + doc.content = await aread(path_name) return doc async def get_all(self) -> List[Document]: From d5d7db0bf80d6210bbf71d5f274fe49c5ef7575d Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:22:29 +0800 Subject: [PATCH 317/413] bug fix and proper log --- metagpt/config.py | 4 ++-- metagpt/utils/dependency_file.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 45a560209..629a5b797 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -48,6 +48,7 @@ class Config(metaclass=Singleton): self._init_with_config_files_and_env(yaml_file) logger.debug("Config loading done.") self._update() + logger.info(f"OpenAI API Model: {self.openai_api_model}") def _update(self): # logger.info("Config loading done.") @@ -74,7 +75,7 @@ class Config(metaclass=Singleton): self.openai_api_type = self._get("OPENAI_API_TYPE") self.openai_api_version = self._get("OPENAI_API_VERSION") self.openai_api_rpm = self._get("RPM", 3) - self.openai_api_model = self._get("OPENAI_API_MODEL", "gpt-4") + self.openai_api_model = self._get("OPENAI_API_MODEL", "gpt-4-1106-preview") self.max_tokens_rsp = self._get("MAX_TOKENS", 2048) self.deployment_name = self._get("DEPLOYMENT_NAME") self.deployment_id = self._get("DEPLOYMENT_ID") @@ -137,7 +138,6 @@ class Config(metaclass=Singleton): continue configs.update(yaml_data) OPTIONS.set(configs) - logger.info(f"Default OpenAI API Model: {self.openai_api_model}") @staticmethod def _get(*args, **kwargs): diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index d03444f0e..8a6575e9e 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -37,7 +37,7 @@ class DependencyFile: """Load dependencies from the file asynchronously.""" if not self._filename.exists(): return - self._dependencies = await aread(self._filename) + self._dependencies = json.loads(await aread(self._filename)) @handle_exception async def save(self): From 2c1538f35035a5732137ef760a1b86d2bac50ada Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:31:38 +0800 Subject: [PATCH 318/413] bug fix and proper log --- metagpt/schema.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index 7359084f5..b24f114b0 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -266,11 +266,11 @@ T = TypeVar("T", bound="BaseModel") class BaseContext(BaseModel): - @staticmethod + @classmethod @handle_exception - def loads(val: str, cls: Type[T]) -> Optional[T]: - m = json.loads(val) - return cls(**m) + def loads(cls: Type[T], val: str) -> Optional[T]: + i = json.loads(val) + return cls(**i) class CodingContext(BaseContext): From e67dbc92e42fcc9479027df6e9d00eabce20df36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Tue, 19 Dec 2023 16:37:01 +0800 Subject: [PATCH 319/413] feat: disable -- max_auto_summarize_code --- metagpt/startup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index f930c386b..e886ad2a4 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -26,7 +26,7 @@ def startup( ), reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), max_auto_summarize_code: int = typer.Option( - default=-1, + default=0, help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. This parameter is used for debugging the workflow.", ), ): From 93745b85ccfbe7b953c17a36867dc823ff2699c5 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:54:06 +0800 Subject: [PATCH 320/413] refine config --- config/config.yaml | 2 +- metagpt/config.py | 51 +++++++++++++++++++------------ metagpt/provider/anthropic_api.py | 4 +-- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 8fd208c59..9a7207c1a 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -20,7 +20,7 @@ RPM: 10 #SPARK_URL : "ws://spark-api.xf-yun.com/v2.1/chat" #### if Anthropic -#Anthropic_API_KEY: "YOUR_API_KEY" +#ANTHROPIC_API_KEY: "YOUR_API_KEY" #### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb #### You can use ENGINE or DEPLOYMENT mode diff --git a/metagpt/config.py b/metagpt/config.py index 629a5b797..702a2ddc9 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -46,30 +46,41 @@ class Config(metaclass=Singleton): def __init__(self, yaml_file=default_yaml_file): self._init_with_config_files_and_env(yaml_file) - logger.debug("Config loading done.") self._update() + logger.debug("Config loading done.") logger.info(f"OpenAI API Model: {self.openai_api_model}") + @staticmethod + def _is_valid_llm_key(k) -> bool: + return k and k != "YOUR_API_KEY" + + def _check_llm_exists(self): + if not any( + [ + self._is_valid_llm_key(self.openai_api_key), + self._is_valid_llm_key(self.anthropic_api_key), + self._is_valid_llm_key(self.zhipuai_api_key), + self._is_valid_llm_key(self.fireworks_api_key), + self.open_llm_api_base, + ] + ): + raise NotConfiguredException( + "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY " + "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" + ) + def _update(self): # logger.info("Config loading done.") self.global_proxy = self._get("GLOBAL_PROXY") + self.openai_api_key = self._get("OPENAI_API_KEY") - self.anthropic_api_key = self._get("Anthropic_API_KEY") + self.anthropic_api_key = self._get("ANTHROPIC_API_KEY") self.zhipuai_api_key = self._get("ZHIPUAI_API_KEY") self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") self.fireworks_api_key = self._get("FIREWORKS_API_KEY") - if ( - (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) - and (not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key) - and (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key) - and (not self.open_llm_api_base) - and (not self.fireworks_api_key or "YOUR_API_KEY" == self.fireworks_api_key) - ): - raise NotConfiguredException( - "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first " - "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" - ) + self._check_llm_exists() + self.openai_api_base = self._get("OPENAI_API_BASE") self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy self.openai_api_type = self._get("OPENAI_API_TYPE") @@ -89,7 +100,7 @@ class Config(metaclass=Singleton): self.fireworks_api_base = self._get("FIREWORKS_API_BASE") self.fireworks_api_model = self._get("FIREWORKS_API_MODEL") - self.claude_api_key = self._get("Anthropic_API_KEY") + self.claude_api_key = self._get("ANTHROPIC_API_KEY") self.serpapi_api_key = self._get("SERPAPI_API_KEY") self.serper_api_key = self._get("SERPER_API_KEY") self.google_api_key = self._get("GOOGLE_API_KEY") @@ -141,8 +152,8 @@ class Config(metaclass=Singleton): @staticmethod def _get(*args, **kwargs): - m = OPTIONS.get() - return m.get(*args, **kwargs) + i = OPTIONS.get() + return i.get(*args, **kwargs) def get(self, key, *args, **kwargs): """Search for a value in config/key.yaml, config/config.yaml, and env; raise an error if not found""" @@ -155,8 +166,8 @@ class Config(metaclass=Singleton): OPTIONS.get()[name] = value def __getattr__(self, name: str) -> Any: - m = OPTIONS.get() - return m.get(name) + i = OPTIONS.get() + return i.get(name) def set_context(self, options: dict): """Update current config""" @@ -175,8 +186,8 @@ class Config(metaclass=Singleton): def new_environ(self): """Return a new os.environ object""" env = os.environ.copy() - m = self.options - env.update({k: v for k, v in m.items() if isinstance(v, str)}) + i = self.options + env.update({k: v for k, v in i.items() if isinstance(v, str)}) return env diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py index 03802a716..f5b06c855 100644 --- a/metagpt/provider/anthropic_api.py +++ b/metagpt/provider/anthropic_api.py @@ -14,7 +14,7 @@ from metagpt.config import CONFIG class Claude2: def ask(self, prompt): - client = Anthropic(api_key=CONFIG.claude_api_key) + client = Anthropic(api_key=CONFIG.anthropic_api_key) res = client.completions.create( model="claude-2", @@ -24,7 +24,7 @@ class Claude2: return res.completion async def aask(self, prompt): - client = Anthropic(api_key=CONFIG.claude_api_key) + client = Anthropic(api_key=CONFIG.anthropic_api_key) res = client.completions.create( model="claude-2", From 7f04ec2060da2ccdc3ca72a4d5e7e60377958b7d Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:06:07 +0800 Subject: [PATCH 321/413] refine code --- metagpt/config.py | 8 ++++++++ metagpt/repo_parser.py | 2 +- metagpt/startup.py | 9 +++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 702a2ddc9..48ac82a3a 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -130,6 +130,14 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() + def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): + """update config via cli""" + self.project_path = project_path + self.project_name = project_name + self.inc = inc + self.reqa_file = reqa_file + self.max_auto_summarize_code = max_auto_summarize_code + def _ensure_workspace_exists(self): self.workspace_path.mkdir(parents=True, exist_ok=True) logger.debug(f"WORKSPACE_PATH set to {self.workspace_path}") diff --git a/metagpt/repo_parser.py b/metagpt/repo_parser.py index 9a1218ef1..3524a5bce 100644 --- a/metagpt/repo_parser.py +++ b/metagpt/repo_parser.py @@ -96,4 +96,4 @@ def error(): if __name__ == "__main__": - error() + main() diff --git a/metagpt/startup.py b/metagpt/startup.py index f930c386b..047f35cf6 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -27,7 +27,8 @@ def startup( reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), max_auto_summarize_code: int = typer.Option( default=-1, - help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. This parameter is used for debugging the workflow.", + help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " + "unlimited. This parameter is used for debugging the workflow.", ), ): """Run a startup. Be a boss.""" @@ -41,14 +42,10 @@ def startup( from metagpt.team import Team # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. - CONFIG.project_path = project_path if project_path: inc = True project_name = project_name or Path(project_path).name - CONFIG.project_name = project_name - CONFIG.inc = inc - CONFIG.reqa_file = reqa_file - CONFIG.max_auto_summarize_code = max_auto_summarize_code + CONFIG.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code) company = Team() company.hire( From 2bae7f2bfb116d9deeab3e6d6237da0a12bdd2be Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:11:02 +0800 Subject: [PATCH 322/413] refine code --- metagpt/config.py | 13 +++++++++++++ metagpt/startup.py | 5 ----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 48ac82a3a..bdf580a1f 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -45,6 +45,7 @@ class Config(metaclass=Singleton): default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): + self._init_cli_paras() self._init_with_config_files_and_env(yaml_file) self._update() logger.debug("Config loading done.") @@ -130,8 +131,20 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() + def _init_cli_paras(self): + self.project_path = None + self.project_name = None + self.inc = None + self.reqa_file = None + self.max_auto_summarize_code = None + def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): """update config via cli""" + + # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. + if project_path: + inc = True + project_name = project_name or Path(project_path).name self.project_path = project_path self.project_name = project_name self.inc = inc diff --git a/metagpt/startup.py b/metagpt/startup.py index 047f35cf6..37526dbcc 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio -from pathlib import Path import typer @@ -41,10 +40,6 @@ def startup( ) from metagpt.team import Team - # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. - if project_path: - inc = True - project_name = project_name or Path(project_path).name CONFIG.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code) company = Team() From 1213c5f88fe2ab257681d7f383e311c6bcbff925 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:14:50 +0800 Subject: [PATCH 323/413] fix comment --- metagpt/team.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/team.py b/metagpt/team.py index a5c405f80..ddd145269 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -21,8 +21,8 @@ from metagpt.utils.common import NoMoneyException class Team(BaseModel): """ - Team: Possesses one or more roles (agents), SOP (Standard Operating Procedures), and a platform for instant messaging, - dedicated to perform any multi-agent activity, such as collaboratively writing executable code. + Team: Possesses one or more roles (agents), SOP (Standard Operating Procedures), and a env for instant messaging, + dedicated to env any multi-agent activity, such as collaboratively writing executable code. """ env: Environment = Field(default_factory=Environment) From f27461f7582ec1143f43718ae79373187e0c7684 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:55:34 +0800 Subject: [PATCH 324/413] add llm provider registry --- metagpt/config.py | 57 +++++++++++++---------- metagpt/llm.py | 21 +-------- metagpt/provider/fireworks_api.py | 4 +- metagpt/provider/llm_provider_registry.py | 34 ++++++++++++++ metagpt/provider/open_llm_api.py | 4 +- metagpt/provider/openai_api.py | 4 +- metagpt/provider/spark_api.py | 4 +- metagpt/provider/zhipuai_api.py | 4 +- metagpt/schema.py | 10 ++-- 9 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 metagpt/provider/llm_provider_registry.py diff --git a/metagpt/config.py b/metagpt/config.py index bdf580a1f..a0d61b39f 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -8,6 +8,7 @@ Provide configuration, singleton """ import os from copy import deepcopy +from enum import Enum from pathlib import Path from typing import Any @@ -31,6 +32,15 @@ class NotConfiguredException(Exception): super().__init__(self.message) +class LLMProviderEnum(Enum): + OPENAI = "openai" + ANTHROPIC = "anthropic" + SPARK = "spark" + ZHIPUAI = "zhipuai" + FIREWORKS = "fireworks" + OPEN_LLM = "open_llm" + + class Config(metaclass=Singleton): """ Regular usage method: @@ -45,31 +55,37 @@ class Config(metaclass=Singleton): default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): - self._init_cli_paras() + # cli paras + self.project_path = "" + self.project_name = "" + self.inc = False + self.reqa_file = "" + self.max_auto_summarize_code = 0 + self._init_with_config_files_and_env(yaml_file) self._update() logger.debug("Config loading done.") logger.info(f"OpenAI API Model: {self.openai_api_model}") + def get_default_llm_provider_enum(self): + if self._is_valid_llm_key(self.openai_api_key): + llm = LLMProviderEnum.OPENAI + elif self._is_valid_llm_key(self.anthropic_api_key): + llm = LLMProviderEnum.ANTHROPIC + elif self._is_valid_llm_key(self.zhipuai_api_key): + llm = LLMProviderEnum.ZHIPUAI + elif self._is_valid_llm_key(self.fireworks_api_key): + llm = LLMProviderEnum.FIREWORKS + elif self.open_llm_api_base: + llm = LLMProviderEnum.OPEN_LLM + else: + raise NotConfiguredException("You should config a LLM configuration first") + return llm + @staticmethod def _is_valid_llm_key(k) -> bool: return k and k != "YOUR_API_KEY" - def _check_llm_exists(self): - if not any( - [ - self._is_valid_llm_key(self.openai_api_key), - self._is_valid_llm_key(self.anthropic_api_key), - self._is_valid_llm_key(self.zhipuai_api_key), - self._is_valid_llm_key(self.fireworks_api_key), - self.open_llm_api_base, - ] - ): - raise NotConfiguredException( - "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY " - "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" - ) - def _update(self): # logger.info("Config loading done.") self.global_proxy = self._get("GLOBAL_PROXY") @@ -80,7 +96,7 @@ class Config(metaclass=Singleton): self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") self.fireworks_api_key = self._get("FIREWORKS_API_KEY") - self._check_llm_exists() + _ = self.get_default_llm_provider_enum() self.openai_api_base = self._get("OPENAI_API_BASE") self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy @@ -131,13 +147,6 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() - def _init_cli_paras(self): - self.project_path = None - self.project_name = None - self.inc = None - self.reqa_file = None - self.max_auto_summarize_code = None - def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): """update config via cli""" diff --git a/metagpt/llm.py b/metagpt/llm.py index 7c0ad7975..e0c0716de 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -8,12 +8,8 @@ from metagpt.config import CONFIG from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.provider.fireworks_api import FireWorksGPTAPI from metagpt.provider.human_provider import HumanProvider -from metagpt.provider.open_llm_api import OpenLLMGPTAPI -from metagpt.provider.openai_api import OpenAIGPTAPI -from metagpt.provider.spark_api import SparkAPI -from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI +from metagpt.provider.llm_provider_registry import LLMProviderRegistry _ = HumanProvider() # Avoid pre-commit error @@ -21,17 +17,4 @@ _ = HumanProvider() # Avoid pre-commit error def LLM() -> BaseGPTAPI: """initialize different LLM instance according to the key field existence""" # TODO a little trick, can use registry to initialize LLM instance further - if CONFIG.openai_api_key: - llm = OpenAIGPTAPI() - elif CONFIG.spark_api_key: - llm = SparkAPI() - elif CONFIG.zhipuai_api_key: - llm = ZhiPuAIGPTAPI() - elif CONFIG.open_llm_api_base: - llm = OpenLLMGPTAPI() - elif CONFIG.fireworks_api_key: - llm = FireWorksGPTAPI() - else: - raise RuntimeError("You should config a LLM configuration first") - - return llm + return LLMProviderRegistry.get_provider(CONFIG.get_default_llm_provider_enum()) diff --git a/metagpt/provider/fireworks_api.py b/metagpt/provider/fireworks_api.py index 47ac9cf61..a76151666 100644 --- a/metagpt/provider/fireworks_api.py +++ b/metagpt/provider/fireworks_api.py @@ -4,10 +4,12 @@ import openai -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter +@register_provider(LLMProviderEnum.FIREWORKS) class FireWorksGPTAPI(OpenAIGPTAPI): def __init__(self): self.__init_fireworks(CONFIG) diff --git a/metagpt/provider/llm_provider_registry.py b/metagpt/provider/llm_provider_registry.py new file mode 100644 index 000000000..2b3ef93a3 --- /dev/null +++ b/metagpt/provider/llm_provider_registry.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/19 17:26 +@Author : alexanderwu +@File : llm_provider_registry.py +""" +from metagpt.config import LLMProviderEnum + + +class LLMProviderRegistry: + def __init__(self): + self.providers = {} + + def register(self, key, provider_cls): + self.providers[key] = provider_cls + + def get_provider(self, enum: LLMProviderEnum): + """get provider instance according to the enum""" + return self.providers[enum]() + + +# Registry instance +LLM_REGISTRY = LLMProviderRegistry() + + +def register_provider(key): + """register provider to registry""" + + def decorator(cls): + LLM_REGISTRY.register(key, cls) + return cls + + return decorator diff --git a/metagpt/provider/open_llm_api.py b/metagpt/provider/open_llm_api.py index f421e30c8..bada0e294 100644 --- a/metagpt/provider/open_llm_api.py +++ b/metagpt/provider/open_llm_api.py @@ -4,8 +4,9 @@ import openai -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter @@ -31,6 +32,7 @@ class OpenLLMCostManager(CostManager): CONFIG.total_cost = self.total_cost +@register_provider(LLMProviderEnum.OPEN_LLM) class OpenLLMGPTAPI(OpenAIGPTAPI): def __init__(self): self.__init_openllm(CONFIG) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 86054881e..0be70b3ca 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -18,10 +18,11 @@ from tenacity import ( wait_random_exponential, ) -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMA, GENERAL_TOOL_CHOICE +from metagpt.provider.llm_provider_registry import register_provider from metagpt.schema import Message from metagpt.utils.singleton import Singleton from metagpt.utils.token_counter import ( @@ -137,6 +138,7 @@ See FAQ 5.8 raise retry_state.outcome.exception() +@register_provider(LLMProviderEnum.OPENAI) class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): """ Check https://platform.openai.com/examples for examples diff --git a/metagpt/provider/spark_api.py b/metagpt/provider/spark_api.py index 60c86f4dc..484fa7956 100644 --- a/metagpt/provider/spark_api.py +++ b/metagpt/provider/spark_api.py @@ -19,11 +19,13 @@ from wsgiref.handlers import format_date_time import websocket # 使用websocket_client -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.llm_provider_registry import register_provider +@register_provider(LLMProviderEnum.SPARK) class SparkAPI(BaseGPTAPI): def __init__(self): logger.warning("当前方法无法支持异步运行。当你使用acompletion时,并不能并行访问。") diff --git a/metagpt/provider/zhipuai_api.py b/metagpt/provider/zhipuai_api.py index 92119b764..eef0e51e1 100644 --- a/metagpt/provider/zhipuai_api.py +++ b/metagpt/provider/zhipuai_api.py @@ -16,9 +16,10 @@ from tenacity import ( wait_random_exponential, ) -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, log_and_reraise from metagpt.provider.zhipuai.zhipu_model_api import ZhiPuModelAPI @@ -30,6 +31,7 @@ class ZhiPuEvent(Enum): FINISH = "finish" +@register_provider(LLMProviderEnum.ZHIPUAI) class ZhiPuAIGPTAPI(BaseGPTAPI): """ Refs to `https://open.bigmodel.cn/dev/api#chatglm_turbo` diff --git a/metagpt/schema.py b/metagpt/schema.py index b24f114b0..aacc2cebb 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -164,8 +164,8 @@ class Message(BaseModel): @handle_exception(exception_type=JSONDecodeError, default_return=None) def load(val): """Convert the json string to object.""" - d = json.loads(val) - return Message(**d) + i = json.loads(val) + return Message(**i) class UserMessage(Message): @@ -247,16 +247,16 @@ class MessageQueue: return json.dumps(lst) @staticmethod - def load(i) -> "MessageQueue": + def load(data) -> "MessageQueue": """Convert the json string to the `MessageQueue` object.""" queue = MessageQueue() try: - lst = json.loads(i) + lst = json.loads(data) for i in lst: msg = Message(**i) queue.push(msg) except JSONDecodeError as e: - logger.warning(f"JSON load failed: {i}, error:{e}") + logger.warning(f"JSON load failed: {data}, error:{e}") return queue From 25b8a6dcef768ed1e45489e2dd3a5462f37fd593 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:02:51 +0800 Subject: [PATCH 325/413] make registry work --- metagpt/llm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index e0c0716de..60f110a00 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -9,7 +9,7 @@ from metagpt.config import CONFIG from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.human_provider import HumanProvider -from metagpt.provider.llm_provider_registry import LLMProviderRegistry +from metagpt.provider.llm_provider_registry import LLM_REGISTRY _ = HumanProvider() # Avoid pre-commit error @@ -17,4 +17,4 @@ _ = HumanProvider() # Avoid pre-commit error def LLM() -> BaseGPTAPI: """initialize different LLM instance according to the key field existence""" # TODO a little trick, can use registry to initialize LLM instance further - return LLMProviderRegistry.get_provider(CONFIG.get_default_llm_provider_enum()) + return LLM_REGISTRY.get_provider(CONFIG.get_default_llm_provider_enum()) From 77735d6e612422911dedd86c40aebb2b7c69dcb3 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:04:12 +0800 Subject: [PATCH 326/413] make registry work --- metagpt/llm.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index 60f110a00..8763642f0 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -6,7 +6,7 @@ @File : llm.py """ -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.human_provider import HumanProvider from metagpt.provider.llm_provider_registry import LLM_REGISTRY @@ -14,7 +14,6 @@ from metagpt.provider.llm_provider_registry import LLM_REGISTRY _ = HumanProvider() # Avoid pre-commit error -def LLM() -> BaseGPTAPI: - """initialize different LLM instance according to the key field existence""" - # TODO a little trick, can use registry to initialize LLM instance further - return LLM_REGISTRY.get_provider(CONFIG.get_default_llm_provider_enum()) +def LLM(provider: LLMProviderEnum = CONFIG.get_default_llm_provider_enum()) -> BaseGPTAPI: + """get the default llm provider""" + return LLM_REGISTRY.get_provider(provider) From 3baf47a3d64ebf9278ec5bee5e6ec524fdf9f666 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:50:55 +0800 Subject: [PATCH 327/413] refine code for isinstance --- metagpt/actions/write_prd.py | 2 +- metagpt/roles/role.py | 2 +- metagpt/roles/searcher.py | 2 +- metagpt/utils/common.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index bb0cf8fb9..adba7decb 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -182,7 +182,7 @@ class WritePRD(Action): return if not CONFIG.project_name: - if isinstance(prd, ActionOutput) or isinstance(prd, ActionNode): + if isinstance(prd, (ActionOutput, ActionNode)): ws_name = prd.instruct_content.dict()["Project Name"] else: ws_name = CodeParser.parse_str(block="Project Name", text=prd) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 48688ad5f..e13bf454b 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -267,7 +267,7 @@ class Role: async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index 5760202ff..31de8e896 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -59,7 +59,7 @@ class Searcher(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index bf435b74f..fa18694e3 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -197,7 +197,7 @@ class OutputParser: result = ast.literal_eval(structure_text) # Ensure the result matches the specified data type - if isinstance(result, list) or isinstance(result, dict): + if isinstance(result, (list, dict)): return result raise ValueError(f"The extracted structure is not a {data_type}.") From 5aa4ef5d836771b3335ded771626e44dfce74c2c Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:54:04 +0800 Subject: [PATCH 328/413] fix typo --- metagpt/config.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index d4e85ca7b..766024222 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -55,8 +55,7 @@ class Config(metaclass=Singleton): default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): - - golbal_options = OPTIONS.get() + global_options = OPTIONS.get() # cli paras self.project_path = "" self.project_name = "" @@ -66,7 +65,7 @@ class Config(metaclass=Singleton): self._init_with_config_files_and_env(yaml_file) self._update() - golbal_options.update(OPTIONS.get()) + global_options.update(OPTIONS.get()) logger.debug("Config loading done.") logger.info(f"OpenAI API Model: {self.openai_api_model}") From 9d1b628bce1de85b401bbb781c75707f7774dfba Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:00:20 +0800 Subject: [PATCH 329/413] refine cli --- metagpt/startup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index a89b9c5e9..d6f3397bc 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -6,7 +6,7 @@ import typer from metagpt.config import CONFIG -app = typer.Typer() +app = typer.Typer(add_completion=False) @app.command() @@ -23,7 +23,9 @@ def startup( default="", help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", ), - reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), + reqa_file: str = typer.Option( + default="", help="Specify the source file name for rewriting the quality assurance " "code." + ), max_auto_summarize_code: int = typer.Option( default=0, help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " From 505133cacc587c5894f10bed149d774c41b857e2 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:00:39 +0800 Subject: [PATCH 330/413] refine cli --- metagpt/startup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index d6f3397bc..a1af90ffc 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -21,10 +21,10 @@ def startup( inc: bool = typer.Option(default=False, help="Incremental mode. Use it to coop with existing repo."), project_path: str = typer.Option( default="", - help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", + help="Specify the directory path of the old version project to fulfill the incremental requirements.", ), reqa_file: str = typer.Option( - default="", help="Specify the source file name for rewriting the quality assurance " "code." + default="", help="Specify the source file name for rewriting the quality assurance code." ), max_auto_summarize_code: int = typer.Option( default=0, From 6dfa4e2c9e44d8db8e8e1c67646ae88d4547c968 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:15:30 +0800 Subject: [PATCH 331/413] fix pylint --- examples/agent_creator.py | 9 ++++----- metagpt/memory/longterm_memory.py | 10 +++++----- metagpt/memory/memory_storage.py | 2 +- metagpt/roles/product_manager.py | 2 +- metagpt/roles/qa_engineer.py | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 05417d24a..26af8a287 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -12,9 +12,8 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -with open(METAGPT_ROOT / "examples/build_customized_agent.py", "r") as f: - # use official example script to guide AgentCreator - MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read() +EXAMPLE_CODE_FILE = METAGPT_ROOT / "examples/build_customized_agent.py" +MULTI_ACTION_AGENT_CODE_EXAMPLE = EXAMPLE_CODE_FILE.read_text() class CreateAgent(Action): @@ -50,8 +49,8 @@ class CreateAgent(Action): match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" CONFIG.workspace_path.mkdir(parents=True, exist_ok=True) - with open(CONFIG.workspace_path / "agent_created_agent.py", "w") as f: - f.write(code_text) + new_file = CONFIG.workspace_path / "agent_created_agent.py" + new_file.write_text(code_text) return code_text diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index 22032a86e..ab2214261 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -19,7 +19,7 @@ class LongTermMemory(Memory): def __init__(self): self.memory_storage: MemoryStorage = MemoryStorage() - super(LongTermMemory, self).__init__() + super().__init__() self.rc = None # RoleContext self.msg_from_recover = False @@ -37,7 +37,7 @@ class LongTermMemory(Memory): self.msg_from_recover = False def add(self, message: Message): - super(LongTermMemory, self).add(message) + super().add(message) for action in self.rc.watch: if message.cause_by == action and not self.msg_from_recover: # currently, only add role's watching messages to its memory_storage @@ -50,7 +50,7 @@ class LongTermMemory(Memory): 1. find the short-term memory(stm) news 2. furthermore, filter out similar messages based on ltm(long-term memory), get the final news """ - stm_news = super(LongTermMemory, self).find_news(observed, k=k) # shot-term memory news + stm_news = super().find_news(observed, k=k) # shot-term memory news if not self.memory_storage.is_initialized: # memory_storage hasn't initialized, use default `find_news` to get stm_news return stm_news @@ -64,9 +64,9 @@ class LongTermMemory(Memory): return ltm_news[-k:] def delete(self, message: Message): - super(LongTermMemory, self).delete(message) + super().delete(message) # TODO delete message in memory_storage def clear(self): - super(LongTermMemory, self).clear() + super().clear() self.memory_storage.clean() diff --git a/metagpt/memory/memory_storage.py b/metagpt/memory/memory_storage.py index a213f6d7a..fafb33568 100644 --- a/metagpt/memory/memory_storage.py +++ b/metagpt/memory/memory_storage.py @@ -58,7 +58,7 @@ class MemoryStorage(FaissStore): return index_fpath, storage_fpath def persist(self): - super(MemoryStorage, self).persist() + super().persist() logger.debug(f"Agent {self.role_id} persist memory into local") def add(self, message: Message) -> bool: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index e5e9f2b5e..7858d2caa 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -54,4 +54,4 @@ class ProductManager(Role): return self._rc.todo async def _observe(self, ignore_memory=False) -> int: - return await super(ProductManager, self)._observe(ignore_memory=True) + return await super()._observe(ignore_memory=True) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 4439b9b19..71b474a3b 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -178,4 +178,4 @@ class QaEngineer(Role): async def _observe(self, ignore_memory=False) -> int: # This role has events that trigger and execute themselves based on conditions, and cannot rely on the # content of memory to activate. - return await super(QaEngineer, self)._observe(ignore_memory=True) + return await super()._observe(ignore_memory=True) From c12cd7b9c6bd2d900fbd70072cd9731b86486e1b Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:25:01 +0800 Subject: [PATCH 332/413] refine code --- metagpt/config.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 766024222..80a3a28f4 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -67,25 +67,23 @@ class Config(metaclass=Singleton): self._update() global_options.update(OPTIONS.get()) logger.debug("Config loading done.") - logger.info(f"OpenAI API Model: {self.openai_api_model}") - def get_default_llm_provider_enum(self): - if self._is_valid_llm_key(self.openai_api_key): - llm = LLMProviderEnum.OPENAI - elif self._is_valid_llm_key(self.anthropic_api_key): - llm = LLMProviderEnum.ANTHROPIC - elif self._is_valid_llm_key(self.zhipuai_api_key): - llm = LLMProviderEnum.ZHIPUAI - elif self._is_valid_llm_key(self.fireworks_api_key): - llm = LLMProviderEnum.FIREWORKS - elif self.open_llm_api_base: - llm = LLMProviderEnum.OPEN_LLM - else: - raise NotConfiguredException("You should config a LLM configuration first") - return llm + def get_default_llm_provider_enum(self) -> LLMProviderEnum: + for k, v in [ + (self.openai_api_key, LLMProviderEnum.OPENAI), + (self.anthropic_api_key, LLMProviderEnum.ANTHROPIC), + (self.zhipuai_api_key, LLMProviderEnum.ZHIPUAI), + (self.fireworks_api_key, LLMProviderEnum.FIREWORKS), + (self.open_llm_api_base, LLMProviderEnum.OPEN_LLM), # reuse logic. but not a key + ]: + if self._is_valid_llm_key(k): + if self.openai_api_model: + logger.info(f"OpenAI API Model: {self.openai_api_model}") + return v + raise NotConfiguredException("You should config a LLM configuration first") @staticmethod - def _is_valid_llm_key(k) -> bool: + def _is_valid_llm_key(k: str) -> bool: return k and k != "YOUR_API_KEY" def _update(self): From edb90690263b5b0aa91ecdf61e94476e6ff613c4 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:26:01 +0800 Subject: [PATCH 333/413] delete manager.py --- metagpt/manager.py | 66 ---------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 metagpt/manager.py diff --git a/metagpt/manager.py b/metagpt/manager.py deleted file mode 100644 index a063608be..000000000 --- a/metagpt/manager.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/11 14:42 -@Author : alexanderwu -@File : manager.py -""" -from metagpt.llm import LLM -from metagpt.logs import logger -from metagpt.schema import Message - - -class Manager: - def __init__(self, llm: LLM = LLM()): - self.llm = llm # Large Language Model - self.role_directions = { - "User": "Product Manager", - "Product Manager": "Architect", - "Architect": "Engineer", - "Engineer": "QA Engineer", - "QA Engineer": "Product Manager", - } - self.prompt_template = """ - Given the following message: - {message} - - And the current status of roles: - {roles} - - Which role should handle this message? - """ - - async def handle(self, message: Message, environment): - """ - 管理员处理信息,现在简单的将信息递交给下一个人 - The administrator processes the information, now simply passes the information on to the next person - :param message: - :param environment: - :return: - """ - # Get all roles from the environment - roles = environment.get_roles() - # logger.debug(f"{roles=}, {message=}") - - # Build a context for the LLM to understand the situation - # context = { - # "message": str(message), - # "roles": {role.name: role.get_info() for role in roles}, - # } - # Ask the LLM to decide which role should handle the message - # chosen_role_name = self.llm.ask(self.prompt_template.format(context)) - - # FIXME: 现在通过简单的字典决定流向,但之后还是应该有思考过程 - # The direction of flow is now determined by a simple dictionary, but there should still be a thought process afterwards - next_role_profile = self.role_directions[message.role] - # logger.debug(f"{next_role_profile}") - for _, role in roles.items(): - if next_role_profile == role.profile: - next_role = role - break - else: - logger.error(f"No available role can handle message: {message}.") - return - - # Find the chosen role and handle the message - return await next_role.handle(message) From 8a1237460eb1afd77be3d8db6d61adbcdcf271a2 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:27:11 +0800 Subject: [PATCH 334/413] remove useless fields --- metagpt/actions/action.py | 12 +----------- metagpt/actions/search_and_summarize.py | 3 +-- metagpt/roles/role.py | 2 +- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 7bb26ea91..1292b6684 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -26,22 +26,12 @@ class Action(ABC): self.llm = llm self.context = context self.prefix = "" # aask*时会加上prefix,作为system_message - self.profile = "" # FIXME: USELESS self.desc = "" # for skill manager self.nodes = ... - # Output, useless - # self.content = "" - # self.instruct_content = None - # self.env = None - - # def set_env(self, env): - # self.env = env - - def set_prefix(self, prefix, profile): + def set_prefix(self, prefix): """Set prefix for later usage""" self.prefix = prefix - self.profile = profile return self def __str__(self): diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 5e4cdaea0..a1d81bc65 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -130,8 +130,7 @@ class SearchAndSummarize(Action): system_prompt = [system_text] prompt = SEARCH_AND_SUMMARIZE_PROMPT.format( - # PREFIX = self.prefix, - ROLE=self.profile, + ROLE=self.prefix, CONTEXT=rsp, QUERY_HISTORY="\n".join([str(i) for i in context[:-1]]), QUERY=str(context[-1]), diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index e13bf454b..bf37a6637 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -146,7 +146,7 @@ class Role: self._actions = [] def _init_action_system_message(self, action: Action): - action.set_prefix(self._get_prefix(), self.profile) + action.set_prefix(self._get_prefix()) def _init_actions(self, actions): self._reset() From f0fd5ac59bd8be8e0083aa89a5d38d7cf3c3d639 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:17:02 +0800 Subject: [PATCH 335/413] refine a lot of code, fix pylint, use actionnode include ui, action _aask_v1, detail_mining, prepare_interview, etc. --- metagpt/actions/action.py | 48 +++----- metagpt/actions/action_node.py | 81 +++++--------- metagpt/actions/design_api.py | 10 +- metagpt/actions/detail_mining.py | 50 +++------ metagpt/actions/prepare_interview.py | 35 ++---- metagpt/actions/project_management.py | 10 +- metagpt/actions/write_prd.py | 8 +- metagpt/config.py | 2 +- metagpt/utils/get_template.py | 6 +- tests/metagpt/actions/test_detail_mining.py | 4 +- .../metagpt/actions/test_prepare_interview.py | 21 ++++ tests/metagpt/roles/ui_role.py | 104 +++++++++--------- 12 files changed, 163 insertions(+), 216 deletions(-) create mode 100644 tests/metagpt/actions/test_prepare_interview.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1292b6684..5c5884e8b 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -6,19 +6,26 @@ @File : action.py """ +from __future__ import annotations + from abc import ABC from typing import Optional -from tenacity import retry, stop_after_attempt, wait_random_exponential - -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM -from metagpt.logs import logger -from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess -from metagpt.utils.common import OutputParser, general_after_log +from metagpt.schema import BaseContext class Action(ABC): + """Action abstract class, requiring all inheritors to provide a series of standard capabilities""" + + name: str + llm: LLM + context: dict | BaseContext | str | None + prefix: str + desc: str + node: ActionNode | None + def __init__(self, name: str = "", context=None, llm: LLM = None): self.name: str = name if llm is None: @@ -27,7 +34,7 @@ class Action(ABC): self.context = context self.prefix = "" # aask*时会加上prefix,作为system_message self.desc = "" # for skill manager - self.nodes = ... + self.node = None def set_prefix(self, prefix): """Set prefix for later usage""" @@ -47,33 +54,6 @@ class Action(ABC): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry( - wait=wait_random_exponential(min=1, max=60), - stop=stop_after_attempt(6), - after=general_after_log(logger), - ) - async def _aask_v1( - self, - prompt: str, - output_class_name: str, - output_data_mapping: dict, - system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format - ) -> ActionOutput: - content = await self.llm.aask(prompt, system_msgs) - logger.debug(f"llm raw output:\n{content}") - output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - - if format == "json": - parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") - - else: # using markdown parser - parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - - logger.debug(f"parsed_data:\n{parsed_data}") - instruct_content = output_class(**parsed_data) - return ActionOutput(content, instruct_content) - async def run(self, *args, **kwargs): """Run action""" raise NotImplementedError("The run method should be implemented in a subclass.") diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 6f1215920..0368d2df1 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -6,17 +6,15 @@ @File : action_node.py """ import json -import re -from typing import Any, Dict, List, Optional, Type +from typing import Dict, Generic, List, Optional, Type, TypeVar from pydantic import BaseModel, create_model, root_validator, validator from tenacity import retry, stop_after_attempt, wait_random_exponential -from metagpt.actions import ActionOutput from metagpt.llm import BaseGPTAPI from metagpt.logs import logger -from metagpt.utils.common import OutputParser -from metagpt.utils.custom_decoder import CustomDecoder +from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess +from metagpt.utils.common import OutputParser, general_after_log CONSTRAINT = """ - Language: Please use the same language as the user input. @@ -43,14 +41,17 @@ Fill in the above nodes based on the format example. """ -def dict_to_markdown(d, prefix="###", postfix="\n"): +def dict_to_markdown(d, prefix="-", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" return markdown_str -class ActionNode: +T = TypeVar("T") + + +class ActionNode(Generic[T]): """ActionNode is a tree of nodes.""" mode: str @@ -65,7 +66,7 @@ class ActionNode: expected_type: Type # such as str / int / float etc. # context: str # everything in the history. instruction: str # the instructions should be followed. - example: Any # example for In Context-Learning. + example: T # example for In Context-Learning. # Action Output content: str @@ -76,7 +77,7 @@ class ActionNode: key: str, expected_type: Type, instruction: str, - example: str, + example: T, content: str = "", children: dict[str, "ActionNode"] = None, ): @@ -148,29 +149,6 @@ class ActionNode: new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) return new_class - @classmethod - def create_model_class_v2(cls, class_name: str, mapping: Dict[str, Type]): - """基于pydantic v2的模型动态生成,用来检验结果类型正确性,待验证""" - new_class = create_model(class_name, **mapping) - - @model_validator(mode="before") - def check_missing_fields(data): - required_fields = set(mapping.keys()) - missing_fields = required_fields - set(data.keys()) - if missing_fields: - raise ValueError(f"Missing fields: {missing_fields}") - return data - - @field_validator("*") - def check_name(v: Any, field: str) -> Any: - if field not in mapping.keys(): - raise ValueError(f"Unrecognized block: {field}") - return v - - new_class.__model_validator_check_missing_fields = classmethod(check_missing_fields) - new_class.__field_validator_check_name = classmethod(check_name) - return new_class - def create_children_class(self): """使用object内有的字段直接生成model_class""" class_name = f"{self.key}_AN" @@ -245,6 +223,7 @@ class ActionNode: """ # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", + # compile example暂时不支持markdown self.instruction = self.compile_instruction(to="markdown", mode=mode) self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) prompt = template.format( @@ -252,36 +231,32 @@ class ActionNode: ) return prompt - @retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(6)) + @retry( + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), + after=general_after_log(logger), + ) async def _aask_v1( self, prompt: str, output_class_name: str, output_data_mapping: dict, system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format - ) -> ActionOutput: + schema="markdown", # compatible to original format + ) -> (str, BaseModel): + """Use ActionOutput to wrap the output of aask""" content = await self.llm.aask(prompt, system_msgs) - logger.debug(content) - output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - - if format == "json": - pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" - matches = re.findall(pattern, content, re.DOTALL) - - for match in matches: - if match: - content = match - break - - parsed_data = CustomDecoder(strict=False).decode(content) + logger.debug(f"llm raw output:\n{content}") + output_class = self.create_model_class(output_class_name, output_data_mapping) + if schema == "json": + parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - logger.debug(parsed_data) + logger.debug(f"parsed_data:\n{parsed_data}") instruct_content = output_class(**parsed_data) - return ActionOutput(content, instruct_content) + return content, instruct_content def get(self, key): return self.instruct_content.dict()[key] @@ -302,9 +277,9 @@ class ActionNode: mapping = self.get_mapping(mode) class_name = f"{self.key}_AN" - output = await self._aask_v1(prompt, class_name, mapping, format=to) - self.content = output.content - self.instruct_content = output.instruct_content + content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=to) + self.content = content + self.instruct_content = scontent return self async def fill(self, context, llm, to="json", mode="auto", strgy="simple"): diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 5a5f52de7..f757ca856 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -50,7 +50,7 @@ class WriteDesign(Action): "clearly and in detail." ) - async def run(self, with_messages, format=CONFIG.prompt_format): + async def run(self, with_messages, schema=CONFIG.prompt_schema): # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) changed_prds = prds_file_repo.changed_files @@ -80,13 +80,13 @@ class WriteDesign(Action): # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - async def _new_system_design(self, context, format=CONFIG.prompt_format): - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + async def _new_system_design(self, context, schema=CONFIG.prompt_schema): + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) return node - async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): + async def _merge(self, prd_doc, system_design_doc, schema=CONFIG.prompt_schema): context = NEW_REQ_TEMPLATE.format(old_design=system_design_doc.content, context=prd_doc.content) - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) system_design_doc.content = node.instruct_content.json(ensure_ascii=False) return system_design_doc diff --git a/metagpt/actions/detail_mining.py b/metagpt/actions/detail_mining.py index 5afcf52c6..0314d30dd 100644 --- a/metagpt/actions/detail_mining.py +++ b/metagpt/actions/detail_mining.py @@ -5,47 +5,31 @@ @Author : fisherdeng @File : detail_mining.py """ -from metagpt.actions import Action, ActionOutput +from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode -PROMPT_TEMPLATE = """ -##TOPIC +CONTEXT_TEMPLATE = """ +## TOPIC {topic} -##RECORD +## RECORD {record} - -##Format example -{format_example} ------ - -Task: Refer to the "##TOPIC" (discussion objectives) and "##RECORD" (discussion records) to further inquire about the details that interest you, within a word limit of 150 words. -Special Note 1: Your intention is solely to ask questions without endorsing or negating any individual's viewpoints. -Special Note 2: This output should only include the topic "##OUTPUT". Do not add, remove, or modify the topic. Begin the output with '##OUTPUT', followed by an immediate line break, and then proceed to provide the content in the specified format as outlined in the "##Format example" section. -Special Note 3: The output should be in the same language as the input. """ -FORMAT_EXAMPLE = """ -## - -##OUTPUT -...(Please provide the specific details you would like to inquire about here.) - -## - -## -""" -OUTPUT_MAPPING = { - "OUTPUT": (str, ...), -} +QUESTIONS = ActionNode( + key="Questions", + expected_type=list[str], + instruction="Task: Refer to the context to further inquire about the details that interest you, within a word limit" + " of 150 words. Please provide the specific details you would like to inquire about here", + example=["1. What ...", "2. How ...", "3. ..."], +) class DetailMining(Action): - """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion.""" + """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and + "##RECORD" (discussion records), thereby deepening the discussion.""" - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) - - async def run(self, topic, record) -> ActionOutput: - prompt = PROMPT_TEMPLATE.format(topic=topic, record=record, format_example=FORMAT_EXAMPLE) - rsp = await self._aask_v1(prompt, "detail_mining", OUTPUT_MAPPING) + async def run(self, topic, record): + context = CONTEXT_TEMPLATE.format(topic=topic, record=record) + rsp = await QUESTIONS.fill(context=context, llm=self.llm) return rsp diff --git a/metagpt/actions/prepare_interview.py b/metagpt/actions/prepare_interview.py index b2704616e..7ed42d590 100644 --- a/metagpt/actions/prepare_interview.py +++ b/metagpt/actions/prepare_interview.py @@ -6,35 +6,18 @@ @File : prepare_interview.py """ from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode -PROMPT_TEMPLATE = """ -# Context -{context} - -## Format example ---- -Q1: question 1 here -References: - - point 1 - - point 2 - -Q2: question 2 here... ---- - ------ -Role: You are an interviewer of our company who is well-knonwn in frontend or backend develop; +QUESTIONS = ActionNode( + key="Questions", + expected_type=list[str], + instruction="""Role: You are an interviewer of our company who is well-knonwn in frontend or backend develop; Requirement: Provide a list of questions for the interviewer to ask the interviewee, by reading the resume of the interviewee in the context. -Attention: Provide as markdown block as the format above, at least 10 questions. -""" - -# prepare for a interview +Attention: Provide as markdown block as the format above, at least 10 questions.""", + example=["1. What ...", "2. How ..."], +) class PrepareInterview(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - async def run(self, context): - prompt = PROMPT_TEMPLATE.format(context=context) - question_list = await self._aask_v1(prompt) - return question_list + return await QUESTIONS.fill(context=context, llm=self.llm) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 1f14e7944..fe2c8d537 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -42,7 +42,7 @@ class WriteTasks(Action): def __init__(self, name="CreateTasks", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, with_messages, format=CONFIG.prompt_format): + async def run(self, with_messages, schema=CONFIG.prompt_schema): system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_system_designs = system_design_file_repo.changed_files @@ -89,16 +89,16 @@ class WriteTasks(Action): await self._save_pdf(task_doc=task_doc) return task_doc - async def _run_new_tasks(self, context, format=CONFIG.prompt_format): - node = await PM_NODE.fill(context, self.llm, format) + async def _run_new_tasks(self, context, schema=CONFIG.prompt_schema): + node = await PM_NODE.fill(context, self.llm, schema) # prompt_template, format_example = get_template(templates, format) # prompt = prompt_template.format(context=context, format_example=format_example) # rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) return node - async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: + async def _merge(self, system_design_doc, task_doc, schema=CONFIG.prompt_schema) -> Document: context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content) - node = await PM_NODE.fill(context, self.llm, format) + node = await PM_NODE.fill(context, self.llm, schema) task_doc.content = node.instruct_content.json(ensure_ascii=False) return task_doc diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index adba7decb..1cf21dbb7 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -111,7 +111,7 @@ class WritePRD(Action): # optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) - async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format) -> ActionOutput: + async def _run_new_requirement(self, requirements, schema=CONFIG.prompt_schema) -> ActionOutput: # sas = SearchAndSummarize() # # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) # rsp = "" @@ -121,7 +121,7 @@ class WritePRD(Action): # logger.info(rsp) project_name = CONFIG.project_name if CONFIG.project_name else "" context = CONTEXT_TEMPLATE.format(requirements=requirements, project_name=project_name) - node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=format) + node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=schema) await self._rename_workspace(node) return node @@ -130,11 +130,11 @@ class WritePRD(Action): node = await WP_IS_RELATIVE_NODE.fill(context, self.llm) return node.get("is_relative") == "YES" - async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: + async def _merge(self, new_requirement_doc, prd_doc, schema=CONFIG.prompt_schema) -> Document: if not CONFIG.project_name: CONFIG.project_name = Path(CONFIG.project_path).name prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) - node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=format) + node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=schema) prd_doc.content = node.instruct_content.json(ensure_ascii=False) await self._rename_workspace(node) return prd_doc diff --git a/metagpt/config.py b/metagpt/config.py index 80a3a28f4..131854a56 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -143,7 +143,7 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.repair_llm_output = self._get("REPAIR_LLM_OUTPUT", False) - self.prompt_format = self._get("PROMPT_FORMAT", "json") + self.prompt_schema = self._get("PROMPT_FORMAT", "json") self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() diff --git a/metagpt/utils/get_template.py b/metagpt/utils/get_template.py index 86c1915f7..7e05e5d5e 100644 --- a/metagpt/utils/get_template.py +++ b/metagpt/utils/get_template.py @@ -8,10 +8,10 @@ from metagpt.config import CONFIG -def get_template(templates, format=CONFIG.prompt_format): - selected_templates = templates.get(format) +def get_template(templates, schema=CONFIG.prompt_schema): + selected_templates = templates.get(schema) if selected_templates is None: - raise ValueError(f"Can't find {format} in passed in templates") + raise ValueError(f"Can't find {schema} in passed in templates") # Extract the selected templates prompt_template = selected_templates["PROMPT_TEMPLATE"] diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_detail_mining.py index 891dca6ca..30bcf9dfb 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_detail_mining.py @@ -19,5 +19,5 @@ async def test_detail_mining(): rsp = await detail_mining.run(topic=topic, record=record) logger.info(f"{rsp.content=}") - assert "##OUTPUT" in rsp.content - assert "蛋糕" in rsp.content + assert "Questions" in rsp.content + assert "1." in rsp.content diff --git a/tests/metagpt/actions/test_prepare_interview.py b/tests/metagpt/actions/test_prepare_interview.py new file mode 100644 index 000000000..7c32882e0 --- /dev/null +++ b/tests/metagpt/actions/test_prepare_interview.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/13 00:26 +@Author : fisherdeng +@File : test_detail_mining.py +""" +import pytest + +from metagpt.actions.prepare_interview import PrepareInterview +from metagpt.logs import logger + + +@pytest.mark.asyncio +async def test_prepare_interview(): + action = PrepareInterview() + rsp = await action.run("I just graduated and hope to find a job as a Python engineer") + logger.info(f"{rsp.content=}") + + assert "Questions" in rsp.content + assert "1." in rsp.content diff --git a/tests/metagpt/roles/ui_role.py b/tests/metagpt/roles/ui_role.py index 8ac799bf3..0932efa1f 100644 --- a/tests/metagpt/roles/ui_role.py +++ b/tests/metagpt/roles/ui_role.py @@ -10,6 +10,7 @@ from importlib import import_module from metagpt.actions import Action, ActionOutput, WritePRD # from metagpt.const import WORKSPACE_ROOT +from metagpt.actions.action_node import ActionNode from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role @@ -17,44 +18,38 @@ from metagpt.schema import Message from metagpt.tools.sd_engine import SDEngine PROMPT_TEMPLATE = """ -# Context {context} -## Format example -{format_example} ------ -Role: You are a UserInterface Designer; the goal is to finish a UI design according to PRD, give a design description, and select specified elements and UI style. -Requirements: Based on the context, fill in the following missing information, provide detailed HTML and CSS code -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. - -## UI Design Description:Provide as Plain text, place the design objective here -## Selected Elements:Provide as Plain text, up to 5 specified elements, clear and simple -## HTML Layout:Provide as Plain text, use standard HTML code -## CSS Styles (styles.css):Provide as Plain text,use standard css code -## Anything UNCLEAR:Provide as Plain text. Try to clarify it. - +## Role +You are a UserInterface Designer; the goal is to finish a UI design according to PRD, give a design description, and select specified elements and UI style. """ -FORMAT_EXAMPLE = """ +UI_DESIGN_DESC = ActionNode( + key="UI Design Desc", + expected_type=str, + instruction="place the design objective here", + example="Snake games are classic and addictive games with simple yet engaging elements. Here are the main elements" + " commonly found in snake games", +) -## UI Design Description -```Snake games are classic and addictive games with simple yet engaging elements. Here are the main elements commonly found in snake games ``` +SELECTED_ELEMENTS = ActionNode( + key="Selected Elements", + expected_type=list[str], + instruction="up to 5 specified elements, clear and simple", + example=[ + "Game Grid: The game grid is a rectangular...", + "Snake: The player controls a snake that moves across the grid...", + "Food: Food items (often represented as small objects or differently colored blocks)", + "Score: The player's score increases each time the snake eats a piece of food. The longer the snake becomes, the higher the score.", + "Game Over: The game ends when the snake collides with itself or an obstacle. At this point, the player's final score is displayed, and they are given the option to restart the game.", + ], +) -## Selected Elements - -Game Grid: The game grid is a rectangular... - -Snake: The player controls a snake that moves across the grid... - -Food: Food items (often represented as small objects or differently colored blocks) - -Score: The player's score increases each time the snake eats a piece of food. The longer the snake becomes, the higher the score. - -Game Over: The game ends when the snake collides with itself or an obstacle. At this point, the player's final score is displayed, and they are given the option to restart the game. - - -## HTML Layout - +HTML_LAYOUT = ActionNode( + key="HTML Layout", + expected_type=str, + instruction="use standard HTML code", + example=""" @@ -71,9 +66,14 @@ Game Over: The game ends when the snake collides with itself or an obstacle. At +""", +) -## CSS Styles (styles.css) -body { +CSS_STYLES = ActionNode( + key="CSS Styles", + expected_type=str, + instruction="use standard css code", + example="""body { display: flex; justify-content: center; align-items: center; @@ -121,19 +121,25 @@ body { color: #ff0000; display: none; } +""", +) -## Anything UNCLEAR -There are no unclear points. +ANYTHING_UNCLEAR = ActionNode( + key="Anything UNCLEAR", + expected_type=str, + instruction="Mention any aspects of the project that are unclear and try to clarify them.", + example="...", +) -""" +NODES = [ + UI_DESIGN_DESC, + SELECTED_ELEMENTS, + HTML_LAYOUT, + CSS_STYLES, + ANYTHING_UNCLEAR, +] -OUTPUT_MAPPING = { - "UI Design Description": (str, ...), - "Selected Elements": (str, ...), - "HTML Layout": (str, ...), - "CSS Styles (styles.css)": (str, ...), - "Anything UNCLEAR": (str, ...), -} +UI_DESIGN_NODE = ActionNode.from_children("UI_DESIGN", NODES) def load_engine(func): @@ -223,10 +229,8 @@ class UIDesign(Action): css_file_path = save_dir / "ui_design.css" html_file_path = save_dir / "ui_design.html" - with open(css_file_path, "w") as css_file: - css_file.write(css_content) - with open(html_file_path, "w") as html_file: - html_file.write(html_content) + css_file_path.write_text(css_content) + html_file_path.write_text(html_content) async def run(self, requirements: list[Message], *args, **kwargs) -> ActionOutput: """Run the UI Design action.""" @@ -234,9 +238,9 @@ class UIDesign(Action): context = requirements[-1].content ui_design_draft = self.parse_requirement(context=context) # todo: parse requirements str - prompt = PROMPT_TEMPLATE.format(context=ui_design_draft, format_example=FORMAT_EXAMPLE) + prompt = PROMPT_TEMPLATE.format(context=ui_design_draft) logger.info(prompt) - ui_describe = await self._aask_v1(prompt, "ui_design", OUTPUT_MAPPING) + ui_describe = await UI_DESIGN_NODE.fill(prompt) logger.info(ui_describe.content) logger.info(ui_describe.instruct_content) css = self.parse_css_code(context=ui_describe.content) From 09e2f05a6a553c32cfdcdb53ec680d73acda1af2 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:24:08 +0800 Subject: [PATCH 336/413] refactor action_output and action_node --- metagpt/actions/action_node.py | 4 ++-- metagpt/actions/action_output.py | 26 +-------------------- metagpt/actions/write_prd.py | 2 +- metagpt/utils/serialize.py | 4 ++-- tests/metagpt/actions/test_action_output.py | 6 ++--- tests/metagpt/memory/test_memory_storage.py | 4 ++-- tests/metagpt/utils/test_serialize.py | 4 ++-- 7 files changed, 13 insertions(+), 37 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 0368d2df1..865cb2d32 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -6,7 +6,7 @@ @File : action_node.py """ import json -from typing import Dict, Generic, List, Optional, Type, TypeVar +from typing import Any, Dict, Generic, List, Optional, Tuple, Type, TypeVar from pydantic import BaseModel, create_model, root_validator, validator from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -127,7 +127,7 @@ class ActionNode(Generic[T]): return self.get_self_mapping() @classmethod - def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): + def create_model_class(cls, class_name: str, mapping: Dict[str, Tuple[Type, Any]]): """基于pydantic v1的模型动态生成,用来检验结果类型正确性""" new_class = create_model(class_name, **mapping) diff --git a/metagpt/actions/action_output.py b/metagpt/actions/action_output.py index 25326d43b..6be8dac50 100644 --- a/metagpt/actions/action_output.py +++ b/metagpt/actions/action_output.py @@ -6,9 +6,7 @@ @File : action_output """ -from typing import Dict, Type - -from pydantic import BaseModel, create_model, root_validator, validator +from pydantic import BaseModel class ActionOutput: @@ -18,25 +16,3 @@ class ActionOutput: def __init__(self, content: str, instruct_content: BaseModel): self.content = content self.instruct_content = instruct_content - - @classmethod - def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): - new_class = create_model(class_name, **mapping) - - @validator("*", allow_reuse=True) - def check_name(v, field): - if field.name not in mapping.keys(): - raise ValueError(f"Unrecognized block: {field.name}") - return v - - @root_validator(pre=True, allow_reuse=True) - def check_missing_fields(values): - required_fields = set(mapping.keys()) - missing_fields = required_fields - set(values.keys()) - if missing_fields: - raise ValueError(f"Missing fields: {missing_fields}") - return values - - new_class.__validator_check_name = classmethod(check_name) - new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) - return new_class diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 1cf21dbb7..23925ff10 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -67,7 +67,7 @@ class WritePRD(Action): def __init__(self, name="", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput | Message: + async def run(self, with_messages, schema=CONFIG.prompt_schema, *args, **kwargs) -> ActionOutput | Message: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are # related to the PRD. If they are related, rewrite the PRD. docs_file_repo = CONFIG.git_repo.new_file_repository(relative_path=DOCS_FILE_REPO) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 124176fcb..5e52846e1 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -6,7 +6,7 @@ import copy import pickle from typing import Dict, List -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.schema import Message @@ -60,7 +60,7 @@ def deserialize_message(message_ser: str) -> Message: message = pickle.loads(message_ser) if message.instruct_content: ic = message.instruct_content - ic_obj = ActionOutput.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + ic_obj = ActionNode.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new diff --git a/tests/metagpt/actions/test_action_output.py b/tests/metagpt/actions/test_action_output.py index ef8e239bd..f1765cb03 100644 --- a/tests/metagpt/actions/test_action_output.py +++ b/tests/metagpt/actions/test_action_output.py @@ -7,7 +7,7 @@ """ from typing import List, Tuple -from metagpt.actions import ActionOutput +from metagpt.actions.action_node import ActionNode t_dict = { "Required Python third-party packages": '"""\nflask==1.1.2\npygame==2.0.1\n"""\n', @@ -37,12 +37,12 @@ WRITE_TASKS_OUTPUT_MAPPING = { def test_create_model_class(): - test_class = ActionOutput.create_model_class("test_class", WRITE_TASKS_OUTPUT_MAPPING) + test_class = ActionNode.create_model_class("test_class", WRITE_TASKS_OUTPUT_MAPPING) assert test_class.__name__ == "test_class" def test_create_model_class_with_mapping(): - t = ActionOutput.create_model_class("test_class_1", WRITE_TASKS_OUTPUT_MAPPING) + t = ActionNode.create_model_class("test_class_1", WRITE_TASKS_OUTPUT_MAPPING) t1 = t(**t_dict) value = t1.dict()["Task list"] assert value == ["game.py", "app.py", "static/css/styles.css", "static/js/script.js", "templates/index.html"] diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index c67ca689f..7b74eb512 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -8,7 +8,7 @@ from typing import List from metagpt.actions import UserRequirement, WritePRD -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.memory.memory_storage import MemoryStorage from metagpt.schema import Message @@ -42,7 +42,7 @@ def test_idea_message(): def test_actionout_message(): out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} - ic_obj = ActionOutput.create_model_class("prd", out_mapping) + ic_obj = ActionNode.create_model_class("prd", out_mapping) role_id = "UTUser2(Architect)" content = "The user has requested the creation of a command-line interface (CLI) snake game" diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index ffa34866c..f027d53f8 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -7,7 +7,7 @@ from typing import List, Tuple from metagpt.actions import WritePRD -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.schema import Message from metagpt.utils.serialize import ( actionoutout_schema_to_mapping, @@ -54,7 +54,7 @@ def test_actionoutout_schema_to_mapping(): def test_serialize_and_deserialize_message(): out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} - ic_obj = ActionOutput.create_model_class("prd", out_mapping) + ic_obj = ActionNode.create_model_class("prd", out_mapping) message = Message( content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD From 33c58d97fef317afba757ba04ece00fd1830130d Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:32:52 +0800 Subject: [PATCH 337/413] refine code --- metagpt/actions/action_node.py | 2 +- metagpt/actions/write_prd_an.py | 8 ++++---- metagpt/provider/postprecess/base_postprecess_plugin.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 865cb2d32..790069369 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -232,7 +232,7 @@ class ActionNode(Generic[T]): return prompt @retry( - wait=wait_random_exponential(min=1, max=60), + wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6), after=general_after_log(logger), ) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index d96c0aeac..edd94a463 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -47,7 +47,7 @@ PRODUCT_GOALS = ActionNode( USER_STORIES = ActionNode( key="User Stories", expected_type=list[str], - instruction="Provide up to five scenario-based user stories.", + instruction="Provide up to 3 to 5 scenario-based user stories.", example=[ "As a user, I want to be able to choose difficulty levels", "As a player, I want to see my score after each game", @@ -57,7 +57,7 @@ USER_STORIES = ActionNode( COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", expected_type=list[str], - instruction="Provide analyses for up to seven competitive products.", + instruction="Provide 5 to 7 competitive products.", example=["Python Snake Game: Simple interface, lacks advanced features"], ) @@ -92,8 +92,8 @@ REQUIREMENT_ANALYSIS = ActionNode( REQUIREMENT_POOL = ActionNode( key="Requirement Pool", expected_type=list[list[str]], - instruction="List down the requirements with their priority (P0, P1, P2).", - example=[["P0", "..."], ["P1", "..."]], + instruction="List down the top-5 requirements with their priority (P0, P1, P2).", + example=[["P0", "The main code ..."], ["P0", "The game algorithm ..."]], ) UI_DESIGN_DRAFT = ActionNode( diff --git a/metagpt/provider/postprecess/base_postprecess_plugin.py b/metagpt/provider/postprecess/base_postprecess_plugin.py index 0d1cfbb11..721476507 100644 --- a/metagpt/provider/postprecess/base_postprecess_plugin.py +++ b/metagpt/provider/postprecess/base_postprecess_plugin.py @@ -44,7 +44,7 @@ class BasePostPrecessPlugin(object): def run_retry_parse_json_text(self, content: str) -> Union[dict, list]: """inherited class can re-implement the function""" - logger.info(f"extracted json CONTENT from output:\n{content}") + logger.debug(f"extracted json CONTENT from output:\n{content}") parsed_data = retry_parse_json_text(output=content) # should use output=content return parsed_data From 62f34db137dcd73b965e613497ca1dd2df1ddcd9 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 23:53:04 +0800 Subject: [PATCH 338/413] refine code. move azure tts to tool, refactor actions --- metagpt/actions/__init__.py | 2 - metagpt/actions/action.py | 5 ++- metagpt/actions/analyze_dep_libs.py | 37 ------------------- metagpt/actions/design_filenames.py | 30 --------------- ...detail_mining.py => generate_questions.py} | 18 ++------- metagpt/schema.py | 3 +- metagpt/{actions => tools}/azure_tts.py | 19 ++++------ tests/metagpt/actions/test_azure_tts.py | 4 +- tests/metagpt/actions/test_detail_mining.py | 20 ++++++---- 9 files changed, 32 insertions(+), 106 deletions(-) delete mode 100644 metagpt/actions/analyze_dep_libs.py delete mode 100644 metagpt/actions/design_filenames.py rename metagpt/actions/{detail_mining.py => generate_questions.py} (69%) rename metagpt/{actions => tools}/azure_tts.py (65%) diff --git a/metagpt/actions/__init__.py b/metagpt/actions/__init__.py index 79ff94b3e..c34c72ed2 100644 --- a/metagpt/actions/__init__.py +++ b/metagpt/actions/__init__.py @@ -13,7 +13,6 @@ from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.debug_error import DebugError from metagpt.actions.design_api import WriteDesign from metagpt.actions.design_api_review import DesignReview -from metagpt.actions.design_filenames import DesignFilenames from metagpt.actions.project_management import AssignTasks, WriteTasks from metagpt.actions.research import CollectLinks, WebBrowseAndSummarize, ConductResearch from metagpt.actions.run_code import RunCode @@ -33,7 +32,6 @@ class ActionType(Enum): WRITE_PRD_REVIEW = WritePRDReview WRITE_DESIGN = WriteDesign DESIGN_REVIEW = DesignReview - DESIGN_FILENAMES = DesignFilenames WRTIE_CODE = WriteCode WRITE_CODE_REVIEW = WriteCodeReview WRITE_TEST = WriteTest diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 5c5884e8b..a3a9c0195 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -13,7 +13,7 @@ from typing import Optional from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM -from metagpt.schema import BaseContext +from metagpt.schema import CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext class Action(ABC): @@ -21,7 +21,8 @@ class Action(ABC): name: str llm: LLM - context: dict | BaseContext | str | None + # FIXME: simplify context + context: dict | CodingContext | CodeSummarizeContext | TestingContext | RunCodeContext | str | None prefix: str desc: str node: ActionNode | None diff --git a/metagpt/actions/analyze_dep_libs.py b/metagpt/actions/analyze_dep_libs.py deleted file mode 100644 index 53d40200a..000000000 --- a/metagpt/actions/analyze_dep_libs.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/19 12:01 -@Author : alexanderwu -@File : analyze_dep_libs.py -""" - -from metagpt.actions import Action - -PROMPT = """You are an AI developer, trying to write a program that generates code for users based on their intentions. - -For the user's prompt: - ---- -The API is: {prompt} ---- - -We decide the generated files are: {filepaths_string} - -Now that we have a file list, we need to understand the shared dependencies they have. -Please list and briefly describe the shared contents between the files we are generating, including exported variables, -data patterns, id names of all DOM elements that javascript functions will use, message names and function names. -Focus only on the names of shared dependencies, do not add any other explanations. -""" - - -class AnalyzeDepLibs(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = "Analyze the runtime dependencies of the program based on the context" - - async def run(self, requirement, filepaths_string): - # prompt = f"Below is the product requirement document (PRD):\n\n{prd}\n\n{PROMPT}" - prompt = PROMPT.format(prompt=requirement, filepaths_string=filepaths_string) - design_filenames = await self._aask(prompt) - return design_filenames diff --git a/metagpt/actions/design_filenames.py b/metagpt/actions/design_filenames.py deleted file mode 100644 index ffa171d7b..000000000 --- a/metagpt/actions/design_filenames.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/19 11:50 -@Author : alexanderwu -@File : design_filenames.py -""" -from metagpt.actions import Action -from metagpt.logs import logger - -PROMPT = """You are an AI developer, trying to write a program that generates code for users based on their intentions. -When given their intentions, provide a complete and exhaustive list of file paths needed to write the program for the user. -Only list the file paths you will write and return them as a Python string list. -Do not add any other explanations, just return a Python string list.""" - - -class DesignFilenames(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = ( - "Based on the PRD, consider system design, and carry out the basic design of the corresponding " - "APIs, data structures, and database tables. Please give your design, feedback clearly and in detail." - ) - - async def run(self, prd): - prompt = f"The following is the Product Requirement Document (PRD):\n\n{prd}\n\n{PROMPT}" - design_filenames = await self._aask(prompt) - logger.debug(prompt) - logger.debug(design_filenames) - return design_filenames diff --git a/metagpt/actions/detail_mining.py b/metagpt/actions/generate_questions.py similarity index 69% rename from metagpt/actions/detail_mining.py rename to metagpt/actions/generate_questions.py index 0314d30dd..c38c463bc 100644 --- a/metagpt/actions/detail_mining.py +++ b/metagpt/actions/generate_questions.py @@ -3,19 +3,11 @@ """ @Time : 2023/9/12 17:45 @Author : fisherdeng -@File : detail_mining.py +@File : generate_questions.py """ from metagpt.actions import Action from metagpt.actions.action_node import ActionNode -CONTEXT_TEMPLATE = """ -## TOPIC -{topic} - -## RECORD -{record} -""" - QUESTIONS = ActionNode( key="Questions", expected_type=list[str], @@ -25,11 +17,9 @@ QUESTIONS = ActionNode( ) -class DetailMining(Action): +class GenerateQuestions(Action): """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion.""" - async def run(self, topic, record): - context = CONTEXT_TEMPLATE.format(topic=topic, record=record) - rsp = await QUESTIONS.fill(context=context, llm=self.llm) - return rsp + async def run(self, context): + return await QUESTIONS.fill(context=context, llm=self.llm) diff --git a/metagpt/schema.py b/metagpt/schema.py index aacc2cebb..d2f8d33e6 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -18,6 +18,7 @@ import asyncio import json import os.path import uuid +from abc import ABC from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path @@ -265,7 +266,7 @@ class MessageQueue: T = TypeVar("T", bound="BaseModel") -class BaseContext(BaseModel): +class BaseContext(BaseModel, ABC): @classmethod @handle_exception def loads(cls: Type[T], val: str) -> Optional[T]: diff --git a/metagpt/actions/azure_tts.py b/metagpt/tools/azure_tts.py similarity index 65% rename from metagpt/actions/azure_tts.py rename to metagpt/tools/azure_tts.py index daa3f6892..e59d98016 100644 --- a/metagpt/actions/azure_tts.py +++ b/metagpt/tools/azure_tts.py @@ -7,19 +7,16 @@ """ from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer -from metagpt.actions.action import Action -from metagpt.config import Config +from metagpt.config import CONFIG -class AzureTTS(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.config = Config() +class AzureTTS: + """https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles""" - # Parameters reference: https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles - def synthesize_speech(self, lang, voice, role, text, output_file): - subscription_key = self.config.get("AZURE_TTS_SUBSCRIPTION_KEY") - region = self.config.get("AZURE_TTS_REGION") + @classmethod + def synthesize_speech(cls, lang, voice, role, text, output_file): + subscription_key = CONFIG.get("AZURE_TTS_SUBSCRIPTION_KEY") + region = CONFIG.get("AZURE_TTS_REGION") speech_config = SpeechConfig(subscription=subscription_key, region=region) speech_config.speech_synthesis_voice_name = voice @@ -41,5 +38,5 @@ class AzureTTS(Action): if __name__ == "__main__": - azure_tts = AzureTTS("azure_tts") + azure_tts = AzureTTS() azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "Hello, I am Kaka", "output.wav") diff --git a/tests/metagpt/actions/test_azure_tts.py b/tests/metagpt/actions/test_azure_tts.py index bcafe10f5..9995e9691 100644 --- a/tests/metagpt/actions/test_azure_tts.py +++ b/tests/metagpt/actions/test_azure_tts.py @@ -5,11 +5,11 @@ @Author : alexanderwu @File : test_azure_tts.py """ -from metagpt.actions.azure_tts import AzureTTS +from metagpt.tools.azure_tts import AzureTTS def test_azure_tts(): - azure_tts = AzureTTS("azure_tts") + azure_tts = AzureTTS() azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "你好,我是卡卡", "output.wav") # 运行需要先配置 SUBSCRIPTION_KEY diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_detail_mining.py index 30bcf9dfb..a178ec840 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_detail_mining.py @@ -3,20 +3,26 @@ """ @Time : 2023/9/13 00:26 @Author : fisherdeng -@File : test_detail_mining.py +@File : test_generate_questions.py """ import pytest -from metagpt.actions.detail_mining import DetailMining +from metagpt.actions.generate_questions import GenerateQuestions from metagpt.logs import logger +context = """ +## topic +如何做一个生日蛋糕 + +## record +我认为应该先准备好材料,然后再开始做蛋糕。 +""" + @pytest.mark.asyncio -async def test_detail_mining(): - topic = "如何做一个生日蛋糕" - record = "我认为应该先准备好材料,然后再开始做蛋糕。" - detail_mining = DetailMining("detail_mining") - rsp = await detail_mining.run(topic=topic, record=record) +async def test_generate_questions(): + detail_mining = GenerateQuestions() + rsp = await detail_mining.run(context) logger.info(f"{rsp.content=}") assert "Questions" in rsp.content From 0f78d4ea51d6e7d579dc7340e9b7e2039d0f5aa2 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 23:58:18 +0800 Subject: [PATCH 339/413] refine code --- metagpt/actions/action_node.py | 52 +++++++++++++++++----------------- metagpt/actions/design_api.py | 4 +-- metagpt/actions/write_prd.py | 4 +-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 790069369..092dd5755 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -112,15 +112,15 @@ class ActionNode(Generic[T]): obj.add_children(nodes) return obj - def get_children_mapping(self) -> Dict[str, Type]: + def get_children_mapping(self) -> Dict[str, Tuple[Type, Any]]: """获得子ActionNode的字典,以key索引""" return {k: (v.expected_type, ...) for k, v in self.children.items()} - def get_self_mapping(self) -> Dict[str, Type]: + def get_self_mapping(self) -> Dict[str, Tuple[Type, Any]]: """get self key: type mapping""" return {self.key: (self.expected_type, ...)} - def get_mapping(self, mode="children") -> Dict[str, Type]: + def get_mapping(self, mode="children") -> Dict[str, Tuple[Type, Any]]: """get key: type mapping under mode""" if mode == "children" or (mode == "auto" and self.children): return self.get_children_mapping() @@ -175,46 +175,46 @@ class ActionNode(Generic[T]): return node_dict # 遍历子节点并递归调用 to_dict 方法 - for child_key, child_node in self.children.items(): + for _, child_node in self.children.items(): node_dict.update(child_node.to_dict(format_func)) return node_dict - def compile_to(self, i: Dict, to) -> str: - if to == "json": + def compile_to(self, i: Dict, schema) -> str: + if schema == "json": return json.dumps(i, indent=4) - elif to == "markdown": + elif schema == "markdown": return dict_to_markdown(i) else: return str(i) - def tagging(self, text, to, tag="") -> str: + def tagging(self, text, schema, tag="") -> str: if not tag: return text - if to == "json": + if schema == "json": return f"[{tag}]\n" + text + f"\n[/{tag}]" else: return f"[{tag}]\n" + text + f"\n[/{tag}]" - def _compile_f(self, to, mode, tag, format_func) -> str: + def _compile_f(self, schema, mode, tag, format_func) -> str: nodes = self.to_dict(format_func=format_func, mode=mode) - text = self.compile_to(nodes, to) - return self.tagging(text, to, tag) + text = self.compile_to(nodes, schema) + return self.tagging(text, schema, tag) - def compile_instruction(self, to="raw", mode="children", tag="") -> str: + def compile_instruction(self, schema="raw", mode="children", tag="") -> str: """compile to raw/json/markdown template with all/root/children nodes""" format_func = lambda i: f"{i.expected_type} # {i.instruction}" - return self._compile_f(to, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func) - def compile_example(self, to="raw", mode="children", tag="") -> str: + def compile_example(self, schema="raw", mode="children", tag="") -> str: """compile to raw/json/markdown examples with all/root/children nodes""" # 这里不能使用f-string,因为转译为str后再json.dumps会额外加上引号,无法作为有效的example # 错误示例:"File list": "['main.py', 'const.py', 'game.py']", 注意这里值不是list,而是str format_func = lambda i: i.example - return self._compile_f(to, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func) - def compile(self, context, to="json", mode="children", template=SIMPLE_TEMPLATE) -> str: + def compile(self, context, schema="json", mode="children", template=SIMPLE_TEMPLATE) -> str: """ mode: all/root/children mode="children": 编译所有子节点为一个统一模板,包括instruction与example @@ -224,8 +224,8 @@ class ActionNode(Generic[T]): # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", # compile example暂时不支持markdown - self.instruction = self.compile_instruction(to="markdown", mode=mode) - self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) + self.instruction = self.compile_instruction(schema="markdown", mode=mode) + self.example = self.compile_example(schema=schema, tag="CONTENT", mode=mode) prompt = template.format( context=context, example=self.example, instruction=self.instruction, constraint=CONSTRAINT ) @@ -272,22 +272,22 @@ class ActionNode(Generic[T]): def set_context(self, context): self.set_recursive("context", context) - async def simple_fill(self, to, mode): - prompt = self.compile(context=self.context, to=to, mode=mode) + async def simple_fill(self, schema, mode): + prompt = self.compile(context=self.context, schema=schema, mode=mode) mapping = self.get_mapping(mode) class_name = f"{self.key}_AN" - content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=to) + content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=schema) self.content = content self.instruct_content = scontent return self - async def fill(self, context, llm, to="json", mode="auto", strgy="simple"): + async def fill(self, context, llm, schema="json", mode="auto", strgy="simple"): """Fill the node(s) with mode. :param context: Everything we should know when filling node. :param llm: Large Language Model with pre-defined system message. - :param to: json/markdown, determine example and output format. + :param schema: json/markdown, determine example and output format. - json: it's easy to open source LLM with json format - markdown: when generating code, markdown is always better :param mode: auto/children/root @@ -303,12 +303,12 @@ class ActionNode(Generic[T]): self.set_context(context) if strgy == "simple": - return await self.simple_fill(to, mode) + return await self.simple_fill(schema, mode) elif strgy == "complex": # 这里隐式假设了拥有children tmp = {} for _, i in self.children.items(): - child = await i.simple_fill(to, mode) + child = await i.simple_fill(schema, mode) tmp.update(child.instruct_content.dict()) cls = self.create_children_class() self.instruct_content = cls(**tmp) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index f757ca856..548725fde 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -81,12 +81,12 @@ class WriteDesign(Action): return ActionOutput(content=changed_files.json(), instruct_content=changed_files) async def _new_system_design(self, context, schema=CONFIG.prompt_schema): - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, schema=schema) return node async def _merge(self, prd_doc, system_design_doc, schema=CONFIG.prompt_schema): context = NEW_REQ_TEMPLATE.format(old_design=system_design_doc.content, context=prd_doc.content) - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, schema=schema) system_design_doc.content = node.instruct_content.json(ensure_ascii=False) return system_design_doc diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 23925ff10..7c160fa89 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -121,7 +121,7 @@ class WritePRD(Action): # logger.info(rsp) project_name = CONFIG.project_name if CONFIG.project_name else "" context = CONTEXT_TEMPLATE.format(requirements=requirements, project_name=project_name) - node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=schema) + node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, schema=schema) await self._rename_workspace(node) return node @@ -134,7 +134,7 @@ class WritePRD(Action): if not CONFIG.project_name: CONFIG.project_name = Path(CONFIG.project_path).name prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) - node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=schema) + node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, schema=schema) prd_doc.content = node.instruct_content.json(ensure_ascii=False) await self._rename_workspace(node) return prd_doc From d0382b0ba7dfa69c7aafb7f6619c81531637d728 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:34:57 +0800 Subject: [PATCH 340/413] refine devcontainer README --- .devcontainer/README.md | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/.devcontainer/README.md b/.devcontainer/README.md index dd088aab1..be692c14d 100644 --- a/.devcontainer/README.md +++ b/.devcontainer/README.md @@ -1,39 +1,34 @@ -# Dev container +# Dev Container -This project includes a [dev container](https://containers.dev/), which lets you use a container as a full-featured dev environment. +This project includes a [Dev Container](https://containers.dev/), offering you a comprehensive and fully-featured development environment within a container. By leveraging the Dev Container configuration in this folder, you can seamlessly build and initiate MetaGPT locally. For detailed information, please refer to the main README in the home directory. -You can use the dev container configuration in this folder to build and start running MetaGPT locally! For more, refer to the main README under the home directory. -You can use it in [GitHub Codespaces](https://github.com/features/codespaces) or the [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). +You can utilize this Dev Container in [GitHub Codespaces](https://github.com/features/codespaces) or with the [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). ## GitHub Codespaces -Open in GitHub Codespaces +[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/geekan/MetaGPT) -You may use the button above to open this repo in a Codespace +Click the button above to open this repository in a Codespace. For additional information, refer to the [GitHub documentation on creating a Codespace](https://docs.github.com/en/free-pro-team@latest/github/developing-online-with-codespaces/creating-a-codespace#creating-a-codespace). -For more info, check out the [GitHub documentation](https://docs.github.com/en/free-pro-team@latest/github/developing-online-with-codespaces/creating-a-codespace#creating-a-codespace). - ## VS Code Dev Containers -Open in Dev Containers +[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/geekan/MetaGPT) -Note: If you click this link you will open the main repo and not your local cloned repo, you can use this link and replace with your username and cloned repo name: -https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/geekan/MetaGPT +Note: Clicking the link above opens the main repository. To open your local cloned repository, replace the URL with your username and cloned repository's name: `https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com//` +If you have VS Code and Docker installed, use the button above to get started. This will prompt VS Code to install the Dev Containers extension if it's not already installed, clone the source code into a container volume, and set up a dev container for you. -If you already have VS Code and Docker installed, you can use the button above to get started. This will cause VS Code to automatically install the Dev Containers extension if needed, clone the source code into a container volume, and spin up a dev container for use. +Alternatively, follow these steps to open this repository in a container using the VS Code Dev Containers extension: -You can also follow these steps to open this repo in a container using the VS Code Dev Containers extension: +1. For first-time users of a development container, ensure your system meets the prerequisites (e.g., Docker installation) as outlined in the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started). -1. If this is your first time using a development container, please ensure your system meets the pre-reqs (i.e. have Docker installed) in the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started). - -2. Open a locally cloned copy of the code: - - - Fork and Clone this repository to your local filesystem. +2. To open a locally cloned copy of the code: + - Fork and clone this repository to your local file system. - Press F1 and select the **Dev Containers: Open Folder in Container...** command. - - Select the cloned copy of this folder, wait for the container to start, and try things out! + - Choose the cloned folder, wait for the container to initialize, and start exploring! -You can learn more in the [Dev Containers documentation](https://code.visualstudio.com/docs/devcontainers/containers). +Learn more in the [VS Code Dev Containers documentation](https://code.visualstudio.com/docs/devcontainers/containers). -## Tips and tricks +## Tips and Tricks -* If you are working with the same repository folder in a container and Windows, you'll want consistent line endings (otherwise you may see hundreds of changes in the SCM view). The `.gitattributes` file in the root of this repo will disable line ending conversion and should prevent this. See [tips and tricks](https://code.visualstudio.com/docs/devcontainers/tips-and-tricks#_resolving-git-line-ending-issues-in-containers-resulting-in-many-modified-files) for more info. -* If you'd like to review the contents of the image used in this dev container, you can check it out in the [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/python) repo. +* When working with the same repository folder in both a container and on Windows, it's crucial to have consistent line endings to avoid numerous changes in the SCM view. The `.gitattributes` file in the root of this repository disables line ending conversion, helping to prevent this issue. For more information, see [resolving git line ending issues in containers](https://code.visualstudio.com/docs/devcontainers/tips-and-tricks#_resolving-git-line-ending-issues-in-containers-resulting-in-many-modified-files). + +* If you're curious about the contents of the image used in this Dev Container, you can review it in the [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/python) repository. From 1a62148dc6ea684a9dc0da372dc5c1ba3ac785a9 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:35:15 +0800 Subject: [PATCH 341/413] add proper space --- .devcontainer/postCreateCommand.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/postCreateCommand.sh b/.devcontainer/postCreateCommand.sh index 46788e306..3901193cd 100644 --- a/.devcontainer/postCreateCommand.sh +++ b/.devcontainer/postCreateCommand.sh @@ -4,4 +4,4 @@ sudo npm install -g @mermaid-js/mermaid-cli # Step 2: Ensure that Python 3.9+ is installed on your system. You can check this by using: python --version -pip install -e. \ No newline at end of file +pip install -e . \ No newline at end of file From 6b235e536e6d5b2590db97cdcd4aece779227c13 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:39:35 +0800 Subject: [PATCH 342/413] .gitattributes: ensure lf --- .gitattributes | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.gitattributes b/.gitattributes index 32555a806..7f1424434 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,29 @@ +# HTML code is incorrectly calculated into statistics, so ignore them *.html linguist-detectable=false +# Auto detect text files and perform LF normalization +* text=auto eol=lf + +# Ensure shell scripts use LF (Linux style) line endings on Windows +*.sh text eol=lf + +# Treat specific binary files as binary and prevent line ending conversion +*.png binary +*.jpg binary +*.gif binary +*.ico binary + +# Preserve original line endings for specific document files +*.doc text eol=crlf +*.docx text eol=crlf +*.pdf binary + +# Ensure source code and script files use LF line endings +*.py text eol=lf +*.js text eol=lf +*.html text eol=lf +*.css text eol=lf + +# Specify custom diff driver for specific file types +*.md diff=markdown +*.json diff=json From efebc07e54374accd65c7a82c2c10fb4b1dfdb0a Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:47:28 +0800 Subject: [PATCH 343/413] refine .gitignore and .pre-commit-config.yaml --- .gitignore | 8 +------- .pre-commit-config.yaml | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 0ac318ff5..c12506b0e 100644 --- a/.gitignore +++ b/.gitignore @@ -144,24 +144,18 @@ cython_debug/ allure-report allure-results -# idea +# idea / vscode / macos .idea .DS_Store .vscode -log.txt -docs/scripts/set_env.sh key.yaml -output.json data -data/output_add.json data.ms examples/nb/ .chroma *~$* workspace/* -*.mmd tmp -output.wav metagpt/roles/idea_agent.py .aider* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b1892a709..338f832ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ default_stages: [ commit ] # Install # 1. pip install pre-commit -# 2. pre-commit install(the first time you download the repo, it will be cached for future use) +# 2. pre-commit install repos: - repo: https://github.com/pycqa/isort rev: 5.11.5 From 3b7c2e48599b9837894de766eb7f6bb275752667 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:49:08 +0800 Subject: [PATCH 344/413] updating time of license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 5b0c000cd..67460e101 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License -Copyright (c) Chenglin Wu +Copyright (c) 2023 Chenglin Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 394055d7e6380b05f28ffaebf53b7ae50c9d79a6 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:53:36 +0800 Subject: [PATCH 345/413] align ruff.toml with black --- ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruff.toml b/ruff.toml index 7835865e0..21de5ee14 100644 --- a/ruff.toml +++ b/ruff.toml @@ -31,7 +31,7 @@ exclude = [ ] # Same as Black. -line-length = 119 +line-length = 120 # Allow unused variables when underscore-prefixed. dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" From 5c7c522c623e56efbc89e47adfa5b59ebf775754 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:54:29 +0800 Subject: [PATCH 346/413] uncomment fire in requirements.txt due to usage in the example --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 515a4d88b..f5ef63c58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ channels==4.0.0 # docx==0.2.4 #faiss==1.5.3 faiss_cpu==1.7.4 -# fire==0.4.0 +fire==0.4.0 typer # godot==0.1.1 # google_api_python_client==2.93.0 From 66c0bce60bfffb3727f27554ee0cbb5d0fac8817 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:58:56 +0800 Subject: [PATCH 347/413] add proper space --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c6e22989b..9eeacbccb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ COPY . /app/metagpt WORKDIR /app/metagpt RUN mkdir workspace &&\ pip install --no-cache-dir -r requirements.txt &&\ - pip install -e. + pip install -e . # Running with an infinite loop using the tail command CMD ["sh", "-c", "tail -f /dev/null"] From 77ec9b823f985fc0f30bccb5a71b2eec18b77f1d Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:59:23 +0800 Subject: [PATCH 348/413] remove duplicate string --- .dockerignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 2968dd34d..8c09eaf73 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ workspace tmp build -workspace dist data geckodriver.log From 68c8ef107347f713ee6f3433735374d175b98017 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 20 Dec 2023 10:44:30 +0800 Subject: [PATCH 349/413] update ser&deser code --- metagpt/actions/action.py | 1 - metagpt/roles/role.py | 26 ++++-- metagpt/schema.py | 8 +- metagpt/startup.py | 37 +++++--- metagpt/utils/utils.py | 17 ++-- startup.py | 86 ------------------- .../serialize_deserialize/test_role.py | 2 +- .../serialize_deserialize/test_team.py | 14 ++- 8 files changed, 70 insertions(+), 121 deletions(-) delete mode 100644 startup.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 570863388..8cba18945 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -20,7 +20,6 @@ from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess from metagpt.utils.common import OutputParser from metagpt.utils.utils import general_after_log -from metagpt.utils.utils import import_class action_subclass_registry = {} diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 9b1e0bf94..09371ae08 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -39,7 +39,7 @@ from metagpt.provider.human_provider import HumanProvider from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output -from metagpt.utils.utils import read_json_file, write_json_file, import_class +from metagpt.utils.utils import read_json_file, write_json_file, import_class, role_raise_decorator PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -137,6 +137,7 @@ class Role(BaseModel): # builtin variables recovered: bool = False # to tag if a recovered role + latest_observed_msg: Message = None # record the latest observed message when interrupted builtin_class_name: str = "" _private_attributes = { @@ -200,7 +201,6 @@ class Role(BaseModel): def _reset(self): object.__setattr__(self, "_states", []) object.__setattr__(self, "_actions", []) - # object.__setattr__(self, "_rc", RoleContext()) @property def _setting(self): @@ -210,7 +210,7 @@ class Role(BaseModel): stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ if stg_path is None else stg_path - role_info = self.dict(exclude={"_rc": {"memory": True}, "_llm": True}) + role_info = self.dict(exclude={"_rc": {"memory": True, "msg_buffer": True}, "_llm": True}) role_info.update({ "role_class": self.__class__.__name__, "module_name": self.__module__ @@ -311,7 +311,7 @@ class Role(BaseModel): def _set_state(self, state: int): """Update the current state.""" self._rc.state = state - logger.debug(self._actions) + logger.debug(f"actions={self._actions}, state={state}") self._rc.todo = self._actions[self._rc.state] if state >= 0 else None def set_env(self, env: "Environment"): @@ -388,15 +388,30 @@ class Role(BaseModel): return msg + def _find_news(self, observed: list[Message], existed: list[Message]) -> list[Message]: + news = [] + # Warning, remove `id` here to make it work for recover + observed_pure = [msg.dict(exclude={"id": True}) for msg in observed] + existed_pure = [msg.dict(exclude={"id": True}) for msg in existed] + for idx, new in enumerate(observed_pure): + if new["cause_by"] in self._rc.watch and new not in existed_pure: + news.append(observed[idx]) + return news + async def _observe(self, ignore_memory=False) -> int: """Prepare new messages for processing from the message buffer and other sources.""" # Read unprocessed messages from the msg buffer. news = self._rc.msg_buffer.pop_all() + if self.recovered: + news = [self.latest_observed_msg] if self.latest_observed_msg else [] + else: + self.latest_observed_msg = news[-1] if len(news) > 0 else None # record the latest observed msg + # Store the read messages in your own memory to prevent duplicate processing. old_messages = [] if ignore_memory else self._rc.memory.get() self._rc.memory.add_batch(news) # Filter out messages of interest. - self._rc.news = [n for n in news if n.cause_by in self._rc.watch and n not in old_messages] + self._rc.news = self._find_news(news, old_messages) # Design Rules: # If you need to further categorize Message objects, you can do so using the Message.set_meta function. @@ -484,6 +499,7 @@ class Role(BaseModel): """A wrapper to return the most recent k memories of this role, return all when k=0""" return self._rc.memory.get(k=k) + @role_raise_decorator async def run(self, with_message=None): """Observe, and think and act based on the results of the observation""" if with_message: diff --git a/metagpt/schema.py b/metagpt/schema.py index 0ec9b5c60..0fdc24e02 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -26,7 +26,6 @@ from typing import Dict, List, Set, TypedDict, Optional, Any from pydantic import BaseModel, Field -from metagpt.actions import UserRequirement from metagpt.config import CONFIG from metagpt.const import ( MESSAGE_ROUTE_CAUSE_BY, @@ -118,8 +117,9 @@ class Message(BaseModel): ic_new = ic_obj(**ic["value"]) kwargs["instruct_content"] = ic_new - kwargs["id"] = uuid.uuid4().hex - kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", UserRequirement)) + kwargs["id"] = kwargs.get("id", uuid.uuid4().hex) + kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", + import_class("UserRequirement", "metagpt.actions.add_requirement"))) kwargs["sent_from"] = any_to_str(kwargs.get("sent_from", "")) kwargs["send_to"] = any_to_str_set(kwargs.get("send_to", {MESSAGE_ROUTE_TO_ALL})) super(Message, self).__init__(**kwargs) @@ -218,7 +218,7 @@ class MessageQueue(BaseModel): if key in kwargs: object.__setattr__(self, key, kwargs[key]) else: - object.__setattr__(self, key, self._private_attributes[key]) + object.__setattr__(self, key, Queue()) def pop(self) -> Message | None: """Pop one message from the queue.""" diff --git a/metagpt/startup.py b/metagpt/startup.py index f930c386b..17eb26665 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -27,8 +27,10 @@ def startup( reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), max_auto_summarize_code: int = typer.Option( default=-1, - help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating unlimited. This parameter is used for debugging the workflow.", + help="The maximum number of times the 'SummarizeCode' action is automatically invoked, " + "with -1 indicating unlimited. This parameter is used for debugging the workflow.", ), + recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage") ): """Run a startup. Be a boss.""" from metagpt.roles import ( @@ -50,20 +52,29 @@ def startup( CONFIG.reqa_file = reqa_file CONFIG.max_auto_summarize_code = max_auto_summarize_code - company = Team() - company.hire( - [ - ProductManager(), - Architect(), - ProjectManager(), - ] - ) + if not recover_path: + company = Team() + company.hire( + [ + ProductManager(), + Architect(), + ProjectManager(), + ] + ) - if implement or code_review: - company.hire([Engineer(n_borg=5, use_code_review=code_review)]) + if implement or code_review: + company.hire([Engineer(n_borg=5, use_code_review=code_review)]) - if run_tests: - company.hire([QaEngineer()]) + if run_tests: + company.hire([QaEngineer()]) + else: + # # stg_path = SERDESER_PATH.joinpath("team") + stg_path = Path(recover_path) + if not stg_path.exists() or not str(stg_path).endswith("team"): + raise FileNotFoundError(f"{recover_path} not exists or not endswith `team`") + + company = Team.recover(stg_path=stg_path) + idea = company.idea # use original idea company.invest(investment) company.run_project(idea) diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py index 57da57b00..aa7c039c4 100644 --- a/metagpt/utils/utils.py +++ b/metagpt/utils/utils.py @@ -88,18 +88,15 @@ def role_raise_decorator(func): return await func(self, *args, **kwargs) except KeyboardInterrupt as kbi: logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") - if self._rc.env: - newest_msgs = self._rc.env.memory.get(1) - if len(newest_msgs) > 0: - self._rc.memory.delete(newest_msgs[0]) + if self.latest_observed_msg: + self._rc.memory.delete(self.latest_observed_msg) raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside except Exception as exp: - if self._rc.env: - newest_msgs = self._rc.env.memory.get(1) - if len(newest_msgs) > 0: - logger.warning("There is a exception in role's execution, in order to resume, " - "we delete the newest role communication message in the role's memory.") - self._rc.memory.delete(newest_msgs[0]) # remove newest msg of the role to make it observed again + if self.latest_observed_msg: + logger.warning("There is a exception in role's execution, in order to resume, " + "we delete the newest role communication message in the role's memory.") + # remove role newest observed msg to make it observed again + self._rc.memory.delete(self.latest_observed_msg) raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside return wrapper diff --git a/startup.py b/startup.py deleted file mode 100644 index c4928a1b5..000000000 --- a/startup.py +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from typing import Optional -import asyncio -import fire -from pathlib import Path - -from metagpt.roles import ( - Architect, - Engineer, - ProductManager, - ProjectManager, - QaEngineer, -) -from metagpt.team import Team - - -async def startup( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = False, - run_tests: bool = False, - implement: bool = True, - recover_path: Optional[str] = None, -): - """Run a startup. Be a boss.""" - if not recover_path: - company = Team() - company.hire( - [ - ProductManager(), - Architect(), - ProjectManager(), - ] - ) - - # if implement or code_review - if implement or code_review: - # developing features: implement the idea - company.hire([Engineer(n_borg=5, use_code_review=code_review)]) - - if run_tests: - # developing features: run tests on the spot and identify bugs - # (bug fixing capability comes soon!) - company.hire([QaEngineer()]) - else: - # # stg_path = SERDESER_PATH.joinpath("team") - stg_path = Path(recover_path) - if not stg_path.exists() or not str(stg_path).endswith("team"): - raise FileNotFoundError(f"{recover_path} not exists or not endswith `team`") - - company = Team.recover(stg_path=stg_path) - idea = company.idea # use original idea - - company.invest(investment) - company.start_project(idea) - await company.run(n_round=n_round) - - -def main( - idea: str, - investment: float = 3.0, - n_round: int = 5, - code_review: bool = True, - run_tests: bool = False, - implement: bool = True, - recover_path: str = None, -): - """ - We are a software startup comprised of AI. By investing in us, - you are empowering a future filled with limitless possibilities. - :param idea: Your innovative idea, such as "Creating a snake game." - :param investment: As an investor, you have the opportunity to contribute - a certain dollar amount to this AI company. - :param n_round: - :param code_review: Whether to use code review. - :param recover_path: recover the project from existing serialized storage - :return: - """ - asyncio.run(startup(idea, investment, n_round, code_review, run_tests, implement, recover_path)) - - -if __name__ == "__main__": - fire.Fire(main) diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index f25403dc0..87cf75caa 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -84,7 +84,7 @@ async def test_role_serdeser_interrupt(): logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") role_c.serialize(stg_path) - assert role_c._rc.memory.count() == 2 + assert role_c._rc.memory.count() == 1 new_role_a: Role = Role.deserialize(stg_path) assert new_role_a._rc.state == 1 diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index 01e0a6c70..e87df9b52 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -106,11 +106,23 @@ async def test_team_recover_multi_roles_save(): stg_path = SERDESER_PATH.joinpath("team") shutil.rmtree(stg_path, ignore_errors=True) + role_a = RoleA() + role_b = RoleB() + + assert role_a.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleA", + "RoleA"} + assert role_b.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleB", + "RoleB"} + assert role_b._rc.watch == {"tests.metagpt.serialize_deserialize.test_serdeser_base.ActionPass"} + company = Team() - company.hire([RoleA(), RoleB()]) + company.hire([role_a, role_b]) company.run_project(idea) await company.run(n_round=4) new_company = Team.recover(stg_path) new_company.run_project(idea) + + assert new_company.env.get_role(role_b.profile)._rc.state == 1 + await new_company.run(n_round=4) From 32af743b36a8e31cf3c4a063a2869ea7da40a6f8 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 20 Dec 2023 10:54:49 +0800 Subject: [PATCH 350/413] rm metagpt/utils/utils.py --- metagpt/actions/action.py | 4 +- metagpt/environment.py | 3 +- metagpt/memory/memory.py | 3 +- .../postprecess/base_postprecess_plugin.py | 2 +- metagpt/roles/role.py | 3 +- metagpt/schema.py | 3 +- metagpt/team.py | 3 +- metagpt/utils/common.py | 99 ++++++++++++++++- metagpt/utils/repair_llm_raw_output.py | 2 +- metagpt/utils/serialize.py | 2 +- metagpt/utils/utils.py | 102 ------------------ .../serialize_deserialize/test_role.py | 2 +- 12 files changed, 109 insertions(+), 119 deletions(-) delete mode 100644 metagpt/utils/utils.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 8cba18945..9c7fb06e1 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -18,8 +18,8 @@ from metagpt.llm import LLM from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess -from metagpt.utils.common import OutputParser -from metagpt.utils.utils import general_after_log +from metagpt.utils.common import OutputParser, general_after_log + action_subclass_registry = {} diff --git a/metagpt/environment.py b/metagpt/environment.py index 9108cdf06..a3cbe6978 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -20,8 +20,7 @@ from pydantic import BaseModel, Field from metagpt.logs import logger from metagpt.roles.role import Role, role_subclass_registry from metagpt.schema import Message -from metagpt.utils.common import is_subscribed -from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.common import is_subscribed, read_json_file, write_json_file class Environment(BaseModel): diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 198c0970d..66ab5d4e9 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -14,8 +14,7 @@ from typing import Iterable, Set from pydantic import BaseModel, Field from metagpt.schema import Message -from metagpt.utils.common import any_to_str, any_to_str_set -from metagpt.utils.utils import read_json_file, write_json_file +from metagpt.utils.common import any_to_str, any_to_str_set, read_json_file, write_json_file class Memory(BaseModel): diff --git a/metagpt/provider/postprecess/base_postprecess_plugin.py b/metagpt/provider/postprecess/base_postprecess_plugin.py index 0d1cfbb11..afcef2531 100644 --- a/metagpt/provider/postprecess/base_postprecess_plugin.py +++ b/metagpt/provider/postprecess/base_postprecess_plugin.py @@ -44,7 +44,7 @@ class BasePostPrecessPlugin(object): def run_retry_parse_json_text(self, content: str) -> Union[dict, list]: """inherited class can re-implement the function""" - logger.info(f"extracted json CONTENT from output:\n{content}") + # logger.info(f"extracted json CONTENT from output:\n{content}") parsed_data = retry_parse_json_text(output=content) # should use output=content return parsed_data diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 09371ae08..efe3bcbd4 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -37,9 +37,8 @@ from metagpt.memory import Memory from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.human_provider import HumanProvider from metagpt.schema import Message, MessageQueue -from metagpt.utils.common import any_to_str +from metagpt.utils.common import any_to_str, read_json_file, write_json_file, import_class, role_raise_decorator from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output -from metagpt.utils.utils import read_json_file, write_json_file, import_class, role_raise_decorator PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ diff --git a/metagpt/schema.py b/metagpt/schema.py index 0fdc24e02..1c1fdd94d 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -36,10 +36,9 @@ from metagpt.const import ( TASK_FILE_REPO, ) from metagpt.logs import logger -from metagpt.utils.common import any_to_str, any_to_str_set +from metagpt.utils.common import any_to_str, any_to_str_set, import_class from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ actionoutput_str_to_mapping -from metagpt.utils.utils import import_class class RawMessage(TypedDict): diff --git a/metagpt/team.py b/metagpt/team.py index 30e3dc618..383f2da36 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -19,8 +19,7 @@ from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import NoMoneyException -from metagpt.utils.utils import read_json_file, write_json_file, serialize_decorator +from metagpt.utils.common import NoMoneyException, read_json_file, write_json_file, serialize_decorator class Team(BaseModel): diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index a9bdd6e2d..c909180cc 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -13,12 +13,21 @@ from __future__ import annotations import ast import contextlib +import importlib import inspect +import json import os import platform import re +import traceback +import typing +from pathlib import Path +from typing import Any from typing import List, Tuple, Union +from pydantic.json import pydantic_encoder +from tenacity import _utils + from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger @@ -184,7 +193,7 @@ class OutputParser: if start_index != -1 and end_index != -1: # Extract the structure part - structure_text = text[start_index : end_index + 1] + structure_text = text[start_index: end_index + 1] try: # Attempt to convert the text to a Python data type using ast.literal_eval @@ -363,3 +372,91 @@ def is_subscribed(message, tags): if t in message.send_to: return True return False + + +def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + def log_it(retry_state: "RetryCallState") -> None: + if retry_state.fn is None: + fn_name = "" + else: + fn_name = _utils.get_callback_name(retry_state.fn) + logger.error( + f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " + f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " + f"exp: {retry_state.outcome.exception()}" + ) + + return log_it + + +def read_json_file(json_file: str, encoding=None) -> list[Any]: + if not Path(json_file).exists(): + raise FileNotFoundError(f"json_file: {json_file} not exist, return []") + + with open(json_file, "r", encoding=encoding) as fin: + try: + data = json.load(fin) + except Exception as exp: + raise ValueError(f"read json file: {json_file} failed") + return data + + +def write_json_file(json_file: str, data: list, encoding=None): + folder_path = Path(json_file).parent + if not folder_path.exists(): + folder_path.mkdir(parents=True, exist_ok=True) + + with open(json_file, "w", encoding=encoding) as fout: + json.dump(data, fout, ensure_ascii=False, indent=4, default=pydantic_encoder) + + +def import_class(class_name: str, module_name: str) -> type: + module = importlib.import_module(module_name) + a_class = getattr(module, class_name) + return a_class + + +def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> object: + a_class = import_class(class_name, module_name) + class_inst = a_class(*args, **kwargs) + return class_inst + + +def format_trackback_info(limit: int = 2): + return traceback.format_exc(limit=limit) + + +def serialize_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + result = await func(self, *args, **kwargs) + self.serialize() # Team.serialize + return result + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + except Exception as exp: + logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") + self.serialize() # Team.serialize + + return wrapper + + +def role_raise_decorator(func): + async def wrapper(self, *args, **kwargs): + try: + return await func(self, *args, **kwargs) + except KeyboardInterrupt as kbi: + logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") + if self.latest_observed_msg: + self._rc.memory.delete(self.latest_observed_msg) + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside + except Exception as exp: + if self.latest_observed_msg: + logger.warning("There is a exception in role's execution, in order to resume, " + "we delete the newest role communication message in the role's memory.") + # remove role newest observed msg to make it observed again + self._rc.memory.delete(self.latest_observed_msg) + raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside + + return wrapper diff --git a/metagpt/utils/repair_llm_raw_output.py b/metagpt/utils/repair_llm_raw_output.py index 4aafd8e66..67ad4e963 100644 --- a/metagpt/utils/repair_llm_raw_output.py +++ b/metagpt/utils/repair_llm_raw_output.py @@ -253,7 +253,7 @@ def retry_parse_json_text(output: str) -> Union[list, dict]: if CONFIG.repair_llm_output is True, the _aask_v1 and the retry_parse_json_text will loop for {x=3*3} times. it's a two-layer retry cycle """ - logger.debug(f"output to json decode:\n{output}") + # logger.debug(f"output to json decode:\n{output}") # if CONFIG.repair_llm_output is True, it will try to fix output until the retry break parsed_data = CustomDecoder(strict=False).decode(output) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 93f584057..9a758da34 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -5,7 +5,7 @@ import copy import pickle -from metagpt.utils.utils import import_class +from metagpt.utils.common import import_class def actionoutout_schema_to_mapping(schema: dict) -> dict: diff --git a/metagpt/utils/utils.py b/metagpt/utils/utils.py deleted file mode 100644 index aa7c039c4..000000000 --- a/metagpt/utils/utils.py +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# @Desc : - -import typing -from typing import Any -import json -from pathlib import Path -import importlib -from tenacity import _utils -import traceback -from pydantic.json import pydantic_encoder - -from metagpt.logs import logger - - -def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: - def log_it(retry_state: "RetryCallState") -> None: - if retry_state.fn is None: - fn_name = "" - else: - fn_name = _utils.get_callback_name(retry_state.fn) - logger.error( - f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " - f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " - f"exp: {retry_state.outcome.exception()}" - ) - - return log_it - - -def read_json_file(json_file: str, encoding=None) -> list[Any]: - if not Path(json_file).exists(): - raise FileNotFoundError(f"json_file: {json_file} not exist, return []") - - with open(json_file, "r", encoding=encoding) as fin: - try: - data = json.load(fin) - except Exception as exp: - raise ValueError(f"read json file: {json_file} failed") - return data - - -def write_json_file(json_file: str, data: list, encoding=None): - folder_path = Path(json_file).parent - if not folder_path.exists(): - folder_path.mkdir(parents=True, exist_ok=True) - - with open(json_file, "w", encoding=encoding) as fout: - json.dump(data, fout, ensure_ascii=False, indent=4, default=pydantic_encoder) - - -def import_class(class_name: str, module_name: str) -> type: - module = importlib.import_module(module_name) - a_class = getattr(module, class_name) - return a_class - - -def import_class_inst(class_name: str, module_name: str, *args, **kwargs) -> object: - a_class = import_class(class_name, module_name) - class_inst = a_class(*args, **kwargs) - return class_inst - - -def format_trackback_info(limit: int = 2): - return traceback.format_exc(limit=limit) - - -def serialize_decorator(func): - async def wrapper(self, *args, **kwargs): - try: - result = await func(self, *args, **kwargs) - self.serialize() # Team.serialize - return result - except KeyboardInterrupt as kbi: - logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") - self.serialize() # Team.serialize - except Exception as exp: - logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") - self.serialize() # Team.serialize - - return wrapper - - -def role_raise_decorator(func): - async def wrapper(self, *args, **kwargs): - try: - return await func(self, *args, **kwargs) - except KeyboardInterrupt as kbi: - logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") - if self.latest_observed_msg: - self._rc.memory.delete(self.latest_observed_msg) - raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside - except Exception as exp: - if self.latest_observed_msg: - logger.warning("There is a exception in role's execution, in order to resume, " - "we delete the newest role communication message in the role's memory.") - # remove role newest observed msg to make it observed again - self._rc.memory.delete(self.latest_observed_msg) - raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside - - return wrapper diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 87cf75caa..88c7f7d8b 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -15,7 +15,7 @@ from metagpt.roles.engineer import Engineer from metagpt.roles.product_manager import ProductManager from metagpt.roles.role import Role from metagpt.schema import Message -from metagpt.utils.utils import format_trackback_info +from metagpt.utils.common import format_trackback_info from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path From b3750d5947894779fbaff392b242e722e57a05d6 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 11:52:11 +0800 Subject: [PATCH 351/413] refine code for prepare document. remove useless logic --- metagpt/actions/prepare_documents.py | 29 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 8d3445ae4..3c0885954 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -12,28 +12,29 @@ from pathlib import Path from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import DEFAULT_WORKSPACE_ROOT, DOCS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.schema import Document from metagpt.utils.file_repository import FileRepository from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) + """PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt.""" + + def _init_repo(self): + """Initialize the Git environment.""" + path = CONFIG.project_path + if not path: + name = CONFIG.project_name or FileRepository.new_filename() + path = Path(CONFIG.workspace_path) / name + + if path.exists() and not CONFIG.inc: + shutil.rmtree(path) + CONFIG.git_repo = GitRepository(local_path=path, auto_init=True) async def run(self, with_messages, **kwargs): - if not CONFIG.git_repo: - # Create and initialize the workspace folder, initialize the Git environment. - project_name = CONFIG.project_name or FileRepository.new_filename() - workdir = CONFIG.project_path - if not workdir and CONFIG.workspace_path: - workdir = Path(CONFIG.workspace_path) / project_name - workdir = Path(workdir or DEFAULT_WORKSPACE_ROOT / project_name) - if not CONFIG.inc and workdir.exists(): - shutil.rmtree(workdir) - CONFIG.git_repo = GitRepository() - CONFIG.git_repo.open(local_path=workdir, auto_init=True) + """Create and initialize the workspace folder, initialize the Git environment.""" + self._init_repo() # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. doc = Document(root_path=DOCS_FILE_REPO, filename=REQUIREMENT_FILENAME, content=with_messages[0].content) From f365348f49815c85fe4ca163647e66ad56ccd73f Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 11:59:59 +0800 Subject: [PATCH 352/413] add .pylintrc --- docs/.pylintrc | 639 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 639 insertions(+) create mode 100644 docs/.pylintrc diff --git a/docs/.pylintrc b/docs/.pylintrc new file mode 100644 index 000000000..9e8488bc7 --- /dev/null +++ b/docs/.pylintrc @@ -0,0 +1,639 @@ +[MAIN] + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Clear in-memory caches upon conclusion of linting. Useful if running pylint +# in a server-like mode. +clear-cache-post-run=no + +# Load and enable all available extensions. Use --list-extensions to see a list +# all available extensions. +#enable-all-extensions= + +# In error mode, messages with a category besides ERROR or FATAL are +# suppressed, and no reports are done by default. Error mode is compatible with +# disabling specific errors. +#errors-only= + +# Always return a 0 (non-error) status code, even if lint errors are found. +# This is primarily useful in continuous integration scripts. +#exit-zero= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist=pydantic + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold under which the program will exit with error. +fail-under=10 + +# Interpret the stdin as a python script, whose filename needs to be passed as +# the module_or_package argument. +#from-stdin= + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regular expressions patterns to the +# ignore-list. The regex matches against paths and can be in Posix or Windows +# format. Because '\\' represents the directory delimiter on Windows systems, +# it can't be used as an escape character. +ignore-paths= + +# Files or directories matching the regular expression patterns are skipped. +# The regex matches against base names, not paths. The default value ignores +# Emacs file locks +#ignore-patterns=^\.# +ignore-patterns=(.)*_test\.py,test_(.)*\.py + + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use, and will cap the count on Windows to +# avoid hangs. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=120 + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.9 + +# Discover python modules and packages in the file system subtree. +recursive=no + +# Add paths to the list of the source roots. Supports globbing patterns. The +# source root is an absolute path or a path relative to the current working +# directory used to determine a package namespace for modules located under the +# source root. +source-roots= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + +# In verbose mode, extra non-checker-related info will be displayed. +#verbose= + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. If left empty, argument names will be checked with the set +# naming style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. If left empty, attribute names will be checked with the set naming +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. If left empty, class attribute names will be checked +# with the set naming style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. If left empty, class constant names will be checked with +# the set naming style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. If left empty, class names will be checked with the set naming style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. If left empty, constant names will be checked with the set naming +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. If left empty, function names will be checked with the set +# naming style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + v, + e, + d, + m, + df, + ex, + Run, + _ + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. If left empty, inline iteration names will be checked +# with the set naming style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. If left empty, method names will be checked with the set naming style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. If left empty, module names will be checked with the set naming style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Regular expression matching correct type alias names. If left empty, type +# alias names will be checked with the set naming style. +#typealias-rgx= + +# Regular expression matching correct type variable names. If left empty, type +# variable names will be checked with the set naming style. +#typevar-rgx= + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. If left empty, variable names will be checked with the set +# naming style. +#variable-rgx= + + +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict,_fields,_replace,_source,_make,os._exit + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=mcs + + +[DESIGN] + +# List of regular expressions of class ancestor names to ignore when counting +# public methods (see R0903) +exclude-too-few-public-methods= + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement (see R0916). +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when caught. +overgeneral-exceptions=builtins.BaseException,builtins.Exception + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow explicit reexports by alias from a package __init__. +allow-reexport-from-package=no + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, +# UNDEFINED. +confidence=HIGH, + CONTROL_FLOW, + INFERENCE, + INFERENCE_FAILURE, + UNDEFINED + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then re-enable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + expression-not-assigned, + pointless-statement + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[METHOD_ARGS] + +# List of qualified names (i.e., library.method) which require a timeout +# parameter e.g. 'requests.api.get,requests.api.post' +timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.api.options,requests.api.patch,requests.api.post,requests.api.put,requests.api.request + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +notes-rgx= + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'fatal', 'error', 'warning', 'refactor', +# 'convention', and 'info' which contain the number of messages in each +# category, as well as 'statement' which is the total number of statements +# analyzed. This score is used by the global evaluation report (RP0004). +evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +#output-format= + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=yes + +# Signatures are removed from the similarity computation +ignore-signatures=yes + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. No available dictionaries : You need to install +# both the python package and the system dependency for enchant to work.. +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear at the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of symbolic message names to ignore for Mixin members. +ignored-checks-for-mixins=no-member, + not-async-context-manager, + not-context-manager, + attribute-defined-outside-init + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# Regex pattern to define which classes are considered mixins. +mixin-class-rgx=.*[Mm]ixin + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io From 1ab0ae99a90c54b6c8d104684a5127f91710e04c Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 12:48:57 +0800 Subject: [PATCH 353/413] refine sop --- metagpt/actions/write_prd_an.py | 21 ++++++++++++++------- metagpt/roles/product_manager.py | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index edd94a463..8698c739f 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -26,8 +26,8 @@ PROGRAMMING_LANGUAGE = ActionNode( ORIGINAL_REQUIREMENTS = ActionNode( key="Original Requirements", expected_type=str, - instruction="Place the polished, complete original requirements here.", - example="The game should have a leaderboard and multiple difficulty levels.", + instruction="Place the original user's requirements here.", + example="Create a 2048 game", ) PROJECT_NAME = ActionNode( @@ -41,7 +41,7 @@ PRODUCT_GOALS = ActionNode( key="Product Goals", expected_type=list[str], instruction="Provide up to three clear, orthogonal product goals.", - example=["Create an engaging user experience", "Ensure high performance", "Provide customizable features"], + example=["Create an engaging user experience", "Improve accessibility, be responsive", "More beautiful UI"], ) USER_STORIES = ActionNode( @@ -49,8 +49,11 @@ USER_STORIES = ActionNode( expected_type=list[str], instruction="Provide up to 3 to 5 scenario-based user stories.", example=[ - "As a user, I want to be able to choose difficulty levels", + "As a player, I want to be able to choose difficulty levels", "As a player, I want to see my score after each game", + "As a player, I want to get restart button when I lose", + "As a player, I want to see beautiful UI that make me feel good", + "As a player, I want to play game via mobile phone", ], ) @@ -58,7 +61,11 @@ COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", expected_type=list[str], instruction="Provide 5 to 7 competitive products.", - example=["Python Snake Game: Simple interface, lacks advanced features"], + example=[ + "2048 Game A: Simple interface, lacks responsive features", + "play2048.co: Beautiful and responsive UI with my best score shown", + "2048game.com: Responsive UI with my best score shown, but many ads", + ], ) COMPETITIVE_QUADRANT_CHART = ActionNode( @@ -86,7 +93,7 @@ REQUIREMENT_ANALYSIS = ActionNode( key="Requirement Analysis", expected_type=str, instruction="Provide a detailed analysis of the requirements.", - example="The product should be user-friendly.", + example="", ) REQUIREMENT_POOL = ActionNode( @@ -107,7 +114,7 @@ ANYTHING_UNCLEAR = ActionNode( key="Anything UNCLEAR", expected_type=str, instruction="Mention any aspects of the project that are unclear and try to clarify them.", - example="...", + example="", ) ISSUE_TYPE = ActionNode( diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 7858d2caa..61263cb50 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -28,8 +28,8 @@ class ProductManager(Role): self, name: str = "Alice", profile: str = "Product Manager", - goal: str = "efficiently create a successful product", - constraints: str = "use same language as user requirement", + goal: str = "efficiently create a successful product that meets market demands and user expectations", + constraints: str = "utilize the same language as the user requirements for seamless communication", ) -> None: """ Initializes the ProductManager role with given attributes. From de02894578a4adc5b4de404549d46d2291181899 Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Sun, 17 Dec 2023 13:52:37 +0800 Subject: [PATCH 354/413] patch release v0.5.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 730fffd35..73a05eeae 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.5.0", + version="0.5.1", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From e8a848a6145166ef39a7be1e2dd5f8cb4e05a733 Mon Sep 17 00:00:00 2001 From: shenchucheng Date: Sun, 17 Dec 2023 14:41:59 +0800 Subject: [PATCH 355/413] add deprecated warnings for the start_project method --- metagpt/team.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/metagpt/team.py b/metagpt/team.py index 383f2da36..9aa89ee2b 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -3,12 +3,13 @@ """ @Time : 2023/5/12 00:30 @Author : alexanderwu -@File : software_company.py +@File : team.py @Modified By: mashenquan, 2023/11/27. Add an archiving operation after completing the project, as specified in Section 2.2.3.3 of RFC 135. """ -from pathlib import Path +from pathlib import Path +import warnings from pydantic import BaseModel, Field from metagpt.actions import UserRequirement @@ -80,7 +81,7 @@ class Team(BaseModel): raise NoMoneyException(CONFIG.total_cost, f"Insufficient funds: {CONFIG.max_budget}") def run_project(self, idea, send_to: str = ""): - """Start a project from publishing user requirement.""" + """Run a project from publishing user requirement.""" self.idea = idea # Human requirement. @@ -88,6 +89,16 @@ class Team(BaseModel): Message(role="Human", content=idea, cause_by=UserRequirement, send_to=send_to or MESSAGE_ROUTE_TO_ALL) ) + def start_project(self, idea, send_to: str = ""): + """ + Deprecated: This method will be removed in the future. + Please use the `run_project` method instead. + """ + warnings.warn("The 'start_project' method is deprecated and will be removed in the future. " + "Please use the 'run_project' method instead.", + DeprecationWarning, stacklevel=2) + return self.run_project(idea=idea, send_to=send_to) + def _save(self): logger.info(self.json(ensure_ascii=False)) From 31f1be98a0aa95a94ae307186143a6258d901a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8E=98=E6=9D=83=20=E9=A9=AC?= Date: Mon, 18 Dec 2023 16:13:21 +0800 Subject: [PATCH 356/413] fixbug: recursive user requirement dead loop --- metagpt/roles/role.py | 9 +++++---- tests/metagpt/test_role.py | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index efe3bcbd4..3a8721004 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -27,15 +27,15 @@ from typing import Iterable, Set, Type, Any from pydantic import BaseModel, Field + from metagpt.actions.action import Action, ActionOutput, action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.const import SERDESER_PATH -from metagpt.llm import LLM +from metagpt.llm import LLM, HumanProvider from metagpt.logs import logger from metagpt.memory import Memory from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.provider.human_provider import HumanProvider from metagpt.schema import Message, MessageQueue from metagpt.utils.common import any_to_str, read_json_file, write_json_file, import_class, role_raise_decorator from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output @@ -293,8 +293,7 @@ class Role(BaseModel): """Watch Actions of interest. Role will select Messages caused by these Actions from its personal message buffer during _observe. """ - tags = {any_to_str(t) for t in actions} - self._rc.watch.update(tags) + self._rc.watch = {any_to_str(t) for t in actions} # check RoleContext after adding watch actions self._rc.check(self._role_id) @@ -509,6 +508,8 @@ class Role(BaseModel): msg = with_message elif isinstance(with_message, list): msg = Message(content="\n".join(with_message)) + if not msg.cause_by: + msg.cause_by = UserRequirement self.put_message(msg) if not await self._observe(): diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 8fac2503c..611d321fc 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -14,11 +14,11 @@ import uuid import pytest from pydantic import BaseModel -from metagpt.actions import Action, ActionOutput +from metagpt.actions import Action, ActionOutput, UserRequirement from metagpt.environment import Environment from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import get_class_name +from metagpt.utils.common import any_to_str, get_class_name class MockAction(Action): @@ -60,7 +60,7 @@ async def test_react(): name=seed.name, profile=seed.profile, goal=seed.goal, constraints=seed.constraints, desc=seed.desc ) role.subscribe({seed.subscription}) - assert role._rc.watch == set({}) + assert role._rc.watch == {any_to_str(UserRequirement)} assert role.name == seed.name assert role.profile == seed.profile assert role._setting.goal == seed.goal From f2e1053b489c2bedca3f05e2487c6913d31fb8f8 Mon Sep 17 00:00:00 2001 From: garylin2099 Date: Mon, 18 Dec 2023 19:26:38 +0800 Subject: [PATCH 357/413] update version and roadmap --- docs/ROADMAP.md | 8 ++++---- setup.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index afc9ff445..3cb03f374 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -30,10 +30,10 @@ ### Tasks 4. Complete the design and implementation of module breakdown 5. Support various modes of memory: clearly distinguish between long-term and short-term memory 6. Perfect the test role, and carry out necessary interactions with humans - 7. Allowing natural communication between roles (expected v0.5.0) + 7. ~~Allowing natural communication between roles~~ (v0.5.0) 8. Implement SkillManager and the process of incremental Skill learning (experimentation done with game agents) 9. Automatically get RPM and configure it by calling the corresponding openai page, so that each key does not need to be manually configured - 10. IMPORTANT: Support incremental development (expected v0.5.0) + 10. ~~IMPORTANT: Support incremental development~~ (v0.5.0) 3. Strategies 1. Support ReAct strategy (experimentation done with game agents) 2. Support CoT strategy (experimentation done with game agents) @@ -45,8 +45,8 @@ ### Tasks 2. Implementation: Knowledge search, supporting 10+ data formats 3. Implementation: Data EDA (expected v0.6.0) 4. Implementation: Review - 5. Implementation: Add Document (expected v0.5.0) - 6. Implementation: Delete Document (expected v0.5.0) + 5. ~~Implementation~~: Add Document (v0.5.0) + 6. ~~Implementation~~: Delete Document (v0.5.0) 7. Implementation: Self-training 8. ~~Implementation: DebugError~~ (v0.2.1) 9. Implementation: Generate reliable unit tests based on YAPI diff --git a/setup.py b/setup.py index 73a05eeae..57290f4cd 100644 --- a/setup.py +++ b/setup.py @@ -30,7 +30,7 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", - version="0.5.1", + version="0.5.2", description="The Multi-Role Meta Programming Framework", long_description=long_description, long_description_content_type="text/markdown", From 548e6d5f25d6263f471b3f6a76ffd1749a2213f7 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 10:52:16 +0800 Subject: [PATCH 358/413] remove requirements-ocr.txt and place the optional setup to setup.py --- requirements-ocr.txt | 4 ---- setup.py | 1 + 2 files changed, 1 insertion(+), 4 deletions(-) delete mode 100644 requirements-ocr.txt diff --git a/requirements-ocr.txt b/requirements-ocr.txt deleted file mode 100644 index cf6103afc..000000000 --- a/requirements-ocr.txt +++ /dev/null @@ -1,4 +0,0 @@ -paddlepaddle==2.4.2 -paddleocr>=2.0.1 -tabulate==0.9.0 --r requirements.txt diff --git a/setup.py b/setup.py index 57290f4cd..a06530015 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ setup( "search-google": ["google-api-python-client==2.94.0"], "search-ddg": ["duckduckgo-search==3.8.5"], "pyppeteer": ["pyppeteer>=1.0.2"], + "ocr": ["paddlepaddle==2.4.2", "paddleocr>=2.0.1", "tabulate==0.9.0"], }, cmdclass={ "install_mermaid": InstallMermaidCLI, From 4e6d1a00f87378a04465d43e81d248c7219447cf Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:01:20 +0800 Subject: [PATCH 359/413] use pre-commit --- metagpt/actions/action_node.py | 12 ++++++++++-- metagpt/actions/project_management_an.py | 2 +- metagpt/roles/project_manager.py | 1 + 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index fb7d621d8..9bb12fc84 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -52,6 +52,7 @@ def dict_to_markdown(d, prefix="-", postfix="\n"): class ActionNode: """ActionNode is a tree of nodes.""" + mode: str # Action Context @@ -70,8 +71,15 @@ class ActionNode: content: str instruct_content: BaseModel - def __init__(self, key: str, expected_type: Type, instruction: str, example: str, content: str = "", - children: dict[str, "ActionNode"] = None): + def __init__( + self, + key: str, + expected_type: Type, + instruction: str, + example: str, + content: str = "", + children: dict[str, "ActionNode"] = None, + ): self.key = key self.expected_type = expected_type self.instruction = instruction diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index 970cb0594..6208c1051 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -44,7 +44,7 @@ FULL_API_SPEC = ActionNode( key="Full API spec", expected_type=str, instruction="Describe all APIs using OpenAPI 3.0 spec that may be used by both frontend and backend. If front-end " - "and back-end communication is not required, leave it blank.", + "and back-end communication is not required, leave it blank.", example="openapi: 3.0.0 ...", ) diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index f98d28cb7..42564cd70 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -30,5 +30,6 @@ class ProjectManager(Role): def __init__(self, **kwargs) -> None: super().__init__(**kwargs) + self._init_actions([WriteTasks]) self._watch([WriteDesign]) From b14b3f4dd9e4a3d4fd2ffef85871e483c61677ca Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:10:17 +0800 Subject: [PATCH 360/413] setup.py: update --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index a06530015..8ef2a6946 100644 --- a/setup.py +++ b/setup.py @@ -31,14 +31,14 @@ with open(path.join(here, "requirements.txt"), encoding="utf-8") as f: setup( name="metagpt", version="0.5.2", - description="The Multi-Role Meta Programming Framework", + description="The Multi-Agent Framework", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/geekan/MetaGPT", author="Alexander Wu", author_email="alexanderwu@deepwisdom.ai", license="MIT", - keywords="metagpt multi-role multi-agent programming gpt llm metaprogramming", + keywords="metagpt multi-agent multi-role programming gpt llm metaprogramming", packages=find_packages(exclude=["contrib", "docs", "examples", "tests*"]), python_requires=">=3.9", install_requires=requirements, From 2296aea055be706a3d80c2441410aec2f6cd97c9 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:22:21 +0800 Subject: [PATCH 361/413] delete inspect_module.py because we have ast tree parser --- metagpt/inspect_module.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 metagpt/inspect_module.py diff --git a/metagpt/inspect_module.py b/metagpt/inspect_module.py deleted file mode 100644 index 48ceffc57..000000000 --- a/metagpt/inspect_module.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/28 14:54 -@Author : alexanderwu -@File : inspect_module.py -""" - -import inspect - -import metagpt # replace with your module - - -def print_classes_and_functions(module): - """FIXME: NOT WORK..""" - for name, obj in inspect.getmembers(module): - if inspect.isclass(obj): - print(f"Class: {name}") - elif inspect.isfunction(obj): - print(f"Function: {name}") - else: - print(name) - - print(dir(module)) - - -if __name__ == "__main__": - print_classes_and_functions(metagpt) From f371e3a49979e87be1ce64b23b5d094b102cd271 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:49:06 +0800 Subject: [PATCH 362/413] token_counter: add gpt-3.5-turbo-16k in list and add comment for them --- metagpt/utils/token_counter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/token_counter.py b/metagpt/utils/token_counter.py index 266a53268..ebfb85de7 100644 --- a/metagpt/utils/token_counter.py +++ b/metagpt/utils/token_counter.py @@ -56,6 +56,7 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): if model in { "gpt-3.5-turbo-0613", "gpt-3.5-turbo-16k-0613", + "gpt-3.5-turbo-16k", "gpt-3.5-turbo-1106", "gpt-4-0314", "gpt-4-32k-0314", @@ -63,7 +64,7 @@ def count_message_tokens(messages, model="gpt-3.5-turbo-0613"): "gpt-4-32k-0613", "gpt-4-1106-preview", }: - tokens_per_message = 3 + tokens_per_message = 3 # # every reply is primed with <|start|>assistant<|message|> tokens_per_name = 1 elif model == "gpt-3.5-turbo-0301": tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n From e8cb7991c447ff9e24303111b435ef0c1ebe7051 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 11:52:23 +0800 Subject: [PATCH 363/413] openai_api: refine logic --- metagpt/provider/openai_api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index a73bb0aa0..86054881e 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -329,7 +329,8 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): usage["completion_tokens"] = completion_tokens return usage except Exception as e: - logger.error("usage calculation failed!", e) + logger.error(f"{self.model} usage calculation failed!", e) + return {} else: return usage @@ -360,7 +361,7 @@ class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): return results def _update_costs(self, usage: dict): - if CONFIG.calc_usage: + if CONFIG.calc_usage and usage: try: prompt_tokens = int(usage["prompt_tokens"]) completion_tokens = int(usage["completion_tokens"]) From f71753ba0dc7fcfacc3456755a0fa6a19d7b8374 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 13:51:51 +0800 Subject: [PATCH 364/413] add function import, avoid "import" --- metagpt/utils/common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index c909180cc..6301cd6a3 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -25,8 +25,9 @@ from pathlib import Path from typing import Any from typing import List, Tuple, Union +import loguru from pydantic.json import pydantic_encoder -from tenacity import _utils +from tenacity import RetryCallState, _utils from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger From 8f649252900a8f1e7977cdf2eea8da9a8d4518dc Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 14:17:54 +0800 Subject: [PATCH 365/413] refine utils code --- metagpt/utils/common.py | 51 ++++++++++++++++++++++++------------ tests/metagpt/test_role.py | 8 +++--- tests/metagpt/test_schema.py | 9 +++---- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 6301cd6a3..08df480ee 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -301,9 +301,6 @@ class NoMoneyException(Exception): def print_members(module, indent=0): """ https://stackoverflow.com/questions/1796180/how-can-i-get-a-list-of-all-classes-within-current-module-in-python - :param module: - :param indent: - :return: """ prefix = " " * indent for name, obj in inspect.getmembers(module): @@ -321,6 +318,7 @@ def print_members(module, indent=0): def parse_recipient(text): + # FIXME: use ActionNode instead. pattern = r"## Send To:\s*([A-Za-z]+)\s*?" # hard code for now recipient = re.search(pattern, text) if recipient: @@ -337,18 +335,12 @@ def get_class_name(cls) -> str: return f"{cls.__module__}.{cls.__name__}" -def get_object_name(obj) -> str: - """Return class name of the object""" - cls = type(obj) - return f"{cls.__module__}.{cls.__name__}" - - -def any_to_str(val) -> str: +def any_to_str(val: str | typing.Callable) -> str: """Return the class name or the class name of the object, or 'val' if it's a string type.""" if isinstance(val, str): return val if not callable(val): - return get_object_name(val) + return get_class_name(type(val)) return get_class_name(val) @@ -356,32 +348,57 @@ def any_to_str(val) -> str: def any_to_str_set(val) -> set: """Convert any type to string set.""" res = set() - if isinstance(val, dict) or isinstance(val, list) or isinstance(val, set) or isinstance(val, tuple): + + # Check if the value is iterable, but not a string (since strings are technically iterable) + if isinstance(val, (dict, list, set, tuple)): + # Special handling for dictionaries to iterate over values + if isinstance(val, dict): + val = val.values() + for i in val: res.add(any_to_str(i)) else: res.add(any_to_str(val)) + return res -def is_subscribed(message, tags): +def is_subscribed(message: "Message", tags: set): """Return whether it's consumer""" if MESSAGE_ROUTE_TO_ALL in message.send_to: return True - for t in tags: - if t in message.send_to: + for i in tags: + if i in message.send_to: return True return False -def general_after_log(logger: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: +def general_after_log(i: "loguru.Logger", sec_format: str = "%0.3f") -> typing.Callable[["RetryCallState"], None]: + """ + Generates a logging function to be used after a call is retried. + + This generated function logs an error message with the outcome of the retried function call. It includes + the name of the function, the time taken for the call in seconds (formatted according to `sec_format`), + the number of attempts made, and the exception raised, if any. + + :param i: A Logger instance from the loguru library used to log the error message. + :param sec_format: A string format specifier for how to format the number of seconds since the start of the call. + Defaults to three decimal places. + :return: A callable that accepts a RetryCallState object and returns None. This callable logs the details + of the retried call. + """ + def log_it(retry_state: "RetryCallState") -> None: + # If the function name is not known, default to "" if retry_state.fn is None: fn_name = "" else: + # Retrieve the callable's name using a utility function fn_name = _utils.get_callback_name(retry_state.fn) - logger.error( + + # Log an error message with the function name, time since start, attempt number, and the exception + i.error( f"Finished call to '{fn_name}' after {sec_format % retry_state.seconds_since_start}(s), " f"this was the {_utils.to_ordinal(retry_state.attempt_number)} time calling it. " f"exp: {retry_state.outcome.exception()}" diff --git a/tests/metagpt/test_role.py b/tests/metagpt/test_role.py index 611d321fc..dbe45130d 100644 --- a/tests/metagpt/test_role.py +++ b/tests/metagpt/test_role.py @@ -18,7 +18,7 @@ from metagpt.actions import Action, ActionOutput, UserRequirement from metagpt.environment import Environment from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import any_to_str, get_class_name +from metagpt.utils.common import any_to_str class MockAction(Action): @@ -88,13 +88,13 @@ async def test_react(): @pytest.mark.asyncio async def test_msg_to(): m = Message(content="a", send_to=["a", MockRole, Message]) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} m = Message(content="a", cause_by=MockAction, send_to={"a", MockRole, Message}) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} m = Message(content="a", send_to=("a", MockRole, Message)) - assert m.send_to == set({"a", get_class_name(MockRole), get_class_name(Message)}) + assert m.send_to == {"a", any_to_str(MockRole), any_to_str(Message)} if __name__ == "__main__": diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 10343c192..c8602d953 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -16,8 +16,7 @@ from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.actions.action_output import ActionOutput from metagpt.actions.write_code import WriteCode from metagpt.utils.serialize import serialize_general_message, deserialize_general_message - -from metagpt.utils.common import get_class_name +from metagpt.utils.common import any_to_str @pytest.mark.asyncio @@ -58,9 +57,9 @@ def test_message(): m.cause_by = "Message" assert m.cause_by == "Message" m.cause_by = Action - assert m.cause_by == get_class_name(Action) + assert m.cause_by == any_to_str(Action) m.cause_by = Action() - assert m.cause_by == get_class_name(Action) + assert m.cause_by == any_to_str(Action) m.content = "b" assert m.content == "b" @@ -71,7 +70,7 @@ def test_routes(): m.send_to = "b" assert m.send_to == {"b"} m.send_to = {"e", Action} - assert m.send_to == {"e", get_class_name(Action)} + assert m.send_to == {"e", any_to_str(Action)} def test_message_serdeser(): From 5c341cb05383685c3d4403d22495850af33c8b3f Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:16:52 +0800 Subject: [PATCH 366/413] refine code: use handle_exception function instead of in-function duplicate code frags --- metagpt/actions/action_node.py | 2 +- metagpt/actions/run_code.py | 30 ++++----- metagpt/config.py | 1 + metagpt/repo_parser.py | 19 ++++-- metagpt/schema.py | 78 ++++++++-------------- metagpt/tools/search_engine_meilisearch.py | 12 ++-- metagpt/utils/common.py | 10 +++ metagpt/utils/custom_decoder.py | 2 +- metagpt/utils/dependency_file.py | 20 ++---- metagpt/utils/exceptions.py | 59 ++++++++++++++++ metagpt/utils/file.py | 45 ++++++------- metagpt/utils/file_repository.py | 11 +-- 12 files changed, 159 insertions(+), 130 deletions(-) create mode 100644 metagpt/utils/exceptions.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 9bb12fc84..6f1215920 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -43,7 +43,7 @@ Fill in the above nodes based on the format example. """ -def dict_to_markdown(d, prefix="-", postfix="\n"): +def dict_to_markdown(d, prefix="###", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index fa13a0980..1b9fd252f 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -16,13 +16,13 @@ class. """ import subprocess -import traceback from typing import Tuple from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import RunCodeResult +from metagpt.utils.exceptions import handle_exception PROMPT_TEMPLATE = """ Role: You are a senior development and qa engineer, your role is summarize the code running result. @@ -78,15 +78,12 @@ class RunCode(Action): super().__init__(name, context, llm) @classmethod + @handle_exception async def run_text(cls, code) -> Tuple[str, str]: - try: - # We will document_store the result in this dictionary - namespace = {} - exec(code, namespace) - return namespace.get("result", ""), "" - except Exception: - # If there is an error in the code, return the error message - return "", traceback.format_exc() + # We will document_store the result in this dictionary + namespace = {} + exec(code, namespace) + return namespace.get("result", ""), "" @classmethod async def run_script(cls, working_directory, additional_python_paths=[], command=[]) -> Tuple[str, str]: @@ -145,18 +142,17 @@ class RunCode(Action): rsp = await self._aask(prompt) return RunCodeResult(summary=rsp, stdout=outs, stderr=errs) + @staticmethod + @handle_exception(exception_type=subprocess.CalledProcessError) + def _install_via_subprocess(cmd, check, cwd, env): + return subprocess.run(cmd, check=check, cwd=cwd, env=env) + @staticmethod def _install_dependencies(working_directory, env): install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"] logger.info(" ".join(install_command)) - try: - subprocess.run(install_command, check=True, cwd=working_directory, env=env) - except subprocess.CalledProcessError as e: - logger.warning(f"{e}") + RunCode._install_via_subprocess(install_command, check=True, cwd=working_directory, env=env) install_pytest_command = ["python", "-m", "pip", "install", "pytest"] logger.info(" ".join(install_pytest_command)) - try: - subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env) - except subprocess.CalledProcessError as e: - logger.warning(f"{e}") + RunCode._install_via_subprocess(install_pytest_command, check=True, cwd=working_directory, env=env) diff --git a/metagpt/config.py b/metagpt/config.py index d7f5c1249..d6e6d8b88 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -139,6 +139,7 @@ class Config(metaclass=Singleton): continue configs.update(yaml_data) OPTIONS.set(configs) + logger.info(f"Default OpenAI API Model: {self.openai_api_model}") @staticmethod def _get(*args, **kwargs): diff --git a/metagpt/repo_parser.py b/metagpt/repo_parser.py index b84dbab9a..9a1218ef1 100644 --- a/metagpt/repo_parser.py +++ b/metagpt/repo_parser.py @@ -15,17 +15,17 @@ from pydantic import BaseModel, Field from metagpt.config import CONFIG from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception class RepoParser(BaseModel): base_directory: Path = Field(default=None) - def parse_file(self, file_path): + @classmethod + @handle_exception(exception_type=Exception, default_return=[]) + def _parse_file(cls, file_path: Path) -> list: """Parse a Python file in the repository.""" - try: - return ast.parse(file_path.read_text()).body - except: - return [] + return ast.parse(file_path.read_text()).body def extract_class_and_function_info(self, tree, file_path): """Extract class, function, and global variable information from the AST.""" @@ -52,7 +52,7 @@ class RepoParser(BaseModel): files_classes = [] directory = self.base_directory for path in directory.rglob("*.py"): - tree = self.parse_file(path) + tree = self._parse_file(path) file_info = self.extract_class_and_function_info(tree, path) files_classes.append(file_info) @@ -90,5 +90,10 @@ def main(): logger.info(pformat(symbols)) +def error(): + """raise Exception and logs it""" + RepoParser._parse_file(Path("test.py")) + + if __name__ == "__main__": - main() + error() diff --git a/metagpt/schema.py b/metagpt/schema.py index 1c1fdd94d..c026ea1d9 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -22,7 +22,7 @@ import uuid from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path -from typing import Dict, List, Set, TypedDict, Optional, Any +from typing import Dict, List, Optional, Set, Type, TypedDict, TypeVar, Any from pydantic import BaseModel, Field @@ -39,6 +39,7 @@ from metagpt.logs import logger from metagpt.utils.common import any_to_str, any_to_str_set, import_class from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ actionoutput_str_to_mapping +from metagpt.utils.exceptions import handle_exception class RawMessage(TypedDict): @@ -163,14 +164,11 @@ class Message(BaseModel): return self.json(exclude_none=True) @staticmethod + @handle_exception(exception_type=JSONDecodeError, default_return=None) def load(val): """Convert the json string to object.""" - try: - d = json.loads(val) - return Message(**d) - except JSONDecodeError as err: - logger.error(f"parse json failed: {val}, error:{err}") - return None + d = json.loads(val) + return Message(**d) class UserMessage(Message): @@ -265,50 +263,46 @@ class MessageQueue(BaseModel): return json.dumps(lst) @staticmethod - def load(self, v) -> MessageQueue: + def load(i) -> "MessageQueue": """Convert the json string to the `MessageQueue` object.""" - q = MessageQueue() + queue = MessageQueue() try: - lst = json.loads(v) + lst = json.loads(i) for i in lst: msg = Message(**i) - q.push(msg) + queue.push(msg) except JSONDecodeError as e: - logger.warning(f"JSON load failed: {v}, error:{e}") + logger.warning(f"JSON load failed: {i}, error:{e}") - return q + return queue -class CodingContext(BaseModel): +# 定义一个泛型类型变量 +T = TypeVar("T", bound="BaseModel") + + +class BaseContext(BaseModel): + @staticmethod + @handle_exception + def loads(val: str, cls: Type[T]) -> Optional[T]: + m = json.loads(val) + return cls(**m) + + +class CodingContext(BaseContext): filename: str design_doc: Optional[Document] task_doc: Optional[Document] code_doc: Optional[Document] - @staticmethod - def loads(val: str) -> CodingContext | None: - try: - m = json.loads(val) - return CodingContext(**m) - except Exception: - return None - -class TestingContext(BaseModel): +class TestingContext(BaseContext): filename: str code_doc: Document test_doc: Optional[Document] - @staticmethod - def loads(val: str) -> TestingContext | None: - try: - m = json.loads(val) - return TestingContext(**m) - except Exception: - return None - -class RunCodeContext(BaseModel): +class RunCodeContext(BaseContext): mode: str = "script" code: Optional[str] code_filename: str = "" @@ -320,28 +314,12 @@ class RunCodeContext(BaseModel): output_filename: Optional[str] output: Optional[str] - @staticmethod - def loads(val: str) -> RunCodeContext | None: - try: - m = json.loads(val) - return RunCodeContext(**m) - except Exception: - return None - -class RunCodeResult(BaseModel): +class RunCodeResult(BaseContext): summary: str stdout: str stderr: str - @staticmethod - def loads(val: str) -> RunCodeResult | None: - try: - m = json.loads(val) - return RunCodeResult(**m) - except Exception: - return None - class CodeSummarizeContext(BaseModel): design_filename: str = "" @@ -365,5 +343,5 @@ class CodeSummarizeContext(BaseModel): return hash((self.design_filename, self.task_filename)) -class BugFixContext(BaseModel): +class BugFixContext(BaseContext): filename: str = "" diff --git a/metagpt/tools/search_engine_meilisearch.py b/metagpt/tools/search_engine_meilisearch.py index f7c1c685a..ea6db4dbd 100644 --- a/metagpt/tools/search_engine_meilisearch.py +++ b/metagpt/tools/search_engine_meilisearch.py @@ -11,6 +11,8 @@ from typing import List import meilisearch from meilisearch.index import Index +from metagpt.utils.exceptions import handle_exception + class DataSource: def __init__(self, name: str, url: str): @@ -34,11 +36,7 @@ class MeilisearchEngine: index.add_documents(documents) self.set_index(index) + @handle_exception(exception_type=Exception, default_return=[]) def search(self, query): - try: - search_results = self._index.search(query) - return search_results["hits"] - except Exception as e: - # Handle MeiliSearch API errors - print(f"MeiliSearch API error: {e}") - return [] + search_results = self._index.search(query) + return search_results["hits"] diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 08df480ee..0060950dc 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -25,12 +25,14 @@ from pathlib import Path from typing import Any from typing import List, Tuple, Union +import aiofiles import loguru from pydantic.json import pydantic_encoder from tenacity import RetryCallState, _utils from metagpt.const import MESSAGE_ROUTE_TO_ALL from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception def check_cmd_exists(command) -> int: @@ -478,3 +480,11 @@ def role_raise_decorator(func): raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside return wrapper + + +@handle_exception +async def aread(file_path: str) -> str: + """Read file asynchronously.""" + async with aiofiles.open(str(file_path), mode="r") as reader: + content = await reader.read() + return content diff --git a/metagpt/utils/custom_decoder.py b/metagpt/utils/custom_decoder.py index 373d16356..eb01a1115 100644 --- a/metagpt/utils/custom_decoder.py +++ b/metagpt/utils/custom_decoder.py @@ -25,7 +25,7 @@ def py_make_scanner(context): except IndexError: raise StopIteration(idx) from None - if nextchar == '"' or nextchar == "'": + if nextchar in ("'", '"'): if idx + 2 < len(string) and string[idx + 1] == nextchar and string[idx + 2] == nextchar: # Handle the case where the next two characters are the same as nextchar return parse_string(string, idx + 3, strict, delimiter=nextchar * 3) # triple quote diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index e8347d567..d03444f0e 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -15,7 +15,8 @@ from typing import Set import aiofiles from metagpt.config import CONFIG -from metagpt.logs import logger +from metagpt.utils.common import aread +from metagpt.utils.exceptions import handle_exception class DependencyFile: @@ -36,21 +37,14 @@ class DependencyFile: """Load dependencies from the file asynchronously.""" if not self._filename.exists(): return - try: - async with aiofiles.open(str(self._filename), mode="r") as reader: - data = await reader.read() - self._dependencies = json.loads(data) - except Exception as e: - logger.error(f"Failed to load {str(self._filename)}, error:{e}") + self._dependencies = await aread(self._filename) + @handle_exception async def save(self): """Save dependencies to the file asynchronously.""" - try: - data = json.dumps(self._dependencies) - async with aiofiles.open(str(self._filename), mode="w") as writer: - await writer.write(data) - except Exception as e: - logger.error(f"Failed to save {str(self._filename)}, error:{e}") + data = json.dumps(self._dependencies) + async with aiofiles.open(str(self._filename), mode="w") as writer: + await writer.write(data) async def update(self, filename: Path | str, dependencies: Set[Path | str], persist=True): """Update dependencies for a file asynchronously. diff --git a/metagpt/utils/exceptions.py b/metagpt/utils/exceptions.py new file mode 100644 index 000000000..b4b5aa590 --- /dev/null +++ b/metagpt/utils/exceptions.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/19 14:46 +@Author : alexanderwu +@File : exceptions.py +""" + + +import asyncio +import functools +import traceback +from typing import Any, Callable, Tuple, Type, TypeVar, Union + +from metagpt.logs import logger + +ReturnType = TypeVar("ReturnType") + + +def handle_exception( + _func: Callable[..., ReturnType] = None, + *, + exception_type: Union[Type[Exception], Tuple[Type[Exception], ...]] = Exception, + default_return: Any = None, +) -> Callable[..., ReturnType]: + """handle exception, return default value""" + + def decorator(func: Callable[..., ReturnType]) -> Callable[..., ReturnType]: + @functools.wraps(func) + async def async_wrapper(*args: Any, **kwargs: Any) -> ReturnType: + try: + return await func(*args, **kwargs) + except exception_type as e: + logger.opt(depth=1).error( + f"Calling {func.__name__} with args: {args}, kwargs: {kwargs} failed: {e}, " + f"stack: {traceback.format_exc()}" + ) + return default_return + + @functools.wraps(func) + def sync_wrapper(*args: Any, **kwargs: Any) -> ReturnType: + try: + return func(*args, **kwargs) + except exception_type as e: + logger.opt(depth=1).error( + f"Calling {func.__name__} with args: {args}, kwargs: {kwargs} failed: {e}, " + f"stack: {traceback.format_exc()}" + ) + return default_return + + if asyncio.iscoroutinefunction(func): + return async_wrapper + else: + return sync_wrapper + + if _func is None: + return decorator + else: + return decorator(_func) diff --git a/metagpt/utils/file.py b/metagpt/utils/file.py index 6bb9a1a97..f62b44eb8 100644 --- a/metagpt/utils/file.py +++ b/metagpt/utils/file.py @@ -11,6 +11,7 @@ from pathlib import Path import aiofiles from metagpt.logs import logger +from metagpt.utils.exceptions import handle_exception class File: @@ -19,6 +20,7 @@ class File: CHUNK_SIZE = 64 * 1024 @classmethod + @handle_exception async def write(cls, root_path: Path, filename: str, content: bytes) -> Path: """Write the file content to the local specified path. @@ -33,18 +35,15 @@ class File: Raises: Exception: If an unexpected error occurs during the file writing process. """ - try: - root_path.mkdir(parents=True, exist_ok=True) - full_path = root_path / filename - async with aiofiles.open(full_path, mode="wb") as writer: - await writer.write(content) - logger.debug(f"Successfully write file: {full_path}") - return full_path - except Exception as e: - logger.error(f"Error writing file: {e}") - raise e + root_path.mkdir(parents=True, exist_ok=True) + full_path = root_path / filename + async with aiofiles.open(full_path, mode="wb") as writer: + await writer.write(content) + logger.debug(f"Successfully write file: {full_path}") + return full_path @classmethod + @handle_exception async def read(cls, file_path: Path, chunk_size: int = None) -> bytes: """Partitioning read the file content from the local specified path. @@ -58,18 +57,14 @@ class File: Raises: Exception: If an unexpected error occurs during the file reading process. """ - try: - chunk_size = chunk_size or cls.CHUNK_SIZE - async with aiofiles.open(file_path, mode="rb") as reader: - chunks = list() - while True: - chunk = await reader.read(chunk_size) - if not chunk: - break - chunks.append(chunk) - content = b"".join(chunks) - logger.debug(f"Successfully read file, the path of file: {file_path}") - return content - except Exception as e: - logger.error(f"Error reading file: {e}") - raise e + chunk_size = chunk_size or cls.CHUNK_SIZE + async with aiofiles.open(file_path, mode="rb") as reader: + chunks = list() + while True: + chunk = await reader.read(chunk_size) + if not chunk: + break + chunks.append(chunk) + content = b"".join(chunks) + logger.debug(f"Successfully read file, the path of file: {file_path}") + return content diff --git a/metagpt/utils/file_repository.py b/metagpt/utils/file_repository.py index 2eca799a8..099556a6b 100644 --- a/metagpt/utils/file_repository.py +++ b/metagpt/utils/file_repository.py @@ -19,6 +19,7 @@ import aiofiles from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.schema import Document +from metagpt.utils.common import aread from metagpt.utils.json_to_markdown import json_to_markdown @@ -97,15 +98,7 @@ class FileRepository: path_name = self.workdir / filename if not path_name.exists(): return None - try: - async with aiofiles.open(str(path_name), mode="r") as reader: - doc.content = await reader.read() - except FileNotFoundError as e: - logger.info(f"open {str(path_name)} failed:{e}") - return None - except Exception as e: - logger.info(f"open {str(path_name)} failed:{e}") - return None + doc.content = await aread(path_name) return doc async def get_all(self) -> List[Document]: From 437abd1754603a6037fce7f2d1c8cbaa46c56116 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:22:29 +0800 Subject: [PATCH 367/413] bug fix and proper log --- metagpt/config.py | 3 +-- metagpt/utils/dependency_file.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index d6e6d8b88..5f2be971a 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -76,7 +76,7 @@ class Config(metaclass=Singleton): self.openai_api_type = self._get("OPENAI_API_TYPE") self.openai_api_version = self._get("OPENAI_API_VERSION") self.openai_api_rpm = self._get("RPM", 3) - self.openai_api_model = self._get("OPENAI_API_MODEL", "gpt-4") + self.openai_api_model = self._get("OPENAI_API_MODEL", "gpt-4-1106-preview") self.max_tokens_rsp = self._get("MAX_TOKENS", 2048) self.deployment_name = self._get("DEPLOYMENT_NAME") self.deployment_id = self._get("DEPLOYMENT_ID") @@ -139,7 +139,6 @@ class Config(metaclass=Singleton): continue configs.update(yaml_data) OPTIONS.set(configs) - logger.info(f"Default OpenAI API Model: {self.openai_api_model}") @staticmethod def _get(*args, **kwargs): diff --git a/metagpt/utils/dependency_file.py b/metagpt/utils/dependency_file.py index d03444f0e..8a6575e9e 100644 --- a/metagpt/utils/dependency_file.py +++ b/metagpt/utils/dependency_file.py @@ -37,7 +37,7 @@ class DependencyFile: """Load dependencies from the file asynchronously.""" if not self._filename.exists(): return - self._dependencies = await aread(self._filename) + self._dependencies = json.loads(await aread(self._filename)) @handle_exception async def save(self): From 9ca0d57a91bea18a19cf9b80b8854d00b310b67a Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:31:38 +0800 Subject: [PATCH 368/413] bug fix and proper log --- metagpt/schema.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index c026ea1d9..991ceaae0 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -282,11 +282,11 @@ T = TypeVar("T", bound="BaseModel") class BaseContext(BaseModel): - @staticmethod + @classmethod @handle_exception - def loads(val: str, cls: Type[T]) -> Optional[T]: - m = json.loads(val) - return cls(**m) + def loads(cls: Type[T], val: str) -> Optional[T]: + i = json.loads(val) + return cls(**i) class CodingContext(BaseContext): From b43d8462deb4c35d997b8c2ae3d797a0cb1853f6 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 16:54:06 +0800 Subject: [PATCH 369/413] refine config --- config/config.yaml | 2 +- metagpt/config.py | 51 +++++++++++++++++++------------ metagpt/provider/anthropic_api.py | 4 +-- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index dc4c4ea5a..f547462ba 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -23,7 +23,7 @@ RPM: 10 #SPARK_URL : "ws://spark-api.xf-yun.com/v2.1/chat" #### if Anthropic -#Anthropic_API_KEY: "YOUR_API_KEY" +#ANTHROPIC_API_KEY: "YOUR_API_KEY" #### if AZURE, check https://github.com/openai/openai-cookbook/blob/main/examples/azure/chat.ipynb #### You can use ENGINE or DEPLOYMENT mode diff --git a/metagpt/config.py b/metagpt/config.py index 5f2be971a..386c4784e 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -47,30 +47,41 @@ class Config(metaclass=Singleton): def __init__(self, yaml_file=default_yaml_file): golbal_options = OPTIONS.get() self._init_with_config_files_and_env(yaml_file) - logger.debug("Config loading done.") self._update() golbal_options.update(OPTIONS.get()) + logger.debug("Config loading done.") + + @staticmethod + def _is_valid_llm_key(k) -> bool: + return k and k != "YOUR_API_KEY" + + def _check_llm_exists(self): + if not any( + [ + self._is_valid_llm_key(self.openai_api_key), + self._is_valid_llm_key(self.anthropic_api_key), + self._is_valid_llm_key(self.zhipuai_api_key), + self._is_valid_llm_key(self.fireworks_api_key), + self.open_llm_api_base, + ] + ): + raise NotConfiguredException( + "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY " + "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" + ) def _update(self): # logger.info("Config loading done.") self.global_proxy = self._get("GLOBAL_PROXY") + self.openai_api_key = self._get("OPENAI_API_KEY") - self.anthropic_api_key = self._get("Anthropic_API_KEY") + self.anthropic_api_key = self._get("ANTHROPIC_API_KEY") self.zhipuai_api_key = self._get("ZHIPUAI_API_KEY") self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") self.fireworks_api_key = self._get("FIREWORKS_API_KEY") - if ( - (not self.openai_api_key or "YOUR_API_KEY" == self.openai_api_key) - and (not self.anthropic_api_key or "YOUR_API_KEY" == self.anthropic_api_key) - and (not self.zhipuai_api_key or "YOUR_API_KEY" == self.zhipuai_api_key) - and (not self.open_llm_api_base) - and (not self.fireworks_api_key or "YOUR_API_KEY" == self.fireworks_api_key) - ): - raise NotConfiguredException( - "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY first " - "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" - ) + self._check_llm_exists() + self.openai_api_base = self._get("OPENAI_API_BASE") self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy self.openai_api_type = self._get("OPENAI_API_TYPE") @@ -90,7 +101,7 @@ class Config(metaclass=Singleton): self.fireworks_api_base = self._get("FIREWORKS_API_BASE") self.fireworks_api_model = self._get("FIREWORKS_API_MODEL") - self.claude_api_key = self._get("Anthropic_API_KEY") + self.claude_api_key = self._get("ANTHROPIC_API_KEY") self.serpapi_api_key = self._get("SERPAPI_API_KEY") self.serper_api_key = self._get("SERPER_API_KEY") self.google_api_key = self._get("GOOGLE_API_KEY") @@ -142,8 +153,8 @@ class Config(metaclass=Singleton): @staticmethod def _get(*args, **kwargs): - m = OPTIONS.get() - return m.get(*args, **kwargs) + i = OPTIONS.get() + return i.get(*args, **kwargs) def get(self, key, *args, **kwargs): """Search for a value in config/key.yaml, config/config.yaml, and env; raise an error if not found""" @@ -156,8 +167,8 @@ class Config(metaclass=Singleton): OPTIONS.get()[name] = value def __getattr__(self, name: str) -> Any: - m = OPTIONS.get() - return m.get(name) + i = OPTIONS.get() + return i.get(name) def set_context(self, options: dict): """Update current config""" @@ -176,8 +187,8 @@ class Config(metaclass=Singleton): def new_environ(self): """Return a new os.environ object""" env = os.environ.copy() - m = self.options - env.update({k: v for k, v in m.items() if isinstance(v, str)}) + i = self.options + env.update({k: v for k, v in i.items() if isinstance(v, str)}) return env diff --git a/metagpt/provider/anthropic_api.py b/metagpt/provider/anthropic_api.py index 03802a716..f5b06c855 100644 --- a/metagpt/provider/anthropic_api.py +++ b/metagpt/provider/anthropic_api.py @@ -14,7 +14,7 @@ from metagpt.config import CONFIG class Claude2: def ask(self, prompt): - client = Anthropic(api_key=CONFIG.claude_api_key) + client = Anthropic(api_key=CONFIG.anthropic_api_key) res = client.completions.create( model="claude-2", @@ -24,7 +24,7 @@ class Claude2: return res.completion async def aask(self, prompt): - client = Anthropic(api_key=CONFIG.claude_api_key) + client = Anthropic(api_key=CONFIG.anthropic_api_key) res = client.completions.create( model="claude-2", From 67de3132483409c9ed3b85809bcb5cfc7276d347 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:06:07 +0800 Subject: [PATCH 370/413] refine code --- metagpt/config.py | 8 ++++++++ metagpt/repo_parser.py | 2 +- metagpt/startup.py | 10 +++------- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 386c4784e..50ad6a3b2 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -131,6 +131,14 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() + def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): + """update config via cli""" + self.project_path = project_path + self.project_name = project_name + self.inc = inc + self.reqa_file = reqa_file + self.max_auto_summarize_code = max_auto_summarize_code + def _ensure_workspace_exists(self): self.workspace_path.mkdir(parents=True, exist_ok=True) logger.debug(f"WORKSPACE_PATH set to {self.workspace_path}") diff --git a/metagpt/repo_parser.py b/metagpt/repo_parser.py index 9a1218ef1..3524a5bce 100644 --- a/metagpt/repo_parser.py +++ b/metagpt/repo_parser.py @@ -96,4 +96,4 @@ def error(): if __name__ == "__main__": - error() + main() diff --git a/metagpt/startup.py b/metagpt/startup.py index 17eb26665..6ae47213e 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -27,8 +27,8 @@ def startup( reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), max_auto_summarize_code: int = typer.Option( default=-1, - help="The maximum number of times the 'SummarizeCode' action is automatically invoked, " - "with -1 indicating unlimited. This parameter is used for debugging the workflow.", + help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " + "unlimited. This parameter is used for debugging the workflow.", ), recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage") ): @@ -43,14 +43,10 @@ def startup( from metagpt.team import Team # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. - CONFIG.project_path = project_path if project_path: inc = True project_name = project_name or Path(project_path).name - CONFIG.project_name = project_name - CONFIG.inc = inc - CONFIG.reqa_file = reqa_file - CONFIG.max_auto_summarize_code = max_auto_summarize_code + CONFIG.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code) if not recover_path: company = Team() From 1162f21b6ceef6e09c84b55927cc72c4930d03d1 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:11:02 +0800 Subject: [PATCH 371/413] refine code --- metagpt/config.py | 12 ++++++++++++ metagpt/startup.py | 5 ----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 50ad6a3b2..68b7a2a96 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -131,8 +131,20 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() + def _init_cli_paras(self): + self.project_path = None + self.project_name = None + self.inc = None + self.reqa_file = None + self.max_auto_summarize_code = None + def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): """update config via cli""" + + # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. + if project_path: + inc = True + project_name = project_name or Path(project_path).name self.project_path = project_path self.project_name = project_name self.inc = inc diff --git a/metagpt/startup.py b/metagpt/startup.py index 6ae47213e..a25b71cd0 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -1,7 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio -from pathlib import Path import typer @@ -42,10 +41,6 @@ def startup( ) from metagpt.team import Team - # Use in the PrepareDocuments action according to Section 2.2.3.5.1 of RFC 135. - if project_path: - inc = True - project_name = project_name or Path(project_path).name CONFIG.update_via_cli(project_path, project_name, inc, reqa_file, max_auto_summarize_code) if not recover_path: From bd12087be4dd16f4b460d3bd0b4a7b6fb41eaa9a Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:14:50 +0800 Subject: [PATCH 372/413] fix comment --- metagpt/team.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/team.py b/metagpt/team.py index 9aa89ee2b..1df3c4052 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -25,8 +25,8 @@ from metagpt.utils.common import NoMoneyException, read_json_file, write_json_fi class Team(BaseModel): """ - Team: Possesses one or more roles (agents), SOP (Standard Operating Procedures), and a platform for instant messaging, - dedicated to perform any multi-agent activity, such as collaboratively writing executable code. + Team: Possesses one or more roles (agents), SOP (Standard Operating Procedures), and a env for instant messaging, + dedicated to env any multi-agent activity, such as collaboratively writing executable code. """ env: Environment = Field(default_factory=Environment) From f32f9c82e54581241de42bcf21a5d2efcd12c9e1 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 17:55:34 +0800 Subject: [PATCH 373/413] add llm provider registry --- metagpt/config.py | 56 +++++++++++++---------- metagpt/llm.py | 21 +-------- metagpt/provider/fireworks_api.py | 4 +- metagpt/provider/llm_provider_registry.py | 34 ++++++++++++++ metagpt/provider/open_llm_api.py | 4 +- metagpt/provider/openai_api.py | 4 +- metagpt/provider/spark_api.py | 4 +- metagpt/provider/zhipuai_api.py | 4 +- metagpt/schema.py | 10 ++-- 9 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 metagpt/provider/llm_provider_registry.py diff --git a/metagpt/config.py b/metagpt/config.py index 68b7a2a96..c8346ccdc 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -8,6 +8,7 @@ Provide configuration, singleton """ import os from copy import deepcopy +from enum import Enum from pathlib import Path from typing import Any @@ -31,6 +32,15 @@ class NotConfiguredException(Exception): super().__init__(self.message) +class LLMProviderEnum(Enum): + OPENAI = "openai" + ANTHROPIC = "anthropic" + SPARK = "spark" + ZHIPUAI = "zhipuai" + FIREWORKS = "fireworks" + OPEN_LLM = "open_llm" + + class Config(metaclass=Singleton): """ Regular usage method: @@ -46,30 +56,37 @@ class Config(metaclass=Singleton): def __init__(self, yaml_file=default_yaml_file): golbal_options = OPTIONS.get() + # cli paras + self.project_path = "" + self.project_name = "" + self.inc = False + self.reqa_file = "" + self.max_auto_summarize_code = 0 + self._init_with_config_files_and_env(yaml_file) self._update() golbal_options.update(OPTIONS.get()) logger.debug("Config loading done.") + def get_default_llm_provider_enum(self): + if self._is_valid_llm_key(self.openai_api_key): + llm = LLMProviderEnum.OPENAI + elif self._is_valid_llm_key(self.anthropic_api_key): + llm = LLMProviderEnum.ANTHROPIC + elif self._is_valid_llm_key(self.zhipuai_api_key): + llm = LLMProviderEnum.ZHIPUAI + elif self._is_valid_llm_key(self.fireworks_api_key): + llm = LLMProviderEnum.FIREWORKS + elif self.open_llm_api_base: + llm = LLMProviderEnum.OPEN_LLM + else: + raise NotConfiguredException("You should config a LLM configuration first") + return llm + @staticmethod def _is_valid_llm_key(k) -> bool: return k and k != "YOUR_API_KEY" - def _check_llm_exists(self): - if not any( - [ - self._is_valid_llm_key(self.openai_api_key), - self._is_valid_llm_key(self.anthropic_api_key), - self._is_valid_llm_key(self.zhipuai_api_key), - self._is_valid_llm_key(self.fireworks_api_key), - self.open_llm_api_base, - ] - ): - raise NotConfiguredException( - "Set OPENAI_API_KEY or Anthropic_API_KEY or ZHIPUAI_API_KEY " - "or FIREWORKS_API_KEY or OPEN_LLM_API_BASE" - ) - def _update(self): # logger.info("Config loading done.") self.global_proxy = self._get("GLOBAL_PROXY") @@ -80,7 +97,7 @@ class Config(metaclass=Singleton): self.open_llm_api_base = self._get("OPEN_LLM_API_BASE") self.open_llm_api_model = self._get("OPEN_LLM_API_MODEL") self.fireworks_api_key = self._get("FIREWORKS_API_KEY") - self._check_llm_exists() + _ = self.get_default_llm_provider_enum() self.openai_api_base = self._get("OPENAI_API_BASE") self.openai_proxy = self._get("OPENAI_PROXY") or self.global_proxy @@ -131,13 +148,6 @@ class Config(metaclass=Singleton): self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() - def _init_cli_paras(self): - self.project_path = None - self.project_name = None - self.inc = None - self.reqa_file = None - self.max_auto_summarize_code = None - def update_via_cli(self, project_path, project_name, inc, reqa_file, max_auto_summarize_code): """update config via cli""" diff --git a/metagpt/llm.py b/metagpt/llm.py index 7c0ad7975..e0c0716de 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -8,12 +8,8 @@ from metagpt.config import CONFIG from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.provider.fireworks_api import FireWorksGPTAPI from metagpt.provider.human_provider import HumanProvider -from metagpt.provider.open_llm_api import OpenLLMGPTAPI -from metagpt.provider.openai_api import OpenAIGPTAPI -from metagpt.provider.spark_api import SparkAPI -from metagpt.provider.zhipuai_api import ZhiPuAIGPTAPI +from metagpt.provider.llm_provider_registry import LLMProviderRegistry _ = HumanProvider() # Avoid pre-commit error @@ -21,17 +17,4 @@ _ = HumanProvider() # Avoid pre-commit error def LLM() -> BaseGPTAPI: """initialize different LLM instance according to the key field existence""" # TODO a little trick, can use registry to initialize LLM instance further - if CONFIG.openai_api_key: - llm = OpenAIGPTAPI() - elif CONFIG.spark_api_key: - llm = SparkAPI() - elif CONFIG.zhipuai_api_key: - llm = ZhiPuAIGPTAPI() - elif CONFIG.open_llm_api_base: - llm = OpenLLMGPTAPI() - elif CONFIG.fireworks_api_key: - llm = FireWorksGPTAPI() - else: - raise RuntimeError("You should config a LLM configuration first") - - return llm + return LLMProviderRegistry.get_provider(CONFIG.get_default_llm_provider_enum()) diff --git a/metagpt/provider/fireworks_api.py b/metagpt/provider/fireworks_api.py index 47ac9cf61..a76151666 100644 --- a/metagpt/provider/fireworks_api.py +++ b/metagpt/provider/fireworks_api.py @@ -4,10 +4,12 @@ import openai -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter +@register_provider(LLMProviderEnum.FIREWORKS) class FireWorksGPTAPI(OpenAIGPTAPI): def __init__(self): self.__init_fireworks(CONFIG) diff --git a/metagpt/provider/llm_provider_registry.py b/metagpt/provider/llm_provider_registry.py new file mode 100644 index 000000000..2b3ef93a3 --- /dev/null +++ b/metagpt/provider/llm_provider_registry.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/19 17:26 +@Author : alexanderwu +@File : llm_provider_registry.py +""" +from metagpt.config import LLMProviderEnum + + +class LLMProviderRegistry: + def __init__(self): + self.providers = {} + + def register(self, key, provider_cls): + self.providers[key] = provider_cls + + def get_provider(self, enum: LLMProviderEnum): + """get provider instance according to the enum""" + return self.providers[enum]() + + +# Registry instance +LLM_REGISTRY = LLMProviderRegistry() + + +def register_provider(key): + """register provider to registry""" + + def decorator(cls): + LLM_REGISTRY.register(key, cls) + return cls + + return decorator diff --git a/metagpt/provider/open_llm_api.py b/metagpt/provider/open_llm_api.py index f421e30c8..bada0e294 100644 --- a/metagpt/provider/open_llm_api.py +++ b/metagpt/provider/open_llm_api.py @@ -4,8 +4,9 @@ import openai -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, OpenAIGPTAPI, RateLimiter @@ -31,6 +32,7 @@ class OpenLLMCostManager(CostManager): CONFIG.total_cost = self.total_cost +@register_provider(LLMProviderEnum.OPEN_LLM) class OpenLLMGPTAPI(OpenAIGPTAPI): def __init__(self): self.__init_openllm(CONFIG) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index 86054881e..0be70b3ca 100644 --- a/metagpt/provider/openai_api.py +++ b/metagpt/provider/openai_api.py @@ -18,10 +18,11 @@ from tenacity import ( wait_random_exponential, ) -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.constant import GENERAL_FUNCTION_SCHEMA, GENERAL_TOOL_CHOICE +from metagpt.provider.llm_provider_registry import register_provider from metagpt.schema import Message from metagpt.utils.singleton import Singleton from metagpt.utils.token_counter import ( @@ -137,6 +138,7 @@ See FAQ 5.8 raise retry_state.outcome.exception() +@register_provider(LLMProviderEnum.OPENAI) class OpenAIGPTAPI(BaseGPTAPI, RateLimiter): """ Check https://platform.openai.com/examples for examples diff --git a/metagpt/provider/spark_api.py b/metagpt/provider/spark_api.py index 60c86f4dc..484fa7956 100644 --- a/metagpt/provider/spark_api.py +++ b/metagpt/provider/spark_api.py @@ -19,11 +19,13 @@ from wsgiref.handlers import format_date_time import websocket # 使用websocket_client -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.llm_provider_registry import register_provider +@register_provider(LLMProviderEnum.SPARK) class SparkAPI(BaseGPTAPI): def __init__(self): logger.warning("当前方法无法支持异步运行。当你使用acompletion时,并不能并行访问。") diff --git a/metagpt/provider/zhipuai_api.py b/metagpt/provider/zhipuai_api.py index 92119b764..eef0e51e1 100644 --- a/metagpt/provider/zhipuai_api.py +++ b/metagpt/provider/zhipuai_api.py @@ -16,9 +16,10 @@ from tenacity import ( wait_random_exponential, ) -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.provider.llm_provider_registry import register_provider from metagpt.provider.openai_api import CostManager, log_and_reraise from metagpt.provider.zhipuai.zhipu_model_api import ZhiPuModelAPI @@ -30,6 +31,7 @@ class ZhiPuEvent(Enum): FINISH = "finish" +@register_provider(LLMProviderEnum.ZHIPUAI) class ZhiPuAIGPTAPI(BaseGPTAPI): """ Refs to `https://open.bigmodel.cn/dev/api#chatglm_turbo` diff --git a/metagpt/schema.py b/metagpt/schema.py index 991ceaae0..59203c404 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -167,8 +167,8 @@ class Message(BaseModel): @handle_exception(exception_type=JSONDecodeError, default_return=None) def load(val): """Convert the json string to object.""" - d = json.loads(val) - return Message(**d) + i = json.loads(val) + return Message(**i) class UserMessage(Message): @@ -263,16 +263,16 @@ class MessageQueue(BaseModel): return json.dumps(lst) @staticmethod - def load(i) -> "MessageQueue": + def load(data) -> "MessageQueue": """Convert the json string to the `MessageQueue` object.""" queue = MessageQueue() try: - lst = json.loads(i) + lst = json.loads(data) for i in lst: msg = Message(**i) queue.push(msg) except JSONDecodeError as e: - logger.warning(f"JSON load failed: {i}, error:{e}") + logger.warning(f"JSON load failed: {data}, error:{e}") return queue From af59323a692c90a2cfe58eede0dd3189ac6568b7 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:02:51 +0800 Subject: [PATCH 374/413] make registry work --- metagpt/llm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index e0c0716de..60f110a00 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -9,7 +9,7 @@ from metagpt.config import CONFIG from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.human_provider import HumanProvider -from metagpt.provider.llm_provider_registry import LLMProviderRegistry +from metagpt.provider.llm_provider_registry import LLM_REGISTRY _ = HumanProvider() # Avoid pre-commit error @@ -17,4 +17,4 @@ _ = HumanProvider() # Avoid pre-commit error def LLM() -> BaseGPTAPI: """initialize different LLM instance according to the key field existence""" # TODO a little trick, can use registry to initialize LLM instance further - return LLMProviderRegistry.get_provider(CONFIG.get_default_llm_provider_enum()) + return LLM_REGISTRY.get_provider(CONFIG.get_default_llm_provider_enum()) From fc829edc45571f9ca3b5d3212a4f49e46d77a4eb Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:04:12 +0800 Subject: [PATCH 375/413] make registry work --- metagpt/llm.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/metagpt/llm.py b/metagpt/llm.py index 60f110a00..8763642f0 100644 --- a/metagpt/llm.py +++ b/metagpt/llm.py @@ -6,7 +6,7 @@ @File : llm.py """ -from metagpt.config import CONFIG +from metagpt.config import CONFIG, LLMProviderEnum from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.provider.human_provider import HumanProvider from metagpt.provider.llm_provider_registry import LLM_REGISTRY @@ -14,7 +14,6 @@ from metagpt.provider.llm_provider_registry import LLM_REGISTRY _ = HumanProvider() # Avoid pre-commit error -def LLM() -> BaseGPTAPI: - """initialize different LLM instance according to the key field existence""" - # TODO a little trick, can use registry to initialize LLM instance further - return LLM_REGISTRY.get_provider(CONFIG.get_default_llm_provider_enum()) +def LLM(provider: LLMProviderEnum = CONFIG.get_default_llm_provider_enum()) -> BaseGPTAPI: + """get the default llm provider""" + return LLM_REGISTRY.get_provider(provider) From 06d8dccc16cd6b1694b97960708d1e73c130b7c7 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:50:55 +0800 Subject: [PATCH 376/413] refine code for isinstance --- metagpt/actions/write_prd.py | 2 +- metagpt/roles/role.py | 2 +- metagpt/roles/searcher.py | 2 +- metagpt/utils/common.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index f087d8650..0febb2656 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -185,7 +185,7 @@ class WritePRD(Action): return if not CONFIG.project_name: - if isinstance(prd, ActionOutput) or isinstance(prd, ActionNode): + if isinstance(prd, (ActionOutput, ActionNode)): ws_name = prd.instruct_content.dict()["Project Name"] else: ws_name = CodeParser.parse_str(block="Project Name", text=prd) diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 3a8721004..fa09999e5 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -370,7 +370,7 @@ class Role(BaseModel): async def _act(self) -> Message: logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.important_memory) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index 7d58ad922..a5c399f47 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -60,7 +60,7 @@ class Searcher(Role): logger.info(f"{self._setting}: ready to {self._rc.todo}") response = await self._rc.todo.run(self._rc.memory.get(k=0)) - if isinstance(response, ActionOutput) or isinstance(response, ActionNode): + if isinstance(response, (ActionOutput, ActionNode)): msg = Message( content=response.content, instruct_content=response.instruct_content, diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 0060950dc..a445c9f31 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -203,7 +203,7 @@ class OutputParser: result = ast.literal_eval(structure_text) # Ensure the result matches the specified data type - if isinstance(result, list) or isinstance(result, dict): + if isinstance(result, (list, dict)): return result raise ValueError(f"The extracted structure is not a {data_type}.") From da91fb18c0d135cac509df35d8f53098a3c1f00d Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 18:54:04 +0800 Subject: [PATCH 377/413] fix typo --- metagpt/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index c8346ccdc..8ed957808 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -55,7 +55,7 @@ class Config(metaclass=Singleton): default_yaml_file = METAGPT_ROOT / "config/config.yaml" def __init__(self, yaml_file=default_yaml_file): - golbal_options = OPTIONS.get() + global_options = OPTIONS.get() # cli paras self.project_path = "" self.project_name = "" @@ -65,7 +65,7 @@ class Config(metaclass=Singleton): self._init_with_config_files_and_env(yaml_file) self._update() - golbal_options.update(OPTIONS.get()) + global_options.update(OPTIONS.get()) logger.debug("Config loading done.") def get_default_llm_provider_enum(self): From acb968663f6ba10e4a621e53ab6d2255163a6519 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:00:20 +0800 Subject: [PATCH 378/413] refine cli --- metagpt/startup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index a25b71cd0..9c17edc1c 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -6,7 +6,7 @@ import typer from metagpt.config import CONFIG -app = typer.Typer() +app = typer.Typer(add_completion=False) @app.command() @@ -23,7 +23,9 @@ def startup( default="", help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", ), - reqa_file: str = typer.Option(default="", help="Specify the source file name for rewriting the quality test code."), + reqa_file: str = typer.Option( + default="", help="Specify the source file name for rewriting the quality assurance " "code." + ), max_auto_summarize_code: int = typer.Option( default=-1, help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " From b5b1ef7ead978303e27364a4d52cf090322a9743 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:00:39 +0800 Subject: [PATCH 379/413] refine cli --- metagpt/startup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index 9c17edc1c..b66f9e305 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -21,10 +21,10 @@ def startup( inc: bool = typer.Option(default=False, help="Incremental mode. Use it to coop with existing repo."), project_path: str = typer.Option( default="", - help="Specify the directory path of the old version project to fulfill the " "incremental requirements.", + help="Specify the directory path of the old version project to fulfill the incremental requirements.", ), reqa_file: str = typer.Option( - default="", help="Specify the source file name for rewriting the quality assurance " "code." + default="", help="Specify the source file name for rewriting the quality assurance code." ), max_auto_summarize_code: int = typer.Option( default=-1, From 79bb44b0b7978c590143f1a8b1775747d5490a66 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:15:30 +0800 Subject: [PATCH 380/413] fix pylint --- examples/agent_creator.py | 9 ++++----- metagpt/memory/longterm_memory.py | 8 ++++---- metagpt/memory/memory_storage.py | 2 +- metagpt/roles/product_manager.py | 2 +- metagpt/roles/qa_engineer.py | 2 +- 5 files changed, 11 insertions(+), 12 deletions(-) diff --git a/examples/agent_creator.py b/examples/agent_creator.py index 05417d24a..26af8a287 100644 --- a/examples/agent_creator.py +++ b/examples/agent_creator.py @@ -12,9 +12,8 @@ from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -with open(METAGPT_ROOT / "examples/build_customized_agent.py", "r") as f: - # use official example script to guide AgentCreator - MULTI_ACTION_AGENT_CODE_EXAMPLE = f.read() +EXAMPLE_CODE_FILE = METAGPT_ROOT / "examples/build_customized_agent.py" +MULTI_ACTION_AGENT_CODE_EXAMPLE = EXAMPLE_CODE_FILE.read_text() class CreateAgent(Action): @@ -50,8 +49,8 @@ class CreateAgent(Action): match = re.search(pattern, rsp, re.DOTALL) code_text = match.group(1) if match else "" CONFIG.workspace_path.mkdir(parents=True, exist_ok=True) - with open(CONFIG.workspace_path / "agent_created_agent.py", "w") as f: - f.write(code_text) + new_file = CONFIG.workspace_path / "agent_created_agent.py" + new_file.write_text(code_text) return code_text diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index d36188f0c..069740054 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -44,7 +44,7 @@ class LongTermMemory(Memory): self.msg_from_recover = False def add(self, message: Message): - super(LongTermMemory, self).add(message) + super().add(message) for action in self.rc.watch: if message.cause_by == action and not self.msg_from_recover: # currently, only add role's watching messages to its memory_storage @@ -57,7 +57,7 @@ class LongTermMemory(Memory): 1. find the short-term memory(stm) news 2. furthermore, filter out similar messages based on ltm(long-term memory), get the final news """ - stm_news = super(LongTermMemory, self).find_news(observed, k=k) # shot-term memory news + stm_news = super().find_news(observed, k=k) # shot-term memory news if not self.memory_storage.is_initialized: # memory_storage hasn't initialized, use default `find_news` to get stm_news return stm_news @@ -71,9 +71,9 @@ class LongTermMemory(Memory): return ltm_news[-k:] def delete(self, message: Message): - super(LongTermMemory, self).delete(message) + super().delete(message) # TODO delete message in memory_storage def clear(self): - super(LongTermMemory, self).clear() + super().clear() self.memory_storage.clean() diff --git a/metagpt/memory/memory_storage.py b/metagpt/memory/memory_storage.py index a213f6d7a..fafb33568 100644 --- a/metagpt/memory/memory_storage.py +++ b/metagpt/memory/memory_storage.py @@ -58,7 +58,7 @@ class MemoryStorage(FaissStore): return index_fpath, storage_fpath def persist(self): - super(MemoryStorage, self).persist() + super().persist() logger.debug(f"Agent {self.role_id} persist memory into local") def add(self, message: Message) -> bool: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 11bda2127..6dba21fe1 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -45,4 +45,4 @@ class ProductManager(Role): return self._rc.todo async def _observe(self, ignore_memory=False) -> int: - return await super(ProductManager, self)._observe(ignore_memory=True) + return await super()._observe(ignore_memory=True) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index ec404570c..acb79ab80 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -186,4 +186,4 @@ class QaEngineer(Role): async def _observe(self, ignore_memory=False) -> int: # This role has events that trigger and execute themselves based on conditions, and cannot rely on the # content of memory to activate. - return await super(QaEngineer, self)._observe(ignore_memory=True) + return await super()._observe(ignore_memory=True) From 3920982786bdfee81443639f7f1c060da474ca24 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:25:01 +0800 Subject: [PATCH 381/413] refine code --- metagpt/config.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/metagpt/config.py b/metagpt/config.py index 8ed957808..80a3a28f4 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -68,23 +68,22 @@ class Config(metaclass=Singleton): global_options.update(OPTIONS.get()) logger.debug("Config loading done.") - def get_default_llm_provider_enum(self): - if self._is_valid_llm_key(self.openai_api_key): - llm = LLMProviderEnum.OPENAI - elif self._is_valid_llm_key(self.anthropic_api_key): - llm = LLMProviderEnum.ANTHROPIC - elif self._is_valid_llm_key(self.zhipuai_api_key): - llm = LLMProviderEnum.ZHIPUAI - elif self._is_valid_llm_key(self.fireworks_api_key): - llm = LLMProviderEnum.FIREWORKS - elif self.open_llm_api_base: - llm = LLMProviderEnum.OPEN_LLM - else: - raise NotConfiguredException("You should config a LLM configuration first") - return llm + def get_default_llm_provider_enum(self) -> LLMProviderEnum: + for k, v in [ + (self.openai_api_key, LLMProviderEnum.OPENAI), + (self.anthropic_api_key, LLMProviderEnum.ANTHROPIC), + (self.zhipuai_api_key, LLMProviderEnum.ZHIPUAI), + (self.fireworks_api_key, LLMProviderEnum.FIREWORKS), + (self.open_llm_api_base, LLMProviderEnum.OPEN_LLM), # reuse logic. but not a key + ]: + if self._is_valid_llm_key(k): + if self.openai_api_model: + logger.info(f"OpenAI API Model: {self.openai_api_model}") + return v + raise NotConfiguredException("You should config a LLM configuration first") @staticmethod - def _is_valid_llm_key(k) -> bool: + def _is_valid_llm_key(k: str) -> bool: return k and k != "YOUR_API_KEY" def _update(self): From 029eed1792d555b4b373264a2ba8f12d0b81c7aa Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:26:01 +0800 Subject: [PATCH 382/413] delete manager.py --- metagpt/manager.py | 66 ---------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 metagpt/manager.py diff --git a/metagpt/manager.py b/metagpt/manager.py deleted file mode 100644 index a063608be..000000000 --- a/metagpt/manager.py +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/11 14:42 -@Author : alexanderwu -@File : manager.py -""" -from metagpt.llm import LLM -from metagpt.logs import logger -from metagpt.schema import Message - - -class Manager: - def __init__(self, llm: LLM = LLM()): - self.llm = llm # Large Language Model - self.role_directions = { - "User": "Product Manager", - "Product Manager": "Architect", - "Architect": "Engineer", - "Engineer": "QA Engineer", - "QA Engineer": "Product Manager", - } - self.prompt_template = """ - Given the following message: - {message} - - And the current status of roles: - {roles} - - Which role should handle this message? - """ - - async def handle(self, message: Message, environment): - """ - 管理员处理信息,现在简单的将信息递交给下一个人 - The administrator processes the information, now simply passes the information on to the next person - :param message: - :param environment: - :return: - """ - # Get all roles from the environment - roles = environment.get_roles() - # logger.debug(f"{roles=}, {message=}") - - # Build a context for the LLM to understand the situation - # context = { - # "message": str(message), - # "roles": {role.name: role.get_info() for role in roles}, - # } - # Ask the LLM to decide which role should handle the message - # chosen_role_name = self.llm.ask(self.prompt_template.format(context)) - - # FIXME: 现在通过简单的字典决定流向,但之后还是应该有思考过程 - # The direction of flow is now determined by a simple dictionary, but there should still be a thought process afterwards - next_role_profile = self.role_directions[message.role] - # logger.debug(f"{next_role_profile}") - for _, role in roles.items(): - if next_role_profile == role.profile: - next_role = role - break - else: - logger.error(f"No available role can handle message: {message}.") - return - - # Find the chosen role and handle the message - return await next_role.handle(message) From 25ea21321fbf5f1212289e77de46875037ecaa85 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 19:27:11 +0800 Subject: [PATCH 383/413] remove useless fields --- metagpt/actions/action.py | 9 +++------ metagpt/actions/search_and_summarize.py | 3 +-- metagpt/roles/role.py | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 9c7fb06e1..ba1bb48de 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -14,6 +14,7 @@ from pydantic import BaseModel, Field from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI @@ -29,11 +30,8 @@ class Action(BaseModel): llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) context = "" prefix = "" # aask*时会加上prefix,作为system_message - profile = "" # FIXME: USELESS desc = "" # for skill manager - nodes = [] - # content: Optional[str] = None - # instruct_content: Optional[str] = None + node: ActionNode = Field(default_factory=ActionNode) # builtin variables builtin_class_name: str = "" @@ -58,10 +56,9 @@ class Action(BaseModel): obj_dict.pop("llm") return obj_dict - def set_prefix(self, prefix, profile): + def set_prefix(self, prefix): """Set prefix for later usage""" self.prefix = prefix - self.profile = profile return self def __str__(self): diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index aa4d0f654..3f110c370 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -148,8 +148,7 @@ class SearchAndSummarize(Action): system_prompt = [system_text] prompt = SEARCH_AND_SUMMARIZE_PROMPT.format( - # PREFIX = self.prefix, - ROLE=self.profile, + ROLE=self.prefix, CONTEXT=rsp, QUERY_HISTORY="\n".join([str(i) for i in context[:-1]]), QUERY=str(context[-1]), diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index fa09999e5..e57f21ec3 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -238,7 +238,7 @@ class Role(BaseModel): return role def _init_action_system_message(self, action: Action): - action.set_prefix(self._get_prefix(), self.profile) + action.set_prefix(self._get_prefix()) def set_recovered(self, recovered: bool = False): self.recovered = recovered From a75ab7971fad845f1d07c0fe455cc1a398ec54b4 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:17:02 +0800 Subject: [PATCH 384/413] refine a lot of code, fix pylint, use actionnode include ui, action _aask_v1, detail_mining, prepare_interview, etc. --- metagpt/actions/action.py | 34 +----- metagpt/actions/action_node.py | 81 +++++--------- metagpt/actions/design_api.py | 10 +- metagpt/actions/detail_mining.py | 50 +++------ metagpt/actions/prepare_interview.py | 35 ++---- metagpt/actions/project_management.py | 10 +- metagpt/actions/write_prd.py | 8 +- metagpt/config.py | 2 +- metagpt/utils/get_template.py | 6 +- tests/metagpt/actions/test_detail_mining.py | 4 +- .../metagpt/actions/test_prepare_interview.py | 21 ++++ tests/metagpt/roles/ui_role.py | 104 +++++++++--------- 12 files changed, 150 insertions(+), 215 deletions(-) create mode 100644 tests/metagpt/actions/test_prepare_interview.py diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index ba1bb48de..1fcc8fc80 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -11,15 +11,9 @@ from __future__ import annotations from typing import Optional, Any from pydantic import BaseModel, Field -from tenacity import retry, stop_after_attempt, wait_random_exponential - -from metagpt.actions.action_output import ActionOutput from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM -from metagpt.logs import logger from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess -from metagpt.utils.common import OutputParser, general_after_log action_subclass_registry = {} @@ -31,7 +25,7 @@ class Action(BaseModel): context = "" prefix = "" # aask*时会加上prefix,作为system_message desc = "" # for skill manager - node: ActionNode = Field(default_factory=ActionNode) + node: ActionNode = Field(default_factory=ActionNode, exclude=True) # builtin variables builtin_class_name: str = "" @@ -74,32 +68,6 @@ class Action(BaseModel): system_msgs.append(self.prefix) return await self.llm.aask(prompt, system_msgs) - @retry( - wait=wait_random_exponential(min=1, max=60), - stop=stop_after_attempt(6), - after=general_after_log(logger), - ) - async def _aask_v1( - self, - prompt: str, - output_class_name: str, - output_data_mapping: dict, - system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format - ) -> ActionOutput: - content = await self.llm.aask(prompt, system_msgs) - logger.debug(f"llm raw output:\n{content}") - output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - - if format == "json": - parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") - else: # using markdown parser - parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - - logger.debug(parsed_data) - instruct_content = output_class(**parsed_data) - return ActionOutput(content, instruct_content) - async def run(self, *args, **kwargs): """Run action""" raise NotImplementedError("The run method should be implemented in a subclass.") diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 6f1215920..0368d2df1 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -6,17 +6,15 @@ @File : action_node.py """ import json -import re -from typing import Any, Dict, List, Optional, Type +from typing import Dict, Generic, List, Optional, Type, TypeVar from pydantic import BaseModel, create_model, root_validator, validator from tenacity import retry, stop_after_attempt, wait_random_exponential -from metagpt.actions import ActionOutput from metagpt.llm import BaseGPTAPI from metagpt.logs import logger -from metagpt.utils.common import OutputParser -from metagpt.utils.custom_decoder import CustomDecoder +from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess +from metagpt.utils.common import OutputParser, general_after_log CONSTRAINT = """ - Language: Please use the same language as the user input. @@ -43,14 +41,17 @@ Fill in the above nodes based on the format example. """ -def dict_to_markdown(d, prefix="###", postfix="\n"): +def dict_to_markdown(d, prefix="-", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix} {key}: {value}{postfix}" return markdown_str -class ActionNode: +T = TypeVar("T") + + +class ActionNode(Generic[T]): """ActionNode is a tree of nodes.""" mode: str @@ -65,7 +66,7 @@ class ActionNode: expected_type: Type # such as str / int / float etc. # context: str # everything in the history. instruction: str # the instructions should be followed. - example: Any # example for In Context-Learning. + example: T # example for In Context-Learning. # Action Output content: str @@ -76,7 +77,7 @@ class ActionNode: key: str, expected_type: Type, instruction: str, - example: str, + example: T, content: str = "", children: dict[str, "ActionNode"] = None, ): @@ -148,29 +149,6 @@ class ActionNode: new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) return new_class - @classmethod - def create_model_class_v2(cls, class_name: str, mapping: Dict[str, Type]): - """基于pydantic v2的模型动态生成,用来检验结果类型正确性,待验证""" - new_class = create_model(class_name, **mapping) - - @model_validator(mode="before") - def check_missing_fields(data): - required_fields = set(mapping.keys()) - missing_fields = required_fields - set(data.keys()) - if missing_fields: - raise ValueError(f"Missing fields: {missing_fields}") - return data - - @field_validator("*") - def check_name(v: Any, field: str) -> Any: - if field not in mapping.keys(): - raise ValueError(f"Unrecognized block: {field}") - return v - - new_class.__model_validator_check_missing_fields = classmethod(check_missing_fields) - new_class.__field_validator_check_name = classmethod(check_name) - return new_class - def create_children_class(self): """使用object内有的字段直接生成model_class""" class_name = f"{self.key}_AN" @@ -245,6 +223,7 @@ class ActionNode: """ # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", + # compile example暂时不支持markdown self.instruction = self.compile_instruction(to="markdown", mode=mode) self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) prompt = template.format( @@ -252,36 +231,32 @@ class ActionNode: ) return prompt - @retry(wait=wait_random_exponential(min=1, max=10), stop=stop_after_attempt(6)) + @retry( + wait=wait_random_exponential(min=1, max=60), + stop=stop_after_attempt(6), + after=general_after_log(logger), + ) async def _aask_v1( self, prompt: str, output_class_name: str, output_data_mapping: dict, system_msgs: Optional[list[str]] = None, - format="markdown", # compatible to original format - ) -> ActionOutput: + schema="markdown", # compatible to original format + ) -> (str, BaseModel): + """Use ActionOutput to wrap the output of aask""" content = await self.llm.aask(prompt, system_msgs) - logger.debug(content) - output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - - if format == "json": - pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" - matches = re.findall(pattern, content, re.DOTALL) - - for match in matches: - if match: - content = match - break - - parsed_data = CustomDecoder(strict=False).decode(content) + logger.debug(f"llm raw output:\n{content}") + output_class = self.create_model_class(output_class_name, output_data_mapping) + if schema == "json": + parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) - logger.debug(parsed_data) + logger.debug(f"parsed_data:\n{parsed_data}") instruct_content = output_class(**parsed_data) - return ActionOutput(content, instruct_content) + return content, instruct_content def get(self, key): return self.instruct_content.dict()[key] @@ -302,9 +277,9 @@ class ActionNode: mapping = self.get_mapping(mode) class_name = f"{self.key}_AN" - output = await self._aask_v1(prompt, class_name, mapping, format=to) - self.content = output.content - self.instruct_content = output.instruct_content + content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=to) + self.content = content + self.instruct_content = scontent return self async def fill(self, context, llm, to="json", mode="auto", strgy="simple"): diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index c1778d53f..49c5a019d 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -49,7 +49,7 @@ class WriteDesign(Action): "data structures, library tables, processes, and paths. Please provide your design, feedback " \ "clearly and in detail." - async def run(self, with_messages: Message, format: str = CONFIG.prompt_format): + async def run(self, with_messages: Message, schema: str = CONFIG.prompt_schema): # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. prds_file_repo = CONFIG.git_repo.new_file_repository(PRDS_FILE_REPO) changed_prds = prds_file_repo.changed_files @@ -79,13 +79,13 @@ class WriteDesign(Action): # leaving room for global optimization in subsequent steps. return ActionOutput(content=changed_files.json(), instruct_content=changed_files) - async def _new_system_design(self, context, format=CONFIG.prompt_format): - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + async def _new_system_design(self, context, schema=CONFIG.prompt_schema): + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) return node - async def _merge(self, prd_doc, system_design_doc, format=CONFIG.prompt_format): + async def _merge(self, prd_doc, system_design_doc, schema=CONFIG.prompt_schema): context = NEW_REQ_TEMPLATE.format(old_design=system_design_doc.content, context=prd_doc.content) - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=format) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) system_design_doc.content = node.instruct_content.json(ensure_ascii=False) return system_design_doc diff --git a/metagpt/actions/detail_mining.py b/metagpt/actions/detail_mining.py index 5afcf52c6..0314d30dd 100644 --- a/metagpt/actions/detail_mining.py +++ b/metagpt/actions/detail_mining.py @@ -5,47 +5,31 @@ @Author : fisherdeng @File : detail_mining.py """ -from metagpt.actions import Action, ActionOutput +from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode -PROMPT_TEMPLATE = """ -##TOPIC +CONTEXT_TEMPLATE = """ +## TOPIC {topic} -##RECORD +## RECORD {record} - -##Format example -{format_example} ------ - -Task: Refer to the "##TOPIC" (discussion objectives) and "##RECORD" (discussion records) to further inquire about the details that interest you, within a word limit of 150 words. -Special Note 1: Your intention is solely to ask questions without endorsing or negating any individual's viewpoints. -Special Note 2: This output should only include the topic "##OUTPUT". Do not add, remove, or modify the topic. Begin the output with '##OUTPUT', followed by an immediate line break, and then proceed to provide the content in the specified format as outlined in the "##Format example" section. -Special Note 3: The output should be in the same language as the input. """ -FORMAT_EXAMPLE = """ -## - -##OUTPUT -...(Please provide the specific details you would like to inquire about here.) - -## - -## -""" -OUTPUT_MAPPING = { - "OUTPUT": (str, ...), -} +QUESTIONS = ActionNode( + key="Questions", + expected_type=list[str], + instruction="Task: Refer to the context to further inquire about the details that interest you, within a word limit" + " of 150 words. Please provide the specific details you would like to inquire about here", + example=["1. What ...", "2. How ...", "3. ..."], +) class DetailMining(Action): - """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion.""" + """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and + "##RECORD" (discussion records), thereby deepening the discussion.""" - def __init__(self, name="", context=None, llm=None): - super().__init__(name, context, llm) - - async def run(self, topic, record) -> ActionOutput: - prompt = PROMPT_TEMPLATE.format(topic=topic, record=record, format_example=FORMAT_EXAMPLE) - rsp = await self._aask_v1(prompt, "detail_mining", OUTPUT_MAPPING) + async def run(self, topic, record): + context = CONTEXT_TEMPLATE.format(topic=topic, record=record) + rsp = await QUESTIONS.fill(context=context, llm=self.llm) return rsp diff --git a/metagpt/actions/prepare_interview.py b/metagpt/actions/prepare_interview.py index b2704616e..7ed42d590 100644 --- a/metagpt/actions/prepare_interview.py +++ b/metagpt/actions/prepare_interview.py @@ -6,35 +6,18 @@ @File : prepare_interview.py """ from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode -PROMPT_TEMPLATE = """ -# Context -{context} - -## Format example ---- -Q1: question 1 here -References: - - point 1 - - point 2 - -Q2: question 2 here... ---- - ------ -Role: You are an interviewer of our company who is well-knonwn in frontend or backend develop; +QUESTIONS = ActionNode( + key="Questions", + expected_type=list[str], + instruction="""Role: You are an interviewer of our company who is well-knonwn in frontend or backend develop; Requirement: Provide a list of questions for the interviewer to ask the interviewee, by reading the resume of the interviewee in the context. -Attention: Provide as markdown block as the format above, at least 10 questions. -""" - -# prepare for a interview +Attention: Provide as markdown block as the format above, at least 10 questions.""", + example=["1. What ...", "2. How ..."], +) class PrepareInterview(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - async def run(self, context): - prompt = PROMPT_TEMPLATE.format(context=context) - question_list = await self._aask_v1(prompt) - return question_list + return await QUESTIONS.fill(context=context, llm=self.llm) diff --git a/metagpt/actions/project_management.py b/metagpt/actions/project_management.py index 2727f7e7f..095881e60 100644 --- a/metagpt/actions/project_management.py +++ b/metagpt/actions/project_management.py @@ -45,7 +45,7 @@ class WriteTasks(Action): context: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) - async def run(self, with_messages, format=CONFIG.prompt_format): + async def run(self, with_messages, schema=CONFIG.prompt_schema): system_design_file_repo = CONFIG.git_repo.new_file_repository(SYSTEM_DESIGN_FILE_REPO) changed_system_designs = system_design_file_repo.changed_files @@ -92,16 +92,16 @@ class WriteTasks(Action): await self._save_pdf(task_doc=task_doc) return task_doc - async def _run_new_tasks(self, context, format=CONFIG.prompt_format): - node = await PM_NODE.fill(context, self.llm, format) + async def _run_new_tasks(self, context, schema=CONFIG.prompt_schema): + node = await PM_NODE.fill(context, self.llm, schema) # prompt_template, format_example = get_template(templates, format) # prompt = prompt_template.format(context=context, format_example=format_example) # rsp = await self._aask_v1(prompt, "task", OUTPUT_MAPPING, format=format) return node - async def _merge(self, system_design_doc, task_doc, format=CONFIG.prompt_format) -> Document: + async def _merge(self, system_design_doc, task_doc, schema=CONFIG.prompt_schema) -> Document: context = NEW_REQ_TEMPLATE.format(context=system_design_doc.content, old_tasks=task_doc.content) - node = await PM_NODE.fill(context, self.llm, format) + node = await PM_NODE.fill(context, self.llm, schema) task_doc.content = node.instruct_content.json(ensure_ascii=False) return task_doc diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 0febb2656..ae1e0379c 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -113,7 +113,7 @@ class WritePRD(Action): # optimization in subsequent steps. return ActionOutput(content=change_files.json(), instruct_content=change_files) - async def _run_new_requirement(self, requirements, format=CONFIG.prompt_format) -> ActionOutput: + async def _run_new_requirement(self, requirements, schema=CONFIG.prompt_schema) -> ActionOutput: # sas = SearchAndSummarize() # # rsp = await sas.run(context=requirements, system_text=SEARCH_AND_SUMMARIZE_SYSTEM_EN_US) # rsp = "" @@ -123,7 +123,7 @@ class WritePRD(Action): # logger.info(rsp) project_name = CONFIG.project_name if CONFIG.project_name else "" context = CONTEXT_TEMPLATE.format(requirements=requirements, project_name=project_name) - node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=format) + node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=schema) await self._rename_workspace(node) return node @@ -132,11 +132,11 @@ class WritePRD(Action): node = await WP_IS_RELATIVE_NODE.fill(context, self.llm) return node.get("is_relative") == "YES" - async def _merge(self, new_requirement_doc, prd_doc, format=CONFIG.prompt_format) -> Document: + async def _merge(self, new_requirement_doc, prd_doc, schema=CONFIG.prompt_schema) -> Document: if not CONFIG.project_name: CONFIG.project_name = Path(CONFIG.project_path).name prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) - node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=format) + node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=schema) prd_doc.content = node.instruct_content.json(ensure_ascii=False) await self._rename_workspace(node) return prd_doc diff --git a/metagpt/config.py b/metagpt/config.py index 80a3a28f4..131854a56 100644 --- a/metagpt/config.py +++ b/metagpt/config.py @@ -143,7 +143,7 @@ class Config(metaclass=Singleton): self.pyppeteer_executable_path = self._get("PYPPETEER_EXECUTABLE_PATH", "") self.repair_llm_output = self._get("REPAIR_LLM_OUTPUT", False) - self.prompt_format = self._get("PROMPT_FORMAT", "json") + self.prompt_schema = self._get("PROMPT_FORMAT", "json") self.workspace_path = Path(self._get("WORKSPACE_PATH", DEFAULT_WORKSPACE_ROOT)) self._ensure_workspace_exists() diff --git a/metagpt/utils/get_template.py b/metagpt/utils/get_template.py index 86c1915f7..7e05e5d5e 100644 --- a/metagpt/utils/get_template.py +++ b/metagpt/utils/get_template.py @@ -8,10 +8,10 @@ from metagpt.config import CONFIG -def get_template(templates, format=CONFIG.prompt_format): - selected_templates = templates.get(format) +def get_template(templates, schema=CONFIG.prompt_schema): + selected_templates = templates.get(schema) if selected_templates is None: - raise ValueError(f"Can't find {format} in passed in templates") + raise ValueError(f"Can't find {schema} in passed in templates") # Extract the selected templates prompt_template = selected_templates["PROMPT_TEMPLATE"] diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_detail_mining.py index 891dca6ca..30bcf9dfb 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_detail_mining.py @@ -19,5 +19,5 @@ async def test_detail_mining(): rsp = await detail_mining.run(topic=topic, record=record) logger.info(f"{rsp.content=}") - assert "##OUTPUT" in rsp.content - assert "蛋糕" in rsp.content + assert "Questions" in rsp.content + assert "1." in rsp.content diff --git a/tests/metagpt/actions/test_prepare_interview.py b/tests/metagpt/actions/test_prepare_interview.py new file mode 100644 index 000000000..7c32882e0 --- /dev/null +++ b/tests/metagpt/actions/test_prepare_interview.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/9/13 00:26 +@Author : fisherdeng +@File : test_detail_mining.py +""" +import pytest + +from metagpt.actions.prepare_interview import PrepareInterview +from metagpt.logs import logger + + +@pytest.mark.asyncio +async def test_prepare_interview(): + action = PrepareInterview() + rsp = await action.run("I just graduated and hope to find a job as a Python engineer") + logger.info(f"{rsp.content=}") + + assert "Questions" in rsp.content + assert "1." in rsp.content diff --git a/tests/metagpt/roles/ui_role.py b/tests/metagpt/roles/ui_role.py index 8ac799bf3..0932efa1f 100644 --- a/tests/metagpt/roles/ui_role.py +++ b/tests/metagpt/roles/ui_role.py @@ -10,6 +10,7 @@ from importlib import import_module from metagpt.actions import Action, ActionOutput, WritePRD # from metagpt.const import WORKSPACE_ROOT +from metagpt.actions.action_node import ActionNode from metagpt.config import CONFIG from metagpt.logs import logger from metagpt.roles import Role @@ -17,44 +18,38 @@ from metagpt.schema import Message from metagpt.tools.sd_engine import SDEngine PROMPT_TEMPLATE = """ -# Context {context} -## Format example -{format_example} ------ -Role: You are a UserInterface Designer; the goal is to finish a UI design according to PRD, give a design description, and select specified elements and UI style. -Requirements: Based on the context, fill in the following missing information, provide detailed HTML and CSS code -Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. - -## UI Design Description:Provide as Plain text, place the design objective here -## Selected Elements:Provide as Plain text, up to 5 specified elements, clear and simple -## HTML Layout:Provide as Plain text, use standard HTML code -## CSS Styles (styles.css):Provide as Plain text,use standard css code -## Anything UNCLEAR:Provide as Plain text. Try to clarify it. - +## Role +You are a UserInterface Designer; the goal is to finish a UI design according to PRD, give a design description, and select specified elements and UI style. """ -FORMAT_EXAMPLE = """ +UI_DESIGN_DESC = ActionNode( + key="UI Design Desc", + expected_type=str, + instruction="place the design objective here", + example="Snake games are classic and addictive games with simple yet engaging elements. Here are the main elements" + " commonly found in snake games", +) -## UI Design Description -```Snake games are classic and addictive games with simple yet engaging elements. Here are the main elements commonly found in snake games ``` +SELECTED_ELEMENTS = ActionNode( + key="Selected Elements", + expected_type=list[str], + instruction="up to 5 specified elements, clear and simple", + example=[ + "Game Grid: The game grid is a rectangular...", + "Snake: The player controls a snake that moves across the grid...", + "Food: Food items (often represented as small objects or differently colored blocks)", + "Score: The player's score increases each time the snake eats a piece of food. The longer the snake becomes, the higher the score.", + "Game Over: The game ends when the snake collides with itself or an obstacle. At this point, the player's final score is displayed, and they are given the option to restart the game.", + ], +) -## Selected Elements - -Game Grid: The game grid is a rectangular... - -Snake: The player controls a snake that moves across the grid... - -Food: Food items (often represented as small objects or differently colored blocks) - -Score: The player's score increases each time the snake eats a piece of food. The longer the snake becomes, the higher the score. - -Game Over: The game ends when the snake collides with itself or an obstacle. At this point, the player's final score is displayed, and they are given the option to restart the game. - - -## HTML Layout - +HTML_LAYOUT = ActionNode( + key="HTML Layout", + expected_type=str, + instruction="use standard HTML code", + example=""" @@ -71,9 +66,14 @@ Game Over: The game ends when the snake collides with itself or an obstacle. At +""", +) -## CSS Styles (styles.css) -body { +CSS_STYLES = ActionNode( + key="CSS Styles", + expected_type=str, + instruction="use standard css code", + example="""body { display: flex; justify-content: center; align-items: center; @@ -121,19 +121,25 @@ body { color: #ff0000; display: none; } +""", +) -## Anything UNCLEAR -There are no unclear points. +ANYTHING_UNCLEAR = ActionNode( + key="Anything UNCLEAR", + expected_type=str, + instruction="Mention any aspects of the project that are unclear and try to clarify them.", + example="...", +) -""" +NODES = [ + UI_DESIGN_DESC, + SELECTED_ELEMENTS, + HTML_LAYOUT, + CSS_STYLES, + ANYTHING_UNCLEAR, +] -OUTPUT_MAPPING = { - "UI Design Description": (str, ...), - "Selected Elements": (str, ...), - "HTML Layout": (str, ...), - "CSS Styles (styles.css)": (str, ...), - "Anything UNCLEAR": (str, ...), -} +UI_DESIGN_NODE = ActionNode.from_children("UI_DESIGN", NODES) def load_engine(func): @@ -223,10 +229,8 @@ class UIDesign(Action): css_file_path = save_dir / "ui_design.css" html_file_path = save_dir / "ui_design.html" - with open(css_file_path, "w") as css_file: - css_file.write(css_content) - with open(html_file_path, "w") as html_file: - html_file.write(html_content) + css_file_path.write_text(css_content) + html_file_path.write_text(html_content) async def run(self, requirements: list[Message], *args, **kwargs) -> ActionOutput: """Run the UI Design action.""" @@ -234,9 +238,9 @@ class UIDesign(Action): context = requirements[-1].content ui_design_draft = self.parse_requirement(context=context) # todo: parse requirements str - prompt = PROMPT_TEMPLATE.format(context=ui_design_draft, format_example=FORMAT_EXAMPLE) + prompt = PROMPT_TEMPLATE.format(context=ui_design_draft) logger.info(prompt) - ui_describe = await self._aask_v1(prompt, "ui_design", OUTPUT_MAPPING) + ui_describe = await UI_DESIGN_NODE.fill(prompt) logger.info(ui_describe.content) logger.info(ui_describe.instruct_content) css = self.parse_css_code(context=ui_describe.content) From d159bfc4e195a6a72ff5b54dcbea9f36c36373fd Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:24:08 +0800 Subject: [PATCH 385/413] refactor action_output and action_node --- metagpt/actions/action_node.py | 4 ++-- metagpt/actions/action_output.py | 26 +-------------------- metagpt/actions/write_prd.py | 2 +- metagpt/utils/serialize.py | 11 +++++---- tests/metagpt/actions/test_action_output.py | 6 ++--- tests/metagpt/memory/test_memory_storage.py | 4 ++-- tests/metagpt/utils/test_serialize.py | 4 ++-- 7 files changed, 18 insertions(+), 39 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 0368d2df1..865cb2d32 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -6,7 +6,7 @@ @File : action_node.py """ import json -from typing import Dict, Generic, List, Optional, Type, TypeVar +from typing import Any, Dict, Generic, List, Optional, Tuple, Type, TypeVar from pydantic import BaseModel, create_model, root_validator, validator from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -127,7 +127,7 @@ class ActionNode(Generic[T]): return self.get_self_mapping() @classmethod - def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): + def create_model_class(cls, class_name: str, mapping: Dict[str, Tuple[Type, Any]]): """基于pydantic v1的模型动态生成,用来检验结果类型正确性""" new_class = create_model(class_name, **mapping) diff --git a/metagpt/actions/action_output.py b/metagpt/actions/action_output.py index 25326d43b..6be8dac50 100644 --- a/metagpt/actions/action_output.py +++ b/metagpt/actions/action_output.py @@ -6,9 +6,7 @@ @File : action_output """ -from typing import Dict, Type - -from pydantic import BaseModel, create_model, root_validator, validator +from pydantic import BaseModel class ActionOutput: @@ -18,25 +16,3 @@ class ActionOutput: def __init__(self, content: str, instruct_content: BaseModel): self.content = content self.instruct_content = instruct_content - - @classmethod - def create_model_class(cls, class_name: str, mapping: Dict[str, Type]): - new_class = create_model(class_name, **mapping) - - @validator("*", allow_reuse=True) - def check_name(v, field): - if field.name not in mapping.keys(): - raise ValueError(f"Unrecognized block: {field.name}") - return v - - @root_validator(pre=True, allow_reuse=True) - def check_missing_fields(values): - required_fields = set(mapping.keys()) - missing_fields = required_fields - set(values.keys()) - if missing_fields: - raise ValueError(f"Missing fields: {missing_fields}") - return values - - new_class.__validator_check_name = classmethod(check_name) - new_class.__root_validator_check_missing_fields = classmethod(check_missing_fields) - return new_class diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index ae1e0379c..411051199 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -69,7 +69,7 @@ class WritePRD(Action): content: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) - async def run(self, with_messages, format=CONFIG.prompt_format, *args, **kwargs) -> ActionOutput | Message: + async def run(self, with_messages, schema=CONFIG.prompt_schema, *args, **kwargs) -> ActionOutput | Message: # Determine which requirement documents need to be rewritten: Use LLM to assess whether new requirements are # related to the PRD. If they are related, rewrite the PRD. docs_file_repo = CONFIG.git_repo.new_file_repository(relative_path=DOCS_FILE_REPO) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 9a758da34..d4db5985b 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -5,7 +5,12 @@ import copy import pickle +<<<<<<< HEAD from metagpt.utils.common import import_class +======= +from metagpt.actions.action_node import ActionNode +from metagpt.schema import Message +>>>>>>> 09e2f05 (refactor action_output and action_node) def actionoutout_schema_to_mapping(schema: dict) -> dict: @@ -104,13 +109,11 @@ def deserialize_general_message(message_dict: dict) -> "Message": return message -def deserialize_message(message_ser: str) -> "Message": +def deserialize_message(message_ser: str) -> Message: message = pickle.loads(message_ser) if message.instruct_content: ic = message.instruct_content - - actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") - ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + ic_obj = ActionNode.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new diff --git a/tests/metagpt/actions/test_action_output.py b/tests/metagpt/actions/test_action_output.py index ef8e239bd..f1765cb03 100644 --- a/tests/metagpt/actions/test_action_output.py +++ b/tests/metagpt/actions/test_action_output.py @@ -7,7 +7,7 @@ """ from typing import List, Tuple -from metagpt.actions import ActionOutput +from metagpt.actions.action_node import ActionNode t_dict = { "Required Python third-party packages": '"""\nflask==1.1.2\npygame==2.0.1\n"""\n', @@ -37,12 +37,12 @@ WRITE_TASKS_OUTPUT_MAPPING = { def test_create_model_class(): - test_class = ActionOutput.create_model_class("test_class", WRITE_TASKS_OUTPUT_MAPPING) + test_class = ActionNode.create_model_class("test_class", WRITE_TASKS_OUTPUT_MAPPING) assert test_class.__name__ == "test_class" def test_create_model_class_with_mapping(): - t = ActionOutput.create_model_class("test_class_1", WRITE_TASKS_OUTPUT_MAPPING) + t = ActionNode.create_model_class("test_class_1", WRITE_TASKS_OUTPUT_MAPPING) t1 = t(**t_dict) value = t1.dict()["Task list"] assert value == ["game.py", "app.py", "static/css/styles.css", "static/js/script.js", "templates/index.html"] diff --git a/tests/metagpt/memory/test_memory_storage.py b/tests/metagpt/memory/test_memory_storage.py index c67ca689f..7b74eb512 100644 --- a/tests/metagpt/memory/test_memory_storage.py +++ b/tests/metagpt/memory/test_memory_storage.py @@ -8,7 +8,7 @@ from typing import List from metagpt.actions import UserRequirement, WritePRD -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.memory.memory_storage import MemoryStorage from metagpt.schema import Message @@ -42,7 +42,7 @@ def test_idea_message(): def test_actionout_message(): out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} - ic_obj = ActionOutput.create_model_class("prd", out_mapping) + ic_obj = ActionNode.create_model_class("prd", out_mapping) role_id = "UTUser2(Architect)" content = "The user has requested the creation of a command-line interface (CLI) snake game" diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index ffa34866c..f027d53f8 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -7,7 +7,7 @@ from typing import List, Tuple from metagpt.actions import WritePRD -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.schema import Message from metagpt.utils.serialize import ( actionoutout_schema_to_mapping, @@ -54,7 +54,7 @@ def test_actionoutout_schema_to_mapping(): def test_serialize_and_deserialize_message(): out_mapping = {"field1": (str, ...), "field2": (List[str], ...)} out_data = {"field1": "field1 value", "field2": ["field2 value1", "field2 value2"]} - ic_obj = ActionOutput.create_model_class("prd", out_mapping) + ic_obj = ActionNode.create_model_class("prd", out_mapping) message = Message( content="prd demand", instruct_content=ic_obj(**out_data), role="user", cause_by=WritePRD From a06acbbbe8fc8ada928cfd82bdeab36ecba9e5c9 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 21:32:52 +0800 Subject: [PATCH 386/413] refine code --- metagpt/actions/action_node.py | 2 +- metagpt/actions/write_prd_an.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 865cb2d32..790069369 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -232,7 +232,7 @@ class ActionNode(Generic[T]): return prompt @retry( - wait=wait_random_exponential(min=1, max=60), + wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6), after=general_after_log(logger), ) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index d96c0aeac..edd94a463 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -47,7 +47,7 @@ PRODUCT_GOALS = ActionNode( USER_STORIES = ActionNode( key="User Stories", expected_type=list[str], - instruction="Provide up to five scenario-based user stories.", + instruction="Provide up to 3 to 5 scenario-based user stories.", example=[ "As a user, I want to be able to choose difficulty levels", "As a player, I want to see my score after each game", @@ -57,7 +57,7 @@ USER_STORIES = ActionNode( COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", expected_type=list[str], - instruction="Provide analyses for up to seven competitive products.", + instruction="Provide 5 to 7 competitive products.", example=["Python Snake Game: Simple interface, lacks advanced features"], ) @@ -92,8 +92,8 @@ REQUIREMENT_ANALYSIS = ActionNode( REQUIREMENT_POOL = ActionNode( key="Requirement Pool", expected_type=list[list[str]], - instruction="List down the requirements with their priority (P0, P1, P2).", - example=[["P0", "..."], ["P1", "..."]], + instruction="List down the top-5 requirements with their priority (P0, P1, P2).", + example=[["P0", "The main code ..."], ["P0", "The game algorithm ..."]], ) UI_DESIGN_DRAFT = ActionNode( From 4d78dbce406dc85e90eb865037b883de278390d5 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 23:53:04 +0800 Subject: [PATCH 387/413] refine code. move azure tts to tool, refactor actions --- metagpt/actions/__init__.py | 2 - metagpt/actions/action.py | 3 +- metagpt/actions/analyze_dep_libs.py | 37 ------------------- metagpt/actions/design_filenames.py | 30 --------------- ...detail_mining.py => generate_questions.py} | 18 ++------- metagpt/schema.py | 3 +- metagpt/{actions => tools}/azure_tts.py | 19 ++++------ metagpt/utils/serialize.py | 4 +- tests/metagpt/actions/test_azure_tts.py | 4 +- tests/metagpt/actions/test_detail_mining.py | 20 ++++++---- 10 files changed, 32 insertions(+), 108 deletions(-) delete mode 100644 metagpt/actions/analyze_dep_libs.py delete mode 100644 metagpt/actions/design_filenames.py rename metagpt/actions/{detail_mining.py => generate_questions.py} (69%) rename metagpt/{actions => tools}/azure_tts.py (65%) diff --git a/metagpt/actions/__init__.py b/metagpt/actions/__init__.py index 79ff94b3e..c34c72ed2 100644 --- a/metagpt/actions/__init__.py +++ b/metagpt/actions/__init__.py @@ -13,7 +13,6 @@ from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.debug_error import DebugError from metagpt.actions.design_api import WriteDesign from metagpt.actions.design_api_review import DesignReview -from metagpt.actions.design_filenames import DesignFilenames from metagpt.actions.project_management import AssignTasks, WriteTasks from metagpt.actions.research import CollectLinks, WebBrowseAndSummarize, ConductResearch from metagpt.actions.run_code import RunCode @@ -33,7 +32,6 @@ class ActionType(Enum): WRITE_PRD_REVIEW = WritePRDReview WRITE_DESIGN = WriteDesign DESIGN_REVIEW = DesignReview - DESIGN_FILENAMES = DesignFilenames WRTIE_CODE = WriteCode WRITE_CODE_REVIEW = WriteCodeReview WRITE_TEST = WriteTest diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 1fcc8fc80..e18983d7d 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -14,6 +14,7 @@ from pydantic import BaseModel, Field from metagpt.actions.action_node import ActionNode from metagpt.llm import LLM from metagpt.provider.base_gpt_api import BaseGPTAPI +from metagpt.schema import CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext action_subclass_registry = {} @@ -22,7 +23,7 @@ action_subclass_registry = {} class Action(BaseModel): name: str = "" llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) - context = "" + context: dict | CodingContext | CodeSummarizeContext | TestingContext | RunCodeContext | str | None = "" prefix = "" # aask*时会加上prefix,作为system_message desc = "" # for skill manager node: ActionNode = Field(default_factory=ActionNode, exclude=True) diff --git a/metagpt/actions/analyze_dep_libs.py b/metagpt/actions/analyze_dep_libs.py deleted file mode 100644 index 53d40200a..000000000 --- a/metagpt/actions/analyze_dep_libs.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/19 12:01 -@Author : alexanderwu -@File : analyze_dep_libs.py -""" - -from metagpt.actions import Action - -PROMPT = """You are an AI developer, trying to write a program that generates code for users based on their intentions. - -For the user's prompt: - ---- -The API is: {prompt} ---- - -We decide the generated files are: {filepaths_string} - -Now that we have a file list, we need to understand the shared dependencies they have. -Please list and briefly describe the shared contents between the files we are generating, including exported variables, -data patterns, id names of all DOM elements that javascript functions will use, message names and function names. -Focus only on the names of shared dependencies, do not add any other explanations. -""" - - -class AnalyzeDepLibs(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = "Analyze the runtime dependencies of the program based on the context" - - async def run(self, requirement, filepaths_string): - # prompt = f"Below is the product requirement document (PRD):\n\n{prd}\n\n{PROMPT}" - prompt = PROMPT.format(prompt=requirement, filepaths_string=filepaths_string) - design_filenames = await self._aask(prompt) - return design_filenames diff --git a/metagpt/actions/design_filenames.py b/metagpt/actions/design_filenames.py deleted file mode 100644 index ffa171d7b..000000000 --- a/metagpt/actions/design_filenames.py +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -@Time : 2023/5/19 11:50 -@Author : alexanderwu -@File : design_filenames.py -""" -from metagpt.actions import Action -from metagpt.logs import logger - -PROMPT = """You are an AI developer, trying to write a program that generates code for users based on their intentions. -When given their intentions, provide a complete and exhaustive list of file paths needed to write the program for the user. -Only list the file paths you will write and return them as a Python string list. -Do not add any other explanations, just return a Python string list.""" - - -class DesignFilenames(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.desc = ( - "Based on the PRD, consider system design, and carry out the basic design of the corresponding " - "APIs, data structures, and database tables. Please give your design, feedback clearly and in detail." - ) - - async def run(self, prd): - prompt = f"The following is the Product Requirement Document (PRD):\n\n{prd}\n\n{PROMPT}" - design_filenames = await self._aask(prompt) - logger.debug(prompt) - logger.debug(design_filenames) - return design_filenames diff --git a/metagpt/actions/detail_mining.py b/metagpt/actions/generate_questions.py similarity index 69% rename from metagpt/actions/detail_mining.py rename to metagpt/actions/generate_questions.py index 0314d30dd..c38c463bc 100644 --- a/metagpt/actions/detail_mining.py +++ b/metagpt/actions/generate_questions.py @@ -3,19 +3,11 @@ """ @Time : 2023/9/12 17:45 @Author : fisherdeng -@File : detail_mining.py +@File : generate_questions.py """ from metagpt.actions import Action from metagpt.actions.action_node import ActionNode -CONTEXT_TEMPLATE = """ -## TOPIC -{topic} - -## RECORD -{record} -""" - QUESTIONS = ActionNode( key="Questions", expected_type=list[str], @@ -25,11 +17,9 @@ QUESTIONS = ActionNode( ) -class DetailMining(Action): +class GenerateQuestions(Action): """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and "##RECORD" (discussion records), thereby deepening the discussion.""" - async def run(self, topic, record): - context = CONTEXT_TEMPLATE.format(topic=topic, record=record) - rsp = await QUESTIONS.fill(context=context, llm=self.llm) - return rsp + async def run(self, context): + return await QUESTIONS.fill(context=context, llm=self.llm) diff --git a/metagpt/schema.py b/metagpt/schema.py index 59203c404..327bfd2d1 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -19,6 +19,7 @@ import asyncio import json import os.path import uuid +from abc import ABC from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path @@ -281,7 +282,7 @@ class MessageQueue(BaseModel): T = TypeVar("T", bound="BaseModel") -class BaseContext(BaseModel): +class BaseContext(BaseModel, ABC): @classmethod @handle_exception def loads(cls: Type[T], val: str) -> Optional[T]: diff --git a/metagpt/actions/azure_tts.py b/metagpt/tools/azure_tts.py similarity index 65% rename from metagpt/actions/azure_tts.py rename to metagpt/tools/azure_tts.py index daa3f6892..e59d98016 100644 --- a/metagpt/actions/azure_tts.py +++ b/metagpt/tools/azure_tts.py @@ -7,19 +7,16 @@ """ from azure.cognitiveservices.speech import AudioConfig, SpeechConfig, SpeechSynthesizer -from metagpt.actions.action import Action -from metagpt.config import Config +from metagpt.config import CONFIG -class AzureTTS(Action): - def __init__(self, name, context=None, llm=None): - super().__init__(name, context, llm) - self.config = Config() +class AzureTTS: + """https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles""" - # Parameters reference: https://learn.microsoft.com/zh-cn/azure/cognitive-services/speech-service/language-support?tabs=tts#voice-styles-and-roles - def synthesize_speech(self, lang, voice, role, text, output_file): - subscription_key = self.config.get("AZURE_TTS_SUBSCRIPTION_KEY") - region = self.config.get("AZURE_TTS_REGION") + @classmethod + def synthesize_speech(cls, lang, voice, role, text, output_file): + subscription_key = CONFIG.get("AZURE_TTS_SUBSCRIPTION_KEY") + region = CONFIG.get("AZURE_TTS_REGION") speech_config = SpeechConfig(subscription=subscription_key, region=region) speech_config.speech_synthesis_voice_name = voice @@ -41,5 +38,5 @@ class AzureTTS(Action): if __name__ == "__main__": - azure_tts = AzureTTS("azure_tts") + azure_tts = AzureTTS() azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "Hello, I am Kaka", "output.wav") diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index d4db5985b..8ad46a120 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -5,12 +5,10 @@ import copy import pickle -<<<<<<< HEAD + from metagpt.utils.common import import_class -======= from metagpt.actions.action_node import ActionNode from metagpt.schema import Message ->>>>>>> 09e2f05 (refactor action_output and action_node) def actionoutout_schema_to_mapping(schema: dict) -> dict: diff --git a/tests/metagpt/actions/test_azure_tts.py b/tests/metagpt/actions/test_azure_tts.py index bcafe10f5..9995e9691 100644 --- a/tests/metagpt/actions/test_azure_tts.py +++ b/tests/metagpt/actions/test_azure_tts.py @@ -5,11 +5,11 @@ @Author : alexanderwu @File : test_azure_tts.py """ -from metagpt.actions.azure_tts import AzureTTS +from metagpt.tools.azure_tts import AzureTTS def test_azure_tts(): - azure_tts = AzureTTS("azure_tts") + azure_tts = AzureTTS() azure_tts.synthesize_speech("zh-CN", "zh-CN-YunxiNeural", "Boy", "你好,我是卡卡", "output.wav") # 运行需要先配置 SUBSCRIPTION_KEY diff --git a/tests/metagpt/actions/test_detail_mining.py b/tests/metagpt/actions/test_detail_mining.py index 30bcf9dfb..a178ec840 100644 --- a/tests/metagpt/actions/test_detail_mining.py +++ b/tests/metagpt/actions/test_detail_mining.py @@ -3,20 +3,26 @@ """ @Time : 2023/9/13 00:26 @Author : fisherdeng -@File : test_detail_mining.py +@File : test_generate_questions.py """ import pytest -from metagpt.actions.detail_mining import DetailMining +from metagpt.actions.generate_questions import GenerateQuestions from metagpt.logs import logger +context = """ +## topic +如何做一个生日蛋糕 + +## record +我认为应该先准备好材料,然后再开始做蛋糕。 +""" + @pytest.mark.asyncio -async def test_detail_mining(): - topic = "如何做一个生日蛋糕" - record = "我认为应该先准备好材料,然后再开始做蛋糕。" - detail_mining = DetailMining("detail_mining") - rsp = await detail_mining.run(topic=topic, record=record) +async def test_generate_questions(): + detail_mining = GenerateQuestions() + rsp = await detail_mining.run(context) logger.info(f"{rsp.content=}") assert "Questions" in rsp.content From b4af3b6270fe1b4d6f57283964e29e6a0d8b1a19 Mon Sep 17 00:00:00 2001 From: geekan Date: Tue, 19 Dec 2023 23:58:18 +0800 Subject: [PATCH 388/413] refine code --- metagpt/actions/action_node.py | 52 +++++++++++++++++----------------- metagpt/actions/design_api.py | 4 +-- metagpt/actions/write_prd.py | 4 +-- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 790069369..092dd5755 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -112,15 +112,15 @@ class ActionNode(Generic[T]): obj.add_children(nodes) return obj - def get_children_mapping(self) -> Dict[str, Type]: + def get_children_mapping(self) -> Dict[str, Tuple[Type, Any]]: """获得子ActionNode的字典,以key索引""" return {k: (v.expected_type, ...) for k, v in self.children.items()} - def get_self_mapping(self) -> Dict[str, Type]: + def get_self_mapping(self) -> Dict[str, Tuple[Type, Any]]: """get self key: type mapping""" return {self.key: (self.expected_type, ...)} - def get_mapping(self, mode="children") -> Dict[str, Type]: + def get_mapping(self, mode="children") -> Dict[str, Tuple[Type, Any]]: """get key: type mapping under mode""" if mode == "children" or (mode == "auto" and self.children): return self.get_children_mapping() @@ -175,46 +175,46 @@ class ActionNode(Generic[T]): return node_dict # 遍历子节点并递归调用 to_dict 方法 - for child_key, child_node in self.children.items(): + for _, child_node in self.children.items(): node_dict.update(child_node.to_dict(format_func)) return node_dict - def compile_to(self, i: Dict, to) -> str: - if to == "json": + def compile_to(self, i: Dict, schema) -> str: + if schema == "json": return json.dumps(i, indent=4) - elif to == "markdown": + elif schema == "markdown": return dict_to_markdown(i) else: return str(i) - def tagging(self, text, to, tag="") -> str: + def tagging(self, text, schema, tag="") -> str: if not tag: return text - if to == "json": + if schema == "json": return f"[{tag}]\n" + text + f"\n[/{tag}]" else: return f"[{tag}]\n" + text + f"\n[/{tag}]" - def _compile_f(self, to, mode, tag, format_func) -> str: + def _compile_f(self, schema, mode, tag, format_func) -> str: nodes = self.to_dict(format_func=format_func, mode=mode) - text = self.compile_to(nodes, to) - return self.tagging(text, to, tag) + text = self.compile_to(nodes, schema) + return self.tagging(text, schema, tag) - def compile_instruction(self, to="raw", mode="children", tag="") -> str: + def compile_instruction(self, schema="raw", mode="children", tag="") -> str: """compile to raw/json/markdown template with all/root/children nodes""" format_func = lambda i: f"{i.expected_type} # {i.instruction}" - return self._compile_f(to, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func) - def compile_example(self, to="raw", mode="children", tag="") -> str: + def compile_example(self, schema="raw", mode="children", tag="") -> str: """compile to raw/json/markdown examples with all/root/children nodes""" # 这里不能使用f-string,因为转译为str后再json.dumps会额外加上引号,无法作为有效的example # 错误示例:"File list": "['main.py', 'const.py', 'game.py']", 注意这里值不是list,而是str format_func = lambda i: i.example - return self._compile_f(to, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func) - def compile(self, context, to="json", mode="children", template=SIMPLE_TEMPLATE) -> str: + def compile(self, context, schema="json", mode="children", template=SIMPLE_TEMPLATE) -> str: """ mode: all/root/children mode="children": 编译所有子节点为一个统一模板,包括instruction与example @@ -224,8 +224,8 @@ class ActionNode(Generic[T]): # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", # compile example暂时不支持markdown - self.instruction = self.compile_instruction(to="markdown", mode=mode) - self.example = self.compile_example(to=to, tag="CONTENT", mode=mode) + self.instruction = self.compile_instruction(schema="markdown", mode=mode) + self.example = self.compile_example(schema=schema, tag="CONTENT", mode=mode) prompt = template.format( context=context, example=self.example, instruction=self.instruction, constraint=CONSTRAINT ) @@ -272,22 +272,22 @@ class ActionNode(Generic[T]): def set_context(self, context): self.set_recursive("context", context) - async def simple_fill(self, to, mode): - prompt = self.compile(context=self.context, to=to, mode=mode) + async def simple_fill(self, schema, mode): + prompt = self.compile(context=self.context, schema=schema, mode=mode) mapping = self.get_mapping(mode) class_name = f"{self.key}_AN" - content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=to) + content, scontent = await self._aask_v1(prompt, class_name, mapping, schema=schema) self.content = content self.instruct_content = scontent return self - async def fill(self, context, llm, to="json", mode="auto", strgy="simple"): + async def fill(self, context, llm, schema="json", mode="auto", strgy="simple"): """Fill the node(s) with mode. :param context: Everything we should know when filling node. :param llm: Large Language Model with pre-defined system message. - :param to: json/markdown, determine example and output format. + :param schema: json/markdown, determine example and output format. - json: it's easy to open source LLM with json format - markdown: when generating code, markdown is always better :param mode: auto/children/root @@ -303,12 +303,12 @@ class ActionNode(Generic[T]): self.set_context(context) if strgy == "simple": - return await self.simple_fill(to, mode) + return await self.simple_fill(schema, mode) elif strgy == "complex": # 这里隐式假设了拥有children tmp = {} for _, i in self.children.items(): - child = await i.simple_fill(to, mode) + child = await i.simple_fill(schema, mode) tmp.update(child.instruct_content.dict()) cls = self.create_children_class() self.instruct_content = cls(**tmp) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 49c5a019d..f5e122356 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -80,12 +80,12 @@ class WriteDesign(Action): return ActionOutput(content=changed_files.json(), instruct_content=changed_files) async def _new_system_design(self, context, schema=CONFIG.prompt_schema): - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, schema=schema) return node async def _merge(self, prd_doc, system_design_doc, schema=CONFIG.prompt_schema): context = NEW_REQ_TEMPLATE.format(old_design=system_design_doc.content, context=prd_doc.content) - node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, to=schema) + node = await DESIGN_API_NODE.fill(context=context, llm=self.llm, schema=schema) system_design_doc.content = node.instruct_content.json(ensure_ascii=False) return system_design_doc diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index 411051199..df66e6442 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -123,7 +123,7 @@ class WritePRD(Action): # logger.info(rsp) project_name = CONFIG.project_name if CONFIG.project_name else "" context = CONTEXT_TEMPLATE.format(requirements=requirements, project_name=project_name) - node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, to=schema) + node = await WRITE_PRD_NODE.fill(context=context, llm=self.llm, schema=schema) await self._rename_workspace(node) return node @@ -136,7 +136,7 @@ class WritePRD(Action): if not CONFIG.project_name: CONFIG.project_name = Path(CONFIG.project_path).name prompt = NEW_REQ_TEMPLATE.format(requirements=new_requirement_doc.content, old_prd=prd_doc.content) - node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, to=schema) + node = await WRITE_PRD_NODE.fill(context=prompt, llm=self.llm, schema=schema) prd_doc.content = node.instruct_content.json(ensure_ascii=False) await self._rename_workspace(node) return prd_doc From 8107861302f4a31a624372ce4a1f59ed64f0276f Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:34:57 +0800 Subject: [PATCH 389/413] refine devcontainer README --- .devcontainer/README.md | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/.devcontainer/README.md b/.devcontainer/README.md index dd088aab1..be692c14d 100644 --- a/.devcontainer/README.md +++ b/.devcontainer/README.md @@ -1,39 +1,34 @@ -# Dev container +# Dev Container -This project includes a [dev container](https://containers.dev/), which lets you use a container as a full-featured dev environment. +This project includes a [Dev Container](https://containers.dev/), offering you a comprehensive and fully-featured development environment within a container. By leveraging the Dev Container configuration in this folder, you can seamlessly build and initiate MetaGPT locally. For detailed information, please refer to the main README in the home directory. -You can use the dev container configuration in this folder to build and start running MetaGPT locally! For more, refer to the main README under the home directory. -You can use it in [GitHub Codespaces](https://github.com/features/codespaces) or the [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). +You can utilize this Dev Container in [GitHub Codespaces](https://github.com/features/codespaces) or with the [VS Code Dev Containers extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers). ## GitHub Codespaces -Open in GitHub Codespaces +[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/geekan/MetaGPT) -You may use the button above to open this repo in a Codespace +Click the button above to open this repository in a Codespace. For additional information, refer to the [GitHub documentation on creating a Codespace](https://docs.github.com/en/free-pro-team@latest/github/developing-online-with-codespaces/creating-a-codespace#creating-a-codespace). -For more info, check out the [GitHub documentation](https://docs.github.com/en/free-pro-team@latest/github/developing-online-with-codespaces/creating-a-codespace#creating-a-codespace). - ## VS Code Dev Containers -Open in Dev Containers +[![Open in Dev Containers](https://img.shields.io/static/v1?label=Dev%20Containers&message=Open&color=blue&logo=visualstudiocode)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/geekan/MetaGPT) -Note: If you click this link you will open the main repo and not your local cloned repo, you can use this link and replace with your username and cloned repo name: -https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/geekan/MetaGPT +Note: Clicking the link above opens the main repository. To open your local cloned repository, replace the URL with your username and cloned repository's name: `https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com//` +If you have VS Code and Docker installed, use the button above to get started. This will prompt VS Code to install the Dev Containers extension if it's not already installed, clone the source code into a container volume, and set up a dev container for you. -If you already have VS Code and Docker installed, you can use the button above to get started. This will cause VS Code to automatically install the Dev Containers extension if needed, clone the source code into a container volume, and spin up a dev container for use. +Alternatively, follow these steps to open this repository in a container using the VS Code Dev Containers extension: -You can also follow these steps to open this repo in a container using the VS Code Dev Containers extension: +1. For first-time users of a development container, ensure your system meets the prerequisites (e.g., Docker installation) as outlined in the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started). -1. If this is your first time using a development container, please ensure your system meets the pre-reqs (i.e. have Docker installed) in the [getting started steps](https://aka.ms/vscode-remote/containers/getting-started). - -2. Open a locally cloned copy of the code: - - - Fork and Clone this repository to your local filesystem. +2. To open a locally cloned copy of the code: + - Fork and clone this repository to your local file system. - Press F1 and select the **Dev Containers: Open Folder in Container...** command. - - Select the cloned copy of this folder, wait for the container to start, and try things out! + - Choose the cloned folder, wait for the container to initialize, and start exploring! -You can learn more in the [Dev Containers documentation](https://code.visualstudio.com/docs/devcontainers/containers). +Learn more in the [VS Code Dev Containers documentation](https://code.visualstudio.com/docs/devcontainers/containers). -## Tips and tricks +## Tips and Tricks -* If you are working with the same repository folder in a container and Windows, you'll want consistent line endings (otherwise you may see hundreds of changes in the SCM view). The `.gitattributes` file in the root of this repo will disable line ending conversion and should prevent this. See [tips and tricks](https://code.visualstudio.com/docs/devcontainers/tips-and-tricks#_resolving-git-line-ending-issues-in-containers-resulting-in-many-modified-files) for more info. -* If you'd like to review the contents of the image used in this dev container, you can check it out in the [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/python) repo. +* When working with the same repository folder in both a container and on Windows, it's crucial to have consistent line endings to avoid numerous changes in the SCM view. The `.gitattributes` file in the root of this repository disables line ending conversion, helping to prevent this issue. For more information, see [resolving git line ending issues in containers](https://code.visualstudio.com/docs/devcontainers/tips-and-tricks#_resolving-git-line-ending-issues-in-containers-resulting-in-many-modified-files). + +* If you're curious about the contents of the image used in this Dev Container, you can review it in the [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/python) repository. From a7b909e6fe816cb1840a480614f7a77074275ca8 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:35:15 +0800 Subject: [PATCH 390/413] add proper space --- .devcontainer/postCreateCommand.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devcontainer/postCreateCommand.sh b/.devcontainer/postCreateCommand.sh index 46788e306..3901193cd 100644 --- a/.devcontainer/postCreateCommand.sh +++ b/.devcontainer/postCreateCommand.sh @@ -4,4 +4,4 @@ sudo npm install -g @mermaid-js/mermaid-cli # Step 2: Ensure that Python 3.9+ is installed on your system. You can check this by using: python --version -pip install -e. \ No newline at end of file +pip install -e . \ No newline at end of file From 111e820722ed814d8321e34d3604e52ba96a5436 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:39:35 +0800 Subject: [PATCH 391/413] .gitattributes: ensure lf --- .gitattributes | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/.gitattributes b/.gitattributes index 32555a806..7f1424434 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,29 @@ +# HTML code is incorrectly calculated into statistics, so ignore them *.html linguist-detectable=false +# Auto detect text files and perform LF normalization +* text=auto eol=lf + +# Ensure shell scripts use LF (Linux style) line endings on Windows +*.sh text eol=lf + +# Treat specific binary files as binary and prevent line ending conversion +*.png binary +*.jpg binary +*.gif binary +*.ico binary + +# Preserve original line endings for specific document files +*.doc text eol=crlf +*.docx text eol=crlf +*.pdf binary + +# Ensure source code and script files use LF line endings +*.py text eol=lf +*.js text eol=lf +*.html text eol=lf +*.css text eol=lf + +# Specify custom diff driver for specific file types +*.md diff=markdown +*.json diff=json From 250c5503de0374d37c9d153a75f7f84708bc2319 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:47:28 +0800 Subject: [PATCH 392/413] refine .gitignore and .pre-commit-config.yaml --- .gitignore | 8 +------- .pre-commit-config.yaml | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 0ac318ff5..c12506b0e 100644 --- a/.gitignore +++ b/.gitignore @@ -144,24 +144,18 @@ cython_debug/ allure-report allure-results -# idea +# idea / vscode / macos .idea .DS_Store .vscode -log.txt -docs/scripts/set_env.sh key.yaml -output.json data -data/output_add.json data.ms examples/nb/ .chroma *~$* workspace/* -*.mmd tmp -output.wav metagpt/roles/idea_agent.py .aider* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b1892a709..338f832ac 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ default_stages: [ commit ] # Install # 1. pip install pre-commit -# 2. pre-commit install(the first time you download the repo, it will be cached for future use) +# 2. pre-commit install repos: - repo: https://github.com/pycqa/isort rev: 5.11.5 From ec6493a748bce00b768a81caea2ff59cf729c40b Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:49:08 +0800 Subject: [PATCH 393/413] updating time of license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 5b0c000cd..67460e101 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License -Copyright (c) Chenglin Wu +Copyright (c) 2023 Chenglin Wu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From d85adbd6402d85425e9891aa10a060d77b9af489 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:53:36 +0800 Subject: [PATCH 394/413] align ruff.toml with black --- ruff.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruff.toml b/ruff.toml index 7835865e0..21de5ee14 100644 --- a/ruff.toml +++ b/ruff.toml @@ -31,7 +31,7 @@ exclude = [ ] # Same as Black. -line-length = 119 +line-length = 120 # Allow unused variables when underscore-prefixed. dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" From 3a44b89ad882297d33c441745ec80e686ccc29a6 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:54:29 +0800 Subject: [PATCH 395/413] uncomment fire in requirements.txt due to usage in the example --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 515a4d88b..f5ef63c58 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,7 +6,7 @@ channels==4.0.0 # docx==0.2.4 #faiss==1.5.3 faiss_cpu==1.7.4 -# fire==0.4.0 +fire==0.4.0 typer # godot==0.1.1 # google_api_python_client==2.93.0 From de23c23839b29d04209fb2781cf702043e9c16c7 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:58:56 +0800 Subject: [PATCH 396/413] add proper space --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c6e22989b..9eeacbccb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ COPY . /app/metagpt WORKDIR /app/metagpt RUN mkdir workspace &&\ pip install --no-cache-dir -r requirements.txt &&\ - pip install -e. + pip install -e . # Running with an infinite loop using the tail command CMD ["sh", "-c", "tail -f /dev/null"] From 2abc211e0d10a9e92ca79c7bc717985e206bb61b Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 00:59:23 +0800 Subject: [PATCH 397/413] remove duplicate string --- .dockerignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.dockerignore b/.dockerignore index 2968dd34d..8c09eaf73 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ workspace tmp build -workspace dist data geckodriver.log From 9eaf08b7dd47398be1c4a4c1fd810a529129e7d5 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 11:52:11 +0800 Subject: [PATCH 398/413] refine code for prepare document. remove useless logic --- metagpt/actions/prepare_documents.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 6bb18be7b..696dc9a89 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -15,7 +15,7 @@ from pydantic import Field from metagpt.actions import Action, ActionOutput from metagpt.config import CONFIG -from metagpt.const import DEFAULT_WORKSPACE_ROOT, DOCS_FILE_REPO, REQUIREMENT_FILENAME +from metagpt.const import DOCS_FILE_REPO, REQUIREMENT_FILENAME from metagpt.llm import LLM from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Document @@ -24,22 +24,26 @@ from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): + """PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt.""" + name: str = "PrepareDocuments" context: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) + def _init_repo(self): + """Initialize the Git environment.""" + path = CONFIG.project_path + if not path: + name = CONFIG.project_name or FileRepository.new_filename() + path = Path(CONFIG.workspace_path) / name + + if path.exists() and not CONFIG.inc: + shutil.rmtree(path) + CONFIG.git_repo = GitRepository(local_path=path, auto_init=True) + async def run(self, with_messages, **kwargs): - if not CONFIG.git_repo: - # Create and initialize the workspace folder, initialize the Git environment. - project_name = CONFIG.project_name or FileRepository.new_filename() - workdir = CONFIG.project_path - if not workdir and CONFIG.workspace_path: - workdir = Path(CONFIG.workspace_path) / project_name - workdir = Path(workdir or DEFAULT_WORKSPACE_ROOT / project_name) - if not CONFIG.inc and workdir.exists(): - shutil.rmtree(workdir) - CONFIG.git_repo = GitRepository() - CONFIG.git_repo.open(local_path=workdir, auto_init=True) + """Create and initialize the workspace folder, initialize the Git environment.""" + self._init_repo() # Write the newly added requirements from the main parameter idea to `docs/requirement.txt`. doc = Document(root_path=DOCS_FILE_REPO, filename=REQUIREMENT_FILENAME, content=with_messages[0].content) From 608e0e9f16e1f1d2d081dd784621bdf23b684446 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 11:59:59 +0800 Subject: [PATCH 399/413] add .pylintrc --- docs/.pylintrc | 639 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 639 insertions(+) create mode 100644 docs/.pylintrc diff --git a/docs/.pylintrc b/docs/.pylintrc new file mode 100644 index 000000000..9e8488bc7 --- /dev/null +++ b/docs/.pylintrc @@ -0,0 +1,639 @@ +[MAIN] + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Clear in-memory caches upon conclusion of linting. Useful if running pylint +# in a server-like mode. +clear-cache-post-run=no + +# Load and enable all available extensions. Use --list-extensions to see a list +# all available extensions. +#enable-all-extensions= + +# In error mode, messages with a category besides ERROR or FATAL are +# suppressed, and no reports are done by default. Error mode is compatible with +# disabling specific errors. +#errors-only= + +# Always return a 0 (non-error) status code, even if lint errors are found. +# This is primarily useful in continuous integration scripts. +#exit-zero= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist=pydantic + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold under which the program will exit with error. +fail-under=10 + +# Interpret the stdin as a python script, whose filename needs to be passed as +# the module_or_package argument. +#from-stdin= + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regular expressions patterns to the +# ignore-list. The regex matches against paths and can be in Posix or Windows +# format. Because '\\' represents the directory delimiter on Windows systems, +# it can't be used as an escape character. +ignore-paths= + +# Files or directories matching the regular expression patterns are skipped. +# The regex matches against base names, not paths. The default value ignores +# Emacs file locks +#ignore-patterns=^\.# +ignore-patterns=(.)*_test\.py,test_(.)*\.py + + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use, and will cap the count on Windows to +# avoid hangs. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=120 + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.9 + +# Discover python modules and packages in the file system subtree. +recursive=no + +# Add paths to the list of the source roots. Supports globbing patterns. The +# source root is an absolute path or a path relative to the current working +# directory used to determine a package namespace for modules located under the +# source root. +source-roots= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + +# In verbose mode, extra non-checker-related info will be displayed. +#verbose= + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. If left empty, argument names will be checked with the set +# naming style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. If left empty, attribute names will be checked with the set naming +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. If left empty, class attribute names will be checked +# with the set naming style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. If left empty, class constant names will be checked with +# the set naming style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. If left empty, class names will be checked with the set naming style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. If left empty, constant names will be checked with the set naming +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. If left empty, function names will be checked with the set +# naming style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + v, + e, + d, + m, + df, + ex, + Run, + _ + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. If left empty, inline iteration names will be checked +# with the set naming style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. If left empty, method names will be checked with the set naming style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. If left empty, module names will be checked with the set naming style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Regular expression matching correct type alias names. If left empty, type +# alias names will be checked with the set naming style. +#typealias-rgx= + +# Regular expression matching correct type variable names. If left empty, type +# variable names will be checked with the set naming style. +#typevar-rgx= + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. If left empty, variable names will be checked with the set +# naming style. +#variable-rgx= + + +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict,_fields,_replace,_source,_make,os._exit + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=mcs + + +[DESIGN] + +# List of regular expressions of class ancestor names to ignore when counting +# public methods (see R0903) +exclude-too-few-public-methods= + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement (see R0916). +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when caught. +overgeneral-exceptions=builtins.BaseException,builtins.Exception + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow explicit reexports by alias from a package __init__. +allow-reexport-from-package=no + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, +# UNDEFINED. +confidence=HIGH, + CONTROL_FLOW, + INFERENCE, + INFERENCE_FAILURE, + UNDEFINED + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then re-enable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + expression-not-assigned, + pointless-statement + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[METHOD_ARGS] + +# List of qualified names (i.e., library.method) which require a timeout +# parameter e.g. 'requests.api.get,requests.api.post' +timeout-methods=requests.api.delete,requests.api.get,requests.api.head,requests.api.options,requests.api.patch,requests.api.post,requests.api.put,requests.api.request + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +notes-rgx= + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'fatal', 'error', 'warning', 'refactor', +# 'convention', and 'info' which contain the number of messages in each +# category, as well as 'statement' which is the total number of statements +# analyzed. This score is used by the global evaluation report (RP0004). +evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +#output-format= + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=yes + +# Signatures are removed from the similarity computation +ignore-signatures=yes + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. No available dictionaries : You need to install +# both the python package and the system dependency for enchant to work.. +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear at the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of symbolic message names to ignore for Mixin members. +ignored-checks-for-mixins=no-member, + not-async-context-manager, + not-context-manager, + attribute-defined-outside-init + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# Regex pattern to define which classes are considered mixins. +mixin-class-rgx=.*[Mm]ixin + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io From d5913970d545d08218285c25fddc9c5e0d625ec7 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 12:48:57 +0800 Subject: [PATCH 400/413] refine sop --- metagpt/actions/write_prd_an.py | 21 ++++++++++++++------- metagpt/roles/product_manager.py | 4 ++-- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index edd94a463..8698c739f 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -26,8 +26,8 @@ PROGRAMMING_LANGUAGE = ActionNode( ORIGINAL_REQUIREMENTS = ActionNode( key="Original Requirements", expected_type=str, - instruction="Place the polished, complete original requirements here.", - example="The game should have a leaderboard and multiple difficulty levels.", + instruction="Place the original user's requirements here.", + example="Create a 2048 game", ) PROJECT_NAME = ActionNode( @@ -41,7 +41,7 @@ PRODUCT_GOALS = ActionNode( key="Product Goals", expected_type=list[str], instruction="Provide up to three clear, orthogonal product goals.", - example=["Create an engaging user experience", "Ensure high performance", "Provide customizable features"], + example=["Create an engaging user experience", "Improve accessibility, be responsive", "More beautiful UI"], ) USER_STORIES = ActionNode( @@ -49,8 +49,11 @@ USER_STORIES = ActionNode( expected_type=list[str], instruction="Provide up to 3 to 5 scenario-based user stories.", example=[ - "As a user, I want to be able to choose difficulty levels", + "As a player, I want to be able to choose difficulty levels", "As a player, I want to see my score after each game", + "As a player, I want to get restart button when I lose", + "As a player, I want to see beautiful UI that make me feel good", + "As a player, I want to play game via mobile phone", ], ) @@ -58,7 +61,11 @@ COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", expected_type=list[str], instruction="Provide 5 to 7 competitive products.", - example=["Python Snake Game: Simple interface, lacks advanced features"], + example=[ + "2048 Game A: Simple interface, lacks responsive features", + "play2048.co: Beautiful and responsive UI with my best score shown", + "2048game.com: Responsive UI with my best score shown, but many ads", + ], ) COMPETITIVE_QUADRANT_CHART = ActionNode( @@ -86,7 +93,7 @@ REQUIREMENT_ANALYSIS = ActionNode( key="Requirement Analysis", expected_type=str, instruction="Provide a detailed analysis of the requirements.", - example="The product should be user-friendly.", + example="", ) REQUIREMENT_POOL = ActionNode( @@ -107,7 +114,7 @@ ANYTHING_UNCLEAR = ActionNode( key="Anything UNCLEAR", expected_type=str, instruction="Mention any aspects of the project that are unclear and try to clarify them.", - example="...", + example="", ) ISSUE_TYPE = ActionNode( diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 6dba21fe1..72e5a9be5 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -27,8 +27,8 @@ class ProductManager(Role): """ name: str = "Alice" profile: str = Field(default="Product Manager") - goal: str = "efficiently create a successful product" - constraints: str = "use same language as user requirement" + goal: str = "efficiently create a successful product that meets market demands and user expectations" + constraints: str = "utilize the same language as the user requirements for seamless communication" def __init__(self, **kwargs) -> None: super().__init__(**kwargs) From 6959d40e6d265c0de99ebf057bbc4434febf2a22 Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 15:04:25 +0800 Subject: [PATCH 401/413] add write_review action and its test --- metagpt/actions/action_node.py | 4 +- metagpt/actions/write_review.py | 40 ++++++++++++++++ metagpt/utils/common.py | 25 +++++++++- tests/metagpt/actions/test_write_review.py | 53 ++++++++++++++++++++++ 4 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 metagpt/actions/write_review.py create mode 100644 tests/metagpt/actions/test_write_review.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 092dd5755..58688aefa 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -41,10 +41,10 @@ Fill in the above nodes based on the format example. """ -def dict_to_markdown(d, prefix="-", postfix="\n"): +def dict_to_markdown(d, prefix="##", kv_sep="\n", postfix="\n"): markdown_str = "" for key, value in d.items(): - markdown_str += f"{prefix} {key}: {value}{postfix}" + markdown_str += f"{prefix}{key}{kv_sep}{value}{postfix}" return markdown_str diff --git a/metagpt/actions/write_review.py b/metagpt/actions/write_review.py new file mode 100644 index 000000000..94dd9951b --- /dev/null +++ b/metagpt/actions/write_review.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Author : alexanderwu +@File : write_review.py +""" +from typing import List + +from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode + +# from metagpt.llm import LLM + +REVIEW = ActionNode( + key="Review", + expected_type=List[str], + instruction="Act as an experienced Reviewer and review the given output. Ask a series of critical questions, " + "concisely and clearly, to help the writer improve their work.", + example=[ + "This is a good PRD, but I think it can be improved by adding more details.", + ], +) + +LGTM = ActionNode( + key="LGTM", + expected_type=str, + instruction="If the output is good enough, give a LGTM (Looks Good To Me) to the writer, " + "else LBTM (Looks Bad To Me).", + example="LGTM", +) + +WRITE_REVIEW_NODE = ActionNode.from_children("WRITE_REVIEW_NODE", [REVIEW, LGTM]) + + +class WriteReview(Action): + """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and + "##RECORD" (discussion records), thereby deepening the discussion.""" + + async def run(self, context): + return await WRITE_REVIEW_NODE.fill(context=context, llm=self.llm, schema="markdown") diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index fa18694e3..a290c7db7 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -18,7 +18,7 @@ import os import platform import re import typing -from typing import List, Tuple, Union +from typing import List, Tuple, Union, get_args, get_origin import aiofiles import loguru @@ -129,8 +129,31 @@ class OutputParser: parsed_data[block] = content return parsed_data + @staticmethod + def extract_content(text, tag="CONTENT"): + # Use regular expression to extract content between [CONTENT] and [/CONTENT] + extracted_content = re.search(rf"\[{tag}\](.*?)\[/{tag}\]", text, re.DOTALL) + + if extracted_content: + return extracted_content.group(1).strip() + else: + return "No content found between [CONTENT] and [/CONTENT] tags." + + @staticmethod + def is_supported_list_type(i): + origin = get_origin(i) + if origin is not List: + return False + + args = get_args(i) + if args == (str,) or args == (Tuple[str, str],) or args == (List[str],): + return True + + return False + @classmethod def parse_data_with_mapping(cls, data, mapping): + data = cls.extract_content(text=data) block_dict = cls.parse_blocks(data) parsed_data = {} for block, content in block_dict.items(): diff --git a/tests/metagpt/actions/test_write_review.py b/tests/metagpt/actions/test_write_review.py new file mode 100644 index 000000000..2d188b720 --- /dev/null +++ b/tests/metagpt/actions/test_write_review.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/12/20 15:01 +@Author : alexanderwu +@File : test_write_review.py +""" +import pytest + +from metagpt.actions.write_review import WriteReview + +CONTEXT = """ +{ + "Language": "zh_cn", + "Programming Language": "Python", + "Original Requirements": "写一个简单的2048", + "Project Name": "game_2048", + "Product Goals": [ + "创建一个引人入胜的用户体验", + "确保高性能", + "提供可定制的功能" + ], + "User Stories": [ + "作为用户,我希望能够选择不同的难度级别", + "作为玩家,我希望在每局游戏结束后能看到我的得分" + ], + "Competitive Analysis": [ + "Python Snake Game: 界面简单,缺乏高级功能" + ], + "Competitive Quadrant Chart": "quadrantChart\n title \"Reach and engagement of campaigns\"\n x-axis \"Low Reach\" --> \"High Reach\"\n y-axis \"Low Engagement\" --> \"High Engagement\"\n quadrant-1 \"我们应该扩展\"\n quadrant-2 \"需要推广\"\n quadrant-3 \"重新评估\"\n quadrant-4 \"可能需要改进\"\n \"Campaign A\": [0.3, 0.6]\n \"Campaign B\": [0.45, 0.23]\n \"Campaign C\": [0.57, 0.69]\n \"Campaign D\": [0.78, 0.34]\n \"Campaign E\": [0.40, 0.34]\n \"Campaign F\": [0.35, 0.78]\n \"Our Target Product\": [0.5, 0.6]", + "Requirement Analysis": "产品应该用户友好。", + "Requirement Pool": [ + [ + "P0", + "主要代码..." + ], + [ + "P0", + "游戏算法..." + ] + ], + "UI Design draft": "基本功能描述,简单的风格和布局。", + "Anything UNCLEAR": "..." +} +""" + + +@pytest.mark.asyncio +async def test_write_review(): + write_review = WriteReview() + review = await write_review.run(CONTEXT) + assert review.instruct_content + assert review.get("LGTM") in ["LGTM", "LBTM"] From 8bec6e98cc8ad5ef1e4d0bb5f0407d08adb682ac Mon Sep 17 00:00:00 2001 From: geekan Date: Wed, 20 Dec 2023 15:20:39 +0800 Subject: [PATCH 402/413] use typing.List instead of list --- metagpt/actions/action_node.py | 3 +++ metagpt/actions/design_api_an.py | 4 +++- metagpt/actions/project_management_an.py | 10 ++++++---- metagpt/actions/write_prd_an.py | 9 +++++---- metagpt/actions/write_review.py | 4 +--- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 58688aefa..4376e09ed 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -4,6 +4,9 @@ @Time : 2023/12/11 18:45 @Author : alexanderwu @File : action_node.py + +NOTE: You should use typing.List instead of list to do type annotation. Because in the markdown extraction process, + we can use typing to extract the type of the node, but we cannot use built-in list to extract. """ import json from typing import Any, Dict, Generic, List, Optional, Tuple, Type, TypeVar diff --git a/metagpt/actions/design_api_an.py b/metagpt/actions/design_api_an.py index 0a303cdd5..7d6802381 100644 --- a/metagpt/actions/design_api_an.py +++ b/metagpt/actions/design_api_an.py @@ -5,6 +5,8 @@ @Author : alexanderwu @File : design_api_an.py """ +from typing import List + from metagpt.actions.action_node import ActionNode from metagpt.logs import logger from metagpt.utils.mermaid import MMC1, MMC2 @@ -22,7 +24,7 @@ PROJECT_NAME = ActionNode( FILE_LIST = ActionNode( key="File list", - expected_type=list[str], + expected_type=List[str], instruction="Only need relative paths. ALWAYS write a main.py or app.py here", example=["main.py", "game.py"], ) diff --git a/metagpt/actions/project_management_an.py b/metagpt/actions/project_management_an.py index 6208c1051..215a67202 100644 --- a/metagpt/actions/project_management_an.py +++ b/metagpt/actions/project_management_an.py @@ -5,26 +5,28 @@ @Author : alexanderwu @File : project_management_an.py """ +from typing import List + from metagpt.actions.action_node import ActionNode from metagpt.logs import logger REQUIRED_PYTHON_PACKAGES = ActionNode( key="Required Python packages", - expected_type=list[str], + expected_type=List[str], instruction="Provide required Python packages in requirements.txt format.", example=["flask==1.1.2", "bcrypt==3.2.0"], ) REQUIRED_OTHER_LANGUAGE_PACKAGES = ActionNode( key="Required Other language third-party packages", - expected_type=list[str], + expected_type=List[str], instruction="List down the required packages for languages other than Python.", example=["No third-party dependencies required"], ) LOGIC_ANALYSIS = ActionNode( key="Logic Analysis", - expected_type=list[list[str]], + expected_type=List[List[str]], instruction="Provide a list of files with the classes/methods/functions to be implemented, " "including dependency analysis and imports.", example=[ @@ -35,7 +37,7 @@ LOGIC_ANALYSIS = ActionNode( TASK_LIST = ActionNode( key="Task list", - expected_type=list[str], + expected_type=List[str], instruction="Break down the tasks into a list of filenames, prioritized by dependency order.", example=["game.py", "main.py"], ) diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 8698c739f..d58d72f64 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -5,6 +5,7 @@ @Author : alexanderwu @File : write_prd_an.py """ +from typing import List from metagpt.actions.action_node import ActionNode from metagpt.logs import logger @@ -39,14 +40,14 @@ PROJECT_NAME = ActionNode( PRODUCT_GOALS = ActionNode( key="Product Goals", - expected_type=list[str], + expected_type=List[str], instruction="Provide up to three clear, orthogonal product goals.", example=["Create an engaging user experience", "Improve accessibility, be responsive", "More beautiful UI"], ) USER_STORIES = ActionNode( key="User Stories", - expected_type=list[str], + expected_type=List[str], instruction="Provide up to 3 to 5 scenario-based user stories.", example=[ "As a player, I want to be able to choose difficulty levels", @@ -59,7 +60,7 @@ USER_STORIES = ActionNode( COMPETITIVE_ANALYSIS = ActionNode( key="Competitive Analysis", - expected_type=list[str], + expected_type=List[str], instruction="Provide 5 to 7 competitive products.", example=[ "2048 Game A: Simple interface, lacks responsive features", @@ -98,7 +99,7 @@ REQUIREMENT_ANALYSIS = ActionNode( REQUIREMENT_POOL = ActionNode( key="Requirement Pool", - expected_type=list[list[str]], + expected_type=List[List[str]], instruction="List down the top-5 requirements with their priority (P0, P1, P2).", example=[["P0", "The main code ..."], ["P0", "The game algorithm ..."]], ) diff --git a/metagpt/actions/write_review.py b/metagpt/actions/write_review.py index 94dd9951b..13690a1a5 100644 --- a/metagpt/actions/write_review.py +++ b/metagpt/actions/write_review.py @@ -9,8 +9,6 @@ from typing import List from metagpt.actions import Action from metagpt.actions.action_node import ActionNode -# from metagpt.llm import LLM - REVIEW = ActionNode( key="Review", expected_type=List[str], @@ -24,7 +22,7 @@ REVIEW = ActionNode( LGTM = ActionNode( key="LGTM", expected_type=str, - instruction="If the output is good enough, give a LGTM (Looks Good To Me) to the writer, " + instruction="LGTM/LBTM. If the output is good enough, give a LGTM (Looks Good To Me) to the writer, " "else LBTM (Looks Bad To Me).", example="LGTM", ) From 3f0f008690d1c19ab379cf2925603f55d6599c10 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 20 Dec 2023 15:59:15 +0800 Subject: [PATCH 403/413] update ActionOutput.create_model_class to ActionNode.create_model_class --- tests/metagpt/serialize_deserialize/test_action.py | 2 +- tests/metagpt/serialize_deserialize/test_environment.py | 4 ++-- tests/metagpt/serialize_deserialize/test_memory.py | 6 +++--- tests/metagpt/serialize_deserialize/test_schema.py | 6 +++--- .../metagpt/serialize_deserialize/test_serdeser_base.py | 6 +++--- .../serialize_deserialize/test_write_code_review.py | 9 ++------- tests/metagpt/test_schema.py | 4 ++-- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/tests/metagpt/serialize_deserialize/test_action.py b/tests/metagpt/serialize_deserialize/test_action.py index 63d8e7b7c..14d558c13 100644 --- a/tests/metagpt/serialize_deserialize/test_action.py +++ b/tests/metagpt/serialize_deserialize/test_action.py @@ -4,7 +4,7 @@ # @Desc : import pytest -from metagpt.actions import Action, WriteTest +from metagpt.actions import Action from metagpt.llm import LLM diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py index 3a374460c..b741b9c4b 100644 --- a/tests/metagpt/serialize_deserialize/test_environment.py +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -4,7 +4,7 @@ import shutil -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.project_management import WriteTasks from metagpt.environment import Environment @@ -32,7 +32,7 @@ def test_env_deserialize(): def test_environment_serdeser(): out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} - ic_obj = ActionOutput.create_model_class("prd", out_mapping) + ic_obj = ActionNode.create_model_class("prd", out_mapping) message = Message( content="prd", diff --git a/tests/metagpt/serialize_deserialize/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py index 47410c615..0d756518b 100644 --- a/tests/metagpt/serialize_deserialize/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -4,7 +4,7 @@ from pydantic import BaseModel -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.actions.design_api import WriteDesign from metagpt.memory.memory import Memory @@ -20,7 +20,7 @@ def test_memory_serdeser(): out_mapping = {"field2": (list[str], ...)} out_data = {"field2": ["field2 value1", "field2 value2"]} - ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + ic_obj = ActionNode.create_model_class("system_design", out_mapping) msg2 = Message(role="Architect", instruct_content=ic_obj(**out_data), content="system design content", @@ -46,7 +46,7 @@ def test_memory_serdeser_save(): out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} - ic_obj = ActionOutput.create_model_class("system_design", out_mapping) + ic_obj = ActionNode.create_model_class("system_design", out_mapping) msg2 = Message(role="Architect", instruct_content=ic_obj(**out_data), content="system design content", diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py index 02afa762d..72b7153a7 100644 --- a/tests/metagpt/serialize_deserialize/test_schema.py +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # @Desc : unittest of schema ser&deser -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.actions.write_code import WriteCode from metagpt.schema import Message from metagpt.utils.common import any_to_str @@ -12,7 +12,7 @@ from tests.metagpt.serialize_deserialize.test_serdeser_base import MockMessage def test_message_serdeser(): out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} - ic_obj = ActionOutput.create_model_class("code", out_mapping) + ic_obj = ActionNode.create_model_class("code", out_mapping) message = Message( content="code", @@ -34,7 +34,7 @@ def test_message_without_postprocess(): """ to explain `instruct_content` should be postprocessed """ out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} - ic_obj = ActionOutput.create_model_class("code", out_mapping) + ic_obj = ActionNode.create_model_class("code", out_mapping) message = MockMessage( content="code", instruct_content=ic_obj(**out_data) diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index 20f708e30..eac083cf9 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -7,8 +7,8 @@ from pathlib import Path from pydantic import BaseModel, Field -from metagpt.actions.action import Action -from metagpt.actions.action_output import ActionOutput +from metagpt.actions import Action, ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.roles.role import Role, RoleReactMode @@ -29,7 +29,7 @@ class ActionPass(Action): output_mapping = { "result": (str, ...) } - pass_class = ActionOutput.create_model_class("pass", output_mapping) + pass_class = ActionNode.create_model_class("pass", output_mapping) pass_output = ActionOutput("ActionPass run passed", pass_class(**{"result": "pass result"})) return pass_output diff --git a/tests/metagpt/serialize_deserialize/test_write_code_review.py b/tests/metagpt/serialize_deserialize/test_write_code_review.py index 6ca4c6027..a15b744db 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code_review.py +++ b/tests/metagpt/serialize_deserialize/test_write_code_review.py @@ -9,13 +9,6 @@ from metagpt.llm import LLM from metagpt.schema import CodingContext, Document -def test_write_task_serialize(): - action = WriteCodeReview() - ser_action_dict = action.dict() - assert ser_action_dict["name"] == "WriteCodeReview" - # assert "llm" in ser_action_dict # not export - - @pytest.mark.asyncio async def test_write_code_review_deserialize(): code_content = """ @@ -30,6 +23,8 @@ def div(a: int, b: int = 0): action = WriteCodeReview(context=context) serialized_data = action.dict() + assert serialized_data["name"] == "WriteCodeReview" + new_action = WriteCodeReview(**serialized_data) assert new_action.name == "WriteCodeReview" diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index c8602d953..054a92de1 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -13,7 +13,7 @@ import pytest from metagpt.actions import Action from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage -from metagpt.actions.action_output import ActionOutput +from metagpt.actions.action_node import ActionNode from metagpt.actions.write_code import WriteCode from metagpt.utils.serialize import serialize_general_message, deserialize_general_message from metagpt.utils.common import any_to_str @@ -76,7 +76,7 @@ def test_routes(): def test_message_serdeser(): out_mapping = {"field3": (str, ...), "field4": (list[str], ...)} out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} - ic_obj = ActionOutput.create_model_class("code", out_mapping) + ic_obj = ActionNode.create_model_class("code", out_mapping) message = Message( content="code", From 15279376d40ec59405295af2c80b9c7c96ddd294 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 20 Dec 2023 16:01:17 +0800 Subject: [PATCH 404/413] rebase update after #589 --- metagpt/actions/action.py | 5 ++--- metagpt/actions/debug_error.py | 10 +++++++--- metagpt/actions/fix_bug.py | 1 + metagpt/actions/run_code.py | 10 +++++++--- metagpt/actions/summarize_code.py | 8 ++++++-- metagpt/actions/write_code.py | 3 +-- metagpt/actions/write_code_review.py | 4 +--- metagpt/roles/qa_engineer.py | 25 ++++++++++++------------- metagpt/roles/role.py | 3 ++- metagpt/schema.py | 4 ++-- metagpt/utils/serialize.py | 13 +++++-------- 11 files changed, 46 insertions(+), 40 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index e18983d7d..535c25cb9 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -11,12 +11,11 @@ from __future__ import annotations from typing import Optional, Any from pydantic import BaseModel, Field -from metagpt.actions.action_node import ActionNode + from metagpt.llm import LLM from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext - action_subclass_registry = {} @@ -26,7 +25,7 @@ class Action(BaseModel): context: dict | CodingContext | CodeSummarizeContext | TestingContext | RunCodeContext | str | None = "" prefix = "" # aask*时会加上prefix,作为system_message desc = "" # for skill manager - node: ActionNode = Field(default_factory=ActionNode, exclude=True) + # node: ActionNode = Field(default_factory=ActionNode, exclude=True) # builtin variables builtin_class_name: str = "" diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 39f3bc1bc..839acdc2e 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -10,11 +10,14 @@ """ import re +from pydantic import Field + from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO +from metagpt.llm import LLM, BaseGPTAPI from metagpt.logs import logger -from metagpt.schema import RunCodeResult +from metagpt.schema import RunCodeResult, RunCodeContext from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository @@ -47,8 +50,9 @@ Now you should start rewriting the code: class DebugError(Action): - def __init__(self, name="DebugError", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "DebugError" + context: RunCodeContext = Field(default_factory=RunCodeContext) + llm: BaseGPTAPI = Field(default_factory=LLM) async def run(self, *args, **kwargs) -> str: output_doc = await FileRepository.get_file( diff --git a/metagpt/actions/fix_bug.py b/metagpt/actions/fix_bug.py index 6bd550d3d..eea40c91a 100644 --- a/metagpt/actions/fix_bug.py +++ b/metagpt/actions/fix_bug.py @@ -9,6 +9,7 @@ from metagpt.actions import Action class FixBug(Action): """Fix bug action without any implementation details""" + name: str = "FixBug" async def run(self, *args, **kwargs): raise NotImplementedError diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index 1b9fd252f..ea16c8891 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -18,10 +18,13 @@ import subprocess from typing import Tuple +from pydantic import Field + from metagpt.actions.action import Action from metagpt.config import CONFIG +from metagpt.llm import LLM, BaseGPTAPI from metagpt.logs import logger -from metagpt.schema import RunCodeResult +from metagpt.schema import RunCodeResult, RunCodeContext from metagpt.utils.exceptions import handle_exception PROMPT_TEMPLATE = """ @@ -74,8 +77,9 @@ standard errors: class RunCode(Action): - def __init__(self, name="RunCode", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "RunCode" + context: RunCodeContext = Field(default_factory=RunCodeContext) + llm: BaseGPTAPI = Field(default_factory=LLM) @classmethod @handle_exception diff --git a/metagpt/actions/summarize_code.py b/metagpt/actions/summarize_code.py index f8d8d2b47..0aec15937 100644 --- a/metagpt/actions/summarize_code.py +++ b/metagpt/actions/summarize_code.py @@ -7,12 +7,15 @@ """ from pathlib import Path +from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.const import SYSTEM_DESIGN_FILE_REPO, TASK_FILE_REPO +from metagpt.llm import LLM, BaseGPTAPI from metagpt.logs import logger +from metagpt.schema import CodeSummarizeContext from metagpt.utils.file_repository import FileRepository PROMPT_TEMPLATE = """ @@ -89,8 +92,9 @@ flowchart TB class SummarizeCode(Action): - def __init__(self, name="SummarizeCode", context=None, llm=None): - super().__init__(name, context, llm) + name: str = "SummarizeCode" + context: CodeSummarizeContext = Field(default_factory=CodeSummarizeContext) + llm: BaseGPTAPI = Field(default_factory=LLM) @retry(stop=stop_after_attempt(2), wait=wait_random_exponential(min=1, max=60)) async def summarize_code(self, prompt): diff --git a/metagpt/actions/write_code.py b/metagpt/actions/write_code.py index 046f9f456..4d0690e0f 100644 --- a/metagpt/actions/write_code.py +++ b/metagpt/actions/write_code.py @@ -16,7 +16,6 @@ """ import json -from typing import Optional from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -90,7 +89,7 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc class WriteCode(Action): name: str = "WriteCode" - context: Optional[Document] = None + context: Document = Field(default_factory=Document) llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index f4ab0adfe..580069b74 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -8,8 +8,6 @@ WriteCode object, rather than passing them in when calling the run function. """ -from typing import Optional - from pydantic import Field from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -124,7 +122,7 @@ REWRITE_CODE_TEMPLATE = """ class WriteCodeReview(Action): name: str = "WriteCodeReview" - context: Optional[CodingContext] = None + context: CodingContext = Field(default_factory=CodingContext) llm: BaseGPTAPI = Field(default_factory=LLM) @retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6)) diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index acb79ab80..893faa9dd 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -17,6 +17,11 @@ from pydantic import Field +from metagpt.actions import ( + DebugError, + RunCode, + WriteTest, +) from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( @@ -24,11 +29,6 @@ from metagpt.const import ( TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO, ) -from metagpt.actions import ( - DebugError, - RunCode, - WriteTest, -) from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Document, Message, RunCodeContext, TestingContext @@ -40,17 +40,16 @@ class QaEngineer(Role): name: str = Field(default="Edward") profile: str = Field(default="QaEngineer") goal: str = "Write comprehensive and robust tests to ensure codes will work as expected without bugs" - constraints: str = "The test code you write should conform to code standard like PEP8, be modular, easy to read and maintain" + constraints: str = "The test code you write should conform to code standard like PEP8, be modular, " \ + "easy to read and maintain" test_round_allowed: int = 5 - def __init__( - self, - **kwargs - ): + def __init__(self, **kwargs): super().__init__(**kwargs) - self._init_actions( - [WriteTest] - ) # FIXME: a bit hack here, only init one action to circumvent _think() logic, will overwrite _think() in future updates + + # FIXME: a bit hack here, only init one action to circumvent _think() logic, + # will overwrite _think() in future updates + self._init_actions([WriteTest]) self._watch([SummarizeCode, WriteTest, RunCode, DebugError]) self.test_round = 0 diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 0bc129174..4bce64245 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -27,7 +27,8 @@ from typing import Iterable, Set, Type, Any from pydantic import BaseModel, Field -from metagpt.actions.action import Action, ActionOutput, action_subclass_registry +from metagpt.actions import Action, ActionOutput +from metagpt.actions.action import action_subclass_registry from metagpt.actions.action_node import ActionNode from metagpt.actions.add_requirement import UserRequirement from metagpt.const import SERDESER_PATH diff --git a/metagpt/schema.py b/metagpt/schema.py index 327bfd2d1..e5df6fb10 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -113,8 +113,8 @@ class Message(BaseModel): ic = instruct_content mapping = actionoutput_str_to_mapping(ic["mapping"]) - actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") - ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) kwargs["instruct_content"] = ic_new diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 7bfd55008..1d90e8de8 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -6,8 +6,6 @@ import copy import pickle from metagpt.utils.common import import_class -from metagpt.actions.action_node import ActionNode -from metagpt.schema import Message def actionoutout_schema_to_mapping(schema: dict) -> dict: @@ -90,27 +88,26 @@ def serialize_message(message: "Message"): def deserialize_general_message(message_dict: dict) -> "Message": """ deserialize Message, not to load""" instruct_content = message_dict.pop("instruct_content") - cause_by = message_dict.pop("cause_by") message_cls = import_class("Message", "metagpt.schema") message = message_cls(**message_dict) if instruct_content: ic = instruct_content mapping = actionoutput_str_to_mapping(ic["mapping"]) - - actionoutput_class = import_class("ActionOutput", "metagpt.actions.action_output") - ic_obj = actionoutput_class.create_model_class(class_name=ic["class"], mapping=mapping) + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new return message -def deserialize_message(message_ser: str) -> Message: +def deserialize_message(message_ser: str) -> "Message": message = pickle.loads(message_ser) if message.instruct_content: ic = message.instruct_content - ic_obj = ActionNode.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=ic["mapping"]) ic_new = ic_obj(**ic["value"]) message.instruct_content = ic_new From 6877fa444feee9b3e00ede2d426e65c8a0b20446 Mon Sep 17 00:00:00 2001 From: better629 Date: Wed, 20 Dec 2023 18:55:29 +0800 Subject: [PATCH 405/413] deal with nested BaseModel --- metagpt/schema.py | 18 +++++++++++------- metagpt/utils/common.py | 4 +--- metagpt/utils/serialize.py | 2 +- .../metagpt/serialize_deserialize/test_team.py | 3 +++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index e5df6fb10..1bb07aa95 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -108,9 +108,9 @@ class Message(BaseModel): send_to: Set = Field(default_factory={MESSAGE_ROUTE_TO_ALL}) def __init__(self, **kwargs): - instruct_content = kwargs.get("instruct_content", None) - if instruct_content and not isinstance(instruct_content, BaseModel): - ic = instruct_content + ic = kwargs.get("instruct_content", None) + if ic and not isinstance(ic, BaseModel) and "class" in ic: + # compatible with custom-defined ActionOutput mapping = actionoutput_str_to_mapping(ic["mapping"]) actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import @@ -140,13 +140,17 @@ class Message(BaseModel): def dict(self, *args, **kwargs) -> "DictStrAny": """ overwrite the `dict` to dump dynamic pydantic model""" obj_dict = super(Message, self).dict(*args, **kwargs) - ic = self.instruct_content # deal custom-defined action + ic = self.instruct_content if ic: + # compatible with custom-defined ActionOutput schema = ic.schema() - mapping = actionoutout_schema_to_mapping(schema) - mapping = actionoutput_mapping_to_str(mapping) + # `Documents` contain definitions + if "definitions" not in schema: + # TODO refine with nested BaseModel + mapping = actionoutout_schema_to_mapping(schema) + mapping = actionoutput_mapping_to_str(mapping) - obj_dict["instruct_content"] = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} + obj_dict["instruct_content"] = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} return obj_dict def __str__(self): diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index a445c9f31..ab7a3d99e 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -450,14 +450,12 @@ def serialize_decorator(func): async def wrapper(self, *args, **kwargs): try: result = await func(self, *args, **kwargs) - self.serialize() # Team.serialize return result except KeyboardInterrupt as kbi: logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") - self.serialize() # Team.serialize except Exception as exp: logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") - self.serialize() # Team.serialize + self.serialize() # Team.serialize return wrapper diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index 1d90e8de8..a52dc8f45 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -62,7 +62,7 @@ def serialize_general_message(message: "Message") -> dict: message_cp = copy.deepcopy(message) ic = message_cp.instruct_content if ic: - # model create by pydantic create_model like `pydantic.main.prd`, can't pickle.dump directly + # model create by pydantic create_model like `pydantic.main.prd`, can't load directly schema = ic.schema() mapping = actionoutout_schema_to_mapping(schema) mapping = actionoutput_mapping_to_str(mapping) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index e87df9b52..d6a477b0e 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -10,6 +10,7 @@ import pytest from metagpt.const import SERDESER_PATH from metagpt.roles import ProjectManager, ProductManager, Architect from metagpt.team import Team +from metagpt.logs import logger from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK @@ -120,6 +121,8 @@ async def test_team_recover_multi_roles_save(): company.run_project(idea) await company.run(n_round=4) + logger.info("Team recovered") + new_company = Team.recover(stg_path) new_company.run_project(idea) From 0543c0f76b18680031a59ce5cccd5e1a1899cb58 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 21 Dec 2023 00:16:28 +0800 Subject: [PATCH 406/413] just use deserialize instead of recover --- metagpt/startup.py | 2 +- tests/metagpt/serialize_deserialize/test_team.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metagpt/startup.py b/metagpt/startup.py index 5a3e482a4..59e0cb199 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -67,7 +67,7 @@ def startup( if not stg_path.exists() or not str(stg_path).endswith("team"): raise FileNotFoundError(f"{recover_path} not exists or not endswith `team`") - company = Team.recover(stg_path=stg_path) + company = Team.deserialize(stg_path=stg_path) idea = company.idea # use original idea company.invest(investment) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index d6a477b0e..db6001325 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -89,7 +89,7 @@ async def test_team_recover_save(): company.run_project(idea) await company.run(n_round=4) - new_company = Team.recover(stg_path) + new_company = Team.deserialize(stg_path) new_role_c = new_company.env.get_role(role_c.profile) # assert new_role_c._rc.memory == role_c._rc.memory assert new_role_c._rc.env != role_c._rc.env @@ -123,7 +123,7 @@ async def test_team_recover_multi_roles_save(): logger.info("Team recovered") - new_company = Team.recover(stg_path) + new_company = Team.deserialize(stg_path) new_company.run_project(idea) assert new_company.env.get_role(role_b.profile)._rc.state == 1 From 24060ea8a65d45e32d816b4ad596e74f3f4a78fe Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 21 Dec 2023 00:18:09 +0800 Subject: [PATCH 407/413] update use Field with uniform rule: define default_factory or exclude, use Field --- metagpt/environment.py | 2 +- metagpt/memory/memory.py | 2 +- metagpt/roles/architect.py | 10 +++++----- metagpt/roles/customer_service.py | 6 +++--- metagpt/roles/engineer.py | 2 +- metagpt/roles/product_manager.py | 10 +++++----- metagpt/roles/project_manager.py | 4 ++-- metagpt/roles/qa_engineer.py | 4 ++-- metagpt/roles/role.py | 6 +++--- metagpt/roles/sales.py | 6 +++--- metagpt/schema.py | 2 +- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/metagpt/environment.py b/metagpt/environment.py index a3cbe6978..ab296557f 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -30,7 +30,7 @@ class Environment(BaseModel): roles: dict[str, Role] = Field(default_factory=dict) members: dict[Role, Set] = Field(default_factory=dict) - history: str = Field(default="") # For debug + history: str = "" # For debug class Config: arbitrary_types_allowed = True diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 66ab5d4e9..076db832a 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -19,7 +19,7 @@ from metagpt.utils.common import any_to_str, any_to_str_set, read_json_file, wri class Memory(BaseModel): """The most basic memory: super-memory""" - storage: list[Message] = Field(default=[]) + storage: list[Message] = [] index: dict[str, list[Message]] = Field(default_factory=defaultdict(list)) def __init__(self, **kwargs): diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index a36cd6e93..bd6cd110b 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -22,11 +22,11 @@ class Architect(Role): goal (str): Primary goal or responsibility of the architect. constraints (str): Constraints or guidelines for the architect. """ - name: str = Field(default="Bob") - profile: str = Field(default="Architect") - goal: str = Field(default="design a concise, usable, complete software system") - constraints: str = Field(default="make sure the architecture is simple enough and use appropriate open source " - "libraries. Use same language as user requirement") + name: str = "Bob" + profile: str = "Architect" + goal: str = "design a concise, usable, complete software system" + constraints: str = "make sure the architecture is simple enough and use appropriate open source " \ + "libraries. Use same language as user requirement" def __init__(self, **kwargs) -> None: super().__init__(**kwargs) diff --git a/metagpt/roles/customer_service.py b/metagpt/roles/customer_service.py index 62792696f..b2033ac0b 100644 --- a/metagpt/roles/customer_service.py +++ b/metagpt/roles/customer_service.py @@ -28,9 +28,9 @@ DESC = """ class CustomerService(Sales): - name: str = Field(default="Xiaomei") - profile: str = Field(default="Human customer service") - desc: str = DESC, + name: str = "Xiaomei" + profile: str = "Human customer service" + desc: str = DESC store: Optional[str] = None diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 206afb38c..337184068 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -70,7 +70,7 @@ class Engineer(Role): use_code_review (bool): Whether to use code review. """ name: str = "Alex" - profile: str = Field(default="Engineer") + profile: str = "Engineer" goal: str = "write elegant, readable, extensible, efficient code" constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " \ "Use same language as user requirement" diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 72e5a9be5..6369688a5 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -20,13 +20,13 @@ class ProductManager(Role): Represents a Product Manager role responsible for product development and management. Attributes: - name (str): Name of the project manager. - profile (str): Role profile, default is 'Project Manager'. - goal (str): Goal of the project manager. - constraints (str): Constraints or limitations for the project manager. + name (str): Name of the product manager. + profile (str): Role profile, default is 'Product Manager'. + goal (str): Goal of the product manager. + constraints (str): Constraints or limitations for the product manager. """ name: str = "Alice" - profile: str = Field(default="Product Manager") + profile: str = "Product Manager" goal: str = "efficiently create a successful product that meets market demands and user expectations" constraints: str = "utilize the same language as the user requirements for seamless communication" diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 42564cd70..bf572d1f8 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -22,8 +22,8 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ - name: str = Field(default="Eve") - profile: str = Field(default="Project Manager") + name: str = "Eve" + profile: str = "Project Manager" goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " \ "dependencies to start with the prerequisite modules" constraints: str = "use same language as user requirement" diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 893faa9dd..369e3dc63 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -37,8 +37,8 @@ from metagpt.utils.file_repository import FileRepository class QaEngineer(Role): - name: str = Field(default="Edward") - profile: str = Field(default="QaEngineer") + name: str = "Edward" + profile: str = "QaEngineer" goal: str = "Write comprehensive and robust tests to ensure codes will work as expected without bugs" constraints: str = "The test code you write should conform to code standard like PEP8, be modular, " \ "easy to read and maintain" diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index 4bce64245..f87c4e250 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -129,9 +129,9 @@ class Role(BaseModel): _llm: BaseGPTAPI = Field(default_factory=LLM) _role_id: str = "" - _states: list[str] = Field(default=[]) - _actions: list[Action] = Field(default=[]) - _rc: RoleContext = Field(default=RoleContext) + _states: list[str] = [] + _actions: list[Action] = [] + _rc: RoleContext = Field(default_factory=RoleContext) _subscription: tuple[str] = set() # builtin variables diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index 826413dc8..fd5a42915 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -16,14 +16,14 @@ from metagpt.tools import SearchEngineType class Sales(Role): - name: str = Field(default="Xiaomei") - profile: str = Field(default="Retail sales guide") + name: str = "Xiaomei" + profile: str = "Retail sales guide" desc: str = "I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " "will answer questions only based on the information in the knowledge base." "If I feel that you can't get the answer from the reference material, then I will directly reply that" " I don't know, and I won't tell you that this is from the knowledge base," "but pretend to be what I know. Note that each of my replies will be replied in the tone of a " - "professional guide", + "professional guide" store: Optional[str] = None diff --git a/metagpt/schema.py b/metagpt/schema.py index 1bb07aa95..5103a4f20 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -101,7 +101,7 @@ class Message(BaseModel): id: str # According to Section 2.2.3.1.1 of RFC 135 content: str - instruct_content: BaseModel = Field(default=None) + instruct_content: BaseModel = None role: str = "user" # system / user / assistant cause_by: str = "" sent_from: str = "" From 2178cecd25916a53c77695eb25c46d2f472ff1b1 Mon Sep 17 00:00:00 2001 From: better629 Date: Thu, 21 Dec 2023 00:34:53 +0800 Subject: [PATCH 408/413] rm useless functions in serialize.py --- metagpt/utils/serialize.py | 31 ------------------------------- tests/metagpt/test_schema.py | 27 +++++++++++++-------------- 2 files changed, 13 insertions(+), 45 deletions(-) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index a52dc8f45..3939b1306 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -57,20 +57,6 @@ def actionoutput_str_to_mapping(mapping: dict) -> dict: return new_mapping -def serialize_general_message(message: "Message") -> dict: - """ serialize Message, not to save""" - message_cp = copy.deepcopy(message) - ic = message_cp.instruct_content - if ic: - # model create by pydantic create_model like `pydantic.main.prd`, can't load directly - schema = ic.schema() - mapping = actionoutout_schema_to_mapping(schema) - mapping = actionoutput_mapping_to_str(mapping) - - message_cp.instruct_content = {"class": schema["title"], "mapping": mapping, "value": ic.dict()} - return message_cp.dict() - - def serialize_message(message: "Message"): message_cp = copy.deepcopy(message) # avoid `instruct_content` value update by reference ic = message_cp.instruct_content @@ -85,23 +71,6 @@ def serialize_message(message: "Message"): return msg_ser -def deserialize_general_message(message_dict: dict) -> "Message": - """ deserialize Message, not to load""" - instruct_content = message_dict.pop("instruct_content") - - message_cls = import_class("Message", "metagpt.schema") - message = message_cls(**message_dict) - if instruct_content: - ic = instruct_content - mapping = actionoutput_str_to_mapping(ic["mapping"]) - actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import - ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) - ic_new = ic_obj(**ic["value"]) - message.instruct_content = ic_new - - return message - - def deserialize_message(message_ser: str) -> "Message": message = pickle.loads(message_ser) if message.instruct_content: diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index 054a92de1..ef706abfa 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -15,7 +15,6 @@ from metagpt.actions import Action from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.actions.action_node import ActionNode from metagpt.actions.write_code import WriteCode -from metagpt.utils.serialize import serialize_general_message, deserialize_general_message from metagpt.utils.common import any_to_str @@ -23,10 +22,10 @@ from metagpt.utils.common import any_to_str def test_messages(): test_content = "test_message" msgs = [ - UserMessage(test_content), - SystemMessage(test_content), - AIMessage(test_content), - Message(test_content, role="QA"), + UserMessage(content=test_content), + SystemMessage(content=test_content), + AIMessage(content=test_content), + Message(content=test_content, role="QA"), ] text = str(msgs) roles = ["user", "system", "assistant", "QA"] @@ -35,7 +34,7 @@ def test_messages(): @pytest.mark.asyncio def test_message(): - m = Message("a", role="v1") + m = Message(content="a", role="v1") v = m.dump() d = json.loads(v) assert d @@ -48,7 +47,7 @@ def test_message(): assert m.content == "a" assert m.role == "v2" - m = Message("a", role="b", cause_by="c", x="d", send_to="c") + m = Message(content="a", role="b", cause_by="c", x="d", send_to="c") assert m.content == "a" assert m.role == "b" assert m.send_to == {"c"} @@ -66,7 +65,7 @@ def test_message(): @pytest.mark.asyncio def test_routes(): - m = Message("a", role="b", cause_by="c", x="d", send_to="c") + m = Message(content="a", role="b", cause_by="c", x="d", send_to="c") m.send_to = "b" assert m.send_to == {"b"} m.send_to = {"e", Action} @@ -84,8 +83,8 @@ def test_message_serdeser(): role="engineer", cause_by=WriteCode ) - message_dict = serialize_general_message(message) - assert message_dict["cause_by"] == {"action_class": "WriteCode", "module_name": "metagpt.actions.write_code"} + message_dict = message.dict() + assert message_dict["cause_by"] == "metagpt.actions.write_code.WriteCode" assert message_dict["instruct_content"] == { "class": "code", "mapping": { @@ -98,14 +97,14 @@ def test_message_serdeser(): } } - new_message = deserialize_general_message(message_dict) + new_message = Message(**message_dict) assert new_message.content == message.content assert new_message.instruct_content == message.instruct_content assert new_message.cause_by == message.cause_by assert new_message.instruct_content.field3 == out_data["field3"] message = Message(content="code") - message_dict = serialize_general_message(message) - new_message = deserialize_general_message(message_dict) + message_dict = message.dict() + new_message = Message(**message_dict) assert new_message.instruct_content is None - assert new_message.cause_by == "" + assert new_message.cause_by == "metagpt.actions.add_requirement.UserRequirement" From fa1af925376b12a11f8c5e585bdb0a101f027792 Mon Sep 17 00:00:00 2001 From: voidking Date: Tue, 19 Dec 2023 20:34:53 +0800 Subject: [PATCH 409/413] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81pre-commit?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pre-commit.yaml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/pre-commit.yaml diff --git a/.github/workflows/pre-commit.yaml b/.github/workflows/pre-commit.yaml new file mode 100644 index 000000000..ed4bbb144 --- /dev/null +++ b/.github/workflows/pre-commit.yaml @@ -0,0 +1,30 @@ +name: Pre-commit checks + +on: + pull_request: + branches: + - '**' + push: + branches: + - '**' + +jobs: + pre-commit-check: + runs-on: ubuntu-latest + steps: + - name: Checkout Source Code + uses: actions/checkout@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.9.17' + + - name: Install pre-commit + run: pip install pre-commit + + - name: Initialize pre-commit + run: pre-commit install + + - name: Run pre-commit hooks + run: pre-commit run --all-files \ No newline at end of file From bf9fa4476549d2b57fbe62f5a6df9d2825d46a21 Mon Sep 17 00:00:00 2001 From: voidking Date: Wed, 20 Dec 2023 14:07:52 +0800 Subject: [PATCH 410/413] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20metagpt/team.py=20?= =?UTF-8?q?=E7=AC=A6=E5=90=88=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/team.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/metagpt/team.py b/metagpt/team.py index 1df3c4052..0c1efb812 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -10,6 +10,7 @@ from pathlib import Path import warnings + from pydantic import BaseModel, Field from metagpt.actions import UserRequirement @@ -94,9 +95,12 @@ class Team(BaseModel): Deprecated: This method will be removed in the future. Please use the `run_project` method instead. """ - warnings.warn("The 'start_project' method is deprecated and will be removed in the future. " - "Please use the 'run_project' method instead.", - DeprecationWarning, stacklevel=2) + warnings.warn( + "The 'start_project' method is deprecated and will be removed in the future. " + "Please use the 'run_project' method instead.", + DeprecationWarning, + stacklevel=2, + ) return self.run_project(idea=idea, send_to=send_to) def _save(self): From 4929e41f18cb047bf583fd43d25a16bacb886d93 Mon Sep 17 00:00:00 2001 From: voidking Date: Thu, 21 Dec 2023 10:48:46 +0800 Subject: [PATCH 411/413] run pre-commit to find potential issues and fix them --- metagpt/actions/action.py | 9 +++- metagpt/actions/debug_error.py | 2 +- metagpt/actions/design_api.py | 8 ++-- metagpt/actions/fix_bug.py | 1 + metagpt/actions/prepare_documents.py | 1 + metagpt/actions/run_code.py | 2 +- metagpt/actions/search_and_summarize.py | 18 ++++---- metagpt/actions/write_code_review.py | 12 ++--- metagpt/actions/write_prd.py | 5 +- metagpt/actions/write_test.py | 5 +- metagpt/environment.py | 20 ++++---- metagpt/memory/longterm_memory.py | 3 +- metagpt/memory/memory.py | 15 ++++-- .../postprecess/base_postprecess_plugin.py | 1 - metagpt/roles/architect.py | 8 ++-- metagpt/roles/customer_service.py | 6 +-- metagpt/roles/engineer.py | 11 +++-- metagpt/roles/product_manager.py | 2 +- metagpt/roles/project_manager.py | 8 ++-- metagpt/roles/qa_engineer.py | 15 +++--- metagpt/roles/role.py | 46 +++++++++++-------- metagpt/roles/sales.py | 2 - metagpt/roles/searcher.py | 2 +- metagpt/schema.py | 20 ++++---- metagpt/startup.py | 4 +- metagpt/team.py | 20 +++++--- metagpt/utils/common.py | 25 +++++----- .../serialize_deserialize/test_environment.py | 11 +++-- .../serialize_deserialize/test_memory.py | 22 ++++----- .../serialize_deserialize/test_role.py | 19 ++++---- .../serialize_deserialize/test_schema.py | 14 ++---- .../test_serdeser_base.py | 7 ++- .../serialize_deserialize/test_team.py | 18 +++++--- .../serialize_deserialize/test_write_code.py | 5 +- .../test_write_code_review.py | 2 +- tests/metagpt/test_environment.py | 33 +++++-------- tests/metagpt/test_schema.py | 20 ++------ tests/metagpt/test_team.py | 2 +- 38 files changed, 209 insertions(+), 215 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 535c25cb9..62434e7f8 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -8,13 +8,18 @@ from __future__ import annotations -from typing import Optional, Any +from typing import Any, Optional from pydantic import BaseModel, Field from metagpt.llm import LLM from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.schema import CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext +from metagpt.schema import ( + CodeSummarizeContext, + CodingContext, + RunCodeContext, + TestingContext, +) action_subclass_registry = {} diff --git a/metagpt/actions/debug_error.py b/metagpt/actions/debug_error.py index 839acdc2e..9dc6862f9 100644 --- a/metagpt/actions/debug_error.py +++ b/metagpt/actions/debug_error.py @@ -17,7 +17,7 @@ from metagpt.config import CONFIG from metagpt.const import TEST_CODES_FILE_REPO, TEST_OUTPUTS_FILE_REPO from metagpt.llm import LLM, BaseGPTAPI from metagpt.logs import logger -from metagpt.schema import RunCodeResult, RunCodeContext +from metagpt.schema import RunCodeContext, RunCodeResult from metagpt.utils.common import CodeParser from metagpt.utils.file_repository import FileRepository diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index f5e122356..055365421 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -45,9 +45,11 @@ class WriteDesign(Action): name: str = "" context: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) - desc: str = "Based on the PRD, think about the system design, and design the corresponding APIs, " \ - "data structures, library tables, processes, and paths. Please provide your design, feedback " \ - "clearly and in detail." + desc: str = ( + "Based on the PRD, think about the system design, and design the corresponding APIs, " + "data structures, library tables, processes, and paths. Please provide your design, feedback " + "clearly and in detail." + ) async def run(self, with_messages: Message, schema: str = CONFIG.prompt_schema): # Use `git diff` to identify which PRD documents have been modified in the `docs/prds` directory. diff --git a/metagpt/actions/fix_bug.py b/metagpt/actions/fix_bug.py index eea40c91a..56b488218 100644 --- a/metagpt/actions/fix_bug.py +++ b/metagpt/actions/fix_bug.py @@ -9,6 +9,7 @@ from metagpt.actions import Action class FixBug(Action): """Fix bug action without any implementation details""" + name: str = "FixBug" async def run(self, *args, **kwargs): diff --git a/metagpt/actions/prepare_documents.py b/metagpt/actions/prepare_documents.py index 9b5128cbd..696dc9a89 100644 --- a/metagpt/actions/prepare_documents.py +++ b/metagpt/actions/prepare_documents.py @@ -25,6 +25,7 @@ from metagpt.utils.git_repository import GitRepository class PrepareDocuments(Action): """PrepareDocuments Action: initialize project folder and add new requirements to docs/requirements.txt.""" + name: str = "PrepareDocuments" context: Optional[str] = None llm: BaseGPTAPI = Field(default_factory=LLM) diff --git a/metagpt/actions/run_code.py b/metagpt/actions/run_code.py index ea16c8891..bca9b337d 100644 --- a/metagpt/actions/run_code.py +++ b/metagpt/actions/run_code.py @@ -24,7 +24,7 @@ from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.llm import LLM, BaseGPTAPI from metagpt.logs import logger -from metagpt.schema import RunCodeResult, RunCodeContext +from metagpt.schema import RunCodeContext, RunCodeResult from metagpt.utils.exceptions import handle_exception PROMPT_TEMPLATE = """ diff --git a/metagpt/actions/search_and_summarize.py b/metagpt/actions/search_and_summarize.py index 3f110c370..6ab7becb6 100644 --- a/metagpt/actions/search_and_summarize.py +++ b/metagpt/actions/search_and_summarize.py @@ -5,18 +5,18 @@ @Author : alexanderwu @File : search_google.py """ +from typing import Optional + import pydantic -from typing import Optional, Any -from pydantic import BaseModel, Field +from pydantic import Field, root_validator from metagpt.actions import Action +from metagpt.config import CONFIG, Config from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI -from metagpt.config import Config, CONFIG from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Message from metagpt.tools.search_engine import SearchEngine -from pydantic import root_validator SEARCH_AND_SUMMARIZE_SYSTEM = """### Requirements 1. Please summarize the latest dialogue based on the reference information (secondary) and dialogue history (primary). Do not include text that is irrelevant to the conversation. @@ -120,7 +120,7 @@ class SearchAndSummarize(Action): engine = values.get("engine") search_func = values.get("search_func") config = Config() - + if engine is None: engine = config.search_engine try: @@ -135,7 +135,7 @@ class SearchAndSummarize(Action): if self.search_engine is None: logger.warning("Configure one of SERPAPI_API_KEY, SERPER_API_KEY, GOOGLE_API_KEY to unlock full feature") return "" - + query = context[-1].content # logger.debug(query) rsp = await self.search_engine.run(query) @@ -144,9 +144,9 @@ class SearchAndSummarize(Action): logger.error("empty rsp...") return "" # logger.info(rsp) - + system_prompt = [system_text] - + prompt = SEARCH_AND_SUMMARIZE_PROMPT.format( ROLE=self.prefix, CONTEXT=rsp, diff --git a/metagpt/actions/write_code_review.py b/metagpt/actions/write_code_review.py index 580069b74..1eba672a5 100644 --- a/metagpt/actions/write_code_review.py +++ b/metagpt/actions/write_code_review.py @@ -142,15 +142,9 @@ class WriteCodeReview(Action): iterative_code = self.context.code_doc.content k = CONFIG.code_review_k_times or 1 for i in range(k): - format_example = FORMAT_EXAMPLE.format( - filename=self.context.code_doc.filename - ) - task_content = ( - self.context.task_doc.content if self.context.task_doc else "" - ) - code_context = await WriteCode.get_codes( - self.context.task_doc, exclude=self.context.filename - ) + format_example = FORMAT_EXAMPLE.format(filename=self.context.code_doc.filename) + task_content = self.context.task_doc.content if self.context.task_doc else "" + code_context = await WriteCode.get_codes(self.context.task_doc, exclude=self.context.filename) context = "\n".join( [ "## System Design\n" + str(self.context.design_doc) + "\n", diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index df66e6442..1223e5486 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -143,8 +143,9 @@ class WritePRD(Action): async def _update_prd(self, requirement_doc, prd_doc, prds_file_repo, *args, **kwargs) -> Document | None: if not prd_doc: - prd = await self._run_new_requirement(requirements=[requirement_doc.content if requirement_doc else ""], - *args, **kwargs) + prd = await self._run_new_requirement( + requirements=[requirement_doc.content if requirement_doc else ""], *args, **kwargs + ) new_prd_doc = Document( root_path=PRDS_FILE_REPO, filename=FileRepository.new_filename() + ".json", diff --git a/metagpt/actions/write_test.py b/metagpt/actions/write_test.py index fa3931ba6..9eb0bdbb6 100644 --- a/metagpt/actions/write_test.py +++ b/metagpt/actions/write_test.py @@ -9,14 +9,15 @@ """ from typing import Optional + from pydantic import Field -from metagpt.llm import LLM -from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.actions.action import Action from metagpt.config import CONFIG from metagpt.const import TEST_CODES_FILE_REPO +from metagpt.llm import LLM from metagpt.logs import logger +from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Document, TestingContext from metagpt.utils.common import CodeParser diff --git a/metagpt/environment.py b/metagpt/environment.py index ab296557f..58569ec08 100644 --- a/metagpt/environment.py +++ b/metagpt/environment.py @@ -56,12 +56,14 @@ class Environment(BaseModel): roles_path = stg_path.joinpath("roles.json") roles_info = [] for role_key, role in self.roles.items(): - roles_info.append({ - "role_class": role.__class__.__name__, - "module_name": role.__module__, - "role_name": role.name, - "role_sub_tags": list(self.members.get(role)) - }) + roles_info.append( + { + "role_class": role.__class__.__name__, + "module_name": role.__module__, + "role_name": role.name, + "role_sub_tags": list(self.members.get(role)), + } + ) role.serialize(stg_path=stg_path.joinpath(f"roles/{role.__class__.__name__}_{role.name}")) write_json_file(roles_path, roles_info) @@ -70,7 +72,7 @@ class Environment(BaseModel): @classmethod def deserialize(cls, stg_path: Path) -> "Environment": - """ stg_path: ./storage/team/environment/ """ + """stg_path: ./storage/team/environment/""" roles_path = stg_path.joinpath("roles.json") roles_info = read_json_file(roles_path) roles = [] @@ -83,9 +85,7 @@ class Environment(BaseModel): history = read_json_file(stg_path.joinpath("history.json")) history = history.get("content") - environment = Environment(**{ - "history": history - }) + environment = Environment(**{"history": history}) environment.add_roles(roles) return environment diff --git a/metagpt/memory/longterm_memory.py b/metagpt/memory/longterm_memory.py index 76a8deabb..710074f81 100644 --- a/metagpt/memory/longterm_memory.py +++ b/metagpt/memory/longterm_memory.py @@ -5,9 +5,7 @@ """ from typing import Optional -from pydantic import Field -from typing import Optional from pydantic import Field from metagpt.logs import logger @@ -22,6 +20,7 @@ class LongTermMemory(Memory): - recover memory when it staruped - update memory when it changed """ + memory_storage: MemoryStorage = Field(default_factory=MemoryStorage) rc: Optional["RoleContext"] = None msg_from_recover: bool = False diff --git a/metagpt/memory/memory.py b/metagpt/memory/memory.py index 076db832a..e9891ed00 100644 --- a/metagpt/memory/memory.py +++ b/metagpt/memory/memory.py @@ -6,7 +6,6 @@ @File : memory.py @Modified By: mashenquan, 2023-11-1. According to RFC 116: Updated the type of index key. """ -import copy from collections import defaultdict from pathlib import Path from typing import Iterable, Set @@ -14,11 +13,17 @@ from typing import Iterable, Set from pydantic import BaseModel, Field from metagpt.schema import Message -from metagpt.utils.common import any_to_str, any_to_str_set, read_json_file, write_json_file +from metagpt.utils.common import ( + any_to_str, + any_to_str_set, + read_json_file, + write_json_file, +) class Memory(BaseModel): """The most basic memory: super-memory""" + storage: list[Message] = [] index: dict[str, list[Message]] = Field(default_factory=defaultdict(list)) @@ -32,14 +37,14 @@ class Memory(BaseModel): self.index = new_index def serialize(self, stg_path: Path): - """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/ """ + """stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" memory_path = stg_path.joinpath("memory.json") storage = self.dict() write_json_file(memory_path, storage) @classmethod def deserialize(cls, stg_path: Path) -> "Memory": - """ stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" + """stg_path = ./storage/team/environment/ or ./storage/team/environment/roles/{role_class}_{role_name}/""" memory_path = stg_path.joinpath("memory.json") memory_dict = read_json_file(memory_path) @@ -68,7 +73,7 @@ class Memory(BaseModel): return [message for message in self.storage if content in message.content] def delete_newest(self) -> "Message": - """ delete the newest message from the storage""" + """delete the newest message from the storage""" if len(self.storage) > 0: newest_msg = self.storage.pop() if newest_msg.cause_by and newest_msg in self.index[newest_msg.cause_by]: diff --git a/metagpt/provider/postprecess/base_postprecess_plugin.py b/metagpt/provider/postprecess/base_postprecess_plugin.py index afcef2531..46646be91 100644 --- a/metagpt/provider/postprecess/base_postprecess_plugin.py +++ b/metagpt/provider/postprecess/base_postprecess_plugin.py @@ -4,7 +4,6 @@ from typing import Union -from metagpt.logs import logger from metagpt.utils.repair_llm_raw_output import ( RepairType, extract_content_from_output, diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index bd6cd110b..c6ceaccb7 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -5,7 +5,6 @@ @Author : alexanderwu @File : architect.py """ -from pydantic import Field from metagpt.actions import WritePRD from metagpt.actions.design_api import WriteDesign @@ -22,11 +21,14 @@ class Architect(Role): goal (str): Primary goal or responsibility of the architect. constraints (str): Constraints or guidelines for the architect. """ + name: str = "Bob" profile: str = "Architect" goal: str = "design a concise, usable, complete software system" - constraints: str = "make sure the architecture is simple enough and use appropriate open source " \ - "libraries. Use same language as user requirement" + constraints: str = ( + "make sure the architecture is simple enough and use appropriate open source " + "libraries. Use same language as user requirement" + ) def __init__(self, **kwargs) -> None: super().__init__(**kwargs) diff --git a/metagpt/roles/customer_service.py b/metagpt/roles/customer_service.py index b2033ac0b..777f62731 100644 --- a/metagpt/roles/customer_service.py +++ b/metagpt/roles/customer_service.py @@ -6,7 +6,6 @@ @File : sales.py """ from typing import Optional -from pydantic import Field from metagpt.roles import Sales @@ -27,14 +26,11 @@ DESC = """ class CustomerService(Sales): - name: str = "Xiaomei" profile: str = "Human customer service" desc: str = DESC store: Optional[str] = None - def __init__( - self, - **kwargs): + def __init__(self, **kwargs): super().__init__(**kwargs) diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 337184068..e0234f378 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -24,8 +24,6 @@ from collections import defaultdict from pathlib import Path from typing import Set -from pydantic import Field - from metagpt.actions import Action, WriteCode, WriteCodeReview, WriteTasks from metagpt.actions.fix_bug import FixBug from metagpt.actions.summarize_code import SummarizeCode @@ -69,11 +67,14 @@ class Engineer(Role): n_borg (int): Number of borgs. use_code_review (bool): Whether to use code review. """ + name: str = "Alex" profile: str = "Engineer" goal: str = "write elegant, readable, extensible, efficient code" - constraints: str = "the code should conform to standards like google-style and be modular and maintainable. " \ - "Use same language as user requirement" + constraints: str = ( + "the code should conform to standards like google-style and be modular and maintainable. " + "Use same language as user requirement" + ) n_borg: int = 1 use_code_review: bool = False code_todos: list = [] @@ -212,7 +213,7 @@ class Engineer(Role): @staticmethod async def _new_coding_context( - filename, src_file_repo, task_file_repo, design_file_repo, dependency + filename, src_file_repo, task_file_repo, design_file_repo, dependency ) -> CodingContext: old_code_doc = await src_file_repo.get(filename) if not old_code_doc: diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index 6369688a5..c794ad2eb 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -7,7 +7,6 @@ @Modified By: mashenquan, 2023/11/27. Add `PrepareDocuments` action according to Section 2.2.3.5.1 of RFC 135. """ -from pydantic import Field from metagpt.actions import UserRequirement, WritePRD from metagpt.actions.prepare_documents import PrepareDocuments @@ -25,6 +24,7 @@ class ProductManager(Role): goal (str): Goal of the product manager. constraints (str): Constraints or limitations for the product manager. """ + name: str = "Alice" profile: str = "Product Manager" goal: str = "efficiently create a successful product that meets market demands and user expectations" diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index bf572d1f8..1fad4afc2 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -5,7 +5,6 @@ @Author : alexanderwu @File : project_manager.py """ -from pydantic import Field from metagpt.actions import WriteTasks from metagpt.actions.design_api import WriteDesign @@ -22,10 +21,13 @@ class ProjectManager(Role): goal (str): Goal of the project manager. constraints (str): Constraints or limitations for the project manager. """ + name: str = "Eve" profile: str = "Project Manager" - goal: str = "break down tasks according to PRD/technical design, generate a task list, and analyze task " \ - "dependencies to start with the prerequisite modules" + goal: str = ( + "break down tasks according to PRD/technical design, generate a task list, and analyze task " + "dependencies to start with the prerequisite modules" + ) constraints: str = "use same language as user requirement" def __init__(self, **kwargs) -> None: diff --git a/metagpt/roles/qa_engineer.py b/metagpt/roles/qa_engineer.py index 369e3dc63..5e509300b 100644 --- a/metagpt/roles/qa_engineer.py +++ b/metagpt/roles/qa_engineer.py @@ -15,13 +15,8 @@ of SummarizeCode. """ -from pydantic import Field -from metagpt.actions import ( - DebugError, - RunCode, - WriteTest, -) +from metagpt.actions import DebugError, RunCode, WriteTest from metagpt.actions.summarize_code import SummarizeCode from metagpt.config import CONFIG from metagpt.const import ( @@ -40,8 +35,9 @@ class QaEngineer(Role): name: str = "Edward" profile: str = "QaEngineer" goal: str = "Write comprehensive and robust tests to ensure codes will work as expected without bugs" - constraints: str = "The test code you write should conform to code standard like PEP8, be modular, " \ - "easy to read and maintain" + constraints: str = ( + "The test code you write should conform to code standard like PEP8, be modular, " "easy to read and maintain" + ) test_round_allowed: int = 5 def __init__(self, **kwargs): @@ -118,7 +114,8 @@ class QaEngineer(Role): ) run_code_context.code = None run_code_context.test_code = None - recipient = parse_recipient(result.summary) # the recipient might be Engineer or myself + # the recipient might be Engineer or myself + recipient = parse_recipient(result.summary) mappings = {"Engineer": "Alex", "QaEngineer": "Edward"} self.publish_message( Message( diff --git a/metagpt/roles/role.py b/metagpt/roles/role.py index f87c4e250..8c5743467 100644 --- a/metagpt/roles/role.py +++ b/metagpt/roles/role.py @@ -23,7 +23,7 @@ from __future__ import annotations from enum import Enum from pathlib import Path -from typing import Iterable, Set, Type, Any +from typing import Any, Iterable, Set, Type from pydantic import BaseModel, Field @@ -37,7 +37,13 @@ from metagpt.logs import logger from metagpt.memory import Memory from metagpt.provider.base_gpt_api import BaseGPTAPI from metagpt.schema import Message, MessageQueue -from metagpt.utils.common import any_to_str, read_json_file, write_json_file, import_class, role_raise_decorator +from metagpt.utils.common import ( + any_to_str, + import_class, + read_json_file, + role_raise_decorator, + write_json_file, +) from metagpt.utils.repair_llm_raw_output import extract_state_value_from_output PREFIX_TEMPLATE = """You are a {profile}, named {name}, your goal is {goal}, and the constraint is {constraints}. """ @@ -82,18 +88,22 @@ class RoleReactMode(str, Enum): class RoleContext(BaseModel): """Role Runtime Context""" + # # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison` env: "Environment" = Field(default=None, exclude=True) # TODO judge if ser&deser - msg_buffer: MessageQueue = Field(default_factory=MessageQueue, - exclude=True) # Message Buffer with Asynchronous Updates + msg_buffer: MessageQueue = Field( + default_factory=MessageQueue, exclude=True + ) # Message Buffer with Asynchronous Updates memory: Memory = Field(default_factory=Memory) # long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory) state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None todo: Action = Field(default=None, exclude=True) watch: set[str] = Field(default_factory=set) news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used - react_mode: RoleReactMode = RoleReactMode.REACT # see `Role._set_react_mode` for definitions of the following two attributes + react_mode: RoleReactMode = ( + RoleReactMode.REACT + ) # see `Role._set_react_mode` for definitions of the following two attributes max_react_loop: int = 1 class Config: @@ -120,6 +130,7 @@ role_subclass_registry = {} class Role(BaseModel): """Role/Agent""" + name: str = "" profile: str = "" goal: str = "" @@ -145,7 +156,7 @@ class Role(BaseModel): "_states": [], "_actions": [], "_rc": RoleContext(), - "_subscription": set() + "_subscription": set(), } __hash__ = object.__hash__ # support Role as hashable type in `Environment.members` @@ -206,14 +217,14 @@ class Role(BaseModel): return f"{self.name}({self.profile})" def serialize(self, stg_path: Path = None): - stg_path = SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") \ - if stg_path is None else stg_path + stg_path = ( + SERDESER_PATH.joinpath(f"team/environment/roles/{self.__class__.__name__}_{self.name}") + if stg_path is None + else stg_path + ) role_info = self.dict(exclude={"_rc": {"memory": True, "msg_buffer": True}, "_llm": True}) - role_info.update({ - "role_class": self.__class__.__name__, - "module_name": self.__module__ - }) + role_info.update({"role_class": self.__class__.__name__, "module_name": self.__module__}) role_info_path = stg_path.joinpath("role_info.json") write_json_file(role_info_path, role_info) @@ -221,7 +232,7 @@ class Role(BaseModel): @classmethod def deserialize(cls, stg_path: Path) -> "Role": - """ stg_path = ./storage/team/environment/roles/{role_class}_{role_name}""" + """stg_path = ./storage/team/environment/roles/{role_class}_{role_name}""" role_info_path = stg_path.joinpath("role_info.json") role_info = read_json_file(role_info_path) @@ -328,12 +339,9 @@ class Role(BaseModel): """Get the role prefix""" if self.desc: return self.desc - return PREFIX_TEMPLATE.format(**{ - "profile": self.profile, - "name": self.name, - "goal": self.goal, - "constraints": self.constraints - }) + return PREFIX_TEMPLATE.format( + **{"profile": self.profile, "name": self.name, "goal": self.goal, "constraints": self.constraints} + ) async def _think(self) -> None: """Think about what to do and decide on the next action""" diff --git a/metagpt/roles/sales.py b/metagpt/roles/sales.py index fd5a42915..ba0a6fc6b 100644 --- a/metagpt/roles/sales.py +++ b/metagpt/roles/sales.py @@ -7,7 +7,6 @@ """ from typing import Optional -from pydantic import Field from metagpt.actions import SearchAndSummarize from metagpt.roles import Role @@ -15,7 +14,6 @@ from metagpt.tools import SearchEngineType class Sales(Role): - name: str = "Xiaomei" profile: str = "Retail sales guide" desc: str = "I am a sales guide in retail. My name is Xiaomei. I will answer some customer questions next, and I " diff --git a/metagpt/roles/searcher.py b/metagpt/roles/searcher.py index a5c399f47..a2136064f 100644 --- a/metagpt/roles/searcher.py +++ b/metagpt/roles/searcher.py @@ -35,7 +35,7 @@ class Searcher(Role): goal: str = "Provide search services for users" constraints: str = "Answer is rich and complete" engine: SearchEngineType = SearchEngineType.SERPAPI_GOOGLE - + def __init__(self, **kwargs) -> None: """ Initializes the Searcher role with given attributes. diff --git a/metagpt/schema.py b/metagpt/schema.py index 5103a4f20..4a9df7fe2 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -23,7 +23,7 @@ from abc import ABC from asyncio import Queue, QueueEmpty, wait_for from json import JSONDecodeError from pathlib import Path -from typing import Dict, List, Optional, Set, Type, TypedDict, TypeVar, Any +from typing import Any, Dict, List, Optional, Set, Type, TypedDict, TypeVar from pydantic import BaseModel, Field @@ -38,9 +38,12 @@ from metagpt.const import ( ) from metagpt.logs import logger from metagpt.utils.common import any_to_str, any_to_str_set, import_class -from metagpt.utils.serialize import actionoutout_schema_to_mapping, actionoutput_mapping_to_str, \ - actionoutput_str_to_mapping from metagpt.utils.exceptions import handle_exception +from metagpt.utils.serialize import ( + actionoutout_schema_to_mapping, + actionoutput_mapping_to_str, + actionoutput_str_to_mapping, +) class RawMessage(TypedDict): @@ -119,8 +122,9 @@ class Message(BaseModel): kwargs["instruct_content"] = ic_new kwargs["id"] = kwargs.get("id", uuid.uuid4().hex) - kwargs["cause_by"] = any_to_str(kwargs.get("cause_by", - import_class("UserRequirement", "metagpt.actions.add_requirement"))) + kwargs["cause_by"] = any_to_str( + kwargs.get("cause_by", import_class("UserRequirement", "metagpt.actions.add_requirement")) + ) kwargs["sent_from"] = any_to_str(kwargs.get("sent_from", "")) kwargs["send_to"] = any_to_str_set(kwargs.get("send_to", {MESSAGE_ROUTE_TO_ALL})) super(Message, self).__init__(**kwargs) @@ -138,7 +142,7 @@ class Message(BaseModel): super().__setattr__(key, new_val) def dict(self, *args, **kwargs) -> "DictStrAny": - """ overwrite the `dict` to dump dynamic pydantic model""" + """overwrite the `dict` to dump dynamic pydantic model""" obj_dict = super(Message, self).dict(*args, **kwargs) ic = self.instruct_content if ic: @@ -208,9 +212,7 @@ class MessageQueue(BaseModel): _queue: Queue = Field(default_factory=Queue) - _private_attributes = { - "_queue": Queue() - } + _private_attributes = {"_queue": Queue()} class Config: arbitrary_types_allowed = True diff --git a/metagpt/startup.py b/metagpt/startup.py index 59e0cb199..767a19a9d 100644 --- a/metagpt/startup.py +++ b/metagpt/startup.py @@ -1,9 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio +from pathlib import Path import typer -from pathlib import Path from metagpt.config import CONFIG @@ -32,7 +32,7 @@ def startup( help="The maximum number of times the 'SummarizeCode' action is automatically invoked, with -1 indicating " "unlimited. This parameter is used for debugging the workflow.", ), - recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage") + recover_path: str = typer.Option(default=None, help="recover the project from existing serialized storage"), ): """Run a startup. Be a boss.""" from metagpt.roles import ( diff --git a/metagpt/team.py b/metagpt/team.py index 0c1efb812..8b92ed47a 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -8,20 +8,24 @@ Section 2.2.3.3 of RFC 135. """ -from pathlib import Path import warnings +from pathlib import Path from pydantic import BaseModel, Field from metagpt.actions import UserRequirement from metagpt.config import CONFIG -from metagpt.const import MESSAGE_ROUTE_TO_ALL -from metagpt.const import SERDESER_PATH +from metagpt.const import MESSAGE_ROUTE_TO_ALL, SERDESER_PATH from metagpt.environment import Environment from metagpt.logs import logger from metagpt.roles import Role from metagpt.schema import Message -from metagpt.utils.common import NoMoneyException, read_json_file, write_json_file, serialize_decorator +from metagpt.utils.common import ( + NoMoneyException, + read_json_file, + serialize_decorator, + write_json_file, +) class Team(BaseModel): @@ -51,12 +55,14 @@ class Team(BaseModel): @classmethod def deserialize(cls, stg_path: Path) -> "Team": - """ stg_path = ./storage/team """ + """stg_path = ./storage/team""" # recover team_info team_info_path = stg_path.joinpath("team_info.json") if not team_info_path.exists(): - raise FileNotFoundError("recover storage meta file `team_info.json` not exist, " - "not to recover and please start a new project.") + raise FileNotFoundError( + "recover storage meta file `team_info.json` not exist, " + "not to recover and please start a new project." + ) team_info: dict = read_json_file(team_info_path) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index e123e8fd9..ea3316d66 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -22,8 +22,7 @@ import re import traceback import typing from pathlib import Path -from typing import Any -from typing import List, Tuple, Union, get_args, get_origin +from typing import Any, List, Tuple, Union, get_args, get_origin import aiofiles import loguru @@ -219,7 +218,7 @@ class OutputParser: if start_index != -1 and end_index != -1: # Extract the structure part - structure_text = text[start_index: end_index + 1] + structure_text = text[start_index : end_index + 1] try: # Attempt to convert the text to a Python data type using ast.literal_eval @@ -439,7 +438,7 @@ def read_json_file(json_file: str, encoding=None) -> list[Any]: with open(json_file, "r", encoding=encoding) as fin: try: data = json.load(fin) - except Exception as exp: + except Exception: raise ValueError(f"read json file: {json_file} failed") return data @@ -474,9 +473,9 @@ def serialize_decorator(func): try: result = await func(self, *args, **kwargs) return result - except KeyboardInterrupt as kbi: + except KeyboardInterrupt: logger.error(f"KeyboardInterrupt occurs, start to serialize the project, exp:\n{format_trackback_info()}") - except Exception as exp: + except Exception: logger.error(f"Exception occurs, start to serialize the project, exp:\n{format_trackback_info()}") self.serialize() # Team.serialize @@ -491,14 +490,18 @@ def role_raise_decorator(func): logger.error(f"KeyboardInterrupt: {kbi} occurs, start to serialize the project") if self.latest_observed_msg: self._rc.memory.delete(self.latest_observed_msg) - raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside - except Exception as exp: + # raise again to make it captured outside + raise Exception(format_trackback_info(limit=None)) + except Exception: if self.latest_observed_msg: - logger.warning("There is a exception in role's execution, in order to resume, " - "we delete the newest role communication message in the role's memory.") + logger.warning( + "There is a exception in role's execution, in order to resume, " + "we delete the newest role communication message in the role's memory." + ) # remove role newest observed msg to make it observed again self._rc.memory.delete(self.latest_observed_msg) - raise Exception(format_trackback_info(limit=None)) # raise again to make it captured outside + # raise again to make it captured outside + raise Exception(format_trackback_info(limit=None)) return wrapper diff --git a/tests/metagpt/serialize_deserialize/test_environment.py b/tests/metagpt/serialize_deserialize/test_environment.py index b741b9c4b..096c1dd68 100644 --- a/tests/metagpt/serialize_deserialize/test_environment.py +++ b/tests/metagpt/serialize_deserialize/test_environment.py @@ -11,7 +11,11 @@ from metagpt.environment import Environment from metagpt.roles.project_manager import ProjectManager from metagpt.schema import Message from metagpt.utils.common import any_to_str -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleC, ActionOK, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import ( + ActionOK, + RoleC, + serdeser_path, +) def test_env_serialize(): @@ -35,10 +39,7 @@ def test_environment_serdeser(): ic_obj = ActionNode.create_model_class("prd", out_mapping) message = Message( - content="prd", - instruct_content=ic_obj(**out_data), - role="product manager", - cause_by=any_to_str(UserRequirement) + content="prd", instruct_content=ic_obj(**out_data), role="product manager", cause_by=any_to_str(UserRequirement) ) environment = Environment() diff --git a/tests/metagpt/serialize_deserialize/test_memory.py b/tests/metagpt/serialize_deserialize/test_memory.py index 0d756518b..5a40f5c3b 100644 --- a/tests/metagpt/serialize_deserialize/test_memory.py +++ b/tests/metagpt/serialize_deserialize/test_memory.py @@ -14,17 +14,14 @@ from tests.metagpt.serialize_deserialize.test_serdeser_base import serdeser_path def test_memory_serdeser(): - msg1 = Message(role="Boss", - content="write a snake game", - cause_by=UserRequirement) + msg1 = Message(role="Boss", content="write a snake game", cause_by=UserRequirement) out_mapping = {"field2": (list[str], ...)} out_data = {"field2": ["field2 value1", "field2 value2"]} ic_obj = ActionNode.create_model_class("system_design", out_mapping) - msg2 = Message(role="Architect", - instruct_content=ic_obj(**out_data), - content="system design content", - cause_by=WriteDesign) + msg2 = Message( + role="Architect", instruct_content=ic_obj(**out_data), content="system design content", cause_by=WriteDesign + ) memory = Memory() memory.add_batch([msg1, msg2]) @@ -40,17 +37,14 @@ def test_memory_serdeser(): def test_memory_serdeser_save(): - msg1 = Message(role="User", - content="write a 2048 game", - cause_by=UserRequirement) + msg1 = Message(role="User", content="write a 2048 game", cause_by=UserRequirement) out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} ic_obj = ActionNode.create_model_class("system_design", out_mapping) - msg2 = Message(role="Architect", - instruct_content=ic_obj(**out_data), - content="system design content", - cause_by=WriteDesign) + msg2 = Message( + role="Architect", instruct_content=ic_obj(**out_data), content="system design content", cause_by=WriteDesign + ) memory = Memory() memory.add_batch([msg1, msg2]) diff --git a/tests/metagpt/serialize_deserialize/test_role.py b/tests/metagpt/serialize_deserialize/test_role.py index 88c7f7d8b..72da8a6fc 100644 --- a/tests/metagpt/serialize_deserialize/test_role.py +++ b/tests/metagpt/serialize_deserialize/test_role.py @@ -16,7 +16,12 @@ from metagpt.roles.product_manager import ProductManager from metagpt.roles.role import Role from metagpt.schema import Message from metagpt.utils.common import format_trackback_info -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path +from tests.metagpt.serialize_deserialize.test_serdeser_base import ( + RoleA, + RoleB, + RoleC, + serdeser_path, +) def test_roles(): @@ -75,12 +80,10 @@ async def test_role_serdeser_interrupt(): role_c = RoleC() shutil.rmtree(SERDESER_PATH.joinpath("team"), ignore_errors=True) - stg_path = SERDESER_PATH.joinpath(f"team", "environment", "roles", "{role_c.__class__.__name__}_{role_c.name}") + stg_path = SERDESER_PATH.joinpath("team", "environment", "roles", f"{role_c.__class__.__name__}_{role_c.name}") try: - await role_c.run( - with_message=Message(content="demo", cause_by=UserRequirement) - ) - except Exception as exp: + await role_c.run(with_message=Message(content="demo", cause_by=UserRequirement)) + except Exception: logger.error(f"Exception in `role_a.run`, detail: {format_trackback_info()}") role_c.serialize(stg_path) @@ -90,6 +93,4 @@ async def test_role_serdeser_interrupt(): assert new_role_a._rc.state == 1 with pytest.raises(Exception): - await role_c.run( - with_message=Message(content="demo", cause_by=UserRequirement) - ) + await role_c.run(with_message=Message(content="demo", cause_by=UserRequirement)) diff --git a/tests/metagpt/serialize_deserialize/test_schema.py b/tests/metagpt/serialize_deserialize/test_schema.py index 72b7153a7..0358265a9 100644 --- a/tests/metagpt/serialize_deserialize/test_schema.py +++ b/tests/metagpt/serialize_deserialize/test_schema.py @@ -14,12 +14,7 @@ def test_message_serdeser(): out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} ic_obj = ActionNode.create_model_class("code", out_mapping) - message = Message( - content="code", - instruct_content=ic_obj(**out_data), - role="engineer", - cause_by=WriteCode - ) + message = Message(content="code", instruct_content=ic_obj(**out_data), role="engineer", cause_by=WriteCode) ser_data = message.dict() assert ser_data["cause_by"] == "metagpt.actions.write_code.WriteCode" assert ser_data["instruct_content"]["class"] == "code" @@ -31,14 +26,11 @@ def test_message_serdeser(): def test_message_without_postprocess(): - """ to explain `instruct_content` should be postprocessed """ + """to explain `instruct_content` should be postprocessed""" out_mapping = {"field1": (list[str], ...)} out_data = {"field1": ["field1 value1", "field1 value2"]} ic_obj = ActionNode.create_model_class("code", out_mapping) - message = MockMessage( - content="code", - instruct_content=ic_obj(**out_data) - ) + message = MockMessage(content="code", instruct_content=ic_obj(**out_data)) ser_data = message.dict() assert ser_data["instruct_content"] == {"field1": ["field1 value1", "field1 value2"]} diff --git a/tests/metagpt/serialize_deserialize/test_serdeser_base.py b/tests/metagpt/serialize_deserialize/test_serdeser_base.py index eac083cf9..a66813489 100644 --- a/tests/metagpt/serialize_deserialize/test_serdeser_base.py +++ b/tests/metagpt/serialize_deserialize/test_serdeser_base.py @@ -16,7 +16,8 @@ serdeser_path = Path(__file__).absolute().parent.joinpath("..", "..", "data", "s class MockMessage(BaseModel): - """ to test normal dict without postprocess """ + """to test normal dict without postprocess""" + content: str = "" instruct_content: BaseModel = Field(default=None) @@ -26,9 +27,7 @@ class ActionPass(Action): async def run(self, messages: list["Message"]) -> ActionOutput: await asyncio.sleep(5) # sleep to make other roles can watch the executed Message - output_mapping = { - "result": (str, ...) - } + output_mapping = {"result": (str, ...)} pass_class = ActionNode.create_model_class("pass", output_mapping) pass_output = ActionOutput("ActionPass run passed", pass_class(**{"result": "pass result"})) diff --git a/tests/metagpt/serialize_deserialize/test_team.py b/tests/metagpt/serialize_deserialize/test_team.py index db6001325..dc41fa4ed 100644 --- a/tests/metagpt/serialize_deserialize/test_team.py +++ b/tests/metagpt/serialize_deserialize/test_team.py @@ -8,10 +8,16 @@ import shutil import pytest from metagpt.const import SERDESER_PATH -from metagpt.roles import ProjectManager, ProductManager, Architect -from metagpt.team import Team from metagpt.logs import logger -from tests.metagpt.serialize_deserialize.test_serdeser_base import RoleA, RoleB, RoleC, serdeser_path, ActionOK +from metagpt.roles import Architect, ProductManager, ProjectManager +from metagpt.team import Team +from tests.metagpt.serialize_deserialize.test_serdeser_base import ( + ActionOK, + RoleA, + RoleB, + RoleC, + serdeser_path, +) def test_team_deserialize(): @@ -110,10 +116,8 @@ async def test_team_recover_multi_roles_save(): role_a = RoleA() role_b = RoleB() - assert role_a.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleA", - "RoleA"} - assert role_b.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleB", - "RoleB"} + assert role_a.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleA", "RoleA"} + assert role_b.subscription == {"tests.metagpt.serialize_deserialize.test_serdeser_base.RoleB", "RoleB"} assert role_b._rc.watch == {"tests.metagpt.serialize_deserialize.test_serdeser_base.ActionPass"} company = Team() diff --git a/tests/metagpt/serialize_deserialize/test_write_code.py b/tests/metagpt/serialize_deserialize/test_write_code.py index 0114c48da..65b8f456a 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code.py +++ b/tests/metagpt/serialize_deserialize/test_write_code.py @@ -19,8 +19,9 @@ def test_write_design_serialize(): @pytest.mark.asyncio async def test_write_code_deserialize(): - context = CodingContext(filename="test_code.py", - design_doc=Document(content="write add function to calculate two numbers")) + context = CodingContext( + filename="test_code.py", design_doc=Document(content="write add function to calculate two numbers") + ) doc = Document(content=context.json()) action = WriteCode(context=doc) serialized_data = action.dict() diff --git a/tests/metagpt/serialize_deserialize/test_write_code_review.py b/tests/metagpt/serialize_deserialize/test_write_code_review.py index a15b744db..01026590c 100644 --- a/tests/metagpt/serialize_deserialize/test_write_code_review.py +++ b/tests/metagpt/serialize_deserialize/test_write_code_review.py @@ -18,7 +18,7 @@ def div(a: int, b: int = 0): context = CodingContext( filename="test_op.py", design_doc=Document(content="divide two numbers"), - code_doc=Document(content=code_content) + code_doc=Document(content=code_content), ) action = WriteCodeReview(context=context) diff --git a/tests/metagpt/test_environment.py b/tests/metagpt/test_environment.py index ee322368e..56e2b4fc3 100644 --- a/tests/metagpt/test_environment.py +++ b/tests/metagpt/test_environment.py @@ -6,9 +6,10 @@ @File : test_environment.py """ -import pytest from pathlib import Path +import pytest + from metagpt.actions import UserRequirement from metagpt.environment import Environment from metagpt.logs import logger @@ -16,7 +17,6 @@ from metagpt.manager import Manager from metagpt.roles import Architect, ProductManager, Role from metagpt.schema import Message - serdeser_path = Path(__file__).absolute().parent.joinpath("../data/serdeser_storage") @@ -26,23 +26,16 @@ def env(): def test_add_role(env: Environment): - role = ProductManager(name="Alice", - profile="product manager", - goal="create a new product", - constraints="limited resources") + role = ProductManager( + name="Alice", profile="product manager", goal="create a new product", constraints="limited resources" + ) env.add_role(role) assert env.get_role(role.profile) == role def test_get_roles(env: Environment): - role1 = Role(name="Alice", - profile="product manager", - goal="create a new product", - constraints="limited resources") - role2 = Role(name="Bob", - profile="engineer", - goal="develop the new product", - constraints="short deadline") + role1 = Role(name="Alice", profile="product manager", goal="create a new product", constraints="limited resources") + role2 = Role(name="Bob", profile="engineer", goal="develop the new product", constraints="short deadline") env.add_role(role1) env.add_role(role2) roles = env.get_roles() @@ -51,14 +44,10 @@ def test_get_roles(env: Environment): @pytest.mark.asyncio async def test_publish_and_process_message(env: Environment): - product_manager = ProductManager(name="Alice", - profile="Product Manager", - goal="做AI Native产品", - constraints="资源有限") - architect = Architect(name="Bob", - profile="Architect", - goal="设计一个可用、高效、较低成本的系统,包括数据结构与接口", - constraints="资源有限,需要节省成本") + product_manager = ProductManager(name="Alice", profile="Product Manager", goal="做AI Native产品", constraints="资源有限") + architect = Architect( + name="Bob", profile="Architect", goal="设计一个可用、高效、较低成本的系统,包括数据结构与接口", constraints="资源有限,需要节省成本" + ) env.add_roles([product_manager, architect]) diff --git a/tests/metagpt/test_schema.py b/tests/metagpt/test_schema.py index ef706abfa..1742757e8 100644 --- a/tests/metagpt/test_schema.py +++ b/tests/metagpt/test_schema.py @@ -9,12 +9,13 @@ """ import json + import pytest from metagpt.actions import Action -from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.actions.action_node import ActionNode from metagpt.actions.write_code import WriteCode +from metagpt.schema import AIMessage, Message, SystemMessage, UserMessage from metagpt.utils.common import any_to_str @@ -77,24 +78,13 @@ def test_message_serdeser(): out_data = {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]} ic_obj = ActionNode.create_model_class("code", out_mapping) - message = Message( - content="code", - instruct_content=ic_obj(**out_data), - role="engineer", - cause_by=WriteCode - ) + message = Message(content="code", instruct_content=ic_obj(**out_data), role="engineer", cause_by=WriteCode) message_dict = message.dict() assert message_dict["cause_by"] == "metagpt.actions.write_code.WriteCode" assert message_dict["instruct_content"] == { "class": "code", - "mapping": { - "field3": "(, Ellipsis)", - "field4": "(list[str], Ellipsis)" - }, - "value": { - "field3": "field3 value3", - "field4": ["field4 value1", "field4 value2"] - } + "mapping": {"field3": "(, Ellipsis)", "field4": "(list[str], Ellipsis)"}, + "value": {"field3": "field3 value3", "field4": ["field4 value1", "field4 value2"]}, } new_message = Message(**message_dict) diff --git a/tests/metagpt/test_team.py b/tests/metagpt/test_team.py index efd035bb2..930306b5e 100644 --- a/tests/metagpt/test_team.py +++ b/tests/metagpt/test_team.py @@ -2,8 +2,8 @@ # -*- coding: utf-8 -*- # @Desc : unittest of team -from metagpt.team import Team from metagpt.roles.project_manager import ProjectManager +from metagpt.team import Team def test_team(): From f4198dc1116ff7ace820b56513556afe7e216354 Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 21 Dec 2023 11:03:13 +0800 Subject: [PATCH 412/413] refine action node and add some experiment --- metagpt/actions/action_node.py | 57 +-- metagpt/actions/write_code_an_draft.py | 591 +++++++++++++++++++++++++ metagpt/actions/write_review.py | 5 +- metagpt/utils/common.py | 3 +- tests/metagpt/test_prompt.py | 342 ++++++++++++++ 5 files changed, 968 insertions(+), 30 deletions(-) create mode 100644 metagpt/actions/write_code_an_draft.py create mode 100644 tests/metagpt/test_prompt.py diff --git a/metagpt/actions/action_node.py b/metagpt/actions/action_node.py index 4376e09ed..8a0aaf146 100644 --- a/metagpt/actions/action_node.py +++ b/metagpt/actions/action_node.py @@ -9,7 +9,7 @@ NOTE: You should use typing.List instead of list to do type annotation. Because we can use typing to extract the type of the node, but we cannot use built-in list to extract. """ import json -from typing import Any, Dict, Generic, List, Optional, Tuple, Type, TypeVar +from typing import Any, Dict, List, Optional, Tuple, Type from pydantic import BaseModel, create_model, root_validator, validator from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -19,10 +19,11 @@ from metagpt.logs import logger from metagpt.provider.postprecess.llm_output_postprecess import llm_output_postprecess from metagpt.utils.common import OutputParser, general_after_log -CONSTRAINT = """ -- Language: Please use the same language as the user input. -- Format: output wrapped inside [CONTENT][/CONTENT] as format example, nothing else. -""" +TAG = "CONTENT" + +LANGUAGE_CONSTRAINT = "Language: Please use the same language as the user input." +FORMAT_CONSTRAINT = f"Format: output wrapped inside [{TAG}][/{TAG}] like format example, nothing else." + SIMPLE_TEMPLATE = """ ## context @@ -33,28 +34,25 @@ SIMPLE_TEMPLATE = """ ## format example {example} -## nodes: ": # " +## nodes: ": # " {instruction} ## constraint {constraint} ## action -Fill in the above nodes based on the format example. +Follow instructions of nodes, generate output and make sure it follows the format example. """ -def dict_to_markdown(d, prefix="##", kv_sep="\n", postfix="\n"): +def dict_to_markdown(d, prefix="- ", kv_sep="\n", postfix="\n"): markdown_str = "" for key, value in d.items(): markdown_str += f"{prefix}{key}{kv_sep}{value}{postfix}" return markdown_str -T = TypeVar("T") - - -class ActionNode(Generic[T]): +class ActionNode: """ActionNode is a tree of nodes.""" mode: str @@ -69,7 +67,7 @@ class ActionNode(Generic[T]): expected_type: Type # such as str / int / float etc. # context: str # everything in the history. instruction: str # the instructions should be followed. - example: T # example for In Context-Learning. + example: Any # example for In Context-Learning. # Action Output content: str @@ -80,7 +78,7 @@ class ActionNode(Generic[T]): key: str, expected_type: Type, instruction: str, - example: T, + example: Any, content: str = "", children: dict[str, "ActionNode"] = None, ): @@ -183,11 +181,11 @@ class ActionNode(Generic[T]): return node_dict - def compile_to(self, i: Dict, schema) -> str: + def compile_to(self, i: Dict, schema, kv_sep) -> str: if schema == "json": return json.dumps(i, indent=4) elif schema == "markdown": - return dict_to_markdown(i) + return dict_to_markdown(i, kv_sep=kv_sep) else: return str(i) @@ -196,26 +194,26 @@ class ActionNode(Generic[T]): return text if schema == "json": return f"[{tag}]\n" + text + f"\n[/{tag}]" - else: + else: # markdown return f"[{tag}]\n" + text + f"\n[/{tag}]" - def _compile_f(self, schema, mode, tag, format_func) -> str: + def _compile_f(self, schema, mode, tag, format_func, kv_sep) -> str: nodes = self.to_dict(format_func=format_func, mode=mode) - text = self.compile_to(nodes, schema) + text = self.compile_to(nodes, schema, kv_sep) return self.tagging(text, schema, tag) - def compile_instruction(self, schema="raw", mode="children", tag="") -> str: + def compile_instruction(self, schema="markdown", mode="children", tag="") -> str: """compile to raw/json/markdown template with all/root/children nodes""" format_func = lambda i: f"{i.expected_type} # {i.instruction}" - return self._compile_f(schema, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func, kv_sep=": ") - def compile_example(self, schema="raw", mode="children", tag="") -> str: + def compile_example(self, schema="json", mode="children", tag="") -> str: """compile to raw/json/markdown examples with all/root/children nodes""" # 这里不能使用f-string,因为转译为str后再json.dumps会额外加上引号,无法作为有效的example # 错误示例:"File list": "['main.py', 'const.py', 'game.py']", 注意这里值不是list,而是str format_func = lambda i: i.example - return self._compile_f(schema, mode, tag, format_func) + return self._compile_f(schema, mode, tag, format_func, kv_sep="\n") def compile(self, context, schema="json", mode="children", template=SIMPLE_TEMPLATE) -> str: """ @@ -228,9 +226,16 @@ class ActionNode(Generic[T]): # FIXME: json instruction会带来格式问题,如:"Project name": "web_2048 # 项目名称使用下划线", # compile example暂时不支持markdown self.instruction = self.compile_instruction(schema="markdown", mode=mode) - self.example = self.compile_example(schema=schema, tag="CONTENT", mode=mode) + self.example = self.compile_example(schema=schema, tag=TAG, mode=mode) + # nodes = ", ".join(self.to_dict(mode=mode).keys()) + constraints = [LANGUAGE_CONSTRAINT, FORMAT_CONSTRAINT] + constraint = "\n".join(constraints) + prompt = template.format( - context=context, example=self.example, instruction=self.instruction, constraint=CONSTRAINT + context=context, + example=self.example, + instruction=self.instruction, + constraint=constraint, ) return prompt @@ -253,7 +258,7 @@ class ActionNode(Generic[T]): output_class = self.create_model_class(output_class_name, output_data_mapping) if schema == "json": - parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key="[/CONTENT]") + parsed_data = llm_output_postprecess(output=content, schema=output_class.schema(), req_key=f"[/{TAG}]") else: # using markdown parser parsed_data = OutputParser.parse_data_with_mapping(content, output_data_mapping) diff --git a/metagpt/actions/write_code_an_draft.py b/metagpt/actions/write_code_an_draft.py new file mode 100644 index 000000000..968c8924b --- /dev/null +++ b/metagpt/actions/write_code_an_draft.py @@ -0,0 +1,591 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Author : alexanderwu +@File : write_review.py +""" +import asyncio +from typing import List + +from metagpt.actions import Action +from metagpt.actions.action_node import ActionNode + +REVIEW = ActionNode( + key="Review", + expected_type=List[str], + instruction="Act as an experienced reviewer and critically assess the given output. Provide specific and" + " constructive feedback, highlighting areas for improvement and suggesting changes.", + example=[ + "The logic in the function `calculate_total` seems flawed. Shouldn't it consider the discount rate as well?", + "The TODO function is not implemented yet? Should we implement it before commit?", + ], +) + +LGTM = ActionNode( + key="LGTM", + expected_type=str, + instruction="LGTM/LBTM. If the code is fully implemented, " + "give a LGTM (Looks Good To Me), otherwise provide a LBTM (Looks Bad To Me).", + example="LBTM", +) + +ACTIONS = ActionNode( + key="Actions", + expected_type=str, + instruction="Based on the code review outcome, suggest actionable steps. This can include code changes, " + "refactoring suggestions, or any follow-up tasks.", + example="""1. Refactor the `process_data` method to improve readability and efficiency. +2. Cover edge cases in the `validate_user` function. +3. Implement a the TODO in the `calculate_total` function. +4. Fix the `handle_events` method to update the game state only if a move is successful. + ```python + def handle_events(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + if event.type == pygame.KEYDOWN: + moved = False + if event.key == pygame.K_UP: + moved = self.game.move('UP') + elif event.key == pygame.K_DOWN: + moved = self.game.move('DOWN') + elif event.key == pygame.K_LEFT: + moved = self.game.move('LEFT') + elif event.key == pygame.K_RIGHT: + moved = self.game.move('RIGHT') + if moved: + # Update the game state only if a move was successful + self.render() + return True + ``` +""", +) + +WRITE_DRAFT = ActionNode( + key="WriteDraft", + expected_type=str, + instruction="Could you write draft code for move function in order to implement it?", + example="Draft: ...", +) + + +WRITE_MOVE_FUNCTION = ActionNode( + key="WriteFunction", + expected_type=str, + instruction="write code for the function not implemented.", + example=""" +```Code +... +``` +""", +) + + +REWRITE_CODE = ActionNode( + key="RewriteCode", + expected_type=str, + instruction="""rewrite code based on the Review and Actions""", + example=""" +```python +## example.py +def calculate_total(price, quantity): + total = price * quantity +``` +""", +) + + +CODE_REVIEW_CONTEXT = """ +# System +Role: You are a professional software engineer, and your main task is to review and revise the code. You need to ensure that the code conforms to the google-style standards, is elegantly designed and modularized, easy to read and maintain. +Language: Please use the same language as the user requirement, but the title and code should be still in English. For example, if the user speaks Chinese, the specific text of your answer should also be in Chinese. + +# Context +## System Design +{"Implementation approach": "我们将使用HTML、CSS和JavaScript来实现这个单机的响应式2048游戏。为了确保游戏性能流畅和响应式设计,我们会选择使用Vue.js框架,因为它易于上手且适合构建交互式界面。我们还将使用localStorage来记录玩家的最高分。", "File list": ["index.html", "styles.css", "main.js", "game.js", "storage.js"], "Data structures and interfaces": "classDiagram\ + class Game {\ + -board Array\ + -score Number\ + -bestScore Number\ + +constructor()\ + +startGame()\ + +move(direction: String)\ + +getBoard() Array\ + +getScore() Number\ + +getBestScore() Number\ + +setBestScore(score: Number)\ + }\ + class Storage {\ + +getBestScore() Number\ + +setBestScore(score: Number)\ + }\ + class Main {\ + +init()\ + +bindEvents()\ + }\ + Game --> Storage : uses\ + Main --> Game : uses", "Program call flow": "sequenceDiagram\ + participant M as Main\ + participant G as Game\ + participant S as Storage\ + M->>G: init()\ + G->>S: getBestScore()\ + S-->>G: return bestScore\ + M->>G: bindEvents()\ + M->>G: startGame()\ + loop Game Loop\ + M->>G: move(direction)\ + G->>S: setBestScore(score)\ + S-->>G: return\ + end", "Anything UNCLEAR": "目前项目要求明确,没有不清楚的地方。"} + +## Tasks +{"Required Python packages": ["无需Python包"], "Required Other language third-party packages": ["vue.js"], "Logic Analysis": [["index.html", "作为游戏的入口文件和主要的HTML结构"], ["styles.css", "包含所有的CSS样式,确保游戏界面美观"], ["main.js", "包含Main类,负责初始化游戏和绑定事件"], ["game.js", "包含Game类,负责游戏逻辑,如开始游戏、移动方块等"], ["storage.js", "包含Storage类,用于获取和设置玩家的最高分"]], "Task list": ["index.html", "styles.css", "storage.js", "game.js", "main.js"], "Full API spec": "", "Shared Knowledge": "\'game.js\' 包含游戏逻辑相关的函数,被 \'main.js\' 调用。", "Anything UNCLEAR": "目前项目要求明确,没有不清楚的地方。"} + +## Code Files +----- index.html + + + + + + 2048游戏 + + + + +
+

2048

+
+
+
分数
+
{{ score }}
+
+
+
最高分
+
{{ bestScore }}
+
+
+
+
+
+ {{ cell !== 0 ? cell : \'\' }} +
+
+
+ +
+ + + + + + + + +----- styles.css +/* styles.css */ +body, html { + margin: 0; + padding: 0; + font-family: \'Arial\', sans-serif; +} + +#app { + text-align: center; + font-size: 18px; + color: #776e65; +} + +h1 { + color: #776e65; + font-size: 72px; + font-weight: bold; + margin: 20px 0; +} + +.scores-container { + display: flex; + justify-content: center; + margin-bottom: 20px; +} + +.score-container, .best-container { + background: #bbada0; + padding: 10px; + border-radius: 5px; + margin: 0 10px; + min-width: 100px; + text-align: center; +} + +.score-header, .best-header { + color: #eee4da; + font-size: 18px; + margin-bottom: 5px; +} + +.game-container { + max-width: 500px; + margin: 0 auto 20px; + background: #bbada0; + padding: 15px; + border-radius: 10px; + position: relative; +} + +.grid-row { + display: flex; +} + +.grid-cell { + background: #cdc1b4; + width: 100px; + height: 100px; + margin: 5px; + display: flex; + justify-content: center; + align-items: center; + font-size: 35px; + font-weight: bold; + color: #776e65; + border-radius: 3px; +} + +/* Dynamic classes for different number cells */ +.number-cell-2 { + background: #eee4da; +} + +.number-cell-4 { + background: #ede0c8; +} + +.number-cell-8 { + background: #f2b179; + color: #f9f6f2; +} + +.number-cell-16 { + background: #f59563; + color: #f9f6f2; +} + +.number-cell-32 { + background: #f67c5f; + color: #f9f6f2; +} + +.number-cell-64 { + background: #f65e3b; + color: #f9f6f2; +} + +.number-cell-128 { + background: #edcf72; + color: #f9f6f2; +} + +.number-cell-256 { + background: #edcc61; + color: #f9f6f2; +} + +.number-cell-512 { + background: #edc850; + color: #f9f6f2; +} + +.number-cell-1024 { + background: #edc53f; + color: #f9f6f2; +} + +.number-cell-2048 { + background: #edc22e; + color: #f9f6f2; +} + +/* Larger numbers need smaller font sizes */ +.number-cell-1024, .number-cell-2048 { + font-size: 30px; +} + +button { + background-color: #8f7a66; + color: #f9f6f2; + border: none; + border-radius: 3px; + padding: 10px 20px; + font-size: 18px; + cursor: pointer; + outline: none; +} + +button:hover { + background-color: #9f8b76; +} + +----- storage.js +## storage.js +class Storage { + // 获取最高分 + getBestScore() { + // 尝试从localStorage中获取最高分,如果不存在则默认为0 + const bestScore = localStorage.getItem(\'bestScore\'); + return bestScore ? Number(bestScore) : 0; + } + + // 设置最高分 + setBestScore(score) { + // 将最高分设置到localStorage中 + localStorage.setItem(\'bestScore\', score.toString()); + } +} + + + +## Code to be Reviewed: game.js +```Code +## game.js +class Game { + constructor() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.bestScore = 0; + } + + createEmptyBoard() { + const board = []; + for (let i = 0; i < 4; i++) { + board[i] = [0, 0, 0, 0]; + } + return board; + } + + startGame() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.addRandomTile(); + this.addRandomTile(); + } + + addRandomTile() { + let emptyCells = []; + for (let r = 0; r < 4; r++) { + for (let c = 0; c < 4; c++) { + if (this.board[r][c] === 0) { + emptyCells.push({ r, c }); + } + } + } + if (emptyCells.length > 0) { + let randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)]; + this.board[randomCell.r][randomCell.c] = Math.random() < 0.9 ? 2 : 4; + } + } + + move(direction) { + // This function will handle the logic for moving tiles + // in the specified direction and merging them + // It will also update the score and add a new random tile if the move is successful + // The actual implementation of this function is complex and would require + // a significant amount of code to handle all the cases for moving and merging tiles + // For the purposes of this example, we will not implement the full logic + // Instead, we will just call addRandomTile to simulate a move + this.addRandomTile(); + } + + getBoard() { + return this.board; + } + + getScore() { + return this.score; + } + + getBestScore() { + return this.bestScore; + } + + setBestScore(score) { + this.bestScore = score; + } +} + +``` +""" + + +CODE_REVIEW_SMALLEST_CONTEXT = """ +## Code to be Reviewed: game.js +```Code +// game.js +class Game { + constructor() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.bestScore = 0; + } + + createEmptyBoard() { + const board = []; + for (let i = 0; i < 4; i++) { + board[i] = [0, 0, 0, 0]; + } + return board; + } + + startGame() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.addRandomTile(); + this.addRandomTile(); + } + + addRandomTile() { + let emptyCells = []; + for (let r = 0; r < 4; r++) { + for (let c = 0; c < 4; c++) { + if (this.board[r][c] === 0) { + emptyCells.push({ r, c }); + } + } + } + if (emptyCells.length > 0) { + let randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)]; + this.board[randomCell.r][randomCell.c] = Math.random() < 0.9 ? 2 : 4; + } + } + + move(direction) { + // This function will handle the logic for moving tiles + // in the specified direction and merging them + // It will also update the score and add a new random tile if the move is successful + // The actual implementation of this function is complex and would require + // a significant amount of code to handle all the cases for moving and merging tiles + // For the purposes of this example, we will not implement the full logic + // Instead, we will just call addRandomTile to simulate a move + this.addRandomTile(); + } + + getBoard() { + return this.board; + } + + getScore() { + return this.score; + } + + getBestScore() { + return this.bestScore; + } + + setBestScore(score) { + this.bestScore = score; + } +} + +``` +""" + + +CODE_REVIEW_SAMPLE = """ +## Code Review: game.js +1. The code partially implements the requirements. The `Game` class is missing the full implementation of the `move` method, which is crucial for the game\'s functionality. +2. The code logic is not completely correct. The `move` method is not implemented, which means the game cannot process player moves. +3. The existing code follows the "Data structures and interfaces" in terms of class structure but lacks full method implementations. +4. Not all functions are implemented. The `move` method is incomplete and does not handle the logic for moving and merging tiles. +5. All necessary pre-dependencies seem to be imported since the code does not indicate the need for additional imports. +6. The methods from other files (such as `Storage`) are not being used in the provided code snippet, but the class structure suggests that they will be used correctly. + +## Actions +1. Implement the `move` method to handle tile movements and merging. This is a complex task that requires careful consideration of the game\'s rules and logic. Here is a simplified version of how one might begin to implement the `move` method: + ```javascript + move(direction) { + // Simplified logic for moving tiles up + if (direction === \'up\') { + for (let col = 0; col < 4; col++) { + let tiles = this.board.map(row => row[col]).filter(val => val !== 0); + let merged = []; + for (let i = 0; i < tiles.length; i++) { + if (tiles[i] === tiles[i + 1]) { + tiles[i] *= 2; + this.score += tiles[i]; + tiles[i + 1] = 0; + merged.push(i); + } + } + tiles = tiles.filter(val => val !== 0); + while (tiles.length < 4) { + tiles.push(0); + } + for (let row = 0; row < 4; row++) { + this.board[row][col] = tiles[row]; + } + } + } + // Additional logic needed for \'down\', \'left\', \'right\' + // ... + this.addRandomTile(); + } + ``` +2. Integrate the `Storage` class methods to handle the best score. This means updating the `startGame` and `setBestScore` methods to use `Storage` for retrieving and setting the best score: + ```javascript + startGame() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.bestScore = new Storage().getBestScore(); // Retrieve the best score from storage + this.addRandomTile(); + this.addRandomTile(); + } + + setBestScore(score) { + if (score > this.bestScore) { + this.bestScore = score; + new Storage().setBestScore(score); // Set the new best score in storage + } + } + ``` + +## Code Review Result +LBTM + +``` +""" + + +WRITE_CODE_NODE = ActionNode.from_children("WRITE_REVIEW_NODE", [REVIEW, LGTM, ACTIONS]) +WRITE_MOVE_NODE = ActionNode.from_children("WRITE_MOVE_NODE", [WRITE_DRAFT, WRITE_MOVE_FUNCTION]) + + +CR_FOR_MOVE_FUNCTION_BY_3 = """ +The move function implementation provided appears to be well-structured and follows a clear logic for moving and merging tiles in the specified direction. However, there are a few potential improvements that could be made to enhance the code: + +1. Encapsulation: The logic for moving and merging tiles could be encapsulated into smaller, reusable functions to improve readability and maintainability. + +2. Magic Numbers: There are some magic numbers (e.g., 4, 3) used in the loops that could be replaced with named constants for improved readability and easier maintenance. + +3. Comments: Adding comments to explain the logic and purpose of each section of the code can improve understanding for future developers who may need to work on or maintain the code. + +4. Error Handling: It's important to consider error handling for unexpected input or edge cases to ensure the function behaves as expected in all scenarios. + +Overall, the code could benefit from refactoring to improve readability, maintainability, and extensibility. If you would like, I can provide a refactored version of the move function that addresses these considerations. +""" + + +class WriteCodeAN(Action): + """Write a code review for the context.""" + + async def run(self, context): + self.llm.system_prompt = "You are an outstanding engineer and can implement any code" + return await WRITE_MOVE_FUNCTION.fill(context=context, llm=self.llm, schema="json") + # return await WRITE_CODE_NODE.fill(context=context, llm=self.llm, schema="markdown") + + +async def main(): + await WriteCodeAN().run(CODE_REVIEW_SMALLEST_CONTEXT) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/metagpt/actions/write_review.py b/metagpt/actions/write_review.py index 13690a1a5..8a4856317 100644 --- a/metagpt/actions/write_review.py +++ b/metagpt/actions/write_review.py @@ -31,8 +31,7 @@ WRITE_REVIEW_NODE = ActionNode.from_children("WRITE_REVIEW_NODE", [REVIEW, LGTM] class WriteReview(Action): - """This class allows LLM to further mine noteworthy details based on specific "##TOPIC"(discussion topic) and - "##RECORD" (discussion records), thereby deepening the discussion.""" + """Write a review for the given context.""" async def run(self, context): - return await WRITE_REVIEW_NODE.fill(context=context, llm=self.llm, schema="markdown") + return await WRITE_REVIEW_NODE.fill(context=context, llm=self.llm, schema="json") diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index ea3316d66..e5d4573e8 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -158,7 +158,8 @@ class OutputParser: @classmethod def parse_data_with_mapping(cls, data, mapping): - data = cls.extract_content(text=data) + if "[CONTENT]" in data: + data = cls.extract_content(text=data) block_dict = cls.parse_blocks(data) parsed_data = {} for block, content in block_dict.items(): diff --git a/tests/metagpt/test_prompt.py b/tests/metagpt/test_prompt.py new file mode 100644 index 000000000..f7b1cc68e --- /dev/null +++ b/tests/metagpt/test_prompt.py @@ -0,0 +1,342 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@Time : 2023/5/11 14:45 +@Author : alexanderwu +@File : test_llm.py +""" + +import pytest + +from metagpt.llm import LLM + +CODE_REVIEW_SMALLEST_CONTEXT = """ +## game.js +```Code +// game.js +class Game { + constructor() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.bestScore = 0; + } + + createEmptyBoard() { + const board = []; + for (let i = 0; i < 4; i++) { + board[i] = [0, 0, 0, 0]; + } + return board; + } + + startGame() { + this.board = this.createEmptyBoard(); + this.score = 0; + this.addRandomTile(); + this.addRandomTile(); + } + + addRandomTile() { + let emptyCells = []; + for (let r = 0; r < 4; r++) { + for (let c = 0; c < 4; c++) { + if (this.board[r][c] === 0) { + emptyCells.push({ r, c }); + } + } + } + if (emptyCells.length > 0) { + let randomCell = emptyCells[Math.floor(Math.random() * emptyCells.length)]; + this.board[randomCell.r][randomCell.c] = Math.random() < 0.9 ? 2 : 4; + } + } + + move(direction) { + // This function will handle the logic for moving tiles + // in the specified direction and merging them + // It will also update the score and add a new random tile if the move is successful + // The actual implementation of this function is complex and would require + // a significant amount of code to handle all the cases for moving and merging tiles + // For the purposes of this example, we will not implement the full logic + // Instead, we will just call addRandomTile to simulate a move + this.addRandomTile(); + } + + getBoard() { + return this.board; + } + + getScore() { + return this.score; + } + + getBestScore() { + return this.bestScore; + } + + setBestScore(score) { + this.bestScore = score; + } +} + +``` +""" + +MOVE_DRAFT = """ +## move function draft + +```javascript +move(direction) { + let moved = false; + switch (direction) { + case 'up': + for (let c = 0; c < 4; c++) { + for (let r = 1; r < 4; r++) { + if (this.board[r][c] !== 0) { + let row = r; + while (row > 0 && this.board[row - 1][c] === 0) { + this.board[row - 1][c] = this.board[row][c]; + this.board[row][c] = 0; + row--; + moved = true; + } + if (row > 0 && this.board[row - 1][c] === this.board[row][c]) { + this.board[row - 1][c] *= 2; + this.board[row][c] = 0; + this.score += this.board[row - 1][c]; + moved = true; + } + } + } + } + break; + case 'down': + // Implement logic for moving tiles down + // Similar to the 'up' case but iterating in reverse order + // and checking for merging in the opposite direction + break; + case 'left': + // Implement logic for moving tiles left + // Similar to the 'up' case but iterating over columns first + // and checking for merging in the opposite direction + break; + case 'right': + // Implement logic for moving tiles right + // Similar to the 'up' case but iterating over columns in reverse order + // and checking for merging in the opposite direction + break; + } + + if (moved) { + this.addRandomTile(); + } +} +``` +""" + +FUNCTION_TO_MERMAID_CLASS = """ +## context +``` +class UIDesign(Action): + #Class representing the UI Design action. + def __init__(self, name, context=None, llm=None): + super().__init__(name, context, llm) # 需要调用LLM进一步丰富UI设计的prompt + @parse + def parse_requirement(self, context: str): + #Parse UI Design draft from the context using regex. + pattern = r"## UI Design draft.*?\n(.*?)## Anything UNCLEAR" + return context, pattern + @parse + def parse_ui_elements(self, context: str): + #Parse Selected Elements from the context using regex. + pattern = r"## Selected Elements.*?\n(.*?)## HTML Layout" + return context, pattern + @parse + def parse_css_code(self, context: str): + pattern = r"```css.*?\n(.*?)## Anything UNCLEAR" + return context, pattern + @parse + def parse_html_code(self, context: str): + pattern = r"```html.*?\n(.*?)```" + return context, pattern + async def draw_icons(self, context, *args, **kwargs): + #Draw icons using SDEngine. + engine = SDEngine() + icon_prompts = self.parse_ui_elements(context) + icons = icon_prompts.split("\n") + icons = [s for s in icons if len(s.strip()) > 0] + prompts_batch = [] + for icon_prompt in icons: + # fixme: 添加icon lora + prompt = engine.construct_payload(icon_prompt + ".") + prompts_batch.append(prompt) + await engine.run_t2i(prompts_batch) + logger.info("Finish icon design using StableDiffusion API") + async def _save(self, css_content, html_content): + save_dir = CONFIG.workspace_path / "resources" / "codes" + if not os.path.exists(save_dir): + os.makedirs(save_dir, exist_ok=True) + # Save CSS and HTML content to files + css_file_path = save_dir / "ui_design.css" + html_file_path = save_dir / "ui_design.html" + with open(css_file_path, "w") as css_file: + css_file.write(css_content) + with open(html_file_path, "w") as html_file: + html_file.write(html_content) + async def run(self, requirements: list[Message], *args, **kwargs) -> ActionOutput: + #Run the UI Design action. + # fixme: update prompt (根据需求细化prompt) + context = requirements[-1].content + ui_design_draft = self.parse_requirement(context=context) + # todo: parse requirements str + prompt = PROMPT_TEMPLATE.format(context=ui_design_draft, format_example=FORMAT_EXAMPLE) + logger.info(prompt) + ui_describe = await self._aask_v1(prompt, "ui_design", OUTPUT_MAPPING) + logger.info(ui_describe.content) + logger.info(ui_describe.instruct_content) + css = self.parse_css_code(context=ui_describe.content) + html = self.parse_html_code(context=ui_describe.content) + await self._save(css_content=css, html_content=html) + await self.draw_icons(ui_describe.content) + return ui_describe +``` +----- +## format example +[CONTENT] +{ + "ClassView": "classDiagram\n class A {\n -int x\n +int y\n -int speed\n -int direction\n +__init__(x: int, y: int, speed: int, direction: int)\n +change_direction(new_direction: int) None\n +move() None\n }\n " +} +[/CONTENT] +## nodes: ": # " +- ClassView: # Generate the mermaid class diagram corresponding to source code in "context." +## constraint +- Language: Please use the same language as the user input. +- Format: output wrapped inside [CONTENT][/CONTENT] as format example, nothing else. +## action +Fill in the above nodes(ClassView) based on the format example. +""" + +MOVE_FUNCTION = """ +## move function implementation + +```javascript +move(direction) { + let moved = false; + switch (direction) { + case 'up': + for (let c = 0; c < 4; c++) { + for (let r = 1; r < 4; r++) { + if (this.board[r][c] !== 0) { + let row = r; + while (row > 0 && this.board[row - 1][c] === 0) { + this.board[row - 1][c] = this.board[row][c]; + this.board[row][c] = 0; + row--; + moved = true; + } + if (row > 0 && this.board[row - 1][c] === this.board[row][c]) { + this.board[row - 1][c] *= 2; + this.board[row][c] = 0; + this.score += this.board[row - 1][c]; + moved = true; + } + } + } + } + break; + case 'down': + for (let c = 0; c < 4; c++) { + for (let r = 2; r >= 0; r--) { + if (this.board[r][c] !== 0) { + let row = r; + while (row < 3 && this.board[row + 1][c] === 0) { + this.board[row + 1][c] = this.board[row][c]; + this.board[row][c] = 0; + row++; + moved = true; + } + if (row < 3 && this.board[row + 1][c] === this.board[row][c]) { + this.board[row + 1][c] *= 2; + this.board[row][c] = 0; + this.score += this.board[row + 1][c]; + moved = true; + } + } + } + } + break; + case 'left': + for (let r = 0; r < 4; r++) { + for (let c = 1; c < 4; c++) { + if (this.board[r][c] !== 0) { + let col = c; + while (col > 0 && this.board[r][col - 1] === 0) { + this.board[r][col - 1] = this.board[r][col]; + this.board[r][col] = 0; + col--; + moved = true; + } + if (col > 0 && this.board[r][col - 1] === this.board[r][col]) { + this.board[r][col - 1] *= 2; + this.board[r][col] = 0; + this.score += this.board[r][col - 1]; + moved = true; + } + } + } + } + break; + case 'right': + for (let r = 0; r < 4; r++) { + for (let c = 2; c >= 0; c--) { + if (this.board[r][c] !== 0) { + let col = c; + while (col < 3 && this.board[r][col + 1] === 0) { + this.board[r][col + 1] = this.board[r][col]; + this.board[r][col] = 0; + col++; + moved = true; + } + if (col < 3 && this.board[r][col + 1] === this.board[r][col]) { + this.board[r][col + 1] *= 2; + this.board[r][col] = 0; + this.score += this.board[r][col + 1]; + moved = true; + } + } + } + } + break; + } + + if (moved) { + this.addRandomTile(); + } +} +``` +""" + + +@pytest.fixture() +def llm(): + return LLM() + + +@pytest.mark.asyncio +async def test_llm_code_review(llm): + choices = [ + "Please review the move function code above. Should it be refactor?", + "Please implement the move function", + "Please write a draft for the move function in order to implement it", + ] + # prompt = CODE_REVIEW_SMALLEST_CONTEXT+ "\n\n" + MOVE_DRAFT + "\n\n" + choices[1] + # rsp = await llm.aask(prompt) + + prompt = CODE_REVIEW_SMALLEST_CONTEXT + "\n\n" + MOVE_FUNCTION + "\n\n" + choices[0] + prompt = FUNCTION_TO_MERMAID_CLASS + + _ = await llm.aask(prompt) + + +# if __name__ == "__main__": +# pytest.main([__file__, "-s"]) From e772ffdc1e8836d45428f1050dc50dad0c1a843b Mon Sep 17 00:00:00 2001 From: geekan Date: Thu, 21 Dec 2023 11:05:24 +0800 Subject: [PATCH 413/413] fix pydantic not support future issue --- metagpt/actions/action.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index 62434e7f8..cd2b5148f 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -8,7 +8,7 @@ from __future__ import annotations -from typing import Any, Optional +from typing import Any, Optional, Union from pydantic import BaseModel, Field @@ -27,7 +27,7 @@ action_subclass_registry = {} class Action(BaseModel): name: str = "" llm: BaseGPTAPI = Field(default_factory=LLM, exclude=True) - context: dict | CodingContext | CodeSummarizeContext | TestingContext | RunCodeContext | str | None = "" + context: Union[dict, CodingContext, CodeSummarizeContext, TestingContext, RunCodeContext, str, None] = "" prefix = "" # aask*时会加上prefix,作为system_message desc = "" # for skill manager # node: ActionNode = Field(default_factory=ActionNode, exclude=True)