-
Notifications
You must be signed in to change notification settings - Fork 186
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
Refactor Session class and add independent get_analytics method #437
Open
dragutin-oreski
wants to merge
5
commits into
AgentOps-AI:main
Choose a base branch
from
dragutin-oreski:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Refactored token cost and duration handling into reusable methods. - Added get_analytics function to allow independent session statistics retrieval. - Improved code readability and modularity.
…patch Refactor Session class and add get_analytics function
Another option is to have a function like this: import json
from decimal import ROUND_HALF_UP, Decimal
from agentops import logger
from agentops.exceptions import ApiServerException
from agentops.helpers import get_ISO_time, filter_unjsonable
from agentops.http_client import HttpClient
from typing import Union
from datetime import datetime
def get_analytics(session) -> dict[str, Union[Decimal, str]]:
if not session.end_timestamp:
session.end_timestamp = get_ISO_time()
formatted_duration = _format_duration(
session.init_timestamp, session.end_timestamp
)
token_cost = _get_token_cost(session)
if token_cost == "unknown" or token_cost is None:
token_cost_d = Decimal(0)
else:
token_cost_d = Decimal(token_cost)
formatted_cost = _format_token_cost(token_cost_d)
return {
"llm_calls": session.event_counts["llms"],
"tool_calls": session.event_counts["tools"],
"actions": session.event_counts["actions"],
"errors": session.event_counts["errors"],
"duration": formatted_duration,
"cost": formatted_cost,
}
def _format_duration(start_time, end_time):
start = datetime.fromisoformat(start_time.replace("Z", "+00:00"))
end = datetime.fromisoformat(end_time.replace("Z", "+00:00"))
duration = end - start
hours, remainder = divmod(duration.total_seconds(), 3600)
minutes, seconds = divmod(remainder, 60)
parts = []
if hours > 0:
parts.append(f"{int(hours)}h")
if minutes > 0:
parts.append(f"{int(minutes)}m")
parts.append(f"{seconds:.1f}s")
return " ".join(parts)
def _get_token_cost(session):
with session.lock:
payload = {"session": session.__dict__}
try:
res = HttpClient.post(
f"{session.config.endpoint}/v2/update_session",
json.dumps(filter_unjsonable(payload)).encode("utf-8"),
jwt=session.jwt,
)
except ApiServerException as e:
return logger.error(f"Could not end session - {e}")
logger.debug(res.body)
return res.body.get("token_cost", "unknown")
def _format_token_cost(token_cost_d):
return (
"{:.2f}".format(token_cost_d)
if token_cost_d == 0
else "{:.6f}".format(
token_cost_d.quantize(Decimal("0.000001"), rounding=ROUND_HALF_UP)
)
)
|
Awesome-- I like this idea a lot. Will test out your PR today |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📥 Pull Request
Background
I need to access analytics directly, not only through the logs.
I created a workaround for myself to get the analytics stats, but I figured that others could benefit from it as well.
So, I added a function that can return the session analytics back to the user.
📘 Description
Refactored the Session class to improve code modularity and maintainability. Specifically:
• Introduced a get_analytics method to allow independent retrieval of session analytics (e.g., token cost, LLM calls, tool usage, etc.).
• Moved token cost and duration formatting into separate methods for better code reuse.
• Ensured that the analytics function can be used independently of the session lifecycle.
🧪 Testing
• Tested the get_analytics method by running sessions and validating the returned statistics, including correct duration formatting and token cost calculation.
• Verified that the session behaves as expected when ending, ensuring no regression in the current end_session logic.
• Ensured thread management and queue flushing still work as intended in the refactored code.