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

运行结果正确

相关推荐
宸津-代码粉碎机2 分钟前
Spring 6.0+Boot 3.0实战避坑全指南:5大类高频问题与解决方案(附代码示例)
java·数据仓库·hive·hadoop·python·技术文档编写
傻啦嘿哟2 分钟前
Python自动整理音乐文件:按艺术家和专辑分类歌曲
数据库·python·分类
weixin_462446237 分钟前
基于 Flask + lunar-python 的农历转换 API 实战(公历 ↔ 农历 / 干支 / 生肖 / 节日)
python·flask·节日
weixin_5795996620 分钟前
编写一个程序,输入两个数字的加减乘除余数(Python版)
开发语言·python
liu****23 分钟前
02_Pandas_数据结构
数据结构·python·pandas·python基础
RFCEO42 分钟前
用手机写 Python程序解决方案
开发语言·python·智能手机·qpython环境安装
0思必得043 分钟前
[Web自动化] Requests模块基本使用
运维·前端·python·自动化·html·web自动化
AAA简单玩转程序设计1 小时前
救命!Python 这些基础操作居然能省一半工作量
python
Brduino脑机接口技术答疑1 小时前
TDCA 算法在 SSVEP 场景中:Padding 的应用对象与工程实践指南
人工智能·python·算法·数据分析·脑机接口·eeg
quant_19861 小时前
如何处理大规模行情数据:从源头到终端的实战教程
大数据·开发语言·经验分享·python·金融