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

运行结果正确

相关推荐
李可以量化16 分钟前
Tornado 从入门到精通(二)下:协程调用方式与经典使用模式
python
雪碧透心凉_24 分钟前
Python OpenCV图像处理入门
图像处理·python·opencv
李可以量化39 分钟前
Tornado 从入门到精通(二)上:协程核心机制详解
python
晴天1641 分钟前
AgentLoop分享(上): 让 AI 真正“自主干活“-Day15
人工智能·python
Patrick在香港1 小时前
Python爬虫框架实战:优雅抓取香港政府公开API数据的通用方案
java·开发语言·爬虫·python·ai编程·数据可视化·可用性测试
李白的天不白1 小时前
python下载脚本
python
2501_909509101 小时前
DAY 45
python
郝学胜_神的一滴2 小时前
Python 高级编程 027:四大推导式全攻略
python·pycharm
菜冻鱼2 小时前
Python-sklearn-特征工程
开发语言·人工智能·python·机器学习·numpy·matplotlib·sklearn
mit6.8242 小时前
archived
后端·python·flask