Repository navigation
Expand file tree
/
Copy pathmetrics.py
More file actions
52 lines (43 loc) · 1.63 KB
/
Copy pathmetrics.py
File metadata and controls
52 lines (43 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Trace-derived summaries for completed loop runs."""
from __future__ import annotations
from dataclasses import dataclass
from .models import LoopTrace
@dataclass(frozen=True)
class MetricReport:
"""A non-mutating summary calculated from a loop trace."""
steps: int
final_score: float
success: bool
cost: float
average_step_gain: float
@classmethod
def from_trace(cls, trace: LoopTrace) -> "MetricReport":
"""Compute metrics from recorded events without changing the trace."""
evaluations = [event.payload for event in trace.events if event.phase == "EVALUATE"]
scores = [float(payload["score"]) for payload in evaluations if "score" in payload]
action_costs = [
float(event.payload["cost"])
for event in trace.events
if event.phase == "ACT" and "cost" in event.payload
]
steps = (
trace.final_state.step
if trace.final_state is not None
else max((event.step for event in trace.events), default=0)
)
final_score = scores[-1] if scores else 0.0
success = (
trace.final_state.status == "SUCCEEDED"
if trace.final_state is not None
else bool(evaluations[-1].get("success", False)) if evaluations else False
)
average_step_gain = (
(scores[-1] - scores[0]) / (len(scores) - 1) if len(scores) > 1 else 0.0
)
return cls(
steps=steps,
final_score=final_score,
success=success,
cost=sum(action_costs),
average_step_gain=average_step_gain,
)