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/respjson"
 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	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
201	JSON struct {
202		ID            respjson.Field
203		CreatedAt     respjson.Field
204		FileCounts    respjson.Field
205		Object        respjson.Field
206		Status        respjson.Field
207		VectorStoreID respjson.Field
208		ExtraFields   map[string]respjson.Field
209		raw           string
210	} `json:"-"`
211}
212
213// Returns the unmodified JSON received from the API
214func (r VectorStoreFileBatch) RawJSON() string { return r.JSON.raw }
215func (r *VectorStoreFileBatch) UnmarshalJSON(data []byte) error {
216	return apijson.UnmarshalRoot(data, r)
217}
218
219type VectorStoreFileBatchFileCounts struct {
220	// The number of files that where cancelled.
221	Cancelled int64 `json:"cancelled,required"`
222	// The number of files that have been processed.
223	Completed int64 `json:"completed,required"`
224	// The number of files that have failed to process.
225	Failed int64 `json:"failed,required"`
226	// The number of files that are currently being processed.
227	InProgress int64 `json:"in_progress,required"`
228	// The total number of files.
229	Total int64 `json:"total,required"`
230	// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
231	JSON struct {
232		Cancelled   respjson.Field
233		Completed   respjson.Field
234		Failed      respjson.Field
235		InProgress  respjson.Field
236		Total       respjson.Field
237		ExtraFields map[string]respjson.Field
238		raw         string
239	} `json:"-"`
240}
241
242// Returns the unmodified JSON received from the API
243func (r VectorStoreFileBatchFileCounts) RawJSON() string { return r.JSON.raw }
244func (r *VectorStoreFileBatchFileCounts) UnmarshalJSON(data []byte) error {
245	return apijson.UnmarshalRoot(data, r)
246}
247
248// The status of the vector store files batch, which can be either `in_progress`,
249// `completed`, `cancelled` or `failed`.
250type VectorStoreFileBatchStatus string
251
252const (
253	VectorStoreFileBatchStatusInProgress VectorStoreFileBatchStatus = "in_progress"
254	VectorStoreFileBatchStatusCompleted  VectorStoreFileBatchStatus = "completed"
255	VectorStoreFileBatchStatusCancelled  VectorStoreFileBatchStatus = "cancelled"
256	VectorStoreFileBatchStatusFailed     VectorStoreFileBatchStatus = "failed"
257)
258
259type VectorStoreFileBatchNewParams struct {
260	// A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
261	// the vector store should use. Useful for tools like `file_search` that can access
262	// files.
263	FileIDs []string `json:"file_ids,omitzero,required"`
264	// Set of 16 key-value pairs that can be attached to an object. This can be useful
265	// for storing additional information about the object in a structured format, and
266	// querying for objects via API or the dashboard. Keys are strings with a maximum
267	// length of 64 characters. Values are strings with a maximum length of 512
268	// characters, booleans, or numbers.
269	Attributes map[string]VectorStoreFileBatchNewParamsAttributeUnion `json:"attributes,omitzero"`
270	// The chunking strategy used to chunk the file(s). If not set, will use the `auto`
271	// strategy. Only applicable if `file_ids` is non-empty.
272	ChunkingStrategy FileChunkingStrategyParamUnion `json:"chunking_strategy,omitzero"`
273	paramObj
274}
275
276func (r VectorStoreFileBatchNewParams) MarshalJSON() (data []byte, err error) {
277	type shadow VectorStoreFileBatchNewParams
278	return param.MarshalObject(r, (*shadow)(&r))
279}
280func (r *VectorStoreFileBatchNewParams) UnmarshalJSON(data []byte) error {
281	return apijson.UnmarshalRoot(data, r)
282}
283
284// Only one field can be non-zero.
285//
286// Use [param.IsOmitted] to confirm if a field is set.
287type VectorStoreFileBatchNewParamsAttributeUnion struct {
288	OfString param.Opt[string]  `json:",omitzero,inline"`
289	OfFloat  param.Opt[float64] `json:",omitzero,inline"`
290	OfBool   param.Opt[bool]    `json:",omitzero,inline"`
291	paramUnion
292}
293
294func (u VectorStoreFileBatchNewParamsAttributeUnion) MarshalJSON() ([]byte, error) {
295	return param.MarshalUnion(u, u.OfString, u.OfFloat, u.OfBool)
296}
297func (u *VectorStoreFileBatchNewParamsAttributeUnion) UnmarshalJSON(data []byte) error {
298	return apijson.UnmarshalRoot(data, u)
299}
300
301func (u *VectorStoreFileBatchNewParamsAttributeUnion) asAny() any {
302	if !param.IsOmitted(u.OfString) {
303		return &u.OfString.Value
304	} else if !param.IsOmitted(u.OfFloat) {
305		return &u.OfFloat.Value
306	} else if !param.IsOmitted(u.OfBool) {
307		return &u.OfBool.Value
308	}
309	return nil
310}
311
312type VectorStoreFileBatchListFilesParams struct {
313	// A cursor for use in pagination. `after` is an object ID that defines your place
314	// in the list. For instance, if you make a list request and receive 100 objects,
315	// ending with obj_foo, your subsequent call can include after=obj_foo in order to
316	// fetch the next page of the list.
317	After param.Opt[string] `query:"after,omitzero" json:"-"`
318	// A cursor for use in pagination. `before` 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	// starting with obj_foo, your subsequent call can include before=obj_foo in order
321	// to fetch the previous page of the list.
322	Before param.Opt[string] `query:"before,omitzero" json:"-"`
323	// A limit on the number of objects to be returned. Limit can range between 1 and
324	// 100, and the default is 20.
325	Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
326	// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
327	//
328	// Any of "in_progress", "completed", "failed", "cancelled".
329	Filter VectorStoreFileBatchListFilesParamsFilter `query:"filter,omitzero" json:"-"`
330	// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
331	// order and `desc` for descending order.
332	//
333	// Any of "asc", "desc".
334	Order VectorStoreFileBatchListFilesParamsOrder `query:"order,omitzero" json:"-"`
335	paramObj
336}
337
338// URLQuery serializes [VectorStoreFileBatchListFilesParams]'s query parameters as
339// `url.Values`.
340func (r VectorStoreFileBatchListFilesParams) URLQuery() (v url.Values, err error) {
341	return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
342		ArrayFormat:  apiquery.ArrayQueryFormatBrackets,
343		NestedFormat: apiquery.NestedQueryFormatBrackets,
344	})
345}
346
347// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
348type VectorStoreFileBatchListFilesParamsFilter string
349
350const (
351	VectorStoreFileBatchListFilesParamsFilterInProgress VectorStoreFileBatchListFilesParamsFilter = "in_progress"
352	VectorStoreFileBatchListFilesParamsFilterCompleted  VectorStoreFileBatchListFilesParamsFilter = "completed"
353	VectorStoreFileBatchListFilesParamsFilterFailed     VectorStoreFileBatchListFilesParamsFilter = "failed"
354	VectorStoreFileBatchListFilesParamsFilterCancelled  VectorStoreFileBatchListFilesParamsFilter = "cancelled"
355)
356
357// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
358// order and `desc` for descending order.
359type VectorStoreFileBatchListFilesParamsOrder string
360
361const (
362	VectorStoreFileBatchListFilesParamsOrderAsc  VectorStoreFileBatchListFilesParamsOrder = "asc"
363	VectorStoreFileBatchListFilesParamsOrderDesc VectorStoreFileBatchListFilesParamsOrder = "desc"
364)