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/swarmnetwork.md

5.1 KiB

# Class Name: SwarmNetwork

## Overview and Introduction
The `SwarmNetwork` class is responsible for managing the agents pool and the task queue. It also monitors the health of the agents and scales the pool up or down based on the number of pending tasks and the current load of the agents.

## Class Definition

The `SwarmNetwork` class has the following parameters:

| Parameter         | Type              | Description                                                                   |
|-------------------|-------------------|-------------------------------------------------------------------------------|
| idle_threshold    | float             | Threshold for idle agents to trigger scaling down                             |
| busy_threshold    | float             | Threshold for busy agents to trigger scaling up                               |
| agents            | List[Agent]       | List of agent instances to be added to the pool                              |
| api_enabled       | Optional[bool]    | Flag to enable/disable the API functionality                                 |
| logging_enabled   | Optional[bool]    | Flag to enable/disable logging                                                |
| other arguments   | *args             | Additional arguments                                                           |
| other keyword     | **kwargs          | Additional keyword arguments                                                  |

## Function Explanation and Usage

### Function: `add_task`
- Adds a task to the task queue
- Parameters:
  - `task`: The task to be added to the queue
- Example:
  ```python
  from swarms.structs.agent import Agent
  from swarms.structs.swarm_net import SwarmNetwork

  agent = Agent()
  swarm = SwarmNetwork(agents=[agent])
  swarm.add_task("task")

Function: async_add_task

  • Asynchronous function to add a task to the task queue
  • Parameters:
    • task: The task to be added to the queue
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    await swarm.async_add_task("task")
    

Function: run_single_agent

  • Executes a task on a single agent
  • Parameters:
    • agent_id: ID of the agent to run the task on
    • task: The task to be executed by the agent (optional)
  • Returns:
    • Result of the task execution
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    swarm.run_single_agent(agent_id, "task")
    

Function: run_many_agents

  • Executes a task on all the agents in the pool
  • Parameters:
    • task: The task to be executed by the agents (optional)
  • Returns:
    • List of results from each agent
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    swarm.run_many_agents("task")
    

Function: list_agents

  • Lists all the agents in the pool
  • Returns:
    • List of active agents
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    swarm.list_agents()
    

Function: add_agent

  • Adds an agent to the agent pool
  • Parameters:
    • agent: Agent instance to be added to the pool
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork()
    swarm.add_agent(agent)
    

Function: remove_agent

  • Removes an agent from the agent pool
  • Parameters:
    • agent_id: ID of the agent to be removed from the pool
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    swarm.remove_agent(agent_id)
    

Function: scale_up

  • Scales up the agent pool by adding new agents
  • Parameters:
    • num_agents: Number of agents to be added (optional)
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    swarm = SwarmNetwork()
    swarm.scale_up(num_agents=5)
    

Function: scale_down

  • Scales down the agent pool by removing existing agents
  • Parameters:
    • num_agents: Number of agents to be removed (optional)
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    swarm = SwarmNetwork(agents=[agent1, agent2, agent3, agent4, agent5])
    swarm.scale_down(num_agents=2)
    

Function: create_apis_for_agents

  • Creates APIs for each agent in the pool (optional)
  • Example:
    from swarms.structs.agent import Agent
    from swarms.structs.swarm_net import SwarmNetwork
    
    agent = Agent()
    swarm = SwarmNetwork(agents=[agent])
    swarm.create_apis_for_agents()
    

Additional Information

  • The SwarmNetwork class is an essential part of the swarms.structs library, enabling efficient management and scaling of agent pools.