From 3cf84e45d3806f9d0b88dfa5d3f6ec223b7c9c31 Mon Sep 17 00:00:00 2001 From: Howard Gil Date: Thu, 28 Mar 2024 01:27:50 -0700 Subject: [PATCH 1/3] Moved default values down to Configuration which is lowest level, not Config --- agentops/client.py | 43 +++++++------------ agentops/config.py | 40 ++++++++++++++--- agentops/meta_client.py | 7 +-- agentops/worker.py | 2 +- .../_test_handler_openai_v1.py | 1 + 5 files changed, 57 insertions(+), 36 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index 92ba1844..a90cd475 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -13,7 +13,6 @@ from .host_env import get_host_env from uuid import uuid4 from typing import Optional, List -from os import environ import traceback import logging import inspect @@ -22,7 +21,7 @@ import sys from .meta_client import MetaClient -from .config import Configuration +from .config import Configuration, ConfigurationError from .llm_tracker import LlmTracker @@ -32,20 +31,22 @@ class Client(metaclass=MetaClient): Client for AgentOps service. Args: - api_key (str, optional): API Key for AgentOps services. If none is provided, key will - be read from the AGENTOPS_API_KEY environment variable. + api_key (str, optional): API Key for AgentOps services. + parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. tags (List[str], optional): Tags for the sessions that can be used for grouping or sorting later (e.g. ["GPT-4"]). - endpoint (str, optional): The endpoint for the AgentOps service. Defaults to 'https://api.agentops.ai'. + endpoint (str, optional): The endpoint for the AgentOps service. max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 1000. max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100. override (bool): Whether to override and LLM calls to emit as events. + auto_start_session (bool): Whether to start a session automatically when the client is created. Attributes: _session (Session, optional): A Session is a grouping of events (e.g. a run of your agent). """ - def __init__(self, api_key: Optional[str] = None, + def __init__(self, + api_key: Optional[str] = None, parent_key: Optional[str] = None, tags: Optional[List[str]] = None, endpoint: Optional[str] = None, @@ -58,27 +59,15 @@ def __init__(self, api_key: Optional[str] = None, self._session = None self._worker = None self._tags = tags - self.config = None - - # These handle the case where .init() is used with optionals, and one of these - # params are None, which is does not trigger the Optional default in the constructor - if not api_key: - api_key = environ.get("AGENTOPS_API_KEY") - if not api_key: - logging.warning("AgentOps: No API key provided - no data will be recorded.") - return - - if not parent_key: - parent_key = environ.get('AGENTOPS_PARENT_KEY', None) - - if not endpoint: - endpoint = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai') - - self.config = Configuration(api_key, - parent_key, - endpoint, - max_wait_time, - max_queue_size) + + try: + self.config = Configuration(api_key, + parent_key, + endpoint, + max_wait_time, + max_queue_size) + except ConfigurationError: + return self._handle_unclean_exits() diff --git a/agentops/config.py b/agentops/config.py index 9779cfbb..c731e5c9 100644 --- a/agentops/config.py +++ b/agentops/config.py @@ -7,6 +7,7 @@ from typing import Optional from os import environ +import logging class Configuration: @@ -14,17 +15,38 @@ class Configuration: Stores the configuration settings for AgentOps clients. Args: - api_key (str): API Key for AgentOps services - endpoint (str, optional): The endpoint for the AgentOps service. Defaults to 'https://api.agentops.ai'. + + Client for AgentOps service. + + Args: + api_key (str, optional): API Key for AgentOps services. If none is provided, key will + be read from the AGENTOPS_API_KEY environment variable. + parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will + be read from the AGENTOPS_PARENT_KEY environment variable. + endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will + be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'. max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 30000. max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100. """ - def __init__(self, api_key: str, - parent_key: Optional[str], - endpoint: Optional[str] = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai'), + def __init__(self, + api_key: Optional[str] = None, + parent_key: Optional[str] = None, + endpoint: Optional[str] = None, max_wait_time: Optional[int] = None, max_queue_size: Optional[int] = None): + + if not api_key: + api_key = environ.get('AGENTOPS_API_KEY', None) + if not api_key: + raise ConfigurationError("AgentOps: No API key provided - no data will be recorded.") + + if not parent_key: + parent_key = environ.get('AGENTOPS_PARENT_KEY', None) + + if not endpoint: + endpoint = environ.get('AGENTOPS_API_ENDPOINT', 'https://api.agentops.ai') + self._api_key: str = api_key self._endpoint = endpoint self._max_wait_time = max_wait_time or 30000 @@ -118,3 +140,11 @@ def parent_key(self): @parent_key.setter def parent_key(self, value: str): self._parent_key = value + + +class ConfigurationError(Exception): + """Exception raised for errors related to Configuration""" + + def __init__(self, message: str): + super().__init__(message) + logging.warning(message) diff --git a/agentops/meta_client.py b/agentops/meta_client.py index 17de1b56..52cb5809 100644 --- a/agentops/meta_client.py +++ b/agentops/meta_client.py @@ -45,8 +45,9 @@ def wrapper(self, *args, **kwargs): try: return method(self, *args, **kwargs) except Exception as e: - type(self).send_exception_to_server(e, self.config._api_key) - logging.warning(f"AgentOps: Error: {e}") + config = getattr(self, 'config', None) + if config is not None: + type(self).send_exception_to_server(e, self.config._api_key) raise e - return wrapper \ No newline at end of file + return wrapper diff --git a/agentops/worker.py b/agentops/worker.py index d4410c24..d409ee10 100644 --- a/agentops/worker.py +++ b/agentops/worker.py @@ -2,7 +2,7 @@ import threading import time from .http_client import HttpClient -from .config import Configuration +from .config import Configuration, ConfigurationError from .session import Session from .helpers import safe_serialize, filter_unjsonable from typing import Dict diff --git a/tests/openai_handlers/_test_handler_openai_v1.py b/tests/openai_handlers/_test_handler_openai_v1.py index 97ea1795..e5843e77 100644 --- a/tests/openai_handlers/_test_handler_openai_v1.py +++ b/tests/openai_handlers/_test_handler_openai_v1.py @@ -1,4 +1,5 @@ import time +import openai from openai.resources.chat import completions from openai import OpenAI, AsyncOpenAI import agentops From d8a99f8b9fe4e3a2ea3ec6f83906ab80d42cc26f Mon Sep 17 00:00:00 2001 From: Howard Gil Date: Thu, 28 Mar 2024 15:36:42 -0700 Subject: [PATCH 2/3] Added back logging and fixing docstrings --- agentops/client.py | 16 ++++++++++------ agentops/config.py | 4 ---- agentops/meta_client.py | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index a90cd475..c945f6ec 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -31,14 +31,18 @@ class Client(metaclass=MetaClient): Client for AgentOps service. Args: - api_key (str, optional): API Key for AgentOps services. - parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. - tags (List[str], optional): Tags for the sessions that can be used for grouping or - sorting later (e.g. ["GPT-4"]). - endpoint (str, optional): The endpoint for the AgentOps service. + + api_key (str, optional): API Key for AgentOps services. If none is provided, key will + be read from the AGENTOPS_API_KEY environment variable. + parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will + be read from the AGENTOPS_PARENT_KEY environment variable. + endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will + be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'. max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 1000. max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100. + tags (List[str], optional): Tags for the sessions that can be used for grouping or + sorting later (e.g. ["GPT-4"]). override (bool): Whether to override and LLM calls to emit as events. auto_start_session (bool): Whether to start a session automatically when the client is created. Attributes: @@ -48,10 +52,10 @@ class Client(metaclass=MetaClient): def __init__(self, api_key: Optional[str] = None, parent_key: Optional[str] = None, - tags: Optional[List[str]] = None, endpoint: Optional[str] = None, max_wait_time: Optional[int] = None, max_queue_size: Optional[int] = None, + tags: Optional[List[str]] = None, override=True, auto_start_session=True ): diff --git a/agentops/config.py b/agentops/config.py index c731e5c9..2bd56262 100644 --- a/agentops/config.py +++ b/agentops/config.py @@ -14,10 +14,6 @@ class Configuration: """ Stores the configuration settings for AgentOps clients. - Args: - - Client for AgentOps service. - Args: api_key (str, optional): API Key for AgentOps services. If none is provided, key will be read from the AGENTOPS_API_KEY environment variable. diff --git a/agentops/meta_client.py b/agentops/meta_client.py index 52cb5809..29f78588 100644 --- a/agentops/meta_client.py +++ b/agentops/meta_client.py @@ -45,6 +45,7 @@ def wrapper(self, *args, **kwargs): try: return method(self, *args, **kwargs) except Exception as e: + logging.warning(f"AgentOps: Error: {e}") config = getattr(self, 'config', None) if config is not None: type(self).send_exception_to_server(e, self.config._api_key) From 6b4c7791767550c934da1168d953b50a7dd6a31e Mon Sep 17 00:00:00 2001 From: Howard Gil Date: Thu, 28 Mar 2024 15:38:51 -0700 Subject: [PATCH 3/3] Requested by Shawn --- agentops/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agentops/client.py b/agentops/client.py index c945f6ec..7580b8ca 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -39,7 +39,7 @@ class Client(metaclass=MetaClient): endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'. max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. - Defaults to 1000. + Defaults to 30,000 (30 seconds) max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100. tags (List[str], optional): Tags for the sessions that can be used for grouping or sorting later (e.g. ["GPT-4"]).