This comprehensive documentation covers all functions in the multi_agent_exec.py module for running multiple agents using various execution strategies. The module provides synchronous and asynchronous execution methods, optimized performance with uvloop, and utility functions for information retrieval.
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_single_agentagent=Agent(agent_name="Financial-Analyst",system_prompt="You are a financial analysis expert",model_name="gpt-4o-mini",max_loops=1)result=run_single_agent(agent,"Analyze the current stock market trends")print(result)
importasynciofromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agent_asyncasyncdefmain():agent=Agent(agent_name="Researcher",system_prompt="You are a research assistant",model_name="gpt-4o-mini",max_loops=1)result=awaitrun_agent_async(agent,"Research AI advancements in 2024")print(result)asyncio.run(main())
importasynciofromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_concurrently_asyncasyncdefmain():agents=[Agent(agent_name=f"Analyst-{i}",system_prompt="You are a market analyst",model_name="gpt-4o-mini",max_loops=1)foriinrange(3)]task="Analyze the impact of AI on job markets"results=awaitrun_agents_concurrently_async(agents,task)fori,resultinenumerate(results):print(f"Agent {i+1} result: {result}")asyncio.run(main())
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_concurrently# Create multiple agentsagents=[Agent(agent_name="Tech-Analyst",system_prompt="You are a technology analyst",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="Finance-Analyst",system_prompt="You are a financial analyst",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="Market-Strategist",system_prompt="You are a market strategist",model_name="gpt-4o-mini",max_loops=1)]task="Analyze the future of electric vehicles in 2025"results=run_agents_concurrently(agents,task,max_workers=4)fori,resultinenumerate(results):print(f"Agent {i+1} ({agents[i].agent_name}): {result}")
importosfromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_concurrently_multiprocessagents=[Agent(agent_name=f"Research-Agent-{i}",system_prompt="You are a research specialist",model_name="gpt-4o-mini",max_loops=1)foriinrange(5)]task="Research the benefits of renewable energy"batch_size=os.cpu_count()# Use all CPU coresresults=run_agents_concurrently_multiprocess(agents,task,batch_size)print(f"Completed {len(results)} agent executions")
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportbatched_grid_agent_executionagents=[Agent(agent_name="Data-Scientist",system_prompt="You are a data science expert",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="ML-Engineer",system_prompt="You are a machine learning engineer",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="AI-Researcher",system_prompt="You are an AI researcher",model_name="gpt-4o-mini",max_loops=1)]tasks=["Analyze machine learning algorithms performance","Design a neural network architecture","Research latest AI breakthroughs"]results=batched_grid_agent_execution(agents,tasks,max_workers=3)fori,resultinenumerate(results):print(f"Task {i+1}: {tasks[i]}")print(f"Result: {result}\n")
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_with_different_tasks# Create agentsagents=[Agent(agent_name="Content-Writer",system_prompt="You are a content writer",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="Editor",system_prompt="You are an editor",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="SEO-Specialist",system_prompt="You are an SEO specialist",model_name="gpt-4o-mini",max_loops=1)]# Create agent-task pairsagent_task_pairs=[(agents[0],"Write a blog post about sustainable living"),(agents[1],"Edit and improve this article draft"),(agents[2],"Optimize this content for SEO")]results=run_agents_with_different_tasks(agent_task_pairs,batch_size=2)fori,resultinenumerate(results):agent,task=agent_task_pairs[i]print(f"{agent.agent_name} - {task}: {result}")
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_concurrently_uvloop# Note: uvloop must be installed (pip install uvloop)agents=[Agent(agent_name="Performance-Analyst",system_prompt="You are a performance analyst",model_name="gpt-4o-mini",max_loops=1)for_inrange(5)]task="Analyze system performance metrics"results=run_agents_concurrently_uvloop(agents,task)print(f"Processed {len(results)} agents with uvloop optimization")
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_with_tasks_uvloopagents=[Agent(agent_name="Data-Analyst-1",system_prompt="You are a data analyst",model_name="gpt-4o-mini",max_loops=1),Agent(agent_name="Data-Analyst-2",system_prompt="You are a data analyst",model_name="gpt-4o-mini",max_loops=1)]tasks=["Analyze sales data from Q1 2024","Analyze customer satisfaction metrics"]results=run_agents_with_tasks_uvloop(agents,tasks)fori,resultinenumerate(results):print(f"Task: {tasks[i]}")print(f"Result: {result}\n")
fromswarms.structs.multi_agent_execimportget_swarms_info# Assuming you have swarm objectsswarms=[# Your swarm objects here]info=get_swarms_info(swarms)print(info)# Output:# Available Swarms:## [Swarm 1]# Name: ResearchSwarm# Description: A swarm for research tasks# Length of Agents: 3# Swarm Type: hierarchical
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportget_agents_infoagents=[Agent(agent_name="Research-Agent",system_prompt="You are a research assistant",model_name="gpt-4o-mini",max_loops=2,role="Researcher"),Agent(agent_name="Analysis-Agent",system_prompt="You are a data analyst",model_name="gpt-4o-mini",max_loops=1,role="Analyst")]info=get_agents_info(agents,team_name="Data Team")print(info)# Output:# Available Agents for Team: Data Team## [Agent 1]# Name: Research-Agent# Description: You are a research assistant# Role: Researcher# Model: gpt-4o-mini# Max Loops: 2## [Agent 2]# Name: Analysis-Agent# Description: You are a data analyst# Role: Analyst# Model: gpt-4o-mini# Max Loops: 1
importasynciofromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimport(run_agents_concurrently,run_agents_with_different_tasks,batched_grid_agent_execution,get_agents_info)# Create specialized agentsagents=[Agent(agent_name="Market-Researcher",system_prompt="You are a market research expert specializing in consumer behavior",model_name="gpt-4o-mini",max_loops=1,role="Researcher"),Agent(agent_name="Data-Analyst",system_prompt="You are a data analyst expert in statistical analysis",model_name="gpt-4o-mini",max_loops=1,role="Analyst"),Agent(agent_name="Strategy-Consultant",system_prompt="You are a strategy consultant specializing in business development",model_name="gpt-4o-mini",max_loops=1,role="Consultant"),Agent(agent_name="Financial-Advisor",system_prompt="You are a financial advisor specializing in investment strategies",model_name="gpt-4o-mini",max_loops=1,role="Advisor")]# Display agent informationprint("=== Agent Information ===")print(get_agents_info(agents,"Business Intelligence Team"))print("\n"+"="*50+"\n")# Example 1: Same task for all agentsprint("=== Example 1: Concurrent Execution with Same Task ===")task="Analyze the impact of remote work trends on commercial real estate market in 2024"results=run_agents_concurrently(agents,task,max_workers=4)fori,resultinenumerate(results):print(f"\n{agents[i].agent_name} Analysis:")print(f"Result: {result}")print("\n"+"="*50+"\n")# Example 2: Different tasks for different agentsprint("=== Example 2: Different Tasks for Different Agents ===")agent_task_pairs=[(agents[0],"Research consumer preferences for electric vehicles"),(agents[1],"Analyze sales data for EV market penetration"),(agents[2],"Develop marketing strategy for EV adoption"),(agents[3],"Assess financial viability of EV charging infrastructure")]results=run_agents_with_different_tasks(agent_task_pairs,batch_size=2)fori,resultinenumerate(results):agent,task=agent_task_pairs[i]print(f"\n{agent.agent_name} - Task: {task}")print(f"Result: {result}")print("\n"+"="*50+"\n")# Example 3: Grid execution with matched agents and tasksprint("=== Example 3: Batched Grid Execution ===")grid_agents=agents[:3]# Use first 3 agentsgrid_tasks=["Forecast market trends for renewable energy","Evaluate risk factors in green technology investments","Compare traditional vs sustainable investment portfolios"]grid_results=batched_grid_agent_execution(grid_agents,grid_tasks,max_workers=3)fori,resultinenumerate(grid_results):print(f"\nTask {i+1}: {grid_tasks[i]}")print(f"Agent: {grid_agents[i].agent_name}")print(f"Result: {result}")print("\n=== Workflow Complete ===")
fromswarms.structs.agentimportAgentfromswarms.structs.multi_agent_execimportrun_agents_concurrentlyimportlogging# Set up logginglogging.basicConfig(level=logging.INFO)# Create agents with error handlingagents=[Agent(agent_name=f"Agent-{i}",system_prompt="You are a helpful assistant",model_name="gpt-4o-mini",max_loops=1)foriinrange(5)]task="Perform a complex analysis task"try:results=run_agents_concurrently(agents,task,max_workers=4)# Handle results (some may be exceptions)fori,resultinenumerate(results):ifisinstance(result,Exception):print(f"Agent {i+1} failed with error: {result}")else:print(f"Agent {i+1} succeeded: {result}")exceptExceptionase:print(f"Execution failed: {e}")# Best practices:# 1. Always handle exceptions in results# 2. Use appropriate max_workers based on system resources# 3. Monitor memory usage for large agent counts# 4. Consider batch processing for very large numbers of agents# 5. Use uvloop functions for I/O intensive tasks