MetaGPT入门(二)

接着MetaGPT入门(一),在文件里再添加一个role类

python 复制代码
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 msg

async 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里会详细记录运行过程及各种设置,可以仔细研究

运行结果正确

相关推荐
2601_962299883 分钟前
Linux执行Python脚本方法
linux·python·脚本·解释器·shebang
旋生万物1 小时前
量子纠错容错率87%?螺旋拓扑码用“相位保护“给量子比特上保险(附Python)
python·ai编程·量子计算·量子纠错·螺旋计算
用户8356290780511 小时前
使用 Python 设置 Excel 页眉和页脚
后端·python
databook1 小时前
异常值检测与处理方法总结
python·数据挖掘·数据分析
千里码aicood1 小时前
面向智能助理的多轮医疗对话生成系统开发
python
MC皮蛋侠客1 小时前
OPC UA 系列(一):标准全景与 Python 最小闭环——让第一条设备数据流动起来
开发语言·python·opcua
用户3721574261351 小时前
使用 Python 提取 Word 文档中的表格数据
python
有味道的男人2 小时前
得物详情 API|支持实时价格、成色、鉴别服务数据
windows·python·api
小溪学编程2 小时前
Java BufferedReader 详解:从基础用法到性能优化
java·python·性能优化
2601_962300472 小时前
基于Python与Tkinter的ADB Monkey自动化测试脚本开发
python·adb·tkinter·android测试·gui自动化