[BUG-FIX] Swarm Router Bug Fix

pull/1204/head
Aksh Parekh 5 days ago
parent 7e5974b05d
commit 6741c8f981

@ -290,14 +290,14 @@ task = "Write a short story about a robot who discovers music."
# --- Example 1: SequentialWorkflow --- # --- Example 1: SequentialWorkflow ---
# Agents run one after another in a chain: Writer -> Editor -> Reviewer. # Agents run one after another in a chain: Writer -> Editor -> Reviewer.
print("Running a Sequential Workflow...") print("Running a Sequential Workflow...")
sequential_router = SwarmRouter(swarm_type=SwarmType.SequentialWorkflow, agents=agents) sequential_router = SwarmRouter(swarm_type="SequentialWorkflow", agents=agents)
sequential_output = sequential_router.run(task) sequential_output = sequential_router.run(task)
print(f"Final Sequential Output:\n{sequential_output}\n") print(f"Final Sequential Output:\n{sequential_output}\n")
# --- Example 2: ConcurrentWorkflow --- # --- Example 2: ConcurrentWorkflow ---
# All agents receive the same initial task and run at the same time. # All agents receive the same initial task and run at the same time.
print("Running a Concurrent Workflow...") print("Running a Concurrent Workflow...")
concurrent_router = SwarmRouter(swarm_type=SwarmType.ConcurrentWorkflow, agents=agents) concurrent_router = SwarmRouter(swarm_type="ConcurrentWorkflow", agents=agents)
concurrent_outputs = concurrent_router.run(task) concurrent_outputs = concurrent_router.run(task)
# This returns a dictionary of each agent's output # This returns a dictionary of each agent's output
for agent_name, output in concurrent_outputs.items(): for agent_name, output in concurrent_outputs.items():
@ -312,9 +312,9 @@ aggregator = Agent(
model_name="gpt-4o-mini" model_name="gpt-4o-mini"
) )
moa_router = SwarmRouter( moa_router = SwarmRouter(
swarm_type=SwarmType.MixtureOfAgents, swarm_type="MixtureOfAgents",
agents=agents, agents=agents,
aggregator_agent=aggregator, # MoA requires an aggregator aggregator_agent=aggregator,
) )
aggregated_output = moa_router.run(task) aggregated_output = moa_router.run(task)
print(f"Final Aggregated Output:\n{aggregated_output}\n") print(f"Final Aggregated Output:\n{aggregated_output}\n")

