Skip to content

Commit

Permalink
agentops functions dont work without first calling init (#255)
Browse files Browse the repository at this point in the history
* block without init

* handle no tokens better

* fixed wrapper

* add decorators

* bump version

* init on start session
  • Loading branch information
bboynton97 authored Jun 13, 2024
1 parent 448d63c commit 961ba21
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
36 changes: 35 additions & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# agentops/__init__.py
import functools
import os
import logging
from typing import Optional, List, Union
Expand All @@ -18,6 +19,23 @@
except ModuleNotFoundError:
pass

is_initialized = False


def noop(*args, **kwargs):
return


def check_init(child_function):
@functools.wraps(child_function)
def wrapper(*args, **kwargs):
if is_initialized:
return child_function(*args, **kwargs)
else:
return noop(*args, **kwargs)

return wrapper


def init(
api_key: Optional[str] = None,
Expand Down Expand Up @@ -79,6 +97,9 @@ def init(
skip_auto_end_session=skip_auto_end_session,
)

global is_initialized
is_initialized = True

return inherited_session_id or c.current_session_id


Expand Down Expand Up @@ -118,9 +139,19 @@ def start_session(
e.g. ["test_run"].
config: (Configuration, optional): Client configuration object
"""
return Client().start_session(tags, config, inherited_session_id)

try:
sess_result = Client().start_session(tags, config, inherited_session_id)

global is_initialized
is_initialized = True

return sess_result
except Exception:
pass


@check_init
def record(event: Union[Event, ErrorEvent]):
"""
Record an event with the AgentOps service.
Expand All @@ -131,6 +162,7 @@ def record(event: Union[Event, ErrorEvent]):
Client().record(event)


@check_init
def add_tags(tags: List[str]):
"""
Append to session tags at runtime.
Expand All @@ -141,6 +173,7 @@ def add_tags(tags: List[str]):
Client().add_tags(tags)


@check_init
def set_tags(tags: List[str]):
"""
Replace session tags at runtime.
Expand Down Expand Up @@ -169,5 +202,6 @@ def stop_instrumenting():
Client().stop_instrumenting()


@check_init
def create_agent(name: str, agent_id: Optional[str] = None):
return Client().create_agent(name=name, agent_id=agent_id)
2 changes: 1 addition & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def end_session(
self._session.end_session(end_state, end_state_reason)
token_cost = self._worker.end_session(self._session)

if token_cost == "unknown":
if token_cost is None or token_cost == "unknown":
logger.info("Could not determine cost of run.")
else:
token_cost_d = Decimal(token_cost)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "agentops"
version = "0.2.2"
version = "0.2.3"
authors = [
{ name="Alex Reibman", email="[email protected]" },
{ name="Shawn Qiu", email="[email protected]" },
Expand Down
1 change: 0 additions & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def setup_method(self):

def test_session(self, mock_req):
agentops.start_session(config=self.config)
print(self.config.api_key)

agentops.record(ActionEvent(self.event_type))
agentops.record(ActionEvent(self.event_type))
Expand Down

0 comments on commit 961ba21

Please sign in to comment.