From 99183ed0aace798282eacc226c51fb4d28c19818 Mon Sep 17 00:00:00 2001 From: Shawn Qiu Date: Wed, 22 May 2024 13:09:26 -0700 Subject: [PATCH] fix: indent error event and fix types (#216) * bugfix error event and fix types * remove properties based on review feedback * remove duplicate line * bump version number --- agentops/client.py | 36 ++++++++++++++++++++++-------------- agentops/event.py | 7 ++++--- pyproject.toml | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index 22103c12..bd0e89db 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -80,8 +80,8 @@ def __init__(self, self._tags: Optional[List[str]] = tags self._tags_for_future_session: Optional[List[str]] = None - self._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv( - 'AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true' + self._env_data_opt_out = os.environ.get( + 'AGENTOPS_ENV_DATA_OPT_OUT', 'False').lower() == 'true' try: self.config = Configuration(api_key=api_key, @@ -89,6 +89,11 @@ def __init__(self, endpoint=endpoint, max_wait_time=max_wait_time, max_queue_size=max_queue_size) + + if inherited_session_id is not None: + # Check if inherited_session_id is valid + UUID(inherited_session_id) + except ConfigurationError: return @@ -106,7 +111,7 @@ def __init__(self, self.llm_tracker = LlmTracker(self) self.llm_tracker.override_api() - def _check_for_partner_frameworks(self, instrument_llm_calls, auto_start_session) -> (bool, bool): + def _check_for_partner_frameworks(self, instrument_llm_calls, auto_start_session) -> tuple[bool, bool]: partner_frameworks = get_partner_frameworks() for framework in partner_frameworks.keys(): if framework in sys.modules: @@ -119,7 +124,9 @@ def _check_for_partner_frameworks(self, instrument_llm_calls, auto_start_session pass except Exception as e: logger.warning( - "Failed to set up autogen logger with AgentOps. Error: " + e) + f"Failed to set up autogen logger with AgentOps. Error: {e}") + + return partner_frameworks[framework] return instrument_llm_calls, auto_start_session @@ -145,7 +152,7 @@ def add_tags(self, tags: List[str]): else: if self._tags_for_future_session: for tag in tags: - if tag not in self._session.tags: + if tag not in self._tags_for_future_session: self._tags_for_future_session.append(tag) else: self._tags_for_future_session = tags @@ -182,10 +189,10 @@ def record(self, event: Union[Event, ErrorEvent]): if not event.trigger_event.end_timestamp or event.trigger_event.init_timestamp == event.trigger_event.end_timestamp: event.trigger_event.end_timestamp = get_ISO_time() - event.trigger_event_id = event.trigger_event.id - event.trigger_event_type = event.trigger_event.event_type - self._worker.add_event(event.trigger_event.__dict__) - event.trigger_event = None # removes trigger_event from serialization + event.trigger_event_id = event.trigger_event.id + event.trigger_event_type = event.trigger_event.event_type + self._worker.add_event(event.trigger_event.__dict__) + event.trigger_event = None # removes trigger_event from serialization self._worker.add_event(event.__dict__) @@ -287,8 +294,9 @@ def start_session(self, tags: Optional[List[str]] = None, config: Optional[Confi session_id = UUID( inherited_session_id) if inherited_session_id is not None else uuid4() - 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._session = Session(session_id=session_id, + tags=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: @@ -334,9 +342,9 @@ def end_session(self, self._session = None self._worker = None - def create_agent(self, name: str, agent_id: Optional[str] = None) -> str: + def create_agent(self, name: str, agent_id: Optional[str] = None): if agent_id is None: - agent_id = str(uuid.uuid4()) + agent_id = str(uuid4()) if self._worker: self._worker.create_agent(name=name, agent_id=agent_id) return agent_id @@ -382,7 +390,7 @@ def handle_exception(exc_type, exc_value, exc_traceback): sys.__excepthook__(exc_type, exc_value, exc_traceback) # if main thread - if isinstance(threading.current_thread(), threading._MainThread): + if threading.current_thread() is threading.main_thread(): atexit.register(lambda: cleanup(end_state="Indeterminate", end_state_reason="Process exited without calling end_session()")) signal.signal(signal.SIGINT, signal_handler) diff --git a/agentops/event.py b/agentops/event.py index 71b43519..497aeb9e 100644 --- a/agentops/event.py +++ b/agentops/event.py @@ -37,12 +37,13 @@ class Event: } """ - event_type: str # EventType.ENUM.value + event_type: EventType params: Optional[dict] = None - returns: Optional[str] = None + returns: Optional[str | List[str]] = None init_timestamp: str = field(default_factory=get_ISO_time) end_timestamp: Optional[str] = None - agent_id: Optional[UUID] = field(default_factory=check_call_stack_for_agent_id) + agent_id: Optional[UUID] = field( + default_factory=check_call_stack_for_agent_id) id: UUID = field(default_factory=uuid4) diff --git a/pyproject.toml b/pyproject.toml index 68b447c3..772c33e9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "agentops" -version = "0.1.11" +version = "0.1.12" authors = [ { name="Alex Reibman", email="areibman@gmail.com" }, { name="Shawn Qiu", email="siyangqiu@gmail.com" },