- Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathvectorstorefilebatch.go
375 lines (337 loc) · 15.3 KB
/
vectorstorefilebatch.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"sync"
"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/constant"
)
// VectorStoreFileBatchService 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 [NewVectorStoreFileBatchService] method instead.
typeVectorStoreFileBatchServicestruct {
Options []option.RequestOption
}
// NewVectorStoreFileBatchService 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.
funcNewVectorStoreFileBatchService(opts...option.RequestOption) (rVectorStoreFileBatchService) {
r=VectorStoreFileBatchService{}
r.Options=opts
return
}
// Create a vector store file batch.
func (r*VectorStoreFileBatchService) New(ctx context.Context, vectorStoreIDstring, bodyVectorStoreFileBatchNewParams, opts...option.RequestOption) (res*VectorStoreFileBatch, errerror) {
opts=append(r.Options[:], opts...)
opts=append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
ifvectorStoreID=="" {
err=errors.New("missing required vector_store_id parameter")
return
}
path:=fmt.Sprintf("vector_stores/%s/file_batches", vectorStoreID)
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Create a vector store file batch and polls the API until the task is complete.
// Pass 0 for pollIntervalMs to enable default polling interval.
func (r*VectorStoreFileBatchService) NewAndPoll(ctx context.Context, vectorStoreIdstring, bodyVectorStoreFileBatchNewParams, pollIntervalMsint, opts...option.RequestOption) (res*VectorStoreFileBatch, errerror) {
batch, err:=r.New(ctx, vectorStoreId, body, opts...)
iferr!=nil {
returnnil, err
}
returnr.PollStatus(ctx, vectorStoreId, batch.ID, pollIntervalMs, opts...)
}
// Uploads the given files concurrently and then creates a vector store file batch.
//
// If you've already uploaded certain files that you want to include in this batch
// then you can pass their IDs through the file_ids argument.
//
// Pass 0 for pollIntervalMs to enable default polling interval.
//
// By default, if any file upload fails then an exception will be eagerly raised.
func (r*VectorStoreFileBatchService) UploadAndPoll(ctx context.Context, vectorStoreIDstring, files []FileNewParams, fileIDs []string, pollIntervalMsint, opts...option.RequestOption) (*VectorStoreFileBatch, error) {
iflen(files) <=0 {
returnnil, errors.New("No `files` provided to process. If you've already uploaded files you should use `.NewAndPoll()` instead")
}
filesService:=NewFileService(r.Options...)
uploadedFileIDs:=make(chanstring, len(files))
fileUploadErrors:=make(chanerror, len(files))
wg:= sync.WaitGroup{}
for_, file:=rangefiles {
wg.Add(1)
gofunc(fileFileNewParams) {
deferwg.Done()
fileObj, err:=filesService.New(ctx, file, opts...)
iferr!=nil {
fileUploadErrors<-err
return
}
uploadedFileIDs<-fileObj.ID
}(file)
}
wg.Wait()
close(uploadedFileIDs)
close(fileUploadErrors)
forerr:=rangefileUploadErrors {
returnnil, err
}
forid:=rangeuploadedFileIDs {
fileIDs=append(fileIDs, id)
}
returnr.NewAndPoll(ctx, vectorStoreID, VectorStoreFileBatchNewParams{
FileIDs: fileIDs,
}, pollIntervalMs, opts...)
}
// Retrieves a vector store file batch.
func (r*VectorStoreFileBatchService) Get(ctx context.Context, vectorStoreIDstring, batchIDstring, opts...option.RequestOption) (res*VectorStoreFileBatch, errerror) {
opts=append(r.Options[:], opts...)
opts=append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
ifvectorStoreID=="" {
err=errors.New("missing required vector_store_id parameter")
return
}
ifbatchID=="" {
err=errors.New("missing required batch_id parameter")
return
}
path:=fmt.Sprintf("vector_stores/%s/file_batches/%s", vectorStoreID, batchID)
err=requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Cancel a vector store file batch. This attempts to cancel the processing of
// files in this batch as soon as possible.
func (r*VectorStoreFileBatchService) Cancel(ctx context.Context, vectorStoreIDstring, batchIDstring, opts...option.RequestOption) (res*VectorStoreFileBatch, errerror) {
opts=append(r.Options[:], opts...)
opts=append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
ifvectorStoreID=="" {
err=errors.New("missing required vector_store_id parameter")
return
}
ifbatchID=="" {
err=errors.New("missing required batch_id parameter")
return
}
path:=fmt.Sprintf("vector_stores/%s/file_batches/%s/cancel", vectorStoreID, batchID)
err=requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
return
}
// Returns a list of vector store files in a batch.
func (r*VectorStoreFileBatchService) ListFiles(ctx context.Context, vectorStoreIDstring, batchIDstring, queryVectorStoreFileBatchListFilesParams, opts...option.RequestOption) (res*pagination.CursorPage[VectorStoreFile], errerror) {
varraw*http.Response
opts=append(r.Options[:], opts...)
opts=append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
ifvectorStoreID=="" {
err=errors.New("missing required vector_store_id parameter")
return
}
ifbatchID=="" {
err=errors.New("missing required batch_id parameter")
return
}
path:=fmt.Sprintf("vector_stores/%s/file_batches/%s/files", vectorStoreID, batchID)
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
}
// Returns a list of vector store files in a batch.
func (r*VectorStoreFileBatchService) ListFilesAutoPaging(ctx context.Context, vectorStoreIDstring, batchIDstring, queryVectorStoreFileBatchListFilesParams, opts...option.RequestOption) *pagination.CursorPageAutoPager[VectorStoreFile] {
returnpagination.NewCursorPageAutoPager(r.ListFiles(ctx, vectorStoreID, batchID, query, opts...))
}
// A batch of files attached to a vector store.
typeVectorStoreFileBatchstruct {
// The identifier, which can be referenced in API endpoints.
IDstring`json:"id,required"`
// The Unix timestamp (in seconds) for when the vector store files batch was
// created.
CreatedAtint64`json:"created_at,required"`
FileCountsVectorStoreFileBatchFileCounts`json:"file_counts,required"`
// The object type, which is always `vector_store.file_batch`.
Object constant.VectorStoreFilesBatch`json:"object,required"`
// The status of the vector store files batch, which can be either `in_progress`,
// `completed`, `cancelled` or `failed`.
//
// Any of "in_progress", "completed", "cancelled", "failed".
StatusVectorStoreFileBatchStatus`json:"status,required"`
// The ID of the
// [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object)
// that the [File](https://platform.openai.com/docs/api-reference/files) is
// attached to.
VectorStoreIDstring`json:"vector_store_id,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
ID resp.Field
CreatedAt resp.Field
FileCounts resp.Field
Object resp.Field
Status resp.Field
VectorStoreID resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rVectorStoreFileBatch) RawJSON() string { returnr.JSON.raw }
func (r*VectorStoreFileBatch) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
typeVectorStoreFileBatchFileCountsstruct {
// The number of files that where cancelled.
Cancelledint64`json:"cancelled,required"`
// The number of files that have been processed.
Completedint64`json:"completed,required"`
// The number of files that have failed to process.
Failedint64`json:"failed,required"`
// The number of files that are currently being processed.
InProgressint64`json:"in_progress,required"`
// The total number of files.
Totalint64`json:"total,required"`
// Metadata for the response, check the presence of optional fields with the
// [resp.Field.IsPresent] method.
JSONstruct {
Cancelled resp.Field
Completed resp.Field
Failed resp.Field
InProgress resp.Field
Total resp.Field
ExtraFieldsmap[string]resp.Field
rawstring
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (rVectorStoreFileBatchFileCounts) RawJSON() string { returnr.JSON.raw }
func (r*VectorStoreFileBatchFileCounts) UnmarshalJSON(data []byte) error {
returnapijson.UnmarshalRoot(data, r)
}
// The status of the vector store files batch, which can be either `in_progress`,
// `completed`, `cancelled` or `failed`.
typeVectorStoreFileBatchStatusstring
const (
VectorStoreFileBatchStatusInProgressVectorStoreFileBatchStatus="in_progress"
VectorStoreFileBatchStatusCompletedVectorStoreFileBatchStatus="completed"
VectorStoreFileBatchStatusCancelledVectorStoreFileBatchStatus="cancelled"
VectorStoreFileBatchStatusFailedVectorStoreFileBatchStatus="failed"
)
typeVectorStoreFileBatchNewParamsstruct {
// A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
// the vector store should use. Useful for tools like `file_search` that can access
// files.
FileIDs []string`json:"file_ids,omitzero,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, booleans, or numbers.
Attributesmap[string]VectorStoreFileBatchNewParamsAttributeUnion`json:"attributes,omitzero"`
// The chunking strategy used to chunk the file(s). If not set, will use the `auto`
// strategy. Only applicable if `file_ids` is non-empty.
ChunkingStrategyFileChunkingStrategyParamUnion`json:"chunking_strategy,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 (fVectorStoreFileBatchNewParams) IsPresent() bool { return!param.IsOmitted(f) &&!f.IsNull() }
func (rVectorStoreFileBatchNewParams) MarshalJSON() (data []byte, errerror) {
typeshadowVectorStoreFileBatchNewParams
returnparam.MarshalObject(r, (*shadow)(&r))
}
// Only one field can be non-zero.
//
// Use [param.IsOmitted] to confirm if a field is set.
typeVectorStoreFileBatchNewParamsAttributeUnionstruct {
OfString param.Opt[string] `json:",omitzero,inline"`
OfFloat param.Opt[float64] `json:",omitzero,inline"`
OfBool param.Opt[bool] `json:",omitzero,inline"`
paramUnion
}
// 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 (uVectorStoreFileBatchNewParamsAttributeUnion) IsPresent() bool {
return!param.IsOmitted(u) &&!u.IsNull()
}
func (uVectorStoreFileBatchNewParamsAttributeUnion) MarshalJSON() ([]byte, error) {
returnparam.MarshalUnion[VectorStoreFileBatchNewParamsAttributeUnion](u.OfString, u.OfFloat, u.OfBool)
}
func (u*VectorStoreFileBatchNewParamsAttributeUnion) asAny() any {
if!param.IsOmitted(u.OfString) {
return&u.OfString.Value
} elseif!param.IsOmitted(u.OfFloat) {
return&u.OfFloat.Value
} elseif!param.IsOmitted(u.OfBool) {
return&u.OfBool.Value
}
returnnil
}
typeVectorStoreFileBatchListFilesParamsstruct {
// 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 cursor for use in pagination. `before` is an object ID that defines your place
// in the list. For instance, if you make a list request and receive 100 objects,
// starting with obj_foo, your subsequent call can include before=obj_foo in order
// to fetch the previous page of the list.
Before param.Opt[string] `query:"before,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:"-"`
// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
//
// Any of "in_progress", "completed", "failed", "cancelled".
FilterVectorStoreFileBatchListFilesParamsFilter`query:"filter,omitzero" json:"-"`
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
// order and `desc` for descending order.
//
// Any of "asc", "desc".
OrderVectorStoreFileBatchListFilesParamsOrder`query:"order,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 (fVectorStoreFileBatchListFilesParams) IsPresent() bool {
return!param.IsOmitted(f) &&!f.IsNull()
}
// URLQuery serializes [VectorStoreFileBatchListFilesParams]'s query parameters as
// `url.Values`.
func (rVectorStoreFileBatchListFilesParams) URLQuery() (v url.Values, errerror) {
returnapiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
typeVectorStoreFileBatchListFilesParamsFilterstring
const (
VectorStoreFileBatchListFilesParamsFilterInProgressVectorStoreFileBatchListFilesParamsFilter="in_progress"
VectorStoreFileBatchListFilesParamsFilterCompletedVectorStoreFileBatchListFilesParamsFilter="completed"
VectorStoreFileBatchListFilesParamsFilterFailedVectorStoreFileBatchListFilesParamsFilter="failed"
VectorStoreFileBatchListFilesParamsFilterCancelledVectorStoreFileBatchListFilesParamsFilter="cancelled"
)
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
// order and `desc` for descending order.
typeVectorStoreFileBatchListFilesParamsOrderstring
const (
VectorStoreFileBatchListFilesParamsOrderAscVectorStoreFileBatchListFilesParamsOrder="asc"
VectorStoreFileBatchListFilesParamsOrderDescVectorStoreFileBatchListFilesParamsOrder="desc"
)