parent
58d9a36726
commit
b4d9e29e50
@ -0,0 +1,206 @@
|
|||||||
|
# AsyncWorkflow Documentation
|
||||||
|
|
||||||
|
The `AsyncWorkflow` class represents an asynchronous workflow designed to execute tasks concurrently. This class is ideal for scenarios where tasks need to be run asynchronously, leveraging Python's asyncio capabilities to manage multiple tasks efficiently.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Asynchronous Execution**: Tasks are run concurrently using asyncio, allowing for non-blocking operations.
|
||||||
|
- **Task Pool**: A collection of tasks to be executed within the workflow.
|
||||||
|
- **Event Loop**: The asyncio event loop that manages the execution of asynchronous tasks.
|
||||||
|
- **Stopping Condition**: A condition that, when met, stops the execution of the workflow.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|----------|------|---------|-------------|
|
||||||
|
| `name` | `str` | `"Async Workflow"` | The name of the workflow. |
|
||||||
|
| `description` | `str` | `"A workflow to run asynchronous tasks"` | The description of the workflow. |
|
||||||
|
| `max_loops` | `int` | `1` | The maximum number of loops to run the workflow. |
|
||||||
|
| `autosave` | `bool` | `True` | Flag indicating whether to autosave the results. |
|
||||||
|
| `dashboard` | `bool` | `False` | Flag indicating whether to display a dashboard. |
|
||||||
|
| `task_pool` | `List[Any]` | `[]` | The list of tasks in the workflow. |
|
||||||
|
| `results` | `List[Any]` | `[]` | The list of results from running the tasks. |
|
||||||
|
| `loop` | `Optional[asyncio.AbstractEventLoop]` | `None` | The event loop to use. |
|
||||||
|
| `stopping_condition` | `Optional[Callable]` | `None` | The stopping condition for the workflow. |
|
||||||
|
| `agents` | `List[Agent]` | `None` | A list of agents participating in the workflow. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `name` | `str` | The name of the workflow. |
|
||||||
|
| `description` | `str` | The description of the workflow. |
|
||||||
|
| `max_loops` | `int` | The maximum number of loops to run the workflow. |
|
||||||
|
| `autosave` | `bool` | Flag indicating whether to autosave the results. |
|
||||||
|
| `dashboard` | `bool` | Flag indicating whether to display a dashboard. |
|
||||||
|
| `task_pool` | `List[Any]` | The list of tasks in the workflow. |
|
||||||
|
| `results` | `List[Any]` | The list of results from running the tasks. |
|
||||||
|
| `loop` | `Optional[asyncio.AbstractEventLoop]` | The event loop to use. |
|
||||||
|
| `stopping_condition` | `Optional[Callable]` | The stopping condition for the workflow. |
|
||||||
|
| `agents` | `List[Agent]` | A list of agents participating in the workflow. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### add
|
||||||
|
|
||||||
|
Adds a task or a list of tasks to the task pool.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| `task` | `Any` | `None` | A single task to add. |
|
||||||
|
| `tasks` | `List[Any]` | `None` | A list of tasks to add. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If neither task nor tasks are provided.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = AsyncWorkflow()
|
||||||
|
task1 = Task(description="Task 1")
|
||||||
|
task2 = Task(description="Task 2")
|
||||||
|
|
||||||
|
# Adding a single task
|
||||||
|
await workflow.add(task=task1)
|
||||||
|
|
||||||
|
# Adding multiple tasks
|
||||||
|
await workflow.add(tasks=[task1, task2])
|
||||||
|
```
|
||||||
|
|
||||||
|
### delete
|
||||||
|
|
||||||
|
Deletes a task from the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| `task` | `Any` | `None` | A single task to delete. |
|
||||||
|
| `tasks` | `List[Task]` | `None` | A list of tasks to delete. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = AsyncWorkflow()
|
||||||
|
task1 = Task(description="Task 1")
|
||||||
|
task2 = Task(description="Task 2")
|
||||||
|
|
||||||
|
# Adding tasks to the workflow
|
||||||
|
await workflow.add(tasks=[task1, task2])
|
||||||
|
|
||||||
|
# Deleting a single task
|
||||||
|
await workflow.delete(task=task1)
|
||||||
|
|
||||||
|
# Deleting multiple tasks
|
||||||
|
await workflow.delete(tasks=[task1, task2])
|
||||||
|
```
|
||||||
|
|
||||||
|
### run
|
||||||
|
|
||||||
|
Runs the workflow and returns the results.
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `List[Any]` | The results of the executed tasks. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = AsyncWorkflow()
|
||||||
|
task1 = Task(description="Task 1", execute=async_function)
|
||||||
|
task2 = Task(description="Task 2", execute=async_function)
|
||||||
|
|
||||||
|
# Adding tasks to the workflow
|
||||||
|
await workflow.add(tasks=[task1, task2])
|
||||||
|
|
||||||
|
# Running the workflow
|
||||||
|
results = await workflow.run()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Additional Examples
|
||||||
|
|
||||||
|
#### Example 1: Simple AsyncWorkflow
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.task import Task
|
||||||
|
|
||||||
|
async def simple_task():
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return "Task Completed"
|
||||||
|
|
||||||
|
workflow = AsyncWorkflow()
|
||||||
|
task = Task(description="Simple Task", execute=simple_task)
|
||||||
|
|
||||||
|
# Adding a task to the workflow
|
||||||
|
await workflow.add(task=task)
|
||||||
|
|
||||||
|
# Running the workflow
|
||||||
|
results = await workflow.run()
|
||||||
|
print(results) # Output: ["Task Completed"]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 2: Workflow with Multiple Tasks
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.task import Task
|
||||||
|
|
||||||
|
async def task1():
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return "Task 1 Completed"
|
||||||
|
|
||||||
|
async def task2():
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
return "Task 2 Completed"
|
||||||
|
|
||||||
|
workflow = AsyncWorkflow()
|
||||||
|
task_1 = Task(description="Task 1", execute=task1)
|
||||||
|
task_2 = Task(description="Task 2", execute=task2)
|
||||||
|
|
||||||
|
# Adding tasks to the workflow
|
||||||
|
await workflow.add(tasks=[task_1, task_2])
|
||||||
|
|
||||||
|
# Running the workflow
|
||||||
|
results = await workflow.run()
|
||||||
|
print(results) # Output: ["Task 1 Completed", "Task 2 Completed"]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 3: Workflow with Stopping Condition
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.task import Task
|
||||||
|
|
||||||
|
async def task1():
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
return "Task 1 Completed"
|
||||||
|
|
||||||
|
async def task2():
|
||||||
|
await asyncio.sleep(2)
|
||||||
|
return "Task 2 Completed"
|
||||||
|
|
||||||
|
def stop_condition(results):
|
||||||
|
return "Task 2 Completed" in results
|
||||||
|
|
||||||
|
workflow = AsyncWorkflow(stopping_condition=stop_condition)
|
||||||
|
task_1 = Task(description="Task 1", execute=task1)
|
||||||
|
task_2 = Task(description="Task 2", execute=task2)
|
||||||
|
|
||||||
|
# Adding tasks to the workflow
|
||||||
|
await workflow.add(tasks=[task_1, task_2])
|
||||||
|
|
||||||
|
# Running the workflow
|
||||||
|
results = await workflow.run()
|
||||||
|
print(results) # Output: ["Task 1 Completed", "Task 2 Completed"]
|
||||||
|
```
|
@ -0,0 +1,191 @@
|
|||||||
|
# AutoSwarm
|
||||||
|
|
||||||
|
The `AutoSwarm` class represents a swarm of agents that can be created and managed automatically. This class leverages the `AutoSwarmRouter` to route tasks to appropriate swarms and supports custom preprocessing, routing, and postprocessing of tasks. It is designed to handle complex workflows efficiently.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Swarm**: A group of agents working together to complete tasks.
|
||||||
|
- **Routing**: Directing tasks to the appropriate swarm based on specific criteria.
|
||||||
|
- **Preprocessing and Postprocessing**: Customizable functions to handle tasks before and after routing.
|
||||||
|
- **Event Loop**: Managing the execution of tasks in a loop.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|---------------------|-------------------------------|-----------|-------------|
|
||||||
|
| `name` | `Optional[str]` | `None` | The name of the swarm. |
|
||||||
|
| `description` | `Optional[str]` | `None` | The description of the swarm. |
|
||||||
|
| `verbose` | `bool` | `False` | Whether to enable verbose mode. |
|
||||||
|
| `custom_params` | `Optional[Dict[str, Any]]` | `None` | Custom parameters for the swarm. |
|
||||||
|
| `custom_preprocess` | `Optional[Callable]` | `None` | Custom preprocessing function for tasks. |
|
||||||
|
| `custom_postprocess`| `Optional[Callable]` | `None` | Custom postprocessing function for task results. |
|
||||||
|
| `custom_router` | `Optional[Callable]` | `None` | Custom routing function for tasks. |
|
||||||
|
| `max_loops` | `int` | `1` | The maximum number of loops to run the workflow. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|----------------------|-------------------------------|-------------|
|
||||||
|
| `name` | `Optional[str]` | The name of the swarm. |
|
||||||
|
| `description` | `Optional[str]` | The description of the swarm. |
|
||||||
|
| `verbose` | `bool` | Whether to enable verbose mode. |
|
||||||
|
| `custom_params` | `Optional[Dict[str, Any]]` | Custom parameters for the swarm. |
|
||||||
|
| `custom_preprocess` | `Optional[Callable]` | Custom preprocessing function for tasks. |
|
||||||
|
| `custom_postprocess` | `Optional[Callable]` | Custom postprocessing function for task results. |
|
||||||
|
| `custom_router` | `Optional[Callable]` | Custom routing function for tasks. |
|
||||||
|
| `max_loops` | `int` | The maximum number of loops to run the workflow. |
|
||||||
|
| `router` | `AutoSwarmRouter` | The router for managing task routing. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### init_logging
|
||||||
|
|
||||||
|
Initializes logging for the `AutoSwarm`.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
swarm = AutoSwarm(name="example_swarm", verbose=True)
|
||||||
|
swarm.init_logging()
|
||||||
|
```
|
||||||
|
|
||||||
|
### run
|
||||||
|
|
||||||
|
Runs the swarm simulation.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|---------|---------|-------------|
|
||||||
|
| `task` | `str` | `None` | The task to be executed. |
|
||||||
|
| `*args` | | | Additional arguments. |
|
||||||
|
| `**kwargs`| | | Additional keyword arguments. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Any` | The result of the executed task. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `Exception`: If any error occurs during task execution.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
swarm = AutoSwarm(name="example_swarm", max_loops=3)
|
||||||
|
result = swarm.run(task="example_task")
|
||||||
|
print(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
### list_all_swarms
|
||||||
|
|
||||||
|
Lists all available swarms and their descriptions.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
swarm = AutoSwarm(name="example_swarm", max_loops=3)
|
||||||
|
swarm.list_all_swarms()
|
||||||
|
# Output:
|
||||||
|
# INFO: Swarm Name: swarm1 || Swarm Description: Description of swarm1
|
||||||
|
# INFO: Swarm Name: swarm2 || Swarm Description: Description of swarm2
|
||||||
|
```
|
||||||
|
|
||||||
|
### Additional Examples
|
||||||
|
|
||||||
|
#### Example 1: Custom Preprocessing and Postprocessing
|
||||||
|
|
||||||
|
```python
|
||||||
|
def custom_preprocess(task, *args, **kwargs):
|
||||||
|
# Custom preprocessing logic
|
||||||
|
task = task.upper()
|
||||||
|
return task, args, kwargs
|
||||||
|
|
||||||
|
def custom_postprocess(result):
|
||||||
|
# Custom postprocessing logic
|
||||||
|
return result.lower()
|
||||||
|
|
||||||
|
swarm = AutoSwarm(
|
||||||
|
name="example_swarm",
|
||||||
|
custom_preprocess=custom_preprocess,
|
||||||
|
custom_postprocess=custom_postprocess,
|
||||||
|
max_loops=3
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with custom preprocessing and postprocessing
|
||||||
|
result = swarm.run(task="example_task")
|
||||||
|
print(result) # Output will be the processed result
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 2: Custom Router Function
|
||||||
|
|
||||||
|
```python
|
||||||
|
def custom_router(swarm, task, *args, **kwargs):
|
||||||
|
# Custom routing logic
|
||||||
|
if "specific" in task:
|
||||||
|
return swarm.router.swarm_dict["specific_swarm"].run(task, *args, **kwargs)
|
||||||
|
return swarm.router.swarm_dict["default_swarm"].run(task, *args, **kwargs)
|
||||||
|
|
||||||
|
swarm = AutoSwarm(
|
||||||
|
name="example_swarm",
|
||||||
|
custom_router=custom_router,
|
||||||
|
max_loops=3
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with custom routing
|
||||||
|
result = swarm.run(task="specific_task")
|
||||||
|
print(result) # Output will be the result of the routed task
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 3: Verbose Mode
|
||||||
|
|
||||||
|
```python
|
||||||
|
swarm = AutoSwarm(
|
||||||
|
name="example_swarm",
|
||||||
|
verbose=True,
|
||||||
|
max_loops=3
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with verbose mode enabled
|
||||||
|
result = swarm.run(task="example_task")
|
||||||
|
# Output will include detailed logs of the task execution process
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Full Example 4:
|
||||||
|
First create a class with BaseSwarm -> Then wrap it in the router -> then pass that to the `AutoSwarm`
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms import BaseSwarm, AutoSwarmRouter, AutoSwarm
|
||||||
|
|
||||||
|
|
||||||
|
class FinancialReportSummarization(BaseSwarm):
|
||||||
|
def __init__(self, name: str = None, *args, **kwargs):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def run(self, task, *args, **kwargs):
|
||||||
|
return task
|
||||||
|
|
||||||
|
|
||||||
|
# Add swarm to router
|
||||||
|
router = AutoSwarmRouter(swarms=[FinancialReportSummarization])
|
||||||
|
|
||||||
|
# Create AutoSwarm Instance
|
||||||
|
autoswarm = AutoSwarm(
|
||||||
|
name="kyegomez/FinancialReportSummarization",
|
||||||
|
description="A swarm for financial document summarizing and generation",
|
||||||
|
verbose=True,
|
||||||
|
router=router,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run the AutoSwarm
|
||||||
|
autoswarm.run("Analyze these documents and give me a summary:")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The `AutoSwarm` class provides a robust framework for managing and executing tasks using a swarm of agents. With customizable preprocessing, routing, and postprocessing functions, it is highly adaptable to various workflows and can handle complex task execution scenarios efficiently. The integration with `AutoSwarmRouter` enhances its flexibility, making it a powerful tool for dynamic task management.
|
@ -0,0 +1,165 @@
|
|||||||
|
# AutoSwarmRouter
|
||||||
|
|
||||||
|
The `AutoSwarmRouter` class is designed to route tasks to the appropriate swarm based on the provided name. This class allows for customization of preprocessing, routing, and postprocessing of tasks, making it highly adaptable to various workflows and requirements.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Routing**: Directing tasks to the appropriate swarm based on specific criteria.
|
||||||
|
- **Preprocessing and Postprocessing**: Customizable functions to handle tasks before and after routing.
|
||||||
|
- **Swarms**: Collections of `BaseSwarm` objects that perform the tasks.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|--------------------|----------------------------------|-----------|-------------|
|
||||||
|
| `name` | `Optional[str]` | `None` | The name of the router. |
|
||||||
|
| `description` | `Optional[str]` | `None` | The description of the router. |
|
||||||
|
| `verbose` | `bool` | `False` | Whether to enable verbose mode. |
|
||||||
|
| `custom_params` | `Optional[Dict[str, Any]]` | `None` | Custom parameters for the router. |
|
||||||
|
| `swarms` | `Sequence[BaseSwarm]` | `None` | A list of `BaseSwarm` objects. |
|
||||||
|
| `custom_preprocess`| `Optional[Callable]` | `None` | Custom preprocessing function for tasks. |
|
||||||
|
| `custom_postprocess`| `Optional[Callable]` | `None` | Custom postprocessing function for task results. |
|
||||||
|
| `custom_router` | `Optional[Callable]` | `None` | Custom routing function for tasks. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|----------------------|----------------------------------|-------------|
|
||||||
|
| `name` | `Optional[str]` | The name of the router. |
|
||||||
|
| `description` | `Optional[str]` | The description of the router. |
|
||||||
|
| `verbose` | `bool` | Whether to enable verbose mode. |
|
||||||
|
| `custom_params` | `Optional[Dict[str, Any]]` | Custom parameters for the router. |
|
||||||
|
| `swarms` | `Sequence[BaseSwarm]` | A list of `BaseSwarm` objects. |
|
||||||
|
| `custom_preprocess` | `Optional[Callable]` | Custom preprocessing function for tasks. |
|
||||||
|
| `custom_postprocess` | `Optional[Callable]` | Custom postprocessing function for task results. |
|
||||||
|
| `custom_router` | `Optional[Callable]` | Custom routing function for tasks. |
|
||||||
|
| `swarm_dict` | `Dict[str, BaseSwarm]` | A dictionary of swarms keyed by their name. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### run
|
||||||
|
|
||||||
|
Executes the swarm simulation and routes the task to the appropriate swarm.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|---------|---------|-------------|
|
||||||
|
| `task` | `str` | `None` | The task to be executed. |
|
||||||
|
| `*args` | | | Additional arguments. |
|
||||||
|
| `**kwargs`| | | Additional keyword arguments. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Any` | The result of the routed task. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If the specified swarm is not found.
|
||||||
|
- `Exception`: If any error occurs during task routing or execution.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
router = AutoSwarmRouter(name="example_router", swarms=[swarm1, swarm2])
|
||||||
|
|
||||||
|
# Running a task
|
||||||
|
result = router.run(task="example_task")
|
||||||
|
```
|
||||||
|
|
||||||
|
### len_of_swarms
|
||||||
|
|
||||||
|
Prints the number of swarms available in the router.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
router = AutoSwarmRouter(name="example_router", swarms=[swarm1, swarm2])
|
||||||
|
|
||||||
|
# Printing the number of swarms
|
||||||
|
router.len_of_swarms() # Output: 2
|
||||||
|
```
|
||||||
|
|
||||||
|
### list_available_swarms
|
||||||
|
|
||||||
|
Logs the available swarms and their descriptions.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
router = AutoSwarmRouter(name="example_router", swarms=[swarm1, swarm2])
|
||||||
|
|
||||||
|
# Listing available swarms
|
||||||
|
router.list_available_swarms()
|
||||||
|
# Output:
|
||||||
|
# INFO: Swarm Name: swarm1 || Swarm Description: Description of swarm1
|
||||||
|
# INFO: Swarm Name: swarm2 || Swarm Description: Description of swarm2
|
||||||
|
```
|
||||||
|
|
||||||
|
### Additional Examples
|
||||||
|
|
||||||
|
#### Example 1: Custom Preprocessing and Postprocessing
|
||||||
|
|
||||||
|
```python
|
||||||
|
def custom_preprocess(task, *args, **kwargs):
|
||||||
|
# Custom preprocessing logic
|
||||||
|
task = task.upper()
|
||||||
|
return task, args, kwargs
|
||||||
|
|
||||||
|
def custom_postprocess(result):
|
||||||
|
# Custom postprocessing logic
|
||||||
|
return result.lower()
|
||||||
|
|
||||||
|
router = AutoSwarmRouter(
|
||||||
|
name="example_router",
|
||||||
|
swarms=[swarm1, swarm2],
|
||||||
|
custom_preprocess=custom_preprocess,
|
||||||
|
custom_postprocess=custom_postprocess
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with custom preprocessing and postprocessing
|
||||||
|
result = router.run(task="example_task")
|
||||||
|
print(result) # Output will be the processed result
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 2: Custom Router Function
|
||||||
|
|
||||||
|
```python
|
||||||
|
def custom_router(router, task, *args, **kwargs):
|
||||||
|
# Custom routing logic
|
||||||
|
if "specific" in task:
|
||||||
|
return router.swarm_dict["specific_swarm"].run(task, *args, **kwargs)
|
||||||
|
return router.swarm_dict["default_swarm"].run(task, *args, **kwargs)
|
||||||
|
|
||||||
|
router = AutoSwarmRouter(
|
||||||
|
name="example_router",
|
||||||
|
swarms=[default_swarm, specific_swarm],
|
||||||
|
custom_router=custom_router
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with custom routing
|
||||||
|
result = router.run(task="specific_task")
|
||||||
|
print(result) # Output will be the result of the routed task
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 3: Verbose Mode
|
||||||
|
|
||||||
|
```python
|
||||||
|
router = AutoSwarmRouter(
|
||||||
|
name="example_router",
|
||||||
|
swarms=[swarm1, swarm2],
|
||||||
|
verbose=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Running a task with verbose mode enabled
|
||||||
|
result = router.run(task="example_task")
|
||||||
|
# Output will include detailed logs of the task routing and execution process
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The `AutoSwarmRouter` class provides a flexible and customizable approach to routing tasks to appropriate swarms, supporting custom preprocessing, routing, and postprocessing functions. This makes it a powerful tool for managing complex workflows that require dynamic task handling and execution.
|
@ -0,0 +1,287 @@
|
|||||||
|
# BaseWorkflow
|
||||||
|
|
||||||
|
The `BaseWorkflow` class serves as a foundational structure for defining and managing workflows. It allows users to add, remove, update, and manage tasks and agents within a workflow, offering flexibility and extensibility for various applications.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Agents**: Entities participating in the workflow.
|
||||||
|
- **Tasks**: Units of work to be executed within the workflow.
|
||||||
|
- **Models**: Computational models used within the workflow.
|
||||||
|
- **Workflow State**: The state of the workflow, which can be saved and restored.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|----------|------|---------|-------------|
|
||||||
|
| `agents` | `List[Agent]` | `None` | A list of agents participating in the workflow. |
|
||||||
|
| `task_pool` | `List[Task]` | `None` | A list of tasks in the workflow. |
|
||||||
|
| `models` | `List[Any]` | `None` | A list of models used in the workflow. |
|
||||||
|
| `*args` | | | Variable length argument list. |
|
||||||
|
| `**kwargs` | | | Arbitrary keyword arguments. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `agents` | `List[Agent]` | A list of agents participating in the workflow. |
|
||||||
|
| `task_pool` | `List[Task]` | A list of tasks in the workflow. |
|
||||||
|
| `models` | `List[Any]` | A list of models used in the workflow. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### add_task
|
||||||
|
|
||||||
|
Adds a task or a list of tasks to the task pool.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| `task` | `Task` | `None` | A single task to add. |
|
||||||
|
| `tasks` | `List[Task]` | `None` | A list of tasks to add. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If neither task nor tasks are provided.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
task1 = Task(description="Task 1")
|
||||||
|
task2 = Task(description="Task 2")
|
||||||
|
|
||||||
|
# Adding a single task
|
||||||
|
workflow.add_task(task=task1)
|
||||||
|
|
||||||
|
# Adding multiple tasks
|
||||||
|
workflow.add_task(tasks=[task1, task2])
|
||||||
|
```
|
||||||
|
|
||||||
|
### add_agent
|
||||||
|
|
||||||
|
Adds an agent to the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `agent` | `Agent` | The agent to add to the workflow. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
agent = Agent(name="Agent 1")
|
||||||
|
|
||||||
|
# Adding an agent to the workflow
|
||||||
|
workflow.add_agent(agent=agent)
|
||||||
|
```
|
||||||
|
|
||||||
|
### run
|
||||||
|
|
||||||
|
Abstract method to run the workflow.
|
||||||
|
|
||||||
|
### __sequential_loop
|
||||||
|
|
||||||
|
Abstract method for the sequential loop.
|
||||||
|
|
||||||
|
### __log
|
||||||
|
|
||||||
|
Logs a message if verbose mode is enabled.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `message` | `str` | The message to log. |
|
||||||
|
|
||||||
|
### __str__
|
||||||
|
|
||||||
|
Returns a string representation of the workflow.
|
||||||
|
|
||||||
|
### __repr__
|
||||||
|
|
||||||
|
Returns a string representation of the workflow for debugging.
|
||||||
|
|
||||||
|
### reset
|
||||||
|
|
||||||
|
Resets the workflow by clearing the results of each task.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.reset()
|
||||||
|
```
|
||||||
|
|
||||||
|
### get_task_results
|
||||||
|
|
||||||
|
Returns the results of each task in the workflow.
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Dict[str, Any]` | The results of each task in the workflow. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
results = workflow.get_task_results()
|
||||||
|
```
|
||||||
|
|
||||||
|
### remove_task
|
||||||
|
|
||||||
|
Removes a task from the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The description of the task to remove. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.remove_task(task="Task 1")
|
||||||
|
```
|
||||||
|
|
||||||
|
### update_task
|
||||||
|
|
||||||
|
Updates the arguments of a task in the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The description of the task to update. |
|
||||||
|
| `**updates` | | The updates to apply to the task. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If the task is not found in the workflow.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
task = Task(description="Task 1", kwargs={"param": 1})
|
||||||
|
|
||||||
|
# Adding a task to the workflow
|
||||||
|
workflow.add_task(task=task)
|
||||||
|
|
||||||
|
# Updating the task
|
||||||
|
workflow.update_task("Task 1", param=2)
|
||||||
|
```
|
||||||
|
|
||||||
|
### delete_task
|
||||||
|
|
||||||
|
Deletes a task from the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The description of the task to delete. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If the task is not found in the workflow.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
task = Task(description="Task 1")
|
||||||
|
|
||||||
|
# Adding a task to the workflow
|
||||||
|
workflow.add_task(task=task)
|
||||||
|
|
||||||
|
# Deleting the task
|
||||||
|
workflow.delete_task("Task 1")
|
||||||
|
```
|
||||||
|
|
||||||
|
### save_workflow_state
|
||||||
|
|
||||||
|
Saves the workflow state to a json file.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| `filepath` | `Optional[str]` | `"sequential_workflow_state.json"` | The path to save the workflow state to. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.save_workflow_state(filepath="workflow_state.json")
|
||||||
|
```
|
||||||
|
|
||||||
|
### add_objective_to_workflow
|
||||||
|
|
||||||
|
Adds an objective to the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The description of the task. |
|
||||||
|
| `**kwargs` | | Additional keyword arguments for the task. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.add_objective_to_workflow(task="New Objective", agent=agent, args=[], kwargs={})
|
||||||
|
```
|
||||||
|
|
||||||
|
### load_workflow_state
|
||||||
|
|
||||||
|
Loads the workflow state from a json file and restores the workflow state.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Default | Description |
|
||||||
|
|-----------|------|---------|-------------|
|
||||||
|
| `filepath` | `str` | `None` | The path to load the workflow state from. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.load_workflow_state(filepath="workflow_state.json")
|
||||||
|
```
|
||||||
|
|
||||||
|
### workflow_dashboard
|
||||||
|
|
||||||
|
Displays a dashboard for the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `**kwargs` | | Additional keyword arguments to pass to the dashboard. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.workflow_dashboard()
|
||||||
|
```
|
||||||
|
|
||||||
|
### workflow_bootup
|
||||||
|
|
||||||
|
Initializes the workflow.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
workflow = BaseWorkflow()
|
||||||
|
workflow.workflow_bootup()
|
||||||
|
```
|
@ -0,0 +1,238 @@
|
|||||||
|
# GroupChat
|
||||||
|
|
||||||
|
The `GroupChat` class is designed to manage a group chat session involving multiple agents. This class handles initializing the conversation, selecting the next speaker, resetting the chat, and executing the chat rounds, providing a structured approach to managing a dynamic and interactive conversation.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Agents**: Entities participating in the group chat.
|
||||||
|
- **Conversation Management**: Handling the flow of conversation, selecting speakers, and maintaining chat history.
|
||||||
|
- **Round-based Execution**: Managing the chat in predefined rounds.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|---------------------|----------------------|-------------|-------------|
|
||||||
|
| `agents` | `List[Agent]` | `None` | List of agents participating in the group chat. |
|
||||||
|
| `max_rounds` | `int` | `10` | Maximum number of chat rounds. |
|
||||||
|
| `admin_name` | `str` | `"Admin"` | Name of the admin user. |
|
||||||
|
| `group_objective` | `str` | `None` | Objective of the group chat. |
|
||||||
|
| `selector_agent` | `Agent` | `None` | Agent responsible for selecting the next speaker. |
|
||||||
|
| `rules` | `str` | `None` | Rules for the group chat. |
|
||||||
|
| `*args` | | | Variable length argument list. |
|
||||||
|
| `**kwargs` | | | Arbitrary keyword arguments. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|---------------------|----------------------|-------------|
|
||||||
|
| `agents` | `List[Agent]` | List of agents participating in the group chat. |
|
||||||
|
| `max_rounds` | `int` | Maximum number of chat rounds. |
|
||||||
|
| `admin_name` | `str` | Name of the admin user. |
|
||||||
|
| `group_objective` | `str` | Objective of the group chat. |
|
||||||
|
| `selector_agent` | `Agent` | Agent responsible for selecting the next speaker. |
|
||||||
|
| `messages` | `Conversation` | Conversation object for storing the chat messages. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### __init__
|
||||||
|
|
||||||
|
Initializes the group chat with the given parameters.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
agents = [Agent(name="Agent 1"), Agent(name="Agent 2")]
|
||||||
|
group_chat = GroupChat(agents=agents, max_rounds=5, admin_name="GroupAdmin")
|
||||||
|
```
|
||||||
|
|
||||||
|
### agent_names
|
||||||
|
|
||||||
|
Returns the names of the agents in the group chat.
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `List[str]` | List of agent names. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
names = group_chat.agent_names
|
||||||
|
print(names) # Output: ['Agent 1', 'Agent 2']
|
||||||
|
```
|
||||||
|
|
||||||
|
### reset
|
||||||
|
|
||||||
|
Resets the group chat by clearing the message history.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
group_chat.reset()
|
||||||
|
```
|
||||||
|
|
||||||
|
### agent_by_name
|
||||||
|
|
||||||
|
Finds an agent whose name is contained within the given name string.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|--------|-------------|
|
||||||
|
| `name` | `str` | Name string to search for. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Agent` | Agent object with a name contained in the given name string. |
|
||||||
|
|
||||||
|
**Raises:**
|
||||||
|
|
||||||
|
- `ValueError`: If no agent is found with a name contained in the given name string.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
agent = group_chat.agent_by_name("Agent 1")
|
||||||
|
print(agent.agent_name) # Output: 'Agent 1'
|
||||||
|
```
|
||||||
|
|
||||||
|
### next_agent
|
||||||
|
|
||||||
|
Returns the next agent in the list.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|--------|-------------|
|
||||||
|
| `agent` | `Agent`| Current agent. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Agent` | Next agent in the list. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
current_agent = group_chat.agents[0]
|
||||||
|
next_agent = group_chat.next_agent(current_agent)
|
||||||
|
print(next_agent.agent_name) # Output: Name of the next agent
|
||||||
|
```
|
||||||
|
|
||||||
|
### select_speaker_msg
|
||||||
|
|
||||||
|
Returns the message for selecting the next speaker.
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `str` | Prompt message for selecting the next speaker. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
message = group_chat.select_speaker_msg()
|
||||||
|
print(message)
|
||||||
|
```
|
||||||
|
|
||||||
|
### select_speaker
|
||||||
|
|
||||||
|
Selects the next speaker.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|----------------------|--------|-------------|
|
||||||
|
| `last_speaker_agent` | `Agent`| Last speaker in the conversation. |
|
||||||
|
| `selector_agent` | `Agent`| Agent responsible for selecting the next speaker. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Agent` | Next speaker. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
next_speaker = group_chat.select_speaker(last_speaker_agent, selector_agent)
|
||||||
|
print(next_speaker.agent_name)
|
||||||
|
```
|
||||||
|
|
||||||
|
### _participant_roles
|
||||||
|
|
||||||
|
Returns the roles of the participants.
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `str` | Participant roles. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
roles = group_chat._participant_roles()
|
||||||
|
print(roles)
|
||||||
|
```
|
||||||
|
|
||||||
|
### __call__
|
||||||
|
|
||||||
|
Executes the group chat as a function.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|--------|-------------|
|
||||||
|
| `task` | `str` | Task to be performed. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `str` | Reply from the last speaker. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
response = group_chat(task="Discuss the project plan")
|
||||||
|
print(response)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Additional Examples
|
||||||
|
|
||||||
|
#### Example 1: Initializing and Running a Group Chat
|
||||||
|
|
||||||
|
```python
|
||||||
|
agents = [Agent(name="Agent 1"), Agent(name="Agent 2"), Agent(name="Agent 3")]
|
||||||
|
selector_agent = Agent(name="Selector")
|
||||||
|
group_chat = GroupChat(agents=agents, selector_agent=selector_agent, max_rounds=3, group_objective="Discuss the quarterly goals.")
|
||||||
|
|
||||||
|
response = group_chat(task="Let's start the discussion on quarterly goals.")
|
||||||
|
print(response)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 2: Resetting the Group Chat
|
||||||
|
|
||||||
|
```python
|
||||||
|
group_chat.reset()
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 3: Selecting the Next Speaker
|
||||||
|
|
||||||
|
```python
|
||||||
|
last_speaker = group_chat.agents[0]
|
||||||
|
next_speaker = group_chat.select_speaker(last_speaker_agent=last_speaker, selector_agent=selector_agent)
|
||||||
|
print(next_speaker.agent_name)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The `GroupChat` class offers a structured approach to managing a group chat involving multiple agents. With functionalities for initializing conversations, selecting speakers, and handling chat rounds, it provides a robust framework for dynamic and interactive discussions. This makes it an essential tool for applications requiring coordinated communication among multiple agents.
|
@ -0,0 +1,204 @@
|
|||||||
|
# MultiProcessWorkflow Documentation
|
||||||
|
|
||||||
|
The `MultiProcessWorkflow` class extends the `BaseWorkflow` to support parallel processing using multiple workers. This class is designed to efficiently execute tasks concurrently, leveraging the power of multi-processing to enhance performance and scalability.
|
||||||
|
|
||||||
|
### Key Concepts
|
||||||
|
|
||||||
|
- **Parallel Processing**: Utilizing multiple workers to execute tasks concurrently.
|
||||||
|
- **Workflow Management**: Handling the execution of tasks in a structured workflow.
|
||||||
|
- **Agents**: Entities responsible for executing tasks.
|
||||||
|
|
||||||
|
## Attributes
|
||||||
|
|
||||||
|
### Arguments
|
||||||
|
|
||||||
|
| Argument | Type | Default | Description |
|
||||||
|
|--------------|---------------------|---------|-------------|
|
||||||
|
| `max_workers`| `int` | `5` | The maximum number of workers to use for parallel processing. |
|
||||||
|
| `autosave` | `bool` | `True` | Flag indicating whether to automatically save the workflow. |
|
||||||
|
| `agents` | `Sequence[Agent]` | `None` | A list of agents participating in the workflow. |
|
||||||
|
| `*args` | | | Additional positional arguments. |
|
||||||
|
| `**kwargs` | | | Additional keyword arguments. |
|
||||||
|
|
||||||
|
### Attributes
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|--------------|---------------------|-------------|
|
||||||
|
| `max_workers`| `int` | The maximum number of workers to use for parallel processing. |
|
||||||
|
| `autosave` | `bool` | Flag indicating whether to automatically save the workflow. |
|
||||||
|
| `agents` | `Sequence[Agent]` | A list of agents participating in the workflow. |
|
||||||
|
|
||||||
|
## Methods
|
||||||
|
|
||||||
|
### __init__
|
||||||
|
|
||||||
|
Initializes the `MultiProcessWorkflow` with the given parameters.
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.task import Task
|
||||||
|
from swarms.structs.multi_process_workflow import MultiProcessWorkflow
|
||||||
|
|
||||||
|
agents = [Agent(name="Agent 1"), Agent(name="Agent 2")]
|
||||||
|
tasks = [Task(name="Task 1", execute=lambda: "result1"), Task(name="Task 2", execute=lambda: "result2")]
|
||||||
|
|
||||||
|
workflow = MultiProcessWorkflow(max_workers=3, agents=agents, tasks=tasks)
|
||||||
|
```
|
||||||
|
|
||||||
|
### execute_task
|
||||||
|
|
||||||
|
Executes a task and handles exceptions.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The task to execute. |
|
||||||
|
| `*args` | | Additional positional arguments for the task execution. |
|
||||||
|
| `**kwargs`| | Additional keyword arguments for the task execution. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `Any` | The result of the task execution. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = workflow.execute_task(task="Sample Task")
|
||||||
|
print(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
### run
|
||||||
|
|
||||||
|
Runs the workflow.
|
||||||
|
|
||||||
|
**Arguments:**
|
||||||
|
|
||||||
|
| Parameter | Type | Description |
|
||||||
|
|-----------|------|-------------|
|
||||||
|
| `task` | `str` | The task to run. |
|
||||||
|
| `*args` | | Additional positional arguments for the task execution. |
|
||||||
|
| `**kwargs`| | Additional keyword arguments for the task execution. |
|
||||||
|
|
||||||
|
**Returns:**
|
||||||
|
|
||||||
|
| Return Type | Description |
|
||||||
|
|-------------|-------------|
|
||||||
|
| `List[Any]` | The results of all executed tasks. |
|
||||||
|
|
||||||
|
**Examples:**
|
||||||
|
|
||||||
|
```python
|
||||||
|
results = workflow.run(task="Sample Task")
|
||||||
|
print(results)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Additional Examples
|
||||||
|
|
||||||
|
#### Example 1: Simple Task Execution
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms import Agent, Task, MultiProcessWorkflow, OpenAIChat
|
||||||
|
from datetime import datetime
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load the environment variables
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
|
||||||
|
# Define a function to be used as the action
|
||||||
|
def my_action():
|
||||||
|
print("Action executed")
|
||||||
|
|
||||||
|
|
||||||
|
# Define a function to be used as the condition
|
||||||
|
def my_condition():
|
||||||
|
print("Condition checked")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# Create an agent
|
||||||
|
agent = Agent(
|
||||||
|
llm=OpenAIChat(openai_api_key=os.environ["OPENAI_API_KEY"]),
|
||||||
|
max_loops=1,
|
||||||
|
dashboard=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a task
|
||||||
|
task = Task(
|
||||||
|
description=(
|
||||||
|
"Generate a report on the top 3 biggest expenses for small"
|
||||||
|
" businesses and how businesses can save 20%"
|
||||||
|
),
|
||||||
|
agent=agent,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a workflow with the task
|
||||||
|
workflow = MultiProcessWorkflow(tasks=[task])
|
||||||
|
|
||||||
|
# Run the workflow
|
||||||
|
results = workflow.run(task)
|
||||||
|
print(results)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 2: Workflow with Multiple Agents
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms import Agent, Task, MultiProcessWorkflow
|
||||||
|
|
||||||
|
# Define tasks
|
||||||
|
def task1():
|
||||||
|
return "Task 1 result"
|
||||||
|
|
||||||
|
def task2():
|
||||||
|
return "Task 2 result"
|
||||||
|
|
||||||
|
# Create agents
|
||||||
|
agent1 = Agent(name="Agent 1", llm=OpenAIChat())
|
||||||
|
agent2 = Agent(name="Agent 2", llm=OpenAIChat())
|
||||||
|
|
||||||
|
# Create tasks
|
||||||
|
task_1 = Task(name="Task 1", execute=task1)
|
||||||
|
task_2 = Task(name="Task 2", execute=task2)
|
||||||
|
|
||||||
|
# Create a workflow
|
||||||
|
workflow = MultiProcessWorkflow(agents=[agent1, agent2], tasks=[task_1, task_2])
|
||||||
|
|
||||||
|
# Run the workflow
|
||||||
|
results = workflow.run(task="Example Task")
|
||||||
|
print(results)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Example 3: Customizing Max Workers
|
||||||
|
|
||||||
|
```python
|
||||||
|
from swarms import Agent, Task, MultiProcessWorkflow, OpenAIChat
|
||||||
|
|
||||||
|
# Define a task
|
||||||
|
def example_task():
|
||||||
|
return "Task result"
|
||||||
|
|
||||||
|
# Create an agent
|
||||||
|
agent = Agent(name="Agent 1", llm=OpenAIChat())
|
||||||
|
|
||||||
|
# Create a task
|
||||||
|
task = Task(name="Example Task", execute=example_task)
|
||||||
|
|
||||||
|
# Create a workflow with custom max workers
|
||||||
|
workflow = MultiProcessWorkflow(max_workers=10, agents=[agent], tasks=[task])
|
||||||
|
|
||||||
|
# Run the workflow
|
||||||
|
results = workflow.run(task="Example Task")
|
||||||
|
print(results)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
The `MultiProcessWorkflow` class provides a powerful framework for managing and executing tasks using multiple workers. With support for parallel processing, customizable workflows, and detailed logging, it is an ideal tool for complex task execution scenarios. This class enhances performance and scalability, making it suitable for a wide range of applications that require efficient task management.
|
Loading…
Reference in new issue