【AI Agent系列】【阿里AgentScope框架】实战1:利用AgentScope实现动态创建Agent和自由组织讨论

简介: 【AI Agent系列】【阿里AgentScope框架】实战1:利用AgentScope实现动态创建Agent和自由组织讨论

大家好,我是 同学小张,持续学习C++进阶知识和AI大模型应用实战案例,持续分享,欢迎大家点赞+关注,共同学习和进步。


从实战中学习和拆解AgentScope框架的使用和知识。本文利用AgentScope框架实现的是 多智能体的自由讨论 。

代码参考:https://github.com/modelscope/agentscope/tree/main/examples/conversation_self_organizing


0. 实现效果

先上最终的实现效果,给大家一个直观的感受。本文实现的效果如下:

有多个Agent(例如案例中的 PhysicsTeacher物理老师、curious student好奇的学生、analytical student分析型学生),针对一个话题展开讨论,每个Agent轮流发言。

1. 需求拆解

要实现多智能体之间的自由讨论,需要实现以下内容:

(1)有多个对话智能体

(2)多个对话智能体之间的通信(数据流控制)

(3)本文要实现的是不固定的多智能体对话,也就是说,多智能体是动态创建的,因此有个Agent来组织讨论,例如本例中讨论的物理问题,该Agent需要根据这个物理问题创建相应的智能体(物理老师和各种学生等)

这么一看,是不是就觉得非常简单了?对话智能体(DialogAgent)和数据流控制(Pipeline)我们前面都已经深入学习过了,还不了解的可以去看我前面的AgentScope相关文章。

2. 代码实现

2.1 初始化AgentScope

AgentScope在使用前,需要先初始化配置,主要是将要使用的大模型和相关的API Key 设置一下:

import agentscope
model_configs = [
    {
        "model_type": "openai",
        "config_name": "gpt-3.5-turbo",
        "model_name": "gpt-3.5-turbo",
        # "api_key": "xxx",  # Load from env if not provided
        # "organization": "xxx",  # Load from env if not provided
        "generate_args": {
            "temperature": 0.5,
        },
    },
]
agentscope.init(model_configs=model_configs)

2.2 创建讨论的组织者

根据前面的需求分析,我们首先需要有一个Agent来根据问题动态生成讨论问题的Agent们。

这里使用一个对话智能体DialogAgent即可:

# init the self-organizing conversation
agent_builder = DialogAgent(
    name="agent_builder",
    sys_prompt="You're a helpful assistant.",
    model_config_name="gpt-3.5-turbo",
)

有了这个agent实例,可以通过传入Prompt和问题来获取参与讨论的Agents以及各Agents的设定。这里的Prompt是比较重要的,看下示例中的Prompt:

Act as a group discussion organizer. Please provide the suitable scenario for discussing this question, and list the roles of the people who need to participate in the discussion in order to answer this question, along with their system prompt to describe their characteristics.
The response must in the format of:
#scenario#: <discussion scenario>
#participants#:
* <participant1 type>: <characteristic description>
* <participant2 type>: <characteristic description>
Here are some examples.
Question: Joy can read 8 pages of a book in 20 minutes. How many hours will it take her to read 120 pages?
Answer:
#scenario#: grade school class discussion
#participants#:
* Instructor: Act as an instructor who is in a class group discussion to guide the student group discussion. Please encourage critical thinking. Encourage participants to think critically and challenge assumptions by asking thought-provoking questions or presenting different perspectives.
* broad-minded-student: Act as a student who is broad-minded and is open to trying new or different ways to solve problems. You are in a group discussion with other student under the guidance of the instructor.
* knowledgeable-student: Act as a knowledgeable student and discuss with others to retrieve more information about the topic. If you do not know the answer to a question, please do not share false information
Please give the discussion scenario and the corresponding participants for the following question:
Question: {question}
Answer:

Prompt里,要求要给出讨论的流程、讨论的参与者与讨论参与者各自的“system prompt”。

运行时,将Prompt与问题组合传给Agent:

query = "假设你眼睛的瞳孔直径为5毫米,你有一台孔径为50厘米的望远镜。望远镜能比你的眼睛多收集多少光?"
x = load_txt(
"D:\\GitHub\\LEARN_LLM\\agentscope\\start_0\\conversation_self_organizing\\agent_builder_instruct.txt",
).format(
    question=query,
)
x = Msg("user", x, role="user")
settings = agent_builder(x)

看下这个Agent的运行结果:

文字版运行结果:

