Repository navigation
Expand file tree
/
Copy pathagent_teams_example.py
More file actions
381 lines (306 loc) · 11.2 KB
/
Copy pathagent_teams_example.py
File metadata and controls
381 lines (306 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Anthropic Claude Opus 4.6 Agent Teams API 使用示例
Agent Teams 功能允许同时协调多个 AI 代理并行工作,
每个代理负责任务的不同部分并直接相互协调。
注意: 此功能目前处于 Research Preview 阶段,需要 API 访问权限
"""
import asyncio
import json
from typing import List, Dict, Any, Optional
from dataclasses import dataclass
from enum import Enum
class AgentRole(Enum):
"""代理角色定义"""
ARCHITECT = "architect" # 系统架构师
DEVELOPER = "developer" # 代码开发者
REVIEWER = "reviewer" # 代码审查员
TESTER = "tester" # 测试工程师
DOCUMENTER = "documenter" # 文档编写者
OPTIMIZER = "optimizer" # 性能优化师
@dataclass
class Agent:
"""单个代理定义"""
id: str
name: str
role: AgentRole
model: str = "claude-opus-4-6"
system_prompt: str = ""
tools: List[str] = None
@dataclass
class SubTask:
"""子任务定义"""
id: str
agent_id: str
description: str
dependencies: List[str] = None # 依赖的其他任务ID
status: str = "pending" # pending, running, completed, failed
result: Any = None
class AgentTeam:
"""
Agent Team 协调器
管理多个代理并行工作
"""
def __init__(self, team_name: str):
self.team_name = team_name
self.agents: Dict[str, Agent] = {}
self.tasks: Dict[str, SubTask] = {}
self.message_bus: List[Dict] = [] # 代理间通信总线
def add_agent(self, agent: Agent):
"""添加代理到团队"""
self.agents[agent.id] = agent
print(f"[Team] 添加代理: {agent.name} ({agent.role.value})")
def create_task(self, agent_id: str, description: str, dependencies: List[str] = None) -> str:
"""创建子任务"""
task_id = f"task_{len(self.tasks)}"
task = SubTask(
id=task_id,
agent_id=agent_id,
description=description,
dependencies=dependencies or []
)
self.tasks[task_id] = task
return task_id
async def run_parallel(self, main_task: str) -> Dict[str, Any]:
"""
并行执行所有任务
这是 Agent Teams 的核心功能 - 多个代理同时工作
"""
print(f"\n{'='*60}")
print(f"🚀 Agent Team: {self.team_name}")
print(f"📝 主任务: {main_task}")
print(f"👥 团队成员: {len(self.agents)} 个代理")
print(f"📋 子任务: {len(self.tasks)} 个")
print(f"{'='*60}\n")
# 实际使用 Anthropic API 的代码结构
# 注意: 这是伪代码,展示 API 调用方式
results = {}
# 1. 分析任务依赖关系
execution_order = self._resolve_dependencies()
# 2. 并行执行(无依赖的任务同时运行)
for batch in execution_order:
batch_tasks = []
for task_id in batch:
task = self.tasks[task_id]
agent = self.agents[task.agent_id]
batch_tasks.append(self._execute_task(agent, task))
# 并行运行
batch_results = await asyncio.gather(*batch_tasks, return_exceptions=True)
for task_id, result in zip(batch, batch_results):
results[task_id] = result
if isinstance(result, Exception):
print(f"❌ 任务 {task_id} 失败: {result}")
else:
print(f"✅ 任务 {task_id} 完成")
return results
def _resolve_dependencies(self) -> List[List[str]]:
"""解析任务依赖,返回可并行执行的批次"""
# 简化的依赖解析
completed = set()
pending = set(self.tasks.keys())
batches = []
while pending:
batch = []
for task_id in pending:
task = self.tasks[task_id]
if all(dep in completed for dep in task.dependencies):
batch.append(task_id)
if not batch:
raise ValueError("循环依赖 detected")
batches.append(batch)
completed.update(batch)
pending -= set(batch)
return batches
async def _execute_task(self, agent: Agent, task: SubTask) -> Any:
"""执行单个任务(实际调用 Anthropic API)"""
print(f"🤖 {agent.name} 开始工作: {task.description[:50]}...")
# 这里会调用实际的 Anthropic API
# client.beta.agent_teams.create(...) 或类似接口
# 模拟执行时间
await asyncio.sleep(1)
result = f"[{agent.role.value}] 完成了: {task.description}"
task.status = "completed"
task.result = result
return result
def broadcast_message(self, from_agent: str, message: str):
"""代理间广播消息"""
self.message_bus.append({
"from": from_agent,
"message": message,
"timestamp": asyncio.get_event_loop().time()
})
print(f"📢 [{from_agent}] 广播: {message}")
class AnthropicAgentTeamsAPI:
"""
Anthropic Agent Teams API 封装
预期 API 接口(基于发布说明):
- 创建 agent team
- 分配子任务
- 并行执行
- 结果协调
"""
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://api.anthropic.com/v1"
# self.client = anthropic.Anthropic(api_key=api_key)
async def create_team(
self,
name: str,
agents: List[Agent],
coordination_mode: str = "parallel"
) -> AgentTeam:
"""
创建 Agent Team
预期 API:
POST /v1/agent-teams
{
"name": "Dev Team",
"agents": [...],
"coordination_mode": "parallel"
}
"""
team = AgentTeam(name)
for agent in agents:
team.add_agent(agent)
return team
async def execute_task(
self,
team: AgentTeam,
task: str,
context: Dict[str, Any] = None
) -> Dict[str, Any]:
"""
使用 Agent Team 执行任务
预期 API:
POST /v1/agent-teams/{team_id}/execute
{
"task": "Build a web app",
"context": {...}
}
"""
return await team.run_parallel(task)
# ==========================================
# 使用示例
# ==========================================
async def example_software_development():
"""
软件开发 Agent Team 示例
模拟一个完整的开发团队并行工作
"""
# 1. 创建 API 客户端
api = AnthropicAgentTeamsAPI(api_key="your-api-key")
# 2. 定义团队角色
agents = [
Agent(
id="arch_1",
name="架构师 Alice",
role=AgentRole.ARCHITECT,
system_prompt="你是系统架构专家,负责设计整体架构和API接口"
),
Agent(
id="dev_1",
name="开发者 Bob",
role=AgentRole.DEVELOPER,
system_prompt="你是资深开发者,负责实现核心功能"
),
Agent(
id="dev_2",
name="开发者 Carol",
role=AgentRole.DEVELOPER,
system_prompt="你是前端专家,负责UI实现"
),
Agent(
id="review_1",
name="审查员 David",
role=AgentRole.REVIEWER,
system_prompt="你是代码审查专家,负责检查代码质量"
),
Agent(
id="test_1",
name="测试员 Eve",
role=AgentRole.TESTER,
system_prompt="你是测试工程师,负责编写测试用例"
),
]
# 3. 创建团队
team = await api.create_team("Dify Workflow Team", agents)
# 4. 创建并行任务
# 架构设计(无依赖)
arch_task = team.create_task("arch_1", "设计系统架构和数据库模型")
# 后端开发(依赖架构)
backend_task = team.create_task("dev_1", "实现API后端", dependencies=[arch_task])
# 前端开发(依赖架构)
frontend_task = team.create_task("dev_2", "实现前端界面", dependencies=[arch_task])
# 代码审查(依赖前后端开发)
review_task = team.create_task("review_1", "审查代码质量", dependencies=[backend_task, frontend_task])
# 测试(依赖审查)
test_task = team.create_task("test_1", "编写并运行测试", dependencies=[review_task])
# 5. 并行执行
results = await api.execute_task(
team,
task="开发 Dify Workflow Generator Web 界面",
context={"tech_stack": "FastAPI + Vue.js", "timeline": "2 weeks"}
)
print("\n" + "="*60)
print("📊 执行结果:")
print("="*60)
for task_id, result in results.items():
print(f"\n{task_id}:")
print(f" 结果: {result}")
async def example_workflow_optimization():
"""
工作流优化 Agent Team 示例
多个专业代理并行优化不同方面
"""
api = AnthropicAgentTeamsAPI(api_key="your-api-key")
agents = [
Agent(id="perf_1", name="性能专家", role=AgentRole.OPTIMIZER),
Agent(id="cost_1", name="成本专家", role=AgentRole.OPTIMIZER),
Agent(id="sec_1", name="安全专家", role=AgentRole.REVIEWER),
Agent(id="doc_1", name="文档专家", role=AgentRole.DOCUMENTER),
]
team = await api.create_team("Optimization Team", agents)
# 并行优化任务(无依赖)
team.create_task("perf_1", "优化执行性能,减少延迟")
team.create_task("cost_1", "优化API调用成本")
team.create_task("sec_1", "安全检查,修复漏洞")
team.create_task("doc_1", "编写技术文档")
results = await api.execute_task(team, "全面优化现有工作流")
return results
# ==========================================
# 实际的 API 调用代码(当 API 可用时)
# ==========================================
async def real_api_example():
"""
使用实际 Anthropic API 的示例
(当 Agent Teams API 正式发布后)
"""
import anthropic
client = anthropic.Anthropic(api_key="your-api-key")
# 创建 agent team
# team = client.beta.agent_teams.create(
# name="Development Team",
# agents=[
# {"role": "architect", "model": "claude-opus-4-6"},
# {"role": "developer", "model": "claude-opus-4-6"},
# {"role": "tester", "model": "claude-opus-4-6"},
# ],
# coordination_mode="parallel"
# )
# 执行任务
# result = client.beta.agent_teams.execute(
# team_id=team.id,
# task="Build a REST API",
# subtasks=[
# {"agent": "architect", "description": "Design API"},
# {"agent": "developer", "description": "Implement API", "depends_on": [0]},
# {"agent": "tester", "description": "Test API", "depends_on": [1]},
# ]
# )
pass
if __name__ == "__main__":
print("🚀 Anthropic Claude Opus 4.6 Agent Teams 示例")
print("="*60)
print("\n注意: Agent Teams 功能目前处于 Research Preview 阶段")
print("需要申请 API 访问权限才能使用\n")
# 运行示例
asyncio.run(example_software_development())