Skip to content

Commit

Permalink
WIP. Refactoring Event to an ABC
Browse files Browse the repository at this point in the history
  • Loading branch information
HowieG committed Feb 29, 2024
1 parent 753c9ea commit 2c85d19
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 13 deletions.
16 changes: 14 additions & 2 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# agentops/__init__.py

from .client import Client
from .event import Event
from .event import Event # TODO: Don't expose? Since it's abstract?
from .logger import AgentOpsLogger
from .enums import Models
from .enums import Models, EventType # TODO: Is EventType needed?

# Import event classes
from .events.action import ActionEvent
from .events.error import ErrorEvent
from .events.llm import LLMEvent
from .events.tool import ToolEvent

# TODO: Expose them at the package level?
# __all__ = ['Client', 'Event', 'AgentOpsLogger', 'Models',
# 'ActionEvent', 'ErrorEvent', 'LLMEvent', 'ToolEvent']
1 change: 1 addition & 0 deletions agentops/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class EventType(Enum):
LLM = "llm"
ACTION = "action"
API = "api"
TOOL = "tool"
ERROR = "error"


Expand Down
4 changes: 2 additions & 2 deletions agentops/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
from abc import ABC, abstractmethod
from .helpers import get_ISO_time
from typing import Optional, List
from enum import EventType
from .enums import EventType


class Event(ABC):
"""
Represents a discrete event to be recorded.
"""

