braintrust 0.0.124 → 0.0.126

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 CHANGED
@@ -6449,7 +6449,7 @@ var projectLogsEventSchema = z.strictObject({
6449
6449
  project_id: projectSchema.shape.id,
6450
6450
  log_id: z.literal("g").describe("A literal 'g' which identifies the log as a project log"),
6451
6451
  input: projectLogsEventBaseSchema.shape.input.describe(
6452
- "The arguments that uniquely define a user input(an arbitrary, JSON serializable object)."
6452
+ "The arguments that uniquely define a user input (an arbitrary, JSON serializable object)."
6453
6453
  ),
6454
6454
  output: projectLogsEventBaseSchema.shape.output.describe(
6455
6455
  "The output of your application, including post-processing (an arbitrary, JSON serializable object), that allows you to determine whether the result is correct or not. For example, in an app that generates SQL queries, the `output` should be the _result_ of the SQL query generated by the model, not the query itself, because there may be multiple valid queries that answer a single question."
@@ -6729,7 +6729,7 @@ var summarizeExperimentResponseSchema = z.strictObject({
6729
6729
  z.strictObject({
6730
6730
  name: z.string().describe("Name of the score"),
6731
6731
  score: z.number().min(0).max(1).describe("Average score across all examples"),
6732
- diff: z.number().min(-1).max(1).describe(
6732
+ diff: z.number().min(-1).max(1).optional().describe(
6733
6733
  "Difference in score between the current and comparison experiment"
6734
6734
  ),
6735
6735
  improvements: z.number().int().min(0).describe("Number of improvements in the score"),
@@ -6741,7 +6741,7 @@ var summarizeExperimentResponseSchema = z.strictObject({
6741
6741
  name: z.string().describe("Name of the metric"),
6742
6742
  metric: z.number().describe("Average metric across all examples"),
6743
6743
  unit: z.string().describe("Unit label for the metric"),
6744
- diff: z.number().describe(
6744
+ diff: z.number().optional().describe(
6745
6745
  "Difference in metric between the current and comparison experiment"
6746
6746
  ),
6747
6747
  improvements: z.number().int().min(0).describe("Number of improvements in the metric"),
@@ -7477,7 +7477,7 @@ var HTTPConnection = class _HTTPConnection {
7477
7477
  } catch (e) {
7478
7478
  if (i < tries - 1) {
7479
7479
  console.log(
7480
- `Retrying API request ${object_type} ${args} ${e.status} ${e.text}`
7480
+ `Retrying API request ${object_type} ${JSON.stringify(args)} ${e.status} ${e.text}`
7481
7481
  );
7482
7482
  continue;
7483
7483
  }
@@ -7770,7 +7770,8 @@ var BackgroundLogger = class {
7770
7770
  // 6 MB for the AWS lambda gateway (from our own testing).
7771
7771
  maxRequestSize = 6 * 1024 * 1024;
7772
7772
  defaultBatchSize = 100;
7773
- numRetries = 3;
7773
+ numTries = 3;
7774
+ failedPublishPayloadsDir = void 0;
7774
7775
  constructor(logConn) {
7775
7776
  this.logConn = logConn;
7776
7777
  const syncFlushEnv = Number(isomorph_default.getEnv("BRAINTRUST_SYNC_FLUSH"));
@@ -7787,9 +7788,15 @@ var BackgroundLogger = class {
7787
7788
  if (!isNaN(maxRequestSizeEnv)) {
7788
7789
  this.maxRequestSize = maxRequestSizeEnv;
7789
7790
  }
7790
- const numRetriesEnv = Number(isomorph_default.getEnv("BRAINTRUST_NUM_RETRIES"));
7791
- if (!isNaN(numRetriesEnv)) {
7792
- this.numRetries = numRetriesEnv;
7791
+ const numTriesEnv = Number(isomorph_default.getEnv("BRAINTRUST_NUM_RETRIES"));
7792
+ if (!isNaN(numTriesEnv)) {
7793
+ this.numTries = numTriesEnv + 1;
7794
+ }
7795
+ const failedPublishPayloadsDirEnv = isomorph_default.getEnv(
7796
+ "BRAINTRUST_FAILED_PUBLISH_PAYLOADS_DIR"
7797
+ );
7798
+ if (failedPublishPayloadsDirEnv) {
7799
+ this.failedPublishPayloadsDir = failedPublishPayloadsDirEnv;
7793
7800
  }
7794
7801
  isomorph_default.processOn("beforeExit", async () => {
7795
7802
  await this.flush();
@@ -7853,13 +7860,13 @@ var BackgroundLogger = class {
7853
7860
  }
7854
7861
  }
7855
7862
  async unwrapLazyValues(wrappedItems) {
7856
- for (let i = 0; i < this.numRetries; ++i) {
7863
+ for (let i = 0; i < this.numTries; ++i) {
7857
7864
  try {
7858
7865
  const itemPromises = wrappedItems.map((x) => x.get());
7859
7866
  return mergeRowBatch(await Promise.all(itemPromises));
7860
7867
  } catch (e) {
7861
7868
  let errmsg = "Encountered error when constructing records to flush";
7862
- const isRetrying = i + 1 < this.numRetries;
7869
+ const isRetrying = i + 1 < this.numTries;
7863
7870
  if (isRetrying) {
7864
7871
  errmsg += ". Retrying";
7865
7872
  }
@@ -7873,14 +7880,14 @@ var BackgroundLogger = class {
7873
7880
  }
7874
7881
  }
7875
7882
  console.warn(
7876
- `Failed to construct log records to flush after ${this.numRetries} retries. Dropping batch`
7883
+ `Failed to construct log records to flush after ${this.numTries} attempts. Dropping batch`
7877
7884
  );
7878
7885
  return [];
7879
7886
  }
7880
7887
  async submitLogsRequest(items) {
7881
7888
  const conn = await this.logConn.get();
7882
7889
  const dataStr = constructLogs3Data(items);
7883
- for (let i = 0; i < this.numRetries; i++) {
7890
+ for (let i = 0; i < this.numTries; i++) {
7884
7891
  const startTime = now();
7885
7892
  let error = void 0;
7886
7893
  try {
@@ -7900,7 +7907,7 @@ var BackgroundLogger = class {
7900
7907
  if (error === void 0) {
7901
7908
  return;
7902
7909
  }
7903
- const isRetrying = i + 1 < this.numRetries;
7910
+ const isRetrying = i + 1 < this.numTries;
7904
7911
  const retryingText = isRetrying ? "" : " Retrying";
7905
7912
  const errorText = (() => {
7906
7913
  if (error instanceof FailedHTTPResponse) {
@@ -7911,6 +7918,22 @@ var BackgroundLogger = class {
7911
7918
  })();
7912
7919
  const errMsg = `log request failed. Elapsed time: ${(now() - startTime) / 1e3} seconds. Payload size: ${dataStr.length}.${retryingText}
7913
7920
  Error: ${errorText}`;
7921
+ if (!isRetrying && this.failedPublishPayloadsDir && isomorph_default.pathJoin && isomorph_default.mkdir && isomorph_default.writeFile) {
7922
+ const payloadFile = isomorph_default.pathJoin(
7923
+ this.failedPublishPayloadsDir,
7924
+ `payload_${getCurrentUnixTimestamp()}_${v4_default().slice(0, 8)}.json`
7925
+ );
7926
+ try {
7927
+ await isomorph_default.mkdir(this.failedPublishPayloadsDir, { recursive: true });
7928
+ await isomorph_default.writeFile(payloadFile, dataStr);
7929
+ } catch (e) {
7930
+ console.error(
7931
+ `Failed to write failed payload to output file ${payloadFile}:
7932
+ `,
7933
+ e
7934
+ );
7935
+ }
7936
+ }
7914
7937
  if (!isRetrying && this.syncFlush) {
7915
7938
  throw new Error(errMsg);
7916
7939
  } else {
@@ -7921,7 +7944,7 @@ Error: ${errorText}`;
7921
7944
  }
7922
7945
  }
7923
7946
  console.warn(
7924
- `log request failed after ${this.numRetries} retries. Dropping batch`
7947
+ `log request failed after ${this.numTries} retries. Dropping batch`
7925
7948
  );
7926
7949
  return;
7927
7950
  }
@@ -8353,7 +8376,7 @@ async function login(options = {}) {
8353
8376
  conn.set_token(apiKey);
8354
8377
  } else {
8355
8378
  throw new Error(
8356
- "Please specify an api key. Token based login is not yet implemented in the JS client."
8379
+ "Please specify an api key (e.g. by setting BRAINTRUST_API_KEY)."
8357
8380
  );
8358
8381
  }
8359
8382
  if (!conn) {
@@ -8787,18 +8810,16 @@ var Experiment = class extends ObjectFetcher {
8787
8810
  comparisonExperimentName = baseExperiment.name;
8788
8811
  }
8789
8812
  }
8790
- if (comparisonExperimentId !== void 0) {
8791
- const results = await state.logConn().get_json(
8792
- "/experiment-comparison2",
8793
- {
8794
- experiment_id: await this.id,
8795
- base_experiment_id: comparisonExperimentId
8796
- },
8797
- 3
8798
- );
8799
- scores = results["scores"];
8800
- metrics = results["metrics"];
8801
- }
8813
+ const results = await state.logConn().get_json(
8814
+ "/experiment-comparison2",
8815
+ {
8816
+ experiment_id: await this.id,
8817
+ base_experiment_id: comparisonExperimentId
8818
+ },
8819
+ 3
8820
+ );
8821
+ scores = results["scores"];
8822
+ metrics = results["metrics"];
8802
8823
  }
8803
8824
  return {
8804
8825
  projectName: (await this.project).name,