You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/docs/swarms/structs/concurrentworkflow.md

2.5 KiB

    # Module/Function Name: ConcurrentWorkflow

    class swarms.structs.ConcurrentWorkflow(max_workers, autosave, saved_state_filepath):
        """
        ConcurrentWorkflow class for running a set of tasks concurrently using N autonomous agents.

        Args:
            - max_workers (int): The maximum number of workers to use for concurrent execution.
            - autosave (bool): Whether to autosave the workflow state.
            - saved_state_filepath (Optional[str]): The file path to save the workflow state.

        """

        def add(self, task, tasks=None):
            """Adds a task to the workflow.

            Args:
                - task (Task): Task to add to the workflow.
                - tasks (List[Task]): List of tasks to add to the workflow (optional).

            """
            try:
                # Implementation of the function goes here
            except Exception as error:
                print(f"[ERROR][ConcurrentWorkflow] {error}")
                raise error

        def run(self, print_results=False, return_results=False):
            """
            Executes the tasks in parallel using a ThreadPoolExecutor.

            Args:
                - print_results (bool): Whether to print the results of each task. Default is False.
                - return_results (bool): Whether to return the results of each task. Default is False.

            Returns:
                - (List[Any]): A list of the results of each task, if return_results is True. Otherwise, returns None.

            """
            try:
                # Implementation of the function goes here
            except Exception as e:
                print(f"Task {task} generated an exception: {e}")

            return results if self.return_results else None

        def _execute_task(self, task):
            """Executes a task.

            Args:
                - task (Task): Task to execute.

            Returns:
                - result: The result of executing the task.

            """
            try:
                # Implementation of the function goes here
            except Exception as error:
                print(f"[ERROR][ConcurrentWorkflow] {error}")
                raise error

    # Usage example:

    from swarms.models import OpenAIChat
    from swarms.structs import ConcurrentWorkflow

    llm = OpenAIChat(openai_api_key="")
    workflow = ConcurrentWorkflow(max_workers=5)
    workflow.add("What's the weather in miami", llm)
    workflow.add("Create a report on these metrics", llm)
    workflow.run()
    workflow.tasks

    """
    ```