- Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathbatch.go
352 lines (322 loc) · 13.6 KB
/
batch.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/internal/apiquery"
"github.com/openai/openai-go/internal/requestconfig"
"github.com/openai/openai-go/option"
"github.com/openai/openai-go/packages/pagination"
"github.com/openai/openai-go/packages/param"
"github.com/openai/openai-go/packages/resp"
"github.com/openai/openai-go/shared"
"github.com/openai/openai-go/shared/constant"
)
// BatchService contains methods and other services that help with interacting with
// the openai 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 [NewBatchService] method instead.
typeBatchServicestruct {
Options []option.RequestOption
}
// NewBatchService 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.
funcNewBatchService(opts...option.RequestOption) (rBatchService) {
r=BatchService{}
r.Options=opts
return
}
// Creates and executes a batch from an uploaded file of requests
func (r*BatchService) New(ctx context.Context, bodyBatchNewParams, opts...option.RequestOption) (res*Batch, errerror) {
opts=append(r.Options[:], opts...)
path:="batches"
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Retrieves a batch.
func (r*BatchService) Get(ctx context.Context, batchIDstring, opts...option.RequestOption) (res*Batch, errerror) {
opts=append(r.Options[:], opts...)
ifbatchID=="" {
err=errors.New("missing required batch_id parameter")
return
}
path:=fmt.Sprintf("batches/%s", batchID)
err=requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// List your organization's batches.
func (r*BatchService) List(ctx context.Context, queryBatchListParams, opts...option.RequestOption) (res*pagination.CursorPage[Batch], errerror) {
varraw*http.Response
opts=append(r.Options[:], opts...)
opts=append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path:="batches"
cfg, err:=requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
iferr!=nil {
returnnil, err
}
err=cfg.Execute()
iferr!=nil {
returnnil, err
}
res.SetPageConfig(cfg, raw)
returnres, nil
}
// List your organization's batches.
func (r*BatchService) ListAutoPaging(ctx context.Context, queryBatchListParams, opts...option.RequestOption) *pagination.CursorPageAutoPager[Batch] {
returnpagination.NewCursorPageAutoPager(r.List(ctx, query, opts...))
}
// Cancels an in-progress batch. The batch will be in status `cancelling` for up to
// 10 minutes, before changing to `cancelled`, where it will have partial results
// (if any) available in the output file.
func (r*BatchService) Cancel(ctx context.Context, batchIDstring, opts...option.RequestOption) (res*Batch, errerror) {
opts=append(r.Options[:], opts...)
ifbatchID=="" {
err=errors.New("missing required batch_id parameter")
return
}
path:=fmt.Sprintf("batches/%s/cancel", batchID)
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
typeBatchstruct {
IDstring`json:"id,required"`
// The time frame within which the batch should be processed.
CompletionWindowstring`json:"completion_window,required"`
// The Unix timestamp (in seconds) for when the batch was created.
CreatedAtint64`json:"created_at,required"`
// The OpenAI API endpoint used by the batch.
Endpointstring`json:"endpoint,required"`
// The ID of the input file for the batch.
InputFileIDstring`json:"input_file_id,required"`
// The object type, which is always `batch`.
Object constant.Batch`json:"object,required"`
// The current status of the batch.
//
// Any of "validating", "failed", "in_progress", "finalizing", "completed",
// "expired", "cancelling", "cancelled".
StatusBatchStatus`json:"status,required"`
// The Unix timestamp (in seconds) for when the batch was cancelled.
CancelledAtint64`json:"cancelled_at"`
// The Unix timestamp (in seconds) for when the batch started cancelling.
CancellingAtint64`json:"cancelling_at"`
// The Unix timestamp (in seconds) for when the batch was completed.
CompletedAtint64`json:"completed_at"`
// The ID of the file containing the outputs of requests with errors.
ErrorFileIDstring`json:"error_file_id"`
ErrorsBatchErrors`json:"errors"`
// The Unix timestamp (in seconds) for when the batch expired.
ExpiredAtint64`json:"expired_at"`
// The Unix timestamp (in seconds) for when the batch will expire.
ExpiresAtint64`json:"expires_at"`
// The Unix timestamp (in seconds) for when the batch failed.
FailedAtint64`json:"failed_at"`
// The Unix timestamp (in seconds) for when the batch started finalizing.
FinalizingAtint64`json:"finalizing_at"`
// The Unix timestamp (in seconds) for when the batch started processing.
InProgressAtint64`json:"in_progress_at"`
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format, and
// querying for objects via API or the dashboard.
//
// Keys are strings with a maximum length of 64 characters. Values are strings with
// a maximum length of 512 characters.
Metadata shared.Metadata`json:"metadata,nullable"`
// The ID of the file containing the outputs of successfully executed requests.
OutputFileIDstring`json:"output_file_id"`
// The request counts for different statuses within the batch.
RequestCountsBatchRequestCounts`json:"request_counts"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
ID resp.Field
CompletionWindow resp.Field
CreatedAt resp.Field
Endpoint resp.Field
InputFileID resp.Field
Object resp.Field
Status resp.Field
CancelledAt resp.Field
CancellingAt resp.Field
CompletedAt resp.Field
ErrorFileID resp.Field
Errors resp.Field
ExpiredAt resp.Field
ExpiresAt resp.Field
FailedAt resp.Field
FinalizingAt resp.Field
InProgressAt resp.Field
Metadata resp.Field
OutputFileID resp.Field
RequestCounts resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rBatch) RawJSON() string { returnr.JSON.raw }
func (r*Batch) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// The current status of the batch.
typeBatchStatusstring
const (
BatchStatusValidatingBatchStatus="validating"
BatchStatusFailedBatchStatus="failed"
BatchStatusInProgressBatchStatus="in_progress"
BatchStatusFinalizingBatchStatus="finalizing"
BatchStatusCompletedBatchStatus="completed"
BatchStatusExpiredBatchStatus="expired"
BatchStatusCancellingBatchStatus="cancelling"
BatchStatusCancelledBatchStatus="cancelled"
)
typeBatchErrorsstruct {
Data []BatchError`json:"data"`
// The object type, which is always `list`.
Objectstring`json:"object"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Data resp.Field
Object resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rBatchErrors) RawJSON() string { returnr.JSON.raw }
func (r*BatchErrors) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
typeBatchErrorstruct {
// An error code identifying the error type.
Codestring`json:"code"`
// The line number of the input file where the error occurred, if applicable.
Lineint64`json:"line,nullable"`
// A human-readable message providing more details about the error.
Messagestring`json:"message"`
// The name of the parameter that caused the error, if applicable.
Paramstring`json:"param,nullable"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Code resp.Field
Line resp.Field
Message resp.Field
Param resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rBatchError) RawJSON() string { returnr.JSON.raw }
func (r*BatchError) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// The request counts for different statuses within the batch.
typeBatchRequestCountsstruct {
// Number of requests that have been completed successfully.
Completedint64`json:"completed,required"`
// Number of requests that have failed.
Failedint64`json:"failed,required"`
// Total number of requests in the batch.
Totalint64`json:"total,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Completed resp.Field
Failed resp.Field
Total resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rBatchRequestCounts) RawJSON() string { returnr.JSON.raw }
func (r*BatchRequestCounts) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
typeBatchNewParamsstruct {
// The time frame within which the batch should be processed. Currently only `24h`
// is supported.
//
// Any of "24h".
CompletionWindowBatchNewParamsCompletionWindow`json:"completion_window,omitzero,required"`
// The endpoint to be used for all requests in the batch. Currently
// `/v1/responses`, `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions`
// are supported. Note that `/v1/embeddings` batches are also restricted to a
// maximum of 50,000 embedding inputs across all requests in the batch.
//
// Any of "/v1/responses", "/v1/chat/completions", "/v1/embeddings",
// "/v1/completions".
EndpointBatchNewParamsEndpoint`json:"endpoint,omitzero,required"`
// The ID of an uploaded file that contains requests for the new batch.
//
// See [upload file](https://platform.openai.com/docs/api-reference/files/create)
// for how to upload a file.
//
// Your input file must be formatted as a
// [JSONL file](https://platform.openai.com/docs/api-reference/batch/request-input),
// and must be uploaded with the purpose `batch`. The file can contain up to 50,000
// requests, and can be up to 200 MB in size.
InputFileIDstring`json:"input_file_id,required"`
// Set of 16 key-value pairs that can be attached to an object. This can be useful
// for storing additional information about the object in a structured format, and
// querying for objects via API or the dashboard.
//
// Keys are strings with a maximum length of 64 characters. Values are strings with
// a maximum length of 512 characters.
Metadata shared.MetadataParam`json:"metadata,omitzero"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fBatchNewParams) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rBatchNewParams) MarshalJSON() (data []byte, errerror) {
typeshadowBatchNewParams
returnparam.MarshalObject(r, (*shadow)(&r))
}
// The time frame within which the batch should be processed. Currently only `24h`
// is supported.
typeBatchNewParamsCompletionWindowstring
const (
BatchNewParamsCompletionWindow24hBatchNewParamsCompletionWindow="24h"
)
// The endpoint to be used for all requests in the batch. Currently
// `/v1/responses`, `/v1/chat/completions`, `/v1/embeddings`, and `/v1/completions`
// are supported. Note that `/v1/embeddings` batches are also restricted to a
// maximum of 50,000 embedding inputs across all requests in the batch.
typeBatchNewParamsEndpointstring
const (
BatchNewParamsEndpointV1ResponsesBatchNewParamsEndpoint="/v1/responses"
BatchNewParamsEndpointV1ChatCompletionsBatchNewParamsEndpoint="/v1/chat/completions"
BatchNewParamsEndpointV1EmbeddingsBatchNewParamsEndpoint="/v1/embeddings"
BatchNewParamsEndpointV1CompletionsBatchNewParamsEndpoint="/v1/completions"
)
typeBatchListParamsstruct {
// A cursor for use in pagination. `after` is an object ID that defines your place
// in the list. For instance, if you make a list request and receive 100 objects,
// ending with obj_foo, your subsequent call can include after=obj_foo in order to
// fetch the next page of the list.
After param.Opt[string] `query:"after,omitzero" json:"-"`
// A limit on the number of objects to be returned. Limit can range between 1 and
// 100, and the default is 20.
Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
paramObj
}
// IsPresent returns true if the field's value is not omitted and not the JSON
// "null". To check if this field is omitted, use [param.IsOmitted].
func (fBatchListParams) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
// URLQuery serializes [BatchListParams]'s query parameters as `url.Values`.
func (rBatchListParams) URLQuery() (v url.Values, errerror) {
returnapiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}