def __init__(self, event_type: EventType = EventType.action,
def __init__(self, event_type: EventType = EventType.ACTION,
tags: Optional[List[str]] = None,
init_timestamp: Optional[str] = None,
):
Expand Down
6 changes: 4 additions & 2 deletions agentops/events/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from typing import Optional
from uuid import UUID

# TODO: Remove agent_id because it's set by server?


class ActionEvent(Event):
def __init__(self, agent_id: UUID, action_type: Optional[str] = None, detail: Optional[str] = None, logs: Optional[str] = None, screenshot: Optional[str] = None, **kwargs):
super().__init__(event_type=EventType.action, **kwargs)
def __init__(self, agent_id: Optional[UUID] = None, action_type: Optional[str] = None, detail: Optional[str] = None, logs: Optional[str] = None, screenshot: Optional[str] = None, **kwargs):
super().__init__(event_type=EventType.ACTION, **kwargs)
self.agent_id = agent_id
self.action_type = action_type
self.detail = detail
Expand Down
2 changes: 1 addition & 1 deletion agentops/events/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class ErrorEvent(Event):
def __init__(self, error_type: Optional[str] = None, code: Optional[str] = None, details: Optional[str] = None, logs: Optional[str] = None, **kwargs):
super().__init__(event_type=EventType.error, **kwargs)
super().__init__(event_type=EventType.ERROR, **kwargs)
self.error_type = error_type # TODO: type is a reserved word
self.code = code,
self.details = details,
Expand Down
7 changes: 5 additions & 2 deletions agentops/events/llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from ..event import Event, EventType, Models
from ..event import Event
from typing import Optional, Dict, Any
from ..enums import EventType, Models


class LLMEvent(Event):
Expand All @@ -9,7 +10,9 @@ def __init__(self,
prompt_tokens: Optional[int] = None,
completion_tokens: Optional[int] = None,
**kwargs):
super().__init__(event_type=EventType.llm, model=model, prompt=prompt, **kwargs)
super().__init__(event_type=EventType.LLM, **kwargs)
self.model = model
self.prompt = prompt
self.prompt_tokens = prompt_tokens
self.completion_tokens = completion_tokens

Expand Down
4 changes: 2 additions & 2 deletions agentops/events/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class ToolEvent(Event):
def __init__(self, agent_id: UUID, name: Optional[str] = None, inputs: Optional[str] = None, outputs: Optional[str] = None, logs: Optional[str] = None, **kwargs):
super().__init__(event_type=EventType.tool, **kwargs)
def __init__(self, agent_id: Optional[UUID] = None, name: Optional[str] = None, inputs: Optional[str] = None, outputs: Optional[str] = None, logs: Optional[str] = None, **kwargs):
super().__init__(event_type=EventType.TOOL, **kwargs)
self.agent_id = agent_id
self.name = name
self.inputs = inputs
Expand Down
9 changes: 7 additions & 2 deletions agentops/logger.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# TODO: Replace
# Remove from Event and put in Sessions
# Maybe send to custom /sessions endpoint


import logging
import re
from .client import Client
Expand All @@ -8,8 +13,8 @@ class AgentOpsLogger():
"""
A utility class for creating loggers and handlers configured to work with the AgentOps service.
This class provides two static methods for creating a logger or a handler that sends log
records to the AgentOps service. The logger and handler are configured with a specific
This class provides two static methods for creating a logger or a handler that sends log
records to the AgentOps service. The logger and handler are configured with a specific
AgentOps client and name.
Example Usage:
Expand Down
109 changes: 109 additions & 0 deletions test_event_ABC.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processing /Users/howardgil/Desktop/agentops/AgentOps-AI/agentops\n",
" Installing build dependencies ... \u001b[?25ldone\n",
"\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n",
"\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25hRequirement already satisfied: requests==2.31.0 in ./env/lib/python3.12/site-packages (from agentops==0.0.20) (2.31.0)\n",
"Requirement already satisfied: pydantic>=1.9.0 in ./env/lib/python3.12/site-packages (from agentops==0.0.20) (2.6.3)\n",
"Requirement already satisfied: packaging<24.0,>=23.1 in ./env/lib/python3.12/site-packages (from agentops==0.0.20) (23.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in ./env/lib/python3.12/site-packages (from requests==2.31.0->agentops==0.0.20) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in ./env/lib/python3.12/site-packages (from requests==2.31.0->agentops==0.0.20) (3.6)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in ./env/lib/python3.12/site-packages (from requests==2.31.0->agentops==0.0.20) (2.2.1)\n",
"Requirement already satisfied: certifi>=2017.4.17 in ./env/lib/python3.12/site-packages (from requests==2.31.0->agentops==0.0.20) (2024.2.2)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in ./env/lib/python3.12/site-packages (from pydantic>=1.9.0->agentops==0.0.20) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in ./env/lib/python3.12/site-packages (from pydantic>=1.9.0->agentops==0.0.20) (2.16.3)\n",
"Requirement already satisfied: typing-extensions>=4.6.1 in ./env/lib/python3.12/site-packages (from pydantic>=1.9.0->agentops==0.0.20) (4.10.0)\n",
"Building wheels for collected packages: agentops\n",
" Building wheel for agentops (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for agentops: filename=agentops-0.0.20-py3-none-any.whl size=20466 sha256=2ec5b069d7411bd7fbabe5ce448264cb4cb603e0a76a9f6edc2ead13392ff0bd\n",
" Stored in directory: /private/var/folders/jb/71y2z4v178jdr5xcgbxfm0gc0000gp/T/pip-ephem-wheel-cache-r78w781y/wheels/43/52/53/9286c77226c557a85392056462580d0d7db8d733d3a9c9be69\n",
"Successfully built agentops\n",
"Installing collected packages: agentops\n",
" Attempting uninstall: agentops\n",
" Found existing installation: agentops 0.0.20\n",
" Uninstalling agentops-0.0.20:\n",
" Successfully uninstalled agentops-0.0.20\n",
"Successfully installed agentops-0.0.20\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install .\n",
"import agentops\n",
"agentopsClient = agentops.Client(api_key=\"e2d78f13-1585-4d45-b482-f67a42ae6099\", tags=[\"event-ABC-test\"])"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"action = agentops.ActionEvent(action_type=\"action-test\",detail=\"This is a test event\", logs=\"blah\")\n",
"event = agentopsClient.record(action)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"llmEvent = agentops.LLMEvent(model=\"gpt-4\",prompt=\"ligma\",prompt_tokens=\"69\",completion_tokens=\"420\")\n",
"event = agentopsClient.record(llmEvent)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"toolEvent = agentops.ToolEvent(name=\"tool-test\",inputs=\"test\",outputs=\"test\",logs=\"test\")\n",
"event = agentopsClient.record(toolEvent)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"errorEvent = agentops.ErrorEvent(error_type=\"f\", code=\"404\", details=\"ligma\", logs=\"69\")\n",
"event = agentopsClient.record(errorEvent)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 2c85d19

Please sign in to comment.