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

Update langchain handler for v1 #109

Merged
merged 12 commits into from
Mar 21, 2024
2 changes: 1 addition & 1 deletion agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# agentops/__init__.py

from .client import Client
from .event import ActionEvent, LLMEvent, ToolEvent, ErrorEvent
from .event import Event, ActionEvent, LLMEvent, ToolEvent, ErrorEvent
from .logger import AgentOpsLogger
from .enums import Models, LLMMessageFormat
from .decorators import record_function
Expand Down
6 changes: 5 additions & 1 deletion agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def set_tags(self, tags: List[str]):
self._session.tags = tags
self._worker.update_session(self._session)

def record(self, event: Event):
def record(self, event: Event | ErrorEvent):
"""
Record an event with the AgentOps service.

Expand Down Expand Up @@ -281,3 +281,7 @@ def handle_exception(exc_type, exc_value, exc_traceback):
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
sys.excepthook = handle_exception

@property
def current_session_id(self):
return self._session.session_id
6 changes: 0 additions & 6 deletions agentops/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ class EventType(Enum):
ERROR = "errors"


class Result(Enum):
SUCCESS = "Success"
FAIL = "Fail"
INDETERMINATE = "Indeterminate"


class Models(Enum):
GPT_3_5_TURBO = "gpt-3.5-turbo"
GPT_3_5_TURBO_0301 = "gpt-3.5-turbo-0301"
Expand Down
4 changes: 2 additions & 2 deletions agentops/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@dataclass
class Event:
event_type: str # EventType.ENUM.value
params: Optional[str] = None
params: Optional[dict] = None
returns: Optional[str] = None
init_timestamp: Optional[str] = field(default_factory=get_ISO_time)
end_timestamp: str = field(default_factory=get_ISO_time)
Expand Down Expand Up @@ -80,7 +80,7 @@ class ToolEvent(Event):
event_type: str = EventType.TOOL.value
agent_id: Optional[UUID] = None
name: Optional[str] = None
logs: Optional[str] = None
logs: Optional[str | dict] = None


# Does not inherit from Event because error will (optionally) be linked to an ActionEvent, LLMEvent, etc that will have the details
Expand Down
44 changes: 44 additions & 0 deletions agentops/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from pprint import pprint, pformat
from functools import wraps
import time
from datetime import datetime
import json
import inspect
import logging
from uuid import UUID
import os
from importlib.metadata import version


Expand Down Expand Up @@ -86,10 +89,51 @@ def check_call_stack_for_agent_id() -> str | None:
return getattr(var, '_agent_ops_agent_id')
return None


# Function decorator that prints function name and its arguments to the console for debug purposes
# Example output:
# <AGENTOPS_DEBUG_OUTPUT>
# on_llm_start called with arguments:
# run_id: UUID('5fda42fe-809b-4179-bad2-321d1a6090c7')
# parent_run_id: UUID('63f1c4da-3e9f-4033-94d0-b3ebed06668f')
# tags: []
# metadata: {}
# invocation_params: {'_type': 'openai-chat',
# 'model': 'gpt-3.5-turbo',
# 'model_name': 'gpt-3.5-turbo',
# 'n': 1,
# 'stop': ['Observation:'],
# 'stream': False,
# 'temperature': 0.7}
# options: {'stop': ['Observation:']}
# name: None
# batch_size: 1
# </AGENTOPS_DEBUG_OUTPUT>

# regex to filter for just this:
# <AGENTOPS_DEBUG_OUTPUT>([\s\S]*?)<\/AGENTOPS_DEBUG_OUTPUT>\n

def debug_print_function_params(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
if os.getenv('DEBUG_MODE') == 'Y':
print("\n<AGENTOPS_DEBUG_OUTPUT>")
print(f"{func.__name__} called with arguments:")

for key, value in kwargs.items():
print(f"{key}: {pformat(value)}")

print("</AGENTOPS_DEBUG_OUTPUT>\n")

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


def get_agentops_version():
try:
pkg_version = version("agentops")
return pkg_version
except Exception as e:
print(f"Error reading package version: {e}")
return None

Loading