-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from NastyRunner13/response_latency-Metrics
Add Response Latency Metric
- Loading branch information
Showing
8 changed files
with
222 additions
and
351 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import json | ||
from typing import List, Optional, Dict, Any | ||
from datetime import datetime | ||
|
||
def parse_timestamp(timestamp: str) -> datetime: | ||
return datetime.fromisoformat(timestamp) | ||
|
||
def calculate_latency(start_time: str, end_time: str) -> Optional[float]: | ||
start = parse_timestamp(start_time) | ||
end = parse_timestamp(end_time) | ||
if start and end: | ||
return (end - start).total_seconds() | ||
else: | ||
return None | ||
|
||
def extract_latency_data(trace_json: Dict[str, Any]) -> List[float]: | ||
latencies = [] | ||
for call_type in ["llm_calls", "tool_calls"]: | ||
if call_type in trace_json: | ||
for call in trace_json[call_type]: | ||
if "start_time" in call and "end_time" in call: | ||
latency = calculate_latency(call["start_time"], call["end_time"]) | ||
if latency is not None: | ||
latencies.append(latency) | ||
return latencies | ||
|
||
def execute_response_latency_metric(trace_json: Dict[str, Any], config: Dict[str, Any] = {}) -> Dict[str, Any]: | ||
try: | ||
# Extract latency data | ||
latencies = extract_latency_data(trace_json) | ||
|
||
if not latencies: | ||
return { | ||
"metric_name": "response_latency", | ||
"config": config, | ||
"result": { | ||
"score": None, | ||
"reason": "No response latencies found in the trace.", | ||
}, | ||
} | ||
|
||
# Calculate primary statistics | ||
average_latency = sum(latencies) / len(latencies) | ||
min_latency = min(latencies) | ||
max_latency = max(latencies) | ||
|
||
# Calculate additional statistics | ||
latencies.sort() | ||
median_latency = latencies[len(latencies) // 2] | ||
p90_latency = latencies[int(len(latencies) * 0.9)] | ||
std_dev_latency = (sum((x - average_latency) ** 2 for x in latencies) / len(latencies)) ** 0.5 | ||
|
||
return { | ||
"metric_name": "response_latency", | ||
"config": config, | ||
"result": { | ||
"score": average_latency, | ||
"reason": "Response latency metrics successfully calculated.", | ||
"details": { | ||
"average_latency": average_latency, | ||
"min_latency": min_latency, | ||
"max_latency": max_latency, | ||
"median_latency": median_latency, | ||
"p90_latency": p90_latency, | ||
"std_dev_latency": std_dev_latency, | ||
}, | ||
}, | ||
} | ||
|
||
except Exception as e: | ||
return { | ||
"metric_name": "response_latency", | ||
"config": config, | ||
"result": { | ||
"score": None, | ||
"reason": f"An error occurred during evaluation: {str(e)}", | ||
}, | ||
} |
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
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
Oops, something went wrong.