From 8a4bc4f2958b20ff18bf33a6f4d5fb7ba2c9333c Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Wed, 12 Jun 2024 21:29:16 -0700 Subject: [PATCH 1/6] block without init --- agentops/__init__.py | 21 +++++++++++++++++++++ tests/test_session.py | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/agentops/__init__.py b/agentops/__init__.py index 2edf10f8..7b80426a 100755 --- a/agentops/__init__.py +++ b/agentops/__init__.py @@ -18,6 +18,19 @@ except ModuleNotFoundError: pass +is_initialized = False + + +def noop(*args, **kwargs): + return + + +def check_init(child_function, *args, **kwargs): + if is_initialized: + return child_function(*args, **kwargs) + else: + return noop + def init( api_key: Optional[str] = None, @@ -79,6 +92,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 @@ -105,6 +121,7 @@ def end_session( ) +@check_init def start_session( tags: Optional[List[str]] = None, config: Optional[Configuration] = None, @@ -121,6 +138,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. @@ -131,6 +149,7 @@ def record(event: Union[Event, ErrorEvent]): Client().record(event) +@check_init def add_tags(tags: List[str]): """ Append to session tags at runtime. @@ -141,6 +160,7 @@ def add_tags(tags: List[str]): Client().add_tags(tags) +@check_init def set_tags(tags: List[str]): """ Replace session tags at runtime. @@ -169,5 +189,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) diff --git a/tests/test_session.py b/tests/test_session.py index e8a1bf14..effe00fa 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -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)) From 2e4a0c54841fa57ac18e926801f67e189c3109cc Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 13 Jun 2024 11:52:19 -0700 Subject: [PATCH 2/6] handle no tokens better --- agentops/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agentops/client.py b/agentops/client.py index 57f9cb60..7108e393 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -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) From 46bed08ba17dc07a089c20ae3370160217e44157 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 13 Jun 2024 12:40:23 -0700 Subject: [PATCH 3/6] fixed wrapper --- agentops/__init__.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/agentops/__init__.py b/agentops/__init__.py index 7b80426a..b8a43e62 100755 --- a/agentops/__init__.py +++ b/agentops/__init__.py @@ -1,4 +1,5 @@ # agentops/__init__.py +import functools import os import logging from typing import Optional, List, Union @@ -25,11 +26,15 @@ def noop(*args, **kwargs): return -def check_init(child_function, *args, **kwargs): - if is_initialized: - return child_function(*args, **kwargs) - else: - return noop +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( @@ -138,7 +143,7 @@ def start_session( return Client().start_session(tags, config, inherited_session_id) -@check_init +# @check_init def record(event: Union[Event, ErrorEvent]): """ Record an event with the AgentOps service. @@ -149,7 +154,7 @@ def record(event: Union[Event, ErrorEvent]): Client().record(event) -@check_init +# @check_init def add_tags(tags: List[str]): """ Append to session tags at runtime. @@ -160,7 +165,7 @@ def add_tags(tags: List[str]): Client().add_tags(tags) -@check_init +# @check_init def set_tags(tags: List[str]): """ Replace session tags at runtime. @@ -189,6 +194,6 @@ def stop_instrumenting(): Client().stop_instrumenting() -@check_init +# @check_init def create_agent(name: str, agent_id: Optional[str] = None): return Client().create_agent(name=name, agent_id=agent_id) From d7dacce68061cf3cf95c4dcaa2d8a468b03f3992 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 13 Jun 2024 12:43:34 -0700 Subject: [PATCH 4/6] add decorators --- agentops/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/agentops/__init__.py b/agentops/__init__.py index b8a43e62..2bc45276 100755 --- a/agentops/__init__.py +++ b/agentops/__init__.py @@ -143,7 +143,7 @@ def start_session( return Client().start_session(tags, config, inherited_session_id) -# @check_init +@check_init def record(event: Union[Event, ErrorEvent]): """ Record an event with the AgentOps service. @@ -154,7 +154,7 @@ def record(event: Union[Event, ErrorEvent]): Client().record(event) -# @check_init +@check_init def add_tags(tags: List[str]): """ Append to session tags at runtime. @@ -165,7 +165,7 @@ def add_tags(tags: List[str]): Client().add_tags(tags) -# @check_init +@check_init def set_tags(tags: List[str]): """ Replace session tags at runtime. @@ -194,6 +194,6 @@ def stop_instrumenting(): Client().stop_instrumenting() -# @check_init +@check_init def create_agent(name: str, agent_id: Optional[str] = None): return Client().create_agent(name=name, agent_id=agent_id) From 35ca376df7e0b1400b06ab1a1236d6ad7b29af0b Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 13 Jun 2024 12:44:35 -0700 Subject: [PATCH 5/6] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 8ea93afc..4324f5c0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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="areibman@gmail.com" }, { name="Shawn Qiu", email="siyangqiu@gmail.com" }, From ec4a695908f0c66a7fc68f1ceb21c98e5ea40e73 Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 13 Jun 2024 12:51:15 -0700 Subject: [PATCH 6/6] init on start session --- agentops/__init__.py | 12 ++++++++++-- tests/test_session.py | 1 - 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/agentops/__init__.py b/agentops/__init__.py index 2bc45276..1335f6a9 100755 --- a/agentops/__init__.py +++ b/agentops/__init__.py @@ -126,7 +126,6 @@ def end_session( ) -@check_init def start_session( tags: Optional[List[str]] = None, config: Optional[Configuration] = None, @@ -140,7 +139,16 @@ 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 diff --git a/tests/test_session.py b/tests/test_session.py index effe00fa..7148a008 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -26,7 +26,6 @@ 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)