diff --git a/docs/swarms/structs/graph_workflow.md b/docs/swarms/structs/graph_workflow.md index 6010956a..4316bd04 100644 --- a/docs/swarms/structs/graph_workflow.md +++ b/docs/swarms/structs/graph_workflow.md @@ -134,10 +134,13 @@ print("Execution results:", results) Below is a comprehensive example demonstrating the creation and execution of a workflow graph: ```python -from swarms import Agent, OpenAIChat + import os + from dotenv import load_dotenv +from swarms import Agent, Edge, GraphWorkflow, Node, NodeType, OpenAIChat + load_dotenv() api_key = os.environ.get("OPENAI_API_KEY") @@ -155,7 +158,9 @@ def sample_task(): 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_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")) @@ -167,6 +172,7 @@ print(wf_graph.visualize()) # Run the workflow results = wf_graph.run() print("Execution results:", results) + ``` In this example, we set up a workflow graph with two agents and one task. We define the entry and end points, visualize the graph, and then execute the workflow, capturing and printing the results. diff --git a/playground/structs/multi_agent_collaboration/graph_workflow_example.py b/playground/structs/multi_agent_collaboration/graph_workflow_example.py new file mode 100644 index 00000000..ad265db1 --- /dev/null +++ b/playground/structs/multi_agent_collaboration/graph_workflow_example.py @@ -0,0 +1,38 @@ + +import os + +from dotenv import load_dotenv + +from swarms import Agent, Edge, GraphWorkflow, Node, NodeType, OpenAIChat + +load_dotenv() + +api_key = os.environ.get("OPENAI_API_KEY") + +llm = OpenAIChat( + temperature=0.5, openai_api_key=api_key, max_tokens=4000 +) +agent1 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) +agent2 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) + +def sample_task(): + print("Running sample task") + return "Task completed" + +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"]) + +print(wf_graph.visualize()) + +# Run the workflow +results = wf_graph.run() +print("Execution results:", results) diff --git a/pyproject.toml b/pyproject.toml index d1a9d26b..04d5eaf7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "5.2.7" +version = "5.2.8" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 0c9ba133..4ebb4a96 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -89,7 +89,7 @@ from swarms.structs.yaml_model import ( pydantic_type_to_yaml_schema, ) from swarms.structs.mixture_of_agents import MixtureOfAgents -from swarms.structs.graph_workflow import GraphWorkflow, Node, NodeType +from swarms.structs.graph_workflow import GraphWorkflow, Node, NodeType, Edge __all__ = [ @@ -167,4 +167,5 @@ __all__ = [ "GraphWorkflow", "Node", "NodeType", + "Edge", ] diff --git a/swarms/structs/graph_workflow.py b/swarms/structs/graph_workflow.py index fec6af8d..d2781f50 100644 --- a/swarms/structs/graph_workflow.py +++ b/swarms/structs/graph_workflow.py @@ -210,40 +210,40 @@ class GraphWorkflow(BaseModel): raise e -# # Example usage -# if __name__ == "__main__": -# from swarms import Agent, OpenAIChat -# import os -# from dotenv import load_dotenv - -# load_dotenv() - -# api_key = os.environ.get("OPENAI_API_KEY") - -# llm = OpenAIChat( -# temperature=0.5, openai_api_key=api_key, max_tokens=4000 -# ) -# agent1 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) -# agent2 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) - -# def sample_task(): -# print("Running sample task") -# return "Task completed" - -# 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"]) - -# print(wf_graph.visualize()) - -# # Run the workflow -# results = wf_graph.run() -# print("Execution results:", results) +# Example usage +if __name__ == "__main__": + from swarms import Agent, OpenAIChat + import os + from dotenv import load_dotenv + + load_dotenv() + + api_key = os.environ.get("OPENAI_API_KEY") + + llm = OpenAIChat( + temperature=0.5, openai_api_key=api_key, max_tokens=4000 + ) + agent1 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) + agent2 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) + + def sample_task(): + print("Running sample task") + return "Task completed" + + 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"]) + + print(wf_graph.visualize()) + + # Run the workflow + results = wf_graph.run() + print("Execution results:", results)