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

set end timestamp if not set before record #179

Merged
merged 4 commits into from
May 3, 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
10 changes: 6 additions & 4 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -135,7 +136,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:
Expand Down Expand Up @@ -270,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

Expand Down
29 changes: 29 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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)

event = ActionEvent()
time.sleep(0.15)
agentops.record(event)

assert event.init_timestamp != event.end_timestamp
Loading