The SwarmRouter is a flexible routing system designed to manage different types of swarms for task execution. It provides a unified interface to interact with various swarm types, including AgentRearrange, MixtureOfAgents, SpreadSheetSwarm, SequentialWorkflow, and ConcurrentWorkflow.
fromswarmsimportAgentfromswarms.structs.swarm_routerimportSwarmRouter,SwarmType# Initialize specialized agentsdata_extractor_agent=Agent(agent_name="Data-Extractor",system_prompt="You are a data extraction specialist...",model_name="gpt-4.1",max_loops=1,)summarizer_agent=Agent(agent_name="Document-Summarizer",system_prompt="You are a document summarization expert...",model_name="gpt-4.1",max_loops=1,)financial_analyst_agent=Agent(agent_name="Financial-Analyst",system_prompt="You are a financial analysis specialist...",model_name="gpt-4.1",max_loops=1,)
sequential_router=SwarmRouter(name="SequentialRouter",description="Process tasks in sequence",agents=[data_extractor_agent,summarizer_agent,financial_analyst_agent],swarm_type=SwarmType.SequentialWorkflow,max_loops=1)# Run a taskresult=sequential_router.run("Analyze and summarize the quarterly financial report")
concurrent_router=SwarmRouter(name="ConcurrentRouter",description="Process tasks concurrently",agents=[data_extractor_agent,summarizer_agent,financial_analyst_agent],swarm_type=SwarmType.ConcurrentWorkflow,max_loops=1)# Run a taskresult=concurrent_router.run("Evaluate multiple aspects of the company simultaneously")
rearrange_router=SwarmRouter(name="RearrangeRouter",description="Dynamically rearrange agents for optimal task processing",agents=[data_extractor_agent,summarizer_agent,financial_analyst_agent],swarm_type=SwarmType.AgentRearrange,flow=f"{data_extractor_agent.agent_name} -> {summarizer_agent.agent_name} -> {financial_analyst_agent.agent_name}",max_loops=1)# Run a taskresult=rearrange_router.run("Process and analyze company documents")
mixture_router=SwarmRouter(name="MixtureRouter",description="Combine multiple expert agents",agents=[data_extractor_agent,summarizer_agent,financial_analyst_agent],swarm_type=SwarmType.MixtureOfAgents,max_loops=1)# Run a taskresult=mixture_router.run("Provide comprehensive analysis of company performance")
Here's a complete example showing how to use SwarmRouter in a real-world scenario:
importosfromswarmsimportAgentfromswarms.structs.swarm_routerimportSwarmRouter,SwarmType# Initialize specialized agentsresearch_agent=Agent(agent_name="ResearchAgent",system_prompt="You are a research specialist...",model_name="gpt-4.1",max_loops=1)analysis_agent=Agent(agent_name="AnalysisAgent",system_prompt="You are an analysis expert...",model_name="gpt-4.1",max_loops=1)summary_agent=Agent(agent_name="SummaryAgent",system_prompt="You are a summarization specialist...",model_name="gpt-4.1",max_loops=1)# Create router with sequential workflowrouter=SwarmRouter(name="ResearchAnalysisRouter",description="Process research and analysis tasks",agents=[research_agent,analysis_agent,summary_agent],swarm_type=SwarmType.SequentialWorkflow,max_loops=1,verbose=True)# Run complex tasktry:result=router.run("Research and analyze the impact of AI on healthcare, ""providing a comprehensive summary of findings.")print("Task Result:",result)# Print logsforloginrouter.get_logs():print(f"{log.timestamp} - {log.level}: {log.message}")exceptExceptionase:print(f"Error processing task: {str(e)}")
This comprehensive guide demonstrates how to effectively use the SwarmRouter in various scenarios, making it easier to manage and orchestrate multiple agents for complex tasks.