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

support to skip crew ending sessions automatically #231

Merged
merged 4 commits into from
May 30, 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: 14 additions & 2 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def init(
instrument_llm_calls=True,
auto_start_session=True,
inherited_session_id: Optional[str] = None,
skip_auto_end_session: Optional[bool] = False,
):
"""
Initializes the AgentOps singleton pattern.
Expand All @@ -51,6 +52,7 @@ def init(
instrument_llm_calls (bool): Whether to instrument LLM calls and emit LLMEvents..
auto_start_session (bool): Whether to start a session automatically when the client is created.
inherited_session_id (optional, str): Init Agentops with an existing Session
skip_auto_end_session (optional, bool): Don't automatically end session based on your framework's decision making
Attributes:
"""
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL")
Expand All @@ -74,13 +76,17 @@ def init(
instrument_llm_calls=instrument_llm_calls,
auto_start_session=auto_start_session,
inherited_session_id=inherited_session_id,
skip_auto_end_session=skip_auto_end_session,
)

return inherited_session_id or c.current_session_id


def end_session(
end_state: str, end_state_reason: Optional[str] = None, video: Optional[str] = None
end_state: str,
end_state_reason: Optional[str] = None,
video: Optional[str] = None,
is_auto_end: Optional[bool] = False,
):
"""
End the current session with the AgentOps service.
Expand All @@ -89,8 +95,14 @@ def end_session(
end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): URL to a video recording of the session
is_auto_end (bool, optional): is this an automatic use of end_session and should be skipped with bypass_auto_end_session
"""
Client().end_session(end_state, end_state_reason, video)
Client().end_session(
end_state=end_state,
end_state_reason=end_state_reason,
video=video,
is_auto_end=is_auto_end,
)


def start_session(
Expand Down
8 changes: 8 additions & 0 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Client(metaclass=MetaClient):
instrument_llm_calls (bool): Whether to instrument LLM calls and emit LLMEvents..
auto_start_session (bool): Whether to start a session automatically when the client is created.
inherited_session_id (optional, str): Init Agentops with an existing Session
skip_auto_end_session (optional, bool): Don't automatically end session based on your framework's decision making
Attributes:
_session (Session, optional): A Session is a grouping of events (e.g. a run of your agent).
_worker (Worker, optional): A Worker manages the event queue and sends session updates to the AgentOps api
Expand All @@ -76,6 +77,7 @@ def __init__(
instrument_llm_calls=True,
auto_start_session=True,
inherited_session_id: Optional[str] = None,
skip_auto_end_session: Optional[bool] = False,
):

if override is not None:
Expand All @@ -102,6 +104,7 @@ def __init__(
endpoint=endpoint,
max_wait_time=max_wait_time,
max_queue_size=max_queue_size,
skip_auto_end_session=skip_auto_end_session,
)

if inherited_session_id is not None:
Expand Down Expand Up @@ -366,6 +369,7 @@ def end_session(
end_state: str,
end_state_reason: Optional[str] = None,
video: Optional[str] = None,
is_auto_end: Optional[bool] = None,
):
"""
End the current session with the AgentOps service.
Expand All @@ -374,8 +378,12 @@ def end_session(
end_state (str): The final state of the session. Options: Success, Fail, or Indeterminate.
end_state_reason (str, optional): The reason for ending the session.
video (str, optional): The video screen recording of the session
is_auto_end (bool, optional): is this an automatic use of end_session and should be skipped with skip_auto_end_session
"""

if is_auto_end and self.config.skip_auto_end_session:
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved
return

if self._session is None or self._session.has_ended:
return logger.warning("Cannot end session - no current session")

Expand Down
6 changes: 6 additions & 0 deletions agentops/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
endpoint: Optional[str] = None,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
skip_auto_end_session: Optional[bool] = False,
):

if not api_key:
Expand All @@ -52,6 +53,7 @@ def __init__(
self._max_wait_time = max_wait_time or 5000
self._max_queue_size = max_queue_size or 100
self._parent_key: Optional[str] = parent_key
self._skip_auto_end_session: Optional[bool] = skip_auto_end_session

@property
def api_key(self) -> str:
Expand Down Expand Up @@ -137,6 +139,10 @@ def max_queue_size(self, value: int):
def parent_key(self):
return self._parent_key

@property
def skip_auto_end_session(self):
return self._skip_auto_end_session

@parent_key.setter
def parent_key(self, value: str):
self._parent_key = value
Expand Down
8 changes: 8 additions & 0 deletions docs/v1/integrations/crewai.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ Crew has comprehensive [documentation](https://docs.crewai.com) available as wel
</Step>
</Steps>

## Special Considerations with Crew
The Crew framework is capable of determining when all tasks have been accomplished and to halt execution. AgentOps will automatically end your active session
when this determination is made. If you don't want your AgentOps session to end at this time, add an optional parameter to your `agentops.init()` call.

```python
agentops.init(skip_auto_end_session=True)
```

## Crew + AgentOps Examples

<CardGroup cols={3}>
Expand Down
1 change: 1 addition & 0 deletions docs/v1/usage/sdk-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The first element of AgentOps is always calling .init()
- `instrument_llm_calls` (bool): Whether to instrument LLM calls and emit LLMEvents.
- `auto_start_session` (bool): Whether to start a session automatically when the client is created. You may wish to delay starting a session in order to do additional setup or starting a session on a child process.
- `inherited_session_id` (str, optional): When creating the client, passing in this value will connect the client to an existing session. This is useful when having separate processes contribute to the same session.
- `skip_auto_end_session` (bool, optional): If you are using a framework such as Crew, the framework can decide when to halt execution. Setting this parameter to true will not end your agentops session when this happens.

**Returns**:

Expand Down
Loading