From d3975472d1db2f045bb8937c818af45ba9c55023 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 May 2024 16:07:47 -0700 Subject: [PATCH 1/4] send end timestamp if not set before record --- agentops/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/agentops/client.py b/agentops/client.py index 544285ef3..e1a9a85c5 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -135,7 +135,8 @@ def record(self, event: Event | ErrorEvent): Args: event (Event): The event to record. """ - + if not event.end_timestamp or event.init_timestamp == event.end_timestamp: + event.end_timestamp = get_ISO_time() if self._session is not None and not self._session.has_ended: if isinstance(event, ErrorEvent): if event.trigger_event: From 76d494db65689211322c17661bd9924fd8519e98 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 May 2024 17:44:55 -0700 Subject: [PATCH 2/4] tests --- agentops/client.py | 1 + tests/test_events.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/test_events.py diff --git a/agentops/client.py b/agentops/client.py index e1a9a85c5..c7986b2a6 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -75,6 +75,7 @@ def __init__(self, self._session = None self._worker = None + self._tags_for_future_session = None self._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv('AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true' diff --git a/tests/test_events.py b/tests/test_events.py new file mode 100644 index 000000000..bb24ba2b4 --- /dev/null +++ b/tests/test_events.py @@ -0,0 +1,31 @@ +import time +import requests_mock +import pytest +import agentops +from agentops import ActionEvent + + +@pytest.fixture +def mock_req(): + with requests_mock.Mocker() as m: + url = 'https://api.agentops.ai' + m.post(url + '/events', text='ok') + m.post(url + '/sessions', json={'status': 'success', 'token_cost': 5}) + yield m + +class TestEvents: + def setup_method(self): + self.api_key = "random_api_key" + self.event_type = 'test_event_type' + self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50, max_queue_size=1) + + def test_record_timestamp(self, mock_req): + # agentops.init(api_key=self.api_key, max_wait_time=50, auto_start_session=False) + agentops.init(api_key=self.api_key) + agentops.start_session(config=self.config) + + event = ActionEvent(self.event_type) + time.sleep(0.15) + agentops.record(event) + + assert event.init_timestamp != event.end_timestamp \ No newline at end of file From 82ddffa7b55129c977dfb6f48d5eb29d937b86a6 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 May 2024 18:41:16 -0700 Subject: [PATCH 3/4] token cost print fix --- agentops/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index c7986b2a6..3ed8f1cb4 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -272,13 +272,13 @@ def end_session(self, self._session.video = video self._session.end_session(end_state, end_state_reason) - token_cost = Decimal(self._worker.end_session(self._session)) + token_cost = self._worker.end_session(self._session) if token_cost == 'unknown': print('🖇 AgentOps: Could not determine cost of run.') else: - + token_cost_d = Decimal(token_cost) print('🖇 AgentOps: This run cost ${}'.format('{:.2f}'.format( - token_cost) if token_cost == 0 else '{:.6f}'.format(token_cost))) + token_cost_d) if token_cost_d == 0 else '{:.6f}'.format(token_cost_d))) self._session = None self._worker = None From 9f1e39d2fed1e8f358eeab0a9ddf02a0be82cf29 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 May 2024 18:47:55 -0700 Subject: [PATCH 4/4] test fix --- tests/test_events.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_events.py b/tests/test_events.py index bb24ba2b4..4e156bdf3 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -20,11 +20,9 @@ def setup_method(self): self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50, max_queue_size=1) def test_record_timestamp(self, mock_req): - # agentops.init(api_key=self.api_key, max_wait_time=50, auto_start_session=False) agentops.init(api_key=self.api_key) - agentops.start_session(config=self.config) - event = ActionEvent(self.event_type) + event = ActionEvent() time.sleep(0.15) agentops.record(event)