From 135b02a8123d87568e912a64b7feae69580ff103 Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Sat, 21 Dec 2024 12:11:48 -0800 Subject: [PATCH] [CLEANUP] --- api/agent_api_test.py | 14 ++++++--- swarms/structs/agent.py | 23 ++++++--------- swarms/telemetry/bootup.py | 58 +++++++++++++++++++++++++++++++------- 3 files changed, 66 insertions(+), 29 deletions(-) diff --git a/api/agent_api_test.py b/api/agent_api_test.py index c8420227..9ccac876 100644 --- a/api/agent_api_test.py +++ b/api/agent_api_test.py @@ -20,7 +20,16 @@ logger = logging.getLogger(__name__) from typing import Dict, Optional, Tuple from uuid import UUID -BASE_URL = "http://0.0.0.0:8000/v1" +# Set up logging +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(levelname)s - %(message)s", + handlers=[ + logging.FileHandler("api_tests.log"), + logging.StreamHandler(), + ], +) +logger = logging.getLogger(__name__) # Configuration @dataclass @@ -136,9 +145,6 @@ class TestRunner: logger.info(f"\nRunning test: {test_name}") start_time = time.time() -<<<<<<< HEAD -======= - def create_test_user(session: TestSession) -> Tuple[bool, str]: """Create a test user and store credentials in session.""" logger.info("Creating test user") diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 2f1f380f..d6caed66 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -2422,22 +2422,15 @@ class Agent: if self.llm is None: raise TypeError("LLM object cannot be None") - # Define common method names for LLM interfaces - method_names = ["run", "__call__", "generate", "invoke"] - - for method_name in method_names: - if hasattr(self.llm, method_name): - try: - method = getattr(self.llm, method_name) - return method(task, *args, **kwargs) - except Exception as e: - raise RuntimeError( - f"Error calling {method_name}: {str(e)}" - ) + try: + out = self.llm.run(task, *args, **kwargs) - raise AttributeError( - f"No suitable method found in the llm object. Expected one of: {method_names}" - ) + return out + except AttributeError as e: + logger.error( + f"Error calling LLM: {e} You need a class with a run(task: str) method" + ) + raise e def handle_sop_ops(self): # If the user inputs a list of strings for the sop then join them and set the sop diff --git a/swarms/telemetry/bootup.py b/swarms/telemetry/bootup.py index 5e38c3ea..87dc1c77 100644 --- a/swarms/telemetry/bootup.py +++ b/swarms/telemetry/bootup.py @@ -1,27 +1,65 @@ import os import logging import warnings -from concurrent.futures import ThreadPoolExecutor +import concurrent.futures +from dotenv import load_dotenv +from loguru import logger from swarms.utils.disable_logging import disable_logging def bootup(): - """Bootup swarms""" + """Initialize swarms environment and configuration + + Handles environment setup, logging configuration, telemetry, + and workspace initialization. + """ try: - logging.disable(logging.CRITICAL) + # Load environment variables + load_dotenv() + + # Configure logging + if ( + os.getenv("SWARMS_VERBOSE_GLOBAL", "False").lower() + == "false" + ): + logger.disable("") + logging.disable(logging.CRITICAL) + + # Silent wandb os.environ["WANDB_SILENT"] = "true" - # Auto set workspace directory + # Configure workspace workspace_dir = os.path.join(os.getcwd(), "agent_workspace") - if not os.path.exists(workspace_dir): - os.makedirs(workspace_dir, exist_ok=True) + os.makedirs(workspace_dir, exist_ok=True) os.environ["WORKSPACE_DIR"] = workspace_dir + # Suppress warnings warnings.filterwarnings("ignore", category=DeprecationWarning) - # Use ThreadPoolExecutor to run disable_logging and auto_update concurrently - with ThreadPoolExecutor(max_workers=1) as executor: - executor.submit(disable_logging) + # Run telemetry functions concurrently + try: + with concurrent.futures.ThreadPoolExecutor( + max_workers=2 + ) as executor: + from swarms.telemetry.sentry_active import ( + activate_sentry, + ) + + future_disable_logging = executor.submit( + disable_logging + ) + future_sentry = executor.submit(activate_sentry) + + # Wait for completion and check for exceptions + future_disable_logging.result() + future_sentry.result() + except Exception as e: + logger.error(f"Error running telemetry functions: {e}") + except Exception as e: - print(f"An error occurred: {str(e)}") + logger.error(f"Error during bootup: {str(e)}") raise + + +# Run bootup +bootup()