braintrust 0.0.113 → 0.0.115

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/logger.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference lib="dom" />
2
- import { TRANSACTION_ID_FIELD, GitMetadataSettings, RepoInfo, TransactionId, ParentExperimentIds, ParentProjectLogIds, IdField, ExperimentLogPartialArgs, ExperimentLogFullArgs, LogFeedbackFullArgs, ExperimentEvent, BackgroundLogEvent, DEFAULT_IS_LEGACY_DATASET, DatasetRecord } from "@braintrust/core";
2
+ import { TRANSACTION_ID_FIELD, GitMetadataSettings, RepoInfo, TransactionId, IdField, ExperimentLogPartialArgs, ExperimentLogFullArgs, LogFeedbackFullArgs, ExperimentEvent, BackgroundLogEvent, DEFAULT_IS_LEGACY_DATASET, DatasetRecord, SpanType, SpanParentObjectType } from "@braintrust/core";
3
3
  import { AnyModelParam, Message, PromptData, Tools, Prompt as PromptRow } from "@braintrust/core/typespecs";
4
4
  import { IsoAsyncLocalStorage } from "./isomorph";
5
5
  import { LazyValue } from "./util";
@@ -9,8 +9,10 @@ export type SetCurrentArg = {
9
9
  type StartSpanEventArgs = ExperimentLogPartialArgs & Partial<IdField>;
10
10
  export type StartSpanArgs = {
11
11
  name?: string;
12
+ type?: SpanType;
12
13
  spanAttributes?: Record<any, any>;
13
14
  startTime?: number;
15
+ parent?: string;
14
16
  parentId?: string;
15
17
  event?: StartSpanEventArgs;
16
18
  };
@@ -48,10 +50,12 @@ export interface Span {
48
50
  *
49
51
  * @param callback The function to be run under the span context.
50
52
  * @param args.name Optional name of the span. If not provided, a name will be inferred from the call stack.
53
+ * @param args.type Optional type of the span. If not provided, the type will be unset.
51
54
  * @param args.span_attributes Optional additional attributes to attach to the span, such as a type name.
52
55
  * @param args.start_time Optional start time of the span, as a timestamp in seconds.
53
56
  * @param args.setCurrent If true (the default), the span will be marked as the currently-active span for the duration of the callback.
54
- * @param args.parentId Optional id of the parent span. If not provided, the current span will be used (depending on context). This is useful for adding spans to an existing trace.
57
+ * @param args.parent Optional parent info string for the span. The string can be generated from `[Span,Experiment,Logger].export`. If not provided, the current span will be used (depending on context). This is useful for adding spans to an existing trace.
58
+ * @param parentId This option is deprecated and will be removed in a future version of Braintrust. Prefer to use `parent` instead.
55
59
  * @param args.event Data to be logged. See `Experiment.log` for full details.
56
60
  * @Returns The result of running `callback`.
57
61
  */
@@ -79,6 +83,10 @@ export interface Span {
79
83
  * Alias for `end`.
80
84
  */
81
85
  close(args?: EndSpanArgs): number;
86
+ /**
87
+ * Flush any pending rows to the server.
88
+ */
89
+ flush(): Promise<void>;
82
90
  kind: "span";
83
91
  }
84
92
  /**
@@ -89,11 +97,12 @@ export declare class NoopSpan implements Span {
89
97
  kind: "span";
90
98
  constructor();
91
99
  log(_: ExperimentLogPartialArgs): void;
92
- logFeedback(event: Omit<LogFeedbackFullArgs, "id">): void;
100
+ logFeedback(_event: Omit<LogFeedbackFullArgs, "id">): void;
93
101
  traced<R>(callback: (span: Span) => R, _1?: StartSpanArgs & SetCurrentArg): R;
94
102
  startSpan(_1?: StartSpanArgs): this;
95
103
  end(args?: EndSpanArgs): number;
96
104
  close(args?: EndSpanArgs): number;
105
+ flush(): Promise<void>;
97
106
  }
98
107
  export declare const NOOP_SPAN: NoopSpan;
99
108
  declare global {
@@ -104,7 +113,9 @@ declare class BraintrustState {
104
113
  currentExperiment: Experiment | undefined;
105
114
  currentLogger: Logger<false> | undefined;
106
115
  currentSpan: IsoAsyncLocalStorage<Span>;
116
+ private _globalBgLogger;
107
117
  appUrl: string | null;
118
+ appPublicUrl: string | null;
108
119
  loginToken: string | null;
109
120
  orgId: string | null;
110
121
  orgName: string | null;
@@ -113,10 +124,12 @@ declare class BraintrustState {
113
124
  gitMetadataSettings?: GitMetadataSettings;
114
125
  private _apiConn;
115
126
  private _logConn;
127
+ private _globalBgLoggerDirty;
116
128
  constructor();
117
129
  resetLoginInfo(): void;
118
130
  apiConn(): HTTPConnection;
119
131
  logConn(): HTTPConnection;
132
+ globalBgLogger(): BackgroundLogger;
120
133
  }
121
134
  export declare function _internalSetInitialState(): void;
122
135
  export declare const _internalGetGlobalState: () => BraintrustState;
@@ -161,10 +174,14 @@ export declare class Logger<IsAsyncFlush extends boolean> {
161
174
  private logOptions;
162
175
  private bgLogger;
163
176
  private lastStartTime;
177
+ private lazyId;
178
+ private calledStartSpan;
164
179
  kind: "logger";
165
180
  constructor(lazyMetadata: LazyValue<OrgProjectMetadata>, logOptions?: LogOptions<IsAsyncFlush>);
166
181
  get org_id(): Promise<string>;
167
182
  get project(): Promise<ObjectMetadata>;
183
+ get id(): Promise<string>;
184
+ private spanParentObjectType;
168
185
  private getState;
169
186
  /**
170
187
  * Log a single event. The event will be batched and uploaded behind the scenes if `logOptions.asyncFlush` is true.
@@ -178,11 +195,11 @@ export declare class Logger<IsAsyncFlush extends boolean> {
178
195
  * @param event.metrics: (Optional) a dictionary of metrics to log. The following keys are populated automatically: "start", "end".
179
196
  * @param event.id: (Optional) a unique identifier for the event. If you don't provide one, BrainTrust will generate one for you.
180
197
  * @param options Additional logging options
181
- * @param options.allowLogConcurrentWithActiveSpan in rare cases where you need to log at the top level separately from an active span on the logger, set this to true.
198
+ * @param options.allowConcurrentWithSpans in rare cases where you need to log at the top level separately from spans on the logger elsewhere, set this to true.
182
199
  * :returns: The `id` of the logged event.
183
200
  */
184
201
  log(event: Readonly<StartSpanEventArgs>, options?: {
185
- allowLogConcurrentWithActiveSpan?: boolean;
202
+ allowConcurrentWithSpans?: boolean;
186
203
  }): PromiseUnless<IsAsyncFlush, string>;
187
204
  /**
188
205
  * Create a new toplevel span underneath the logger. The name defaults to "root".
@@ -190,7 +207,6 @@ export declare class Logger<IsAsyncFlush extends boolean> {
190
207
  * See `Span.traced` for full details.
191
208
  */
192
209
  traced<R>(callback: (span: Span) => R, args?: StartSpanArgs & SetCurrentArg): PromiseUnless<IsAsyncFlush, R>;
193
- private lazyParentIds;
194
210
  /**
195
211
  * Lower-level alternative to `traced`. This allows you to start a span yourself, and can be useful in situations
196
212
  * where you cannot use callbacks. However, spans started with `startSpan` will not be marked as the "current span",
@@ -199,6 +215,7 @@ export declare class Logger<IsAsyncFlush extends boolean> {
199
215
  * See `traced` for full details.
200
216
  */
201
217
  startSpan(args?: StartSpanArgs): Span;
218
+ private startSpanImpl;
202
219
  /**
203
220
  * Log feedback to an event. Feedback is used to save feedback scores, set an expected value, or add a comment.
204
221
  *
@@ -211,6 +228,10 @@ export declare class Logger<IsAsyncFlush extends boolean> {
211
228
  * @param event.source (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
212
229
  */
213
230
  logFeedback(event: LogFeedbackFullArgs): void;
231
+ /**
232
+ * Return a serialized representation of the logger that can be used to start subspans in other places. See `Span.start_span` for more details.
233
+ */
234
+ export(): Promise<string>;
214
235
  flush(): Promise<void>;
215
236
  get asyncFlush(): IsAsyncFlush | undefined;
216
237
  }
@@ -466,7 +487,7 @@ export declare function getSpanParentObject<IsAsyncFlush extends boolean>(option
466
487
  * * Currently-active experiment
467
488
  * * Currently-active logger
468
489
  *
469
- * and creates a span under the first one that is active. If none of these are active, it returns a no-op span object.
490
+ * and creates a span under the first one that is active. Alternatively, if `parent` is specified, it creates a span under the specified parent row. If none of these are active, it returns a no-op span object.
470
491
  *
471
492
  * See `Span.traced` for full details.
472
493
  */
@@ -523,11 +544,14 @@ export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
523
544
  readonly dataset?: AnyDataset;
524
545
  bgLogger: BackgroundLogger;
525
546
  private lastStartTime;
547
+ private lazyId;
548
+ private calledStartSpan;
526
549
  kind: "experiment";
527
550
  constructor(lazyMetadata: LazyValue<ProjectExperimentMetadata>, dataset?: AnyDataset);
528
551
  get id(): Promise<string>;
529
552
  get name(): Promise<string>;
530
553
  get project(): Promise<ObjectMetadata>;
554
+ private spanParentObjectType;
531
555
  protected getState(): Promise<BraintrustState>;
532
556
  /**
533
557
  * Log a single event to the experiment. The event will be batched and uploaded behind the scenes.
@@ -543,11 +567,11 @@ export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
543
567
  * @param event.dataset_record_id: (Optional) the id of the dataset record that this event is associated with. This field is required if and only if the experiment is associated with a dataset.
544
568
  * @param event.inputs: (Deprecated) the same as `input` (will be removed in a future version).
545
569
  * @param options Additional logging options
546
- * @param options.allowLogConcurrentWithActiveSpan in rare cases where you need to log at the top level separately from an active span on the experiment, set this to true.
570
+ * @param options.allowConcurrentWithSpans in rare cases where you need to log at the top level separately from spans on the experiment elsewhere, set this to true.
547
571
  * :returns: The `id` of the logged event.
548
572
  */
549
573
  log(event: Readonly<ExperimentLogFullArgs>, options?: {
550
- allowLogConcurrentWithActiveSpan?: boolean;
574
+ allowConcurrentWithSpans?: boolean;
551
575
  }): string;
552
576
  /**
553
577
  * Create a new toplevel span underneath the experiment. The name defaults to "root".
@@ -555,7 +579,6 @@ export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
555
579
  * See `Span.traced` for full details.
556
580
  */
557
581
  traced<R>(callback: (span: Span) => R, args?: StartSpanArgs & SetCurrentArg): R;
558
- private lazyParentIds;
559
582
  /**
560
583
  * Lower-level alternative to `traced`. This allows you to start a span yourself, and can be useful in situations
561
584
  * where you cannot use callbacks. However, spans started with `startSpan` will not be marked as the "current span",
@@ -564,6 +587,7 @@ export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
564
587
  * See `traced` for full details.
565
588
  */
566
589
  startSpan(args?: StartSpanArgs): Span;
590
+ private startSpanImpl;
567
591
  fetchBaseExperiment(): Promise<{
568
592
  id: any;
569
593
  name: any;
@@ -592,6 +616,10 @@ export declare class Experiment extends ObjectFetcher<ExperimentEvent> {
592
616
  * @param event.source (Optional) the source of the feedback. Must be one of "external" (default), "app", or "api".
593
617
  */
594
618
  logFeedback(event: LogFeedbackFullArgs): void;
619
+ /**
620
+ * Return a serialized representation of the experiment that can be used to start subspans in other places. See `Span.start_span` for more details.
621
+ */
622
+ export(): Promise<string>;
595
623
  /**
596
624
  * Flush any pending rows to the server.
597
625
  */
@@ -612,10 +640,6 @@ export declare class ReadonlyExperiment extends ObjectFetcher<ExperimentEvent> {
612
640
  protected getState(): Promise<BraintrustState>;
613
641
  asDataset<Input, Expected>(): AsyncGenerator<EvalCase<Input, Expected, void>>;
614
642
  }
615
- type ParentSpanInfo = {
616
- span_id: string;
617
- root_span_id: string;
618
- };
619
643
  /**
620
644
  * Primary implementation of the `Span` interface. See the `Span` interface for full details on each method.
621
645
  *
@@ -626,26 +650,30 @@ export declare class SpanImpl implements Span {
626
650
  private internalData;
627
651
  private isMerge;
628
652
  private loggedEndTime;
629
- parentObject: Experiment | Logger<any>;
630
- private parentIds;
631
- private readonly rowIds;
653
+ private parentObjectType;
654
+ private parentObjectId;
655
+ private parentRowId;
656
+ private _id;
632
657
  kind: "span";
633
658
  constructor(args: {
634
- parentObject: Experiment | Logger<any>;
635
- parentIds: LazyValue<ParentExperimentIds | ParentProjectLogIds>;
659
+ parentObjectType: SpanParentObjectType;
660
+ parentObjectId: LazyValue<string>;
661
+ parentRowId: string;
636
662
  bgLogger: BackgroundLogger;
637
- } & Omit<StartSpanArgs, "parentId"> & ({
638
- parentSpanInfo?: ParentSpanInfo;
639
- } | {
640
- parentId?: string;
641
- }));
663
+ defaultRootType?: SpanType;
664
+ } & Omit<StartSpanArgs, "parent" & "parentId">);
642
665
  get id(): string;
643
666
  log(event: ExperimentLogPartialArgs): void;
644
667
  logFeedback(event: Omit<LogFeedbackFullArgs, "id">): void;
645
668
  traced<R>(callback: (span: Span) => R, args?: StartSpanArgs & SetCurrentArg): R;
646
669
  startSpan(args?: StartSpanArgs): Span;
647
670
  end(args?: EndSpanArgs): number;
671
+ /**
672
+ * Return a serialized representation of the span that can be used to start subspans in other places. See `Span.start_span` for more details.
673
+ */
674
+ export(): Promise<string>;
648
675
  close(args?: EndSpanArgs): number;
676
+ flush(): Promise<void>;
649
677
  }
650
678
  /**
651
679
  * A dataset is a collection of records, such as model inputs and expected outputs, which represent
@@ -654,7 +682,7 @@ export declare class SpanImpl implements Span {
654
682
  *
655
683
  * You should not create `Dataset` objects directly. Instead, use the `braintrust.initDataset()` method.
656
684
  */
657
- declare class Dataset<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET> extends ObjectFetcher<DatasetRecord<IsLegacyDataset>> {
685
+ export declare class Dataset<IsLegacyDataset extends boolean = typeof DEFAULT_IS_LEGACY_DATASET> extends ObjectFetcher<DatasetRecord<IsLegacyDataset>> {
658
686
  private readonly lazyMetadata;
659
687
  private bgLogger;
660
688
  constructor(lazyMetadata: LazyValue<ProjectDatasetMetadata>, pinnedVersion?: string, legacy?: IsLegacyDataset);
@@ -764,9 +792,9 @@ export type AnyDataset = Dataset<boolean>;
764
792
  export interface ScoreSummary {
765
793
  name: string;
766
794
  score: number;
767
- diff: number;
768
- improvements: number;
769
- regressions: number;
795
+ diff?: number;
796
+ improvements?: number;
797
+ regressions?: number;
770
798
  }
771
799
  /**
772
800
  * Summary of a metric's performance.
@@ -797,11 +825,11 @@ export interface MetricSummary {
797
825
  export interface ExperimentSummary {
798
826
  projectName: string;
799
827
  experimentName: string;
800
- projectUrl: string;
801
- experimentUrl: string;
802
- comparisonExperimentName: string | undefined;
803
- scores: Record<string, ScoreSummary> | undefined;
804
- metrics: Record<string, MetricSummary> | undefined;
828
+ projectUrl?: string;
829
+ experimentUrl?: string;
830
+ comparisonExperimentName?: string;
831
+ scores: Record<string, ScoreSummary>;
832
+ metrics?: Record<string, MetricSummary>;
805
833
  }
806
834
  /**
807
835
  * Summary of a dataset's data.