tools:extract_scenario_and_participants:82 - {'Scenario': 'Physics class discussion on optics', 'Participants': {'PhysicsTeacher': 'Act as a physics teacher who is leading the discussion on optics. Your role is to facilitate the conversation, provide explanations, and ensure that the discussion stays focused on the topic of light collection and optics.', 'curious-student': 'Act as a student who is curious and eager to learn more about optics and light collection. You ask insightful questions and actively participate in the discussion to deepen your understanding.', 'analytical-student': 'Act as a student who is analytical and enjoys solving problems related to optics. You approach the question methodically and use logical reasoning to arrive at solutions.'}}

输出结果给出了 Scenario 以及该问题的 Participants参与者,参与者有 PhysicsTeacher、curious-student 和 analytical-student。并给出了这几个参与者的角色设定。

之后通过一个解析函数,将里面的角色和设定解析出来就可以用来动态创建这些Agent了。

def extract_scenario_and_participants(content: str) -> dict:
    result = {}
    # define regular expression
    scenario_pattern = r"#scenario#:\s*(.*)"
    participants_pattern = r"\*\s*([^:\n]+):\s*([^\n]+)"
    # search and extract scenario
    scenario_match = re.search(scenario_pattern, content)
    if scenario_match:
        result["Scenario"] = scenario_match.group(1).strip()
    # search and extract participants
    participants_matches = re.finditer(participants_pattern, content)
    participants_dict = {}
    for match in participants_matches:
        participant_type, characteristic = match.groups()
        participants_dict[
            participant_type.strip().replace(" ", "_")
        ] = characteristic.strip()
    result["Participants"] = participants_dict
    
    logger.info(result)
    return result
scenario_participants = extract_scenario_and_participants(settings["content"])

2.3 动态创建讨论者

有了参与者及其描述,直接用循环语句创建这些Agent:

# set the agents that participant the discussion
agents = [
    DialogAgent(
        name=key,
        sys_prompt=val,
        model_config_name="gpt-3.5-turbo",
    )
    for key, val in scenario_participants["Participants"].items()
]

2.4 开始讨论

这里用了 sequentialpipeline 顺序发言:

max_round = 2
msg = Msg("user", f"let's discuss to solve the question with chinese: {query}", role="user")
for i in range(max_round):
    msg = sequentialpipeline(agents, msg)

运行结果见文章刚开始的实现效果,实现讨论。

3. 总结

本文主要拆解了一个利用AgentScope框架实现的多智能体自由讨论案例,先由一个Agent根据问题生成讨论流程和讨论者,然后根据讨论者动态创建Agent。

主要的亮点在于:

(1)有一个Agent把控全局,生成流程和各参与者的描述

(2)动态创建讨论者Agent,这让这个系统有了更好的通用性,根据不同的问题有不同类型和不同数量的Agent会被创建。

值得借鉴。

如果觉得本文对你有帮助,麻烦点个赞和关注呗 ~~~


  • 大家好,我是 同学小张,持续学习C++进阶知识AI大模型应用实战案例
  • 欢迎 点赞 + 关注 👏,持续学习持续干货输出
  • +v: jasper_8017 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏

本站文章一览:

