From 06d79356d48a0f44be449982bff02f75c0f9f83d Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Tue, 17 Jun 2025 21:57:23 +0530 Subject: [PATCH] Fix MultiAgentRouter example --- docs/mkdocs.yml | 1 + docs/swarms/concept/swarm_architectures.md | 1 + .../examples/multi_agent_router_minimal.md | 27 +++++++++++++++++++ docs/swarms/structs/swarm_router.md | 2 ++ .../mar/multi_agent_router_minimal.py | 20 ++++++++++++++ 5 files changed, 51 insertions(+) create mode 100644 docs/swarms/examples/multi_agent_router_minimal.md create mode 100644 examples/multi_agent/mar/multi_agent_router_minimal.py diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 164a74ae..260cab31 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -344,6 +344,7 @@ nav: - Group Chat Example: "swarms/examples/groupchat_example.md" - Sequential Workflow Example: "swarms/examples/sequential_example.md" - SwarmRouter Example: "swarms/examples/swarm_router.md" + - MultiAgentRouter Minimal Example: "swarms/examples/multi_agent_router_minimal.md" - ConcurrentWorkflow Example: "swarms/examples/concurrent_workflow.md" - MixtureOfAgents Example: "swarms/examples/mixture_of_agents.md" - Unique Swarms: "swarms/examples/unique_swarms.md" diff --git a/docs/swarms/concept/swarm_architectures.md b/docs/swarms/concept/swarm_architectures.md index e2d206fd..20aefb4a 100644 --- a/docs/swarms/concept/swarm_architectures.md +++ b/docs/swarms/concept/swarm_architectures.md @@ -34,6 +34,7 @@ Swarm architectures leverage these communication patterns to ensure that agents | Spreadsheet Swarm | Manages tasks at scale, tracking agent outputs in a structured format like CSV files. | [Code Link](https://docs.swarms.world/en/latest/swarms/structs/spreadsheet_swarm/) | Large-scale marketing analytics, financial audits | | Forest Swarm | A swarm structure that organizes agents in a tree-like hierarchy for complex decision-making processes. | [Code Link](https://docs.swarms.world/en/latest/swarms/structs/forest_swarm/) | Multi-stage workflows, hierarchical reinforcement learning | | Swarm Router | Routes and chooses the swarm architecture based on the task requirements and available agents. | [Code Link](https://docs.swarms.world/en/latest/swarms/structs/swarm_router/) | Dynamic task routing, adaptive swarm architecture selection, optimized agent allocation | +| MultiAgentRouter | Boss agent selects the best agent for each task. | [Minimal Example](https://github.com/kyegomez/swarms/blob/master/examples/multi_agent/mar/multi_agent_router_minimal.py) | Task-specific agent routing | diff --git a/docs/swarms/examples/multi_agent_router_minimal.md b/docs/swarms/examples/multi_agent_router_minimal.md new file mode 100644 index 00000000..5e283ec3 --- /dev/null +++ b/docs/swarms/examples/multi_agent_router_minimal.md @@ -0,0 +1,27 @@ +# MultiAgentRouter Minimal Example + +This example shows how to route a task to the most suitable agent using `MultiAgentRouter`. + +```python +from swarms import Agent, MultiAgentRouter + +agents = [ + Agent( + agent_name="Researcher", + system_prompt="Answer questions briefly.", + model_name="gpt-4o-mini", + ), + Agent( + agent_name="Coder", + system_prompt="Write small Python functions.", + model_name="gpt-4o-mini", + ), +] + +router = MultiAgentRouter(agents=agents) + +result = router.route_task("Write a function that adds two numbers") +print("Chosen agent:", result["boss_decision"]["selected_agent"]) +``` + +View the source on [GitHub](https://github.com/kyegomez/swarms/blob/master/examples/multi_agent/mar/multi_agent_router_minimal.py). diff --git a/docs/swarms/structs/swarm_router.md b/docs/swarms/structs/swarm_router.md index 03de7071..f20fbe3c 100644 --- a/docs/swarms/structs/swarm_router.md +++ b/docs/swarms/structs/swarm_router.md @@ -419,6 +419,8 @@ multi_agent_router = SwarmRouter( result = multi_agent_router.run("Analyze the competitive landscape for our new product") ``` +See [MultiAgentRouter Minimal Example](../examples/multi_agent_router_minimal.md) for a lightweight demonstration. + ### HierarchicalSwarm Use Case: Creating a hierarchical structure of agents with a director. diff --git a/examples/multi_agent/mar/multi_agent_router_minimal.py b/examples/multi_agent/mar/multi_agent_router_minimal.py new file mode 100644 index 00000000..7020d0c3 --- /dev/null +++ b/examples/multi_agent/mar/multi_agent_router_minimal.py @@ -0,0 +1,20 @@ +from swarms import Agent, MultiAgentRouter + +if __name__ == "__main__": + agents = [ + Agent( + agent_name="Researcher", + system_prompt="Answer questions briefly.", + model_name="gpt-4o-mini", + ), + Agent( + agent_name="Coder", + system_prompt="Write small Python functions.", + model_name="gpt-4o-mini", + ), + ] + + router = MultiAgentRouter(agents=agents) + + result = router.route_task("Write a function that adds two numbers") + print("Chosen agent:", result["boss_decision"]["selected_agent"])