Skip to content

Commit

Permalink
decouple sessions and client
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Jun 11, 2024
1 parent 69e6dc0 commit cd9917b
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 149 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/black-formatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Black Code Formatter Check

on: [pull_request]

jobs:
black-check:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10' # Specify the version of Python you need

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black
- name: Run Black
run: black --check .
42 changes: 28 additions & 14 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import os
import logging
from typing import Optional, List, Union
from uuid import UUID

from .client import Client
from .config import Configuration
from .config import ClientConfiguration
from .event import Event, ActionEvent, LLMEvent, ToolEvent, ErrorEvent
from .enums import Models
from .decorators import record_function
from .decorators import record_function, track_route
from .agent import track_agent
from .log_config import logger

Expand All @@ -31,7 +33,7 @@ def init(
auto_start_session=True,
inherited_session_id: Optional[str] = None,
skip_auto_end_session: Optional[bool] = False,
):
) -> Union[str, None]:
"""
Initializes the AgentOps singleton pattern.
Expand All @@ -48,7 +50,7 @@ def init(
max_queue_size (int, optional): The maximum size of the event queue. Defaults to 100.
tags (List[str], optional): Tags for the sessions that can be used for grouping or
sorting later (e.g. ["GPT-4"]).
override (bool, optional): [Deprecated] Use `instrument_llm_calls` instead. Whether to instrument LLM calls and emit LLMEvents..
override (bool, optional): [Deprecated] Use `instrument_llm_calls` instead. Whether to instrument LLM calls and emit LLMEvents.
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
Expand Down Expand Up @@ -79,14 +81,20 @@ def init(
skip_auto_end_session=skip_auto_end_session,
)

return inherited_session_id or c.current_session_id
if inherited_session_id:
return inherited_session_id
elif len(c.current_session_ids) is 1:
return c.current_session_ids[0]
else:
return None


def end_session(
end_state: str,
end_state_reason: Optional[str] = None,
video: Optional[str] = None,
is_auto_end: Optional[bool] = False,
session_id: Optional[str] = None,
):
"""
End the current session with the AgentOps service.
Expand All @@ -96,59 +104,65 @@ def end_session(
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
session_id (str, optional): the session to end, if using multiple concurrent sessions
"""
Client().end_session(
end_state=end_state,
end_state_reason=end_state_reason,
video=video,
is_auto_end=is_auto_end,
session_id=session_id,
)


def start_session(
tags: Optional[List[str]] = None,
config: Optional[Configuration] = None,
config: Optional[ClientConfiguration] = None,
inherited_session_id: Optional[str] = None,
):
) -> Union[str, None]:
"""
Start a new session for recording events.
Args:
tags (List[str], optional): Tags that can be used for grouping or sorting later.
e.g. ["test_run"].
config: (Configuration, optional): Client configuration object
config: (Configuration, optional): Client configuration object,
inherited_session_id: (str, optional): Set the session ID to inherit from another client
"""
return Client().start_session(tags, config, inherited_session_id)


def record(event: Union[Event, ErrorEvent]):
def record(event: Union[Event, ErrorEvent], session_id: Optional[str] = None):
"""
Record an event with the AgentOps service.
Args:
event (Event): The event to record.
session_id (str, optional): the session to attribute the event to if using multiple concurrent sessions.
"""
Client().record(event)
Client().record(event, session_id)


def add_tags(tags: List[str]):
def add_tags(tags: List[str], session_id: Optional[str] = None):
"""
Append to session tags at runtime.
Args:
tags (List[str]): The list of tags to append.
session_id (str, optional): which session to add tags to if using multiple concurrent sessions
"""
Client().add_tags(tags)
Client().add_tags(tags, session_id)


def set_tags(tags: List[str]):
def set_tags(tags: List[str], session_id: Optional[str] = None):
"""
Replace session tags at runtime.
Args:
tags (List[str]): The list of tags to set.
session_id (str, optional): which session to set tags on if using multiple concurrent sessions
"""
Client().set_tags(tags)
Client().set_tags(tags, session_id)


def get_api_key() -> str:
Expand Down
Loading

0 comments on commit cd9917b

Please sign in to comment.