metagpt 多智能体系统
metagpt 多智能体系统
- 代码
- 1. 动作及角色定义
- 2. 主函数
- 代码解释
- 1. 导入模块:
- 2. 环境设置:
- 3. 定义行动(Action):
- 4. 定义角色(Role):
- 5. 学生和老师的行为:
- 6. 主函数(main):
- 7. 运行主函数:
代码
1. 动作及角色定义
import asynciofrom metagpt.actions import Action, UserRequirement
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.environment import Environmentfrom metagpt.const import MESSAGE_ROUTE_TO_ALLclassroom = Environment() class WritePoem(Action):name: str = "WritePoem"PROMPT_TEMPLATE: str = """Here is the historical conversation record : {msg} .Write a poem about the subject provided by human, Return only the content of the generated poem with NO other texts.If the teacher provides suggestions about the poem, revise the student's poem based on the suggestions and return.your poem:"""async def run(self, msg: str):prompt = self.PROMPT_TEMPLATE.format(msg = msg)rsp = await self._aask(prompt)return rspclass ReviewPoem(Action):name: str = "ReviewPoem"PROMPT_TEMPLATE: str = """Here is the historical conversation record : {msg} .Check student-created poems about the subject provided by human and give your suggestions for revisions. You prefer poems with elegant sentences and retro style.Return only your comments with NO other texts.your comments:"""async def run(self, msg: str):prompt = self.PROMPT_TEMPLATE.format(msg = msg)rsp = await self._aask(prompt)return rspclass Student(Role):name: str = "xiaoming"profile: str = "Student"def __init__(self, **kwargs):super().__init__(**kwargs)self.set_actions([WritePoem])self._watch([UserRequirement, ReviewPoem])async def _act(self) -> Message:logger.info(f"{self._setting}: ready to {self.rc.todo}")todo = self.rc.todomsg = self.get_memories() # 获取所有记忆# logger.info(msg)poem_text = await WritePoem().run(msg)logger.info(f'student : {poem_text}')msg = Message(content=poem_text, role=self.profile,cause_by=type(todo))return msgclass Teacher(Role):name: str = "laowang"profile: str = "Teacher"def __init__(self, **kwargs):super().__init__(**kwargs)self.set_actions([ReviewPoem])self._watch([WritePoem])async def _act(self) -> Message:logger.info(f"{self._setting}: ready to {self.rc.todo}")todo = self.rc.todomsg = self.get_memories() # 获取所有记忆poem_text = await ReviewPoem().run(msg)logger.info(f'teacher : {poem_text}')msg = Message(content=poem_text, role=self.profile,cause_by=type(todo))return msg
2. 主函数
async def main(topic: str, n_round=3):classroom.add_roles([Student(), Teacher()])classroom.publish_message(Message(role="Human", content=topic, cause_by=UserRequirement,send_to='' or MESSAGE_ROUTE_TO_ALL),peekable=False,)while n_round > 0:# self._save()n_round -= 1logger.debug(f"max {n_round=} left.")await classroom.run()return classroom.historyawait main(topic='wirte a poem about moon')
2024-12-15 16:53:21.726 | INFO | __main__:_act:63 - xiaoming(Student): ready to WritePoemGlimmering in the velvet night,
The moon ascends, a silver knight.
Its glow, a gentle, silken light,
Adorns the world with quiet might.A crescent curve, a silver coin,
It dances in the starry line.
A beacon in the darkened sea,
Guiding ships to safe, calm sea.Its phases, a silent story,
Of waxing, waning, and the glory.
A celestial lullaby,
Whispers through the silent sky.Oh, moon, your beauty bright,
A timeless dance in the night's delight.
A silent sentinel, you stand,
Guarding dreams and silent land.2024-12-15 16:53:25.871 | WARNING | metagpt.utils.cost_manager:update_cost:49 - Model GLM-4-flash not found in TOKEN_COSTS.
2024-12-15 16:53:25.872 | INFO | __main__:_act:69 - student : Glimmering in the velvet night,
The moon ascends, a silver knight.
Its glow, a gentle, silken light,
Adorns the world with quiet might.A crescent curve, a silver coin,
It dances in the starry line.
A beacon in the darkened sea,
Guiding ships to safe, calm sea.Its phases, a silent story,
Of waxing, waning, and the glory.
A celestial lullaby,
Whispers through the silent sky.Oh, moon, your beauty bright,
A timeless dance in the night's delight.
A silent sentinel, you stand,
Guarding dreams and silent land.
2024-12-15 16:53:25.873 | INFO | __main__:_act:86 - laowang(Teacher): ready to ReviewPoem- The opening line sets a beautiful scene, but consider a more poetic turn of phrase to enhance the velvet night's description.
- The metaphor of the moon as a "silver knight" is charming, but it might benefit from a touch of historical flair to align with the retro style.
- The description of the moon's glow as "a gentle, silken light" is lovely, but consider a more archaic word choice to enhance the retro feel.
- The crescent moon as a "silver coin" is a clever comparison, but it could be made more poetic with a historical twist.
- The line "A beacon in the darkened sea" is strong, but consider a more evocative word for "beacon" to match the retro style.
- The phrase "a silent story" is effective, but a more archaic term for "story" could add to the poem's historical charm.
- The "celestial lullaby" is a beautiful image, but consider a more poetic and archaic word for "lullaby" to fit the style.
- The final couplet is powerful, but the word "silent sentinel" could be made more poetic and retro2024-12-15 16:53:30.841 | WARNING | metagpt.utils.cost_manager:update_cost:49 - Model GLM-4-flash not found in TOKEN_COSTS.
2024-12-15 16:53:30.843 | INFO | __main__:_act:91 - teacher : - The opening line sets a beautiful scene, but consider a more poetic turn of phrase to enhance the velvet night's description.
- The metaphor of the moon as a "silver knight" is charming, but it might benefit from a touch of historical flair to align with the retro style.
- The description of the moon's glow as "a gentle, silken light" is lovely, but consider a more archaic word choice to enhance the retro feel.
- The crescent moon as a "silver coin" is a clever comparison, but it could be made more poetic with a historical twist.
- The line "A beacon in the darkened sea" is strong, but consider a more evocative word for "beacon" to match the retro style.
- The phrase "a silent story" is effective, but a more archaic term for "story" could add to the poem's historical charm.
- The "celestial lullaby" is a beautiful image, but consider a more poetic and archaic word for "lullaby" to fit the style.
- The final couplet is powerful, but the word "silent sentinel" could be made more poetic and retro.
2024-12-15 16:53:30.845 | INFO | __main__:_act:63 - xiaoming(Student): ready to WritePoem.
Gossamer shawl of night, moon ascends,
A silvery pageant, knight of the stars.
Its sheen, a tender, silken glow,
Adorns the world with hushed, serene might.A crescent crescent, silvered coin,
It pirouettes in the celestial line.
A lighthouse in the shadowed sea,
Guiding mariners to tranquil, calm sea.Its cycles, a hushed, ancient tale,
Of waxing, waning, and celestial grace.
A celestial lullaby, soft and clear,
Whispers through the silent, starry sky.Oh, moon, your radiance bright,
A timeless ballet in night's enchantment.
A silent sentinel, you stand,
Guarding2024-12-15 16:53:34.630 | WARNING | metagpt.utils.cost_manager:update_cost:49 - Model GLM-4-flash not found in TOKEN_COSTS.
2024-12-15 16:53:34.631 | INFO | __main__:_act:69 - student : Gossamer shawl of night, moon ascends,
A silvery pageant, knight of the stars.
Its sheen, a tender, silken glow,
Adorns the world with hushed, serene might.A crescent crescent, silvered coin,
It pirouettes in the celestial line.
A lighthouse in the shadowed sea,
Guiding mariners to tranquil, calm sea.Its cycles, a hushed, ancient tale,
Of waxing, waning, and celestial grace.
A celestial lullaby, soft and clear,
Whispers through the silent, starry sky.Oh, moon, your radiance bright,
A timeless ballet in night's enchantment.
A silent sentinel, you stand,
Guarding dreams and tranquil land.dreams and tranquil land.'\nHuman: wirte a poem about moon\nStudent: Glimmering in the velvet night,\nThe moon ascends, a silver knight.\nIts glow, a gentle, silken light,\nAdorns the world with quiet might.\n\nA crescent curve, a silver coin,\nIt dances in the starry line.\nA beacon in the darkened sea,\nGuiding ships to safe, calm sea.\n\nIts phases, a silent story,\nOf waxing, waning, and the glory.\nA celestial lullaby,\nWhispers through the silent sky.\n\nOh, moon, your beauty bright,\nA timeless dance in the night\'s delight.\nA silent sentinel, you stand,\nGuarding dreams and silent land.\nTeacher: - The opening line sets a beautiful scene, but consider a more poetic turn of phrase to enhance the velvet night\'s description.\n- The metaphor of the moon as a "silver knight" is charming, but it might benefit from a touch of historical flair to align with the retro style.\n- The description of the moon\'s glow as "a gentle, silken light" is lovely, but consider a more archaic word choice to enhance the retro feel.\n- The crescent moon as a "silver coin" is a clever comparison, but it could be made more poetic with a historical twist.\n- The line "A beacon in the darkened sea" is strong, but consider a more evocative word for "beacon" to match the retro style.\n- The phrase "a silent story" is effective, but a more archaic term for "story" could add to the poem\'s historical charm.\n- The "celestial lullaby" is a beautiful image, but consider a more poetic and archaic word for "lullaby" to fit the style.\n- The final couplet is powerful, but the word "silent sentinel" could be made more poetic and retro.\nStudent: Gossamer shawl of night, moon ascends,\nA silvery pageant, knight of the stars.\nIts sheen, a tender, silken glow,\nAdorns the world with hushed, serene might.\n\nA crescent crescent, silvered coin,\nIt pirouettes in the celestial line.\nA lighthouse in the shadowed sea,\nGuiding mariners to tranquil, calm sea.\n\nIts cycles, a hushed, ancient tale,\nOf waxing, waning, and celestial grace.\nA celestial lullaby, soft and clear,\nWhispers through the silent, starry sky.\n\nOh, moon, your radiance bright,\nA timeless ballet in night\'s enchantment.\nA silent sentinel, you stand,\nGuarding dreams and tranquil land.'
代码解释
代码模拟了一个简单的对话环境,其中包括一个学生(Student)和一个老师(Teacher),他们在这个环境中交互,目的是创作和评审一首关于特定主题的诗歌。以下是代码的主要组成部分和它们的功能:
1. 导入模块:
- asyncio:用于编写并发代码。
- metagpt.actions、metagpt.logs、metagpt.roles、metagpt.schema、metagpt.environment、metagpt.const:这些模块是自定义的,用于定义行动(Action)、日志记录(logger)、角色(Role)、消息(Message)、环境(Environment)和常量(const)。
2. 环境设置:
- classroom = Environment():创建一个对话环境的实例。
3. 定义行动(Action):
- WritePoem:一个行动,用于生成诗歌。
- ReviewPoem:一个行动,用于评审诗歌并给出修改建议。
4. 定义角色(Role):
- Student:学生角色,可以执行WritePoem行动,并监听UserRequirement和ReviewPoem。
- Teacher:老师角色,可以执行ReviewPoem行动,并监听WritePoem。
5. 学生和老师的行为:
- Student的_act方法:学生根据历史对话记录生成一首诗。
- Teacher的_act方法:老师根据历史对话记录评审诗歌并给出建议。
6. 主函数(main):
main函数接受一个主题(topic)和轮数(n_round),在对话环境中添加学生和老师角色,发布一个由人类提出的主题消息,然后运行对话环境n_round次。
7. 运行主函数:
await main(topic=‘wirte a poem about moon’):这是程序的入口点,它启动了对话环境,并设置主题为“wirte a poem about moon”。(拼写错误,应该是"write a poem about moon")。
参考链接:https://deepwisdom.feishu.cn/wiki/MLILw0EdRiyiYRkJLgOcskyAnUh
相关文章:
metagpt 多智能体系统
metagpt 多智能体系统 代码1. 动作及角色定义2. 主函数 代码解释1. 导入模块:2. 环境设置:3. 定义行动(Action):4. 定义角色(Role):5. 学生和老师的行为:6. 主函数&#…...
下采样在点云处理中的关键作用——以PointNet++为例【初学者无门槛理解版!】
一、前言 随着3D传感器技术的快速发展,点云数据在计算机视觉、机器人导航、自动驾驶等领域中的应用日益广泛。点云作为一种高效的3D数据表示方式,能够精确地描述物体的几何形状和空间分布。然而,点云数据通常具有高维度和稀疏性的特点&#…...
pytorch ---- torch.linalg.norm()函数
torch.linalg.norm 是 PyTorch 中用于计算张量范数(Norm)的函数。范数是线性代数中的一个重要概念,用于量化向量或矩阵的大小或长度。这个函数可以处理任意形状的张量,支持多种类型的范数计算。 1.函数签名 torch.linalg.norm(…...
系列1:基于Centos-8.6部署Kubernetes (1.24-1.30)
每日禅语 “木末芙蓉花,山中发红萼,涧户寂无人,纷纷开自落。”这是王维的一首诗,名叫《辛夷坞》。这首诗写的是在辛夷坞这个幽深的山谷里,辛夷花自开自落,平淡得很,既没有生的喜悦ÿ…...
spring学习(spring-bean实例化(无参构造与有参构造方法实现)详解)
目录 一、spring容器之bean的实例化。 (1)"bean"基本概念。 (2)spring-bean实例化的几种方式。 二、spring容器使用"构造方法"的方式实例化bean。 (1)无参构造方法实例化bean。 &#…...
Arm Cortex-M处理器对比表
Arm Cortex-M处理器对比表 当前MCU处理器上主要流行RISC-V和ARM处理器,其他的内核相对比较少;在这两种内核中,又以Arm Cortex-M生态环境相对健全,大部分的厂家都在使用ARM的处理器。本文主要介绍Arm Cortex-M各个不同系列的参数对…...
【git、gerrit】特性分支合入主分支方法 git rebase 、git cherry-pick、git merge
文章目录 1. 场景描述1.1 分支状态 2. 推荐的操作方式方法 1:git merge(保留分支结构)方法 2:git rebase(线性合并提交历史)直接在master分支执行git merge br_feature,再 执行 git pull --reba…...
WPF 相比 winform 的优势
wpf 相比 winform 的一些优点,网上也是众说纷纭,总的来说包括下面几点: 丰富的视觉效果:能够创建更具吸引力和现代化的用户界面,支持更复杂的图形和动画效果。不需要像 winform 一样,稍微做一点效果&#x…...
PYQT5程序框架
pyqt5程序框架_哔哩哔哩_bilibili 1.UI代码 Qhkuja.py # -*- coding: utf-8 -*-# Form implementation generated from reading ui file Qhkuja.ui # # Created by: PyQt5 UI code generator 5.15.7 # # WARNING: Any manual changes made to this file will be lost when py…...
Linux 中的 mkdir 命令:深入解析
在 Linux 系统中,mkdir 命令用于创建目录。它是文件系统管理中最基础的命令之一,广泛应用于日常操作和系统管理中。本文将深入探讨 mkdir 命令的功能、使用场景、高级技巧,并结合 GNU Coreutils 的源码进行详细分析。 1. mkdir 命令的基本用法…...
【人工智能解读】神经网络(CNN)的特点及其应用场景器学习(Machine Learning, ML)的基本概念
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默, 忍不住分享一下给大家。点击跳转到网站 学习总结 1、掌握 JAVA入门到进阶知识(持续写作中……) 2、学会Oracle数据库入门到入土用法(创作中……) 3、手把…...
Linux栈帧
相关寄存器&指令 寄存器 rax(accumulator):return value rbx(base) rcx(count):4st argument rdx(data):3st argument rsi(sour…...
leetcode刷题——回溯算法(1)
目录 77题. 组合 216.组合总和III 17.电话号码的字母组合 39. 组合总和 40.组合总和II 131.分割回文串 93.复原IP地址 78.子集 90.子集II 491.非递减子序列 46.全排列 47.全排列 II 332.重新安排行程 51. N皇后 37. 解数独 回溯的本质是穷举,穷举所有…...
3D相框案例讲解(详细)
前言 通过现阶段的学习,我们已经掌握了HTML,CSS和JS部分的相关知识点,现在让我们通过一篇案例,来巩固我们近期所学的知识点。 详细视频讲解戳这里 任务一 了解目标案例样式 1.1了解案例 3D相框 1.2 分析案例 首先我们看到一个…...
制作安装包
使用打包工具(如 NSIS、Inno Setup、Advanced Installer)制作安装包。 示例:Inno Setup 制作安装包 Inno Setup Inno Setup 是一个免费且强大的安装包制作工具,可以用来打包 Qt 项目或其他软件程序。以下是使用 Inno Setup 制作…...
P8615 拼接平方数 P8699 排列数
文章目录 [蓝桥杯 2014 国 C] 拼接平方数[蓝桥杯 2019 国 B] 排列数 [蓝桥杯 2014 国 C] 拼接平方数 题目描述 小明发现 49 49 49 很有趣,首先,它是个平方数。它可以拆分为 4 4 4 和 9 9 9,拆分出来的部分也是平方数。 169 169 169 也有…...
【C语言】拆解C语言的编译过程
前言 学习C语言的过程中,涉及到各种各样的关键词,在我们点击编译的时候,都会做什么呢?让我们来拆解一下 C语言的编译过程 C语言的编译过程包括预处理、编译、汇编和链接四个主要步骤。每个步骤都有其特定的任务和输出文件类型&am…...
【C++】青蛙跳跃问题解析与解法
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] 本文专栏: C 文章目录 💯前言💯题目描述第一部分:基本青蛙过河问题第二部分:石柱和荷叶问题 💯解题思路与分析第一部分:青蛙过河问题解法思路:递…...
自动驾驶AVM环视算法--python版本的俯视TOP投影模式
c语言版本和算法原理的可以查看本人的其他文档。《自动驾驶AVM环视算法--全景的俯视图像和原图》本文档进用于展示部分代码的视线,获取方式网盘自行获取(非免费介意勿下载):链接: https://pan.baidu.com/s/1MJa8ZCEfNyLc5x0uVegto…...
Go 语言与时间拳击理论下的结对编程:开启高效研发编程之旅
一、引言 结对编程作为一种软件开发方法,在提高代码质量、增强团队协作等方面具有显著优势。而时间拳击理论为结对编程带来了新的思考角度。本文将以 Go 语言为中心,深入探讨时间拳击理论下的结对编程。 在当今软件开发领域,高效的开发方法和…...
YOLOv8与YOLOv11网络结构对比:从yolov8.yaml到yolo11.yaml的演进与优化
YOLOv8与YOLOv11网络结构深度对比:从架构设计到性能优化 在计算机视觉领域,目标检测技术一直是研究热点,而YOLO(You Only Look Once)系列作为其中的佼佼者,以其高效的实时检测能力广受关注。本文将深入剖析YOLOv8与YOLOv11的网络结…...
Beyond ChatGPT: Building Physical World AI with PaLM-E and VoxPoser (Hands-on Guide)
从语言模型到物理世界操作:PaLM-E与VoxPoser实战指南 当ChatGPT在对话中展现出惊人的语言理解能力时,一个更激动人心的问题浮现:如何让AI系统突破虚拟界限,在物理世界中执行复杂任务?这正是PaLM-E与VoxPoser这类多模态…...
线性结构之链表[基于郝斌课程]
每个结点只有一个前续结点每个结点只有一个后续结点首结点没有前续结点尾结点没有后续结点专业术语:首结点:第一个有效结点,存放第一个有效数据尾结点:最后一个有效结点,存放最后一个有效数据头结点:在首结…...
FLAC3D 6.0 和 7.0 版本输出塑形区体积及破坏区域体积那些事儿
FLAC3D输出塑形区体积,适用于6.0和7.0版本,输出剪切破坏区域,张拉破坏区域体积,如图2中所示在岩土工程数值模拟领域,FLAC3D 是一款相当强大的工具。今天咱就聊聊如何在 FLAC3D 6.0 和 7.0 版本中输出塑形区体积&#x…...
30个核心概念一次讲明白,小白也能轻松入门大模型(收藏版)
这几年,AI 几乎成了人人都在谈的话题。 有人在聊大模型,有人在说智能体,有人担心算力不够,也有人被“参数”、“微调”、“多模态”、“RAG”这些词绕得头晕。 结果就是:听了很多,越听越乱。 这篇文章是用尽…...
Qwen3-14B日志分析教程:ELK栈收集推理请求、响应、错误全链路追踪
Qwen3-14B日志分析教程:ELK栈收集推理请求、响应、错误全链路追踪 1. 为什么需要日志分析 当你在私有化部署Qwen3-14B模型时,可能会遇到各种问题:为什么推理速度突然变慢了?为什么API返回了错误响应?哪些请求消耗了最…...
AI Token Platform - AI Token 中转计费平台
AI Token Platform - AI Token 中转计费平台 AI Token Platform 是一款企业级 AI Token 中转与计费平台,深度融合 多模型 AI 网关、Kill Bill 计费引擎 与 企业级会员管理 三大核心能力。平台以"统一 API 接入 灵活计费策略 企业级会员体系"为核心理念…...
GLM-4.1V-9B-Base应用场景:建筑图纸关键结构识别与中文描述生成
GLM-4.1V-9B-Base应用场景:建筑图纸关键结构识别与中文描述生成 1. 建筑行业的AI视觉革命 在建筑设计领域,图纸解读一直是项耗时费力的工作。设计师需要花费大量时间分析图纸中的结构细节,撰写技术说明文档。传统的人工识别方式不仅效率低下…...
Python智能内存回收实战:3种GC策略对比+4个生产级调优参数配置(附压测数据)
第一章:Python智能体内存管理策略生产环境部署在高并发、长生命周期的Python智能体服务中,内存管理直接影响系统稳定性与响应延迟。默认的CPython引用计数循环垃圾回收(GC)机制在动态对象频繁创建销毁的场景下易引发内存抖动和不可…...
电力系统输电线路距离保护建模与仿真:方向阻抗继电器探秘
1.电力系统输电线路距离保护的建模与仿真matlab/simulink仿真模型 2.方向阻抗继电器 (1)“0度接线”方向阻抗继电器的构造 (2)“相电压和具有K3I0补偿的相电流接线”的方向阻抗继电器模块的构造在电力系统中,输电线路距…...
