From d2f33f4a0474eaf53dde4b53e72f66ad380c3f68 Mon Sep 17 00:00:00 2001 From: Abram Date: Sun, 8 Dec 2024 11:35:31 +0100 Subject: [PATCH] refactor (backend): add a fail-safe logic to compute duration in the case of a missing attribute --- .../services/llm_apps_service.py | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/agenta-backend/agenta_backend/services/llm_apps_service.py b/agenta-backend/agenta_backend/services/llm_apps_service.py index d8ce93cb5e..93a17d3d54 100644 --- a/agenta-backend/agenta_backend/services/llm_apps_service.py +++ b/agenta-backend/agenta_backend/services/llm_apps_service.py @@ -58,12 +58,24 @@ 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, ["metrics", "acc", "duration", "total"] - ) - / 1000 + duration_ms = get_nested_value( + trace_tree, ["metrics", "acc", "duration", "total"] ) + if duration_ms: + duration_seconds = duration_ms / 1000 + else: + start_time = get_nested_value(trace_tree, ["time", "start"]) + end_time = get_nested_value(trace_tree, ["time", "end"]) + + if start_time and end_time: + duration_seconds = ( + datetime.fromisoformat(end_time) + - datetime.fromisoformat(start_time) + ).total_seconds() + else: + duration_seconds = None + + latency = duration_seconds cost = get_nested_value( trace_tree, ["metrics", "acc", "costs", "total"] )