@ -29,7 +29,7 @@ GROQ_API_KEY=""
```python ```python
from swarms import Agent from swarms import Agent
from swarms.structs.swarm_router import SwarmRouter, SwarmType from swarms.structs.swarm_router import SwarmRouter
# Initialize specialized agents # Initialize specialized agents
data_extractor_agent = Agent( data_extractor_agent = Agent(
@ -61,7 +61,7 @@ sequential_router = SwarmRouter(
name="SequentialRouter", name="SequentialRouter",
description="Process tasks in sequence", description="Process tasks in sequence",
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent], agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
swarm_type=SwarmType.SequentialWorkflow, swarm_type="SequentialWorkflow",
max_loops=1 max_loops=1
) )
@ -76,7 +76,7 @@ concurrent_router = SwarmRouter(
name="ConcurrentRouter", name="ConcurrentRouter",
description="Process tasks concurrently", description="Process tasks concurrently",
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent], agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
swarm_type=SwarmType.ConcurrentWorkflow, swarm_type="ConcurrentWorkflow",
max_loops=1 max_loops=1
) )
@ -91,8 +91,8 @@ rearrange_router = SwarmRouter(
name="RearrangeRouter", name="RearrangeRouter",
description="Dynamically rearrange agents for optimal task processing", description="Dynamically rearrange agents for optimal task processing",
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent], agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
swarm_type=SwarmType.AgentRearrange, swarm_type="AgentRearrange",
flow=f"{data_extractor_agent.agent_name} -> {summarizer_agent.agent_name} -> {financial_analyst_agent.agent_name}", rearrange_flow=f"{data_extractor_agent.agent_name} -> {summarizer_agent.agent_name} -> {financial_analyst_agent.agent_name}",
max_loops=1 max_loops=1
) )
@ -107,7 +107,7 @@ mixture_router = SwarmRouter(
name="MixtureRouter", name="MixtureRouter",
description="Combine multiple expert agents", description="Combine multiple expert agents",
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent], agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
swarm_type=SwarmType.MixtureOfAgents, swarm_type="MixtureOfAgents",
max_loops=1 max_loops=1
) )
@ -137,7 +137,7 @@ router = SwarmRouter(
name="CustomRouter", name="CustomRouter",
description="Custom router configuration", description="Custom router configuration",
agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent], agents=[data_extractor_agent, summarizer_agent, financial_analyst_agent],
swarm_type=SwarmType.SequentialWorkflow, swarm_type="SequentialWorkflow",
max_loops=3, max_loops=3,
autosave=True, autosave=True,
verbose=True, verbose=True,
@ -145,6 +145,27 @@ router = SwarmRouter(
) )
``` ```
# SwarmType Reference
## Valid SwarmType Values
| Value | Description |
|-------|-------------|
| `"SequentialWorkflow"` | Execute agents in sequence |
| `"ConcurrentWorkflow"` | Execute agents concurrently |
| `"AgentRearrange"` | Dynamically rearrange agent execution order |
| `"MixtureOfAgents"` | Combine outputs from multiple agents |
| `"GroupChat"` | Enable group chat between agents |
| `"MultiAgentRouter"` | Route tasks to appropriate agents |
| `"AutoSwarmBuilder"` | Automatically build swarm configuration |
| `"HiearchicalSwarm"` | Hierarchical agent organization |
| `"MajorityVoting"` | Use majority voting for decisions |
| `"MALT"` | Multi-Agent Learning and Training |
| `"CouncilAsAJudge"` | Council-based evaluation system |
| `"InteractiveGroupChat"` | Interactive group chat with agents |
| `"HeavySwarm"` | Heavy swarm for complex tasks |
| `"auto"` | Automatically select swarm type |
# Best Practices # Best Practices
## Choose the appropriate swarm type based on your task requirements: ## Choose the appropriate swarm type based on your task requirements:
@ -187,7 +208,7 @@ Here's a complete example showing how to use SwarmRouter in a real-world scenari
```python ```python
import os import os
from swarms import Agent from swarms import Agent
from swarms.structs.swarm_router import SwarmRouter, SwarmType from swarms.structs.swarm_router import SwarmRouter
# Initialize specialized agents # Initialize specialized agents
research_agent = Agent( research_agent = Agent(
@ -216,7 +237,7 @@ router = SwarmRouter(
name="ResearchAnalysisRouter", name="ResearchAnalysisRouter",
description="Process research and analysis tasks", description="Process research and analysis tasks",
agents=[research_agent, analysis_agent, summary_agent], agents=[research_agent, analysis_agent, summary_agent],
swarm_type=SwarmType.SequentialWorkflow, swarm_type="SequentialWorkflow",
max_loops=1, max_loops=1,
verbose=True verbose=True
) )

@ -186,14 +186,14 @@ task = "Write a short story about a robot who discovers music."
# --- Example 1: SequentialWorkflow --- # --- Example 1: SequentialWorkflow ---
# Agents run one after another in a chain: Writer -> Editor -> Reviewer. # Agents run one after another in a chain: Writer -> Editor -> Reviewer.
print("Running a Sequential Workflow...") print("Running a Sequential Workflow...")
sequential_router = SwarmRouter(swarm_type=SwarmType.SequentialWorkflow, agents=agents) sequential_router = SwarmRouter(swarm_type="SequentialWorkflow", agents=agents)
sequential_output = sequential_router.run(task) sequential_output = sequential_router.run(task)
print(f"Final Sequential Output:\n{sequential_output}\n") print(f"Final Sequential Output:\n{sequential_output}\n")
# --- Example 2: ConcurrentWorkflow --- # --- Example 2: ConcurrentWorkflow ---
# All agents receive the same initial task and run at the same time. # All agents receive the same initial task and run at the same time.
print("Running a Concurrent Workflow...") print("Running a Concurrent Workflow...")
concurrent_router = SwarmRouter(swarm_type=SwarmType.ConcurrentWorkflow, agents=agents) concurrent_router = SwarmRouter(swarm_type="ConcurrentWorkflow", agents=agents)
concurrent_outputs = concurrent_router.run(task) concurrent_outputs = concurrent_router.run(task)
# This returns a dictionary of each agent's output # This returns a dictionary of each agent's output
for agent_name, output in concurrent_outputs.items(): for agent_name, output in concurrent_outputs.items():
@ -208,9 +208,9 @@ aggregator = Agent(
model_name="gpt-4o-mini" model_name="gpt-4o-mini"
) )
moa_router = SwarmRouter( moa_router = SwarmRouter(
swarm_type=SwarmType.MixtureOfAgents, swarm_type="MixtureOfAgents",
agents=agents, agents=agents,
aggregator_agent=aggregator, # MoA requires an aggregator aggregator_agent=aggregator,
) )
aggregated_output = moa_router.run(task) aggregated_output = moa_router.run(task)
print(f"Final Aggregated Output:\n{aggregated_output}\n") print(f"Final Aggregated Output:\n{aggregated_output}\n")

@ -272,6 +272,40 @@ class SwarmRouter:
"SwarmRouter: Swarm type cannot be 'none'. Check the docs for all the swarm types available. https://docs.swarms.world/en/latest/swarms/structs/swarm_router/" "SwarmRouter: Swarm type cannot be 'none'. Check the docs for all the swarm types available. https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
) )
# Validate swarm type is a valid string
valid_swarm_types = [
"AgentRearrange",
"MixtureOfAgents",
"SequentialWorkflow",
"ConcurrentWorkflow",
"GroupChat",
"MultiAgentRouter",
"AutoSwarmBuilder",
"HiearchicalSwarm",
"auto",
"MajorityVoting",
"MALT",
"CouncilAsAJudge",
"InteractiveGroupChat",
"HeavySwarm",
"BatchedGridWorkflow",
]
if not isinstance(self.swarm_type, str):
raise SwarmRouterConfigError(
f"SwarmRouter: swarm_type must be a string, not {type(self.swarm_type).__name__}. "
f"Valid types are: {', '.join(valid_swarm_types)}. "
"Use swarm_type='SequentialWorkflow' (string), NOT SwarmType.SequentialWorkflow. "
"See https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
)
if self.swarm_type not in valid_swarm_types:
raise SwarmRouterConfigError(
f"SwarmRouter: Invalid swarm_type '{self.swarm_type}'. "
f"Valid types are: {', '.join(valid_swarm_types)}. "
"See https://docs.swarms.world/en/latest/swarms/structs/swarm_router/"
)
if ( if (
self.swarm_type != "HeavySwarm" self.swarm_type != "HeavySwarm"
and self.agents is None and self.agents is None

Loading…
Cancel
Save