Agency Swarm 开源项目教程
1. 项目介绍
Agency Swarm 是一个开源的代理编排框架,构建在最新的 OpenAI Assistants API 之上。该项目旨在简化代理创建过程,并使任何人都能创建具有不同角色和能力的协作代理群(Agencies)。通过将自动化视为现实世界中的实体,如机构和专门的代理角色,Agency Swarm 使得代理和用户的使用更加直观。
主要特点
- 可定制的代理角色:定义如 CEO、虚拟助理、开发者等角色,并使用 Assistants API 自定义其功能。
- 完全控制提示:避免预定义提示的冲突和限制,允许完全自定义。
- 工具创建:使用 Instructor 创建工具,提供便捷的接口和自动类型验证。
- 高效通信:代理通过基于自身描述的“发送消息”工具进行通信。
- 状态管理:Agency Swarm 高效管理 OpenAI 上的助手状态,维护在特殊的
settings.json
文件中。 - 可部署于生产环境:Agency Swarm 设计为可靠且易于部署在生产环境中。
2. 项目快速启动
安装
首先,使用 pip 安装 Agency Swarm:
pip install -U agency-swarm
设置 OpenAI API 密钥
from agency_swarm import set_openai_key
set_openai_key("YOUR_API_KEY")
创建工具
使用 Instructor 定义自定义工具:
from agency_swarm.tools import BaseTool
from pydantic import Field
class MyCustomTool(BaseTool):
"""A brief description of what the custom tool does."""
example_field: str = Field(description="Description of the example field.")
def run(self):
"""The implementation of the run method."""
do_something(self.example_field)
return "Result of MyCustomTool operation"
定义代理角色
from agency_swarm import Agent
ceo = Agent(
name="CEO",
description="Responsible for client communication, task planning and management",
instructions="You must converse with other agents to ensure complete task execution",
tools=[MyCustomTool],
temperature=0.5,
max_prompt_tokens=25000
)
定义机构通信流程
from agency_swarm import Agency
agency = Agency([
ceo,
[ceo, dev],
[ceo, va],
[dev, va]
], shared_instructions='agency_manifesto.md', temperature=0.5, max_prompt_tokens=25000)
运行演示
agency.run_demo()
3. 应用案例和最佳实践
案例1:自动化项目管理
通过定义 CEO 和开发者代理,Agency Swarm 可以自动化项目管理流程,从任务规划到执行,全程由 AI 代理协作完成。
案例2:客户服务自动化
创建虚拟助理代理,负责处理客户查询和问题,通过与内部开发者代理的协作,快速响应客户需求。
最佳实践
- 明确角色定义:确保每个代理的角色和职责清晰明确。
- 优化通信流程:设计高效的通信流程,减少不必要的通信开销。
- 持续监控和调整:定期监控代理的表现,并根据需要调整工具和流程。
4. 典型生态项目
Agency Swarm Lab
Agency Swarm Lab 是一个协作空间,展示使用 Agency Swarm 框架开发的定制 AI 代理团队的惊人能力。通过该实验室,用户可以探索和学习如何创建和优化自己的 AI 代理团队。
Open Source Models
Agency Swarm 支持多种开源模型,用户可以根据需求选择合适的模型进行部署和使用。
API Reference
详细的 API 参考文档,帮助开发者理解和使用 Agency Swarm 的各种功能和接口。
通过以上模块的介绍和实践,用户可以快速上手并深入使用 Agency Swarm 开源项目。