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

运行结果正确

相关推荐
清水白石00820 小时前
模板方法模式全解析:用抽象基类定义算法骨架,让子类优雅填充细节
数据库·python·算法·模板方法模式
封奚泽优20 小时前
使用mmdetection项目进行训练记录
pytorch·python·cuda·mmdetection·mmcv
newbiai20 小时前
TikTok矩阵账号引流怎么解决效率低成本高?
python·线性代数·矩阵
深蓝电商API20 小时前
结构化数据提取:XPath vs CSS 选择器对比
爬虫·python
SCBAiotAigc20 小时前
2026.2.25:conda与uv并存时,如何取消base激活
人工智能·python·conda·uv
wanderful_20 小时前
Django 模拟支付功能开发:踩坑与闭环实现
后端·python·django
YouGIS顽石工坊20 小时前
坐标转换服务私有化部署指南 - 源码与Docker两种方案
python·restful·docker镜像·python源码·pyproj·坐标转换服务·高斯克吕格投影
李可以量化20 小时前
【Python 量化入门】AKshare 保姆级使用教程:零成本获取股票 / 基金 / 期货全市场金融数据
开发语言·python·金融·qmt·miniqmt·量化 qmt ptrade
迷之程序员21 小时前
llama-cpp-python用法,模型加载gpu踩坑全记录
开发语言·python·llama