[FEAT][GraphWorkflow]

pull/518/merge
Kye Gomez 7 months ago
parent b4d9e29e50
commit c9f6df1867

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

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

@ -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 <kye@apac.ai>"]

@ -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",
]

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

Loading…
Cancel
Save