From d1bbee4cdd27e71dbb99cc462cb18ff11e28032c Mon Sep 17 00:00:00 2001 From: Braelyn Boynton Date: Thu, 2 May 2024 14:33:55 -0700 Subject: [PATCH] dont add duplicate tags (#177) * dont add duplicate tags * cleaned up tag confusion --- agentops/client.py | 16 +++++++++------- tests/test_session.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index 2aa90e00..cca33753 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -75,7 +75,6 @@ def __init__(self, self._session = None self._worker = None - self._tags = tags try: self.config = Configuration(api_key=api_key, @@ -90,6 +89,8 @@ def __init__(self, if auto_start_session: self.start_session(tags, self.config, inherited_session_id) + else: + self._tags_for_future_session = tags if instrument_llm_calls: self.llm_tracker = LlmTracker(self) @@ -102,13 +103,14 @@ def add_tags(self, tags: List[str]): Args: tags (List[str]): The list of tags to append. """ - if self._tags is not None: - self._tags.extend(tags) + if self._session.tags is not None: + for tag in tags: + if tag not in self._session.tags: + self._session.tags.append(tag) else: - self._tags = tags + self._session.tags = tags if self._session is not None: - self._session.tags = self._tags self._worker.update_session(self._session) def set_tags(self, tags: List[str]): @@ -118,7 +120,7 @@ def set_tags(self, tags: List[str]): Args: tags (List[str]): The list of tags to set. """ - self._tags = tags + self._tags_for_future_session = tags if self._session is not None: self._session.tags = tags @@ -234,7 +236,7 @@ def start_session(self, tags: Optional[List[str]] = None, config: Optional[Confi if not config and not self.config: return logger.warning("🖇 AgentOps: Cannot start session - missing configuration") - self._session = Session(inherited_session_id or uuid4(), tags or self._tags, host_env=get_host_env()) + self._session = Session(inherited_session_id or uuid4(), tags or self._tags_for_future_session, host_env=get_host_env()) self._worker = Worker(config or self.config) start_session_result = self._worker.start_session(self._session) if not start_session_result: diff --git a/tests/test_session.py b/tests/test_session.py index c553e7bd..d7b92f88 100644 --- a/tests/test_session.py +++ b/tests/test_session.py @@ -48,6 +48,25 @@ def test_session(self, mock_req): assert request_json['session']['end_state'] == end_state assert request_json['session']['tags'] == None + def test_add_tags(self, mock_req): + # Arrange + tags = ['GPT-4'] + agentops.start_session(tags=tags, config=self.config) + agentops.add_tags(['test-tag', 'dupe-tag']) + agentops.add_tags(['dupe-tag']) + + # Act + end_state = 'Success' + agentops.end_session(end_state) + time.sleep(0.15) + + # Assert 3 requests, 1 for session init, 1 for event, 1 for end session + assert mock_req.last_request.headers['X-Agentops-Auth'] == self.api_key + request_json = mock_req.last_request.json() + assert request_json['session']['end_state'] == end_state + assert request_json['session']['tags'] == ['GPT-4', 'test-tag', 'dupe-tag'] + + def test_tags(self, mock_req): # Arrange tags = ['GPT-4']