asyncworkflow with agent lists

pull/631/head
Occupying-Mars 5 months ago
parent bd0a89705f
commit 0ea39bb905

@ -399,6 +399,28 @@ print(f"Generated data: {generated_data}")
```
## OpenAI Assistant Integration
Swarms provides native support for OpenAI's Assistants API through the
`OpenAIAssistant` class:
```python
from swarms import OpenAIAssistant
#Create an assistant
assistant = OpenAIAssistant(
name="Research Assistant",
instructions="You help with research tasks.",
tools=[{"type": "code_interpreter"}]
)
#Run tasks
response = assistant.run("Analyze this dataset")
```
See the [OpenAI Assistant documentation](docs/swarms/agents/openai_assistant.md) for more details.
### Multi Modal Autonomous Agent
Run the agent with multiple modalities useful for various real-world tasks in manufacturing, logistics, and health.
@ -1830,7 +1852,7 @@ Documentation is located here at: [docs.swarms.world](https://docs.swarms.world)
-----
## Folder Structure
The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`.
The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`.
```sh
├── __init__.py

@ -3,6 +3,7 @@ from swarms.structs.auto_swarm import AutoSwarm, AutoSwarmRouter
from swarms.structs.base_structure import BaseStructure
from swarms.structs.base_swarm import BaseSwarm
from swarms.structs.base_workflow import BaseWorkflow
from swarms.structs.async_workflow import AsyncWorkflow
from swarms.structs.concurrent_workflow import ConcurrentWorkflow
from swarms.structs.conversation import Conversation
from swarms.structs.graph_workflow import (
@ -78,6 +79,7 @@ from swarms.structs.multi_agent_exec import (
__all__ = [
"Agent",
"AsyncWorkflow",
"AutoSwarm",
"AutoSwarmRouter",
"BaseStructure",

@ -0,0 +1,62 @@
import asyncio
from typing import Any, Callable, List, Optional
from swarms.structs.base_workflow import BaseWorkflow
from swarms.structs.agent import Agent
from swarms.utils.loguru_logger import logger
class AsyncWorkflow(BaseWorkflow):
def __init__(
self,
name: str = "AsyncWorkflow",
agents: List[Agent] = None,
max_workers: int = 5,
dashboard: bool = False,
autosave: bool = False,
verbose: bool = False,
**kwargs
):
super().__init__(agents=agents, **kwargs)
self.name = name
self.agents = agents or []
self.max_workers = max_workers
self.dashboard = dashboard
self.autosave = autosave
self.verbose = verbose
self.task_pool = []
self.results = []
self.loop = None
async def _execute_agent_task(self, agent: Agent, task: str) -> Any:
"""Execute a single agent task asynchronously"""
try:
if self.verbose:
logger.info(f"Agent {agent.agent_name} processing task: {task}")
result = await agent.arun(task)
if self.verbose:
logger.info(f"Agent {agent.agent_name} completed task")
return result
except Exception as e:
logger.error(f"Error in agent {agent.agent_name}: {str(e)}")
return str(e)
async def run(self, task: str) -> List[Any]:
"""Run the workflow with all agents processing the task concurrently"""
if not self.agents:
raise ValueError("No agents provided to the workflow")
try:
# Create tasks for all agents
tasks = [self._execute_agent_task(agent, task) for agent in self.agents]
# Execute all tasks concurrently
self.results = await asyncio.gather(*tasks, return_exceptions=True)
if self.autosave:
# Implement autosave logic here
pass
return self.results
except Exception as e:
logger.error(f"Error in workflow execution: {str(e)}")
raise
Loading…
Cancel
Save