-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathupload.ts
More file actions
496 lines (453 loc) · 13.9 KB
/
upload.ts
File metadata and controls
496 lines (453 loc) · 13.9 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import {
type CodeBundleType as CodeBundle,
type FunctionType as FunctionObject,
type IfExistsType as IfExists,
} from "../../generated_types";
import type { BuildSuccess, EvaluatorState, FileHandle } from "../types";
import { scorerName, warning } from "../../framework";
import {
_internalGetGlobalState,
Experiment,
FailedHTTPResponse,
} from "../../logger";
import * as esbuild from "esbuild";
import fs from "node:fs";
import path from "node:path";
import { createGzip } from "node:zlib";
import { addAzureBlobHeaders, isEmpty } from "../../util";
import { z } from "zod/v3";
import { capitalize } from "../../../util/index";
import { findCodeDefinition, makeSourceMapContext } from "./infer-source";
import { slugify } from "../../../util/string_util";
import { zodToJsonSchema } from "../../zod/utils";
import pluralize from "pluralize";
import {
FunctionEvent,
ProjectNameIdMap,
serializeRemoteEvalParametersContainer,
} from "../../framework2";
export type EvaluatorMap = Record<
string,
{
evaluator: EvaluatorState["evaluators"][number];
experiment: Experiment;
}
>;
interface BundledFunctionSpec {
project_id: string;
name: string;
slug: string;
description: string;
location: CodeBundle["location"];
function_type: FunctionObject["function_type"];
origin?: FunctionObject["origin"];
function_schema?: FunctionObject["function_schema"];
if_exists?: IfExists;
tags?: string[];
metadata?: Record<string, unknown>;
}
type BundledFunctionEntry = FunctionEvent & {
origin?: FunctionObject["origin"];
function_schema?: FunctionObject["function_schema"];
};
const SANDBOX_GROUP_NAME_METADATA_KEY = "_bt_sandbox_group_name";
const pathInfoSchema = z
.strictObject({
url: z.string(),
bundleId: z.string(),
})
.strip();
export async function uploadHandleBundles({
buildResults,
evalToExperiment,
bundlePromises,
handles,
setCurrent,
showDetailedErrors,
defaultIfExists,
}: {
buildResults: BuildSuccess[];
evalToExperiment?: Record<string, Record<string, Experiment>>;
bundlePromises: {
[k: string]: Promise<esbuild.BuildResult<esbuild.BuildOptions>>;
};
handles: Record<string, FileHandle>;
showDetailedErrors: boolean;
setCurrent: boolean;
defaultIfExists: IfExists;
}) {
console.error(
`Processing ${buildResults.length} ${pluralize("file", buildResults.length)}...`,
);
const projectNameToId = new ProjectNameIdMap();
const uploadPromises = buildResults.map(async (result) => {
if (result.type !== "success") {
return;
}
const sourceFile = result.sourceFile;
const bundleSpecs: BundledFunctionSpec[] = [];
const prompts: FunctionEvent[] = [];
if (setCurrent) {
for (let i = 0; i < result.evaluator.functions.length; i++) {
const fn = result.evaluator.functions[i];
const project_id = await projectNameToId.resolve(fn.project);
bundleSpecs.push({
project_id: project_id,
name: fn.name,
slug: fn.slug,
description: fn.description ?? "",
function_type: fn.type,
location: {
type: "function",
index: i,
},
function_schema:
fn.parameters || fn.returns
? {
parameters: fn.parameters
? zodToJsonSchema(fn.parameters)
: undefined,
returns: fn.returns ? zodToJsonSchema(fn.returns) : undefined,
}
: undefined,
if_exists: fn.ifExists,
tags: fn.tags,
metadata: fn.metadata,
});
}
for (const prompt of result.evaluator.prompts) {
prompts.push(await prompt.toFunctionDefinition(projectNameToId));
}
if (result.evaluator.parameters != null) {
for (const param of result.evaluator.parameters) {
prompts.push(await param.toFunctionDefinition(projectNameToId));
}
}
}
for (const evaluator of Object.values(result.evaluator.evaluators)) {
const experiment =
evalToExperiment?.[sourceFile]?.[evaluator.evaluator.evalName];
const baseInfo = {
project_id: experiment
? (await experiment.project).id
: await projectNameToId.getId(evaluator.evaluator.projectName),
};
const namePrefix = setCurrent
? evaluator.evaluator.experimentName
? `${evaluator.evaluator.experimentName}`
: evaluator.evaluator.evalName
: experiment
? `${await experiment.name}`
: evaluator.evaluator.evalName;
const experimentId = experiment ? await experiment.id : undefined;
const origin: FunctionObject["origin"] = experimentId
? {
object_type: "experiment",
object_id: experimentId,
internal: !setCurrent,
}
: undefined;
const fileSpecs: BundledFunctionSpec[] = [
{
...baseInfo,
// There is a very small chance that someone names a function with the same convention, but
// let's assume it's low enough that it doesn't matter.
...formatNameAndSlug(["eval", namePrefix, "task"]),
description: `Task for eval ${namePrefix}`,
location: {
type: "experiment",
eval_name: evaluator.evaluator.evalName,
position: { type: "task" },
},
function_type: "task",
origin,
},
...(evaluator.evaluator.scores ?? []).map(
(score, i): BundledFunctionSpec => {
const name = scorerName(score, i);
return {
...baseInfo,
// There is a very small chance that someone names a function with the same convention, but
// let's assume it's low enough that it doesn't matter.
...formatNameAndSlug(["eval", namePrefix, "scorer", name]),
description: `Score ${name} for eval ${namePrefix}`,
location: {
type: "experiment",
eval_name: evaluator.evaluator.evalName,
position: { type: "scorer", index: i },
},
function_type: "scorer",
origin,
};
},
),
];
bundleSpecs.push(...fileSpecs);
if (setCurrent) {
const sourceStem = path
.basename(sourceFile, path.extname(sourceFile))
.replace(/\.eval$/, "");
const evalName = evaluator.evaluator.evalName;
const sandboxGroupName = sourceStem;
const resolvedParameters = evaluator.evaluator.parameters
? await Promise.resolve(evaluator.evaluator.parameters)
: undefined;
const evaluatorDefinition = {
...(resolvedParameters
? {
parameters:
serializeRemoteEvalParametersContainer(resolvedParameters),
}
: {}),
scores: (evaluator.evaluator.scores ?? []).map((score, i) => ({
name: scorerName(score, i),
})),
};
bundleSpecs.push({
...baseInfo,
name: `Eval ${evalName} sandbox`,
slug: slugify(`${sourceStem}-${evalName}-sandbox`),
description: `Sandbox eval ${evalName}`,
location: {
type: "sandbox",
sandbox_spec: {
provider: "lambda",
},
entrypoints: [sourceFile],
eval_name: evalName,
evaluator_definition: evaluatorDefinition,
},
function_type: "sandbox",
metadata: {
[SANDBOX_GROUP_NAME_METADATA_KEY]: sandboxGroupName,
},
origin,
});
}
}
const slugs: Set<string> = new Set();
for (const spec of bundleSpecs) {
if (slugs.has(spec.slug)) {
throw new Error(`Duplicate slug: ${spec.slug}`);
}
slugs.add(spec.slug);
}
for (const prompt of prompts) {
if (slugs.has(prompt.slug)) {
throw new Error(`Duplicate slug: ${prompt.slug}`);
}
slugs.add(prompt.slug);
}
return await uploadBundles({
sourceFile,
prompts,
bundleSpecs,
bundlePromises,
handles,
defaultIfExists,
showDetailedErrors,
});
});
const uploadResults = await Promise.all(uploadPromises);
const numUploaded = uploadResults.length;
const numFailed = uploadResults.filter((result) => !result).length;
console.error(
`${numUploaded} ${pluralize("file", numUploaded)} uploaded ${
numFailed > 0
? `with ${numFailed} error${numFailed > 1 ? "s" : ""}`
: "successfully"
}.`,
);
return {
numTotal: buildResults.length,
numUploaded,
numFailed,
};
}
async function uploadBundles({
sourceFile,
prompts,
bundleSpecs,
bundlePromises,
handles,
defaultIfExists,
showDetailedErrors,
}: {
sourceFile: string;
prompts: FunctionEvent[];
bundleSpecs: BundledFunctionSpec[];
bundlePromises: {
[k: string]: Promise<esbuild.BuildResult<esbuild.BuildOptions>>;
};
handles: Record<string, FileHandle>;
defaultIfExists: IfExists;
showDetailedErrors: boolean;
}): Promise<boolean> {
const orgId = _internalGetGlobalState().orgId;
if (!orgId) {
throw new Error("No organization ID found");
}
const loggerConn = _internalGetGlobalState().apiConn();
const runtime_context = {
runtime: "node",
version: process.version.slice(1),
} as const;
const bundle = await bundlePromises[sourceFile];
const bundleFileName = handles[sourceFile].bundleFile;
if (!bundle || !bundleFileName) {
return false;
}
const sourceMapContextPromise = makeSourceMapContext({
inFile: sourceFile,
outFile: bundleFileName,
sourceMapFile: bundleFileName + ".map",
});
let pathInfo: z.infer<typeof pathInfoSchema> | undefined = undefined;
if (bundleSpecs.length > 0) {
try {
pathInfo = pathInfoSchema.parse(
await loggerConn.post_json("function/code", {
org_id: orgId,
runtime_context,
}),
);
} catch (e) {
if (showDetailedErrors) {
console.error(e);
}
const msg =
e instanceof FailedHTTPResponse
? `Unable to upload your code. ${e.status} (${e.text}): ${e.data}`
: `Unable to upload your code. You most likely need to update the API: ${e}`;
console.error(warning(msg));
return false;
}
}
// Upload bundleFile to pathInfo.url
if (isEmpty(bundleFileName)) {
throw new Error("No bundle file found");
}
const bundleFile = path.resolve(bundleFileName);
const uploadPromise = (async (): Promise<boolean> => {
if (!pathInfo) {
return true;
}
const bundleStream = fs.createReadStream(bundleFile).pipe(createGzip());
const bundleData = await new Promise<Buffer>((resolve, reject) => {
const chunks: Buffer[] = [];
bundleStream.on("data", (chunk) => {
chunks.push(chunk);
});
bundleStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
bundleStream.on("error", reject);
});
const headers = {
"Content-Encoding": "gzip",
};
addAzureBlobHeaders(headers, pathInfo.url);
const resp = await fetch(pathInfo.url, {
method: "PUT",
body: bundleData,
headers,
});
if (!resp.ok) {
throw new Error(
`Failed to upload bundle: ${resp.status} ${await resp.text()}`,
);
}
return true;
})();
const sourceMapContext = await sourceMapContextPromise;
// Insert the spec as prompt data
const functionEntries: FunctionEvent[] = [
...prompts,
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
...((await Promise.all(
bundleSpecs.map((spec) =>
buildBundledFunctionEntry({
spec,
runtime_context,
bundleId: pathInfo!.bundleId,
sourceMapContext,
}),
),
)) as FunctionEvent[]),
].map((fn) => ({
...fn,
if_exists: fn.if_exists ?? defaultIfExists,
}));
const logPromise = (async (): Promise<boolean> => {
try {
await _internalGetGlobalState().apiConn().post_json("insert-functions", {
functions: functionEntries,
});
} catch (e) {
if (showDetailedErrors) {
console.error(e);
}
const msg =
e instanceof FailedHTTPResponse
? `Failed to save function definitions for '${sourceFile}'. ${e.status} (${e.text}): ${e.data}`
: `Failed to save function definitions for '${sourceFile}'. You most likely need to update the API: ${e}`;
console.warn(warning(msg));
return false;
}
return true;
})();
const [uploadSuccess, logSuccess] = await Promise.all([
uploadPromise,
logPromise,
]);
return uploadSuccess && logSuccess;
}
function formatNameAndSlug(pieces: string[]) {
const nonEmptyPieces = pieces.filter((piece) => piece.trim() !== "");
return {
name: capitalize(nonEmptyPieces.join(" ")),
slug: slugify(nonEmptyPieces.join("-")),
};
}
export async function buildBundledFunctionEntry({
spec,
runtime_context,
bundleId,
sourceMapContext,
}: {
spec: BundledFunctionSpec;
runtime_context: {
runtime: "node";
version: string;
};
bundleId: string;
sourceMapContext?: Awaited<ReturnType<typeof makeSourceMapContext>>;
}): Promise<BundledFunctionEntry> {
return {
project_id: spec.project_id,
name: spec.name,
slug: spec.slug,
description: spec.description,
function_data: {
type: "code",
data: {
type: "bundle",
runtime_context,
location: spec.location,
bundle_id: bundleId,
preview: sourceMapContext
? await findCodeDefinition({
location: spec.location,
ctx: sourceMapContext,
})
: undefined,
},
},
origin: spec.origin,
function_type: spec.function_type ?? undefined,
function_schema: spec.function_schema,
if_exists: spec.if_exists,
tags: spec.tags,
metadata: spec.metadata,
};
}