1// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
3package openai
4
5import (
6 "context"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "net/http"
11 "net/url"
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"
21 "github.com/openai/openai-go/shared/constant"
22)
23
24// VectorStoreService contains methods and other services that help with
25// interacting with the openai API.
26//
27// Note, unlike clients, this service does not read variables from the environment
28// automatically. You should not instantiate this service directly, and instead use
29// the [NewVectorStoreService] method instead.
30type VectorStoreService struct {
31 Options []option.RequestOption
32 Files VectorStoreFileService
33 FileBatches VectorStoreFileBatchService
34}
35
36// NewVectorStoreService generates a new service that applies the given options to
37// each request. These options are applied after the parent client's options (if
38// there is one), and before any request-specific options.
39func NewVectorStoreService(opts ...option.RequestOption) (r VectorStoreService) {
40 r = VectorStoreService{}
41 r.Options = opts
42 r.Files = NewVectorStoreFileService(opts...)
43 r.FileBatches = NewVectorStoreFileBatchService(opts...)
44 return
45}
46
47// Create a vector store.
48func (r *VectorStoreService) New(ctx context.Context, body VectorStoreNewParams, opts ...option.RequestOption) (res *VectorStore, err error) {
49 opts = append(r.Options[:], opts...)
50 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
51 path := "vector_stores"
52 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
53 return
54}
55
56// Retrieves a vector store.
57func (r *VectorStoreService) Get(ctx context.Context, vectorStoreID string, opts ...option.RequestOption) (res *VectorStore, err error) {
58 opts = append(r.Options[:], opts...)
59 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
60 if vectorStoreID == "" {
61 err = errors.New("missing required vector_store_id parameter")
62 return
63 }
64 path := fmt.Sprintf("vector_stores/%s", vectorStoreID)
65 err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
66 return
67}
68
69// Modifies a vector store.
70func (r *VectorStoreService) Update(ctx context.Context, vectorStoreID string, body VectorStoreUpdateParams, opts ...option.RequestOption) (res *VectorStore, err error) {
71 opts = append(r.Options[:], opts...)
72 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
73 if vectorStoreID == "" {
74 err = errors.New("missing required vector_store_id parameter")
75 return
76 }
77 path := fmt.Sprintf("vector_stores/%s", vectorStoreID)
78 err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
79 return
80}
81
82// Returns a list of vector stores.
83func (r *VectorStoreService) List(ctx context.Context, query VectorStoreListParams, opts ...option.RequestOption) (res *pagination.CursorPage[VectorStore], err error) {
84 var raw *http.Response
85 opts = append(r.Options[:], opts...)
86 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
87 path := "vector_stores"
88 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
89 if err != nil {
90 return nil, err
91 }
92 err = cfg.Execute()
93 if err != nil {
94 return nil, err
95 }
96 res.SetPageConfig(cfg, raw)
97 return res, nil
98}
99
100// Returns a list of vector stores.
101func (r *VectorStoreService) ListAutoPaging(ctx context.Context, query VectorStoreListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[VectorStore] {
102 return pagination.NewCursorPageAutoPager(r.List(ctx, query, opts...))
103}
104
105// Delete a vector store.
106func (r *VectorStoreService) Delete(ctx context.Context, vectorStoreID string, opts ...option.RequestOption) (res *VectorStoreDeleted, err error) {
107 opts = append(r.Options[:], opts...)
108 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
109 if vectorStoreID == "" {
110 err = errors.New("missing required vector_store_id parameter")
111 return
112 }
113 path := fmt.Sprintf("vector_stores/%s", vectorStoreID)
114 err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
115 return
116}
117
118// Search a vector store for relevant chunks based on a query and file attributes
119// filter.
120func (r *VectorStoreService) Search(ctx context.Context, vectorStoreID string, body VectorStoreSearchParams, opts ...option.RequestOption) (res *pagination.Page[VectorStoreSearchResponse], err error) {
121 var raw *http.Response
122 opts = append(r.Options[:], opts...)
123 opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
124 if vectorStoreID == "" {
125 err = errors.New("missing required vector_store_id parameter")
126 return
127 }
128 path := fmt.Sprintf("vector_stores/%s/search", vectorStoreID)
129 cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodPost, path, body, &res, opts...)
130 if err != nil {
131 return nil, err
132 }
133 err = cfg.Execute()
134 if err != nil {
135 return nil, err
136 }
137 res.SetPageConfig(cfg, raw)
138 return res, nil
139}
140
141// Search a vector store for relevant chunks based on a query and file attributes
142// filter.
143func (r *VectorStoreService) SearchAutoPaging(ctx context.Context, vectorStoreID string, body VectorStoreSearchParams, opts ...option.RequestOption) *pagination.PageAutoPager[VectorStoreSearchResponse] {
144 return pagination.NewPageAutoPager(r.Search(ctx, vectorStoreID, body, opts...))
145}
146
147func NewAutoFileChunkingStrategyParam() AutoFileChunkingStrategyParam {
148 return AutoFileChunkingStrategyParam{
149 Type: "auto",
150 }
151}
152
153// The default strategy. This strategy currently uses a `max_chunk_size_tokens` of
154// `800` and `chunk_overlap_tokens` of `400`.
155//
156// This struct has a constant value, construct it with
157// [NewAutoFileChunkingStrategyParam].
158type AutoFileChunkingStrategyParam struct {
159 // Always `auto`.
160 Type constant.Auto `json:"type,required"`
161 paramObj
162}
163
164func (r AutoFileChunkingStrategyParam) MarshalJSON() (data []byte, err error) {
165 type shadow AutoFileChunkingStrategyParam
166 return param.MarshalObject(r, (*shadow)(&r))
167}
168func (r *AutoFileChunkingStrategyParam) UnmarshalJSON(data []byte) error {
169 return apijson.UnmarshalRoot(data, r)
170}
171
172// FileChunkingStrategyUnion contains all possible properties and values from
173// [StaticFileChunkingStrategyObject], [OtherFileChunkingStrategyObject].
174//
175// Use the [FileChunkingStrategyUnion.AsAny] method to switch on the variant.
176//
177// Use the methods beginning with 'As' to cast the union to one of its variants.
178type FileChunkingStrategyUnion struct {
179 // This field is from variant [StaticFileChunkingStrategyObject].
180 Static StaticFileChunkingStrategy `json:"static"`
181 // Any of "static", "other".
182 Type string `json:"type"`
183 JSON struct {
184 Static respjson.Field
185 Type respjson.Field
186 raw string
187 } `json:"-"`
188}
189
190// anyFileChunkingStrategy is implemented by each variant of
191// [FileChunkingStrategyUnion] to add type safety for the return type of
192// [FileChunkingStrategyUnion.AsAny]
193type anyFileChunkingStrategy interface {
194 implFileChunkingStrategyUnion()
195}
196
197func (StaticFileChunkingStrategyObject) implFileChunkingStrategyUnion() {}
198func (OtherFileChunkingStrategyObject) implFileChunkingStrategyUnion() {}
199
200// Use the following switch statement to find the correct variant
201//
202// switch variant := FileChunkingStrategyUnion.AsAny().(type) {
203// case openai.StaticFileChunkingStrategyObject:
204// case openai.OtherFileChunkingStrategyObject:
205// default:
206// fmt.Errorf("no variant present")
207// }
208func (u FileChunkingStrategyUnion) AsAny() anyFileChunkingStrategy {
209 switch u.Type {
210 case "static":
211 return u.AsStatic()
212 case "other":
213 return u.AsOther()
214 }
215 return nil
216}
217
218func (u FileChunkingStrategyUnion) AsStatic() (v StaticFileChunkingStrategyObject) {
219 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
220 return
221}
222
223func (u FileChunkingStrategyUnion) AsOther() (v OtherFileChunkingStrategyObject) {
224 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
225 return
226}
227
228// Returns the unmodified JSON received from the API
229func (u FileChunkingStrategyUnion) RawJSON() string { return u.JSON.raw }
230
231func (r *FileChunkingStrategyUnion) UnmarshalJSON(data []byte) error {
232 return apijson.UnmarshalRoot(data, r)
233}
234
235func FileChunkingStrategyParamOfStatic(static StaticFileChunkingStrategyParam) FileChunkingStrategyParamUnion {
236 var variant StaticFileChunkingStrategyObjectParam
237 variant.Static = static
238 return FileChunkingStrategyParamUnion{OfStatic: &variant}
239}
240
241// Only one field can be non-zero.
242//
243// Use [param.IsOmitted] to confirm if a field is set.
244type FileChunkingStrategyParamUnion struct {
245 OfAuto *AutoFileChunkingStrategyParam `json:",omitzero,inline"`
246 OfStatic *StaticFileChunkingStrategyObjectParam `json:",omitzero,inline"`
247 paramUnion
248}
249
250func (u FileChunkingStrategyParamUnion) MarshalJSON() ([]byte, error) {
251 return param.MarshalUnion(u, u.OfAuto, u.OfStatic)
252}
253func (u *FileChunkingStrategyParamUnion) UnmarshalJSON(data []byte) error {
254 return apijson.UnmarshalRoot(data, u)
255}
256
257func (u *FileChunkingStrategyParamUnion) asAny() any {
258 if !param.IsOmitted(u.OfAuto) {
259 return u.OfAuto
260 } else if !param.IsOmitted(u.OfStatic) {
261 return u.OfStatic
262 }
263 return nil
264}
265
266// Returns a pointer to the underlying variant's property, if present.
267func (u FileChunkingStrategyParamUnion) GetStatic() *StaticFileChunkingStrategyParam {
268 if vt := u.OfStatic; vt != nil {
269 return &vt.Static
270 }
271 return nil
272}
273
274// Returns a pointer to the underlying variant's property, if present.
275func (u FileChunkingStrategyParamUnion) GetType() *string {
276 if vt := u.OfAuto; vt != nil {
277 return (*string)(&vt.Type)
278 } else if vt := u.OfStatic; vt != nil {
279 return (*string)(&vt.Type)
280 }
281 return nil
282}
283
284func init() {
285 apijson.RegisterUnion[FileChunkingStrategyParamUnion](
286 "type",
287 apijson.Discriminator[AutoFileChunkingStrategyParam]("auto"),
288 apijson.Discriminator[StaticFileChunkingStrategyObjectParam]("static"),
289 )
290}
291
292// This is returned when the chunking strategy is unknown. Typically, this is
293// because the file was indexed before the `chunking_strategy` concept was
294// introduced in the API.
295type OtherFileChunkingStrategyObject struct {
296 // Always `other`.
297 Type constant.Other `json:"type,required"`
298 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
299 JSON struct {
300 Type respjson.Field
301 ExtraFields map[string]respjson.Field
302 raw string
303 } `json:"-"`
304}
305
306// Returns the unmodified JSON received from the API
307func (r OtherFileChunkingStrategyObject) RawJSON() string { return r.JSON.raw }
308func (r *OtherFileChunkingStrategyObject) UnmarshalJSON(data []byte) error {
309 return apijson.UnmarshalRoot(data, r)
310}
311
312type StaticFileChunkingStrategy struct {
313 // The number of tokens that overlap between chunks. The default value is `400`.
314 //
315 // Note that the overlap must not exceed half of `max_chunk_size_tokens`.
316 ChunkOverlapTokens int64 `json:"chunk_overlap_tokens,required"`
317 // The maximum number of tokens in each chunk. The default value is `800`. The
318 // minimum value is `100` and the maximum value is `4096`.
319 MaxChunkSizeTokens int64 `json:"max_chunk_size_tokens,required"`
320 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
321 JSON struct {
322 ChunkOverlapTokens respjson.Field
323 MaxChunkSizeTokens respjson.Field
324 ExtraFields map[string]respjson.Field
325 raw string
326 } `json:"-"`
327}
328
329// Returns the unmodified JSON received from the API
330func (r StaticFileChunkingStrategy) RawJSON() string { return r.JSON.raw }
331func (r *StaticFileChunkingStrategy) UnmarshalJSON(data []byte) error {
332 return apijson.UnmarshalRoot(data, r)
333}
334
335// ToParam converts this StaticFileChunkingStrategy to a
336// StaticFileChunkingStrategyParam.
337//
338// Warning: the fields of the param type will not be present. ToParam should only
339// be used at the last possible moment before sending a request. Test for this with
340// StaticFileChunkingStrategyParam.Overrides()
341func (r StaticFileChunkingStrategy) ToParam() StaticFileChunkingStrategyParam {
342 return param.Override[StaticFileChunkingStrategyParam](json.RawMessage(r.RawJSON()))
343}
344
345// The properties ChunkOverlapTokens, MaxChunkSizeTokens are required.
346type StaticFileChunkingStrategyParam struct {
347 // The number of tokens that overlap between chunks. The default value is `400`.
348 //
349 // Note that the overlap must not exceed half of `max_chunk_size_tokens`.
350 ChunkOverlapTokens int64 `json:"chunk_overlap_tokens,required"`
351 // The maximum number of tokens in each chunk. The default value is `800`. The
352 // minimum value is `100` and the maximum value is `4096`.
353 MaxChunkSizeTokens int64 `json:"max_chunk_size_tokens,required"`
354 paramObj
355}
356
357func (r StaticFileChunkingStrategyParam) MarshalJSON() (data []byte, err error) {
358 type shadow StaticFileChunkingStrategyParam
359 return param.MarshalObject(r, (*shadow)(&r))
360}
361func (r *StaticFileChunkingStrategyParam) UnmarshalJSON(data []byte) error {
362 return apijson.UnmarshalRoot(data, r)
363}
364
365type StaticFileChunkingStrategyObject struct {
366 Static StaticFileChunkingStrategy `json:"static,required"`
367 // Always `static`.
368 Type constant.Static `json:"type,required"`
369 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
370 JSON struct {
371 Static respjson.Field
372 Type respjson.Field
373 ExtraFields map[string]respjson.Field
374 raw string
375 } `json:"-"`
376}
377
378// Returns the unmodified JSON received from the API
379func (r StaticFileChunkingStrategyObject) RawJSON() string { return r.JSON.raw }
380func (r *StaticFileChunkingStrategyObject) UnmarshalJSON(data []byte) error {
381 return apijson.UnmarshalRoot(data, r)
382}
383
384// Customize your own chunking strategy by setting chunk size and chunk overlap.
385//
386// The properties Static, Type are required.
387type StaticFileChunkingStrategyObjectParam struct {
388 Static StaticFileChunkingStrategyParam `json:"static,omitzero,required"`
389 // Always `static`.
390 //
391 // This field can be elided, and will marshal its zero value as "static".
392 Type constant.Static `json:"type,required"`
393 paramObj
394}
395
396func (r StaticFileChunkingStrategyObjectParam) MarshalJSON() (data []byte, err error) {
397 type shadow StaticFileChunkingStrategyObjectParam
398 return param.MarshalObject(r, (*shadow)(&r))
399}
400func (r *StaticFileChunkingStrategyObjectParam) UnmarshalJSON(data []byte) error {
401 return apijson.UnmarshalRoot(data, r)
402}
403
404// A vector store is a collection of processed files can be used by the
405// `file_search` tool.
406type VectorStore struct {
407 // The identifier, which can be referenced in API endpoints.
408 ID string `json:"id,required"`
409 // The Unix timestamp (in seconds) for when the vector store was created.
410 CreatedAt int64 `json:"created_at,required"`
411 FileCounts VectorStoreFileCounts `json:"file_counts,required"`
412 // The Unix timestamp (in seconds) for when the vector store was last active.
413 LastActiveAt int64 `json:"last_active_at,required"`
414 // Set of 16 key-value pairs that can be attached to an object. This can be useful
415 // for storing additional information about the object in a structured format, and
416 // querying for objects via API or the dashboard.
417 //
418 // Keys are strings with a maximum length of 64 characters. Values are strings with
419 // a maximum length of 512 characters.
420 Metadata shared.Metadata `json:"metadata,required"`
421 // The name of the vector store.
422 Name string `json:"name,required"`
423 // The object type, which is always `vector_store`.
424 Object constant.VectorStore `json:"object,required"`
425 // The status of the vector store, which can be either `expired`, `in_progress`, or
426 // `completed`. A status of `completed` indicates that the vector store is ready
427 // for use.
428 //
429 // Any of "expired", "in_progress", "completed".
430 Status VectorStoreStatus `json:"status,required"`
431 // The total number of bytes used by the files in the vector store.
432 UsageBytes int64 `json:"usage_bytes,required"`
433 // The expiration policy for a vector store.
434 ExpiresAfter VectorStoreExpiresAfter `json:"expires_after"`
435 // The Unix timestamp (in seconds) for when the vector store will expire.
436 ExpiresAt int64 `json:"expires_at,nullable"`
437 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
438 JSON struct {
439 ID respjson.Field
440 CreatedAt respjson.Field
441 FileCounts respjson.Field
442 LastActiveAt respjson.Field
443 Metadata respjson.Field
444 Name respjson.Field
445 Object respjson.Field
446 Status respjson.Field
447 UsageBytes respjson.Field
448 ExpiresAfter respjson.Field
449 ExpiresAt respjson.Field
450 ExtraFields map[string]respjson.Field
451 raw string
452 } `json:"-"`
453}
454
455// Returns the unmodified JSON received from the API
456func (r VectorStore) RawJSON() string { return r.JSON.raw }
457func (r *VectorStore) UnmarshalJSON(data []byte) error {
458 return apijson.UnmarshalRoot(data, r)
459}
460
461type VectorStoreFileCounts struct {
462 // The number of files that were cancelled.
463 Cancelled int64 `json:"cancelled,required"`
464 // The number of files that have been successfully processed.
465 Completed int64 `json:"completed,required"`
466 // The number of files that have failed to process.
467 Failed int64 `json:"failed,required"`
468 // The number of files that are currently being processed.
469 InProgress int64 `json:"in_progress,required"`
470 // The total number of files.
471 Total int64 `json:"total,required"`
472 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
473 JSON struct {
474 Cancelled respjson.Field
475 Completed respjson.Field
476 Failed respjson.Field
477 InProgress respjson.Field
478 Total respjson.Field
479 ExtraFields map[string]respjson.Field
480 raw string
481 } `json:"-"`
482}
483
484// Returns the unmodified JSON received from the API
485func (r VectorStoreFileCounts) RawJSON() string { return r.JSON.raw }
486func (r *VectorStoreFileCounts) UnmarshalJSON(data []byte) error {
487 return apijson.UnmarshalRoot(data, r)
488}
489
490// The status of the vector store, which can be either `expired`, `in_progress`, or
491// `completed`. A status of `completed` indicates that the vector store is ready
492// for use.
493type VectorStoreStatus string
494
495const (
496 VectorStoreStatusExpired VectorStoreStatus = "expired"
497 VectorStoreStatusInProgress VectorStoreStatus = "in_progress"
498 VectorStoreStatusCompleted VectorStoreStatus = "completed"
499)
500
501// The expiration policy for a vector store.
502type VectorStoreExpiresAfter struct {
503 // Anchor timestamp after which the expiration policy applies. Supported anchors:
504 // `last_active_at`.
505 Anchor constant.LastActiveAt `json:"anchor,required"`
506 // The number of days after the anchor time that the vector store will expire.
507 Days int64 `json:"days,required"`
508 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
509 JSON struct {
510 Anchor respjson.Field
511 Days respjson.Field
512 ExtraFields map[string]respjson.Field
513 raw string
514 } `json:"-"`
515}
516
517// Returns the unmodified JSON received from the API
518func (r VectorStoreExpiresAfter) RawJSON() string { return r.JSON.raw }
519func (r *VectorStoreExpiresAfter) UnmarshalJSON(data []byte) error {
520 return apijson.UnmarshalRoot(data, r)
521}
522
523type VectorStoreDeleted struct {
524 ID string `json:"id,required"`
525 Deleted bool `json:"deleted,required"`
526 Object constant.VectorStoreDeleted `json:"object,required"`
527 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
528 JSON struct {
529 ID respjson.Field
530 Deleted respjson.Field
531 Object respjson.Field
532 ExtraFields map[string]respjson.Field
533 raw string
534 } `json:"-"`
535}
536
537// Returns the unmodified JSON received from the API
538func (r VectorStoreDeleted) RawJSON() string { return r.JSON.raw }
539func (r *VectorStoreDeleted) UnmarshalJSON(data []byte) error {
540 return apijson.UnmarshalRoot(data, r)
541}
542
543type VectorStoreSearchResponse struct {
544 // Set of 16 key-value pairs that can be attached to an object. This can be useful
545 // for storing additional information about the object in a structured format, and
546 // querying for objects via API or the dashboard. Keys are strings with a maximum
547 // length of 64 characters. Values are strings with a maximum length of 512
548 // characters, booleans, or numbers.
549 Attributes map[string]VectorStoreSearchResponseAttributeUnion `json:"attributes,required"`
550 // Content chunks from the file.
551 Content []VectorStoreSearchResponseContent `json:"content,required"`
552 // The ID of the vector store file.
553 FileID string `json:"file_id,required"`
554 // The name of the vector store file.
555 Filename string `json:"filename,required"`
556 // The similarity score for the result.
557 Score float64 `json:"score,required"`
558 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
559 JSON struct {
560 Attributes respjson.Field
561 Content respjson.Field
562 FileID respjson.Field
563 Filename respjson.Field
564 Score respjson.Field
565 ExtraFields map[string]respjson.Field
566 raw string
567 } `json:"-"`
568}
569
570// Returns the unmodified JSON received from the API
571func (r VectorStoreSearchResponse) RawJSON() string { return r.JSON.raw }
572func (r *VectorStoreSearchResponse) UnmarshalJSON(data []byte) error {
573 return apijson.UnmarshalRoot(data, r)
574}
575
576// VectorStoreSearchResponseAttributeUnion contains all possible properties and
577// values from [string], [float64], [bool].
578//
579// Use the methods beginning with 'As' to cast the union to one of its variants.
580//
581// If the underlying value is not a json object, one of the following properties
582// will be valid: OfString OfFloat OfBool]
583type VectorStoreSearchResponseAttributeUnion struct {
584 // This field will be present if the value is a [string] instead of an object.
585 OfString string `json:",inline"`
586 // This field will be present if the value is a [float64] instead of an object.
587 OfFloat float64 `json:",inline"`
588 // This field will be present if the value is a [bool] instead of an object.
589 OfBool bool `json:",inline"`
590 JSON struct {
591 OfString respjson.Field
592 OfFloat respjson.Field
593 OfBool respjson.Field
594 raw string
595 } `json:"-"`
596}
597
598func (u VectorStoreSearchResponseAttributeUnion) AsString() (v string) {
599 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
600 return
601}
602
603func (u VectorStoreSearchResponseAttributeUnion) AsFloat() (v float64) {
604 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
605 return
606}
607
608func (u VectorStoreSearchResponseAttributeUnion) AsBool() (v bool) {
609 apijson.UnmarshalRoot(json.RawMessage(u.JSON.raw), &v)
610 return
611}
612
613// Returns the unmodified JSON received from the API
614func (u VectorStoreSearchResponseAttributeUnion) RawJSON() string { return u.JSON.raw }
615
616func (r *VectorStoreSearchResponseAttributeUnion) UnmarshalJSON(data []byte) error {
617 return apijson.UnmarshalRoot(data, r)
618}
619
620type VectorStoreSearchResponseContent struct {
621 // The text content returned from search.
622 Text string `json:"text,required"`
623 // The type of content.
624 //
625 // Any of "text".
626 Type string `json:"type,required"`
627 // JSON contains metadata for fields, check presence with [respjson.Field.Valid].
628 JSON struct {
629 Text respjson.Field
630 Type respjson.Field
631 ExtraFields map[string]respjson.Field
632 raw string
633 } `json:"-"`
634}
635
636// Returns the unmodified JSON received from the API
637func (r VectorStoreSearchResponseContent) RawJSON() string { return r.JSON.raw }
638func (r *VectorStoreSearchResponseContent) UnmarshalJSON(data []byte) error {
639 return apijson.UnmarshalRoot(data, r)
640}
641
642type VectorStoreNewParams struct {
643 // The name of the vector store.
644 Name param.Opt[string] `json:"name,omitzero"`
645 // Set of 16 key-value pairs that can be attached to an object. This can be useful
646 // for storing additional information about the object in a structured format, and
647 // querying for objects via API or the dashboard.
648 //
649 // Keys are strings with a maximum length of 64 characters. Values are strings with
650 // a maximum length of 512 characters.
651 Metadata shared.Metadata `json:"metadata,omitzero"`
652 // The chunking strategy used to chunk the file(s). If not set, will use the `auto`
653 // strategy. Only applicable if `file_ids` is non-empty.
654 ChunkingStrategy FileChunkingStrategyParamUnion `json:"chunking_strategy,omitzero"`
655 // The expiration policy for a vector store.
656 ExpiresAfter VectorStoreNewParamsExpiresAfter `json:"expires_after,omitzero"`
657 // A list of [File](https://platform.openai.com/docs/api-reference/files) IDs that
658 // the vector store should use. Useful for tools like `file_search` that can access
659 // files.
660 FileIDs []string `json:"file_ids,omitzero"`
661 paramObj
662}
663
664func (r VectorStoreNewParams) MarshalJSON() (data []byte, err error) {
665 type shadow VectorStoreNewParams
666 return param.MarshalObject(r, (*shadow)(&r))
667}
668func (r *VectorStoreNewParams) UnmarshalJSON(data []byte) error {
669 return apijson.UnmarshalRoot(data, r)
670}
671
672// The expiration policy for a vector store.
673//
674// The properties Anchor, Days are required.
675type VectorStoreNewParamsExpiresAfter struct {
676 // The number of days after the anchor time that the vector store will expire.
677 Days int64 `json:"days,required"`
678 // Anchor timestamp after which the expiration policy applies. Supported anchors:
679 // `last_active_at`.
680 //
681 // This field can be elided, and will marshal its zero value as "last_active_at".
682 Anchor constant.LastActiveAt `json:"anchor,required"`
683 paramObj
684}
685
686func (r VectorStoreNewParamsExpiresAfter) MarshalJSON() (data []byte, err error) {
687 type shadow VectorStoreNewParamsExpiresAfter
688 return param.MarshalObject(r, (*shadow)(&r))
689}
690func (r *VectorStoreNewParamsExpiresAfter) UnmarshalJSON(data []byte) error {
691 return apijson.UnmarshalRoot(data, r)
692}
693
694type VectorStoreUpdateParams struct {
695 // The name of the vector store.
696 Name param.Opt[string] `json:"name,omitzero"`
697 // The expiration policy for a vector store.
698 ExpiresAfter VectorStoreUpdateParamsExpiresAfter `json:"expires_after,omitzero"`
699 // Set of 16 key-value pairs that can be attached to an object. This can be useful
700 // for storing additional information about the object in a structured format, and
701 // querying for objects via API or the dashboard.
702 //
703 // Keys are strings with a maximum length of 64 characters. Values are strings with
704 // a maximum length of 512 characters.
705 Metadata shared.Metadata `json:"metadata,omitzero"`
706 paramObj
707}
708
709func (r VectorStoreUpdateParams) MarshalJSON() (data []byte, err error) {
710 type shadow VectorStoreUpdateParams
711 return param.MarshalObject(r, (*shadow)(&r))
712}
713func (r *VectorStoreUpdateParams) UnmarshalJSON(data []byte) error {
714 return apijson.UnmarshalRoot(data, r)
715}
716
717// The expiration policy for a vector store.
718//
719// The properties Anchor, Days are required.
720type VectorStoreUpdateParamsExpiresAfter struct {
721 // The number of days after the anchor time that the vector store will expire.
722 Days int64 `json:"days,required"`
723 // Anchor timestamp after which the expiration policy applies. Supported anchors:
724 // `last_active_at`.
725 //
726 // This field can be elided, and will marshal its zero value as "last_active_at".
727 Anchor constant.LastActiveAt `json:"anchor,required"`
728 paramObj
729}
730
731func (r VectorStoreUpdateParamsExpiresAfter) MarshalJSON() (data []byte, err error) {
732 type shadow VectorStoreUpdateParamsExpiresAfter
733 return param.MarshalObject(r, (*shadow)(&r))
734}
735func (r *VectorStoreUpdateParamsExpiresAfter) UnmarshalJSON(data []byte) error {
736 return apijson.UnmarshalRoot(data, r)
737}
738
739type VectorStoreListParams struct {
740 // A cursor for use in pagination. `after` is an object ID that defines your place
741 // in the list. For instance, if you make a list request and receive 100 objects,
742 // ending with obj_foo, your subsequent call can include after=obj_foo in order to
743 // fetch the next page of the list.
744 After param.Opt[string] `query:"after,omitzero" json:"-"`
745 // A cursor for use in pagination. `before` is an object ID that defines your place
746 // in the list. For instance, if you make a list request and receive 100 objects,
747 // starting with obj_foo, your subsequent call can include before=obj_foo in order
748 // to fetch the previous page of the list.
749 Before param.Opt[string] `query:"before,omitzero" json:"-"`
750 // A limit on the number of objects to be returned. Limit can range between 1 and
751 // 100, and the default is 20.
752 Limit param.Opt[int64] `query:"limit,omitzero" json:"-"`
753 // Sort order by the `created_at` timestamp of the objects. `asc` for ascending
754 // order and `desc` for descending order.
755 //
756 // Any of "asc", "desc".
757 Order VectorStoreListParamsOrder `query:"order,omitzero" json:"-"`
758 paramObj
759}
760
761// URLQuery serializes [VectorStoreListParams]'s query parameters as `url.Values`.
762func (r VectorStoreListParams) URLQuery() (v url.Values, err error) {
763 return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
764 ArrayFormat: apiquery.ArrayQueryFormatBrackets,
765 NestedFormat: apiquery.NestedQueryFormatBrackets,
766 })
767}
768
769// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
770// order and `desc` for descending order.
771type VectorStoreListParamsOrder string
772
773const (
774 VectorStoreListParamsOrderAsc VectorStoreListParamsOrder = "asc"
775 VectorStoreListParamsOrderDesc VectorStoreListParamsOrder = "desc"
776)
777
778type VectorStoreSearchParams struct {
779 // A query string for a search
780 Query VectorStoreSearchParamsQueryUnion `json:"query,omitzero,required"`
781 // The maximum number of results to return. This number should be between 1 and 50
782 // inclusive.
783 MaxNumResults param.Opt[int64] `json:"max_num_results,omitzero"`
784 // Whether to rewrite the natural language query for vector search.
785 RewriteQuery param.Opt[bool] `json:"rewrite_query,omitzero"`
786 // A filter to apply based on file attributes.
787 Filters VectorStoreSearchParamsFiltersUnion `json:"filters,omitzero"`
788 // Ranking options for search.
789 RankingOptions VectorStoreSearchParamsRankingOptions `json:"ranking_options,omitzero"`
790 paramObj
791}
792
793func (r VectorStoreSearchParams) MarshalJSON() (data []byte, err error) {
794 type shadow VectorStoreSearchParams
795 return param.MarshalObject(r, (*shadow)(&r))
796}
797func (r *VectorStoreSearchParams) UnmarshalJSON(data []byte) error {
798 return apijson.UnmarshalRoot(data, r)
799}
800
801// Only one field can be non-zero.
802//
803// Use [param.IsOmitted] to confirm if a field is set.
804type VectorStoreSearchParamsQueryUnion struct {
805 OfString param.Opt[string] `json:",omitzero,inline"`
806 OfStringArray []string `json:",omitzero,inline"`
807 paramUnion
808}
809
810func (u VectorStoreSearchParamsQueryUnion) MarshalJSON() ([]byte, error) {
811 return param.MarshalUnion(u, u.OfString, u.OfStringArray)
812}
813func (u *VectorStoreSearchParamsQueryUnion) UnmarshalJSON(data []byte) error {
814 return apijson.UnmarshalRoot(data, u)
815}
816
817func (u *VectorStoreSearchParamsQueryUnion) asAny() any {
818 if !param.IsOmitted(u.OfString) {
819 return &u.OfString.Value
820 } else if !param.IsOmitted(u.OfStringArray) {
821 return &u.OfStringArray
822 }
823 return nil
824}
825
826// Only one field can be non-zero.
827//
828// Use [param.IsOmitted] to confirm if a field is set.
829type VectorStoreSearchParamsFiltersUnion struct {
830 OfComparisonFilter *shared.ComparisonFilterParam `json:",omitzero,inline"`
831 OfCompoundFilter *shared.CompoundFilterParam `json:",omitzero,inline"`
832 paramUnion
833}
834
835func (u VectorStoreSearchParamsFiltersUnion) MarshalJSON() ([]byte, error) {
836 return param.MarshalUnion(u, u.OfComparisonFilter, u.OfCompoundFilter)
837}
838func (u *VectorStoreSearchParamsFiltersUnion) UnmarshalJSON(data []byte) error {
839 return apijson.UnmarshalRoot(data, u)
840}
841
842func (u *VectorStoreSearchParamsFiltersUnion) asAny() any {
843 if !param.IsOmitted(u.OfComparisonFilter) {
844 return u.OfComparisonFilter
845 } else if !param.IsOmitted(u.OfCompoundFilter) {
846 return u.OfCompoundFilter
847 }
848 return nil
849}
850
851// Returns a pointer to the underlying variant's property, if present.
852func (u VectorStoreSearchParamsFiltersUnion) GetKey() *string {
853 if vt := u.OfComparisonFilter; vt != nil {
854 return &vt.Key
855 }
856 return nil
857}
858
859// Returns a pointer to the underlying variant's property, if present.
860func (u VectorStoreSearchParamsFiltersUnion) GetValue() *shared.ComparisonFilterValueUnionParam {
861 if vt := u.OfComparisonFilter; vt != nil {
862 return &vt.Value
863 }
864 return nil
865}
866
867// Returns a pointer to the underlying variant's property, if present.
868func (u VectorStoreSearchParamsFiltersUnion) GetFilters() []shared.ComparisonFilterParam {
869 if vt := u.OfCompoundFilter; vt != nil {
870 return vt.Filters
871 }
872 return nil
873}
874
875// Returns a pointer to the underlying variant's property, if present.
876func (u VectorStoreSearchParamsFiltersUnion) GetType() *string {
877 if vt := u.OfComparisonFilter; vt != nil {
878 return (*string)(&vt.Type)
879 } else if vt := u.OfCompoundFilter; vt != nil {
880 return (*string)(&vt.Type)
881 }
882 return nil
883}
884
885// Ranking options for search.
886type VectorStoreSearchParamsRankingOptions struct {
887 ScoreThreshold param.Opt[float64] `json:"score_threshold,omitzero"`
888 // Any of "auto", "default-2024-11-15".
889 Ranker string `json:"ranker,omitzero"`
890 paramObj
891}
892
893func (r VectorStoreSearchParamsRankingOptions) MarshalJSON() (data []byte, err error) {
894 type shadow VectorStoreSearchParamsRankingOptions
895 return param.MarshalObject(r, (*shadow)(&r))
896}
897func (r *VectorStoreSearchParamsRankingOptions) UnmarshalJSON(data []byte) error {
898 return apijson.UnmarshalRoot(data, r)
899}
900
901func init() {
902 apijson.RegisterFieldValidator[VectorStoreSearchParamsRankingOptions](
903 "ranker", "auto", "default-2024-11-15",
904 )
905}