vectorstorefilebatch.go

  1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
  2
  3package openai
  4
  5import (
  6	"context"
  7	"errors"
  8	"fmt"
  9	"net/http"
 10	"net/url"
 11	"sync"
 12
 13	"github.com/openai/openai-go/internal/apijson"
 14	"github.com/openai/openai-go/internal/apiquery"
 15	"github.com/openai/openai-go/internal/requestconfig"
 16	"github.com/openai/openai-go/option"
 17	"github.com/openai/openai-go/packages/pagination"
 18	"github.com/openai/openai-go/packages/param"
 19	"github.com/openai/openai-go/packages/resp"
 20	"github.com/openai/openai-go/shared/constant"
 21)
 22
 23// VectorStoreFileBatchService contains methods and other services that help with
 24// interacting with the openai API.
 25//
 26// Note, unlike clients, this service does not read variables from the environment
 27// automatically. You should not instantiate this service directly, and instead use
 28// the [NewVectorStoreFileBatchService] method instead.
 29type VectorStoreFileBatchService struct {
 30	Options []option.RequestOption
 31}
 32
 33// NewVectorStoreFileBatchService generates a new service that applies the given
 34// options to each request. These options are applied after the parent client's
 35// options (if there is one), and before any request-specific options.
 36func NewVectorStoreFileBatchService(opts ...option.RequestOption) (r VectorStoreFileBatchService) {
 37	r = VectorStoreFileBatchService{}
 38	r.Options = opts
 39	return
 40}
 41
 42// Create a vector store file batch.
 43func (r *VectorStoreFileBatchService) New(ctx context.Context, vectorStoreID string, body VectorStoreFileBatchNewParams, opts ...option.RequestOption) (res *VectorStoreFileBatch, err error) {
 44	opts = append(r.Options[:], opts...)
 45	opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
 46	if vectorStoreID == "" {
 47		err = errors.New("missing required vector_store_id parameter")
 48		return
 49	}
 50	path := fmt.Sprintf("vector_stores/%s/file_batches", vectorStoreID)
 51	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
 52	return
 53}
 54
 55// Create a vector store file batch and polls the API until the task is complete.
 56// Pass 0 for pollIntervalMs to enable default polling interval.
 57func (r *VectorStoreFileBatchService) NewAndPoll(ctx context.Context, vectorStoreId string, body VectorStoreFileBatchNewParams, pollIntervalMs int, opts ...option.RequestOption) (res *VectorStoreFileBatch, err error) {
 58	batch, err := r.New(ctx, vectorStoreId, body, opts...)
 59	if err != nil {
 60		return nil, err
 61	}
 62	return r.PollStatus(ctx, vectorStoreId, batch.ID, pollIntervalMs, opts...)
 63}
 64
 65// Uploads the given files concurrently and then creates a vector store file batch.
 66//
 67// If you've already uploaded certain files that you want to include in this batch
 68// then you can pass their IDs through the file_ids argument.
 69//
 70// Pass 0 for pollIntervalMs to enable default polling interval.
 71//
 72// By default, if any file upload fails then an exception will be eagerly raised.
 73func (r *VectorStoreFileBatchService) UploadAndPoll(ctx context.Context, vectorStoreID string, files []FileNewParams, fileIDs []string, pollIntervalMs int, opts ...option.RequestOption) (*VectorStoreFileBatch, error) {
 74	if len(files) <= 0 {
 75		return nil, errors.New("No `files` provided to process. If you've already uploaded files you should use `.NewAndPoll()` instead")
 76	}
 77
 78	filesService := NewFileService(r.Options...)
 79
 80	uploadedFileIDs := make(chan string, len(files))
 81	fileUploadErrors := make(chan error, len(files))
 82	wg := sync.WaitGroup{}
 83
 84	for _, file := range files {
 85		wg.Add(1)
 86		go func(file FileNewParams) {
 87			defer wg.Done()
 88			fileObj, err := filesService.New(ctx, file, opts...)
 89			if err != nil {
 90				fileUploadErrors <- err
 91				return
 92			}
 93			uploadedFileIDs <- fileObj.ID
 94		}(file)
 95	}
 96
 97	wg.Wait()
 98	close(uploadedFileIDs)
 99	close(fileUploadErrors)
100
101	for err := range fileUploadErrors {
102		return nil, err
103	}
104
105	for id := range uploadedFileIDs {
106		fileIDs = append(fileIDs, id)
107	}
108
109	return r.NewAndPoll(ctx, vectorStoreID, VectorStoreFileBatchNewParams{
110		FileIDs: fileIDs,
111	}, pollIntervalMs, opts...)
112}
113
114// Retrieves a vector store file batch.
115func (r *VectorStoreFileBatchService) Get(ctx context.Context, vectorStoreID string, batchID string, opts ...option.RequestOption) (res *VectorStoreFileBatch, err error) {
116	opts = append(r.Options[:], opts...)
117	opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
118	if vectorStoreID == "" {
119		err = errors.New("missing required vector_store_id parameter")
120		return
121	}
122	if batchID == "" {
123		err = errors.New("missing required batch_id parameter")
124		return
125	}
126	path := fmt.Sprintf("vector_stores/%s/file_batches/%s", vectorStoreID, batchID)
127	err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
128	return
129}
130
131// Cancel a vector store file batch. This attempts to cancel the processing of
132// files in this batch as soon as possible.
133func (r *VectorStoreFileBatchService) Cancel(ctx context.Context, vectorStoreID string, batchID string, opts ...option.RequestOption) (res *VectorStoreFileBatch, err error) {
134	opts = append(r.Options[:], opts...)
135	opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
136	if vectorStoreID == "" {
137		err = errors.New("missing required vector_store_id parameter")
138		return
139	}
140	if batchID == "" {
141		err = errors.New("missing required batch_id parameter")
142		return
143	}
144	path := fmt.Sprintf("vector_stores/%s/file_batches/%s/cancel", vectorStoreID, batchID)
145	err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, nil, &res, opts...)
146	return
147}
148
149// Returns a list of vector store files in a batch.
150func (r *VectorStoreFileBatchService) ListFiles(ctx context.Context, vectorStoreID string, batchID string, query VectorStoreFileBatchListFilesParams, opts ...option.RequestOption) (res *pagination.CursorPage[VectorStoreFile], err error) {
151	var raw *http.Response
152	opts = append(r.Options[:], opts...)
153	opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
154	if vectorStoreID == "" {
155		err = errors.New("missing required vector_store_id parameter")
156		return
157	}
158	if batchID == "" {
159		err = errors.New("missing required batch_id parameter")
160		return
161	}
162	path := fmt.Sprintf("vector_stores/%s/file_batches/%s/files", vectorStoreID, batchID)
163	cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
164	if err != nil {
165		return nil, err
166	}
167	err = cfg.Execute()
168	if err != nil {
169		return nil, err
170	}
171	res.SetPageConfig(cfg, raw)
172	return res, nil
173}
174
175// Returns a list of vector store files in a batch.
176func (r *VectorStoreFileBatchService) ListFilesAutoPaging(ctx context.Context, vectorStoreID string, batchID string, query VectorStoreFileBatchListFilesParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[VectorStoreFile] {
177	return pagination.NewCursorPageAutoPager(r.ListFiles(ctx, vectorStoreID, batchID, query, opts...))
178}
179
180// A batch of files attached to a vector store.
181type VectorStoreFileBatch struct {
182	// The identifier, which can be referenced in API endpoints.
183	ID string `json:"id,required"`
184	// The Unix timestamp (in seconds) for when the vector store files batch was
185	// created.
186	CreatedAt  int64                          `json:"created_at,required"`
187	FileCounts VectorStoreFileBatchFileCounts `json:"file_counts,required"`
188	// The object type, which is always `vector_store.file_batch`.
189	Object constant.VectorStoreFilesBatch `json:"object,required"`
190	// The status of the vector store files batch, which can be either `in_progress`,
191	// `completed`, `cancelled` or `failed`.
192	//
193	// Any of "in_progress", "completed", "cancelled", "failed".
194	Status VectorStoreFileBatchStatus `json:"status,required"`
195	// The ID of the
196	// [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object)
197	// that the [File](https://platform.openai.com/docs/api-reference/files) is
198	// attached to.
199	VectorStoreID string `json:"vector_store_id,required"`
200	// Metadata for the response, check the presence of optional fields with the
201	// [resp.Field.IsPresent] method.
202	JSON struct {
203		ID            resp.Field
204		CreatedAt     resp.Field
205		FileCounts    resp.Field
206		Object        resp.Field
207		Status        resp.Field
208		VectorStoreID resp.Field
209		ExtraFields   map[string]resp.Field
210		raw           string
211	} `json:"-"`
212}
213
214// Returns the unmodified JSON received from the API
215func (r VectorStoreFileBatch) RawJSON() string { return r.JSON.raw }
216func (r *VectorStoreFileBatch) UnmarshalJSON(data []byte) error {
217	return apijson.UnmarshalRoot(data, r)
218}
219
220type VectorStoreFileBatchFileCounts struct {
221	// The number of files that where cancelled.
222	Cancelled int64 `json:"cancelled,required"`
223	// The number of files that have been processed.
224	Completed int64 `json:"completed,required"`
225	// The number of files that have failed to process.
226	Failed int64 `json:"failed,required"`
227	// The number of files that are currently being processed.
228	InProgress int64 `json:"in_progress,required"`
229	// The total number of files.
230	Total int64 `json:"total,required"`
231	// Metadata for the response, check the presence of optional fields with the
232	// [resp.Field.IsPresent] method.
233	JSON struct {
234		Cancelled   resp.Field
235		Completed   resp.Field
236		Failed      resp.Field
237		InProgress  resp.Field
238		Total       resp.Field
239		ExtraFields map[string]resp.Field
240		raw         string
241	} `json:"-"`
242}
243
244// Returns the unmodified JSON received from the API
245func (r VectorStoreFileBatchFileCounts) RawJSON() string { return r.JSON.raw }
246func (r *VectorStoreFileBatchFileCounts) UnmarshalJSON(data []byte) error {
247	return apijson.UnmarshalRoot(data, r)
248}
249
250// The status of the vector store files batch, which can be either `in_progress`,
251// `completed`, `cancelled` or `failed`.
252type VectorStoreFileBatchStatus string
253
254const (
255	VectorStoreFileBatchStatusInProgress VectorStoreFileBatchStatus = "in_progress"
256	VectorStoreFileBatchStatusCompleted  VectorStoreFileBatchStatus = "completed"
257	VectorStoreFileBatchStatusCancelled  VectorStoreFileBatchStatus = "cancelled"
258	VectorStoreFileBatchStatusFailed     VectorStoreFileBatchStatus = "failed"
259)
260
261type VectorStoreFileBatchNewParams struct {
262	// A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
263	// the vector store should use. Useful for tools like `file_search` that can access
264	// files.
265	FileIDs []string `json:"file_ids,omitzero,required"`
266	// Set of 16 key-value pairs that can be attached to an object. This can be useful
267	// for storing additional information about the object in a structured format, and
268	// querying for objects via API or the dashboard. Keys are strings with a maximum
269	// length of 64 characters. Values are strings with a maximum length of 512
270	// characters, booleans, or numbers.
271	Attributes map[string]VectorStoreFileBatchNewParamsAttributeUnion `json:"attributes,omitzero"`
272	// The chunking strategy used to chunk the file(s). If not set, will use the `auto`
273	// strategy. Only applicable if `file_ids` is non-empty.
274	ChunkingStrategy FileChunkingStrategyParamUnion `json:"chunking_strategy,omitzero"`
275	paramObj
276}
277
278// IsPresent returns true if the field's value is not omitted and not the JSON
279// "null". To check if this field is omitted, use [param.IsOmitted].
280func (f VectorStoreFileBatchNewParams) IsPresent() bool { return !param.IsOmitted(f) && !f.IsNull() }
281
282func (r VectorStoreFileBatchNewParams) MarshalJSON() (data []byte, err error) {
283	type shadow VectorStoreFileBatchNewParams
284	return param.MarshalObject(r, (*shadow)(&r))
285}
286
287// Only one field can be non-zero.
288//
289// Use [param.IsOmitted] to confirm if a field is set.
290type VectorStoreFileBatchNewParamsAttributeUnion struct {
291	OfString param.Opt[string]  `json:",omitzero,inline"`
292	OfFloat  param.Opt[float64] `json:",omitzero,inline"`
293	OfBool   param.Opt[bool]    `json:",omitzero,inline"`
294	paramUnion
295}
296
297// IsPresent returns true if the field's value is not omitted and not the JSON
298// "null". To check if this field is omitted, use [param.IsOmitted].
299func (u VectorStoreFileBatchNewParamsAttributeUnion) IsPresent() bool {
300	return !param.IsOmitted(u) && !u.IsNull()
301}
302func (u VectorStoreFileBatchNewParamsAttributeUnion) MarshalJSON() ([]byte, error) {
303	return param.MarshalUnion[VectorStoreFileBatchNewParamsAttributeUnion](u.OfString, u.OfFloat, u.OfBool)
304}
305
306func (u *VectorStoreFileBatchNewParamsAttributeUnion) asAny() any {
307	if !param.IsOmitted(u.OfString) {
308		return &u.OfString.Value
309	} else if !param.IsOmitted(u.OfFloat) {
310		return &u.OfFloat.Value
311	} else if !param.IsOmitted(u.OfBool) {
312		return &u.OfBool.Value
313	}
314	return nil
315}
316
317type VectorStoreFileBatchListFilesParams struct {
318	// A cursor for use in pagination. `after` is an object ID that defines your place
319	// in the list. For instance, if you make a list request and receive 100 objects,
320	// ending with obj_foo, your subsequent call can include after=obj_foo in order to
321	// fetch the next page of the list.
322	After param.Opt[string] `query:"after,omitzero" json:"-"`
323	// A cursor for use in pagination. `before` is an object ID that defines your place
324	// in the list. For instance, if you make a list request and receive 100 objects,
325	// starting with obj_foo, your subsequent call can include before=obj_foo in order
326	// to fetch the previous page of the list.
327	Before param.Opt[string] `query:"before,omitzero" json:"-"`
328	// A limit on the number of objects to be returned. Limit can range between 1 and
329	// 100, and the default is 20.
330	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
331	// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
332	//
333	// Any of "in_progress", "completed", "failed", "cancelled".
334	Filter VectorStoreFileBatchListFilesParamsFilter `query:"filter,omitzero" json:"-"`
335	// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
336	// order and `desc` for descending order.
337	//
338	// Any of "asc", "desc".
339	Order VectorStoreFileBatchListFilesParamsOrder `query:"order,omitzero" json:"-"`
340	paramObj
341}
342
343// IsPresent returns true if the field's value is not omitted and not the JSON
344// "null". To check if this field is omitted, use [param.IsOmitted].
345func (f VectorStoreFileBatchListFilesParams) IsPresent() bool {
346	return !param.IsOmitted(f) && !f.IsNull()
347}
348
349// URLQuery serializes [VectorStoreFileBatchListFilesParams]'s query parameters as
350// `url.Values`.
351func (r VectorStoreFileBatchListFilesParams) URLQuery() (v url.Values) {
352	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
353		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
354		NestedFormat: apiquery.NestedQueryFormatBrackets,
355	})
356}
357
358// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
359type VectorStoreFileBatchListFilesParamsFilter string
360
361const (
362	VectorStoreFileBatchListFilesParamsFilterInProgress VectorStoreFileBatchListFilesParamsFilter = "in_progress"
363	VectorStoreFileBatchListFilesParamsFilterCompleted  VectorStoreFileBatchListFilesParamsFilter = "completed"
364	VectorStoreFileBatchListFilesParamsFilterFailed     VectorStoreFileBatchListFilesParamsFilter = "failed"
365	VectorStoreFileBatchListFilesParamsFilterCancelled  VectorStoreFileBatchListFilesParamsFilter = "cancelled"
366)
367
368// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
369// order and `desc` for descending order.
370type VectorStoreFileBatchListFilesParamsOrder string
371
372const (
373	VectorStoreFileBatchListFilesParamsOrderAsc  VectorStoreFileBatchListFilesParamsOrder = "asc"
374	VectorStoreFileBatchListFilesParamsOrderDesc VectorStoreFileBatchListFilesParamsOrder = "desc"
375)