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

agentops functions dont work without first calling init #255

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 26 additions & 0 deletions 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 All @@ -105,6 +126,7 @@ def end_session(
)


@check_init
def start_session(
tags: Optional[List[str]] = None,
config: Optional[Configuration] = None,
Expand All @@ -121,6 +143,7 @@ def start_session(
return Client().start_session(tags, config, inherited_session_id)


@check_init
def record(event: Union[Event, ErrorEvent]):
"""
Record an event with the AgentOps service.
Expand All @@ -131,6 +154,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 +165,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 +194,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
2 changes: 1 addition & 1 deletion tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ 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)
agentops.init(api_key=self.api_key)

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
Loading