🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
Merge branch 'main' into main
brucemeek684a4f4c2e07
diff --cc metagpt/config.pyindex e479ab018,d53571468..3753bb3b0--- a/metagpt/config.py+++ b/metagpt/config.py@@@ -28,9 -28,9 +28,9 @@@ class NotConfiguredException(Exception)class Config(metaclass=Singleton):"""- 常规使用方法:+ Regular usage method:config = Config("config.yaml")- secret_key = config.get("MY_SECRET_KEY")+ secret_key = config.get_key("MY_SECRET_KEY")print("Secret key:", secret_key)"""@@@ -79,9 -79,13 +79,12 @@@self.total_cost = 0.0self.puppeteer_config = self._get("PUPPETEER_CONFIG","")self.mmdc = self._get("MMDC","mmdc")+ self.update_costs = self._get("UPDATE_COSTS",True)+ self.calc_usage = self._get("CALC_USAGE",True)+-def _init_with_config_files_and_env(self, configs: dict, yaml_file):- """从config/key.yaml / config/config.yaml / env三处按优先级递减加载"""+ """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]:diff --cc metagpt/roles/role.pyindex bfdd7f18a,3e3fef7b9..fa2971ae7--- a/metagpt/roles/role.py+++ b/metagpt/roles/role.py@@@ -158,85 -158,85 +158,85 @@@ class Rolenext_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))- 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)-- news = self._rc.memory.remember(observed) # remember recent exact or similar memories-- for i in env_msgs:- self.recv(i)-- news_text = [f"{i.role}: {i.content[:20]}..." for i in news]- if news_text:- logger.debug(f'{self._setting} observed: {news_text}')- return len(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"""- 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):- """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 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)- return rsp+ 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)-+++ news = self._rc.memory.remember(observed) # remember recent exact or similar memories++ for i in env_msgs:+ self.recv(i)++ news_text = [f"{i.role}: {i.content[:20]}..." for i in news]+ if news_text:+ logger.debug(f'{self._setting} observed: {news_text}')+ return len(news)++ def _publish_message(self, msg):- """如果role归属于env,那么role的消息会向env广播"""++ """If the role belongs to env, then the role's messages will be broadcast to env"""+ if not self._rc.env:- # 如果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"""+ 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):- """观察,并基于观察的结果思考、行动"""++ """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 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)+ return rsp