-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment.go
executable file
·430 lines (395 loc) · 19.4 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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package braintrust
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"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/option"
"github.com/braintrustdata/braintrust-go/packages/pagination"
"github.com/braintrustdata/braintrust-go/shared"
)
// 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 return the existing
// experiment unmodified
func (r *ExperimentService) New(ctx context.Context, body ExperimentNewParams, opts ...option.RequestOption) (res *shared.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 *shared.Experiment, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
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.
func (r *ExperimentService) Update(ctx context.Context, experimentID string, body ExperimentUpdateParams, opts ...option.RequestOption) (res *shared.Experiment, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
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 *pagination.ListObjects[shared.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) *pagination.ListObjectsAutoPager[shared.Experiment] {
return pagination.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 *shared.Experiment, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
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) (res *shared.FeedbackResponseSchema, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
path := fmt.Sprintf("v1/experiment/%s/feedback", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, 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. For
// more complex queries, use the `POST /btql` endpoint.
func (r *ExperimentService) Fetch(ctx context.Context, experimentID string, query ExperimentFetchParams, opts ...option.RequestOption) (res *shared.FetchExperimentEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
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. For
// more complex queries, use the `POST /btql` endpoint.
func (r *ExperimentService) FetchPost(ctx context.Context, experimentID string, body ExperimentFetchPostParams, opts ...option.RequestOption) (res *shared.FetchExperimentEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
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 *shared.InsertEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
path := fmt.Sprintf("v1/experiment/%s/insert", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Summarize experiment
func (r *ExperimentService) Summarize(ctx context.Context, experimentID string, query ExperimentSummarizeParams, opts ...option.RequestOption) (res *shared.SummarizeExperimentResponse, err error) {
opts = append(r.Options[:], opts...)
if experimentID == "" {
err = errors.New("missing required experiment_id parameter")
return
}
path := fmt.Sprintf("v1/experiment/%s/summarize", experimentID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return
}
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"`
// Normally, creating an experiment with the same name as an existing experiment
// will return the existing one un-modified. But if `ensure_new` is true,
// registration will generate a new experiment with a unique name in case of a
// conflict.
EnsureNew param.Field[bool] `json:"ensure_new"`
// 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[shared.RepoInfoParam] `json:"repo_info"`
}
func (r ExperimentNewParams) 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[shared.RepoInfoParam] `json:"repo_info"`
}
func (r ExperimentUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentListParams struct {
// Pagination cursor id.
//
// 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"`
// Filter search results to a particular set of object IDs. To specify a list of
// IDs, include the query param multiple times
IDs param.Field[ExperimentListParamsIDsUnion] `query:"ids" format:"uuid"`
// 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"`
// Project id
ProjectID param.Field[string] `query:"project_id" format:"uuid"`
// Name of the project to search for
ProjectName param.Field[string] `query:"project_name"`
// Pagination cursor id.
//
// 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,
})
}
// Filter search results to a particular set of object IDs. To specify a list of
// IDs, include the query param multiple times
//
// Satisfied by [shared.UnionString], [ExperimentListParamsIDsArray].
type ExperimentListParamsIDsUnion interface {
ImplementsExperimentListParamsIDsUnion()
}
type ExperimentListParamsIDsArray []string
func (r ExperimentListParamsIDsArray) ImplementsExperimentListParamsIDsUnion() {}
type ExperimentFeedbackParams struct {
// A list of experiment feedback items
Feedback param.Field[[]shared.FeedbackExperimentItemParam] `json:"feedback,required"`
}
func (r ExperimentFeedbackParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentFetchParams struct {
// limit the number of traces fetched
//
// 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"`
// DEPRECATION NOTICE: The manually-constructed pagination cursor is deprecated in
// favor of the explicit 'cursor' returned by object fetch requests. Please prefer
// the 'cursor' argument going forwards.
//
// Together, `max_xact_id` and `max_root_span_id` form a pagination cursor
//
// Since a paginated fetch query returns results in order from latest to earliest,
// the cursor for the next page can be found as the row with the minimum (earliest)
// value of the tuple `(_xact_id, root_span_id)`. See the documentation of `limit`
// for an overview of paginating fetch queries.
MaxRootSpanID param.Field[string] `query:"max_root_span_id"`
// DEPRECATION NOTICE: The manually-constructed pagination cursor is deprecated in
// favor of the explicit 'cursor' returned by object fetch requests. Please prefer
// the 'cursor' argument going forwards.
//
// Together, `max_xact_id` and `max_root_span_id` form a pagination cursor
//
// Since a paginated fetch query returns results in order from latest to earliest,
// the cursor for the next page can be found as the row with the minimum (earliest)
// value of the tuple `(_xact_id, root_span_id)`. See the documentation of `limit`
// for an overview of paginating fetch queries.
MaxXactID param.Field[string] `query:"max_xact_id"`
// Retrieve a snapshot of 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[string] `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 {
// An opaque string to be used as a cursor for the next page of results, in order
// from latest to earliest.
//
// The string can be obtained directly from the `cursor` property of the previous
// fetch query
Cursor param.Field[string] `json:"cursor"`
// limit the number of traces fetched
//
// 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"`
// DEPRECATION NOTICE: The manually-constructed pagination cursor is deprecated in
// favor of the explicit 'cursor' returned by object fetch requests. Please prefer
// the 'cursor' argument going forwards.
//
// Together, `max_xact_id` and `max_root_span_id` form a pagination cursor
//
// Since a paginated fetch query returns results in order from latest to earliest,
// the cursor for the next page can be found as the row with the minimum (earliest)
// value of the tuple `(_xact_id, root_span_id)`. See the documentation of `limit`
// for an overview of paginating fetch queries.
MaxRootSpanID param.Field[string] `json:"max_root_span_id"`
// DEPRECATION NOTICE: The manually-constructed pagination cursor is deprecated in
// favor of the explicit 'cursor' returned by object fetch requests. Please prefer
// the 'cursor' argument going forwards.
//
// Together, `max_xact_id` and `max_root_span_id` form a pagination cursor
//
// Since a paginated fetch query returns results in order from latest to earliest,
// the cursor for the next page can be found as the row with the minimum (earliest)
// value of the tuple `(_xact_id, root_span_id)`. See the documentation of `limit`
// for an overview of paginating fetch queries.
MaxXactID param.Field[string] `json:"max_xact_id"`
// Retrieve a snapshot of 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[string] `json:"version"`
}
func (r ExperimentFetchPostParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentInsertParams struct {
// A list of experiment events to insert
Events param.Field[[]shared.InsertExperimentEventParam] `json:"events,required"`
}
func (r ExperimentInsertParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type ExperimentSummarizeParams struct {
// The experiment to compare against, if summarizing scores and metrics. If
// omitted, will fall back to the `base_exp_id` stored in the experiment metadata,
// and then to the most recent experiment run in the same project. Must pass
// `summarize_scores=true` for this id to be used
ComparisonExperimentID param.Field[string] `query:"comparison_experiment_id" format:"uuid"`
// Whether to summarize the scores and metrics. If false (or omitted), only the
// metadata will be returned.
SummarizeScores param.Field[bool] `query:"summarize_scores"`
}
// URLQuery serializes [ExperimentSummarizeParams]'s query parameters as
// `url.Values`.
func (r ExperimentSummarizeParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}