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

feat: session end analytics: tools, llms, duration #392

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Changes from 3 commits
Commits
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
22 changes: 22 additions & 0 deletions agentops/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from termcolor import colored
from typing import Optional, List, Union
from uuid import UUID, uuid4
from dateutil import parser

from .exceptions import ApiServerException
from .enums import EndState
Expand Down Expand Up @@ -52,6 +53,7 @@ def __init__(
self.jwt = None
self.lock = threading.Lock()
self.queue = []
self.event_counts = { 'llms': 0, 'tools': 0, 'actions': 0, 'errors': 0, 'apis': 0 }

self.stop_flag = threading.Event()
self.thread = threading.Thread(target=self._run)
Expand Down Expand Up @@ -111,6 +113,11 @@ def end_session(
logger.debug(res.body)
token_cost = res.body.get("token_cost", "unknown")

duration = parser.parse(self.end_timestamp) - parser.parse(self.init_timestamp)
logger.info(f"This run's total LLM calls: {self.event_counts['llms']}")
logger.info(f"This run's total Tool calls: {self.event_counts['tools']}")
logger.info(f"This run's total duration: {duration}")

if token_cost == "unknown" or token_cost is None:
logger.info("Could not determine cost of run.")
token_cost_d = Decimal(0)
Expand Down Expand Up @@ -293,6 +300,21 @@ def _flush_queue(self) -> None:
logger.debug(serialized_payload)
logger.debug("</AGENTOPS_DEBUG_OUTPUT>\n")

# Count total events created based on type
events = payload['events']
for event in events:
event_type = event['event_type']
if event_type == 'llms':
self.event_counts['llms'] += 1
elif event_type == 'tools':
self.event_counts['tools'] += 1
elif event_type == 'actions':
self.event_counts['actions'] += 1
elif event_type == 'errors':
self.event_counts['errors'] += 1
elif event_type == 'apis':
self.event_counts['apis'] += 1

def _run(self) -> None:
while not self.stop_flag.is_set():
time.sleep(self.config.max_wait_time / 1000)
Expand Down
Loading