Skip to content

Commit

Permalink
oops
Browse files Browse the repository at this point in the history
  • Loading branch information
the-praxs committed Nov 23, 2024
1 parent 10aa8db commit f549fef
Showing 1 changed file with 0 additions and 75 deletions.
75 changes: 0 additions & 75 deletions agentops/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,80 +642,5 @@ def get_analytics(self) -> Optional[Dict[str, Any]]:
"Cost": formatted_cost,
}

@staticmethod
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_response(self) -> Optional[Response]:
with self._lock:
payload = {"session": self.__dict__}
try:
response = HttpClient.post(
f"{self.config.endpoint}/v2/update_session",
json.dumps(filter_unjsonable(payload)).encode("utf-8"),
jwt=self.jwt,
)
except ApiServerException as e:
logger.error(f"Could not fetch response from server - {e}")
return None

logger.debug(response.body)
return response

def _get_token_cost(self, response: Response) -> Decimal:
token_cost = response.body.get("token_cost", "unknown")
if token_cost == "unknown" or token_cost is None:
return Decimal(0)
return Decimal(token_cost)

@staticmethod
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))
)

def get_analytics(self) -> Optional[dict[str, Union[Decimal, str]]]:
if not self.end_timestamp:
self.end_timestamp = get_ISO_time()

formatted_duration = self._format_duration(self.init_timestamp, self.end_timestamp)

response = self._get_response()
if response is None:
return None

self.token_cost = self._get_token_cost(response)
formatted_cost = self._format_token_cost(self.token_cost)

self.session_url = response.body.get(
"session_url",
f"https://app.agentops.ai/drilldown?session_id={self.session_id}",
)

return {
"LLM calls": self.event_counts["llms"],
"Tool calls": self.event_counts["tools"],
"Actions": self.event_counts["actions"],
"Errors": self.event_counts["errors"],
"Duration": formatted_duration,
"Cost": formatted_cost,
}


active_sessions: List[Session] = []

0 comments on commit f549fef

Please sign in to comment.