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

运行结果正确

相关推荐
ahead~21 分钟前
【大模型入门】访问GPT_API实战案例
人工智能·python·gpt·大语言模型llm
大模型真好玩1 小时前
准确率飙升!GraphRAG如何利用知识图谱提升RAG答案质量(额外篇)——大规模文本数据下GraphRAG实战
人工智能·python·mcp
19891 小时前
【零基础学AI】第30讲:生成对抗网络(GAN)实战 - 手写数字生成
人工智能·python·深度学习·神经网络·机器学习·生成对抗网络·近邻算法
applebomb1 小时前
没合适的组合wheel包,就自行编译flash_attn吧
python·ubuntu·attention·flash
Chasing__Dreams2 小时前
python--杂识--18.1--pandas数据插入sqlite并进行查询
python·sqlite·pandas
彭泽布衣3 小时前
python2.7/lib-dynload/_ssl.so: undefined symbol: sk_pop_free
python·sk_pop_free
喜欢吃豆3 小时前
从零构建MCP服务器:FastMCP实战指南
运维·服务器·人工智能·python·大模型·mcp
一个处女座的测试3 小时前
Python语言+pytest框架+allure报告+log日志+yaml文件+mysql断言实现接口自动化框架
python·mysql·pytest
nananaij4 小时前
【Python基础入门 re模块实现正则表达式操作】
开发语言·python·正则表达式
蛋仔聊测试4 小时前
Playwright 网络流量监控与修改指南
python