parent
e98d618bd8
commit
1cc69ba925
@ -1,266 +1,181 @@
|
|||||||
# AsyncWorkflow Documentation
|
# 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.
|
The `AsyncWorkflow` class represents an asynchronous workflow that executes tasks concurrently using multiple agents. It allows for efficient task management, leveraging Python's `asyncio` for concurrent execution.
|
||||||
|
|
||||||
### Key Concepts
|
## Key Features
|
||||||
|
- **Concurrent Task Execution**: Distribute tasks across multiple agents asynchronously.
|
||||||
|
- **Configurable Workers**: Limit the number of concurrent workers (agents) for better resource management.
|
||||||
|
- **Autosave Results**: Optionally save the task execution results automatically.
|
||||||
|
- **Verbose Logging**: Enable detailed logging to monitor task execution.
|
||||||
|
- **Error Handling**: Gracefully handles exceptions raised by agents during task execution.
|
||||||
|
|
||||||
- **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
|
## 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 |
|
| Attribute | Type | Description |
|
||||||
|-----------|------|-------------|
|
|-------------------|---------------------|-----------------------------------------------------------------------------|
|
||||||
| `name` | `str` | The name of the workflow. |
|
| `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. |
|
| `agents` | `List[Agent]` | A list of agents participating in the workflow. |
|
||||||
|
| `max_workers` | `int` | The maximum number of concurrent workers (default: 5). |
|
||||||
## Methods
|
| `dashboard` | `bool` | Whether to display a dashboard (currently not implemented). |
|
||||||
|
| `autosave` | `bool` | Whether to autosave task results (default: `False`). |
|
||||||
### add
|
| `verbose` | `bool` | Whether to enable detailed logging (default: `False`). |
|
||||||
|
| `task_pool` | `List` | A pool of tasks to be executed. |
|
||||||
Adds a task or a list of tasks to the task pool.
|
| `results` | `List` | A list to store results of executed tasks. |
|
||||||
|
| `loop` | `asyncio.EventLoop` | The event loop for asynchronous execution. |
|
||||||
**Arguments:**
|
|
||||||
|
---
|
||||||
| Parameter | Type | Default | Description |
|
|
||||||
|-----------|------|---------|-------------|
|
**Description**:
|
||||||
| `task` | `Any` | `None` | A single task to add. |
|
Initializes the `AsyncWorkflow` with specified agents, configuration, and options.
|
||||||
| `tasks` | `List[Any]` | `None` | A list of tasks to add. |
|
|
||||||
|
**Parameters**:
|
||||||
**Raises:**
|
- `name` (`str`): Name of the workflow. Default: "AsyncWorkflow".
|
||||||
|
- `agents` (`List[Agent]`): A list of agents. Default: `None`.
|
||||||
- `ValueError`: If neither task nor tasks are provided.
|
- `max_workers` (`int`): The maximum number of workers. Default: `5`.
|
||||||
|
- `dashboard` (`bool`): Enable dashboard visualization (placeholder for future implementation).
|
||||||
**Examples:**
|
- `autosave` (`bool`): Enable autosave of task results. Default: `False`.
|
||||||
|
- `verbose` (`bool`): Enable detailed logging. Default: `False`.
|
||||||
|
- `**kwargs`: Additional parameters for `BaseWorkflow`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### `_execute_agent_task`
|
||||||
```python
|
```python
|
||||||
workflow = AsyncWorkflow()
|
async def _execute_agent_task(self, agent: Agent, task: str) -> Any:
|
||||||
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])
|
|
||||||
```
|
```
|
||||||
|
**Description**:
|
||||||
|
Executes a single task asynchronously using a given agent.
|
||||||
|
|
||||||
### delete
|
**Parameters**:
|
||||||
|
- `agent` (`Agent`): The agent responsible for executing the task.
|
||||||
Deletes a task from the workflow.
|
- `task` (`str`): The task to be executed.
|
||||||
|
|
||||||
**Arguments:**
|
**Returns**:
|
||||||
|
- `Any`: The result of the task execution or an error message in case of an exception.
|
||||||
| Parameter | Type | Default | Description |
|
|
||||||
|-----------|------|---------|-------------|
|
|
||||||
| `task` | `Any` | `None` | A single task to delete. |
|
|
||||||
| `tasks` | `List[Task]` | `None` | A list of tasks to delete. |
|
|
||||||
|
|
||||||
**Examples:**
|
|
||||||
|
|
||||||
|
**Example**:
|
||||||
```python
|
```python
|
||||||
workflow = AsyncWorkflow()
|
result = await workflow._execute_agent_task(agent, "Sample Task")
|
||||||
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:**
|
|
||||||
|
|
||||||
|
### `run`
|
||||||
```python
|
```python
|
||||||
workflow = AsyncWorkflow()
|
async def run(self, task: str) -> List[Any]:
|
||||||
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()
|
|
||||||
```
|
```
|
||||||
|
**Description**:
|
||||||
|
Executes the specified task concurrently across all agents.
|
||||||
|
|
||||||
### Additional Examples
|
**Parameters**:
|
||||||
|
- `task` (`str`): The task to be executed by all agents.
|
||||||
#### 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()
|
**Returns**:
|
||||||
task = Task(description="Simple Task", execute=simple_task)
|
- `List[Any]`: A list of results or error messages returned by the agents.
|
||||||
|
|
||||||
# Adding a task to the workflow
|
**Raises**:
|
||||||
await workflow.add(task=task)
|
- `ValueError`: If no agents are provided in the workflow.
|
||||||
|
|
||||||
# Running the workflow
|
|
||||||
results = await workflow.run()
|
|
||||||
print(results) # Output: ["Task Completed"]
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Example 2: Workflow with Multiple Tasks
|
|
||||||
|
|
||||||
|
**Example**:
|
||||||
```python
|
```python
|
||||||
import asyncio
|
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():
|
agents = [Agent("Agent1"), Agent("Agent2")]
|
||||||
await asyncio.sleep(2)
|
workflow = AsyncWorkflow(agents=agents, verbose=True)
|
||||||
return "Task 2 Completed"
|
|
||||||
|
|
||||||
workflow = AsyncWorkflow()
|
results = asyncio.run(workflow.run("Process Data"))
|
||||||
task_1 = Task(description="Task 1", execute=task1)
|
print(results)
|
||||||
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
|
---
|
||||||
|
|
||||||
|
## Production-Grade Financial Example: Multiple Agents
|
||||||
|
### Example: Stock Analysis and Investment Strategy
|
||||||
```python
|
```python
|
||||||
import asyncio
|
import asyncio
|
||||||
from swarms.structs.agent import Agent
|
from swarms import Agent
|
||||||
from swarms.structs.task import Task
|
from async_workflow import AsyncWorkflow
|
||||||
|
from swarms.prompts.finance_agent_sys_prompt import FINANCIAL_AGENT_SYS_PROMPT
|
||||||
async def task1():
|
|
||||||
await asyncio.sleep(1)
|
# Initialize multiple Financial Agents
|
||||||
return "Task 1 Completed"
|
portfolio_analysis_agent = Agent(
|
||||||
|
agent_name="Portfolio-Analysis-Agent",
|
||||||
async def task2():
|
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
|
||||||
await asyncio.sleep(2)
|
model_name="gpt-4o-mini",
|
||||||
return "Task 2 Completed"
|
autosave=True,
|
||||||
|
verbose=True,
|
||||||
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"]
|
|
||||||
```
|
|
||||||
|
|
||||||
# Async Workflow
|
|
||||||
|
|
||||||
The AsyncWorkflow allows multiple agents to process tasks concurrently using Python's asyncio framework.
|
|
||||||
|
|
||||||
## Usage Example
|
|
||||||
|
|
||||||
```python
|
stock_strategy_agent = Agent(
|
||||||
import asyncio
|
agent_name="Stock-Strategy-Agent",
|
||||||
from swarms import Agent, AsyncWorkflow
|
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
|
||||||
from swarm_models import OpenAIChat
|
model_name="gpt-4o-mini",
|
||||||
|
autosave=True,
|
||||||
# Initialize model
|
verbose=True,
|
||||||
model = OpenAIChat(
|
|
||||||
openai_api_key="your-api-key",
|
|
||||||
model_name="gpt-4",
|
|
||||||
temperature=0.7
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create agents
|
risk_management_agent = Agent(
|
||||||
agents = [
|
agent_name="Risk-Management-Agent",
|
||||||
Agent(
|
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
|
||||||
agent_name=f"Analysis-Agent-{i}",
|
model_name="gpt-4o-mini",
|
||||||
llm=model,
|
autosave=True,
|
||||||
max_loops=1,
|
|
||||||
dashboard=False,
|
|
||||||
verbose=True,
|
verbose=True,
|
||||||
)
|
)
|
||||||
for i in range(3)
|
|
||||||
]
|
|
||||||
|
|
||||||
# Initialize workflow
|
# Create a workflow with multiple agents
|
||||||
workflow = AsyncWorkflow(
|
workflow = AsyncWorkflow(
|
||||||
name="Analysis-Workflow",
|
name="Financial-Workflow",
|
||||||
agents=agents,
|
agents=[portfolio_analysis_agent, stock_strategy_agent, risk_management_agent],
|
||||||
max_workers=3,
|
verbose=True,
|
||||||
verbose=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Run workflow
|
# Run the workflow
|
||||||
async def main():
|
async def main():
|
||||||
task = "Analyze the potential impact of AI on healthcare"
|
task = "Analyze the current stock market trends and provide an investment strategy with risk assessment."
|
||||||
results = await workflow.run(task)
|
results = await workflow.run(task)
|
||||||
for i, result in enumerate(results):
|
for agent_result in results:
|
||||||
print(f"Agent {i} result: {result}")
|
print(agent_result)
|
||||||
|
|
||||||
# Execute
|
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
```
|
```
|
||||||
|
|
||||||
## Parameters
|
**Output**:
|
||||||
|
```
|
||||||
|
INFO: Agent Portfolio-Analysis-Agent processing task: Analyze the current stock market trends and provide an investment strategy with risk assessment.
|
||||||
|
INFO: Agent Stock-Strategy-Agent processing task: Analyze the current stock market trends and provide an investment strategy with risk assessment.
|
||||||
|
INFO: Agent Risk-Management-Agent processing task: Analyze the current stock market trends and provide an investment strategy with risk assessment.
|
||||||
|
INFO: Agent Portfolio-Analysis-Agent completed task
|
||||||
|
INFO: Agent Stock-Strategy-Agent completed task
|
||||||
|
INFO: Agent Risk-Management-Agent completed task
|
||||||
|
Results:
|
||||||
|
- Detailed portfolio analysis...
|
||||||
|
- Stock investment strategies...
|
||||||
|
- Risk assessment insights...
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
1. **Autosave**: The autosave functionality is a placeholder. Users can implement custom logic to save `self.results`.
|
||||||
|
2. **Error Handling**: Exceptions raised by agents are logged and returned as part of the results.
|
||||||
|
3. **Dashboard**: The `dashboard` feature is currently not implemented but can be extended for visualization.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
- `asyncio`: Python's asynchronous I/O framework.
|
||||||
|
- `loguru`: Logging utility for better log management.
|
||||||
|
- `swarms`: Base components (`BaseWorkflow`, `Agent`).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Future Extensions
|
||||||
|
- **Dashboard**: Implement a real-time dashboard for monitoring agent performance.
|
||||||
|
- **Autosave**: Add persistent storage support for task results.
|
||||||
|
- **Task Management**: Extend task pooling and scheduling logic to support dynamic workloads.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
| Parameter | Type | Default | Description |
|
## License
|
||||||
|-----------|------|---------|-------------|
|
This class is part of the `swarms` framework and follows the framework's licensing terms.
|
||||||
| `name` | str | "AsyncWorkflow" | Name of the workflow |
|
|
||||||
| `agents` | List[Agent] | None | List of agents to execute tasks |
|
|
||||||
| `max_workers` | int | 5 | Maximum number of concurrent workers |
|
|
||||||
| `dashboard` | bool | False | Enable/disable dashboard |
|
|
||||||
| `autosave` | bool | False | Enable/disable autosaving results |
|
|
||||||
| `verbose` | bool | False | Enable/disable verbose logging |
|
|
||||||
|
Loading…
Reference in new issue