-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheval.ts
More file actions
205 lines (181 loc) · 6.54 KB
/
eval.ts
File metadata and controls
205 lines (181 loc) · 6.54 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
import chalk from "chalk";
import { terminalLink } from "termi-link";
import boxen from "boxen";
import Table from "cli-table3";
import pluralize from "pluralize";
import { ExperimentSummary, ScoreSummary, MetricSummary } from "../../logger";
import type { ReporterDef } from "../../reporters/types";
import { EvaluatorDef, EvalResultWithSummary } from "../../framework";
import { isEmpty } from "../../util";
function formatExperimentSummaryFancy(summary: ExperimentSummary) {
let comparisonLine = "";
if (summary.comparisonExperimentName) {
comparisonLine = `${summary.comparisonExperimentName} ${chalk.gray("(baseline)")} ← ${summary.experimentName} ${chalk.gray("(comparison)")}\n\n`;
}
const tableParts: string[] = [];
const hasScores = Object.keys(summary.scores).length > 0;
const hasMetrics = Object.keys(summary.metrics ?? {}).length > 0;
const hasComparison = !!summary.comparisonExperimentName;
if (hasScores || hasMetrics) {
const headers = [chalk.gray("Name"), chalk.gray("Value")];
if (hasComparison) {
headers.push(
chalk.gray("Change"),
chalk.gray("Improvements"),
chalk.gray("Regressions"),
);
}
const combinedTable = new Table({
head: hasComparison ? headers : [],
style: { head: [], "padding-left": 0, "padding-right": 0, border: [] },
chars: {
top: "",
"top-mid": "",
"top-left": "",
"top-right": "",
bottom: "",
"bottom-mid": "",
"bottom-left": "",
"bottom-right": "",
left: "",
"left-mid": "",
mid: "",
"mid-mid": "",
right: "",
"right-mid": "",
middle: " ",
},
colWidths: hasComparison ? [18, 10, 10, 13, 12] : [20, 15],
colAligns: hasComparison
? ["left", "right", "right", "right", "right"]
: ["left", "right"],
wordWrap: false,
});
const scoreValues: ScoreSummary[] = Object.values(summary.scores);
for (const score of scoreValues) {
const scorePercent = (score.score * 100).toFixed(2);
const scoreValue = chalk.white(`${scorePercent}%`);
let diffString = "";
if (!isEmpty(score.diff)) {
const diffPercent = (score.diff! * 100).toFixed(2);
const diffSign = score.diff! > 0 ? "+" : "";
const diffColor = score.diff! > 0 ? chalk.green : chalk.red;
diffString = diffColor(`${diffSign}${diffPercent}%`);
} else {
diffString = chalk.gray("-");
}
const improvements =
score.improvements > 0
? chalk.dim.green(score.improvements)
: chalk.gray("-");
const regressions =
score.regressions > 0
? chalk.dim.red(score.regressions)
: chalk.gray("-");
const row = [`${chalk.blue("◯")} ${score.name}`, scoreValue];
if (hasComparison) {
row.push(diffString, improvements, regressions);
}
combinedTable.push(row);
}
const metricValues: MetricSummary[] = Object.values(summary.metrics ?? {});
for (const metric of metricValues) {
const fractionDigits = Number.isInteger(metric.metric) ? 0 : 2;
const formattedValue = metric.metric.toFixed(fractionDigits);
const metricValue = chalk.white(
metric.unit === "$"
? `${metric.unit}${formattedValue}`
: `${formattedValue}${metric.unit}`,
);
let diffString = "";
if (!isEmpty(metric.diff)) {
const diffPercent = (metric.diff! * 100).toFixed(2);
const diffSign = metric.diff! > 0 ? "+" : "";
const diffColor = metric.diff! > 0 ? chalk.green : chalk.red;
diffString = diffColor(`${diffSign}${diffPercent}%`);
} else {
diffString = chalk.gray("-");
}
const improvements =
metric.improvements > 0
? chalk.dim.green(metric.improvements)
: chalk.gray("-");
const regressions =
metric.regressions > 0
? chalk.dim.red(metric.regressions)
: chalk.gray("-");
const row = [`${chalk.magenta("◯")} ${metric.name}`, metricValue];
if (hasComparison) {
row.push(diffString, improvements, regressions);
}
combinedTable.push(row);
}
tableParts.push(combinedTable.toString());
}
const content = [comparisonLine, ...tableParts].filter(Boolean).join("\n");
const footer = summary.experimentUrl
? terminalLink(
`View results for ${summary.experimentName}`,
summary.experimentUrl,
{ fallback: () => `See results at ${summary.experimentUrl}` },
)
: "";
const boxContent = [content, footer].filter(Boolean).join("\n\n");
try {
return (
"\n" +
boxen(boxContent, {
title: chalk.gray("Experiment summary"),
titleAlignment: "left",
padding: 0.5,
borderColor: "gray",
borderStyle: "round",
})
);
} catch {
return "\n" + chalk.gray("Experiment summary") + "\n" + boxContent + "\n";
}
}
export const warning = chalk.yellow;
export const fancyReporter: ReporterDef<boolean> = {
name: "Braintrust fancy reporter",
async reportEval(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluator: EvaluatorDef<any, any, any, any>,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result: EvalResultWithSummary<any, any, any, any>,
{ verbose, jsonl }: { verbose: boolean; jsonl?: boolean },
) {
const { results, summary } = result;
const failingResults = results.filter(
(r: { error: unknown }) => r.error !== undefined,
);
if (failingResults.length > 0) {
// eslint-disable-next-line no-restricted-properties -- preserving intentional console usage.
console.error(
warning(
`Evaluator ${evaluator.evalName} failed with ${pluralize("error", failingResults.length, true)}. This evaluation ("${evaluator.evalName}") will not be fully logged.`,
),
);
if (jsonl) {
for (const result of failingResults) {
process.stdout.write(JSON.stringify(result));
process.stdout.write("\n");
}
} else if (verbose) {
for (const result of failingResults) {
// eslint-disable-next-line no-restricted-properties -- preserving intentional console usage.
console.error(result);
}
}
}
process.stdout.write(
jsonl ? JSON.stringify(summary) : formatExperimentSummaryFancy(summary),
);
process.stdout.write("\n");
return failingResults.length === 0;
},
async reportRun(evalReports: boolean[]) {
return evalReports.every((r) => r);
},
};