Skip to content

Commit

Permalink
Merge branch 'main' into host-env-opt-out
Browse files Browse the repository at this point in the history
# Conflicts:
#	agentops/client.py
  • Loading branch information
bboynton97 committed May 2, 2024
2 parents 8dab6a5 + d1bbee4 commit 568b9d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
17 changes: 9 additions & 8 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
Client: Provides methods to interact with the AgentOps service.
"""
import os

from .event import ActionEvent, ErrorEvent, Event
from .enums import EndState
from .helpers import get_ISO_time, singleton, check_call_stack_for_agent_id
Expand Down Expand Up @@ -76,7 +75,6 @@ def __init__(self,

self._session = None
self._worker = None
self._tags = tags

self._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv('AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true'

Expand All @@ -93,6 +91,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)
Expand All @@ -105,13 +105,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]):
Expand All @@ -121,7 +122,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
Expand Down Expand Up @@ -237,7 +238,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._env_data_opt_out))
self._session = Session(inherited_session_id or uuid4(), tags or self._tags_for_future_session, host_env=get_host_env(self._env_data_opt_out))
self._worker = Worker(config or self.config)
start_session_result = self._worker.start_session(self._session)
if not start_session_result:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 568b9d1

Please sign in to comment.