Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved default values down to Configuration which is lowest level, not Config #126

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand All @@ -32,53 +31,47 @@ 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.
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'.
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.
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"]).
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,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
tags: Optional[List[str]] = None,
override=True,
auto_start_session=True
):

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
HowieG marked this conversation as resolved.
Show resolved Hide resolved

self._handle_unclean_exits()

Expand Down
36 changes: 31 additions & 5 deletions agentops/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,42 @@

from typing import Optional
from os import environ
import logging


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'.
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
Expand Down Expand Up @@ -118,3 +136,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)
6 changes: 4 additions & 2 deletions agentops/meta_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ 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}")
HowieG marked this conversation as resolved.
Show resolved Hide resolved
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
return wrapper
2 changes: 1 addition & 1 deletion agentops/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/openai_handlers/_test_handler_openai_v1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import openai
from openai.resources.chat import completions
from openai import OpenAI, AsyncOpenAI
import agentops
Expand Down