Skip to content

Commit

Permalink
autoscaler
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Oct 11, 2023
1 parent 35b5264 commit e79b369
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 24 additions & 2 deletions swarms/swarms/autoscaler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import queue
import threading
from time import sleep

from swarms.utils.decorators import error_decorator, log_decorator, timing_decorator
from swarms.workers.worker import Worker

Expand All @@ -14,7 +13,24 @@ class AutoScaler:
# TODO: Missing, Task Assignment, Task delegation, Task completion, Swarm level communication with vector db
Example
Args:
initial_agents (int, optional): Number of initial agents. Defaults to 10.
scale_up_factor (int, optional): Scale up factor. Defaults to 1.
idle_threshold (float, optional): Idle threshold. Defaults to 0.2.
busy_threshold (float, optional): Busy threshold. Defaults to 0.7.
agent ([type], optional): Agent. Defaults to None.
Methods:
add_task: Add task to queue
scale_up: Scale up
scale_down: Scale down
monitor_and_scale: Monitor and scale
start: Start scaling
del_agent: Delete an agent
Usage
```
# usage of usage
auto_scaler = AutoScaler(agent=YourCustomAgent)
Expand Down Expand Up @@ -44,18 +60,21 @@ def __init__(
self.lock = threading.Lock()

def add_task(self, task):
"""Add tasks to queue"""
self.tasks_queue.put(task)

@log_decorator
@error_decorator
@timing_decorator
def scale_up(self):
"""Add more agents"""
with self.lock:
new_agents_counts = len(self.agents_pool) * self.scale_up_factor
for _ in range(new_agents_counts):
self.agents_pool.append(Worker())

def scale_down(self):
"""scale down"""
with self.lock:
if len(self.agents_pool) > 10: # ensure minmum of 10 agents
del self.agents_pool[-1] # remove last agent
Expand All @@ -64,6 +83,7 @@ def scale_down(self):
@error_decorator
@timing_decorator
def monitor_and_scale(self):
"""Monitor and scale"""
while True:
sleep(60) # check minute
pending_tasks = self.task_queue.qsize()
Expand All @@ -78,6 +98,7 @@ def monitor_and_scale(self):
@error_decorator
@timing_decorator
def start(self):
"""Start scaling"""
monitor_thread = threading.Thread(target=self.monitor_and_scale)
monitor_thread.start()

Expand All @@ -89,6 +110,7 @@ def start(self):
available_agent.run(task)

def del_agent(self):
"""Delete an agent"""
with self.lock:
if self.agents_pool:
agent_to_remove = self.agents_poo.pop()
Expand Down
10 changes: 10 additions & 0 deletions swarms/swarms/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from swarms.workers.base import AbstractWorker

class AbstractSwarm(ABC):
"""
Abstract class for swarm simulation architectures
"""
# TODO: Pass in abstract LLM class that can utilize Hf or Anthropic models, Move away from OPENAI
# TODO: ADD Universal Communication Layer, a ocean vectorstore instance
# TODO: BE MORE EXPLICIT ON TOOL USE, TASK DECOMPOSITION AND TASK COMPLETETION AND ALLOCATION
Expand Down Expand Up @@ -63,3 +67,9 @@ def direct_message(
):
"""Send a direct message to a worker"""
pass

@abstractmethod
def autoscaler(self, num_workers: int, worker: ["AbstractWorker"]):
"""Autoscaler that acts like kubernetes for autonomous agents"""
pass

0 comments on commit e79b369

Please sign in to comment.