赞
踩
AgentScope是以开发者为中心的多智能体平台,它使开发者能够更轻松地构建基于大语言模型的多智能体应用程序。
相关文章/教程
大模型的出现使得开发者能够构建多样化的应用程序,为了连接大语言模型和数据以及服务,并更好地解 决复杂任务,AgentScope提供了一系列的开发工具和组件来提高开发效率。AgentScope以
为特点。
https://modelscope.github.io/agentscope/zh_CN/tutorial/101-agentscope.html
是信息的载体(例如指令、多模态数据和对话内容)。在AgentScope中,消息是Python字典的子类, 具有name
和content
作为必要字段,url
作为可选字段并指向额外的资源。
是能够与环境和其他智能体交互,并采取行动改变环境的自主实体。在AgentScope中, 智能体以消息作为输入,并生成相应的响应消息。
是使智能体能够执行特定任务的功能性API。在AgentScope中,服务分为模型API服务 (用于使用大预言模型)和通用API服务(提供各种工具函数)。
表示智能体执行和智能体之间的消息交换的有序序列,类似于TensorFlow中的计算图, 但其并不一定是DAG结构。
面向开发者的易用性。 AgentScope为开发者提供了高易用性,包括灵活易用的语法糖、即拿即用的组件和预构建的multi-agent样例。
可靠稳定的容错机制。 AgentScope确保了对多种模型和APIs的容错性,并允许开发者构建定制的容错策略。
全面兼容多模态数据。 AgentScope支持多模态数据(例如文件、图像、音频和视频)的对话展示、消息传输和数据存储。
高效分布式运行效率。 AgentScope引入了基于actor的分布式机制,使得复杂的分布式工作流的集中式编程和自动并行优化成为可能。
AgentScope由三个层次的层次结构组成。
这些层次提供了对多智能体应用程序的支持,包括单个智能体的基本和高级功能(实用程序层)、资源和运行时管理(管理器和包装层)以及智能体级到工作流级的编程接口(智能体层)。
AgentScope引入了直观的抽象,旨在满足每个层次固有的多样化功能,并简化构建多智能体系统时的复杂层间依赖关系。
此外,我们提供了编程接口和默认机制,以增强多智能体系统在不同层次上对故障的韧性。
AgentScope
├── src
│ ├── agentscope
│ | ├── agents # 与智能体相关的核心组件和实现。
│ | ├── memory # 智能体记忆相关的结构。
│ | ├── models # 用于集成不同模型API的接口。
│ | ├── pipelines # 基础组件和实现,用于运行工作流。
│ | ├── rpc # Rpc模块,用于智能体分布式部署。
│ | ├── service # 为智能体提供各种功能的服务。
| | ├── web # 基于网页的用户交互界面。
│ | ├── utils # 辅助工具和帮助函数。
│ | ├── prompt.py # 提示工程模块。
│ | ├── message.py # 智能体之间消息传递的定义和实现。
│ | ├── ... ..
│ | ├── ... ..
├── scripts # 用于启动本地模型API的脚本。
├── examples # 不同应用程序的预构建示例。
├── docs # 教程和API参考文档。
├── tests # 单元测试模块,用于持续集成。
├── LICENSE # AgentScope使用的官方许可协议。
└── setup.py # 用于安装的设置脚本。
├── ... ..
└── ... ..
AgentScope提供了一系列ModelWrapper
来支持本地模型服务和第三方模型API。
API | Task | Model Wrapper |
---|---|---|
OpenAI API | Chat | OpenAIChatWrapper |
Embedding | OpenAIEmbeddingWrapper | |
DALL·E | OpenAIDALLEWrapper | |
DashScope API | Chat | DashScopeChatWrapper |
Image Synthesis | DashScopeImageSynthesisWrapper | |
Text Embedding | DashScopeTextEmbeddingWrapper | |
Gemini API | Chat | GeminiChatWrapper |
Embedding | GeminiEmbeddingWrapper | |
ollama | Chat | OllamaChatWrapper |
Embedding | OllamaEmbeddingWrapper | |
Generation | OllamaGenerationWrapper | |
Post Request based API | - | PostAPIModelWrapper |
AgentScope支持使用以下库快速部署本地模型服务。
参见:https://github.com/modelscope/agentscope/blob/main/scripts/README.md
<https://modelscope.github.io/agentscope/zh_CN/tutorial/102-installation.html#agentscope)
<https://modelscope.github.io/agentscope/zh_CN/tutorial/102-installation.html#id3)
按照以下步骤从源代码安装AgentScope,并以可编辑模式安装AgentScope:
*注意:该项目正在积极开发中,建议从源码安装AgentScope!*
# 从GitHub上拉取AgentScope的源代码
git clone https://github.com/modelscope/agentscope.git
cd AgentScope
# 针对本地化的multi-agent应用
pip install -e .
# 为分布式multi-agent应用
pip install -e .[distribute] # 在Mac上使用`pip install -e .\[distribute\]`
注意:[distribute]
选项安装了分布式应用程序所需的额外依赖项。在运行这些命令之前,请激活您的虚拟环境。
<https://modelscope.github.io/agentscope/zh_CN/tutorial/102-installation.html#pip)
如果您选择从Pypi安装AgentScope,可以使用pip
轻松地完成:
# 针对本地化的multi-agent应用
pip install agentscope
# 为分布式multi-agent应用
pip install agentscope[distribute] # 在Mac上使用`pip install agentscope\[distribute\]`
https://modelscope.github.io/agentscope/zh_CN/tutorial/103-example.html#example-zh
AgentScope内置了灵活的通信机制。在本教程中,我们将通过一个简单的独立对话示例介绍AgentScope的基本用法。
为了更好的构建多智能体应用,AgentScope将模型的部署与调用解耦开,以API服务调用的方式支持各种不同的模型。
在模型部署方面,用户可以使用第三方模型服务,例如OpenAI API,Google Gemini API, HuggingFace/ModelScope Inference API等,或者也可以通过AgentScope仓库中的脚本快速部署本地开源模型服务,
模型调用方面,用户需要通过设定模型配置来指定模型服务。
以OpenAI Chat API为例,需要准备如下的模型配置:
model_config = {
"config_name": "{config_name}", # A unique name for the model config.
"model_type": "openai", # Choose from "openai", "openai_dall_e", or "openai_embedding".
"model_name": "{model_name}", # The model identifier used in the OpenAI API, such as "gpt-3.5-turbo", "gpt-4", or "text-embedding-ada-002".
"api_key": "xxx", # Your OpenAI API key. If unset, the environment variable OPENAI_API_KEY is used.
"organization": "xxx", # Your OpenAI organization ID. If unset, the environment variable OPENAI_ORGANIZATION is used.
}
更多关于模型调用,部署和开源模型的信息请见模型章节。
准备好模型配置后,用户可以通过调用AgentScope的初始化方法 init
函数来 注册您的配置。
此外,您还可以一次性加载多个模型配置。
import agentscope
# 一次性初始化多个模型配置
openai_cfg_dict = {
# ...
}
modelscope_cfg_dict = {
# ...
}
agentscope.init(model_configs=[openai_cfg_dict, modelscope_cfg_dict])
在初始化AgentScope时,您可以使用模型配置初始化AgentScope,然后定义每个智能体及其对应的角色和特定模型。
import agentscope
from agentscope.agents import DialogAgent, UserAgent
# 读取模型配置
agentscope.init(model_configs="./model_configs.json")
# 创建一个对话智能体和一个用户智能体
dialogAgent = DialogAgent(name="assistant", model_config_name="gpt-4", sys_prompt="You are a helpful ai assistant")
userAgent = UserAgent()
注意:请参考定制你自己的Agent以获取所有可用的智能体以及创建自定义的智能体。
消息(Message)是AgentScope中智能体之间的主要通信手段。
它是一个Python字典,包括了一些基本字段,如消息的content
和消息发送者的name
。
可选地,消息可以包括一个url
,指向本地文件(图像、视频或音频)或网站。
from agentscope.message import Msg
# 来自Alice的简单文本消息示例
message_from_alice = Msg("Alice", "Hi!")
# 来自Bob的带有附加图像的消息示例
message_from_bob = Msg("Bob", "What about this picture I took?", url="/path/to/picture.jpg")
为了在两个智能体之间开始对话,例如dialog_agent
和user_agent
,您可以使用以下循环。
对话将持续进行,直到用户输入"exit"
,这将终止交互。
x = None
while True:
x = dialogAgent(x)
x = userAgent(x)
# 如果用户输入"exit",则终止对话
if x.content == "exit":
print("Exiting the conversation.")
break
进阶的使用中,AgentScope提供了Pipeline来管理智能体之间消息流的选项。 其中sequentialpipeline
代表顺序对话,每个智能体从上一个智能体接收消息并生成其响应。
from agentscope.pipelines.functional import sequentialpipeline
# 在Pipeline结构中执行对话循环
x = None
while x is None or x.content != "exit":
x = sequentialpipeline([dialog_agent, user_agent])
有关如何使用Pipeline进行复杂的智能体交互的更多细节,请参考Pipeline和MsgHub。
2024-03-28(四)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。