-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_swarm.py
More file actions
334 lines (306 loc) · 12.9 KB
/
Copy pathagent_swarm.py
File metadata and controls
334 lines (306 loc) · 12.9 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
#!/usr/bin/env python3
"""
Agent Swarm Example for AINative Python SDK
Demonstrates how to orchestrate multiple AI agents for complex tasks.
"""
import os
import time
from ainative import AINativeClient
from ainative.agent_swarm import AgentType, SwarmStatus
def main():
# Initialize client
api_key = os.getenv("AINATIVE_API_KEY", "your-api-key-here")
client = AINativeClient(api_key=api_key)
print("🤖 AINative Agent Swarm Example\n")
try:
# Create a project for agent swarm
print("Setting up project...")
project = client.zerodb.projects.create(
name="Agent Swarm Demo",
description="Multi-agent orchestration example"
)
project_id = project["id"]
print(f"✅ Project created: {project_id}\n")
# Get available agent types
print("Available Agent Types:")
agent_types = client.agent_swarm.get_agent_types()
for agent_type in agent_types:
print(f" - {agent_type['type']}: {agent_type['description']}")
print()
# Define agent team for a software development task
print("Configuring agent team for software development...")
agents = [
{
"id": "researcher_001",
"type": AgentType.RESEARCHER.value,
"name": "Research Agent",
"capabilities": ["web_search", "documentation_analysis", "best_practices"],
"config": {
"temperature": 0.7,
"max_tokens": 2000,
"focus_areas": ["security", "performance", "scalability"]
}
},
{
"id": "coder_001",
"type": AgentType.CODER.value,
"name": "Senior Developer",
"capabilities": ["python", "javascript", "sql", "api_design"],
"config": {
"temperature": 0.5,
"max_tokens": 3000,
"code_style": "clean_code",
"languages": ["Python", "TypeScript"]
}
},
{
"id": "reviewer_001",
"type": AgentType.REVIEWER.value,
"name": "Code Reviewer",
"capabilities": ["code_review", "security_audit", "performance_analysis"],
"config": {
"temperature": 0.3,
"max_tokens": 1500,
"review_focus": ["security", "efficiency", "maintainability"]
}
},
{
"id": "tester_001",
"type": AgentType.TESTER.value,
"name": "QA Engineer",
"capabilities": ["unit_testing", "integration_testing", "test_automation"],
"config": {
"temperature": 0.4,
"max_tokens": 2000,
"testing_frameworks": ["pytest", "jest", "selenium"]
}
},
{
"id": "documenter_001",
"type": AgentType.DOCUMENTER.value,
"name": "Technical Writer",
"capabilities": ["api_documentation", "user_guides", "code_comments"],
"config": {
"temperature": 0.6,
"max_tokens": 2500,
"documentation_style": "comprehensive"
}
}
]
print(f"✅ Configured {len(agents)} agents\n")
# Start the agent swarm
print("Starting agent swarm...")
swarm = client.agent_swarm.start_swarm(
project_id=project_id,
agents=agents,
objective="Build a secure REST API for user authentication with JWT tokens",
config={
"coordination_mode": "collaborative",
"communication_protocol": "async",
"max_iterations": 10,
"timeout_minutes": 30
}
)
swarm_id = swarm["id"]
print(f"✅ Swarm started: {swarm_id}\n")
# Check swarm status
status = client.agent_swarm.get_status(swarm_id)
print(f"Swarm Status: {status['status']}")
print(f"Active Agents: {status.get('active_agents', 0)}")
print(f"Progress: {status.get('progress', 0)}%\n")
# Task 1: Research Phase
print("📚 Task 1: Research best practices for JWT authentication")
research_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Research current best practices for implementing JWT authentication in REST APIs",
context={
"requirements": [
"Security best practices",
"Token refresh strategies",
"Storage recommendations",
"Common vulnerabilities to avoid"
],
"target_framework": "FastAPI with Python"
},
agents=["researcher_001"]
)
print(f" ✅ Research completed\n")
# Task 2: Design Phase
print("📐 Task 2: Design the authentication system")
design_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Design a secure authentication system based on the research findings",
context={
"research_findings": research_result.get("output", {}),
"requirements": [
"User registration endpoint",
"Login endpoint with JWT generation",
"Token refresh endpoint",
"Password reset flow",
"Role-based access control"
]
},
agents=["coder_001", "reviewer_001"]
)
print(f" ✅ Design completed\n")
# Task 3: Implementation Phase
print("💻 Task 3: Implement the authentication endpoints")
implementation_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Implement the JWT authentication system with all specified endpoints",
context={
"design": design_result.get("output", {}),
"technology_stack": {
"framework": "FastAPI",
"database": "PostgreSQL",
"jwt_library": "python-jose",
"password_hashing": "passlib with bcrypt"
}
},
agents=["coder_001"]
)
print(f" ✅ Implementation completed\n")
# Task 4: Code Review Phase
print("🔍 Task 4: Review the implementation")
review_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Review the implemented code for security vulnerabilities and best practices",
context={
"code": implementation_result.get("output", {}),
"review_criteria": [
"Security vulnerabilities",
"Code quality",
"Performance implications",
"Error handling",
"Input validation"
]
},
agents=["reviewer_001"]
)
print(f" ✅ Code review completed\n")
# Task 5: Testing Phase
print("🧪 Task 5: Create comprehensive tests")
testing_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Create unit and integration tests for the authentication system",
context={
"implementation": implementation_result.get("output", {}),
"test_requirements": [
"Unit tests for each endpoint",
"Integration tests for auth flow",
"Edge cases and error scenarios",
"Security test cases",
"Performance tests"
]
},
agents=["tester_001"]
)
print(f" ✅ Test suite created\n")
# Task 6: Documentation Phase
print("📝 Task 6: Generate documentation")
documentation_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Create comprehensive documentation for the authentication API",
context={
"implementation": implementation_result.get("output", {}),
"documentation_requirements": [
"API endpoint documentation",
"Authentication flow diagrams",
"Setup and configuration guide",
"Security considerations",
"Example code snippets"
]
},
agents=["documenter_001"]
)
print(f" ✅ Documentation generated\n")
# Configure specific agent with custom prompts
print("Configuring agents with specialized prompts...")
client.agent_swarm.set_agent_prompt(
swarm_id=swarm_id,
agent_id="reviewer_001",
prompt="Focus on OWASP Top 10 vulnerabilities and ensure all inputs are properly validated",
system_prompt="You are a senior security engineer specializing in authentication systems"
)
print(" ✅ Agent prompts configured\n")
# Get swarm metrics
print("Performance Metrics:")
metrics = client.agent_swarm.get_metrics(swarm_id=swarm_id)
print(f" Total tasks completed: {metrics.get('tasks_completed', 0)}")
print(f" Average task duration: {metrics.get('avg_task_duration', 0):.2f}s")
print(f" Agent utilization: {metrics.get('agent_utilization', 0):.1f}%")
print(f" Token usage: {metrics.get('total_tokens', 0)}")
print()
# Get agent communications log
print("Agent Communications Summary:")
communications = client.agent_swarm.get_agent_communications(swarm_id=swarm_id)
print(f" Total messages exchanged: {len(communications)}")
if communications:
recent = communications[:3]
for comm in recent:
print(f" - {comm['from_agent']} → {comm['to_agent']}: {comm['type']}")
print()
# Get swarm history
print("Execution History:")
history = client.agent_swarm.get_swarm_history(swarm_id=swarm_id, limit=5)
for entry in history:
print(f" {entry['timestamp']}: {entry['event']} - {entry['description']}")
print()
# Pause and resume demonstration
print("Testing swarm control...")
client.agent_swarm.pause_swarm(swarm_id=swarm_id)
print(" ⏸️ Swarm paused")
time.sleep(2)
client.agent_swarm.resume_swarm(swarm_id=swarm_id)
print(" ▶️ Swarm resumed")
print()
# Final orchestration task
print("🎯 Final Task: Compile final deliverables")
final_result = client.agent_swarm.orchestrate(
swarm_id=swarm_id,
task="Compile all outputs into a final deliverable package",
context={
"components": [
"Research findings",
"System design",
"Implementation code",
"Test suite",
"Documentation"
]
}
)
print(" ✅ Final package compiled\n")
# Stop the swarm
print("Stopping swarm...")
stop_result = client.agent_swarm.stop_swarm(swarm_id=swarm_id)
print(f" ✅ Swarm stopped: {stop_result['status']}\n")
# Create a custom agent template for future use
print("Creating custom agent template...")
custom_agent = client.agent_swarm.create_agent(
name="API Security Specialist",
agent_type=AgentType.REVIEWER,
capabilities=[
"api_security",
"penetration_testing",
"vulnerability_assessment",
"compliance_checking"
],
prompt="You are an API security specialist focused on identifying and mitigating security risks in REST APIs",
config={
"temperature": 0.3,
"specialized_knowledge": ["OWASP", "OAuth2", "JWT", "API Gateway Security"]
}
)
print(f" ✅ Custom agent template created: {custom_agent['id']}\n")
print("✨ Agent Swarm example completed successfully!")
print("\nSummary:")
print("- Orchestrated 5 specialized agents")
print("- Completed 6 complex tasks")
print("- Demonstrated swarm control (pause/resume)")
print("- Created custom agent template")
except Exception as e:
print(f"❌ Error: {e}")
finally:
client.close()
if __name__ == "__main__":
main()