|
| 1 | +"""Community evaluator: time_efficiency |
| 2 | +
|
| 3 | +Scores resolution time relative to a budget. Reads latency from |
| 4 | +the trace's performance_metrics. |
| 5 | +
|
| 6 | +Score = clamp(1.0 - actual_seconds / max_duration_s, 0, 1) |
| 7 | +
|
| 8 | +Returns NOT_EVALUATED when no latency data is available. |
| 9 | +
|
| 10 | +Config options: |
| 11 | + max_duration_s (float): Time budget in seconds (default: 120) |
| 12 | + latency_percentile (str): Which percentile to score against: |
| 13 | + "p50" (default), "p95", "p99" |
| 14 | + latency_source (str): Latency category: |
| 15 | + "overall" (default), "llm_calls", "tool_executions" |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from agentevals_evaluator_sdk import EvalInput, EvalResult, EvalStatus, evaluator |
| 21 | + |
| 22 | +_VALID_PERCENTILES = ("p50", "p95", "p99") |
| 23 | +_VALID_SOURCES = ("overall", "llm_calls", "tool_executions") |
| 24 | + |
| 25 | + |
| 26 | +def _extract_duration_s(perf: dict, percentile: str, source: str) -> tuple[float | None, str]: |
| 27 | + """Extract duration in seconds from a performance_metrics dict. |
| 28 | +
|
| 29 | + Returns (duration_seconds, description_of_source). |
| 30 | +
|
| 31 | + Supports: |
| 32 | + nested (agentevals): latency.<source>.<percentile> in milliseconds |
| 33 | + flat: duration_s (seconds) or duration_ms (milliseconds) |
| 34 | + """ |
| 35 | + latency_block = perf.get("latency") |
| 36 | + if isinstance(latency_block, dict): |
| 37 | + source_block = latency_block.get(source) |
| 38 | + if isinstance(source_block, dict): |
| 39 | + ms_value = source_block.get(percentile) |
| 40 | + if ms_value is not None: |
| 41 | + return float(ms_value) / 1000.0, f"latency.{source}.{percentile}" |
| 42 | + |
| 43 | + duration_s = perf.get("duration_s") |
| 44 | + if duration_s is not None: |
| 45 | + return float(duration_s), "duration_s" |
| 46 | + |
| 47 | + duration_ms = perf.get("duration_ms") |
| 48 | + if duration_ms is not None: |
| 49 | + return float(duration_ms) / 1000.0, "duration_ms" |
| 50 | + |
| 51 | + return None, "no latency data found" |
| 52 | + |
| 53 | + |
| 54 | +def _get_perf(input: EvalInput) -> dict | None: |
| 55 | + """Return the first non-None performance_metrics from any invocation.""" |
| 56 | + for inv in input.invocations: |
| 57 | + if isinstance(inv.performance_metrics, dict): |
| 58 | + return inv.performance_metrics |
| 59 | + return None |
| 60 | + |
| 61 | + |
| 62 | +@evaluator |
| 63 | +def time_efficiency(input: EvalInput) -> EvalResult: |
| 64 | + max_duration = input.config.get("max_duration_s", 120.0) |
| 65 | + percentile = input.config.get("latency_percentile", "p50") |
| 66 | + source = input.config.get("latency_source", "overall") |
| 67 | + n = len(input.invocations) |
| 68 | + |
| 69 | + if percentile not in _VALID_PERCENTILES: |
| 70 | + return EvalResult( |
| 71 | + score=0.0, |
| 72 | + status=EvalStatus.NOT_EVALUATED, |
| 73 | + per_invocation_scores=[None] * n, |
| 74 | + details={"reason": f"invalid latency_percentile '{percentile}', must be one of {_VALID_PERCENTILES}"}, |
| 75 | + ) |
| 76 | + if source not in _VALID_SOURCES: |
| 77 | + return EvalResult( |
| 78 | + score=0.0, |
| 79 | + status=EvalStatus.NOT_EVALUATED, |
| 80 | + per_invocation_scores=[None] * n, |
| 81 | + details={"reason": f"invalid latency_source '{source}', must be one of {_VALID_SOURCES}"}, |
| 82 | + ) |
| 83 | + |
| 84 | + perf = _get_perf(input) |
| 85 | + if perf is None: |
| 86 | + return EvalResult( |
| 87 | + score=0.0, |
| 88 | + status=EvalStatus.NOT_EVALUATED, |
| 89 | + per_invocation_scores=[None] * n, |
| 90 | + details={"reason": "no performance_metrics available"}, |
| 91 | + ) |
| 92 | + |
| 93 | + duration_s, source_desc = _extract_duration_s(perf, percentile, source) |
| 94 | + if duration_s is None: |
| 95 | + return EvalResult( |
| 96 | + score=0.0, |
| 97 | + status=EvalStatus.NOT_EVALUATED, |
| 98 | + per_invocation_scores=[None] * n, |
| 99 | + details={"reason": source_desc}, |
| 100 | + ) |
| 101 | + |
| 102 | + score = max(0.0, min(1.0, 1.0 - duration_s / max_duration)) if max_duration > 0 else 1.0 |
| 103 | + |
| 104 | + breakdown = {} |
| 105 | + latency_block = perf.get("latency") |
| 106 | + if isinstance(latency_block, dict): |
| 107 | + for src in _VALID_SOURCES: |
| 108 | + src_block = latency_block.get(src) |
| 109 | + if isinstance(src_block, dict): |
| 110 | + val = src_block.get(percentile) |
| 111 | + if val is not None: |
| 112 | + breakdown[src] = round(float(val) / 1000.0, 3) |
| 113 | + |
| 114 | + details: dict = { |
| 115 | + "duration_s": round(duration_s, 3), |
| 116 | + "max_duration_s": max_duration, |
| 117 | + "utilization": f"{duration_s / max_duration * 100:.1f}%" if max_duration > 0 else "n/a", |
| 118 | + "source": source_desc, |
| 119 | + } |
| 120 | + if breakdown: |
| 121 | + details["latency_breakdown_s"] = breakdown |
| 122 | + |
| 123 | + return EvalResult( |
| 124 | + score=score, |
| 125 | + per_invocation_scores=[None] * n, |
| 126 | + details=details, |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + time_efficiency.run() |
0 commit comments