Skip to content

Commit

Permalink
refactor (backend): remove deprecated 'span' attribute in 'time' meta…
Browse files Browse the repository at this point in the history
… and compute latency gotten from invocating app in evaluation
  • Loading branch information
aybruhm committed Nov 26, 2024
1 parent cf3fd61 commit 2a7c50a
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions agenta-backend/agenta_backend/services/llm_apps_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import traceback
import aiohttp
from datetime import datetime
from typing import Any, Dict, List, Optional


Expand Down Expand Up @@ -36,6 +37,26 @@ def get_nested_value(d: dict, keys: list, default=None):
return default


def compute_latency(start_time_str: str, end_time_str: str) -> float:
try:
# Define the format to parse the time strings
time_format = "%Y-%m-%dT%H:%M:%S.%f"

# Convert the strings to datetime objects
start_datetime = datetime.strptime(start_time_str, time_format)
end_datetime = datetime.strptime(end_time_str, time_format)

# Calculate the difference in seconds
time_difference = (end_datetime - start_datetime).total_seconds()
return time_difference
except Exception as e:
print(f"Error computing latency from invoking an app. Defaulting to None.")
print(" ------------------- Exception ------------------------")
print(str(e))
print(" ------------------- End Exception ------------------------")
return None


def extract_result_from_response(response: dict):
# Initialize default values
value = None
Expand All @@ -57,11 +78,17 @@ def extract_result_from_response(response: dict):
if "tree" in response:
trace_tree = response.get("tree", {}).get("nodes", [])[0]

latency = (
get_nested_value(trace_tree, ["time", "span"]) / 1_000_000
start_time_str = (
get_nested_value(trace_tree, ["time", "start"])
if trace_tree
else None
)
end_time_str = (
get_nested_value(trace_tree, ["time", "end"])
if trace_tree
else None
)
latency = compute_latency(start_time_str, end_time_str)
cost = get_nested_value(
trace_tree, ["metrics", "acc", "costs", "total"]
)
Expand Down

0 comments on commit 2a7c50a

Please sign in to comment.