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

[DO NOT MERGE] Refactoring core session management #486

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b7ddf3a
refactor(config.py): simplify Configuration class by using dataclass
teocns Nov 1, 2024
70c229a
feat(config.py): add graceful_shutdown_wait_time attribute to Configu…
teocns Nov 12, 2024
d70d6fe
refactor(session): restructure session management and threading
teocns Nov 12, 2024
e1c9b91
chore(pyproject): add "-s" to pytest addopts
teocns Nov 12, 2024
28709a0
fixup! refactor(session): restructure session management and threading
teocns Nov 12, 2024
e991472
Remove EventsCounter
teocns Nov 12, 2024
eb15582
feat(session): add debug logging for session events and state
teocns Nov 12, 2024
85b18dd
fix construction (use dataclass); adjust initialization order
teocns Nov 12, 2024
7078637
refactor(session): change SessionStruct to SessionDict
teocns Nov 12, 2024
3831e1e
privatize locks attr
teocns Nov 12, 2024
eaad74b
clean
teocns Nov 12, 2024
d36440f
Assign `config` as object-level member
teocns Nov 12, 2024
ff399f9
streamline setting of `jwt` in SessionApi - reflect Session's
teocns Nov 12, 2024
f5d53ea
Correct SessionApi initialization
teocns Nov 12, 2024
2044f73
refactor(session): update batch method to use api.batch()
teocns Nov 12, 2024
df3fd74
style(session.py): format code and import statements
teocns Nov 12, 2024
a350785
Fix event_type accessor
teocns Nov 12, 2024
009eddd
Remove exc suppression
teocns Nov 12, 2024
b43a45f
improve SessionDict init
teocns Nov 12, 2024
4218243
fix Sesssion self access
teocns Nov 12, 2024
066715d
misc queue fixes
teocns Nov 12, 2024
266347e
clean imports
teocns Nov 12, 2024
1c71408
feat(session): add active property to Session class
teocns Nov 12, 2024
fed537b
refactor(session): streamline type hints and properties
teocns Nov 12, 2024
47a934f
refactor(session): streamline exception handling logic
teocns Nov 12, 2024
8838b52
make the threads stop properly when requested and prevent the infinit…
teocns Nov 12, 2024
c91f52b
misc
teocns Nov 12, 2024
6bdf714
refactor(session): remove deprecated active property and clean up
teocns Nov 12, 2024
7aca074
improve logging
teocns Nov 12, 2024
00c2319
feat(session): add active property for backward compatibility
teocns Nov 12, 2024
1f00391
refactor(session): clean up end_session and stop_session logic
teocns Nov 12, 2024
ce45bc9
weakset, reference tracking, etc
teocns Nov 12, 2024
561dd2a
SessionsCollection: add custom iterator for sorted sessions
teocns Nov 12, 2024
c73e93e
introduce SessionsCollection
teocns Nov 12, 2024
a7e527f
refactor(client): convert active_sessions to a list
teocns Nov 12, 2024
bfca31a
fixes test_session.TestSingleSessions
teocns Nov 12, 2024
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
2 changes: 1 addition & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self):
self._pre_init_messages: List[str] = []
self._initialized: bool = False
self._llm_tracker: Optional[LlmTracker] = None
self._sessions: List[Session] = active_sessions
self._sessions: List[Session] = list(active_sessions)
self._config = Configuration()
self._pre_init_queue = {"agents": []}

Comment on lines 36 to 42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Performance Improvement

Ensure Explicit List Conversion for Robustness

The change from active_sessions to list(active_sessions) ensures that _sessions is explicitly a list, which is crucial if active_sessions is a mutable sequence like a set or another iterable. This prevents unintended side effects from external modifications to active_sessions, enhancing the robustness of session management.

-        self._sessions: List[Session] = active_sessions
+        self._sessions: List[Session] = list(active_sessions)
Commitable Code Suggestion:
Suggested change
self._pre_init_messages: List[str] = []
self._initialized: bool = False
self._llm_tracker: Optional[LlmTracker] = None
self._sessions: List[Session] = active_sessions
self._sessions: List[Session] = list(active_sessions)
self._config = Configuration()
self._pre_init_queue = {"agents": []}
self._sessions: List[Session] = list(active_sessions)

