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

运行结果正确

相关推荐
大模型真好玩13 分钟前
深入浅出LangGraph AI Agent智能体开发教程(五)—LangGraph 数据分析助手智能体项目实战
人工智能·python·mcp
测试老哥18 分钟前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
百锦再44 分钟前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
张子夜 iiii1 小时前
4步OpenCV-----扫秒身份证号
人工智能·python·opencv·计算机视觉
潮汐退涨月冷风霜2 小时前
数字图像处理(1)OpenCV C++ & Opencv Python显示图像和视频
c++·python·opencv
酷飞飞8 小时前
Python网络与多任务编程:TCP/UDP实战指南
网络·python·tcp/ip
数字化顾问9 小时前
Python:OpenCV 教程——从传统视觉到深度学习:YOLOv8 与 OpenCV DNN 模块协同实现工业缺陷检测
python
学生信的大叔10 小时前
【Python自动化】Ubuntu24.04配置Selenium并测试
python·selenium·自动化
诗句藏于尽头11 小时前
Django模型与数据库表映射的两种方式
数据库·python·django
智数研析社12 小时前
9120 部 TMDb 高分电影数据集 | 7 列全维度指标 (评分 / 热度 / 剧情)+API 权威源 | 电影趋势分析 / 推荐系统 / NLP 建模用
大数据·人工智能·python·深度学习·数据分析·数据集·数据清洗