-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.go
executable file
·393 lines (358 loc) · 16.7 KB
/
dataset.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
// 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"
)
// DatasetService 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 [NewDatasetService] method instead.
type DatasetService struct {
Options []option.RequestOption
}
// NewDatasetService 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 NewDatasetService(opts ...option.RequestOption) (r *DatasetService) {
r = &DatasetService{}
r.Options = opts
return
}
// Create a new dataset. If there is an existing dataset in the project with the
// same name as the one specified in the request, will return the existing dataset
// unmodified
func (r *DatasetService) New(ctx context.Context, body DatasetNewParams, opts ...option.RequestOption) (res *shared.Dataset, err error) {
opts = append(r.Options[:], opts...)
path := "v1/dataset"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Get a dataset object by its id
func (r *DatasetService) Get(ctx context.Context, datasetID string, opts ...option.RequestOption) (res *shared.Dataset, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Partially update a dataset 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 *DatasetService) Update(ctx context.Context, datasetID string, body DatasetUpdateParams, opts ...option.RequestOption) (res *shared.Dataset, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, body, &res, opts...)
return
}
// List out all datasets. The datasets are sorted by creation date, with the most
// recently-created datasets coming first
func (r *DatasetService) List(ctx context.Context, query DatasetListParams, opts ...option.RequestOption) (res *pagination.ListObjects[shared.Dataset], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "v1/dataset"
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 datasets. The datasets are sorted by creation date, with the most
// recently-created datasets coming first
func (r *DatasetService) ListAutoPaging(ctx context.Context, query DatasetListParams, opts ...option.RequestOption) *pagination.ListObjectsAutoPager[shared.Dataset] {
return pagination.NewListObjectsAutoPager(r.List(ctx, query, opts...))
}
// Delete a dataset object by its id
func (r *DatasetService) Delete(ctx context.Context, datasetID string, opts ...option.RequestOption) (res *shared.Dataset, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return
}
// Log feedback for a set of dataset events
func (r *DatasetService) Feedback(ctx context.Context, datasetID string, body DatasetFeedbackParams, opts ...option.RequestOption) (res *shared.FeedbackResponseSchema, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s/feedback", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Fetch the events in a dataset. 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 *DatasetService) Fetch(ctx context.Context, datasetID string, query DatasetFetchParams, opts ...option.RequestOption) (res *shared.FetchDatasetEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s/fetch", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return
}
// Fetch the events in a dataset. 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 *DatasetService) FetchPost(ctx context.Context, datasetID string, body DatasetFetchPostParams, opts ...option.RequestOption) (res *shared.FetchDatasetEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s/fetch", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Insert a set of events into the dataset
func (r *DatasetService) Insert(ctx context.Context, datasetID string, body DatasetInsertParams, opts ...option.RequestOption) (res *shared.InsertEventsResponse, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s/insert", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Summarize dataset
func (r *DatasetService) Summarize(ctx context.Context, datasetID string, query DatasetSummarizeParams, opts ...option.RequestOption) (res *shared.SummarizeDatasetResponse, err error) {
opts = append(r.Options[:], opts...)
if datasetID == "" {
err = errors.New("missing required dataset_id parameter")
return
}
path := fmt.Sprintf("v1/dataset/%s/summarize", datasetID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return
}
type DatasetNewParams struct {
// Name of the dataset. Within a project, dataset names are unique
Name param.Field[string] `json:"name,required"`
// Unique identifier for the project that the dataset belongs under
ProjectID param.Field[string] `json:"project_id,required" format:"uuid"`
// Textual description of the dataset
Description param.Field[string] `json:"description"`
// User-controlled metadata about the dataset
Metadata param.Field[map[string]interface{}] `json:"metadata"`
}
func (r DatasetNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type DatasetUpdateParams struct {
// Textual description of the dataset
Description param.Field[string] `json:"description"`
// User-controlled metadata about the dataset
Metadata param.Field[map[string]interface{}] `json:"metadata"`
// Name of the dataset. Within a project, dataset names are unique
Name param.Field[string] `json:"name"`
}
func (r DatasetUpdateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type DatasetListParams struct {
// Name of the dataset to search for
DatasetName param.Field[string] `query:"dataset_name"`
// 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"`
// 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[DatasetListParamsIDsUnion] `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 [DatasetListParams]'s query parameters as `url.Values`.
func (r DatasetListParams) 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], [DatasetListParamsIDsArray].
type DatasetListParamsIDsUnion interface {
ImplementsDatasetListParamsIDsUnion()
}
type DatasetListParamsIDsArray []string
func (r DatasetListParamsIDsArray) ImplementsDatasetListParamsIDsUnion() {}
type DatasetFeedbackParams struct {
// A list of dataset feedback items
Feedback param.Field[[]shared.FeedbackDatasetItemParam] `json:"feedback,required"`
}
func (r DatasetFeedbackParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type DatasetFetchParams 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 [DatasetFetchParams]'s query parameters as `url.Values`.
func (r DatasetFetchParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type DatasetFetchPostParams 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 DatasetFetchPostParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type DatasetInsertParams struct {
// A list of dataset events to insert
Events param.Field[[]shared.InsertDatasetEventParam] `json:"events,required"`
}
func (r DatasetInsertParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type DatasetSummarizeParams struct {
// Whether to summarize the data. If false (or omitted), only the metadata will be
// returned.
SummarizeData param.Field[bool] `query:"summarize_data"`
}
// URLQuery serializes [DatasetSummarizeParams]'s query parameters as `url.Values`.
func (r DatasetSummarizeParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}