-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy patheval.ts
More file actions
357 lines (333 loc) · 11.6 KB
/
Copy patheval.ts
File metadata and controls
357 lines (333 loc) · 11.6 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import fs from "node:fs";
import { Command, Option } from "commander";
interface EvalRunSummary {
results: unknown[];
mlflow?: {
runId: string;
report: {
written: number;
skipped: number;
failures: Array<{ traceId: string; status?: number; error?: string }>;
};
finish: { finished: boolean; metricsError?: string; finishError?: string };
};
}
type EvalProgress =
| { type: "discovered"; total: number }
| { type: "run-created"; runId: string }
| { type: "start"; id: string; index: number; total: number }
| { type: "result"; result: unknown; index: number; total: number };
/** Subset of `@databricks/appkit/beta`'s eval runner used by this command. */
interface EvalRunner {
runEvalsInDir(opts: {
rootDir?: string;
baseUrl: string;
filter?: string;
tags?: string[];
strict?: boolean;
headers?: Record<string, string>;
mlflow?: { host: string; token: string; experimentId: string };
judge?: { host: string; token: string; model: string };
workspaceClient?: unknown;
warehouseId?: string;
maxConcurrency?: number;
timeoutMs?: number;
retries?: number;
onEvent?: (event: EvalProgress) => void;
}): Promise<EvalRunSummary>;
resolveDatabricksAuth(opts: {
profile?: string;
host?: string;
token?: string;
}): Promise<{ host: string; token: string } | undefined>;
resolveWorkspaceClient(opts: {
profile?: string;
host?: string;
token?: string;
}): unknown;
evalGlyph(result: unknown): string;
formatEvalDetail(result: unknown): string[];
formatSummaryLine(results: unknown[]): string;
formatResultsJson(results: unknown[]): string;
formatResultsJUnit(results: unknown[]): string;
summarize(results: unknown[]): { allPassed: boolean; passRate: number };
}
/**
* Loaded at runtime from the consuming project so this command (which ships in
* `@databricks/shared`) doesn't take a build-time dependency on appkit. The
* specifier is a variable so the type checker treats it as `any`.
*/
async function loadRunner(): Promise<EvalRunner> {
const spec = "@databricks/appkit/beta";
try {
return (await import(spec)) as unknown as EvalRunner;
} catch (err) {
throw new Error(
"Could not load @databricks/appkit. Run `appkit agent eval` from a " +
"project with @databricks/appkit installed. " +
`Cause: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
function parseHeaders(values: string[]): Record<string, string> {
const headers: Record<string, string> = {};
for (const v of values) {
const i = v.indexOf(":");
if (i === -1) continue;
headers[v.slice(0, i).trim()] = v.slice(i + 1).trim();
}
return headers;
}
/** Parse a positive-integer CLI option; junk, zero, or negative → undefined. */
function positiveInt(raw: string | undefined): number | undefined {
const n = raw ? Number.parseInt(raw, 10) : Number.NaN;
return n > 0 ? n : undefined;
}
interface EvalOptions {
url: string;
strict?: boolean;
root?: string;
header?: string[];
tag?: string[];
profile?: string;
databricksHost?: string;
databricksToken?: string;
experiment?: string;
judgeModel?: string;
warehouse?: string;
concurrency?: string;
timeout?: string;
retries?: string;
minPassRate?: string;
reporter?: "text" | "json" | "junit";
output?: string;
}
async function runAgentEval(
filter: string | undefined,
opts: EvalOptions,
): Promise<void> {
const runner = await loadRunner();
// Databricks credentials shared by auth resolution and the workspace client:
// an explicit flag/DATABRICKS_* env wins, else the SDK resolves from the CLI
// profile.
const credentials = {
profile: opts.profile ?? process.env.DATABRICKS_CONFIG_PROFILE,
host: opts.databricksHost ?? process.env.DATABRICKS_HOST,
token: opts.databricksToken ?? process.env.DATABRICKS_TOKEN,
};
// Resolve Databricks host + bearer the AppKit-native way: an explicit
// host/token wins; otherwise the SDK mints an OAuth token from the CLI
// profile — so no hand-set PAT is required.
const auth = await runner.resolveDatabricksAuth(credentials);
const host = auth?.host;
const token = auth?.token;
// Create a native MLflow "Evaluation run" when creds + an experiment are
// available (traces live in the app; the run + scores are driven from here).
const experimentId = opts.experiment ?? process.env.MLFLOW_EXPERIMENT_ID;
const mlflow =
host && token && experimentId ? { host, token, experimentId } : undefined;
// LLM-as-judge: reuse the Databricks creds + a judge serving endpoint.
const judgeModel = opts.judgeModel ?? process.env.APPKIT_JUDGE_MODEL;
const judge =
judgeModel && host && token
? { host, token, model: judgeModel }
: undefined;
// Managed-dataset reads: a workspace client (same profile/host/token) + a SQL
// warehouse. Only needed by evals that declare `dataset`.
const warehouseId = opts.warehouse ?? process.env.DATABRICKS_WAREHOUSE_ID;
const workspaceClient = runner.resolveWorkspaceClient(credentials);
// Drive up to N evals/rows concurrently (default serial). Ignore junk input.
const maxConcurrency = positiveInt(opts.concurrency);
// Runner-level default per-eval timeout (ms). A per-eval `timeoutMs` wins.
const timeoutMs = positiveInt(opts.timeout);
// Extra attempts for evals that fail on an infra error (turn/timeout). Junk
// or negative input falls back to no retries.
const retries = positiveInt(opts.retries);
// In a machine reporter (json/junit), stdout is reserved for the report (it
// may be piped), so human-facing lines go to stderr and the per-eval live
// streaming is suppressed. Text mode keeps its current stdout behavior.
const reporter = opts.reporter ?? "text";
const machine = reporter !== "text";
const info = (msg: string): void => {
if (machine) console.error(msg);
else console.log(msg);
};
// Stream progress as evals run, instead of going silent until the end.
const onEvent = (event: EvalProgress): void => {
switch (event.type) {
case "discovered":
info(
`Running ${event.total} eval${event.total === 1 ? "" : "s"} against ${opts.url}\n`,
);
break;
case "run-created":
info(`MLflow evaluation run: ${event.runId}\n`);
break;
case "start":
if (machine) break;
process.stdout.write(
`▸ [${event.index + 1}/${event.total}] ${event.id} … `,
);
break;
case "result": {
if (machine) break;
process.stdout.write(`${runner.evalGlyph(event.result)}\n`);
for (const line of runner.formatEvalDetail(event.result)) {
console.log(line);
}
break;
}
}
};
const summary = await runner.runEvalsInDir({
rootDir: opts.root,
baseUrl: opts.url,
filter,
tags: opts.tag,
strict: opts.strict,
headers: opts.header ? parseHeaders(opts.header) : undefined,
mlflow,
judge,
workspaceClient,
warehouseId,
maxConcurrency,
timeoutMs,
retries,
onEvent,
});
// The final human summary always shows (stderr for machine reporters so it
// never pollutes the report on stdout/file).
info(`\n${runner.formatSummaryLine(summary.results)}`);
if (summary.mlflow) {
const { report, finish } = summary.mlflow;
info(
`MLflow: ${report.written} assessment(s) written` +
(report.skipped ? `, ${report.skipped} skipped` : "") +
(report.failures.length ? `, ${report.failures.length} failed` : ""),
);
for (const f of report.failures) {
console.error(
` ✗ trace ${f.traceId}: ${f.status ?? ""} ${f.error ?? ""}`.trim(),
);
}
if (finish.metricsError) {
console.error(` ⚠ metrics not logged: ${finish.metricsError}`);
}
if (!finish.finished) {
console.error(
` ✗ run left RUNNING — failed to finish: ${finish.finishError ?? "unknown"}`,
);
}
} else {
info(
"\nMLflow evaluation run skipped — pass --experiment (or set" +
" MLFLOW_EXPERIMENT_ID) plus --profile/--databricks-host to create one.",
);
}
// Machine-readable report: build the string with a pure formatter, then emit
// it to --output <file> or stdout (kept clean of the human noise above).
if (machine) {
const report =
reporter === "json"
? runner.formatResultsJson(summary.results)
: runner.formatResultsJUnit(summary.results);
if (opts.output) {
fs.writeFileSync(opts.output, `${report}\n`);
info(`Wrote ${reporter} report to ${opts.output}`);
} else {
process.stdout.write(`${report}\n`);
}
}
const stats = runner.summarize(summary.results);
const minPassRate = opts.minPassRate
? Number.parseFloat(opts.minPassRate)
: undefined;
if (minPassRate !== undefined && !Number.isNaN(minPassRate)) {
// Threshold mode: gate on the aggregate pass rate rather than requiring
// every eval to pass.
const ok = stats.passRate >= minPassRate;
info(
`Pass rate ${(stats.passRate * 100).toFixed(0)}% (threshold ${(
minPassRate * 100
).toFixed(0)}%) — ${ok ? "OK" : "below threshold"}`,
);
if (!ok) process.exitCode = 1;
} else if (!stats.allPassed) {
process.exitCode = 1;
}
}
export const agentEvalCommand = new Command("eval")
.description(
"Run agent evals (config/agents/<id>/evals/*.eval.ts) against a running app",
)
.argument(
"[filter]",
"Only run evals whose <agent>/<id> contains this substring (or an exact agent id)",
)
.option("--url <url>", "Base URL of the running app", "http://localhost:3000")
.option("--strict", "Fail on soft-assertion misses too", false)
.option(
"--root <dir>",
"Project root containing config/agents/ (default: cwd)",
)
.option(
"--header <header...>",
"Extra request header as 'Key: value' (repeatable)",
)
.option(
"--tag <tag...>",
"Only run evals tagged with one of these tags (repeatable)",
)
.option(
"--profile <name>",
"Databricks CLI profile to authenticate with via OAuth (default: DATABRICKS_CONFIG_PROFILE)",
)
.option(
"--databricks-host <host>",
"Databricks host for writing MLflow assessments (default: DATABRICKS_HOST)",
)
.option(
"--databricks-token <token>",
"Databricks token for writing MLflow assessments (default: DATABRICKS_TOKEN)",
)
.option(
"--experiment <id>",
"MLflow experiment id for the evaluation run (default: MLFLOW_EXPERIMENT_ID)",
)
.option(
"--warehouse <id>",
"SQL warehouse id for reading managed evaluation datasets (default: DATABRICKS_WAREHOUSE_ID)",
)
.option(
"--judge-model <endpoint>",
"Databricks serving endpoint to use as the LLM judge for t.judge.* (default: APPKIT_JUDGE_MODEL)",
)
.option(
"--concurrency <n>",
"Max evals/dataset rows to drive concurrently (default: 1, serial)",
)
.option(
"--timeout <ms>",
"Default per-eval timeout in ms (a per-eval timeoutMs overrides it)",
)
.option(
"--retries <n>",
"Re-run an eval up to N times when it fails on an infra error (turn/timeout); assertion failures are not retried",
)
.option(
"--min-pass-rate <rate>",
"Gate on aggregate pass rate (0..1) instead of requiring every eval to pass; exit 1 when below",
)
.addOption(
new Option(
"--reporter <format>",
"Report format: text (live console), json (dashboards), or junit (CI test reporters)",
)
.choices(["text", "json", "junit"])
.default("text"),
)
.option(
"--output <file>",
"Write the json/junit report to this file instead of stdout (ignored for text)",
)
.action(runAgentEval);