《MetaGPT智能体开发入门》学习笔记 第五章 多智能体

需求:两个智能体,智能体一和智能体二
智能体一可以写文章,然后更具智能体二的建议进行修改;
智能体二可以看智能体一的输出并且给出智能体一的内容修改建议;

解决:
MetaGPT有watch观察方法作为外部数据进行思考和action具体代码如下
智能体一的角色和action定义

import asyncio

from metagpt.actions import Action, UserRequirement
from metagpt.logs import logger
from metagpt.roles import Role
from metagpt.schema import Message
from metagpt.environment import Environment

from metagpt.const import MESSAGE_ROUTE_TO_ALL

classroom = Environment()
# 学生写诗词
class WritePoem(Action):

    name: str = "WritePoem"

    PROMPT_TEMPLATE: str = """
    Here is the historical conversation record : {msg} .
    写一篇有human提供主题的100字发言稿,只返回生成的文稿不返回其他文本。如果老师提供了关于发言稿的修改建议,根据建议修改并返回。
    language: chinese
    your poem:
    """

    async def run(self, msg: str):
        prompt = self.PROMPT_TEMPLATE.format(msg = msg)
        rsp = await self._aask(prompt)
        return rsp

# 定义学生角色 使用学生的action并且input老师的检查反馈
class Student(Role):
    name: str = "xiaoming"
    profile: str = "Student"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # self._init_actions([WritePoem])
        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.todo
        msg = 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 msg

智能体二的角色和action定义


# 老师检查学生写的诗词
class ReviewPoem(Action):

    name: str = "ReviewPoem"
    PROMPT_TEMPLATE: str = """

    Here is the historical conversation record : {msg} .
    检查学生创作关于human提供主题的文稿,并提出修改建议。您更喜欢表达优雅 引经据典 语句通顺连贯的内容。
    language: chinese
    your comments:
    """
    async def run(self, msg: str):
        prompt = self.PROMPT_TEMPLATE.format(msg = msg)
        rsp = await self._aask(prompt)
        return rsp

# 定义老师角色并且观察也就是input学生写的诗词
class Teacher(Role):

    name: str = "laowang"
    profile: str = "Teacher"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # self._init_actions([ReviewPoem])
        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.todo
        msg = 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

MAIN程序入口:


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 -= 1
        logger.debug(f"max {n_round=} left.")
        await classroom.run()
    return classroom.history

asyncio.run(main(topic='写一个关于光伏性能源的200字文稿'))

执行结果:
执行结果

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在人工智能研究中,状态空间法是一种基于解空间的问题表示和求解方法。该方法通过在可能的解空间内寻找一个解来求解问题。状态空间法以状态和算符为基础来表示和求解问题。在传统人工智能问题中,复杂的求解技术都离不开表示与搜索这两个方面的内容,其中状态空间表示是其中的一个重要概念。状态空间法采用试探搜索方法,在某个可能的解空间内寻找一个解来求解问题。图解人工智能第二章学习笔记中可能会涉及到状态空间法的概念和问题状态描述。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [人工智能及其应用——第二章学习笔记(上)](https://blog.csdn.net/JallinRicher/article/details/122752897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [《人工智能及其应用》课程笔记(二)第2章 知识表示方法](https://blog.csdn.net/qq_46485137/article/details/122101559)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值