From 1649922179c7802a1bba89b262d5da89249beb4d Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Tue, 25 Nov 2025 13:09:34 -0800 Subject: [PATCH] [DOCS][Hiearchical display] --- .../examples/hierarchical_swarm_example.md | 43 +++++++++++++ docs/swarms/structs/hierarchical_swarm.md | 60 +++++++++++++++++++ .../multi_agent/hiearchical_swarm/README.md | 1 + .../display_hierarchy_example.py | 47 +++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 examples/multi_agent/hiearchical_swarm/display_hierarchy_example.py diff --git a/docs/swarms/examples/hierarchical_swarm_example.md b/docs/swarms/examples/hierarchical_swarm_example.md index 86a5b85f..1f1b10c9 100644 --- a/docs/swarms/examples/hierarchical_swarm_example.md +++ b/docs/swarms/examples/hierarchical_swarm_example.md @@ -215,6 +215,48 @@ result = research_swarm.run(task=task) print(result) ``` +## Visualizing Swarm Hierarchy + +You can visualize the hierarchical structure of your swarm before executing tasks using the `display_hierarchy()` method: + +```python +from swarms import Agent +from swarms.structs.hiearchical_swarm import HierarchicalSwarm + +# Create specialized agents +research_agent = Agent( + agent_name="Research-Analyst", + agent_description="Specialized in comprehensive research and data gathering", + model_name="gpt-4o-mini", +) + +analysis_agent = Agent( + agent_name="Data-Analyst", + agent_description="Expert in data analysis and pattern recognition", + model_name="gpt-4o-mini", +) + +strategy_agent = Agent( + agent_name="Strategy-Consultant", + agent_description="Specialized in strategic planning and recommendations", + model_name="gpt-4o-mini", +) + +# Create hierarchical swarm +swarm = HierarchicalSwarm( + name="Swarms Corporation Operations", + description="Enterprise-grade hierarchical swarm for complex task execution", + agents=[research_agent, analysis_agent, strategy_agent], + max_loops=1, + director_model_name="claude-haiku-4-5", +) + +# Display the hierarchy visualization +swarm.display_hierarchy() +``` + +This will output a visual tree structure showing the Director and all worker agents, making it easy to understand the swarm's organizational structure before executing tasks. + ## Key Takeaways 1. **Agent Specialization**: Create agents with specific, well-defined expertise areas @@ -222,5 +264,6 @@ print(result) 3. **Appropriate Loop Count**: Set `max_loops` based on task complexity (1-3 for most tasks) 4. **Verbose Logging**: Enable verbose mode during development for debugging 5. **Context Preservation**: The swarm maintains full conversation history automatically +6. **Hierarchy Visualization**: Use `display_hierarchy()` to visualize swarm structure before execution For more detailed information about the `HierarchicalSwarm` API and advanced usage patterns, see the [main documentation](hierarchical_swarm.md). \ No newline at end of file diff --git a/docs/swarms/structs/hierarchical_swarm.md b/docs/swarms/structs/hierarchical_swarm.md index f458ac40..fe689e9e 100644 --- a/docs/swarms/structs/hierarchical_swarm.md +++ b/docs/swarms/structs/hierarchical_swarm.md @@ -35,6 +35,7 @@ The Hierarchical Swarm follows a clear workflow pattern: | **Comprehensive Logging** | Detailed logging for debugging and monitoring | | **Live Streaming** | Real-time streaming callbacks for monitoring agent outputs | | **Token-by-Token Updates** | Watch text formation in real-time as agents generate responses | +| **Hierarchy Visualization** | Visual tree representation of swarm structure with `display_hierarchy()` | ## Constructor @@ -70,6 +71,65 @@ Initializes a new HierarchicalSwarm instance. ## Core Methods +### `display_hierarchy()` + +Displays a visual tree representation of the hierarchical swarm structure, showing the Director at the top level and all worker agents as children branches. This method uses Rich formatting to create an aesthetically pleasing console output that helps visualize the organizational structure of the swarm. + +#### Returns + +| Type | Description | +|------|-------------| +| `None` | Prints the hierarchy visualization to the console | + +#### Example + +```python +from swarms import Agent +from swarms.structs.hiearchical_swarm import HierarchicalSwarm + +# Create specialized agents +research_agent = Agent( + agent_name="Research-Analyst", + agent_description="Specialized in comprehensive research and data gathering", + model_name="gpt-4o-mini", +) + +analysis_agent = Agent( + agent_name="Data-Analyst", + agent_description="Expert in data analysis and pattern recognition", + model_name="gpt-4o-mini", +) + +strategy_agent = Agent( + agent_name="Strategy-Consultant", + agent_description="Specialized in strategic planning and recommendations", + model_name="gpt-4o-mini", +) + +# Create hierarchical swarm +swarm = HierarchicalSwarm( + name="Swarms Corporation Operations", + description="Enterprise-grade hierarchical swarm for complex task execution", + agents=[research_agent, analysis_agent, strategy_agent], + max_loops=1, + director_model_name="claude-haiku-4-5", +) + +# Display the hierarchy visualization +swarm.display_hierarchy() +``` + +The output will show a visual tree structure like: +``` +┌─ HierarchicalSwarm Hierarchy: Swarms Corporation Operations ─┐ +│ │ +│ 🎯 Director [claude-haiku-4-5] │ +│ ├─ 🤖 Research-Analyst [gpt-4o-mini] - Specialized in... │ +│ ├─ 🤖 Data-Analyst [gpt-4o-mini] - Expert in data... │ +│ └─ 🤖 Strategy-Consultant [gpt-4o-mini] - Specialized... │ +└───────────────────────────────────────────────────────────────┘ +``` + ### `run()` Executes the hierarchical swarm for a specified number of feedback loops, processing the task through multiple iterations for refinement and improvement. diff --git a/examples/multi_agent/hiearchical_swarm/README.md b/examples/multi_agent/hiearchical_swarm/README.md index ca67f345..3ff9c1da 100644 --- a/examples/multi_agent/hiearchical_swarm/README.md +++ b/examples/multi_agent/hiearchical_swarm/README.md @@ -14,6 +14,7 @@ This directory contains examples demonstrating hierarchical swarm patterns for m - [hs_stock_team.py](hs_stock_team.py) - Stock trading team - [hybrid_hiearchical_swarm.py](hybrid_hiearchical_swarm.py) - Hybrid approach - [sector_analysis_hiearchical_swarm.py](sector_analysis_hiearchical_swarm.py) - Sector analysis +- [display_hierarchy_example.py](display_hierarchy_example.py) - Visualize swarm hierarchy structure ## Subdirectories diff --git a/examples/multi_agent/hiearchical_swarm/display_hierarchy_example.py b/examples/multi_agent/hiearchical_swarm/display_hierarchy_example.py new file mode 100644 index 00000000..b470596d --- /dev/null +++ b/examples/multi_agent/hiearchical_swarm/display_hierarchy_example.py @@ -0,0 +1,47 @@ +from swarms import Agent, HierarchicalSwarm + +# Create specialized agents +research_agent = Agent( + agent_name="Research-Analyst", + agent_description="Specialized in comprehensive research and data gathering", + model_name="gpt-4o-mini", + max_loops=1, + verbose=False, +) + +analysis_agent = Agent( + agent_name="Data-Analyst", + agent_description="Expert in data analysis and pattern recognition", + model_name="gpt-4o-mini", + max_loops=1, + verbose=False, +) + +strategy_agent = Agent( + agent_name="Strategy-Consultant", + agent_description="Specialized in strategic planning and recommendations", + model_name="gpt-4o-mini", + max_loops=1, + verbose=False, +) + +# Create hierarchical swarm with interactive dashboard +swarm = HierarchicalSwarm( + name="Swarms Corporation Operations", + description="Enterprise-grade hierarchical swarm for complex task execution", + agents=[research_agent, analysis_agent, strategy_agent], + max_loops=1, + interactive=False, # Enable the Arasaka dashboard + director_model_name="claude-haiku-4-5", + director_temperature=0.7, + director_top_p=None, + planning_enabled=True, +) + + +print(swarm.display_hierarchy()) + +# out = swarm.run( +# "Conduct a research analysis on water stocks and etfs" +# ) +# print(out)