Skip to content

Commit

Permalink
Added back override with deprecation message. Docstring fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
HowieG committed Apr 26, 2024
1 parent ad0559e commit cc76006
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 59 deletions.
13 changes: 8 additions & 5 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def init(api_key: Optional[str] = None,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
tags: Optional[List[str]] = None,
override_llm_calls=True,
override: Optional[bool] = None, # Deprecated
instrument_llm_calls=True,
auto_start_session=True):
"""
Initializes the AgentOps singleton pattern.
Expand All @@ -34,18 +35,20 @@ def init(api_key: Optional[str] = None,
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_llm_calls (bool): Whether to override LLM calls and emit LLMEvents.
override (bool, optional): [Deprecated] Use `instrument_llm_calls` instead. Whether to instrument LLM calls and emit LLMEvents..
instrument_llm_calls (bool): Whether to instrument LLM calls and emit LLMEvents..
auto_start_session (bool): Whether to start a session automatically when the client is created.
Attributes:
"""
"""

Client(api_key=api_key,
parent_key=parent_key,
endpoint=endpoint,
max_wait_time=max_wait_time,
max_queue_size=max_queue_size,
tags=tags,
override_llm_calls=override_llm_calls,
override=override,
instrument_llm_calls=instrument_llm_calls,
auto_start_session=auto_start_session)


Expand All @@ -58,7 +61,7 @@ def end_session(end_state: str,
Args:
end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): The video screen recording of the session
video (str, optional): URL to a video recording of the session
"""
Client().end_session(end_state, end_state_reason, video)

Expand Down
115 changes: 61 additions & 54 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,27 @@
@singleton
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.
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 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_llm_calls (bool): Whether to override LLM calls and emit LLMEvents.
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).
_worker (Worker, optional): A Worker manages the event queue and sends session updates to the AgentOps api server
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 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, optional): [Deprecated] Use `instrument_llm_calls` instead. Whether to instrument LLM calls and emit LLMEvents..
instrument_llm_calls (bool): Whether to instrument LLM calls and emit LLMEvents..
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).
_worker (Worker, optional): A Worker manages the event queue and sends session updates to the AgentOps api server
"""

def __init__(self,
Expand All @@ -57,10 +58,16 @@ def __init__(self,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
tags: Optional[List[str]] = None,
override_llm_calls=True,
override: Optional[bool] = None, # Deprecated
instrument_llm_calls=True,
auto_start_session=True
):

if override is not None:
logging.warning("🖇 AgentOps: The 'override' parameter is deprecated. Use 'instrument_llm_calls' instead.",
DeprecationWarning, stacklevel=2)
instrument_llm_calls = instrument_llm_calls or override

self._session = None
self._worker = None
self._tags = tags
Expand All @@ -79,16 +86,16 @@ def __init__(self,
if auto_start_session:
self.start_session(tags, self.config)

if override_llm_calls:
if instrument_llm_calls:
self.llm_tracker = LlmTracker(self)
self.llm_tracker.override_api()

def add_tags(self, tags: List[str]):
"""
Append to session tags at runtime.
Append to session tags at runtime.
Args:
tags (List[str]): The list of tags to append.
Args:
tags (List[str]): The list of tags to append.
"""
if self._tags is not None:
self._tags.extend(tags)
Expand All @@ -101,10 +108,10 @@ def add_tags(self, tags: List[str]):

def set_tags(self, tags: List[str]):
"""
Replace session tags at runtime.
Replace session tags at runtime.
Args:
tags (List[str]): The list of tags to set.
Args:
tags (List[str]): The list of tags to set.
"""
self._tags = tags

Expand All @@ -114,10 +121,10 @@ def set_tags(self, tags: List[str]):

def record(self, event: Event | ErrorEvent):
"""
Record an event with the AgentOps service.
Record an event with the AgentOps service.
Args:
event (Event): The event to record.
Args:
event (Event): The event to record.
"""

if self._session is not None and not self._session.has_ended:
Expand Down Expand Up @@ -208,12 +215,12 @@ async def _record_event_async(self, func, event_name, *args, **kwargs):

def start_session(self, tags: Optional[List[str]] = None, config: Optional[Configuration] = None):
"""
Start a new session for recording events.
Start a new session for recording events.
Args:
tags (List[str], optional): Tags that can be used for grouping or sorting later.
e.g. ["test_run"].
config: (Configuration, optional): Client configuration object
Args:
tags (List[str], optional): Tags that can be used for grouping or sorting later.
e.g. ["test_run"].
config: (Configuration, optional): Client configuration object
"""
if self._session is not None:
return logging.warning("🖇 AgentOps: Cannot start session - session already started")
Expand All @@ -236,12 +243,12 @@ def end_session(self,
end_state_reason: Optional[str] = None,
video: Optional[str] = None):
"""
End the current session with the AgentOps service.
End the current session with the AgentOps service.
Args:
end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): The video screen recording of the session
Args:
end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): The video screen recording of the session
"""
if self._session is None or self._session.has_ended:
return logging.warning("🖇 AgentOps: Cannot end session - no current session")
Expand Down Expand Up @@ -272,11 +279,11 @@ def cleanup(end_state: Optional[str] = 'Fail', end_state_reason: Optional[str] =

def signal_handler(signum, frame):
"""
Signal handler for SIGINT (Ctrl+C) and SIGTERM. Ends the session and exits the program.
Signal handler for SIGINT (Ctrl+C) and SIGTERM. Ends the session and exits the program.
Args:
signum (int): The signal number.
frame: The current stack frame.
Args:
signum (int): The signal number.
frame: The current stack frame.
"""
signal_name = 'SIGINT' if signum == signal.SIGINT else 'SIGTERM'
logging.info(
Expand All @@ -287,13 +294,13 @@ def signal_handler(signum, frame):

def handle_exception(exc_type, exc_value, exc_traceback):
"""
Handle uncaught exceptions before they result in program termination.
Handle uncaught exceptions before they result in program termination.
Args:
exc_type (Type[BaseException]): The type of the exception.
exc_value (BaseException): The exception instance.
exc_traceback (TracebackType): A traceback object encapsulating the call stack at the
point where the exception originally occurred.
Args:
exc_type (Type[BaseException]): The type of the exception.
exc_value (BaseException): The exception instance.
exc_traceback (TracebackType): A traceback object encapsulating the call stack at the
point where the exception originally occurred.
"""
formatted_traceback = ''.join(traceback.format_exception(exc_type, exc_value,
exc_traceback))
Expand All @@ -320,10 +327,10 @@ def api_key(self):

def set_parent_key(self, parent_key: str):
"""
Set the parent API key which has visibility to projects it is parent to.
Set the parent API key which has visibility to projects it is parent to.
Args:
parent_key (str): The API key of the parent organization to set.
Args:
parent_key (str): The API key of the parent organization to set.
"""
if self._worker:
self._worker.config.parent_key = parent_key
Expand Down

0 comments on commit cc76006

Please sign in to comment.