/
experiment.go
1349 lines (1237 loc) · 68 KB
/
experiment.go
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// File generated from our OpenAPI spec by Stainless.
package braintrust
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/braintrustdata/braintrust-go/internal/apijson"
"github.com/braintrustdata/braintrust-go/internal/apiquery"
"github.com/braintrustdata/braintrust-go/internal/param"
"github.com/braintrustdata/braintrust-go/internal/requestconfig"
"github.com/braintrustdata/braintrust-go/internal/shared"
"github.com/braintrustdata/braintrust-go/option"
)
// ExperimentService contains methods and other services that help with interacting
// with the braintrust API. Note, unlike clients, this service does not read
// variables from the environment automatically. You should not instantiate this
// service directly, and instead use the [NewExperimentService] method instead.
type ExperimentService struct {
Options []option.RequestOption
}
// NewExperimentService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewExperimentService(opts ...option.RequestOption) (r *ExperimentService) {
r = &ExperimentService{}
r.Options = opts
return
}
// Create a new experiment. If there is an existing experiment in the project with
// the same name as the one specified in the request, will create a new experiment
// from `name`, suffixed with a unique identifier
func (r *ExperimentService) New(ctx context.Context, body ExperimentNewParams, opts ...option.RequestOption) (res *Experiment, err error) {
opts = append(r.Options[:], opts...)
path := "v1/experiment"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Get an experiment object by its id
func (r *ExperimentService) Get(ctx context.Context, experimentID string, opts ...option.RequestOption) (res *Experiment, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Partially update an experiment object. Specify the fields to update in the
// payload. Any object-type fields will be deep-merged with existing content.
// Currently we do not support removing fields or setting them to null. As a
// workaround, you may retrieve the full object with `GET /experiment/{id}` and
// then replace it with `PUT /experiment`.
func (r *ExperimentService) Update(ctx context.Context, experimentID string, body ExperimentUpdateParams, opts ...option.RequestOption) (res *Experiment, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, body, &res, opts...)
return
}
// List out all experiments. The experiments are sorted by creation date, with the
// most recently-created experiments coming first
func (r *ExperimentService) List(ctx context.Context, query ExperimentListParams, opts ...option.RequestOption) (res *shared.ListObjects[Experiment], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "v1/experiment"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// List out all experiments. The experiments are sorted by creation date, with the
// most recently-created experiments coming first
func (r *ExperimentService) ListAutoPaging(ctx context.Context, query ExperimentListParams, opts ...option.RequestOption) *shared.ListObjectsAutoPager[Experiment] {
return shared.NewListObjectsAutoPager(r.List(ctx, query, opts...))
}
// Delete an experiment object by its id
func (r *ExperimentService) Delete(ctx context.Context, experimentID string, opts ...option.RequestOption) (res *Experiment, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return
}
// Log feedback for a set of experiment events
func (r *ExperimentService) Feedback(ctx context.Context, experimentID string, body ExperimentFeedbackParams, opts ...option.RequestOption) (err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("Accept", "")}, opts...)
path := fmt.Sprintf("v1/experiment/%s/feedback", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, nil, opts...)
return
}
// Fetch the events in an experiment. Equivalent to the POST form of the same path,
// but with the parameters in the URL query rather than in the request body
func (r *ExperimentService) Fetch(ctx context.Context, experimentID string, query ExperimentFetchParams, opts ...option.RequestOption) (res *ExperimentFetchResponse, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s/fetch", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return
}
// Fetch the events in an experiment. Equivalent to the GET form of the same path,
// but with the parameters in the request body rather than in the URL query
func (r *ExperimentService) FetchPost(ctx context.Context, experimentID string, body ExperimentFetchPostParams, opts ...option.RequestOption) (res *ExperimentFetchPostResponse, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s/fetch", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Insert a set of events into the experiment
func (r *ExperimentService) Insert(ctx context.Context, experimentID string, body ExperimentInsertParams, opts ...option.RequestOption) (res *ExperimentInsertResponse, err error) {
opts = append(r.Options[:], opts...)
path := fmt.Sprintf("v1/experiment/%s/insert", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Create or replace a new experiment. If there is an existing experiment in the
// project with the same name as the one specified in the request, will replace the
// existing experiment with the provided fields
func (r *ExperimentService) Replace(ctx context.Context, body ExperimentReplaceParams, opts ...option.RequestOption) (res *Experiment, err error) {
opts = append(r.Options[:], opts...)
path := "v1/experiment"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &res, opts...)
return
}
type Experiment struct {
// Unique identifier for the experiment
ID string `json:"id,required" format:"uuid"`
// Name of the experiment. Within a project, experiment names are unique
Name string `json:"name,required"`
// Unique identifier for the project that the experiment belongs under
ProjectID string `json:"project_id,required" format:"uuid"`
// Whether or not the experiment is public. Public experiments can be viewed by
// anybody inside or outside the organization
Public bool `json:"public,required"`
// Id of default base experiment to compare against when viewing this experiment
BaseExpID string `json:"base_exp_id,nullable" format:"uuid"`
// Commit, taken directly from `repo_info.commit`
Commit string `json:"commit,nullable"`
// Date of experiment creation
Created time.Time `json:"created,nullable" format:"date-time"`
// Identifier of the linked dataset, or null if the experiment is not linked to a
// dataset
DatasetID string `json:"dataset_id,nullable" format:"uuid"`
// Version number of the linked dataset the experiment was run against. This can be
// used to reproduce the experiment after the dataset has been modified.
DatasetVersion string `json:"dataset_version,nullable"`
// Date of experiment deletion, or null if the experiment is still active
DeletedAt time.Time `json:"deleted_at,nullable" format:"date-time"`
// Textual description of the experiment
Description string `json:"description,nullable"`
// User-controlled metadata about the experiment
Metadata map[string]interface{} `json:"metadata,nullable"`
// Metadata about the state of the repo when the experiment was created
RepoInfo ExperimentRepoInfo `json:"repo_info,nullable"`
// Identifies the user who created the experiment
UserID string `json:"user_id,nullable" format:"uuid"`
JSON experimentJSON `json:"-"`
}
// experimentJSON contains the JSON metadata for the struct [Experiment]
type experimentJSON struct {
ID apijson.Field
Name apijson.Field
ProjectID apijson.Field
Public apijson.Field
BaseExpID apijson.Field
Commit apijson.Field
Created apijson.Field
DatasetID apijson.Field
DatasetVersion apijson.Field
DeletedAt apijson.Field
Description apijson.Field
Metadata apijson.Field
RepoInfo apijson.Field
UserID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Experiment) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Metadata about the state of the repo when the experiment was created
type ExperimentRepoInfo struct {
// Email of the author of the most recent commit
AuthorEmail string `json:"author_email,nullable"`
// Name of the author of the most recent commit
AuthorName string `json:"author_name,nullable"`
// Name of the branch the most recent commit belongs to
Branch string `json:"branch,nullable"`
// SHA of most recent commit
Commit string `json:"commit,nullable"`
// Most recent commit message
CommitMessage string `json:"commit_message,nullable"`
// Time of the most recent commit
CommitTime string `json:"commit_time,nullable"`
// Whether or not the repo had uncommitted changes when snapshotted
Dirty bool `json:"dirty,nullable"`
// If the repo was dirty when run, this includes the diff between the current state
// of the repo and the most recent commit.
GitDiff string `json:"git_diff,nullable"`
// Name of the tag on the most recent commit
Tag string `json:"tag,nullable"`
JSON experimentRepoInfoJSON `json:"-"`
}
// experimentRepoInfoJSON contains the JSON metadata for the struct
// [ExperimentRepoInfo]
type experimentRepoInfoJSON struct {
AuthorEmail apijson.Field
AuthorName apijson.Field
Branch apijson.Field
Commit apijson.Field
CommitMessage apijson.Field
CommitTime apijson.Field
Dirty apijson.Field
GitDiff apijson.Field
Tag apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentRepoInfo) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
type ExperimentFetchResponse struct {
// A list of fetched events
Events []ExperimentFetchResponseEvent `json:"events,required"`
JSON experimentFetchResponseJSON `json:"-"`
}
// experimentFetchResponseJSON contains the JSON metadata for the struct
// [ExperimentFetchResponse]
type experimentFetchResponseJSON struct {
Events apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
type ExperimentFetchResponseEvent struct {
// A unique identifier for the experiment event. If you don't provide one,
// BrainTrust will generate one for you
ID string `json:"id,required"`
// The transaction id of an event is unique to the network operation that processed
// the event insertion. Transaction ids are monotonically increasing over time and
// can be used to retrieve a versioned snapshot of the experiment (see the
// `version` parameter)
XactID int64 `json:"_xact_id,required"`
// Unique identifier for the experiment
ExperimentID string `json:"experiment_id,required" format:"uuid"`
// Unique identifier for the project that the experiment belongs under
ProjectID string `json:"project_id,required" format:"uuid"`
// The `span_id` of the root of the trace this experiment event belongs to
RootSpanID string `json:"root_span_id,required"`
// A unique identifier used to link different experiment events together as part of
// a full trace. See the
// [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full
// details on tracing
SpanID string `json:"span_id,required"`
// Context is additional information about the code that produced the experiment
// event. It is essentially the textual counterpart to `metrics`. Use the
// `caller_*` attributes to track the location in code which produced the
// experiment event
Context ExperimentFetchResponseEventsContext `json:"context,nullable"`
// The timestamp the experiment event was created
Created time.Time `json:"created,nullable" format:"date-time"`
// If the experiment is associated to a dataset, this is the event-level dataset id
// this experiment event is tied to
DatasetRecordID string `json:"dataset_record_id,nullable"`
// The ground truth value (an arbitrary, JSON serializable object) that you'd
// compare to `output` to determine if your `output` value is correct or not.
// Braintrust currently does not compare `output` to `expected` for you, since
// there are so many different ways to do that correctly. Instead, these values are
// just used to help you navigate your experiments while digging into analyses.
// However, we may later use these values to re-score outputs or fine-tune your
// models
Expected interface{} `json:"expected"`
// The arguments that uniquely define a test case (an arbitrary, JSON serializable
// object). Later on, Braintrust will use the `input` to know whether two test
// cases are the same between experiments, so they should not contain
// experiment-specific state. A simple rule of thumb is that if you run the same
// experiment twice, the `input` should be identical
Input interface{} `json:"input"`
// A dictionary with additional data about the test example, model outputs, or just
// about anything else that's relevant, that you can use to help find and analyze
// examples later. For example, you could log the `prompt`, example's `id`, or
// anything else that would be useful to slice/dice later. The values in `metadata`
// can be any JSON-serializable type, but its keys must be strings
Metadata map[string]interface{} `json:"metadata,nullable"`
// Metrics are numerical measurements tracking the execution of the code that
// produced the experiment event. Use "start" and "end" to track the time span over
// which the experiment event was produced
Metrics ExperimentFetchResponseEventsMetrics `json:"metrics,nullable"`
// 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
Output interface{} `json:"output"`
// A dictionary of numeric values (between 0 and 1) to log. The scores should give
// you a variety of signals that help you determine how accurate the outputs are
// compared to what you expect and diagnose failures. For example, a summarization
// app might have one score that tells you how accurate the summary is, and another
// that measures the word similarity between the generated and grouth truth
// summary. The word similarity score could help you determine whether the
// summarization was covering similar concepts or not. You can use these scores to
// help you sort, filter, and compare experiments
Scores map[string]float64 `json:"scores,nullable"`
// Human-identifying attributes of the span, such as name, type, etc.
SpanAttributes ExperimentFetchResponseEventsSpanAttributes `json:"span_attributes,nullable"`
// An array of the parent `span_ids` of this experiment event. This should be empty
// for the root span of a trace, and should most often contain just one parent
// element for subspans
SpanParents []string `json:"span_parents,nullable"`
JSON experimentFetchResponseEventJSON `json:"-"`
}
// experimentFetchResponseEventJSON contains the JSON metadata for the struct
// [ExperimentFetchResponseEvent]
type experimentFetchResponseEventJSON struct {
ID apijson.Field
XactID apijson.Field
ExperimentID apijson.Field
ProjectID apijson.Field
RootSpanID apijson.Field
SpanID apijson.Field
Context apijson.Field
Created apijson.Field
DatasetRecordID apijson.Field
Expected apijson.Field
Input apijson.Field
Metadata apijson.Field
Metrics apijson.Field
Output apijson.Field
Scores apijson.Field
SpanAttributes apijson.Field
SpanParents apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchResponseEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Context is additional information about the code that produced the experiment
// event. It is essentially the textual counterpart to `metrics`. Use the
// `caller_*` attributes to track the location in code which produced the
// experiment event
type ExperimentFetchResponseEventsContext struct {
// Name of the file in code where the experiment event was created
CallerFilename string `json:"caller_filename,nullable"`
// The function in code which created the experiment event
CallerFunctionname string `json:"caller_functionname,nullable"`
// Line of code where the experiment event was created
CallerLineno int64 `json:"caller_lineno,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchResponseEventsContextJSON `json:"-"`
}
// experimentFetchResponseEventsContextJSON contains the JSON metadata for the
// struct [ExperimentFetchResponseEventsContext]
type experimentFetchResponseEventsContextJSON struct {
CallerFilename apijson.Field
CallerFunctionname apijson.Field
CallerLineno apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchResponseEventsContext) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Metrics are numerical measurements tracking the execution of the code that
// produced the experiment event. Use "start" and "end" to track the time span over
// which the experiment event was produced
type ExperimentFetchResponseEventsMetrics struct {
// A unix timestamp recording when the section of code which produced the
// experiment event finished
End float64 `json:"end,nullable"`
// A unix timestamp recording when the section of code which produced the
// experiment event started
Start float64 `json:"start,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchResponseEventsMetricsJSON `json:"-"`
}
// experimentFetchResponseEventsMetricsJSON contains the JSON metadata for the
// struct [ExperimentFetchResponseEventsMetrics]
type experimentFetchResponseEventsMetricsJSON struct {
End apijson.Field
Start apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchResponseEventsMetrics) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Human-identifying attributes of the span, such as name, type, etc.
type ExperimentFetchResponseEventsSpanAttributes struct {
// Name of the span, for display purposes only
Name string `json:"name,nullable"`
// Type of the span, for display purposes only
Type ExperimentFetchResponseEventsSpanAttributesType `json:"type,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchResponseEventsSpanAttributesJSON `json:"-"`
}
// experimentFetchResponseEventsSpanAttributesJSON contains the JSON metadata for
// the struct [ExperimentFetchResponseEventsSpanAttributes]
type experimentFetchResponseEventsSpanAttributesJSON struct {
Name apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchResponseEventsSpanAttributes) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Type of the span, for display purposes only
type ExperimentFetchResponseEventsSpanAttributesType string
const (
ExperimentFetchResponseEventsSpanAttributesTypeLlm ExperimentFetchResponseEventsSpanAttributesType = "llm"
ExperimentFetchResponseEventsSpanAttributesTypeScore ExperimentFetchResponseEventsSpanAttributesType = "score"
ExperimentFetchResponseEventsSpanAttributesTypeFunction ExperimentFetchResponseEventsSpanAttributesType = "function"
ExperimentFetchResponseEventsSpanAttributesTypeEval ExperimentFetchResponseEventsSpanAttributesType = "eval"
ExperimentFetchResponseEventsSpanAttributesTypeTask ExperimentFetchResponseEventsSpanAttributesType = "task"
ExperimentFetchResponseEventsSpanAttributesTypeTool ExperimentFetchResponseEventsSpanAttributesType = "tool"
)
type ExperimentFetchPostResponse struct {
// A list of fetched events
Events []ExperimentFetchPostResponseEvent `json:"events,required"`
JSON experimentFetchPostResponseJSON `json:"-"`
}
// experimentFetchPostResponseJSON contains the JSON metadata for the struct
// [ExperimentFetchPostResponse]
type experimentFetchPostResponseJSON struct {
Events apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchPostResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
type ExperimentFetchPostResponseEvent struct {
// A unique identifier for the experiment event. If you don't provide one,
// BrainTrust will generate one for you
ID string `json:"id,required"`
// The transaction id of an event is unique to the network operation that processed
// the event insertion. Transaction ids are monotonically increasing over time and
// can be used to retrieve a versioned snapshot of the experiment (see the
// `version` parameter)
XactID int64 `json:"_xact_id,required"`
// Unique identifier for the experiment
ExperimentID string `json:"experiment_id,required" format:"uuid"`
// Unique identifier for the project that the experiment belongs under
ProjectID string `json:"project_id,required" format:"uuid"`
// The `span_id` of the root of the trace this experiment event belongs to
RootSpanID string `json:"root_span_id,required"`
// A unique identifier used to link different experiment events together as part of
// a full trace. See the
// [tracing guide](https://www.braintrustdata.com/docs/guides/tracing) for full
// details on tracing
SpanID string `json:"span_id,required"`
// Context is additional information about the code that produced the experiment
// event. It is essentially the textual counterpart to `metrics`. Use the
// `caller_*` attributes to track the location in code which produced the
// experiment event
Context ExperimentFetchPostResponseEventsContext `json:"context,nullable"`
// The timestamp the experiment event was created
Created time.Time `json:"created,nullable" format:"date-time"`
// If the experiment is associated to a dataset, this is the event-level dataset id
// this experiment event is tied to
DatasetRecordID string `json:"dataset_record_id,nullable"`
// The ground truth value (an arbitrary, JSON serializable object) that you'd
// compare to `output` to determine if your `output` value is correct or not.
// Braintrust currently does not compare `output` to `expected` for you, since
// there are so many different ways to do that correctly. Instead, these values are
// just used to help you navigate your experiments while digging into analyses.
// However, we may later use these values to re-score outputs or fine-tune your
// models
Expected interface{} `json:"expected"`
// The arguments that uniquely define a test case (an arbitrary, JSON serializable
// object). Later on, Braintrust will use the `input` to know whether two test
// cases are the same between experiments, so they should not contain
// experiment-specific state. A simple rule of thumb is that if you run the same
// experiment twice, the `input` should be identical
Input interface{} `json:"input"`
// A dictionary with additional data about the test example, model outputs, or just
// about anything else that's relevant, that you can use to help find and analyze
// examples later. For example, you could log the `prompt`, example's `id`, or
// anything else that would be useful to slice/dice later. The values in `metadata`
// can be any JSON-serializable type, but its keys must be strings
Metadata map[string]interface{} `json:"metadata,nullable"`
// Metrics are numerical measurements tracking the execution of the code that
// produced the experiment event. Use "start" and "end" to track the time span over
// which the experiment event was produced
Metrics ExperimentFetchPostResponseEventsMetrics `json:"metrics,nullable"`
// 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
Output interface{} `json:"output"`
// A dictionary of numeric values (between 0 and 1) to log. The scores should give
// you a variety of signals that help you determine how accurate the outputs are
// compared to what you expect and diagnose failures. For example, a summarization
// app might have one score that tells you how accurate the summary is, and another
// that measures the word similarity between the generated and grouth truth
// summary. The word similarity score could help you determine whether the
// summarization was covering similar concepts or not. You can use these scores to
// help you sort, filter, and compare experiments
Scores map[string]float64 `json:"scores,nullable"`
// Human-identifying attributes of the span, such as name, type, etc.
SpanAttributes ExperimentFetchPostResponseEventsSpanAttributes `json:"span_attributes,nullable"`
// An array of the parent `span_ids` of this experiment event. This should be empty
// for the root span of a trace, and should most often contain just one parent
// element for subspans
SpanParents []string `json:"span_parents,nullable"`
JSON experimentFetchPostResponseEventJSON `json:"-"`
}
// experimentFetchPostResponseEventJSON contains the JSON metadata for the struct
// [ExperimentFetchPostResponseEvent]
type experimentFetchPostResponseEventJSON struct {
ID apijson.Field
XactID apijson.Field
ExperimentID apijson.Field
ProjectID apijson.Field
RootSpanID apijson.Field
SpanID apijson.Field
Context apijson.Field
Created apijson.Field
DatasetRecordID apijson.Field
Expected apijson.Field
Input apijson.Field
Metadata apijson.Field
Metrics apijson.Field
Output apijson.Field
Scores apijson.Field
SpanAttributes apijson.Field
SpanParents apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchPostResponseEvent) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Context is additional information about the code that produced the experiment
// event. It is essentially the textual counterpart to `metrics`. Use the
// `caller_*` attributes to track the location in code which produced the
// experiment event
type ExperimentFetchPostResponseEventsContext struct {
// Name of the file in code where the experiment event was created
CallerFilename string `json:"caller_filename,nullable"`
// The function in code which created the experiment event
CallerFunctionname string `json:"caller_functionname,nullable"`
// Line of code where the experiment event was created
CallerLineno int64 `json:"caller_lineno,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchPostResponseEventsContextJSON `json:"-"`
}
// experimentFetchPostResponseEventsContextJSON contains the JSON metadata for the
// struct [ExperimentFetchPostResponseEventsContext]
type experimentFetchPostResponseEventsContextJSON struct {
CallerFilename apijson.Field
CallerFunctionname apijson.Field
CallerLineno apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchPostResponseEventsContext) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Metrics are numerical measurements tracking the execution of the code that
// produced the experiment event. Use "start" and "end" to track the time span over
// which the experiment event was produced
type ExperimentFetchPostResponseEventsMetrics struct {
// A unix timestamp recording when the section of code which produced the
// experiment event finished
End float64 `json:"end,nullable"`
// A unix timestamp recording when the section of code which produced the
// experiment event started
Start float64 `json:"start,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchPostResponseEventsMetricsJSON `json:"-"`
}
// experimentFetchPostResponseEventsMetricsJSON contains the JSON metadata for the
// struct [ExperimentFetchPostResponseEventsMetrics]
type experimentFetchPostResponseEventsMetricsJSON struct {
End apijson.Field
Start apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchPostResponseEventsMetrics) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Human-identifying attributes of the span, such as name, type, etc.
type ExperimentFetchPostResponseEventsSpanAttributes struct {
// Name of the span, for display purposes only
Name string `json:"name,nullable"`
// Type of the span, for display purposes only
Type ExperimentFetchPostResponseEventsSpanAttributesType `json:"type,nullable"`
ExtraFields map[string]interface{} `json:"-,extras"`
JSON experimentFetchPostResponseEventsSpanAttributesJSON `json:"-"`
}
// experimentFetchPostResponseEventsSpanAttributesJSON contains the JSON metadata
// for the struct [ExperimentFetchPostResponseEventsSpanAttributes]
type experimentFetchPostResponseEventsSpanAttributesJSON struct {
Name apijson.Field
Type apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentFetchPostResponseEventsSpanAttributes) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
// Type of the span, for display purposes only
type ExperimentFetchPostResponseEventsSpanAttributesType string
const (
ExperimentFetchPostResponseEventsSpanAttributesTypeLlm ExperimentFetchPostResponseEventsSpanAttributesType = "llm"
ExperimentFetchPostResponseEventsSpanAttributesTypeScore ExperimentFetchPostResponseEventsSpanAttributesType = "score"
ExperimentFetchPostResponseEventsSpanAttributesTypeFunction ExperimentFetchPostResponseEventsSpanAttributesType = "function"
ExperimentFetchPostResponseEventsSpanAttributesTypeEval ExperimentFetchPostResponseEventsSpanAttributesType = "eval"
ExperimentFetchPostResponseEventsSpanAttributesTypeTask ExperimentFetchPostResponseEventsSpanAttributesType = "task"
ExperimentFetchPostResponseEventsSpanAttributesTypeTool ExperimentFetchPostResponseEventsSpanAttributesType = "tool"
)
type ExperimentInsertResponse struct {
// The ids of all rows that were inserted, aligning one-to-one with the rows
// provided as input
RowIDs []string `json:"row_ids,required"`
JSON experimentInsertResponseJSON `json:"-"`
}
// experimentInsertResponseJSON contains the JSON metadata for the struct
// [ExperimentInsertResponse]
type experimentInsertResponseJSON struct {
RowIDs apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *ExperimentInsertResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
type ExperimentNewParams struct {
// Unique identifier for the project that the experiment belongs under
ProjectID param.Field[string] `json:"project_id,required" format:"uuid"`
// Id of default base experiment to compare against when viewing this experiment
BaseExpID param.Field[string] `json:"base_exp_id" format:"uuid"`
// Identifier of the linked dataset, or null if the experiment is not linked to a
// dataset
DatasetID param.Field[string] `json:"dataset_id" format:"uuid"`
// Version number of the linked dataset the experiment was run against. This can be
// used to reproduce the experiment after the dataset has been modified.
DatasetVersion param.Field[string] `json:"dataset_version"`
// Textual description of the experiment
Description param.Field[string] `json:"description"`
// User-controlled metadata about the experiment
Metadata param.Field[map[string]interface{}] `json:"metadata"`
// Name of the experiment. Within a project, experiment names are unique
Name param.Field[string] `json:"name"`
// Whether or not the experiment is public. Public experiments can be viewed by
// anybody inside or outside the organization
Public param.Field[bool] `json:"public"`
// Metadata about the state of the repo when the experiment was created
RepoInfo param.Field[ExperimentNewParamsRepoInfo] `json:"repo_info"`
}
func (r ExperimentNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Metadata about the state of the repo when the experiment was created
type ExperimentNewParamsRepoInfo struct {
// Email of the author of the most recent commit
AuthorEmail param.Field[string] `json:"author_email"`
// Name of the author of the most recent commit
AuthorName param.Field[string] `json:"author_name"`
// Name of the branch the most recent commit belongs to
Branch param.Field[string] `json:"branch"`
// SHA of most recent commit
Commit param.Field[string] `json:"commit"`
// Most recent commit message
CommitMessage param.Field[string] `json:"commit_message"`
// Time of the most recent commit
CommitTime param.Field[string] `json:"commit_time"`
// Whether or not the repo had uncommitted changes when snapshotted
Dirty param.Field[bool] `json:"dirty"`
// If the repo was dirty when run, this includes the diff between the current state
// of the repo and the most recent commit.
GitDiff param.Field[string] `json:"git_diff"`
// Name of the tag on the most recent commit
Tag param.Field[string] `json:"tag"`
}
func (r ExperimentNewParamsRepoInfo) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentUpdateParams struct {
// Id of default base experiment to compare against when viewing this experiment
BaseExpID param.Field[string] `json:"base_exp_id" format:"uuid"`
// Identifier of the linked dataset, or null if the experiment is not linked to a
// dataset
DatasetID param.Field[string] `json:"dataset_id" format:"uuid"`
// Version number of the linked dataset the experiment was run against. This can be
// used to reproduce the experiment after the dataset has been modified.
DatasetVersion param.Field[string] `json:"dataset_version"`
// Textual description of the experiment
Description param.Field[string] `json:"description"`
// User-controlled metadata about the experiment
Metadata param.Field[map[string]interface{}] `json:"metadata"`
// Name of the experiment. Within a project, experiment names are unique
Name param.Field[string] `json:"name"`
// Whether or not the experiment is public. Public experiments can be viewed by
// anybody inside or outside the organization
Public param.Field[bool] `json:"public"`
// Metadata about the state of the repo when the experiment was created
RepoInfo param.Field[ExperimentUpdateParamsRepoInfo] `json:"repo_info"`
}
func (r ExperimentUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Metadata about the state of the repo when the experiment was created
type ExperimentUpdateParamsRepoInfo struct {
// Email of the author of the most recent commit
AuthorEmail param.Field[string] `json:"author_email"`
// Name of the author of the most recent commit
AuthorName param.Field[string] `json:"author_name"`
// Name of the branch the most recent commit belongs to
Branch param.Field[string] `json:"branch"`
// SHA of most recent commit
Commit param.Field[string] `json:"commit"`
// Most recent commit message
CommitMessage param.Field[string] `json:"commit_message"`
// Time of the most recent commit
CommitTime param.Field[string] `json:"commit_time"`
// Whether or not the repo had uncommitted changes when snapshotted
Dirty param.Field[bool] `json:"dirty"`
// If the repo was dirty when run, this includes the diff between the current state
// of the repo and the most recent commit.
GitDiff param.Field[string] `json:"git_diff"`
// Name of the tag on the most recent commit
Tag param.Field[string] `json:"tag"`
}
func (r ExperimentUpdateParamsRepoInfo) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentListParams struct {
// A cursor for pagination. For example, if the initial item in the last page you
// fetched had an id of `foo`, pass `ending_before=foo` to fetch the previous page.
// Note: you may only pass one of `starting_after` and `ending_before`
EndingBefore param.Field[string] `query:"ending_before" format:"uuid"`
// Name of the experiment to search for
ExperimentName param.Field[string] `query:"experiment_name"`
// Limit the number of objects to return
Limit param.Field[int64] `query:"limit"`
// Filter search results to within a particular organization
OrgName param.Field[string] `query:"org_name"`
// Name of the project to search for
ProjectName param.Field[string] `query:"project_name"`
// A cursor for pagination. For example, if the final item in the last page you
// fetched had an id of `foo`, pass `starting_after=foo` to fetch the next page.
// Note: you may only pass one of `starting_after` and `ending_before`
StartingAfter param.Field[string] `query:"starting_after" format:"uuid"`
}
// URLQuery serializes [ExperimentListParams]'s query parameters as `url.Values`.
func (r ExperimentListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ExperimentFeedbackParams struct {
// A list of experiment feedback items
Feedback param.Field[[]ExperimentFeedbackParamsFeedback] `json:"feedback,required"`
}
func (r ExperimentFeedbackParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentFeedbackParamsFeedback struct {
// The id of the experiment event to log feedback for. This is the row `id`
// returned by `POST /v1/experiment/{experiment_id}/insert`
ID param.Field[string] `json:"id,required"`
// An optional comment string to log about the experiment event
Comment param.Field[string] `json:"comment"`
// The ground truth value (an arbitrary, JSON serializable object) that you'd
// compare to `output` to determine if your `output` value is correct or not
Expected param.Field[interface{}] `json:"expected"`
// A dictionary with additional data about the feedback. If you have a `user_id`,
// you can log it here and access it in the Braintrust UI.
Metadata param.Field[map[string]interface{}] `json:"metadata"`
// A dictionary of numeric values (between 0 and 1) to log. These scores will be
// merged into the existing scores for the experiment event
Scores param.Field[map[string]float64] `json:"scores"`
// The source of the feedback. Must be one of "external" (default), "app", or "api"
Source param.Field[ExperimentFeedbackParamsFeedbackSource] `json:"source"`
}
func (r ExperimentFeedbackParamsFeedback) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The source of the feedback. Must be one of "external" (default), "app", or "api"
type ExperimentFeedbackParamsFeedbackSource string
const (
ExperimentFeedbackParamsFeedbackSourceApp ExperimentFeedbackParamsFeedbackSource = "app"
ExperimentFeedbackParamsFeedbackSourceAPI ExperimentFeedbackParamsFeedbackSource = "api"
ExperimentFeedbackParamsFeedbackSourceExternal ExperimentFeedbackParamsFeedbackSource = "external"
)
type ExperimentFetchParams struct {
// Fetch queries may be paginated if the total result size is expected to be large
// (e.g. project_logs which accumulate over a long time). Note that fetch queries
// only support pagination in descending time order (from latest to earliest
// `_xact_id`. Furthermore, later pages may return rows which showed up in earlier
// pages, except with an earlier `_xact_id`. This happens because pagination occurs
// over the whole version history of the event log. You will most likely want to
// exclude any such duplicate, outdated rows (by `id`) from your combined result
// set.
//
// The `limit` parameter controls the number of full traces to return. So you may
// end up with more individual rows than the specified limit if you are fetching
// events containing traces.
Limit param.Field[int64] `query:"limit"`
// Together, `max_xact_id` and `max_root_span_id` form a cursor for paginating
// event fetches. Given a previous fetch with a list of rows, you can determine
// `max_root_span_id` as the maximum of the `root_span_id` field over all rows. See
// the documentation for `limit` for an overview of paginating fetch queries.
MaxRootSpanID param.Field[string] `query:"max_root_span_id"`
// Together, `max_xact_id` and `max_root_span_id` form a cursor for paginating
// event fetches. Given a previous fetch with a list of rows, you can determine
// `max_xact_id` as the maximum of the `_xact_id` field over all rows. See the
// documentation for `limit` for an overview of paginating fetch queries.
MaxXactID param.Field[int64] `query:"max_xact_id"`
// You may specify a version id to retrieve a snapshot of the events from a past
// time. The version id is essentially a filter on the latest event transaction id.
// You can use the `max_xact_id` returned by a past fetch as the version to
// reproduce that exact fetch.
Version param.Field[int64] `query:"version"`
}
// URLQuery serializes [ExperimentFetchParams]'s query parameters as `url.Values`.
func (r ExperimentFetchParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type ExperimentFetchPostParams struct {
// A list of filters on the events to fetch. Currently, only path-lookup type
// filters are supported, but we may add more in the future
Filters param.Field[[]ExperimentFetchPostParamsFilter] `json:"filters"`
// Fetch queries may be paginated if the total result size is expected to be large
// (e.g. project_logs which accumulate over a long time). Note that fetch queries
// only support pagination in descending time order (from latest to earliest
// `_xact_id`. Furthermore, later pages may return rows which showed up in earlier
// pages, except with an earlier `_xact_id`. This happens because pagination occurs
// over the whole version history of the event log. You will most likely want to
// exclude any such duplicate, outdated rows (by `id`) from your combined result
// set.
//
// The `limit` parameter controls the number of full traces to return. So you may
// end up with more individual rows than the specified limit if you are fetching
// events containing traces.
Limit param.Field[int64] `json:"limit"`
// Together, `max_xact_id` and `max_root_span_id` form a cursor for paginating
// event fetches. Given a previous fetch with a list of rows, you can determine
// `max_root_span_id` as the maximum of the `root_span_id` field over all rows. See
// the documentation for `limit` for an overview of paginating fetch queries.
MaxRootSpanID param.Field[string] `json:"max_root_span_id"`
// Together, `max_xact_id` and `max_root_span_id` form a cursor for paginating
// event fetches. Given a previous fetch with a list of rows, you can determine
// `max_xact_id` as the maximum of the `_xact_id` field over all rows. See the
// documentation for `limit` for an overview of paginating fetch queries.
MaxXactID param.Field[int64] `json:"max_xact_id"`
// You may specify a version id to retrieve a snapshot of the events from a past
// time. The version id is essentially a filter on the latest event transaction id.
// You can use the `max_xact_id` returned by a past fetch as the version to
// reproduce that exact fetch.
Version param.Field[int64] `json:"version"`
}
func (r ExperimentFetchPostParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// A path-lookup filter describes an equality comparison against a specific
// sub-field in the event row. For instance, if you wish to filter on the value of
// `c` in `{"input": {"a": {"b": {"c": "hello"}}}}`, pass
// `path=["input", "a", "b", "c"]` and `value="hello"`
type ExperimentFetchPostParamsFilter struct {
// List of fields describing the path to the value to be checked against. For
// instance, if you wish to filter on the value of `c` in
// `{"input": {"a": {"b": {"c": "hello"}}}}`, pass `path=["input", "a", "b", "c"]`
Path param.Field[[]string] `json:"path,required"`
// Denotes the type of filter as a path-lookup filter
Type param.Field[ExperimentFetchPostParamsFiltersType] `json:"type,required"`
// The value to compare equality-wise against the event value at the specified
// `path`. The value must be a "primitive", that is, any JSON-serializable object
// except for objects and arrays. For instance, if you wish to filter on the value
// of "input.a.b.c" in the object `{"input": {"a": {"b": {"c": "hello"}}}}`, pass
// `value="hello"`
Value param.Field[interface{}] `json:"value"`
}
func (r ExperimentFetchPostParamsFilter) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Denotes the type of filter as a path-lookup filter
type ExperimentFetchPostParamsFiltersType string
const (
ExperimentFetchPostParamsFiltersTypePathLookup ExperimentFetchPostParamsFiltersType = "path_lookup"
)
type ExperimentInsertParams struct {
// A list of experiment events to insert
Events param.Field[[]ExperimentInsertParamsEvent] `json:"events,required"`
}
func (r ExperimentInsertParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Satisfied by [ExperimentInsertParamsEventsInsertExperimentEventReplace],
// [ExperimentInsertParamsEventsInsertExperimentEventMerge].
type ExperimentInsertParamsEvent interface {
implementsExperimentInsertParamsEvent()
}
type ExperimentInsertParamsEventsInsertExperimentEventReplace struct {
// A unique identifier for the experiment event. If you don't provide one,
// BrainTrust will generate one for you
ID param.Field[string] `json:"id"`
// The `_is_merge` field controls how the row is merged with any existing row with
// the same id in the DB. By default (or when set to `false`), the existing row is