braintrust 0.0.94 → 0.0.95
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/dist/browser.js +160 -92
- package/dist/cli.js +196 -223
- package/dist/framework.d.ts +21 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.js +212 -98
- package/dist/logger.d.ts +50 -45
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3647,12 +3647,14 @@ var require_pluralize = __commonJS({
|
|
|
3647
3647
|
// src/index.ts
|
|
3648
3648
|
var src_exports = {};
|
|
3649
3649
|
__export(src_exports, {
|
|
3650
|
+
BaseExperiment: () => BaseExperiment,
|
|
3650
3651
|
Dataset: () => Dataset,
|
|
3651
3652
|
Eval: () => Eval,
|
|
3652
3653
|
Experiment: () => Experiment,
|
|
3653
3654
|
Logger: () => Logger,
|
|
3654
3655
|
NOOP_SPAN: () => NOOP_SPAN,
|
|
3655
3656
|
NoopSpan: () => NoopSpan,
|
|
3657
|
+
ReadonlyExperiment: () => ReadonlyExperiment,
|
|
3656
3658
|
SpanImpl: () => SpanImpl,
|
|
3657
3659
|
_internalGetGlobalState: () => _internalGetGlobalState,
|
|
3658
3660
|
_internalSetInitialState: () => _internalSetInitialState,
|
|
@@ -8522,6 +8524,7 @@ function init(project, options = {}) {
|
|
|
8522
8524
|
dataset,
|
|
8523
8525
|
baseExperiment,
|
|
8524
8526
|
isPublic,
|
|
8527
|
+
open,
|
|
8525
8528
|
update,
|
|
8526
8529
|
appUrl,
|
|
8527
8530
|
apiKey,
|
|
@@ -8529,6 +8532,41 @@ function init(project, options = {}) {
|
|
|
8529
8532
|
metadata,
|
|
8530
8533
|
gitMetadataSettings
|
|
8531
8534
|
} = options || {};
|
|
8535
|
+
if (open) {
|
|
8536
|
+
if (isEmpty(experiment)) {
|
|
8537
|
+
throw new Error("Cannot open an experiment without specifying its name");
|
|
8538
|
+
}
|
|
8539
|
+
if (update) {
|
|
8540
|
+
throw new Error("Cannot open and update an experiment at the same time");
|
|
8541
|
+
}
|
|
8542
|
+
const lazyMetadata2 = new LazyValue(async () => {
|
|
8543
|
+
await login({
|
|
8544
|
+
orgName,
|
|
8545
|
+
apiKey,
|
|
8546
|
+
appUrl
|
|
8547
|
+
});
|
|
8548
|
+
const args = {
|
|
8549
|
+
project_name: project,
|
|
8550
|
+
org_name: _state.orgName,
|
|
8551
|
+
experiment_name: experiment
|
|
8552
|
+
};
|
|
8553
|
+
const response = await _state.apiConn().post_json("api/experiment/get", args);
|
|
8554
|
+
if (response.length === 0) {
|
|
8555
|
+
throw new Error(
|
|
8556
|
+
`Experiment ${experiment} not found in project ${project}.`
|
|
8557
|
+
);
|
|
8558
|
+
}
|
|
8559
|
+
const info = response[0];
|
|
8560
|
+
return {
|
|
8561
|
+
id: info.id,
|
|
8562
|
+
name: info.name,
|
|
8563
|
+
fullInfo: info
|
|
8564
|
+
};
|
|
8565
|
+
});
|
|
8566
|
+
return new ReadonlyExperiment(
|
|
8567
|
+
lazyMetadata2
|
|
8568
|
+
);
|
|
8569
|
+
}
|
|
8532
8570
|
const lazyMetadata = new LazyValue(
|
|
8533
8571
|
async () => {
|
|
8534
8572
|
await login({
|
|
@@ -8570,7 +8608,7 @@ function init(project, options = {}) {
|
|
|
8570
8608
|
args["ancestor_commits"] = await isomorph_default.getPastNAncestors();
|
|
8571
8609
|
}
|
|
8572
8610
|
if (dataset !== void 0) {
|
|
8573
|
-
args["dataset_id"] = dataset.id;
|
|
8611
|
+
args["dataset_id"] = await dataset.id;
|
|
8574
8612
|
args["dataset_version"] = await dataset.version();
|
|
8575
8613
|
}
|
|
8576
8614
|
if (isPublic !== void 0) {
|
|
@@ -8925,15 +8963,15 @@ function validateAndSanitizeExperimentLogPartialArgs(event) {
|
|
|
8925
8963
|
}
|
|
8926
8964
|
}
|
|
8927
8965
|
function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
|
|
8928
|
-
if ("input" in event && event.input && "inputs" in event && event.inputs || !("input" in event) && !("inputs" in event)) {
|
|
8966
|
+
if ("input" in event && !isEmpty(event.input) && "inputs" in event && !isEmpty(event.inputs) || !("input" in event) && !("inputs" in event)) {
|
|
8929
8967
|
throw new Error(
|
|
8930
8968
|
"Exactly one of input or inputs (deprecated) must be specified. Prefer input."
|
|
8931
8969
|
);
|
|
8932
8970
|
}
|
|
8933
|
-
if (
|
|
8971
|
+
if (isEmpty(event.output)) {
|
|
8934
8972
|
throw new Error("output must be specified");
|
|
8935
8973
|
}
|
|
8936
|
-
if (
|
|
8974
|
+
if (isEmpty(event.scores)) {
|
|
8937
8975
|
throw new Error("scores must be specified");
|
|
8938
8976
|
}
|
|
8939
8977
|
if (hasDataset && event.datasetRecordId === void 0) {
|
|
@@ -8945,8 +8983,61 @@ function validateAndSanitizeExperimentLogFullArgs(event, hasDataset) {
|
|
|
8945
8983
|
}
|
|
8946
8984
|
return event;
|
|
8947
8985
|
}
|
|
8948
|
-
var
|
|
8986
|
+
var ObjectFetcher = class {
|
|
8987
|
+
constructor(objectType, pinnedVersion) {
|
|
8988
|
+
this.objectType = objectType;
|
|
8989
|
+
this.pinnedVersion = pinnedVersion;
|
|
8990
|
+
this._fetchedData = void 0;
|
|
8991
|
+
}
|
|
8992
|
+
get id() {
|
|
8993
|
+
throw new Error("ObjectFetcher subclasses must have an 'id' attribute");
|
|
8994
|
+
}
|
|
8995
|
+
async getState() {
|
|
8996
|
+
throw new Error("ObjectFetcher subclasses must have a 'getState' method");
|
|
8997
|
+
}
|
|
8998
|
+
async *fetch() {
|
|
8999
|
+
const records = await this.fetchedData();
|
|
9000
|
+
for (const record of records) {
|
|
9001
|
+
yield record;
|
|
9002
|
+
}
|
|
9003
|
+
}
|
|
9004
|
+
[Symbol.iterator]() {
|
|
9005
|
+
return this.fetch();
|
|
9006
|
+
}
|
|
9007
|
+
async fetchedData() {
|
|
9008
|
+
if (this._fetchedData === void 0) {
|
|
9009
|
+
const state = await this.getState();
|
|
9010
|
+
const resp = await state.logConn().get(`object/${this.objectType}`, {
|
|
9011
|
+
id: await this.id,
|
|
9012
|
+
fmt: "json2",
|
|
9013
|
+
version: this.pinnedVersion
|
|
9014
|
+
});
|
|
9015
|
+
this._fetchedData = await resp.json();
|
|
9016
|
+
}
|
|
9017
|
+
return this._fetchedData || [];
|
|
9018
|
+
}
|
|
9019
|
+
clearCache() {
|
|
9020
|
+
this._fetchedData = void 0;
|
|
9021
|
+
}
|
|
9022
|
+
async version() {
|
|
9023
|
+
if (this.pinnedVersion !== void 0) {
|
|
9024
|
+
return this.pinnedVersion;
|
|
9025
|
+
} else {
|
|
9026
|
+
const fetchedData = await this.fetchedData();
|
|
9027
|
+
let maxVersion = void 0;
|
|
9028
|
+
for (const record of fetchedData) {
|
|
9029
|
+
const xactId = record[TRANSACTION_ID_FIELD];
|
|
9030
|
+
if (maxVersion === void 0 || (xactId ?? xactId > maxVersion)) {
|
|
9031
|
+
maxVersion = xactId;
|
|
9032
|
+
}
|
|
9033
|
+
}
|
|
9034
|
+
return maxVersion;
|
|
9035
|
+
}
|
|
9036
|
+
}
|
|
9037
|
+
};
|
|
9038
|
+
var Experiment = class extends ObjectFetcher {
|
|
8949
9039
|
constructor(lazyMetadata, dataset) {
|
|
9040
|
+
super("experiment", void 0);
|
|
8950
9041
|
// For type identification.
|
|
8951
9042
|
this.kind = "experiment";
|
|
8952
9043
|
this.lazyMetadata = lazyMetadata;
|
|
@@ -9039,6 +9130,26 @@ var Experiment = class {
|
|
|
9039
9130
|
...argsRest
|
|
9040
9131
|
});
|
|
9041
9132
|
}
|
|
9133
|
+
async fetchBaseExperiment() {
|
|
9134
|
+
const state = await this.getState();
|
|
9135
|
+
const conn = state.apiConn();
|
|
9136
|
+
try {
|
|
9137
|
+
const resp = await conn.post("/api/base_experiment/get_id", {
|
|
9138
|
+
id: await this.id
|
|
9139
|
+
});
|
|
9140
|
+
const base = await resp.json();
|
|
9141
|
+
return {
|
|
9142
|
+
id: base["base_exp_id"],
|
|
9143
|
+
name: base["base_exp_name"]
|
|
9144
|
+
};
|
|
9145
|
+
} catch (e) {
|
|
9146
|
+
if (e instanceof FailedHTTPResponse && e.status === 400) {
|
|
9147
|
+
return null;
|
|
9148
|
+
} else {
|
|
9149
|
+
throw e;
|
|
9150
|
+
}
|
|
9151
|
+
}
|
|
9152
|
+
}
|
|
9042
9153
|
/**
|
|
9043
9154
|
* Summarize the experiment, including the scores (compared to the closest reference experiment) and metadata.
|
|
9044
9155
|
*
|
|
@@ -9062,14 +9173,10 @@ var Experiment = class {
|
|
|
9062
9173
|
let comparisonExperimentName = void 0;
|
|
9063
9174
|
if (summarizeScores) {
|
|
9064
9175
|
if (comparisonExperimentId === void 0) {
|
|
9065
|
-
const
|
|
9066
|
-
|
|
9067
|
-
|
|
9068
|
-
|
|
9069
|
-
const base_experiments = await resp.json();
|
|
9070
|
-
if (base_experiments.length > 0) {
|
|
9071
|
-
comparisonExperimentId = base_experiments[0]["base_exp_id"];
|
|
9072
|
-
comparisonExperimentName = base_experiments[0]["base_exp_name"];
|
|
9176
|
+
const baseExperiment = await this.fetchBaseExperiment();
|
|
9177
|
+
if (baseExperiment !== null) {
|
|
9178
|
+
comparisonExperimentId = baseExperiment.id;
|
|
9179
|
+
comparisonExperimentName = baseExperiment.name;
|
|
9073
9180
|
}
|
|
9074
9181
|
}
|
|
9075
9182
|
if (comparisonExperimentId !== void 0) {
|
|
@@ -9129,6 +9236,40 @@ var Experiment = class {
|
|
|
9129
9236
|
return this.id;
|
|
9130
9237
|
}
|
|
9131
9238
|
};
|
|
9239
|
+
var ReadonlyExperiment = class extends ObjectFetcher {
|
|
9240
|
+
constructor(lazyMetadata) {
|
|
9241
|
+
super("experiment", void 0);
|
|
9242
|
+
this.lazyMetadata = lazyMetadata;
|
|
9243
|
+
}
|
|
9244
|
+
get id() {
|
|
9245
|
+
return (async () => {
|
|
9246
|
+
return (await this.lazyMetadata.get()).id;
|
|
9247
|
+
})();
|
|
9248
|
+
}
|
|
9249
|
+
get name() {
|
|
9250
|
+
return (async () => {
|
|
9251
|
+
return (await this.lazyMetadata.get()).name;
|
|
9252
|
+
})();
|
|
9253
|
+
}
|
|
9254
|
+
async getState() {
|
|
9255
|
+
await this.lazyMetadata.get();
|
|
9256
|
+
return _state;
|
|
9257
|
+
}
|
|
9258
|
+
async *asDataset() {
|
|
9259
|
+
const records = this.fetch();
|
|
9260
|
+
for await (const record of records) {
|
|
9261
|
+
if (record.root_span_id !== record.span_id) {
|
|
9262
|
+
continue;
|
|
9263
|
+
}
|
|
9264
|
+
const { output, expected } = record;
|
|
9265
|
+
yield {
|
|
9266
|
+
input: record.input,
|
|
9267
|
+
expected: expected ?? output
|
|
9268
|
+
};
|
|
9269
|
+
}
|
|
9270
|
+
}
|
|
9271
|
+
};
|
|
9272
|
+
var executionCounter = 0;
|
|
9132
9273
|
var SpanImpl = class _SpanImpl {
|
|
9133
9274
|
// root_experiment should only be specified for a root span. parent_span
|
|
9134
9275
|
// should only be specified for non-root spans.
|
|
@@ -9154,7 +9295,11 @@ var SpanImpl = class _SpanImpl {
|
|
|
9154
9295
|
start: args.startTime ?? getCurrentUnixTimestamp()
|
|
9155
9296
|
},
|
|
9156
9297
|
context: { ...callerLocation },
|
|
9157
|
-
span_attributes: {
|
|
9298
|
+
span_attributes: {
|
|
9299
|
+
...args.spanAttributes,
|
|
9300
|
+
name,
|
|
9301
|
+
exec_counter: executionCounter++
|
|
9302
|
+
},
|
|
9158
9303
|
created: (/* @__PURE__ */ new Date()).toISOString()
|
|
9159
9304
|
};
|
|
9160
9305
|
this.parentIds = args.parentIds;
|
|
@@ -9252,11 +9397,10 @@ var SpanImpl = class _SpanImpl {
|
|
|
9252
9397
|
return this.end(args);
|
|
9253
9398
|
}
|
|
9254
9399
|
};
|
|
9255
|
-
var Dataset = class {
|
|
9400
|
+
var Dataset = class extends ObjectFetcher {
|
|
9256
9401
|
constructor(lazyMetadata, pinnedVersion) {
|
|
9257
|
-
|
|
9402
|
+
super("dataset", pinnedVersion);
|
|
9258
9403
|
this.lazyMetadata = lazyMetadata;
|
|
9259
|
-
this.pinnedVersion = pinnedVersion;
|
|
9260
9404
|
const logConn = new LazyValue(
|
|
9261
9405
|
() => this.getState().then((state) => state.logConn())
|
|
9262
9406
|
);
|
|
@@ -9364,81 +9508,6 @@ var Dataset = class {
|
|
|
9364
9508
|
dataSummary
|
|
9365
9509
|
};
|
|
9366
9510
|
}
|
|
9367
|
-
/**
|
|
9368
|
-
* Fetch all records in the dataset.
|
|
9369
|
-
*
|
|
9370
|
-
* @example
|
|
9371
|
-
* ```
|
|
9372
|
-
* // Use an async iterator to fetch all records in the dataset.
|
|
9373
|
-
* for await (const record of dataset.fetch()) {
|
|
9374
|
-
* console.log(record);
|
|
9375
|
-
* }
|
|
9376
|
-
*
|
|
9377
|
-
* // You can also iterate over the dataset directly.
|
|
9378
|
-
* for await (const record of dataset) {
|
|
9379
|
-
* console.log(record);
|
|
9380
|
-
* }
|
|
9381
|
-
* ```
|
|
9382
|
-
*
|
|
9383
|
-
* @returns An iterator over the dataset's records.
|
|
9384
|
-
*/
|
|
9385
|
-
async *fetch() {
|
|
9386
|
-
const records = await this.fetchedData();
|
|
9387
|
-
for (const record of records) {
|
|
9388
|
-
yield {
|
|
9389
|
-
id: record.id,
|
|
9390
|
-
input: record.input && JSON.parse(record.input),
|
|
9391
|
-
output: record.input && JSON.parse(record.output),
|
|
9392
|
-
metadata: record.metadata && JSON.parse(record.metadata)
|
|
9393
|
-
};
|
|
9394
|
-
}
|
|
9395
|
-
this.clearCache();
|
|
9396
|
-
}
|
|
9397
|
-
/**
|
|
9398
|
-
* Fetch all records in the dataset.
|
|
9399
|
-
*
|
|
9400
|
-
* @example
|
|
9401
|
-
* ```
|
|
9402
|
-
* // Use an async iterator to fetch all records in the dataset.
|
|
9403
|
-
* for await (const record of dataset) {
|
|
9404
|
-
* console.log(record);
|
|
9405
|
-
* }
|
|
9406
|
-
* ```
|
|
9407
|
-
*/
|
|
9408
|
-
[Symbol.asyncIterator]() {
|
|
9409
|
-
return this.fetch();
|
|
9410
|
-
}
|
|
9411
|
-
async fetchedData() {
|
|
9412
|
-
if (this._fetchedData === void 0) {
|
|
9413
|
-
const state = await this.getState();
|
|
9414
|
-
const resp = await state.logConn().get("object/dataset", {
|
|
9415
|
-
id: await this.id,
|
|
9416
|
-
fmt: "json",
|
|
9417
|
-
version: this.pinnedVersion
|
|
9418
|
-
});
|
|
9419
|
-
const text = await resp.text();
|
|
9420
|
-
this._fetchedData = text.split("\n").filter((x) => x.trim() !== "").map((x) => JSON.parse(x));
|
|
9421
|
-
}
|
|
9422
|
-
return this._fetchedData || [];
|
|
9423
|
-
}
|
|
9424
|
-
clearCache() {
|
|
9425
|
-
this._fetchedData = void 0;
|
|
9426
|
-
}
|
|
9427
|
-
async version() {
|
|
9428
|
-
if (this.pinnedVersion !== void 0) {
|
|
9429
|
-
return this.pinnedVersion;
|
|
9430
|
-
} else {
|
|
9431
|
-
const fetchedData = await this.fetchedData();
|
|
9432
|
-
let maxVersion = void 0;
|
|
9433
|
-
for (const record of fetchedData) {
|
|
9434
|
-
const xactId = record[TRANSACTION_ID_FIELD];
|
|
9435
|
-
if (maxVersion === void 0 || (xactId ?? xactId > maxVersion)) {
|
|
9436
|
-
maxVersion = xactId;
|
|
9437
|
-
}
|
|
9438
|
-
}
|
|
9439
|
-
return maxVersion;
|
|
9440
|
-
}
|
|
9441
|
-
}
|
|
9442
9511
|
/**
|
|
9443
9512
|
* Flush any pending rows to the server.
|
|
9444
9513
|
*/
|
|
@@ -9510,6 +9579,9 @@ var BarProgressReporter = class {
|
|
|
9510
9579
|
|
|
9511
9580
|
// src/framework.ts
|
|
9512
9581
|
var import_pluralize = __toESM(require_pluralize());
|
|
9582
|
+
function BaseExperiment(options = {}) {
|
|
9583
|
+
return { _type: "BaseExperiment", ...options };
|
|
9584
|
+
}
|
|
9513
9585
|
function makeEvalName(projectName, experimentName) {
|
|
9514
9586
|
let out = projectName;
|
|
9515
9587
|
if (experimentName) {
|
|
@@ -9517,6 +9589,12 @@ function makeEvalName(projectName, experimentName) {
|
|
|
9517
9589
|
}
|
|
9518
9590
|
return out;
|
|
9519
9591
|
}
|
|
9592
|
+
function initExperiment(projectName, options = {}) {
|
|
9593
|
+
return init(projectName, {
|
|
9594
|
+
...options,
|
|
9595
|
+
setCurrent: false
|
|
9596
|
+
});
|
|
9597
|
+
}
|
|
9520
9598
|
globalThis._evals = {};
|
|
9521
9599
|
async function Eval(name, evaluator) {
|
|
9522
9600
|
const evalName = makeEvalName(name, evaluator.experimentName);
|
|
@@ -9525,15 +9603,22 @@ async function Eval(name, evaluator) {
|
|
|
9525
9603
|
}
|
|
9526
9604
|
if (globalThis._lazy_load) {
|
|
9527
9605
|
_evals[evalName] = { evalName, projectName: name, ...evaluator };
|
|
9528
|
-
return
|
|
9606
|
+
return {
|
|
9607
|
+
projectName: "_lazy_load",
|
|
9608
|
+
experimentName: "_lazy_load",
|
|
9609
|
+
projectUrl: "",
|
|
9610
|
+
experimentUrl: "",
|
|
9611
|
+
comparisonExperimentName: "",
|
|
9612
|
+
scores: {},
|
|
9613
|
+
metrics: {}
|
|
9614
|
+
};
|
|
9529
9615
|
}
|
|
9530
9616
|
const progressReporter = new BarProgressReporter();
|
|
9531
9617
|
try {
|
|
9532
|
-
const experiment =
|
|
9618
|
+
const experiment = initExperiment(name, {
|
|
9533
9619
|
experiment: evaluator.experimentName,
|
|
9534
9620
|
metadata: evaluator.metadata,
|
|
9535
|
-
isPublic: evaluator.isPublic
|
|
9536
|
-
setCurrent: false
|
|
9621
|
+
isPublic: evaluator.isPublic
|
|
9537
9622
|
});
|
|
9538
9623
|
try {
|
|
9539
9624
|
const ret = await runEvaluator(
|
|
@@ -9574,10 +9659,37 @@ async function runEvaluator(experiment, evaluator, progressReporter, filters) {
|
|
|
9574
9659
|
if (typeof evaluator.data === "string") {
|
|
9575
9660
|
throw new Error("Unimplemented: string data paths");
|
|
9576
9661
|
}
|
|
9577
|
-
|
|
9578
|
-
|
|
9662
|
+
let dataResult = typeof evaluator.data === "function" ? evaluator.data() : evaluator.data;
|
|
9663
|
+
if ("_type" in dataResult) {
|
|
9664
|
+
if (dataResult._type !== "BaseExperiment") {
|
|
9665
|
+
throw new Error("Invalid _type");
|
|
9666
|
+
}
|
|
9667
|
+
if (!experiment) {
|
|
9668
|
+
throw new Error(
|
|
9669
|
+
"Cannot use BaseExperiment() without connecting to Braintrust (you most likely set --no-send-logs)"
|
|
9670
|
+
);
|
|
9671
|
+
}
|
|
9672
|
+
let name = dataResult.name;
|
|
9673
|
+
if (isEmpty(name)) {
|
|
9674
|
+
const baseExperiment = await experiment.fetchBaseExperiment();
|
|
9675
|
+
if (!baseExperiment) {
|
|
9676
|
+
throw new Error("BaseExperiment() failed to fetch base experiment");
|
|
9677
|
+
}
|
|
9678
|
+
name = baseExperiment.name;
|
|
9679
|
+
}
|
|
9680
|
+
dataResult = initExperiment(evaluator.projectName, {
|
|
9681
|
+
experiment: name,
|
|
9682
|
+
open: true
|
|
9683
|
+
}).asDataset();
|
|
9684
|
+
}
|
|
9685
|
+
let data = [];
|
|
9579
9686
|
if (dataResult instanceof Promise) {
|
|
9580
9687
|
data = await dataResult;
|
|
9688
|
+
} else if (Symbol.asyncIterator in dataResult) {
|
|
9689
|
+
data = [];
|
|
9690
|
+
for await (const d of dataResult) {
|
|
9691
|
+
data.push(d);
|
|
9692
|
+
}
|
|
9581
9693
|
} else {
|
|
9582
9694
|
data = dataResult;
|
|
9583
9695
|
}
|
|
@@ -9990,12 +10102,14 @@ var WrapperStream = class {
|
|
|
9990
10102
|
configureNode();
|
|
9991
10103
|
// Annotate the CommonJS export names for ESM import in node:
|
|
9992
10104
|
0 && (module.exports = {
|
|
10105
|
+
BaseExperiment,
|
|
9993
10106
|
Dataset,
|
|
9994
10107
|
Eval,
|
|
9995
10108
|
Experiment,
|
|
9996
10109
|
Logger,
|
|
9997
10110
|
NOOP_SPAN,
|
|
9998
10111
|
NoopSpan,
|
|
10112
|
+
ReadonlyExperiment,
|
|
9999
10113
|
SpanImpl,
|
|
10000
10114
|
_internalGetGlobalState,
|
|
10001
10115
|
_internalSetInitialState,
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference lib="dom" />
|
|
2
|
-
import { IS_MERGE_FIELD, PARENT_ID_FIELD, Source, AUDIT_SOURCE_FIELD, AUDIT_METADATA_FIELD, GitMetadataSettings } from "@braintrust/core";
|
|
2
|
+
import { TRANSACTION_ID_FIELD, IS_MERGE_FIELD, PARENT_ID_FIELD, Source, AUDIT_SOURCE_FIELD, AUDIT_METADATA_FIELD, GitMetadataSettings, TransactionId } from "@braintrust/core";
|
|
3
3
|
import { IsoAsyncLocalStorage } from "./isomorph";
|
|
4
4
|
import { LazyValue } from "./util";
|
|
5
5
|
export type Metadata = Record<string, unknown>;
|
|
@@ -311,7 +311,10 @@ declare class BackgroundLogger {
|
|
|
311
311
|
flush_once(batchSize?: number): Promise<string[]>;
|
|
312
312
|
flush(): Promise<void>;
|
|
313
313
|
}
|
|
314
|
-
|
|
314
|
+
type InitOpenOption<IsOpen extends boolean> = {
|
|
315
|
+
open?: IsOpen;
|
|
316
|
+
};
|
|
317
|
+
export type InitOptions<IsOpen extends boolean> = {
|
|
315
318
|
experiment?: string;
|
|
316
319
|
description?: string;
|
|
317
320
|
dataset?: Dataset;
|
|
@@ -324,7 +327,8 @@ export type InitOptions = {
|
|
|
324
327
|
metadata?: Metadata;
|
|
325
328
|
gitMetadataSettings?: GitMetadataSettings;
|
|
326
329
|
setCurrent?: boolean;
|
|
327
|
-
}
|
|
330
|
+
} & InitOpenOption<IsOpen>;
|
|
331
|
+
type InitializedExperiment<IsOpen extends boolean | undefined> = IsOpen extends true ? ReadonlyExperiment : Experiment;
|
|
328
332
|
/**
|
|
329
333
|
* Log in, and then initialize a new experiment in a specified project. If the project does not exist, it will be created.
|
|
330
334
|
*
|
|
@@ -348,13 +352,14 @@ export type InitOptions = {
|
|
|
348
352
|
* JSON-serializable type, but its keys must be strings.
|
|
349
353
|
* @param options.gitMetadataSettings (Optional) Settings for collecting git metadata. By default, will collect all git metadata fields allowed in org-level settings.
|
|
350
354
|
* @param setCurrent If true (the default), set the global current-experiment to the newly-created one.
|
|
355
|
+
* @param options.open If the experiment already exists, open it in read-only mode.
|
|
351
356
|
* @returns The newly created Experiment.
|
|
352
357
|
*/
|
|
353
|
-
export declare function init(project: string, options?: Readonly<InitOptions
|
|
358
|
+
export declare function init<IsOpen extends boolean = false>(project: string, options?: Readonly<InitOptions<IsOpen>>): InitializedExperiment<IsOpen>;
|
|
354
359
|
/**
|
|
355
360
|
* This function is deprecated. Use `init` instead.
|
|
356
361
|
*/
|
|
357
|
-
export declare function withExperiment<R>(project: string, callback: (experiment: Experiment) => R, options?: Readonly<InitOptions & SetCurrentArg>): R;
|
|
362
|
+
export declare function withExperiment<R>(project: string, callback: (experiment: Experiment) => R, options?: Readonly<InitOptions<false> & SetCurrentArg>): R;
|
|
358
363
|
/**
|
|
359
364
|
* This function is deprecated. Use `initLogger` instead.
|
|
360
365
|
*/
|
|
@@ -486,6 +491,27 @@ export declare function traced<IsAsyncFlush extends boolean = false, R = void>(c
|
|
|
486
491
|
* See `traced` for full details.
|
|
487
492
|
*/
|
|
488
493
|
export declare function startSpan<IsAsyncFlush extends boolean = false>(args?: StartSpanArgs & AsyncFlushArg<IsAsyncFlush>): Span;
|
|
494
|
+
export type WithTransactionId<R> = R & {
|
|
495
|
+
[TRANSACTION_ID_FIELD]: TransactionId;
|
|
496
|
+
};
|
|
497
|
+
declare class ObjectFetcher<RecordType> {
|
|
498
|
+
private objectType;
|
|
499
|
+
private pinnedVersion;
|
|
500
|
+
private _fetchedData;
|
|
501
|
+
constructor(objectType: "dataset" | "experiment", pinnedVersion: string | undefined);
|
|
502
|
+
get id(): Promise<string>;
|
|
503
|
+
protected getState(): Promise<BraintrustState>;
|
|
504
|
+
fetch(): AsyncGenerator<WithTransactionId<RecordType>>;
|
|
505
|
+
[Symbol.iterator](): AsyncGenerator<WithTransactionId<RecordType>, any, unknown>;
|
|
506
|
+
fetchedData(): Promise<WithTransactionId<RecordType>[]>;
|
|
507
|
+
clearCache(): void;
|
|
508
|
+
version(): Promise<string | bigint | undefined>;
|
|
509
|
+
}
|
|
510
|
+
export interface EvalCase<Input, Expected> {
|
|
511
|
+
input: Input;
|
|
512
|
+
expected?: Expected;
|
|
513
|
+
metadata?: Metadata;
|
|
514
|
+
}
|
|
489
515
|
/**
|
|
490
516
|
* An experiment is a collection of logged events, such as model inputs and outputs, which represent
|
|
491
517
|
* a snapshot of your application at a particular point in time. An experiment is meant to capture more
|
|
@@ -498,7 +524,7 @@ export declare function startSpan<IsAsyncFlush extends boolean = false>(args?: S
|
|
|
498
524
|
*
|
|
499
525
|
* You should not create `Experiment` objects directly. Instead, use the `braintrust.init()` method.
|
|
500
526
|
*/
|
|
501
|
-
export declare class Experiment {
|
|
527
|
+
export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
|
|
502
528
|
private readonly lazyMetadata;
|
|
503
529
|
readonly dataset?: Dataset;
|
|
504
530
|
private bgLogger;
|
|
@@ -508,7 +534,7 @@ export declare class Experiment {
|
|
|
508
534
|
get id(): Promise<string>;
|
|
509
535
|
get name(): Promise<string>;
|
|
510
536
|
get project(): Promise<ObjectMetadata>;
|
|
511
|
-
|
|
537
|
+
protected getState(): Promise<BraintrustState>;
|
|
512
538
|
/**
|
|
513
539
|
* Log a single event to the experiment. The event will be batched and uploaded behind the scenes.
|
|
514
540
|
*
|
|
@@ -540,6 +566,10 @@ export declare class Experiment {
|
|
|
540
566
|
* See `traced` for full details.
|
|
541
567
|
*/
|
|
542
568
|
startSpan(args?: StartSpanArgs): Span;
|
|
569
|
+
fetchBaseExperiment(): Promise<{
|
|
570
|
+
id: any;
|
|
571
|
+
name: any;
|
|
572
|
+
} | null>;
|
|
543
573
|
/**
|
|
544
574
|
* Summarize the experiment, including the scores (compared to the closest reference experiment) and metadata.
|
|
545
575
|
*
|
|
@@ -573,6 +603,17 @@ export declare class Experiment {
|
|
|
573
603
|
*/
|
|
574
604
|
close(): Promise<string>;
|
|
575
605
|
}
|
|
606
|
+
/**
|
|
607
|
+
* A read-only view of an experiment, initialized by passing `open: true` to `init()`.
|
|
608
|
+
*/
|
|
609
|
+
export declare class ReadonlyExperiment extends ObjectFetcher<ExperimentEvent> {
|
|
610
|
+
private readonly lazyMetadata;
|
|
611
|
+
constructor(lazyMetadata: LazyValue<ObjectMetadata>);
|
|
612
|
+
get id(): Promise<string>;
|
|
613
|
+
get name(): Promise<string>;
|
|
614
|
+
protected getState(): Promise<BraintrustState>;
|
|
615
|
+
asDataset<Input = unknown, Expected = unknown>(): AsyncGenerator<EvalCase<Input, Expected>>;
|
|
616
|
+
}
|
|
576
617
|
interface ParentExperimentIds {
|
|
577
618
|
kind: "experiment";
|
|
578
619
|
project_id: string;
|
|
@@ -625,16 +666,14 @@ export declare class SpanImpl implements Span {
|
|
|
625
666
|
*
|
|
626
667
|
* You should not create `Dataset` objects directly. Instead, use the `braintrust.initDataset()` method.
|
|
627
668
|
*/
|
|
628
|
-
export declare class Dataset {
|
|
669
|
+
export declare class Dataset extends ObjectFetcher<DatasetRecord> {
|
|
629
670
|
private readonly lazyMetadata;
|
|
630
|
-
private pinnedVersion?;
|
|
631
|
-
private _fetchedData?;
|
|
632
671
|
private bgLogger;
|
|
633
672
|
constructor(lazyMetadata: LazyValue<ProjectDatasetMetadata>, pinnedVersion?: string);
|
|
634
673
|
get id(): Promise<string>;
|
|
635
674
|
get name(): Promise<string>;
|
|
636
675
|
get project(): Promise<ObjectMetadata>;
|
|
637
|
-
|
|
676
|
+
protected getState(): Promise<BraintrustState>;
|
|
638
677
|
/**
|
|
639
678
|
* Insert a single record to the dataset. The record will be batched and uploaded behind the scenes. If you pass in an `id`,
|
|
640
679
|
* and a record with that `id` already exists, it will be overwritten (upsert).
|
|
@@ -665,40 +704,6 @@ export declare class Dataset {
|
|
|
665
704
|
summarize(options?: {
|
|
666
705
|
readonly summarizeData?: boolean;
|
|
667
706
|
}): Promise<DatasetSummary>;
|
|
668
|
-
/**
|
|
669
|
-
* Fetch all records in the dataset.
|
|
670
|
-
*
|
|
671
|
-
* @example
|
|
672
|
-
* ```
|
|
673
|
-
* // Use an async iterator to fetch all records in the dataset.
|
|
674
|
-
* for await (const record of dataset.fetch()) {
|
|
675
|
-
* console.log(record);
|
|
676
|
-
* }
|
|
677
|
-
*
|
|
678
|
-
* // You can also iterate over the dataset directly.
|
|
679
|
-
* for await (const record of dataset) {
|
|
680
|
-
* console.log(record);
|
|
681
|
-
* }
|
|
682
|
-
* ```
|
|
683
|
-
*
|
|
684
|
-
* @returns An iterator over the dataset's records.
|
|
685
|
-
*/
|
|
686
|
-
fetch(): AsyncGenerator<DatasetRecord>;
|
|
687
|
-
/**
|
|
688
|
-
* Fetch all records in the dataset.
|
|
689
|
-
*
|
|
690
|
-
* @example
|
|
691
|
-
* ```
|
|
692
|
-
* // Use an async iterator to fetch all records in the dataset.
|
|
693
|
-
* for await (const record of dataset) {
|
|
694
|
-
* console.log(record);
|
|
695
|
-
* }
|
|
696
|
-
* ```
|
|
697
|
-
*/
|
|
698
|
-
[Symbol.asyncIterator](): AsyncGenerator<DatasetRecord, any, unknown>;
|
|
699
|
-
fetchedData(): Promise<any[]>;
|
|
700
|
-
clearCache(): void;
|
|
701
|
-
version(): Promise<any>;
|
|
702
707
|
/**
|
|
703
708
|
* Flush any pending rows to the server.
|
|
704
709
|
*/
|