diff --git a/README.md b/README.md index bfb77944..be8a26f5 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,37 @@ workflow.run() # Output the results for task in workflow.tasks: print(f"Task: {task.description}, Result: {task.result}") +``` + + + +### `ConcurrentWorkflow` +```python +import os +from dotenv import load_dotenv +from swarms.models import OpenAIChat, Task, ConcurrentWorkflow + +# Load environment variables from .env file +load_dotenv() +# Load environment variables +llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")) + +# Create a workflow +workflow = ConcurrentWorkflow(max_workers=5) + +# Create tasks +task1 = Task(llm, "What's the weather in miami") +task2 = Task(llm, "What's the weather in new york") +task3 = Task(llm, "What's the weather in london") + +# Add tasks to the workflow +workflow.add(task1) +workflow.add(task2) +workflow.add(task3) + +# Run the workflow +workflow.run() ``` diff --git a/concurrent_workflow.py b/concurrent_workflow.py new file mode 100644 index 00000000..61c8fcf8 --- /dev/null +++ b/concurrent_workflow.py @@ -0,0 +1,25 @@ +import os +from dotenv import load_dotenv +from swarms.models import OpenAIChat, Task, ConcurrentWorkflow + +# Load environment variables from .env file +load_dotenv() + +# Load environment variables +llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")) + +# Create a workflow +workflow = ConcurrentWorkflow(max_workers=5) + +# Create tasks +task1 = Task(llm, "What's the weather in miami") +task2 = Task(llm, "What's the weather in new york") +task3 = Task(llm, "What's the weather in london") + +# Add tasks to the workflow +workflow.add(task1) +workflow.add(task2) +workflow.add(task3) + +# Run the workflow +workflow.run() diff --git a/pyproject.toml b/pyproject.toml index f7379c21..b9270dbf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.1.3" +version = "3.1.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/base_workflow.py b/swarms/structs/base_workflow.py new file mode 100644 index 00000000..e69de29b diff --git a/swarms/structs/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py index 05a595e6..00340ee0 100644 --- a/swarms/structs/concurrent_workflow.py +++ b/swarms/structs/concurrent_workflow.py @@ -43,13 +43,22 @@ class ConcurrentWorkflow(BaseStruct): return_results: bool = False use_processes: bool = False - def add(self, task: Task): + def add(self, task: Task, tasks: List[Task] = None): """Adds a task to the workflow. Args: task (Task): _description_ + tasks (List[Task]): _description_ """ - self.tasks.append(task) + try: + if tasks: + for task in tasks: + self.tasks.append(task) + else: + self.tasks.append(task) + except Exception as error: + print(f"[ERROR][ConcurrentWorkflow] {error}") + raise error def run(self): """ diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index ca00fb6a..e4e0785f 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -34,9 +34,24 @@ class RecursiveWorkflow(BaseStruct): self.stop_token is not None ), "stop_token cannot be None" - def add(self, task: Task): - assert task is not None, "task cannot be None" - return self.tasks.appennd(task) + + def add(self, task: Task, tasks: List[Task] = None): + """Adds a task to the workflow. + + Args: + task (Task): _description_ + tasks (List[Task]): _description_ + """ + try: + if tasks: + for task in tasks: + self.tasks.append(task) + else: + self.tasks.append(task) + except Exception as error: + print(f"[ERROR][ConcurrentWorkflow] {error}") + raise error + def run(self): """ diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py index 41192a6f..9f7881c1 100644 --- a/swarms/structs/sequential_workflow.py +++ b/swarms/structs/sequential_workflow.py @@ -53,6 +53,7 @@ class SequentialWorkflow: self, agent: Union[Callable, Agent], task: Optional[str] = None, + tasks: Optional[List[str]] = None, *args, **kwargs, ) -> None: