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 1/4] 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"]) From 6f54434091eae7c1e13195537c9ae732de9b3da9 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Tue, 17 Jun 2025 16:36:03 +0000 Subject: [PATCH 2/4] fix: update minimal multi_agent_router example and docs for correct result handling --- docs/swarms/examples/multi_agent_router_minimal.md | 1 - examples/multi_agent/mar/multi_agent_router_minimal.py | 1 - 2 files changed, 2 deletions(-) diff --git a/docs/swarms/examples/multi_agent_router_minimal.md b/docs/swarms/examples/multi_agent_router_minimal.md index 5e283ec3..17ad8bc3 100644 --- a/docs/swarms/examples/multi_agent_router_minimal.md +++ b/docs/swarms/examples/multi_agent_router_minimal.md @@ -21,7 +21,6 @@ agents = [ 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/examples/multi_agent/mar/multi_agent_router_minimal.py b/examples/multi_agent/mar/multi_agent_router_minimal.py index 7020d0c3..2930c175 100644 --- a/examples/multi_agent/mar/multi_agent_router_minimal.py +++ b/examples/multi_agent/mar/multi_agent_router_minimal.py @@ -17,4 +17,3 @@ if __name__ == "__main__": router = MultiAgentRouter(agents=agents) result = router.route_task("Write a function that adds two numbers") - print("Chosen agent:", result["boss_decision"]["selected_agent"]) From f8f148d6e5d3892b4c2f052c88fbd5e715cfee53 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Tue, 17 Jun 2025 18:15:42 +0000 Subject: [PATCH 3/4] fix: update minimal MultiAgentRouter example and documentation --- .../mar/multi_agent_router_minimal.py | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/examples/multi_agent/mar/multi_agent_router_minimal.py b/examples/multi_agent/mar/multi_agent_router_minimal.py index 2930c175..898ccef3 100644 --- a/examples/multi_agent/mar/multi_agent_router_minimal.py +++ b/examples/multi_agent/mar/multi_agent_router_minimal.py @@ -1,19 +1,25 @@ -from swarms import Agent, MultiAgentRouter +from swarms import Agent +from swarms.structs.swarm_router import SwarmRouter -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", - ), - ] +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) +router = SwarmRouter( + name="multi-agent-router-demo", + description="Routes tasks to the most suitable agent", + agents=agents, + swarm_type="MultiAgentRouter" +) - result = router.route_task("Write a function that adds two numbers") +result = router.run("Write a function that adds two numbers") +print(result) \ No newline at end of file From 1dae904c43460131d0dd0b047db806773d965419 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Tue, 17 Jun 2025 18:18:52 +0000 Subject: [PATCH 4/4] docs: update minimal MultiAgentRouter example and documentation to use SwarmRouter interface --- .../examples/multi_agent_router_minimal.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/swarms/examples/multi_agent_router_minimal.md b/docs/swarms/examples/multi_agent_router_minimal.md index 17ad8bc3..71fc3076 100644 --- a/docs/swarms/examples/multi_agent_router_minimal.md +++ b/docs/swarms/examples/multi_agent_router_minimal.md @@ -1,9 +1,10 @@ # MultiAgentRouter Minimal Example -This example shows how to route a task to the most suitable agent using `MultiAgentRouter`. +This example shows how to route a task to the most suitable agent using `SwarmRouter` with `swarm_type="MultiAgentRouter"`. ```python -from swarms import Agent, MultiAgentRouter +from swarms import Agent +from swarms.structs.swarm_router import SwarmRouter agents = [ Agent( @@ -18,9 +19,15 @@ agents = [ ), ] -router = MultiAgentRouter(agents=agents) +router = SwarmRouter( + name="multi-agent-router-demo", + description="Routes tasks to the most suitable agent", + agents=agents, + swarm_type="MultiAgentRouter" +) -result = router.route_task("Write a function that adds two numbers") +result = router.run("Write a function that adds two numbers") +print(result) ``` -View the source on [GitHub](https://github.com/kyegomez/swarms/blob/master/examples/multi_agent/mar/multi_agent_router_minimal.py). +View the source on [GitHub](https://github.com/kyegomez/swarms/blob/master/examples/multi_agent/mar/multi_agent_router_minimal.py). \ No newline at end of file