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

decouple sessions and client #248

Merged
merged 48 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
cd9917b
decouple sessions and client
bboynton97 Jun 11, 2024
c579cff
formatter test
bboynton97 Jun 11, 2024
bb83806
formatter test
bboynton97 Jun 11, 2024
82f5c16
tests
bboynton97 Jun 11, 2024
9de1dfa
merge main
bboynton97 Jun 11, 2024
2d09428
black formatting
bboynton97 Jun 11, 2024
74a7190
assign llm calls to sessions
bboynton97 Jun 13, 2024
e3b5cac
Merge branch 'main' into multiple-sessions
bboynton97 Jun 13, 2024
132345e
worker and tracker changes
bboynton97 Jun 14, 2024
48b5b47
use jwt for session ids
bboynton97 Jun 14, 2024
49879f2
test fixes
bboynton97 Jun 15, 2024
abb6587
end all sessions
bboynton97 Jun 17, 2024
8ea0177
test: fix session tests
bboynton97 Jun 17, 2024
0149ee7
reset env between tests
bboynton97 Jun 17, 2024
9210b55
formatting
bboynton97 Jun 17, 2024
c0f984f
Merge branch 'main' into multiple-sessions
bboynton97 Jun 17, 2024
8dabd05
add worker to session instead of client
bboynton97 Jun 20, 2024
d6e7470
Merge branch 'main' into multiple-sessions
bboynton97 Jun 21, 2024
73f395a
test fixes!!!
bboynton97 Jun 21, 2024
688a883
Merge branch 'main' into multiple-sessions
bboynton97 Jun 21, 2024
f1f10de
test fixes
bboynton97 Jun 22, 2024
408e45e
test coverage
bboynton97 Jun 22, 2024
04d73da
singleton tests
bboynton97 Jun 24, 2024
21118b3
tracker fixes
bboynton97 Jun 24, 2024
763a161
record function multiple sessions
bboynton97 Jun 25, 2024
94a5866
async tests
bboynton97 Jun 26, 2024
9120b79
test fix
bboynton97 Jun 26, 2024
d98c5e7
block record function if multiple sessions
bboynton97 Jun 26, 2024
70ef005
manual tests
bboynton97 Jun 26, 2024
6f96df7
session end fix
bboynton97 Jun 27, 2024
10eaff0
manual server test
bboynton97 Jun 27, 2024
5cb7204
notebook tests
bboynton97 Jun 28, 2024
7cadfd4
Merge branch 'main' into multiple-sessions
bboynton97 Jul 1, 2024
c14cc4a
merge main
bboynton97 Jul 1, 2024
071000f
black formatting
bboynton97 Jul 1, 2024
6158cd2
cleanup
bboynton97 Jul 2, 2024
990cfb9
docs
bboynton97 Jul 2, 2024
1b82054
moar docs
bboynton97 Jul 2, 2024
83b171c
add patch function
bboynton97 Jul 3, 2024
f807e61
multiple sessions docs
bboynton97 Jul 3, 2024
9e8c799
multisession example
bboynton97 Jul 8, 2024
0971f36
fix tests
bboynton97 Jul 8, 2024
ce89b06
Merge branch 'main' into multiple-sessions
bboynton97 Jul 8, 2024
e07feb8
review fixes
bboynton97 Jul 9, 2024
0488189
import fix
bboynton97 Jul 9, 2024
8d1afc7
comments
bboynton97 Jul 9, 2024
ef36b5c
Merge branch 'main' into multiple-sessions
bboynton97 Jul 9, 2024
27a2f4a
helper functions
bboynton97 Jul 10, 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
1 change: 1 addition & 0 deletions .github/workflows/black-formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ jobs:
python -m pip install --upgrade pip
pip install black
pip install "black[jupyter]"

- name: Run Black
run: black --diff --check .
41 changes: 26 additions & 15 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import os
import logging
from typing import Optional, List, Union

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

try:
from .partners.langchain_callback_handler import (
Expand Down Expand Up @@ -43,12 +45,11 @@ def init(
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
tags: Optional[List[str]] = None,
override: Optional[bool] = None, # Deprecated
instrument_llm_calls=True,
auto_start_session=True,
inherited_session_id: Optional[str] = None,
skip_auto_end_session: Optional[bool] = False,
):
) -> Union[Session, None]:
"""
Initializes the AgentOps singleton pattern.

Expand All @@ -65,11 +66,10 @@ 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..
instrument_llm_calls (bool): 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
skip_auto_end_session (optional, bool): Don't automatically end session based on your framework's decision making
skip_auto_end_session (optional, bool): Don't automatically end session based on your framework's decision-making
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved
Attributes:
"""
logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL")
Expand All @@ -89,17 +89,23 @@ def init(
max_wait_time=max_wait_time,
max_queue_size=max_queue_size,
tags=tags,
override=override,
instrument_llm_calls=instrument_llm_calls,
auto_start_session=auto_start_session,
auto_start_session=False, # handled below
inherited_session_id=inherited_session_id,
skip_auto_end_session=skip_auto_end_session,
)

# handle auto_start_session here so we can get the session object to return rather than client above
session = None
if auto_start_session:
session = c.start_session(
tags=tags, config=c.config, inherited_session_id=inherited_session_id
)

global is_initialized
is_initialized = True

return inherited_session_id or c.current_session_id
return session


def end_session(
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -125,18 +131,25 @@ def end_session(
)


# Mostly used for unit testing -
# prevents unexpected sessions on new tests
def end_all_sessions() -> None:
return Client().end_all_sessions()


def start_session(
tags: Optional[List[str]] = None,
config: Optional[Configuration] = None,
config: Optional[ClientConfiguration] = None,
inherited_session_id: Optional[str] = None,
):
) -> Union[Session, 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
"""

try:
Expand All @@ -161,7 +174,6 @@ def record(event: Union[Event, ErrorEvent]):
Client().record(event)


@check_init
def add_tags(tags: List[str]):
"""
Append to session tags at runtime.
Expand All @@ -172,7 +184,6 @@ def add_tags(tags: List[str]):
Client().add_tags(tags)


@check_init
def set_tags(tags: List[str]):
"""
Replace session tags at runtime.
Expand All @@ -189,7 +200,7 @@ def get_api_key() -> str:

def set_parent_key(parent_key):
"""
Set the parent API key which has visibility to projects it is parent to.
Set the parent API key which has visibility to projects it is parented to.
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved

Args:
parent_key (str): The API key of the parent organization to set.
Expand Down
9 changes: 8 additions & 1 deletion agentops/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@
try:
original_init(self, *args, **kwargs)
self.agent_ops_agent_id = str(uuid4())

session = kwargs.get("session", None)

Check warning on line 22 in agentops/agent.py

View check run for this annotation

Codecov / codecov/patch

agentops/agent.py#L22

Added line #L22 was not covered by tests
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved
if session is not None:
self.agent_ops_session_id = session.session_id

Check warning on line 24 in agentops/agent.py

View check run for this annotation

Codecov / codecov/patch

agentops/agent.py#L24

Added line #L24 was not covered by tests
bboynton97 marked this conversation as resolved.
Show resolved Hide resolved

Client().create_agent(
name=self.agent_ops_agent_name, agent_id=self.agent_ops_agent_id
name=self.agent_ops_agent_name,
agent_id=self.agent_ops_agent_id,
session=session,
)
except AttributeError as e:
logger.warning(
Expand Down
Loading
Loading