Skip to content

Commit

Permalink
Autogen support (#204)
Browse files Browse the repository at this point in the history
* bump project version

* fix adding tags if a session doesnt exist yet

* autogen support

* autogen support

* version fix

* add crew to partners
  • Loading branch information
bboynton97 authored May 14, 2024
1 parent c614d64 commit ea6328d
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 19 deletions.
7 changes: 5 additions & 2 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# agentops/__init__.py
from os import environ
from typing import Optional, List, Union

from .client import Client
from .config import Configuration
from .event import Event, ActionEvent, LLMEvent, ToolEvent, ErrorEvent
Expand All @@ -14,6 +12,7 @@
except ModuleNotFoundError:
pass


def init(api_key: Optional[str] = None,
parent_key: Optional[str] = None,
endpoint: Optional[str] = None,
Expand Down Expand Up @@ -134,3 +133,7 @@ def set_parent_key(parent_key):

def stop_instrumenting():
Client().stop_instrumenting()


def create_agent(name: str, agent_id: Optional[str] = None):
return Client().create_agent(name=name, agent_id=agent_id)
63 changes: 48 additions & 15 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
Client: Provides methods to interact with the AgentOps service.
"""
import os
import uuid

from .event import ActionEvent, ErrorEvent, Event
from .enums import EndState
from .helpers import get_ISO_time, singleton, check_call_stack_for_agent_id
from .helpers import get_ISO_time, singleton, check_call_stack_for_agent_id, get_partner_frameworks
from .session import Session
from .worker import Worker
from .host_env import get_host_env
Expand Down Expand Up @@ -37,22 +39,24 @@ class Client(metaclass=MetaClient):
api_key (str, optional): API Key for AgentOps services. If none is provided, key will
be read from the AGENTOPS_API_KEY environment variable.
parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization. If none is provided, key will
be read from the AGENTOPS_PARENT_KEY environment variable.
parent_key (str, optional): Organization key to give visibility of all user sessions the user's organization.
If none is provided, key will be read from the AGENTOPS_PARENT_KEY environment variable.
endpoint (str, optional): The endpoint for the AgentOps service. If none is provided, key will
be read from the AGENTOPS_API_ENDPOINT environment variable. Defaults to 'https://api.agentops.ai'.
max_wait_time (int, optional): The maximum time to wait in milliseconds before flushing the queue.
Defaults to 30,000 (30 seconds)
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
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 server
_worker (Worker, optional): A Worker manages the event queue and sends session updates to the AgentOps api
server
"""

def __init__(self,
Expand All @@ -78,6 +82,7 @@ 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'

Expand All @@ -92,6 +97,8 @@ def __init__(self,

self._handle_unclean_exits()

instrument_llm_calls, auto_start_session = self._check_for_partner_frameworks(instrument_llm_calls, auto_start_session)

if auto_start_session:
self.start_session(tags, self.config, inherited_session_id)
else:
Expand All @@ -101,22 +108,45 @@ 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):
partner_frameworks = get_partner_frameworks()
for framework in partner_frameworks.keys():
if framework in sys.modules:
self.add_tags([framework])
if 'autogen':
import autogen
autogen.runtime_logging.start(logger_type="agentops")

return partner_frameworks[framework]

return instrument_llm_calls, auto_start_session

def add_tags(self, tags: List[str]):
"""
Append to session tags at runtime.
Args:
tags (List[str]): The list of tags to append.
"""
if self._session.tags is not None:
for tag in tags:
if tag not in self._session.tags:
self._session.tags.append(tag)
else:
self._session.tags = tags

if self._session is not None and self._worker is not None:
self._worker.update_session(self._session)
if self._session:
if self._session.tags is not None:
for tag in tags:
if tag not in self._session.tags:
self._session.tags.append(tag)
else:
self._session.tags = tags

if self._session is not None and self._worker is not None:
self._worker.update_session(self._session)

else:
if self._tags_for_future_session:
for tag in tags:
if tag not in self._session.tags:
self._tags_for_future_session.append(tag)
else:
self._tags_for_future_session = tags

def set_tags(self, tags: List[str]):
"""
Expand Down Expand Up @@ -253,7 +283,7 @@ def start_session(self, tags: Optional[List[str]] = None, config: Optional[Confi
start_session_result = self._worker.start_session(self._session)
if not start_session_result:
self._session = None
return logger.warning("🖇 AgentOps: Cannot start session")
return logger.warning("🖇 AgentOps: Cannot start session - No server response")

logger.info('View info on this session at https://app.agentops.ai/drilldown?session_id=%s',
self._session.session_id)
Expand Down Expand Up @@ -294,9 +324,12 @@ def end_session(self,
self._session = None
self._worker = None

def create_agent(self, agent_id: str, name: str):
def create_agent(self, name: str, agent_id: Optional[str] = None) -> str:
if agent_id is None:
agent_id = str(uuid.uuid4())
if self._worker:
self._worker.create_agent(agent_id, name)
return agent_id

def _handle_unclean_exits(self):
def cleanup(end_state: str = 'Fail', end_state_reason: Optional[str] = None):
Expand Down
11 changes: 11 additions & 0 deletions agentops/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
from importlib.metadata import version


PARTNER_FRAMEWORKS = {
# framework : instrument_llm_calls, auto_start_session
'autogen': (False, True),
'crewai': (False, True)
}


def singleton(class_):
instances = {}

Expand Down Expand Up @@ -138,3 +145,7 @@ def wrapper(self, *args, **kwargs):

return func(self, *args, **kwargs)
return wrapper


def get_partner_frameworks():
return PARTNER_FRAMEWORKS
2 changes: 1 addition & 1 deletion agentops/llm_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,4 @@ def undo_override_openai_v1_completion(self):
def undo_override_openai_v1_async_completion(self):
global original_create_async
from openai.resources.chat import completions
original_create_async = completions.AsyncCompletions.create
completions.AsyncCompletions.create = original_create_async
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dev = [
"requests_mock==1.11.0"
]
langchain = [
"langchain>=0.0.354"
"langchain~=1.19"
]

[project.urls]
Expand Down
50 changes: 50 additions & 0 deletions tests/core_manual_tests/upsert_events.py.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-05-07T01:49:47.567382Z",
"start_time": "2024-05-07T01:49:47.148365Z"
}
},
"outputs": [],
"source": [
"import agentops"
]
},
{
"cell_type": "code",
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
},
"id": "cc69b52023168f58"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit ea6328d

Please sign in to comment.