Add MatrixSwarm example

pull/901/head
Pavan Kumar 1 month ago committed by GitHub
commit a54b2ef8ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -68,6 +68,7 @@ Additionally, we have more comprehensive examples available in [The Swarms Cookb
| Federated | [Federated Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/experimental/federated_swarm.py) | Distributed learning system with privacy-preserving agent collaboration |
| Ant Colony | [Ant Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/experimental/ant_swarm.py) | Bio-inspired optimization using ant colony algorithms for agent coordination |
| Matrix | [Agent Matrix](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/experimental/agent_matrix.py) | Grid-based agent organization for complex problem-solving |
| MatrixSwarm | [MatrixSwarm Example](https://github.com/kyegomez/swarms/blob/master/examples/multi_agent/matrix_swarm_example.py) | Demonstrates transpose/add operations and running agents |
| DFS | [DFS Search Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/experimental/dfs_search_swarm.py) | Depth-first search swarm for complex problem exploration |
| Pulsar | [Pulsar Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/experimental/pulsar_swarm.py) | Pulsar-based coordination for synchronized agent behavior |

@ -0,0 +1,48 @@
from swarms import Agent
from swarms.structs.matrix_swarm import MatrixSwarm
from swarms.prompts.finance_agent_sys_prompt import FINANCIAL_AGENT_SYS_PROMPT
def create_agent(name: str) -> Agent:
"""Utility function to build a simple agent for the matrix."""
return Agent(
agent_name=name,
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
model_name="gpt-4o-mini",
max_loops=1,
streaming_on=False,
verbose=True,
)
if __name__ == "__main__":
# Initialize a 2x2 matrix of agents
agents = [
[create_agent(f"Agent-{i}-{j}") for j in range(2)]
for i in range(2)
]
swarm = MatrixSwarm(agents)
# Perform basic matrix operations
transposed = swarm.transpose()
added = swarm.add(transposed)
# Show shapes after operations
print("Original shape:", len(swarm.agents), len(swarm.agents[0]))
print("Transposed shape:", len(transposed.agents), len(transposed.agents[0]))
print("Added matrix shape:", len(added.agents), len(added.agents[0]))
# Prepare queries for each row of the matrix
queries = [
"What are the benefits of index funds?",
"How does compound interest work?",
]
# Run agents by multiplying the matrix with its transpose
results = swarm.multiply(transposed, queries)
# Display results
for row in results:
for output in row:
print(f"{output.agent_name}: {output.output_result}")
Loading…
Cancel
Save