【AI Agent系列】【阿里AgentScope框架】2. Pipeline模块入门:使用Pipeline模块实现最简单的多智能体交互

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

本站文章一览:

在这里插入图片描述


上篇文章(【AI Agent系列】【阿里AgentScope框架】1. 深入源码:详细解读AgentScope中的智能体定义以及模型配置的流程)我们深入学习了AgentScope框架中的agent模块,并在最开始的时候创建了两个智能体实例,并运行了智能体实例得到了结果。

今天我们在之前代码的基础上,稍微修改一下,引入AgentScope框架的Pipeline模块,实现一个最简单的多智能体交互流程,以此来入门AgentScope中的Pipeline模块。

0. 之前的代码

代码详解请看我之前的文章:【AI Agent系列】【阿里AgentScope框架】0. 快速上手:AgentScope框架简介与你的第一个AgentScope程序

import agentscope
import os

openai_api_key = os.getenv('OPENAI_API_KEY')

# 一次性初始化多个模型配置
openai_cfg_dict = {
    "config_name": "openai_cfg", # A unique name for the model config.
    "model_type": "openai",         # Choose from "openai", "openai_dall_e", or "openai_embedding".

    "model_name": "gpt-3.5-turbo",   # The model identifier used in the OpenAI API, such as "gpt-3.5-turbo", "gpt-4", or "text-embedding-ada-002".
    "api_key": openai_api_key,       # Your OpenAI API key. If unset, the environment variable OPENAI_API_KEY is used.
}

agentscope.init(model_configs=[openai_cfg_dict])

from agentscope.agents import DialogAgent, UserAgent

# 创建一个对话智能体和一个用户智能体
dialogAgent = DialogAgent(name="assistant", model_config_name="openai_cfg", sys_prompt="You are a helpful ai assistant")
userAgent = UserAgent()

x = None
x = dialogAgent(x)
print("diaglogAgent: \n", x)
x = userAgent(x)
print("userAgent: \n", x)

这个代码的最后:

x = None
x = dialogAgent(x)
print("diaglogAgent: \n", x)
x = userAgent(x)
print("userAgent: \n", x)

我们简单的使用了一下这两个智能体实例,这其中也让这两个智能体之间有了一点交互:dialogAgent 的回复传给了 userAgent。但是 userAgent 并没有给 dialogAgent 发送消息,

1. 加入智能体之间的交互

为了使以上两个智能体间产生交互,能相互对话,我们可以这样改:

x = None
while True:
    x = dialogAgent(x)
    x = userAgent(x)

    # 如果用户输入"exit",则终止对话
    if x.content == "exit":
        print("Exiting the conversation.")
        break

加入一个while循环,让dialogAgent的结果给userAgentuserAgent的结果给dialogAgent

那根据上篇文章中我们看的智能体的实现,可以知道这个x传递给了智能体,就相当于加到了这个智能体的memory中:这样就有对方和自己的信息在上下文中了。

if self.memory:
    self.memory.add(x)

2. 使用Pipeline模块实现智能体间的交互

2.1 Pipeline是什么? - 个人简单理解

AgentScope为了方便大家对智能体间交互逻辑的编排,特地封装了 Pipeline 模块,其中包含了一系列地 Pipeline ,就像编程语言中的控制结构:顺序结构、条件分支、循环结构等。利用这些 Pipeline ,大家可以很方便地实现多智能体间的交互逻辑控制。

2.2 上手使用 Pipeline

引入 Pipeline ,改写以上代码为:

from agentscope.pipelines.functional import sequentialpipeline

# 在Pipeline结构中执行对话循环
x = None
while x is None or x.content != "exit":
	x = sequentialpipeline([dialogAgent, userAgent], x)

这里利用了 sequentialpipeline,这个Pipeline是让智能体间的信息流顺序地传递。利用 sequentialpipeline 就轻松实现了两个智能体间的信息交互。

2.3 sequentialpipeline 源码阅读

sequentialpipeline 实现源码如下:

def sequentialpipeline(
    operators: Sequence[Operator],
    x: Optional[dict] = None,
) -> dict:
    """Functional version of SequentialPipeline.

    Args:
        operators (`Sequence[Operator]`):
            Participating operators.
        x (`Optional[dict]`, defaults to `None`):
            The input dictionary.

    Returns:
        `dict`: the output dictionary.
    """
    if len(operators) == 0:
        raise ValueError("No operators provided.")

    msg = operators[0](x)
    for operator in operators[1:]:
        msg = operator(msg)
    return msg

接收两个参数:

  • operators:按顺序排列好的 agent 列表

  • x:给智能体的输入信息,可选,没有输入,那多个智能体间就是自己玩儿自己的了

然后里面的实现逻辑,其实就跟第1小节中下面的传递过程一样了:

x = dialogAgent(x)
x = userAgent(x)

3. 最终代码和实现效果

3.1 完整代码

import agentscope
# import os

# openai_api_key = os.getenv('OPENAI_API_KEY')

# 一次性初始化多个模型配置
openai_cfg_dict = {
    "config_name": "openai_cfg", # A unique name for the model config.
    "model_type": "openai",         # Choose from "openai", "openai_dall_e", or "openai_embedding".

    "model_name": "gpt-3.5-turbo",   # The model identifier used in the OpenAI API, such as "gpt-3.5-turbo", "gpt-4", or "text-embedding-ada-002".
    # "api_key": openai_api_key,       # Your OpenAI API key. If unset, the environment variable OPENAI_API_KEY is used.
}

agentscope.init(model_configs=[openai_cfg_dict])

from agentscope.agents import DialogAgent, UserAgent

# 创建一个对话智能体和一个用户智能体
dialogAgent = DialogAgent(name="assistant", model_config_name="openai_cfg", sys_prompt="You are a helpful ai assistant")
userAgent = UserAgent()

from agentscope.pipelines.functional import sequentialpipeline

# 在Pipeline结构中执行对话循环
x = None
while x is None or x.content != "exit":
  x = sequentialpipeline([dialogAgent, userAgent], x)

3.2 运行效果

在这里插入图片描述

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


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

本站文章一览:

在这里插入图片描述

  • 49
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

同学小张

如果觉得有帮助,欢迎给我鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值