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

dont add duplicate tags #177

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 9 additions & 7 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def __init__(self,

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

try:
self.config = Configuration(api_key=api_key,
Expand All @@ -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)
Expand All @@ -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]):
Expand All @@ -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
Expand Down Expand Up @@ -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:
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
Loading