forked from kyegomez/swarms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sequential_workflow.py
More file actions
324 lines (282 loc) · 9.38 KB
/
Copy pathtest_sequential_workflow.py
File metadata and controls
324 lines (282 loc) · 9.38 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
import pytest
from swarms import Agent, SequentialWorkflow
# Test SequentialWorkflow class
def test_sequential_workflow_initialization():
workflow = SequentialWorkflow()
assert isinstance(workflow, SequentialWorkflow)
assert workflow.agents is None
assert workflow.max_loops == 1
assert workflow.flow == ""
assert workflow.agent_rearrange is None
assert workflow.name == "SequentialWorkflow"
assert workflow.id == "sequential_workflow"
def test_sequential_workflow_initialization_with_agents():
"""Test SequentialWorkflow initialization with agents"""
agent1 = Agent(
agent_name="Agent-1",
agent_description="First test agent",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Agent-2",
agent_description="Second test agent",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Test-Workflow",
description="Test workflow with multiple agents",
agents=[agent1, agent2],
max_loops=1,
)
assert isinstance(workflow, SequentialWorkflow)
assert workflow.name == "Test-Workflow"
assert (
workflow.description == "Test workflow with multiple agents"
)
assert len(workflow.agents) == 2
assert workflow.agents[0] == agent1
assert workflow.agents[1] == agent2
assert workflow.max_loops == 1
def test_sequential_workflow_multi_agent_execution():
"""Test SequentialWorkflow execution with multiple agents"""
agent1 = Agent(
agent_name="Research-Agent",
agent_description="Agent for research tasks",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Analysis-Agent",
agent_description="Agent for analyzing research results",
model_name="gpt-4o",
max_loops=1,
)
agent3 = Agent(
agent_name="Summary-Agent",
agent_description="Agent for summarizing findings",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Multi-Agent-Research-Workflow",
description="Workflow for comprehensive research, analysis, and summarization",
agents=[agent1, agent2, agent3],
max_loops=1,
)
# Test that the workflow executes successfully
result = workflow.run(
"Analyze the impact of renewable energy on climate change"
)
print("\n" + "="*80)
print("WORKFLOW RESULT:")
print("="*80)
print(result)
print("="*80 + "\n")
assert result is not None
# SequentialWorkflow may return different types based on output_type, just ensure it's not None
def test_sequential_workflow_batched_execution():
"""Test batched execution of SequentialWorkflow"""
agent1 = Agent(
agent_name="Data-Collector",
agent_description="Agent for collecting data",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Data-Processor",
agent_description="Agent for processing collected data",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Batched-Processing-Workflow",
agents=[agent1, agent2],
max_loops=1,
)
# Test batched execution
tasks = [
"Analyze solar energy trends",
"Evaluate wind power efficiency",
"Compare renewable energy sources",
]
results = workflow.run_batched(tasks)
assert results is not None
# run_batched returns a list of results
assert isinstance(results, list)
assert len(results) == 3
@pytest.mark.asyncio
async def test_sequential_workflow_async_execution():
"""Test async execution of SequentialWorkflow"""
agent1 = Agent(
agent_name="Async-Research-Agent",
agent_description="Agent for async research tasks",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Async-Analysis-Agent",
agent_description="Agent for async analysis",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Async-Workflow",
agents=[agent1, agent2],
max_loops=1,
)
# Test async execution
result = await workflow.run_async("Analyze AI trends in 2024")
assert result is not None
@pytest.mark.asyncio
async def test_sequential_workflow_concurrent_execution():
"""Test concurrent execution of SequentialWorkflow"""
agent1 = Agent(
agent_name="Concurrent-Research-Agent",
agent_description="Agent for concurrent research",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Concurrent-Analysis-Agent",
agent_description="Agent for concurrent analysis",
model_name="gpt-4o",
max_loops=1,
)
agent3 = Agent(
agent_name="Concurrent-Summary-Agent",
agent_description="Agent for concurrent summarization",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Concurrent-Workflow",
agents=[agent1, agent2, agent3],
max_loops=1,
)
# Test concurrent execution
tasks = [
"Research quantum computing advances",
"Analyze blockchain technology trends",
"Evaluate machine learning applications",
]
results = await workflow.run_concurrent(tasks)
assert results is not None
# run_concurrent returns a list of results
assert isinstance(results, list)
assert len(results) == 3
def test_sequential_workflow_with_multi_agent_collaboration():
"""Test SequentialWorkflow with multi-agent collaboration prompts"""
agent1 = Agent(
agent_name="Market-Research-Agent",
agent_description="Agent for market research",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Competitive-Analysis-Agent",
agent_description="Agent for competitive analysis",
model_name="gpt-4o",
max_loops=1,
)
agent3 = Agent(
agent_name="Strategy-Development-Agent",
agent_description="Agent for developing business strategies",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Business-Strategy-Workflow",
description="Comprehensive business strategy development workflow",
agents=[agent1, agent2, agent3],
max_loops=1,
multi_agent_collab_prompt=True,
)
# Test that collaboration prompt is added
assert agent1.system_prompt is not None
assert agent2.system_prompt is not None
assert agent3.system_prompt is not None
# Test execution
result = workflow.run(
"Develop a business strategy for entering the AI market"
)
assert result is not None
def test_sequential_workflow_error_handling():
"""Test SequentialWorkflow error handling"""
# Test that initialization with None agents is allowed
workflow_none = SequentialWorkflow(agents=None)
assert workflow_none.agents is None
# Test that initialization with empty agents is allowed
workflow_empty = SequentialWorkflow(agents=[])
assert workflow_empty.agents == []
# Test that running with no agents raises error
with pytest.raises(
ValueError, match="Agents list cannot be None or empty"
):
workflow_none.run("test task")
with pytest.raises(
ValueError, match="Agents list cannot be None or empty"
):
workflow_empty.run("test task")
# Test with zero max_loops
with pytest.raises(ValueError, match="max_loops cannot be 0"):
agent1 = Agent(
agent_name="Test-Agent",
agent_description="Test agent",
model_name="gpt-4o",
max_loops=1,
)
SequentialWorkflow(agents=[agent1], max_loops=0)
def test_sequential_workflow_agent_names_extraction():
"""Test that SequentialWorkflow properly extracts agent names for flow"""
agent1 = Agent(
agent_name="Alpha-Agent",
agent_description="First agent",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Beta-Agent",
agent_description="Second agent",
model_name="gpt-4o",
max_loops=1,
)
agent3 = Agent(
agent_name="Gamma-Agent",
agent_description="Third agent",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Test-Flow-Workflow",
agents=[agent1, agent2, agent3],
max_loops=1,
)
# Test flow string generation
expected_flow = "Alpha-Agent -> Beta-Agent -> Gamma-Agent"
assert workflow.flow == expected_flow
def test_sequential_workflow_team_awareness():
"""Test SequentialWorkflow with team awareness enabled"""
agent1 = Agent(
agent_name="Team-Member-1",
agent_description="First team member",
model_name="gpt-4o",
max_loops=1,
)
agent2 = Agent(
agent_name="Team-Member-2",
agent_description="Second team member",
model_name="gpt-4o",
max_loops=1,
)
workflow = SequentialWorkflow(
name="Team-Aware-Workflow",
description="Workflow with team awareness",
agents=[agent1, agent2],
max_loops=1,
team_awareness=True,
)
# Test that workflow initializes successfully with team awareness
assert workflow.team_awareness is True
assert len(workflow.agents) == 2