接着MetaGPT入门(一),在文件里再添加一个role类
class SimpleCoder(Role):def __init__(self,name:str="Alice",profile:str="SimpleCoder",**kwargs):super().__init__(name,profile,**kwargs)self._init_actions([WriteSimpleCode])async def _act(self) -> Message:#记录角色的相关信息logger.info(f'{self._setting}:ready to {self._rc.todo}')#获取需要执行的动作,todo will be SimpleWriteCode()todo = self._rc.todo#获取动作执行需要的信息msg = self.get_memories(k=1)[0]#开始执行动作code_text = await todo.run(msg.content)#将动作的结果封装成消息形式返回,方便与其他Agent通信使用吧msg = Message(content=code_text,role=self.profile, cause_by=type(todo))return msgasync def main():msg = "write a function that calculates the sum of a list"role = SimpleCoder()logger.info(msg)result = await role.run(msg)logger.info(result)asyncio.run(main())
logger里会详细记录运行过程及各种设置,可以仔细研究
运行结果正确