【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 一起交流💬,一起进步💪。
  • 微信公众号也可搜【同学小张】 🙏

本站文章一览:

相关文章
|
7天前
|
人工智能 JSON 数据格式
RAG+Agent人工智能平台:RAGflow实现GraphRA知识库问答,打造极致多模态问答与AI编排流体验
【9月更文挑战第6天】RAG+Agent人工智能平台:RAGflow实现GraphRA知识库问答,打造极致多模态问答与AI编排流体验
RAG+Agent人工智能平台:RAGflow实现GraphRA知识库问答,打造极致多模态问答与AI编排流体验
|
3天前
|
人工智能 开发框架 Java
重磅发布!AI 驱动的 Java 开发框架:Spring AI Alibaba
随着生成式 AI 的快速发展,基于 AI 开发框架构建 AI 应用的诉求迅速增长,涌现出了包括 LangChain、LlamaIndex 等开发框架,但大部分框架只提供了 Python 语言的实现。但这些开发框架对于国内习惯了 Spring 开发范式的 Java 开发者而言,并非十分友好和丝滑。因此,我们基于 Spring AI 发布并快速演进 Spring AI Alibaba,通过提供一种方便的 API 抽象,帮助 Java 开发者简化 AI 应用的开发。同时,提供了完整的开源配套,包括可观测、网关、消息队列、配置中心等。
346 5
|
9天前
|
人工智能 算法 自动驾驶
用AI自动设计智能体,数学提分25.9%,远超手工设计
【9月更文挑战第18天】《智能体自动设计(ADAS)》是由不列颠哥伦比亚大学等机构的研究者们发布的一篇关于自动化设计智能体系统的最新论文。研究中提出了一种创新算法——“Meta Agent Search”,此算法通过迭代生成并优化智能体设计,从而实现更高效的智能体系统构建。实验表明,相比人工设计的智能体,Meta Agent Search生成的智能体在多个领域均有显著的性能提升。然而,该方法也面临着实际应用中的有效性与鲁棒性等挑战。论文详细内容及实验结果可于以下链接查阅:https://arxiv.org/pdf/2408.08435。
41 12
|
1天前
|
人工智能 自然语言处理 API
深入浅出 LangChain 与智能 Agent:构建下一代 AI 助手
我们小时候都玩过乐高积木。通过堆砌各种颜色和形状的积木,我们可以构建出城堡、飞机、甚至整个城市。现在,想象一下如果有一个数字世界的乐高,我们可以用这样的“积木”来构建智能程序,这些程序能够阅读、理解和撰写文本,甚至与我们对话。这就是大型语言模型(LLM)能够做到的,比如 GPT-4,它就像是一套庞大的乐高积木套装,等待我们来发掘和搭建。
|
1天前
|
存储 人工智能 算法
AI伦理学:建立可信的智能系统框架
【9月更文挑战第26天】随着AI技术的迅猛发展,其在各领域的应用日益广泛,但也带来了算法偏见、数据隐私泄露、就业替代等伦理和法律挑战。本文探讨AI伦理学的核心议题,包括数据隐私保护、算法公平性与透明度、机器决策责任归属及对就业市场的影响,并提出建立可信智能系统框架的建议,如强化法律法规、技术创新、建立监督机制、行业自律和公众教育,以确保AI技术的可持续发展和社会接受。
|
13天前
|
消息中间件 人工智能 运维
|
18天前
|
人工智能 开发框架 搜索推荐
移动应用开发的未来:跨平台框架与AI的融合
在移动互联网飞速发展的今天,移动应用开发已成为技术革新的前沿阵地。本文将探讨跨平台框架的兴起,以及人工智能技术如何与移动应用开发相结合,从而引领行业走向更加智能化、高效化的未来。文章通过分析当前流行的跨平台开发工具和AI技术的应用实例,为读者提供对未来移动应用开发的独到见解和预测。
40 3
|
17天前
|
存储 机器学习/深度学习 人工智能
深入浅出 AI 智能体(AI Agent)|技术干货
随着人工智能技术的发展,智能体(AI Agents)逐渐成为人与大模型交互的主要方式。智能体能执行任务、解决问题,并提供个性化服务。其关键组成部分包括规划、记忆和工具使用,使交互更加高效、自然。智能体的应用涵盖专业领域问答、资讯整理、角色扮演等场景,极大地提升了用户体验与工作效率。借助智能体开发平台,用户可以轻松打造定制化AI应用,推动AI技术在各领域的广泛应用与深度融合。
221 0
|
1月前
|
存储 人工智能
|
23天前
|
人工智能 JSON 自然语言处理
你的Agent稳定吗?——基于大模型的AI工程实践思考
本文总结了作者在盒马智能客服的落地场景下的一些思考,从工程的角度阐述对Agent应用重要的稳定性因素和一些解法。