Skip to content

Commit

Permalink
26 tracebacks for failed events (#43)
Browse files Browse the repository at this point in the history
* added failstrings to events

* added api key checker

* update tox

* added daemon

* revert test

* added fail agent for mock testing

* fix typo
  • Loading branch information
areibman authored Nov 2, 2023
1 parent bfbf528 commit 767b4d9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
16 changes: 12 additions & 4 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from uuid import uuid4
from typing import Optional, List
from pydantic import Field
from os import environ
import functools
import logging
import inspect
Expand All @@ -28,7 +29,7 @@ class Client:
Client for AgentOps service.
Args:
api_key (str): API Key for AgentOps services.
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://agentops-server-v2.fly.dev'.
max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue. Defaults to 1000.
Expand All @@ -37,11 +38,18 @@ class Client:
session (Session, optional): A Session is a grouping of events (e.g. a run of your agent).
"""

def __init__(self, api_key: str, tags: Optional[List[str]] = None,
def __init__(self, api_key: Optional[str] = None, tags: Optional[List[str]] = None,
endpoint: Optional[str] = 'https://agentops-server-v2.fly.dev',
max_wait_time: Optional[int] = 1000,
max_queue_size: Optional[int] = 100):

# 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)
Expand Down Expand Up @@ -163,7 +171,7 @@ def _record_event_sync(self, func, event_name, tags, *args, **kwargs):
# Record the event after the function call
self.record(Event(event_type=event_name,
params=arg_values,
returns=None,
returns=str(e),
result='Fail',
action_type='action',
init_timestamp=init_time,
Expand Down Expand Up @@ -207,7 +215,7 @@ async def _record_event_async(self, func, event_name, tags, *args, **kwargs):
# Record the event after the function call
self.record(Event(event_type=event_name,
params=arg_values,
returns=None,
returns=str(e),
result='Fail',
action_type='action',
init_timestamp=init_time,
Expand Down
1 change: 1 addition & 0 deletions agentops/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, config: Configuration) -> None:
self.lock = threading.Lock()
self.stop_flag = threading.Event()
self.thread = threading.Thread(target=self.run)
self.thread.daemon = True
self.thread.start()

def add_event(self, event: dict) -> None:
Expand Down
39 changes: 39 additions & 0 deletions tests/fail_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import agentops
import time
import asyncio
from dotenv import load_dotenv

load_dotenv()


print('init')

ao_client = agentops.Client(tags=['mock tests'])


@ao_client.record_action('fail')
def fail_func(sleep):
time.sleep(sleep)
print('sync sleep')

print(1/0)


@ao_client.record_action('sleep')
def sleep_func(sleep):
time.sleep(sleep)
print('sync sleep')


async def main():

print('Action 1')
sleep_func(0.1)
fail_func(0.1)

ao_client.end_session(end_state='Success')


if __name__ == '__main__':
asyncio.run(main())
print('done')
4 changes: 2 additions & 2 deletions tests/mock_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

print('init')

ao_client = agentops.Client(api_key=os.environ['AGENTOPS_API_KEY'],
tags=['mock tests'])
ao_client = agentops.Client(tags=['mock tests'])


@ao_client.record_action('action')
Expand Down Expand Up @@ -68,3 +67,4 @@ async def main():

if __name__ == '__main__':
asyncio.run(main())
print('done')
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ envlist = py37, py38, py39, mypy
[testenv]
deps =
pytest
pytest-asyncio
requests_mock
coverage
pydantic
Expand Down

0 comments on commit 767b4d9

Please sign in to comment.