相关文章
|
人工智能 自然语言处理 前端开发
Director:构建视频智能体的 AI 框架,用自然语言执行搜索、编辑、合成和生成等复杂视频任务
Director 是一个构建视频智能体的 AI 框架,用户可以通过自然语言命令执行复杂的视频任务,如搜索、编辑、合成和生成视频内容。该框架基于 VideoDB 的“视频即数据”基础设施,集成了多个预构建的视频代理和 AI API,支持高度定制化,适用于开发者和创作者。
1050 9
Director:构建视频智能体的 AI 框架,用自然语言执行搜索、编辑、合成和生成等复杂视频任务
|
存储 人工智能
Optimus-1:哈工大联合鹏城实验室推出挑战开放世界中长期任务的智能体框架
Optimus-1是由哈尔滨工业大学(深圳)和鹏城实验室联合推出的智能体框架,旨在解决开放世界环境中长期任务的挑战。该框架结合了结构化知识和多模态经验,通过混合多模态记忆模块、知识引导规划器和经验驱动反射器,显著提升了在Minecraft等环境中的长期任务性能。本文将详细介绍Optimus-1的主要功能、技术原理以及如何运行该框架。
450 7
Optimus-1:哈工大联合鹏城实验室推出挑战开放世界中长期任务的智能体框架
|
人工智能 自然语言处理 JavaScript
Agent-E:基于 AutoGen 代理框架构建的 AI 浏览器自动化系统
Agent-E 是一个基于 AutoGen 代理框架构建的智能自动化系统,专注于浏览器内的自动化操作。它能够执行多种复杂任务,如填写表单、搜索和排序电商产品、定位网页内容等,从而提高在线效率,减少重复劳动。本文将详细介绍 Agent-E 的功能、技术原理以及如何运行该系统。
1294 5
Agent-E:基于 AutoGen 代理框架构建的 AI 浏览器自动化系统
|
存储 人工智能 自然语言处理
AI经营|多Agent择优生成商品标题
商品标题中关键词的好坏是商品能否被主搜检索到的关键因素,使用大模型自动优化标题成为【AI经营】中的核心能力之一,本文讲述大模型如何帮助商家优化商品素材,提升商品竞争力。
1680 62
AI经营|多Agent择优生成商品标题
|
机器学习/深度学习 人工智能 自然语言处理
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型
谷歌最新推出的Gemini 2.0是一款原生多模态输入输出的AI模型,以Agent技术为核心,支持多种数据类型的输入与输出,具备强大的性能和多语言音频输出能力。本文将详细介绍Gemini 2.0的主要功能、技术原理及其在多个领域的应用场景。
1475 20
Gemini 2.0:谷歌推出的原生多模态输入输出 + Agent 为核心的 AI 模型
|
机器学习/深度学习 人工智能 算法
Meta Motivo:Meta 推出能够控制数字智能体动作的 AI 模型,提升元宇宙互动体验的真实性
Meta Motivo 是 Meta 公司推出的 AI 模型,旨在控制数字智能体的全身动作,提升元宇宙体验的真实性。该模型通过无监督强化学习算法,能够实现零样本学习、行为模仿与生成、多任务泛化等功能,适用于机器人控制、虚拟助手、游戏角色动画等多个应用场景。
457 4
Meta Motivo:Meta 推出能够控制数字智能体动作的 AI 模型,提升元宇宙互动体验的真实性
|
人工智能 自然语言处理 数据挖掘
田渊栋团队新作祭出Agent-as-a-Judge!AI智能体自我审判,成本暴跌97%
田渊栋团队提出Agent-as-a-Judge框架,利用智能体自身评估其他智能体的性能,不仅关注最终结果,还能提供中间反馈,更全面准确地反映智能体的真实能力。该框架在DevAI基准测试中表现出色,成本效益显著,为智能体的自我改进提供了有力支持。
487 7
|
Python 机器学习/深度学习 人工智能
手把手教你从零开始构建并训练你的第一个强化学习智能体:深入浅出Agent项目实战,带你体验编程与AI结合的乐趣
【10月更文挑战第1天】本文通过构建一个简单的强化学习环境,演示了如何创建和训练智能体以完成特定任务。我们使用Python、OpenAI Gym和PyTorch搭建了一个基础的智能体,使其学会在CartPole-v1环境中保持杆子不倒。文中详细介绍了环境设置、神经网络构建及训练过程。此实战案例有助于理解智能体的工作原理及基本训练方法,为更复杂应用奠定基础。首先需安装必要库: ```bash pip install gym torch ``` 接着定义环境并与之交互,实现智能体的训练。通过多个回合的试错学习,智能体逐步优化其策略。这一过程虽从基础做起,但为后续研究提供了良好起点。
2494 4
手把手教你从零开始构建并训练你的第一个强化学习智能体:深入浅出Agent项目实战,带你体验编程与AI结合的乐趣
|
人工智能 自然语言处理 搜索推荐
🤖【多Agent大爆炸】——灵活调用与实践指南,解锁AI协作新技能!
本文深入探讨了单Agent与多Agent在不同场景下的应用及优势,通过实例讲解多Agent如何实现高效协作,涵盖智能物流、教育、医疗等多个领域的实际应用,旨在帮助开发者掌握多Agent系统的调用与实践技巧。
1528 5
|
机器学习/深度学习 传感器 人工智能
2024.11|全球具身智能的端到端AI和具身Agent技术发展到哪里了
2024年,具身智能领域取得显著进展,特别是在端到端AI控制系统和多模态感知技术方面。这些技术不仅推动了学术研究的深入,也为科技公司在实际应用中带来了突破。文章详细介绍了端到端AI的演化、自监督学习的应用、多模态感知技术的突破、基于强化学习的策略优化、模拟环境与现实环境的迁移学习、长程任务规划与任务分解、人机协作与社会交互能力,以及伦理与安全问题。未来几年,具身智能将在多模态感知、自监督学习、任务规划和人机协作等方面继续取得重要突破。
1010 2

热门文章

最新文章