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

运行结果正确

相关推荐
人工干智能3 分钟前
Chat Completions API中的三种role:“system“,“user“,“assistant“
python·llm
Darenm1117 分钟前
JWT鉴权的实现:从原理到 Django + Vue3
后端·python·django
Funny_AI_LAB17 分钟前
Zcode:智谱AI推出的轻量级 AI IDE 编程利器
人工智能·python·算法·编辑器
2501_9444522327 分钟前
活动记录 Cordova 与 OpenHarmony 混合开发实战
python
子夜江寒29 分钟前
基于 Python 使用 SVM、K-means与DBSCAN
python·支持向量机·kmeans
Blossom.11841 分钟前
GPTQ量化实战:从零手写大模型权重量化与反量化引擎
人工智能·python·算法·chatgpt·ai作画·自动化·transformer
Elaine3361 小时前
实战教学:使用 Scrapy 爬取 CSDN 文章与用户头像
python·scrapy·网络爬虫
程序员佳佳2 小时前
文章标题:彻底抛弃OpenAI官方Key?实测GPT-5.2与Banana Pro(Gemini 3):这才是开发者的终极红利!
开发语言·人工智能·python·gpt·ai作画·api·midjourney
qq_356196952 小时前
day49_通道注意力机制 @浙大疏锦行
python
Yeats_Liao2 小时前
MindSpore开发之路(十四):简化训练循环:高阶API `mindspore.Model` 的妙用
人工智能·python·深度学习