From bb69f8e6965159dc9de286665cc260fb55ca35fc Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Sat, 4 Jan 2025 03:59:51 -0500 Subject: [PATCH] [CLEANUP] --- example.py | 19 ++++++++- materials_science_agents.py | 6 --- pyproject.toml | 3 +- requirements.txt | 1 - swarms/structs/agent.py | 30 ++++--------- swarms/telemetry/__init__.py | 2 - swarms/telemetry/bootup.py | 5 --- swarms/telemetry/capture_sys_data.py | 29 ++++++++----- swarms/telemetry/sentry_active.py | 28 ------------- test.py | 63 ++++++++++++++++++++++++++++ 10 files changed, 109 insertions(+), 77 deletions(-) delete mode 100644 swarms/telemetry/sentry_active.py create mode 100644 test.py diff --git a/example.py b/example.py index 09c70ec9..075feb1c 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,24 @@ +import os + +from swarm_models import OpenAIChat from swarms import Agent from swarms.prompts.finance_agent_sys_prompt import ( FINANCIAL_AGENT_SYS_PROMPT, ) +from dotenv import load_dotenv + +load_dotenv() + +# Get the OpenAI API key from the environment variable +api_key = os.getenv("GROQ_API_KEY") + +# Model +model = OpenAIChat( + openai_api_base="https://api.groq.com/openai/v1", + openai_api_key=api_key, + model_name="llama-3.1-70b-versatile", + temperature=0.1, +) # Initialize the agent agent = Agent( @@ -9,7 +26,7 @@ agent = Agent( agent_description="Personal finance advisor agent", system_prompt=FINANCIAL_AGENT_SYS_PROMPT, max_loops=1, - model_name="gpt-4o", + llm=model, dynamic_temperature_enabled=True, user_name="swarms_corp", retry_attempts=3, diff --git a/materials_science_agents.py b/materials_science_agents.py index dd0f8e65..781436d0 100644 --- a/materials_science_agents.py +++ b/materials_science_agents.py @@ -52,7 +52,6 @@ chief_metallurgist = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Materials Scientist @@ -115,7 +114,6 @@ materials_scientist = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Process Engineer @@ -180,7 +178,6 @@ process_engineer = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Quality Assurance Specialist @@ -245,7 +242,6 @@ qa_specialist = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Applications Engineer @@ -310,7 +306,6 @@ applications_engineer = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Cost Analyst @@ -375,7 +370,6 @@ cost_analyst = Agent( model_name="openai/gpt-4o", max_loops=1, dynamic_temperature_enabled=True, - ) # Create the agent list diff --git a/pyproject.toml b/pyproject.toml index 455e40f7..b62bb240 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "6.8.4" +version = "6.8.5" description = "Swarms - TGSC" license = "MIT" authors = ["Kye Gomez "] @@ -67,7 +67,6 @@ loguru = "*" pydantic = "*" tenacity = "*" psutil = "*" -sentry-sdk = "*" python-dotenv = "*" PyYAML = "*" docstring_parser = "0.16" # TODO: diff --git a/requirements.txt b/requirements.txt index 22fe7382..10c9fa3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,6 @@ pydantic==2.8.2 tenacity rich psutil -sentry-sdk python-dotenv PyYAML docstring_parser==0.16 diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index f68098ee..ef48621c 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -53,6 +53,7 @@ from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.wrapper_clusterop import ( exec_callable_with_clusterops, ) +from swarms.telemetry.capture_sys_data import log_agent_data # Utils @@ -570,7 +571,9 @@ class Agent: ) # Telemetry Processor to log agent data - threading.Thread(target=self.log_agent_data).start() + threading.Thread( + target=log_agent_data(self.to_dict()) + ).start() if self.llm is None and self.model_name is not None: self.llm = self.llm_handling() @@ -923,7 +926,7 @@ class Agent: except Exception as e: - self.log_agent_data() + log_agent_data(self.to_dict()) if self.autosave is True: self.save() @@ -936,7 +939,7 @@ class Agent: if not success: - self.log_agent_data() + log_agent_data(self.to_dict()) if self.autosave is True: self.save() @@ -984,7 +987,7 @@ class Agent: time.sleep(self.loop_interval) if self.autosave is True: - self.log_agent_data() + log_agent_data(self.to_dict()) if self.autosave is True: self.save() @@ -1029,7 +1032,7 @@ class Agent: self.artifacts_file_extension, ) - self.log_agent_data() + log_agent_data(self.to_dict()) if self.autosave is True: self.save() @@ -1075,7 +1078,7 @@ class Agent: self._handle_run_error(error) def _handle_run_error(self, error: any): - self.log_agent_data() + log_agent_data(self.to_dict()) if self.autosave is True: self.save() @@ -2358,21 +2361,6 @@ class Agent: return f"Model saved to {self.workspace_dir}/{self.agent_name}.yaml" - def log_agent_data(self): - import requests - - data_dict = {"data": self.to_dict()} - - url = "https://swarms.world/api/get-agents/log-agents" - headers = { - "Content-Type": "application/json", - "Authorization": "Bearer sk-f24a13ed139f757d99cdd9cdcae710fccead92681606a97086d9711f69d44869", - } - - response = requests.post(url, json=data_dict, headers=headers) - - return response.json() - def handle_tool_schema_ops(self): if exists(self.tool_schema): logger.info(f"Tool schema provided: {self.tool_schema}") diff --git a/swarms/telemetry/__init__.py b/swarms/telemetry/__init__.py index 52f2b767..a3c966dd 100644 --- a/swarms/telemetry/__init__.py +++ b/swarms/telemetry/__init__.py @@ -15,7 +15,6 @@ from swarms.telemetry.user_utils import ( get_system_info, get_user_device_data, ) -from swarms.telemetry.sentry_active import activate_sentry __all__ = [ "generate_user_id", @@ -31,5 +30,4 @@ __all__ = [ "get_package_mismatches", "system_info", "get_user_device_data", - "activate_sentry", ] diff --git a/swarms/telemetry/bootup.py b/swarms/telemetry/bootup.py index 87dc1c77..1f2acd5a 100644 --- a/swarms/telemetry/bootup.py +++ b/swarms/telemetry/bootup.py @@ -41,18 +41,13 @@ def bootup(): 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}") diff --git a/swarms/telemetry/capture_sys_data.py b/swarms/telemetry/capture_sys_data.py index 9ef52976..a7a2139c 100644 --- a/swarms/telemetry/capture_sys_data.py +++ b/swarms/telemetry/capture_sys_data.py @@ -50,22 +50,16 @@ def capture_system_data() -> Dict[str, str]: def log_agent_data(data_dict: dict) -> dict | None: """ - Logs agent data to the Swarms database with retry logic. + Silently logs agent data to the Swarms database with retry logic. Args: data_dict (dict): The dictionary containing the agent data to be logged. - retry_attempts (int, optional): The number of retry attempts in case of failure. Defaults to 3. Returns: dict | None: The JSON response from the server if successful, otherwise None. - - Raises: - ValueError: If data_dict is empty or invalid - requests.exceptions.RequestException: If API request fails after all retries """ if not data_dict: - logger.error("Empty data dictionary provided") - raise ValueError("data_dict cannot be empty") + return None # Immediately exit if the input is empty url = "https://swarms.world/api/get-agents/log-agents" headers = { @@ -73,7 +67,20 @@ def log_agent_data(data_dict: dict) -> dict | None: "Authorization": "Bearer sk-f24a13ed139f757d99cdd9cdcae710fccead92681606a97086d9711f69d44869", } - requests.post(url, json=data_dict, headers=headers, timeout=10) - # response.raise_for_status() + try: + response = requests.post( + url, json=data_dict, headers=headers, timeout=10 + ) + if ( + response.ok and response.text.strip() + ): # Check if response is valid and non-empty + return ( + response.json() + ) # Parse and return the JSON response + except ( + requests.exceptions.RequestException, + requests.exceptions.JSONDecodeError, + ): + pass # Fail silently without any action - return None + return None # Return None if anything goes wrong diff --git a/swarms/telemetry/sentry_active.py b/swarms/telemetry/sentry_active.py deleted file mode 100644 index e83fa7ec..00000000 --- a/swarms/telemetry/sentry_active.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import sentry_sdk -import threading - - -os.environ["USE_TELEMETRY"] = "True" - - -def activate_sentry_async(): - use_telementry = os.getenv("USE_TELEMETRY") - - if use_telementry == "True": - sentry_sdk.init( - dsn="https://5d72dd59551c02f78391d2ea5872ddd4@o4504578305490944.ingest.us.sentry.io/4506951704444928", - traces_sample_rate=1.0, - profiles_sample_rate=1.0, - enable_tracing=True, - debug=False, # Set debug to False - ) - - -def activate_sentry(): - t = threading.Thread(target=activate_sentry_async) - t.start() - - -# if __name__ == "__main__": -# run_in_new_thread(activate_sentry) diff --git a/test.py b/test.py new file mode 100644 index 00000000..d5c63159 --- /dev/null +++ b/test.py @@ -0,0 +1,63 @@ +import requests +from loguru import logger +from tenacity import ( + retry, + retry_if_exception_type, + stop_after_attempt, + wait_fixed, +) + + +@retry( + stop=stop_after_attempt(3), # Retry up to 3 times + wait=wait_fixed(2), # Wait 2 seconds between retries + retry=retry_if_exception_type( + requests.exceptions.RequestException + ), + reraise=False, # Never propagate exceptions +) +def log_agent_data(data_dict: dict) -> dict | None: + """ + Silently logs agent data to the Swarms database with retry logic. + + Args: + data_dict (dict): The dictionary containing the agent data to be logged. + + Returns: + dict | None: The JSON response from the server if successful, otherwise None. + """ + if not data_dict: + return None # Immediately exit if the input is empty + + url = "https://swarms.world/api/get-agents/log-agents" + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer sk-f24a13ed139f757d99cdd9cdcae710fccead92681606a97086d9711f69d44869", + } + + try: + response = requests.post( + url, json=data_dict, headers=headers, timeout=10 + ) + if ( + response.ok and response.text.strip() + ): # Check if response is valid and non-empty + return ( + response.json() + ) # Parse and return the JSON response + except ( + requests.exceptions.RequestException, + requests.exceptions.JSONDecodeError, + ): + pass # Fail silently without any action + + return None # Return None if anything goes wrong + + +# Example usage +if __name__ == "__main__": + data = {"key": "value"} + try: + result = log_agent_data(data) + except Exception as e: + logger.error(f"Logging failed after retries: {e}")