braintrust 0.0.145 → 0.0.147
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/braintrust-0.0.141.tgz +0 -0
- package/dist/browser.d.mts +40 -7
- package/dist/browser.d.ts +40 -7
- package/dist/browser.js +122 -7
- package/dist/browser.mjs +121 -8
- package/dist/cli.js +137 -14
- package/dist/index.d.mts +49 -10
- package/dist/index.d.ts +49 -10
- package/dist/index.js +161 -17
- package/dist/index.mjs +160 -18
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -74,6 +74,7 @@ __export(src_exports, {
|
|
|
74
74
|
summarize: () => summarize,
|
|
75
75
|
traceable: () => traceable,
|
|
76
76
|
traced: () => traced,
|
|
77
|
+
updateSpan: () => updateSpan,
|
|
77
78
|
withDataset: () => withDataset,
|
|
78
79
|
withExperiment: () => withExperiment,
|
|
79
80
|
withLogger: () => withLogger,
|
|
@@ -420,6 +421,33 @@ var BraintrustStream = class _BraintrustStream {
|
|
|
420
421
|
toReadableStream() {
|
|
421
422
|
return this.stream;
|
|
422
423
|
}
|
|
424
|
+
/**
|
|
425
|
+
* Returns an async iterator for the BraintrustStream.
|
|
426
|
+
* This allows for easy consumption of the stream using a for-await...of loop.
|
|
427
|
+
*
|
|
428
|
+
* @returns An async iterator that yields BraintrustStreamChunk objects.
|
|
429
|
+
*/
|
|
430
|
+
[Symbol.asyncIterator]() {
|
|
431
|
+
const reader = this.stream.getReader();
|
|
432
|
+
return {
|
|
433
|
+
async next() {
|
|
434
|
+
const { done, value } = await reader.read();
|
|
435
|
+
if (done) {
|
|
436
|
+
reader.releaseLock();
|
|
437
|
+
return { done: true, value: void 0 };
|
|
438
|
+
}
|
|
439
|
+
return { done: false, value };
|
|
440
|
+
},
|
|
441
|
+
async return() {
|
|
442
|
+
reader.releaseLock();
|
|
443
|
+
return { done: true, value: void 0 };
|
|
444
|
+
},
|
|
445
|
+
async throw(error2) {
|
|
446
|
+
reader.releaseLock();
|
|
447
|
+
throw error2;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
}
|
|
423
451
|
/**
|
|
424
452
|
* Get the final value of the stream. The final value is the concatenation of all
|
|
425
453
|
* the chunks in the stream, deserialized into a string or JSON object, depending on
|
|
@@ -708,8 +736,10 @@ var BraintrustState = class _BraintrustState {
|
|
|
708
736
|
state.apiConn().set_token(state.loginToken);
|
|
709
737
|
state.apiConn().make_long_lived();
|
|
710
738
|
state.appConn().set_token(state.loginToken);
|
|
711
|
-
state.
|
|
712
|
-
|
|
739
|
+
if (state.proxyUrl) {
|
|
740
|
+
state.proxyConn().make_long_lived();
|
|
741
|
+
state.proxyConn().set_token(state.loginToken);
|
|
742
|
+
}
|
|
713
743
|
state.loggedIn = true;
|
|
714
744
|
state.loginReplaceApiConn(state.apiConn());
|
|
715
745
|
return state;
|
|
@@ -749,6 +779,9 @@ var BraintrustState = class _BraintrustState {
|
|
|
749
779
|
return this._apiConn;
|
|
750
780
|
}
|
|
751
781
|
proxyConn() {
|
|
782
|
+
if (!this.proxyUrl) {
|
|
783
|
+
return this.apiConn();
|
|
784
|
+
}
|
|
752
785
|
if (!this._proxyConn) {
|
|
753
786
|
if (!this.proxyUrl) {
|
|
754
787
|
throw new Error("Must initialize proxyUrl before requesting proxyConn");
|
|
@@ -966,6 +999,41 @@ function logFeedbackImpl(state, parentObjectType, parentObjectId, {
|
|
|
966
999
|
state.bgLogger().log([record]);
|
|
967
1000
|
}
|
|
968
1001
|
}
|
|
1002
|
+
function updateSpanImpl(state, parentObjectType, parentObjectId, id, event) {
|
|
1003
|
+
const updateEvent = validateAndSanitizeExperimentLogPartialArgs({
|
|
1004
|
+
id,
|
|
1005
|
+
...event
|
|
1006
|
+
});
|
|
1007
|
+
const parentIds = async () => new import_core.SpanComponentsV2({
|
|
1008
|
+
objectType: parentObjectType,
|
|
1009
|
+
objectId: await parentObjectId.get()
|
|
1010
|
+
}).objectIdFields();
|
|
1011
|
+
const record = new LazyValue(async () => ({
|
|
1012
|
+
id,
|
|
1013
|
+
...updateEvent,
|
|
1014
|
+
...await parentIds(),
|
|
1015
|
+
[import_core.IS_MERGE_FIELD]: true
|
|
1016
|
+
}));
|
|
1017
|
+
state.bgLogger().log([record]);
|
|
1018
|
+
}
|
|
1019
|
+
function updateSpan({
|
|
1020
|
+
exported,
|
|
1021
|
+
state,
|
|
1022
|
+
...event
|
|
1023
|
+
}) {
|
|
1024
|
+
const resolvedState = state ?? _globalState;
|
|
1025
|
+
const components = import_core.SpanComponentsV2.fromStr(exported);
|
|
1026
|
+
if (!components.rowIds?.rowId) {
|
|
1027
|
+
throw new Error("Exported span must have a row id");
|
|
1028
|
+
}
|
|
1029
|
+
updateSpanImpl(
|
|
1030
|
+
resolvedState,
|
|
1031
|
+
components.objectType,
|
|
1032
|
+
new LazyValue(spanComponentsToObjectIdLambda(resolvedState, components)),
|
|
1033
|
+
components.rowIds?.rowId,
|
|
1034
|
+
event
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
969
1037
|
function spanComponentsToObjectIdLambda(state, components) {
|
|
970
1038
|
if (components.objectId) {
|
|
971
1039
|
const ret = components.objectId;
|
|
@@ -1170,6 +1238,25 @@ var Logger = class {
|
|
|
1170
1238
|
logFeedback(event) {
|
|
1171
1239
|
logFeedbackImpl(this.state, this.parentObjectType(), this.lazyId, event);
|
|
1172
1240
|
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Update a span in the experiment using its id. It is important that you only update a span once the original span has been fully written and flushed,
|
|
1243
|
+
* since otherwise updates to the span may conflict with the original span.
|
|
1244
|
+
*
|
|
1245
|
+
* @param event The event data to update the span with. Must include `id`. See `Experiment.log` for a full list of valid fields.
|
|
1246
|
+
*/
|
|
1247
|
+
updateSpan(event) {
|
|
1248
|
+
const { id, ...eventRest } = event;
|
|
1249
|
+
if (!id) {
|
|
1250
|
+
throw new Error("Span id is required to update a span");
|
|
1251
|
+
}
|
|
1252
|
+
updateSpanImpl(
|
|
1253
|
+
this.state,
|
|
1254
|
+
this.parentObjectType(),
|
|
1255
|
+
this.lazyId,
|
|
1256
|
+
id,
|
|
1257
|
+
eventRest
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1173
1260
|
/**
|
|
1174
1261
|
* Return a serialized representation of the logger that can be used to start subspans in other places. See `Span.start_span` for more details.
|
|
1175
1262
|
*/
|
|
@@ -1889,7 +1976,7 @@ async function loadPrompt({
|
|
|
1889
1976
|
throw new Error("Must specify slug");
|
|
1890
1977
|
}
|
|
1891
1978
|
const state = stateArg ?? _globalState;
|
|
1892
|
-
state.login({
|
|
1979
|
+
await state.login({
|
|
1893
1980
|
orgName,
|
|
1894
1981
|
apiKey,
|
|
1895
1982
|
appUrl,
|
|
@@ -1976,7 +2063,9 @@ async function loginToState(options = {}) {
|
|
|
1976
2063
|
}
|
|
1977
2064
|
conn.make_long_lived();
|
|
1978
2065
|
state.appConn().set_token(apiKey);
|
|
1979
|
-
state.
|
|
2066
|
+
if (state.proxyUrl) {
|
|
2067
|
+
state.proxyConn().set_token(apiKey);
|
|
2068
|
+
}
|
|
1980
2069
|
state.loginToken = conn.token;
|
|
1981
2070
|
state.loggedIn = true;
|
|
1982
2071
|
state.loginReplaceApiConn(conn);
|
|
@@ -2507,6 +2596,25 @@ var Experiment = class extends ObjectFetcher {
|
|
|
2507
2596
|
logFeedback(event) {
|
|
2508
2597
|
logFeedbackImpl(this.state, this.parentObjectType(), this.lazyId, event);
|
|
2509
2598
|
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Update a span in the experiment using its id. It is important that you only update a span once the original span has been fully written and flushed,
|
|
2601
|
+
* since otherwise updates to the span may conflict with the original span.
|
|
2602
|
+
*
|
|
2603
|
+
* @param event The event data to update the span with. Must include `id`. See `Experiment.log` for a full list of valid fields.
|
|
2604
|
+
*/
|
|
2605
|
+
updateSpan(event) {
|
|
2606
|
+
const { id, ...eventRest } = event;
|
|
2607
|
+
if (!id) {
|
|
2608
|
+
throw new Error("Span id is required to update a span");
|
|
2609
|
+
}
|
|
2610
|
+
updateSpanImpl(
|
|
2611
|
+
this.state,
|
|
2612
|
+
this.parentObjectType(),
|
|
2613
|
+
this.lazyId,
|
|
2614
|
+
id,
|
|
2615
|
+
eventRest
|
|
2616
|
+
);
|
|
2617
|
+
}
|
|
2510
2618
|
/**
|
|
2511
2619
|
* Return a serialized representation of the experiment that can be used to start subspans in other places. See `Span.start_span` for more details.
|
|
2512
2620
|
*/
|
|
@@ -3117,7 +3225,14 @@ async function invoke(args) {
|
|
|
3117
3225
|
forceLogin
|
|
3118
3226
|
});
|
|
3119
3227
|
const parent = parentArg ? typeof parentArg === "string" ? parentArg : await parentArg.export() : await getSpanParentObject().export();
|
|
3120
|
-
const functionId = import_typespecs3.functionIdSchema.safeParse(
|
|
3228
|
+
const functionId = import_typespecs3.functionIdSchema.safeParse({
|
|
3229
|
+
project_name: functionIdArgs.projectName,
|
|
3230
|
+
slug: functionIdArgs.slug,
|
|
3231
|
+
global_function: functionIdArgs.globalFunction,
|
|
3232
|
+
prompt_session_id: functionIdArgs.promptSessionId,
|
|
3233
|
+
prompt_session_function_id: functionIdArgs.promptSessionFunctionId,
|
|
3234
|
+
version: functionIdArgs.version
|
|
3235
|
+
});
|
|
3121
3236
|
if (!functionId.success) {
|
|
3122
3237
|
throw new Error(
|
|
3123
3238
|
`Invalid function ID arguments: ${functionId.error.message}`
|
|
@@ -3127,8 +3242,7 @@ async function invoke(args) {
|
|
|
3127
3242
|
...functionId.data,
|
|
3128
3243
|
input,
|
|
3129
3244
|
parent,
|
|
3130
|
-
stream
|
|
3131
|
-
api_version: import_typespecs3.INVOKE_API_VERSION
|
|
3245
|
+
stream
|
|
3132
3246
|
};
|
|
3133
3247
|
const resp = await state.proxyConn().post(`function/invoke`, request, {
|
|
3134
3248
|
headers: {
|
|
@@ -4297,6 +4411,24 @@ var waterfall$1 = awaitify(waterfall);
|
|
|
4297
4411
|
function BaseExperiment(options = {}) {
|
|
4298
4412
|
return { _type: "BaseExperiment", ...options };
|
|
4299
4413
|
}
|
|
4414
|
+
var EvalResultWithSummary = class {
|
|
4415
|
+
constructor(summary, results) {
|
|
4416
|
+
this.summary = summary;
|
|
4417
|
+
this.results = results;
|
|
4418
|
+
}
|
|
4419
|
+
toString() {
|
|
4420
|
+
return formatExperimentSummary(this.summary);
|
|
4421
|
+
}
|
|
4422
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
4423
|
+
return `EvalResultWithSummary(summary="...", results=[...])`;
|
|
4424
|
+
}
|
|
4425
|
+
toJSON() {
|
|
4426
|
+
return {
|
|
4427
|
+
summary: this.summary,
|
|
4428
|
+
results: this.results
|
|
4429
|
+
};
|
|
4430
|
+
}
|
|
4431
|
+
};
|
|
4300
4432
|
function makeEvalName(projectName, experimentName) {
|
|
4301
4433
|
let out = projectName;
|
|
4302
4434
|
if (experimentName) {
|
|
@@ -4311,6 +4443,17 @@ function initExperiment2(state, options = {}) {
|
|
|
4311
4443
|
setCurrent: false
|
|
4312
4444
|
});
|
|
4313
4445
|
}
|
|
4446
|
+
function callEvaluatorData(data) {
|
|
4447
|
+
let dataResult = typeof data === "function" ? data() : data;
|
|
4448
|
+
let baseExperiment = void 0;
|
|
4449
|
+
if ("_type" in dataResult && dataResult._type === "BaseExperiment") {
|
|
4450
|
+
baseExperiment = dataResult.name;
|
|
4451
|
+
}
|
|
4452
|
+
return {
|
|
4453
|
+
data: dataResult,
|
|
4454
|
+
baseExperiment
|
|
4455
|
+
};
|
|
4456
|
+
}
|
|
4314
4457
|
globalThis._evals = {
|
|
4315
4458
|
evaluators: {},
|
|
4316
4459
|
reporters: {}
|
|
@@ -4327,15 +4470,15 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4327
4470
|
reporter: options.reporter
|
|
4328
4471
|
};
|
|
4329
4472
|
globalThis._spanContext = { currentSpan, NOOP_SPAN };
|
|
4330
|
-
return
|
|
4331
|
-
|
|
4473
|
+
return new EvalResultWithSummary(
|
|
4474
|
+
{
|
|
4332
4475
|
scores: {},
|
|
4333
4476
|
metrics: {},
|
|
4334
4477
|
projectName: "",
|
|
4335
4478
|
experimentName: ""
|
|
4336
4479
|
},
|
|
4337
|
-
|
|
4338
|
-
|
|
4480
|
+
[]
|
|
4481
|
+
);
|
|
4339
4482
|
}
|
|
4340
4483
|
const progressReporter = new BarProgressReporter();
|
|
4341
4484
|
if (typeof options.reporter === "string") {
|
|
@@ -4345,12 +4488,14 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4345
4488
|
}
|
|
4346
4489
|
const resolvedReporter = options.reporter || defaultReporter;
|
|
4347
4490
|
try {
|
|
4491
|
+
const { data, baseExperiment } = callEvaluatorData(evaluator.data);
|
|
4348
4492
|
const experiment = initExperiment2(evaluator.state, {
|
|
4349
4493
|
...evaluator.projectId ? { projectId: evaluator.projectId } : { project: name },
|
|
4350
4494
|
experiment: evaluator.experimentName,
|
|
4351
4495
|
metadata: evaluator.metadata,
|
|
4352
4496
|
isPublic: evaluator.isPublic,
|
|
4353
|
-
update: evaluator.update
|
|
4497
|
+
update: evaluator.update,
|
|
4498
|
+
baseExperiment
|
|
4354
4499
|
});
|
|
4355
4500
|
if (options.onStart) {
|
|
4356
4501
|
experiment.summarize({ summarizeScores: false }).then(options.onStart);
|
|
@@ -4359,7 +4504,8 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4359
4504
|
const evalDef = {
|
|
4360
4505
|
evalName,
|
|
4361
4506
|
projectName: name,
|
|
4362
|
-
...evaluator
|
|
4507
|
+
...evaluator,
|
|
4508
|
+
data
|
|
4363
4509
|
};
|
|
4364
4510
|
const ret = await runEvaluator(experiment, evalDef, progressReporter, []);
|
|
4365
4511
|
progressReporter.stop();
|
|
@@ -4634,10 +4780,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
4634
4780
|
q.push(data);
|
|
4635
4781
|
await q.drain();
|
|
4636
4782
|
const summary = experiment ? await experiment.summarize() : buildLocalSummary(evaluator, results);
|
|
4637
|
-
return
|
|
4638
|
-
summary,
|
|
4639
|
-
results
|
|
4640
|
-
};
|
|
4783
|
+
return new EvalResultWithSummary(summary, results);
|
|
4641
4784
|
}
|
|
4642
4785
|
var error = import_chalk.default.bold.red;
|
|
4643
4786
|
var warning = import_chalk.default.hex("#FFA500");
|
|
@@ -5332,6 +5475,7 @@ configureNode();
|
|
|
5332
5475
|
summarize,
|
|
5333
5476
|
traceable,
|
|
5334
5477
|
traced,
|
|
5478
|
+
updateSpan,
|
|
5335
5479
|
withDataset,
|
|
5336
5480
|
withExperiment,
|
|
5337
5481
|
withLogger,
|
package/dist/index.mjs
CHANGED
|
@@ -360,6 +360,33 @@ var BraintrustStream = class _BraintrustStream {
|
|
|
360
360
|
toReadableStream() {
|
|
361
361
|
return this.stream;
|
|
362
362
|
}
|
|
363
|
+
/**
|
|
364
|
+
* Returns an async iterator for the BraintrustStream.
|
|
365
|
+
* This allows for easy consumption of the stream using a for-await...of loop.
|
|
366
|
+
*
|
|
367
|
+
* @returns An async iterator that yields BraintrustStreamChunk objects.
|
|
368
|
+
*/
|
|
369
|
+
[Symbol.asyncIterator]() {
|
|
370
|
+
const reader = this.stream.getReader();
|
|
371
|
+
return {
|
|
372
|
+
async next() {
|
|
373
|
+
const { done, value } = await reader.read();
|
|
374
|
+
if (done) {
|
|
375
|
+
reader.releaseLock();
|
|
376
|
+
return { done: true, value: void 0 };
|
|
377
|
+
}
|
|
378
|
+
return { done: false, value };
|
|
379
|
+
},
|
|
380
|
+
async return() {
|
|
381
|
+
reader.releaseLock();
|
|
382
|
+
return { done: true, value: void 0 };
|
|
383
|
+
},
|
|
384
|
+
async throw(error2) {
|
|
385
|
+
reader.releaseLock();
|
|
386
|
+
throw error2;
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
}
|
|
363
390
|
/**
|
|
364
391
|
* Get the final value of the stream. The final value is the concatenation of all
|
|
365
392
|
* the chunks in the stream, deserialized into a string or JSON object, depending on
|
|
@@ -648,8 +675,10 @@ var BraintrustState = class _BraintrustState {
|
|
|
648
675
|
state.apiConn().set_token(state.loginToken);
|
|
649
676
|
state.apiConn().make_long_lived();
|
|
650
677
|
state.appConn().set_token(state.loginToken);
|
|
651
|
-
state.
|
|
652
|
-
|
|
678
|
+
if (state.proxyUrl) {
|
|
679
|
+
state.proxyConn().make_long_lived();
|
|
680
|
+
state.proxyConn().set_token(state.loginToken);
|
|
681
|
+
}
|
|
653
682
|
state.loggedIn = true;
|
|
654
683
|
state.loginReplaceApiConn(state.apiConn());
|
|
655
684
|
return state;
|
|
@@ -689,6 +718,9 @@ var BraintrustState = class _BraintrustState {
|
|
|
689
718
|
return this._apiConn;
|
|
690
719
|
}
|
|
691
720
|
proxyConn() {
|
|
721
|
+
if (!this.proxyUrl) {
|
|
722
|
+
return this.apiConn();
|
|
723
|
+
}
|
|
692
724
|
if (!this._proxyConn) {
|
|
693
725
|
if (!this.proxyUrl) {
|
|
694
726
|
throw new Error("Must initialize proxyUrl before requesting proxyConn");
|
|
@@ -906,6 +938,41 @@ function logFeedbackImpl(state, parentObjectType, parentObjectId, {
|
|
|
906
938
|
state.bgLogger().log([record]);
|
|
907
939
|
}
|
|
908
940
|
}
|
|
941
|
+
function updateSpanImpl(state, parentObjectType, parentObjectId, id, event) {
|
|
942
|
+
const updateEvent = validateAndSanitizeExperimentLogPartialArgs({
|
|
943
|
+
id,
|
|
944
|
+
...event
|
|
945
|
+
});
|
|
946
|
+
const parentIds = async () => new SpanComponentsV2({
|
|
947
|
+
objectType: parentObjectType,
|
|
948
|
+
objectId: await parentObjectId.get()
|
|
949
|
+
}).objectIdFields();
|
|
950
|
+
const record = new LazyValue(async () => ({
|
|
951
|
+
id,
|
|
952
|
+
...updateEvent,
|
|
953
|
+
...await parentIds(),
|
|
954
|
+
[IS_MERGE_FIELD]: true
|
|
955
|
+
}));
|
|
956
|
+
state.bgLogger().log([record]);
|
|
957
|
+
}
|
|
958
|
+
function updateSpan({
|
|
959
|
+
exported,
|
|
960
|
+
state,
|
|
961
|
+
...event
|
|
962
|
+
}) {
|
|
963
|
+
const resolvedState = state ?? _globalState;
|
|
964
|
+
const components = SpanComponentsV2.fromStr(exported);
|
|
965
|
+
if (!components.rowIds?.rowId) {
|
|
966
|
+
throw new Error("Exported span must have a row id");
|
|
967
|
+
}
|
|
968
|
+
updateSpanImpl(
|
|
969
|
+
resolvedState,
|
|
970
|
+
components.objectType,
|
|
971
|
+
new LazyValue(spanComponentsToObjectIdLambda(resolvedState, components)),
|
|
972
|
+
components.rowIds?.rowId,
|
|
973
|
+
event
|
|
974
|
+
);
|
|
975
|
+
}
|
|
909
976
|
function spanComponentsToObjectIdLambda(state, components) {
|
|
910
977
|
if (components.objectId) {
|
|
911
978
|
const ret = components.objectId;
|
|
@@ -1110,6 +1177,25 @@ var Logger = class {
|
|
|
1110
1177
|
logFeedback(event) {
|
|
1111
1178
|
logFeedbackImpl(this.state, this.parentObjectType(), this.lazyId, event);
|
|
1112
1179
|
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Update a span in the experiment using its id. It is important that you only update a span once the original span has been fully written and flushed,
|
|
1182
|
+
* since otherwise updates to the span may conflict with the original span.
|
|
1183
|
+
*
|
|
1184
|
+
* @param event The event data to update the span with. Must include `id`. See `Experiment.log` for a full list of valid fields.
|
|
1185
|
+
*/
|
|
1186
|
+
updateSpan(event) {
|
|
1187
|
+
const { id, ...eventRest } = event;
|
|
1188
|
+
if (!id) {
|
|
1189
|
+
throw new Error("Span id is required to update a span");
|
|
1190
|
+
}
|
|
1191
|
+
updateSpanImpl(
|
|
1192
|
+
this.state,
|
|
1193
|
+
this.parentObjectType(),
|
|
1194
|
+
this.lazyId,
|
|
1195
|
+
id,
|
|
1196
|
+
eventRest
|
|
1197
|
+
);
|
|
1198
|
+
}
|
|
1113
1199
|
/**
|
|
1114
1200
|
* Return a serialized representation of the logger that can be used to start subspans in other places. See `Span.start_span` for more details.
|
|
1115
1201
|
*/
|
|
@@ -1829,7 +1915,7 @@ async function loadPrompt({
|
|
|
1829
1915
|
throw new Error("Must specify slug");
|
|
1830
1916
|
}
|
|
1831
1917
|
const state = stateArg ?? _globalState;
|
|
1832
|
-
state.login({
|
|
1918
|
+
await state.login({
|
|
1833
1919
|
orgName,
|
|
1834
1920
|
apiKey,
|
|
1835
1921
|
appUrl,
|
|
@@ -1916,7 +2002,9 @@ async function loginToState(options = {}) {
|
|
|
1916
2002
|
}
|
|
1917
2003
|
conn.make_long_lived();
|
|
1918
2004
|
state.appConn().set_token(apiKey);
|
|
1919
|
-
state.
|
|
2005
|
+
if (state.proxyUrl) {
|
|
2006
|
+
state.proxyConn().set_token(apiKey);
|
|
2007
|
+
}
|
|
1920
2008
|
state.loginToken = conn.token;
|
|
1921
2009
|
state.loggedIn = true;
|
|
1922
2010
|
state.loginReplaceApiConn(conn);
|
|
@@ -2447,6 +2535,25 @@ var Experiment = class extends ObjectFetcher {
|
|
|
2447
2535
|
logFeedback(event) {
|
|
2448
2536
|
logFeedbackImpl(this.state, this.parentObjectType(), this.lazyId, event);
|
|
2449
2537
|
}
|
|
2538
|
+
/**
|
|
2539
|
+
* Update a span in the experiment using its id. It is important that you only update a span once the original span has been fully written and flushed,
|
|
2540
|
+
* since otherwise updates to the span may conflict with the original span.
|
|
2541
|
+
*
|
|
2542
|
+
* @param event The event data to update the span with. Must include `id`. See `Experiment.log` for a full list of valid fields.
|
|
2543
|
+
*/
|
|
2544
|
+
updateSpan(event) {
|
|
2545
|
+
const { id, ...eventRest } = event;
|
|
2546
|
+
if (!id) {
|
|
2547
|
+
throw new Error("Span id is required to update a span");
|
|
2548
|
+
}
|
|
2549
|
+
updateSpanImpl(
|
|
2550
|
+
this.state,
|
|
2551
|
+
this.parentObjectType(),
|
|
2552
|
+
this.lazyId,
|
|
2553
|
+
id,
|
|
2554
|
+
eventRest
|
|
2555
|
+
);
|
|
2556
|
+
}
|
|
2450
2557
|
/**
|
|
2451
2558
|
* Return a serialized representation of the experiment that can be used to start subspans in other places. See `Span.start_span` for more details.
|
|
2452
2559
|
*/
|
|
@@ -3035,7 +3142,6 @@ function configureNode() {
|
|
|
3035
3142
|
|
|
3036
3143
|
// src/functions/invoke.ts
|
|
3037
3144
|
import {
|
|
3038
|
-
INVOKE_API_VERSION,
|
|
3039
3145
|
functionIdSchema
|
|
3040
3146
|
} from "@braintrust/core/typespecs";
|
|
3041
3147
|
async function invoke(args) {
|
|
@@ -3060,7 +3166,14 @@ async function invoke(args) {
|
|
|
3060
3166
|
forceLogin
|
|
3061
3167
|
});
|
|
3062
3168
|
const parent = parentArg ? typeof parentArg === "string" ? parentArg : await parentArg.export() : await getSpanParentObject().export();
|
|
3063
|
-
const functionId = functionIdSchema.safeParse(
|
|
3169
|
+
const functionId = functionIdSchema.safeParse({
|
|
3170
|
+
project_name: functionIdArgs.projectName,
|
|
3171
|
+
slug: functionIdArgs.slug,
|
|
3172
|
+
global_function: functionIdArgs.globalFunction,
|
|
3173
|
+
prompt_session_id: functionIdArgs.promptSessionId,
|
|
3174
|
+
prompt_session_function_id: functionIdArgs.promptSessionFunctionId,
|
|
3175
|
+
version: functionIdArgs.version
|
|
3176
|
+
});
|
|
3064
3177
|
if (!functionId.success) {
|
|
3065
3178
|
throw new Error(
|
|
3066
3179
|
`Invalid function ID arguments: ${functionId.error.message}`
|
|
@@ -3070,8 +3183,7 @@ async function invoke(args) {
|
|
|
3070
3183
|
...functionId.data,
|
|
3071
3184
|
input,
|
|
3072
3185
|
parent,
|
|
3073
|
-
stream
|
|
3074
|
-
api_version: INVOKE_API_VERSION
|
|
3186
|
+
stream
|
|
3075
3187
|
};
|
|
3076
3188
|
const resp = await state.proxyConn().post(`function/invoke`, request, {
|
|
3077
3189
|
headers: {
|
|
@@ -4240,6 +4352,24 @@ var waterfall$1 = awaitify(waterfall);
|
|
|
4240
4352
|
function BaseExperiment(options = {}) {
|
|
4241
4353
|
return { _type: "BaseExperiment", ...options };
|
|
4242
4354
|
}
|
|
4355
|
+
var EvalResultWithSummary = class {
|
|
4356
|
+
constructor(summary, results) {
|
|
4357
|
+
this.summary = summary;
|
|
4358
|
+
this.results = results;
|
|
4359
|
+
}
|
|
4360
|
+
toString() {
|
|
4361
|
+
return formatExperimentSummary(this.summary);
|
|
4362
|
+
}
|
|
4363
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
4364
|
+
return `EvalResultWithSummary(summary="...", results=[...])`;
|
|
4365
|
+
}
|
|
4366
|
+
toJSON() {
|
|
4367
|
+
return {
|
|
4368
|
+
summary: this.summary,
|
|
4369
|
+
results: this.results
|
|
4370
|
+
};
|
|
4371
|
+
}
|
|
4372
|
+
};
|
|
4243
4373
|
function makeEvalName(projectName, experimentName) {
|
|
4244
4374
|
let out = projectName;
|
|
4245
4375
|
if (experimentName) {
|
|
@@ -4254,6 +4384,17 @@ function initExperiment2(state, options = {}) {
|
|
|
4254
4384
|
setCurrent: false
|
|
4255
4385
|
});
|
|
4256
4386
|
}
|
|
4387
|
+
function callEvaluatorData(data) {
|
|
4388
|
+
let dataResult = typeof data === "function" ? data() : data;
|
|
4389
|
+
let baseExperiment = void 0;
|
|
4390
|
+
if ("_type" in dataResult && dataResult._type === "BaseExperiment") {
|
|
4391
|
+
baseExperiment = dataResult.name;
|
|
4392
|
+
}
|
|
4393
|
+
return {
|
|
4394
|
+
data: dataResult,
|
|
4395
|
+
baseExperiment
|
|
4396
|
+
};
|
|
4397
|
+
}
|
|
4257
4398
|
globalThis._evals = {
|
|
4258
4399
|
evaluators: {},
|
|
4259
4400
|
reporters: {}
|
|
@@ -4270,15 +4411,15 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4270
4411
|
reporter: options.reporter
|
|
4271
4412
|
};
|
|
4272
4413
|
globalThis._spanContext = { currentSpan, NOOP_SPAN };
|
|
4273
|
-
return
|
|
4274
|
-
|
|
4414
|
+
return new EvalResultWithSummary(
|
|
4415
|
+
{
|
|
4275
4416
|
scores: {},
|
|
4276
4417
|
metrics: {},
|
|
4277
4418
|
projectName: "",
|
|
4278
4419
|
experimentName: ""
|
|
4279
4420
|
},
|
|
4280
|
-
|
|
4281
|
-
|
|
4421
|
+
[]
|
|
4422
|
+
);
|
|
4282
4423
|
}
|
|
4283
4424
|
const progressReporter = new BarProgressReporter();
|
|
4284
4425
|
if (typeof options.reporter === "string") {
|
|
@@ -4288,12 +4429,14 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4288
4429
|
}
|
|
4289
4430
|
const resolvedReporter = options.reporter || defaultReporter;
|
|
4290
4431
|
try {
|
|
4432
|
+
const { data, baseExperiment } = callEvaluatorData(evaluator.data);
|
|
4291
4433
|
const experiment = initExperiment2(evaluator.state, {
|
|
4292
4434
|
...evaluator.projectId ? { projectId: evaluator.projectId } : { project: name },
|
|
4293
4435
|
experiment: evaluator.experimentName,
|
|
4294
4436
|
metadata: evaluator.metadata,
|
|
4295
4437
|
isPublic: evaluator.isPublic,
|
|
4296
|
-
update: evaluator.update
|
|
4438
|
+
update: evaluator.update,
|
|
4439
|
+
baseExperiment
|
|
4297
4440
|
});
|
|
4298
4441
|
if (options.onStart) {
|
|
4299
4442
|
experiment.summarize({ summarizeScores: false }).then(options.onStart);
|
|
@@ -4302,7 +4445,8 @@ async function Eval(name, evaluator, reporterOrOpts) {
|
|
|
4302
4445
|
const evalDef = {
|
|
4303
4446
|
evalName,
|
|
4304
4447
|
projectName: name,
|
|
4305
|
-
...evaluator
|
|
4448
|
+
...evaluator,
|
|
4449
|
+
data
|
|
4306
4450
|
};
|
|
4307
4451
|
const ret = await runEvaluator(experiment, evalDef, progressReporter, []);
|
|
4308
4452
|
progressReporter.stop();
|
|
@@ -4577,10 +4721,7 @@ async function runEvaluatorInternal(experiment, evaluator, progressReporter, fil
|
|
|
4577
4721
|
q.push(data);
|
|
4578
4722
|
await q.drain();
|
|
4579
4723
|
const summary = experiment ? await experiment.summarize() : buildLocalSummary(evaluator, results);
|
|
4580
|
-
return
|
|
4581
|
-
summary,
|
|
4582
|
-
results
|
|
4583
|
-
};
|
|
4724
|
+
return new EvalResultWithSummary(summary, results);
|
|
4584
4725
|
}
|
|
4585
4726
|
var error = chalk.bold.red;
|
|
4586
4727
|
var warning = chalk.hex("#FFA500");
|
|
@@ -5274,6 +5415,7 @@ export {
|
|
|
5274
5415
|
summarize,
|
|
5275
5416
|
traceable,
|
|
5276
5417
|
traced,
|
|
5418
|
+
updateSpan,
|
|
5277
5419
|
withDataset,
|
|
5278
5420
|
withExperiment,
|
|
5279
5421
|
withLogger,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "braintrust",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.147",
|
|
4
4
|
"description": "SDK for integrating Braintrust",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
72
|
"@ai-sdk/provider": "^0.0.11",
|
|
73
|
-
"@braintrust/core": "0.0.
|
|
73
|
+
"@braintrust/core": "0.0.48",
|
|
74
74
|
"@next/env": "^14.2.3",
|
|
75
75
|
"@vercel/functions": "^1.0.2",
|
|
76
76
|
"ai": "^3.2.16",
|