Repository navigation
Expand file tree
/
Copy pathreport.py
More file actions
310 lines (265 loc) · 9.48 KB
/
Copy pathreport.py
File metadata and controls
310 lines (265 loc) · 9.48 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "click",
# "databricks-sdk",
# "mlflow",
# "pandas",
# "tabulate",
# ]
# ///
import re
from collections import defaultdict
import click
import mlflow
import pandas as pd
from tabulate import tabulate
# Categories and the metrics we expect from each
_OPERATIONAL_METRICS = [
"p50_total_tokens",
"p95_total_tokens",
"sum_total_tokens",
"total_cost_usd",
"avg_cost_per_call_usd",
"latency_avg_s",
"latency_p50_s",
"latency_p95_s",
"suite_wall_clock_s",
"throughput_calls_per_s",
"error_rate",
"error_count",
"total_predictions",
"response_length_avg",
]
CATEGORY_METRICS = {
"enterprise": [
"safety/mean",
"follows_instructions/mean",
"avg_input_tokens",
"avg_output_tokens",
"avg_total_tokens",
*_OPERATIONAL_METRICS,
],
"hallucination": [
"follows_instructions/mean",
"avg_input_tokens",
"avg_output_tokens",
"avg_total_tokens",
*_OPERATIONAL_METRICS,
],
"model": [
"safety/mean",
"correctness/mean",
"follows_instructions/mean",
"bleu/mean",
"rouge/mean",
"cosine_similarity/mean",
"avg_input_tokens",
"avg_output_tokens",
"avg_total_tokens",
*_OPERATIONAL_METRICS,
],
"vision": [
"safety/mean",
"correctness/mean",
"follows_instructions/mean",
"field_accuracy/mean",
"avg_input_tokens",
"avg_output_tokens",
"avg_total_tokens",
*_OPERATIONAL_METRICS,
],
}
KNOWN_CATEGORIES = set(CATEGORY_METRICS.keys())
_INTEGER_METRICS = {"error_count", "total_predictions"}
_LARGE_NUMBER_METRICS = {"p50_total_tokens", "p95_total_tokens", "sum_total_tokens"}
def _format_metric(label: str, val: float) -> str:
name = label.rsplit("/", 1)[-1]
if name in _INTEGER_METRICS:
return f"{int(val)}"
if name.endswith("_usd"):
return f"${val:.6f}"
if name.endswith("_s"):
return f"{val:.3f}"
if name in _LARGE_NUMBER_METRICS:
return f"{val:,.0f}"
return f"{val:.4f}"
def parse_experiment_id(value: str) -> str:
"""Extract experiment ID from a raw ID string or a Databricks URL."""
match = re.search(r"/experiments/(\d+)", value)
if match:
return match.group(1)
if value.isdigit():
return value
raise click.BadParameter(
f"Cannot parse experiment ID from: {value}. "
"Provide a numeric ID or a Databricks experiment URL."
)
def extract_category(row) -> str | None:
"""Extract category from a child run — prefer 'suite' tag, fall back to run name."""
suite_tag = row.get("tags.suite")
if suite_tag and suite_tag in KNOWN_CATEGORIES:
return suite_tag
run_name = row.get("tags.mlflow.runName", "")
for category in KNOWN_CATEGORIES:
if f"-{category}-" in run_name:
return category
return None
@click.command()
@click.option(
"--experiment",
required=True,
help="Experiment ID (numeric) or full Databricks experiment URL.",
)
@click.option(
"--run-id",
default=None,
help="Optional parent run ID to report on a single run instead of all runs.",
)
@click.option(
"--output",
default=None,
type=click.Path(),
help="Optional path to write CSV output (e.g. report.csv).",
)
def report(experiment: str, run_id: str | None, output: str | None):
"""Generate a tabular comparison of evaluation metrics across models."""
experiment_id = parse_experiment_id(experiment)
if run_id:
click.echo(f"Querying experiment {experiment_id}, parent run {run_id} ...")
else:
click.echo(f"Querying experiment {experiment_id} ...")
runs_df = mlflow.search_runs(
experiment_ids=[experiment_id],
output_format="pandas",
)
if runs_df.empty:
click.echo("No runs found for this experiment.")
raise SystemExit(1)
# Identify parent vs child runs via the mlflow.parentRunId tag
parent_tag = "tags.mlflow.parentRunId"
if parent_tag not in runs_df.columns:
click.echo("No child runs found — nothing to report.")
raise SystemExit(1)
parent_runs = runs_df[runs_df[parent_tag].isna()]
child_runs = runs_df[runs_df[parent_tag].notna()]
# Filter to a single parent run if --run-id is provided
if run_id:
parent_runs = parent_runs[parent_runs["run_id"] == run_id]
if parent_runs.empty:
click.echo(f"Parent run {run_id} not found in experiment.")
raise SystemExit(1)
child_runs = child_runs[child_runs[parent_tag] == run_id]
if child_runs.empty:
click.echo(f"No child runs found for parent run {run_id}.")
raise SystemExit(1)
if parent_runs.empty:
click.echo("No parent runs found.")
raise SystemExit(1)
# When reporting all runs, keep only the latest fully completed parent run
# per model. A run is "complete" if it has the playbook.status=complete tag.
# Falls back to checking for child runs with metric data for older runs
# that predate the completion tag.
status_tag = "tags.playbook.status"
if not run_id:
parent_runs = parent_runs.sort_values("start_time", ascending=False)
seen_models: dict[str, str] = {} # model_name -> run_id (latest complete)
for _, row in parent_runs.iterrows():
model_name = row.get("params.model", None)
if not model_name:
name = row.get("tags.mlflow.runName", "")
parts = name.split("-")
model_name = "-".join(parts[1:-1]) if len(parts) >= 3 else name
if model_name in seen_models:
continue
# Check completion tag first
is_complete = (
status_tag in row.index
and pd.notna(row.get(status_tag))
and row[status_tag] == "complete"
)
# Fallback: check if this run has children with metrics
if not is_complete:
run_children = child_runs[child_runs[parent_tag] == row["run_id"]]
metric_cols = [c for c in run_children.columns if c.startswith("metrics.")]
if metric_cols:
is_complete = run_children[metric_cols].notna().any(axis=1).any()
if is_complete:
seen_models[model_name] = row["run_id"]
if not seen_models:
click.echo("No completed runs found.")
raise SystemExit(1)
latest_ids = set(seen_models.values())
parent_runs = parent_runs[parent_runs["run_id"].isin(latest_ids)]
child_runs = child_runs[child_runs[parent_tag].isin(latest_ids)]
click.echo(f"Using latest completed run for each model: {', '.join(seen_models.keys())}")
# Build a lookup: parent_run_id -> (model_name, judge_name)
parent_model: dict[str, str] = {}
judges: set[str] = set()
for _, row in parent_runs.iterrows():
rid = row["run_id"]
model_name = row.get("params.model", None)
if model_name:
parent_model[rid] = model_name
else:
name = row.get("tags.mlflow.runName", "")
parts = name.split("-")
if len(parts) >= 3:
parent_model[rid] = "-".join(parts[1:-1])
else:
parent_model[rid] = name
judge_name = row.get("params.judge")
if judge_name and pd.notna(judge_name):
judges.add(judge_name)
# Collect metrics: { "category/metric": { model: value } }
table_data: dict[str, dict[str, float | None]] = defaultdict(dict)
all_models: set[str] = set()
for _, row in child_runs.iterrows():
parent_id = row[parent_tag]
model_name = parent_model.get(parent_id, "unknown")
all_models.add(model_name)
category = extract_category(row)
if category is None:
continue
expected = CATEGORY_METRICS.get(category, [])
for metric_key in expected:
col = f"metrics.{metric_key}"
if col in row.index and pd.notna(row[col]):
label = f"{category}/{metric_key}"
table_data[label][model_name] = row[col]
if not table_data:
click.echo("No metrics found in child runs.")
raise SystemExit(1)
# Sort models alphabetically and metrics by category order
sorted_models = sorted(all_models)
# Build ordered metric list following CATEGORY_METRICS declaration order
ordered_metrics = []
for category, metrics in CATEGORY_METRICS.items():
for m in metrics:
label = f"{category}/{m}"
if label in table_data:
ordered_metrics.append(label)
# Show judge model(s) used
if judges:
click.echo(f"Judge: {', '.join(sorted(judges))}")
# Build table rows
rows = []
for metric in ordered_metrics:
row = [metric]
for model in sorted_models:
val = table_data[metric].get(model)
if val is None:
row.append("-")
else:
row.append(_format_metric(metric, val))
rows.append(row)
headers = ["Metric"] + sorted_models
click.echo()
click.echo(tabulate(rows, headers=headers, tablefmt="grid"))
click.echo()
if output:
df_out = pd.DataFrame(rows, columns=headers)
df_out.to_csv(output, index=False)
click.echo(f"Report written to {output}")
if __name__ == "__main__":
report()