diff --git a/agentops/__init__.py b/agentops/__init__.py index 0b6557ac..70712b57 100755 --- a/agentops/__init__.py +++ b/agentops/__init__.py @@ -42,7 +42,6 @@ def init( Initializes the AgentOps singleton pattern. 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 @@ -108,6 +107,21 @@ def configure( auto_start_session: Optional[bool] = None, skip_auto_end_session: Optional[bool] = None, ): + """ + Configure the AgentOps Client + + 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. + 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. + max_queue_size (int, optional): The maximum size of the event queue + default_tags (List[str], optional): Default tags for the sessions that can be used for grouping or sorting later (e.g. ["GPT-4"]). + instrument_llm_calls (bool, optional): Whether to instrument LLM calls and emit LLMEvents. + auto_start_session (bool, optional): Whether to start a session automatically when the client is created. + skip_auto_end_session (bool, optional): Don't automatically end session based on your framework's decision-making + (i.e. Crew determining when tasks are complete and ending the session) + """ Client().configure( api_key=api_key, parent_key=parent_key, diff --git a/agentops/client.py b/agentops/client.py index 99621e95..b0f9f140 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -24,7 +24,7 @@ ) from .session import Session, active_sessions from .host_env import get_host_env -from .log_config import logger +from .log_config import logger, unsuppress_logs from .meta_client import MetaClient from .config import Configuration from .llm_tracker import LlmTracker @@ -82,15 +82,7 @@ def configure( ) def initialize(self) -> Union[Session, None]: - logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO") - log_levels = { - "CRITICAL": logging.CRITICAL, - "ERROR": logging.ERROR, - "INFO": logging.INFO, - "WARNING": logging.WARNING, - "DEBUG": logging.DEBUG, - } - logger.setLevel(log_levels.get(logging_level, "INFO")) + unsuppress_logs() for message in Client()._pre_init_messages: logger.warning(message) @@ -180,6 +172,15 @@ def add_default_tags(self, tags: List[str]) -> None: """ self._config.default_tags.update(tags) + def get_default_tags(self) -> List[str]: + """ + Append default tags at runtime. + + Args: + tags (List[str]): The list of tags to set. + """ + return list(self._config.default_tags) + def record(self, event: Union[Event, ErrorEvent]) -> None: """ Record an event with the AgentOps service. @@ -377,6 +378,8 @@ def _update_session(self, session: Session): ] = session def _safe_get_session(self) -> Optional[Session]: + if not self.is_initialized: + return None if len(self._sessions) == 1: return self._sessions[0] diff --git a/agentops/http_client.py b/agentops/http_client.py index 3b456358..a625cd19 100644 --- a/agentops/http_client.py +++ b/agentops/http_client.py @@ -90,7 +90,9 @@ def post( except requests.exceptions.Timeout: result.code = 408 result.status = HttpStatus.TIMEOUT - raise ApiServerException("Could not reach API server - connection timed out") + raise ApiServerException( + "Could not reach API server - connection timed out" + ) except requests.exceptions.HTTPError as e: try: result.parse(e.response) @@ -105,10 +107,12 @@ def post( raise ApiServerException(f"RequestException: {e}") if result.code == 401: - raise ApiServerException(f"API server: invalid API key: {api_key}. Find your API key at https://app.agentops.ai/settings/projects") + raise ApiServerException( + f"API server: invalid API key: {api_key}. Find your API key at https://app.agentops.ai/settings/projects" + ) if result.code == 400: if "message" in result.body: - raise ApiServerException(f"API server: {result.body["message"]}") + raise ApiServerException(f"API server: {result.body['message']}") else: raise ApiServerException(f"API server: {result.body}") if result.code == 500: diff --git a/agentops/log_config.py b/agentops/log_config.py index f814fb52..be271ad2 100644 --- a/agentops/log_config.py +++ b/agentops/log_config.py @@ -51,3 +51,15 @@ def format(self, record): file_handler.setFormatter(formatter) file_handler.setFormatter(formatter) logger.addHandler(file_handler) + + +def unsuppress_logs(): + logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL", "INFO") + log_levels = { + "CRITICAL": logging.CRITICAL, + "ERROR": logging.ERROR, + "INFO": logging.INFO, + "WARNING": logging.WARNING, + "DEBUG": logging.DEBUG, + } + logger.setLevel(log_levels.get(logging_level, "INFO")) diff --git a/tach.yml b/tach.yml index 0d7c6131..08b7b475 100644 --- a/tach.yml +++ b/tach.yml @@ -50,7 +50,6 @@ modules: - path: agentops.http_client depends_on: - path: agentops.exceptions - - path: agentops.log_config - path: agentops.langchain_callback_handler depends_on: [] - path: agentops.llm_tracker @@ -75,6 +74,7 @@ modules: - path: agentops.log_config - path: agentops.session depends_on: + - path: agentops.client - path: agentops.config - path: agentops.enums - path: agentops.event