From 638ec018051c6f1b533447b1ef02ddedada927e2 Mon Sep 17 00:00:00 2001 From: Pavan Kumar <66913595+ascender1729@users.noreply.github.com> Date: Thu, 19 Jun 2025 21:17:41 +0530 Subject: [PATCH] Add graph workflow example and update docs --- docs/examples/index.md | 1 + examples/structs/graph_workflow_basic.py | 50 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 examples/structs/graph_workflow_basic.py diff --git a/docs/examples/index.md b/docs/examples/index.md index 4781c3d7..e10af50e 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -60,6 +60,7 @@ Additionally, we have more comprehensive examples available in [The Swarms Cookb | Concurrent | [Concurrent Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/concurrent_swarm/concurrent_swarm_example.py) | Parallel execution of tasks across multiple agents for improved performance | | Star | [Star Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/different_architectures/star_swarm.py) | Centralized architecture with a hub agent coordinating peripheral agents | | Circular | [Circular Swarm](https://github.com/The-Swarm-Corporation/swarms-examples/blob/main/examples/structs/swarms/different_architectures/circular_swarm.py) | Ring topology for cyclic information flow between agents | +| Graph Workflow | [Graph Workflow Basic](https://github.com/kyegomez/swarms/blob/main/examples/structs/graph_workflow_basic.py) | Minimal graph workflow with two agents and one task | ### Experimental Architectures | Category | Example | Description | diff --git a/examples/structs/graph_workflow_basic.py b/examples/structs/graph_workflow_basic.py new file mode 100644 index 00000000..2d31ed1f --- /dev/null +++ b/examples/structs/graph_workflow_basic.py @@ -0,0 +1,50 @@ +from swarms import Agent, Edge, GraphWorkflow, Node, NodeType + + +def sample_task(): + """Simple callable task.""" + print("Running sample task") + return "Task completed" + + +if __name__ == "__main__": + # Initialize two basic agents + agent1 = Agent( + agent_name="Agent1", + model_name="openai/gpt-4o-mini", + temperature=0.5, + max_tokens=4000, + max_loops=1, + autosave=True, + dashboard=True, + ) + + agent2 = Agent( + agent_name="Agent2", + model_name="openai/gpt-4o-mini", + temperature=0.5, + max_tokens=4000, + max_loops=1, + autosave=True, + dashboard=True, + ) + + # Build the workflow graph + wf_graph = GraphWorkflow() + wf_graph.add_node(Node(id="agent1", type=NodeType.AGENT, agent=agent1)) + wf_graph.add_node(Node(id="agent2", type=NodeType.AGENT, agent=agent2)) + wf_graph.add_node(Node(id="task1", type=NodeType.TASK, callable=sample_task)) + + wf_graph.add_edge(Edge(source="agent1", target="task1")) + wf_graph.add_edge(Edge(source="agent2", target="task1")) + + wf_graph.set_entry_points(["agent1", "agent2"]) + wf_graph.set_end_points(["task1"]) + + # Optional visualization in Mermaid format + print(wf_graph.visualize()) + + # Execute the graph + results = wf_graph.run() + print("Execution results:", results) +