Expand Down
105 changes: 42 additions & 63 deletions agentops/config.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,53 @@
from typing import List, Optional
from dataclasses import dataclass, field
from typing import Optional, Set
from uuid import UUID

from .log_config import logger

# TODO: Use annotations to clarify the purpose of each attribute.
# Details are defined in a docstrings found in __init__.py, but
# it's good to have those right on the fields at class definition

@dataclass
class Configuration:
def __init__(self):
self.api_key: Optional[str] = None
self.parent_key: Optional[str] = None
self.endpoint: str = "https://api.agentops.ai"
self.max_wait_time: int = 5000
self.max_queue_size: int = 512
self.default_tags: set[str] = set()
self.instrument_llm_calls: bool = True
self.auto_start_session: bool = True
self.skip_auto_end_session: bool = False
self.env_data_opt_out: bool = False
api_key: Optional[str] = None
parent_key: Optional[str] = None
endpoint: str = "https://api.agentops.ai"
max_wait_time: int = 5000
max_queue_size: int = 512
graceful_shutdown_wait_time: int = 2000
default_tags: Set[str] = field(default_factory=set)
instrument_llm_calls: bool = True
auto_start_session: bool = True
skip_auto_end_session: bool = False
env_data_opt_out: bool = False

def configure(
self,
client,
api_key: Optional[str] = None,
parent_key: Optional[str] = None,
endpoint: Optional[str] = None,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
default_tags: Optional[List[str]] = None,
instrument_llm_calls: Optional[bool] = None,
auto_start_session: Optional[bool] = None,
skip_auto_end_session: Optional[bool] = None,
env_data_opt_out: Optional[bool] = None,
**kwargs
):
if api_key is not None:
try:
UUID(api_key)
self.api_key = api_key
except ValueError:
message = f"API Key is invalid: {{{api_key}}}.\n\t Find your API key at https://app.agentops.ai/settings/projects"
client.add_pre_init_warning(message)
logger.error(message)

if parent_key is not None:
try:
UUID(parent_key)
self.parent_key = parent_key
except ValueError:
message = f"Parent Key is invalid: {parent_key}"
client.add_pre_init_warning(message)
logger.warning(message)

if endpoint is not None:
self.endpoint = endpoint

if max_wait_time is not None:
self.max_wait_time = max_wait_time

if max_queue_size is not None:
self.max_queue_size = max_queue_size

if default_tags is not None:
self.default_tags.update(default_tags)

if instrument_llm_calls is not None:
self.instrument_llm_calls = instrument_llm_calls

if auto_start_session is not None:
self.auto_start_session = auto_start_session

if skip_auto_end_session is not None:
self.skip_auto_end_session = skip_auto_end_session

if env_data_opt_out is not None:
self.env_data_opt_out = env_data_opt_out
# Special handling for keys that need UUID validation
for key_name in ['api_key', 'parent_key']:
if key_name in kwargs and kwargs[key_name] is not None:
try:
UUID(kwargs[key_name])
setattr(self, key_name, kwargs[key_name])
except ValueError:
message = (
f"API Key is invalid: {{{kwargs[key_name]}}}.\n\t Find your API key at https://app.agentops.ai/settings/projects"
if key_name == 'api_key'
else f"Parent Key is invalid: {kwargs[key_name]}"
)
client.add_pre_init_warning(message)
logger.error(message) if key_name == 'api_key' else logger.warning(message)
kwargs.pop(key_name)

# Special handling for default_tags which needs update() instead of assignment
if 'default_tags' in kwargs and kwargs['default_tags'] is not None:
self.default_tags.update(kwargs.pop('default_tags'))

# Handle all other attributes
for key, value in kwargs.items():
if value is not None and hasattr(self, key):
setattr(self, key, value)
Loading
Loading