Skip to content

Python SDK for AI agent monitoring, LLM cost tracking, benchmarking, and more. Integrates with most LLMs and agent frameworks including CrewAI, Agno, OpenAI Agents SDK, Langchain, Autogen, AG2, and CamelAI

feature: client support starting new sessions (#89)

Shawn Qiu63b8319de815
diff --git a/agentops/client.py b/agentops/client.py
index fc669262..9a6b937a 100644
--- a/agentops/client.py
+++ b/agentops/client.py
@@ -42,80 +42,36 @@ class Client:
def __init__(self, api_key: Optional[str] = None,
tags: Optional[List[str]] = None,
- endpoint: Optional[str] = 'https://agentops-server-v2.fly.dev',
+ endpoint: Optional[str] = 'https://api.agentops.ai',
max_wait_time: Optional[int] = 1000,
max_queue_size: Optional[int] = 100,
override=True,
- bypass_new_session=False
+ auto_start_session=True
):
- # Get API key from env
- if api_key is None:
- api_key = environ.get('AGENTOPS_API_KEY')
-
- if api_key is None:
- print("AgentOps API key not provided. Session data will not be recorded.")
-
- # Create a worker config
- self.config = Configuration(api_key, endpoint,
- max_wait_time, max_queue_size)
+ self._session = None
+ self._worker = None
+ self._tags = tags
+ self.config = None
- # Store a reference to the instance
- Client._instance = self
- atexit.register(lambda: self.cleanup())
+ if not api_key and not environ.get('AGENTOPS_API_KEY'):
+ return logging.warn("AgentOps: No API key provided - no data will be recorded.")
- # Register signal handler for SIGINT (Ctrl+C) and SIGTERM
- signal.signal(signal.SIGINT, self.signal_handler)
- signal.signal(signal.SIGTERM, self.signal_handler)
+ self.config = Configuration(api_key or environ.get('AGENTOPS_API_KEY'),
+ endpoint,
+ max_wait_time,
+ max_queue_size)
- # Override sys.excepthook
- sys.excepthook = self.handle_exception
+ self._handle_unclean_exits()
- self._session = None
- if not bypass_new_session:
+ if auto_start_session:
self.start_session(tags)
- else:
- self._worker = None
- self._tags = tags
if override:
if 'openai' in sys.modules:
self.llm_tracker = LlmTracker(self)
self.llm_tracker.override_api('openai')
- def handle_exception(self, exc_type, exc_value, exc_traceback):
- """
- 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.
- """
- formatted_traceback = ''.join(traceback.format_exception(exc_type, exc_value,
- exc_traceback))
-
- # Perform cleanup)
- self.cleanup(
- end_state_reason=f"{str(exc_value)}: {formatted_traceback}")
-
- # Then call the default excepthook to exit the program
- sys.__excepthook__(exc_type, exc_value, exc_traceback)
-
- def signal_handler(self, signum, frame):
- """
- 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.
- """
- signal_name = 'SIGINT' if signum == signal.SIGINT else 'SIGTERM'
- logging.info(f'Signal {signal_name} detected. Ending session...')
- self.end_session(end_state='Fail',
- end_state_reason=f'Signal {signal_name} detected')
- sys.exit(0)
-
def add_tags(self, tags: List[str]):
if self._session is None:
return print("You must create a session before assigning tags")
@@ -148,10 +104,9 @@ class Client:
self._worker.add_event(
{'session_id': self._session.session_id, **event.__dict__})
else:
- logging.info("This event was not recorded because the previous session has been ended" +
- " Start a new session to record again.")
+ logging.warn("AgentOps: Cannot record event - no current session")
- def record_action(self, event_name: str, tags: Optional[List[str]] = None):
+ def record_function(self, event_name: str, tags: Optional[List[str]] = None):
"""
Decorator to record an event before and after a function call.
Usage:
@@ -264,7 +219,7 @@ class Client:
return returns
- def start_session(self, tags: Optional[List[str]] = None):
+ def start_session(self, tags: Optional[List[str]] = None, config: Optional[Configuration] = None):
"""
Start a new session for recording events.
@@ -273,10 +228,13 @@ class Client:
e.g. ["test_run"].
"""
if self._session is not None:
- return print("Session already started. End this session before starting a new one.")
+ return logging.warn("AgentOps: Cannot start session - session already started")
+
+ if not config and not self.config:
+ return logging.warn("AgentOps: Cannot start session - missing configuration")
self._session = Session(str(uuid4()), tags or self._tags)
- self._worker = Worker(self.config)
+ self._worker = Worker(config or self.config)
self._worker.start_session(self._session)
def end_session(self, end_state: str = Field("Indeterminate",
@@ -295,15 +253,55 @@ class Client:
video (str, optional): The video screen recording of the session
"""
if self._session is None or self._session.has_ended:
- return print("Session has already been ended.")
+ return logging.warn("AgentOps: Cannot end session - no current session")
self._session.video = video
self._session.end_session(end_state, rating, end_state_reason)
self._worker.end_session(self._session)
- # self._session = None
-
- def cleanup(self, end_state_reason: Optional[str] = None):
- # Only run cleanup function if session is created
- if hasattr(self, "session"):
+ self._session = None
+ self._worker = None
+
+ def _handle_unclean_exits(self):
+ def cleanup(end_state_reason: Optional[str] = None):
+ # Only run cleanup function if session is created
+ if self._session is not None:
+ self.end_session(end_state='Fail',
+ end_state_reason=end_state_reason)
+
+ def signal_handler(signum, frame):
+ """
+ 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.
+ """
+ signal_name = 'SIGINT' if signum == signal.SIGINT else 'SIGTERM'
+ logging.info(f'Signal {signal_name} detected. Ending session...')
self.end_session(end_state='Fail',
- end_state_reason=end_state_reason)
+ end_state_reason=f'Signal {signal_name} detected')
+ sys.exit(0)
+
+ def handle_exception(exc_type, exc_value, exc_traceback):
+ """
+ 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.
+ """
+ formatted_traceback = ''.join(traceback.format_exception(exc_type, exc_value,
+ exc_traceback))
+
+ # Perform cleanup
+ cleanup(
+ end_state_reason=f"{str(exc_value)}: {formatted_traceback}")
+
+ # Then call the default excepthook to exit the program
+ sys.__excepthook__(exc_type, exc_value, exc_traceback)
+
+ atexit.register(lambda: cleanup())
+ signal.signal(signal.SIGINT, signal_handler)
+ signal.signal(signal.SIGTERM, signal_handler)
+ sys.excepthook = handle_exception
diff --git a/tests/openai_handlers/test_handler_openai_v0.py b/tests/openai_handlers/_test_handler_openai_v0.py
similarity index 100%
rename from tests/openai_handlers/test_handler_openai_v0.py
rename to tests/openai_handlers/_test_handler_openai_v0.py
diff --git a/tests/openai_handlers/test_handler_openai_v1.py b/tests/openai_handlers/_test_handler_openai_v1.py
similarity index 100%
rename from tests/openai_handlers/test_handler_openai_v1.py
rename to tests/openai_handlers/_test_handler_openai_v1.py
diff --git a/tests/test_canary.py b/tests/test_canary.py
index 60b0ebde..ddceba50 100644
--- a/tests/test_canary.py
+++ b/tests/test_canary.py
@@ -8,7 +8,7 @@ from agentops import Client, Event
@pytest.fixture
def mock_req():
with requests_mock.Mocker() as m:
- url = 'https://agentops-server-v2.fly.dev'
+ url = 'https://api.agentops.ai'
m.post(url + '/events', text='ok')
m.post(url + '/sessions', text='ok')
yield m
@@ -16,7 +16,7 @@ def mock_req():
class TestCanary:
def setup_method(self):
- self.url = 'https://agentops-server-v2.fly.dev'
+ self.url = 'https://api.agentops.ai'
self.api_key = "random_api_key"
self.client = Client(api_key=self.api_key, max_wait_time=5)
diff --git a/tests/test_logger.py b/tests/test_logger.py
index 6ad66faf..09b1835e 100755
--- a/tests/test_logger.py
+++ b/tests/test_logger.py
@@ -7,7 +7,7 @@ from agentops import Client, AgentOpsLogger
@pytest.fixture
def mock_req():
with requests_mock.Mocker() as m:
- url = 'https://agentops-server-v2.fly.dev'
+ url = 'https://api.agentops.ai'
m.post(url + '/events', text='ok')
m.post(url + '/sessions', text='ok')
yield m
@@ -15,7 +15,7 @@ def mock_req():
class TestLogger:
def setup_method(self):
- self.url = 'https://agentops-server-v2.fly.dev'
+ self.url = 'https://api.agentops.ai'
self.api_key = "random_api_key"
self.client = Client(api_key=self.api_key, max_wait_time=5)
diff --git a/tests/test_session.py b/tests/test_session.py
index 4ebe0ac5..8b958d43 100644
--- a/tests/test_session.py
+++ b/tests/test_session.py
@@ -10,7 +10,7 @@ from agentops import Client, Event
@pytest.fixture
def mock_req():
with requests_mock.Mocker() as m:
- url = 'https://agentops-server-v2.fly.dev'
+ url = 'https://api.agentops.ai'
m.post(url + '/events', text='ok')
m.post(url + '/sessions', text='ok')
yield m
@@ -85,7 +85,7 @@ class TestSessions:
class TestRecordAction:
def setup_method(self):
- self.url = 'https://agentops-server-v2.fly.dev'
+ self.url = 'https://api.agentops.ai'
self.api_key = "random_api_key"
self.event_type = 'test_event_type'
self.client = Client(self.api_key, max_wait_time=5)
@@ -93,8 +93,8 @@ class TestRecordAction:
def teardown_method(self):
self.client.end_session(end_state='Success')
- def test_record_action_decorator(self, mock_req):
- @self.client.record_action(event_name=self.event_type, tags=['foo', 'bar'])
+ def test_record_function_decorator(self, mock_req):
+ @self.client.record_function(event_name=self.event_type, tags=['foo', 'bar'])
def add_two(x, y):
return x + y
@@ -112,9 +112,9 @@ class TestRecordAction:
assert request_json['events'][0]['result'] == 'Success'
assert request_json['events'][0]['tags'] == ['foo', 'bar']
- def test_record_action_decorator_multiple(self, mock_req):
+ def test_record_function_decorator_multiple(self, mock_req):
# Arrange
- @self.client.record_action(event_name=self.event_type, tags=['foo', 'bar'])
+ @self.client.record_function(event_name=self.event_type, tags=['foo', 'bar'])
def add_three(x, y, z=3):
return x + y + z
@@ -135,7 +135,7 @@ class TestRecordAction:
@pytest.mark.asyncio
async def test_async_function_call(self, mock_req):
- @self.client.record_action(self.event_type)
+ @self.client.record_function(self.event_type)
async def async_add(x, y):
time.sleep(0.1)
return x + y
@@ -165,7 +165,7 @@ class TestRecordAction:
# Arrange
prompt = 'prompt'
- @self.client.record_action(event_name=self.event_type)
+ @self.client.record_function(event_name=self.event_type)
def foo(prompt=prompt):
return 'output'
@@ -185,7 +185,7 @@ class TestRecordAction:
# Arrange
prompt = 'prompt'
- @self.client.record_action(event_name=self.event_type)
+ @self.client.record_function(event_name=self.event_type)
def llm_call(prompt=prompt):
return 'output'
diff --git a/tests/test_teardown.py b/tests/test_teardown.py
index 60711839..e3f0d902 100644
--- a/tests/test_teardown.py
+++ b/tests/test_teardown.py
@@ -8,7 +8,7 @@ from agentops import Client, Event
@pytest.fixture
def mock_req():
with requests_mock.Mocker() as m:
- url = 'https://agentops-server-v2.fly.dev'
+ url = 'https://api.agentops.ai'
m.post(url + '/events', text='ok')
m.post(url + '/sessions', text='ok')
yield m
diff --git a/tox.ini b/tox.ini
index d1420bc4..601d86af 100644
--- a/tox.ini
+++ b/tox.ini
@@ -10,6 +10,7 @@ envlist = py37, py38, py39, mypy
deps =
pytest
pytest-asyncio
+ pytest-mock
requests_mock
coverage
pydantic