diff --git a/swarms/orchestrator/autoscaler.py b/swarms/orchestrator/autoscaler.py index eff3fc37..0fad0382 100644 --- a/swarms/orchestrator/autoscaler.py +++ b/swarms/orchestrator/autoscaler.py @@ -6,6 +6,7 @@ from swarms.workers.autobot import AutoBot # TODO Handle task assignment and task delegation # TODO: User task => decomposed into very small sub tasks => sub tasks assigned to workers => workers complete and update the swarm, can ask for help from other agents. # TODO: Missing, Task Assignment, Task delegation, Task completion, Swarm level communication with vector db + class AutoScaler: def __init__(self, initial_agents=10, diff --git a/swarms/utils/decorators.py b/swarms/utils/decorators.py new file mode 100644 index 00000000..c83b7b74 --- /dev/null +++ b/swarms/utils/decorators.py @@ -0,0 +1,78 @@ +import time +import logging +import threading +import functools +import warnings + +def log_decorator(func): + def wrapper(*args, **kwargs): + logging.info(f'Entering {func.__name__}') + result = func(*args, **kwargs) + logging.info(f'Exiting {func.__name__}') + return result + return wrapper + +def error_decorator(func): + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except Exception as e: + logging.error(f'Error in {func.__name__}: {str(e)}') + raise + return wrapper + +def timing_decorator(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + logging.info(f'{func.__name__} executed in {end_time - start_time} seconds') + return result + return wrapper + +def retry_decorator(max_retries=5): + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + for _ in range(max_retries): + try: + return func(*args, **kwargs) + except Exception as error: + logging.error(f" Error in {func.__name__}: {str(error)} Retrying ....") + return func(*args, **kwargs) + return wrapper + return decorator + +def singleton_decorator(cls): + instances = {} + def wrapper(*args, **kwargs): + if cls not in instances: + instances[cls] = cls(*args, **kwargs) + return instances[cls] + return wrapper + +def synchronized_decorator(func): + func.__lock__ = threading.Lock() + def wrapper(*args, **kwargs): + with func.__lock__: + return func(*args, **kwargs) + return wrapper + + +def deprecated_decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + warnings.warn(f"{func.__name__} is deprecated", category=DeprecationWarning) + return func(*args, **kwargs) + return wrapper + +def validate_inputs_decorator(validator): + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + if not validator(*args, **kwargs): + raise ValueError("Invalid Inputs") + return func(*args, **kwargs) + return wrapper + return decorator + diff --git a/swarms/workers/autobot.py b/swarms/workers/autobot.py index f487083a..8abe4c93 100644 --- a/swarms/workers/autobot.py +++ b/swarms/workers/autobot.py @@ -11,17 +11,19 @@ from swarms.agents.tools.autogpt import ( ReadFileTool, WebpageQATool, WriteFileTool, - load_qa_with_sources_chain, process_csv, # web_search, - query_website_tool + query_website_tool, ) - +from swarms.utils.decorators import error_decorator, log_decorator, timing_decorator ROOT_DIR = "./data/" class AutoBot: + @log_decorator + @error_decorator + @timing_decorator def __init__(self, model_name="gpt-4", openai_api_key=None, @@ -49,7 +51,10 @@ class AutoBot: self.setup_tools() self.setup_memory() self.setup_agent() - + + @log_decorator + @error_decorator + @timing_decorator def setup_tools(self): self.tools = [ WriteFileTool(root_dir=ROOT_DIR), @@ -81,6 +86,9 @@ class AutoBot: except Exception as error: raise RuntimeError(f"Error setting up agent: {error}") + @log_decorator + @error_decorator + @timing_decorator def run(self, task): try: result = self.agent.run([task])