[DOCS][Hiearchical display]

master
Kye Gomez 1 day ago
parent bb8368e9c0
commit 1649922179

@ -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).

@ -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.

@ -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

@ -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)
Loading…
Cancel
Save