mirror of
https://github.com/openai/openai-go.git
synced 2026-04-01 17:17:14 +09:00
505 lines
19 KiB
Go
505 lines
19 KiB
Go
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
|
|
package openai
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"reflect"
|
|
|
|
"github.com/openai/openai-go/internal/apijson"
|
|
"github.com/openai/openai-go/internal/apiquery"
|
|
"github.com/openai/openai-go/internal/param"
|
|
"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/shared"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// VectorStoreFileService 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 [NewVectorStoreFileService] method instead.
|
|
type VectorStoreFileService struct {
|
|
Options []option.RequestOption
|
|
}
|
|
|
|
// NewVectorStoreFileService 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.
|
|
func NewVectorStoreFileService(opts ...option.RequestOption) (r *VectorStoreFileService) {
|
|
r = &VectorStoreFileService{}
|
|
r.Options = opts
|
|
return
|
|
}
|
|
|
|
// Create a vector store file by attaching a
|
|
// [File](https://platform.openai.com/docs/api-reference/files) to a
|
|
// [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
|
|
func (r *VectorStoreFileService) New(ctx context.Context, vectorStoreID string, body VectorStoreFileNewParams, opts ...option.RequestOption) (res *VectorStoreFile, err error) {
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files", vectorStoreID)
|
|
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
|
|
return
|
|
}
|
|
|
|
// Retrieves a vector store file.
|
|
func (r *VectorStoreFileService) Get(ctx context.Context, vectorStoreID string, fileID string, opts ...option.RequestOption) (res *VectorStoreFile, err error) {
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
if fileID == "" {
|
|
err = errors.New("missing required file_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files/%s", vectorStoreID, fileID)
|
|
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
|
|
return
|
|
}
|
|
|
|
// Update attributes on a vector store file.
|
|
func (r *VectorStoreFileService) Update(ctx context.Context, vectorStoreID string, fileID string, body VectorStoreFileUpdateParams, opts ...option.RequestOption) (res *VectorStoreFile, err error) {
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
if fileID == "" {
|
|
err = errors.New("missing required file_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files/%s", vectorStoreID, fileID)
|
|
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
|
|
return
|
|
}
|
|
|
|
// Returns a list of vector store files.
|
|
func (r *VectorStoreFileService) List(ctx context.Context, vectorStoreID string, query VectorStoreFileListParams, opts ...option.RequestOption) (res *pagination.CursorPage[VectorStoreFile], err error) {
|
|
var raw *http.Response
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files", vectorStoreID)
|
|
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = cfg.Execute()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res.SetPageConfig(cfg, raw)
|
|
return res, nil
|
|
}
|
|
|
|
// Returns a list of vector store files.
|
|
func (r *VectorStoreFileService) ListAutoPaging(ctx context.Context, vectorStoreID string, query VectorStoreFileListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[VectorStoreFile] {
|
|
return pagination.NewCursorPageAutoPager(r.List(ctx, vectorStoreID, query, opts...))
|
|
}
|
|
|
|
// Delete a vector store file. This will remove the file from the vector store but
|
|
// the file itself will not be deleted. To delete the file, use the
|
|
// [delete file](https://platform.openai.com/docs/api-reference/files/delete)
|
|
// endpoint.
|
|
func (r *VectorStoreFileService) Delete(ctx context.Context, vectorStoreID string, fileID string, opts ...option.RequestOption) (res *VectorStoreFileDeleted, err error) {
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2")}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
if fileID == "" {
|
|
err = errors.New("missing required file_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files/%s", vectorStoreID, fileID)
|
|
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
|
|
return
|
|
}
|
|
|
|
// Retrieve the parsed contents of a vector store file.
|
|
func (r *VectorStoreFileService) Content(ctx context.Context, vectorStoreID string, fileID string, opts ...option.RequestOption) (res *pagination.Page[VectorStoreFileContentResponse], err error) {
|
|
var raw *http.Response
|
|
opts = append(r.Options[:], opts...)
|
|
opts = append([]option.RequestOption{option.WithHeader("OpenAI-Beta", "assistants=v2"), option.WithResponseInto(&raw)}, opts...)
|
|
if vectorStoreID == "" {
|
|
err = errors.New("missing required vector_store_id parameter")
|
|
return
|
|
}
|
|
if fileID == "" {
|
|
err = errors.New("missing required file_id parameter")
|
|
return
|
|
}
|
|
path := fmt.Sprintf("vector_stores/%s/files/%s/content", vectorStoreID, fileID)
|
|
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, nil, &res, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = cfg.Execute()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res.SetPageConfig(cfg, raw)
|
|
return res, nil
|
|
}
|
|
|
|
// Retrieve the parsed contents of a vector store file.
|
|
func (r *VectorStoreFileService) ContentAutoPaging(ctx context.Context, vectorStoreID string, fileID string, opts ...option.RequestOption) *pagination.PageAutoPager[VectorStoreFileContentResponse] {
|
|
return pagination.NewPageAutoPager(r.Content(ctx, vectorStoreID, fileID, opts...))
|
|
}
|
|
|
|
// A list of files attached to a vector store.
|
|
type VectorStoreFile struct {
|
|
// The identifier, which can be referenced in API endpoints.
|
|
ID string `json:"id,required"`
|
|
// The Unix timestamp (in seconds) for when the vector store file was created.
|
|
CreatedAt int64 `json:"created_at,required"`
|
|
// The last error associated with this vector store file. Will be `null` if there
|
|
// are no errors.
|
|
LastError VectorStoreFileLastError `json:"last_error,required,nullable"`
|
|
// The object type, which is always `vector_store.file`.
|
|
Object VectorStoreFileObject `json:"object,required"`
|
|
// The status of the vector store file, which can be either `in_progress`,
|
|
// `completed`, `cancelled`, or `failed`. The status `completed` indicates that the
|
|
// vector store file is ready for use.
|
|
Status VectorStoreFileStatus `json:"status,required"`
|
|
// The total vector store usage in bytes. Note that this may be different from the
|
|
// original file size.
|
|
UsageBytes int64 `json:"usage_bytes,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.
|
|
VectorStoreID string `json:"vector_store_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, booleans, or numbers.
|
|
Attributes map[string]VectorStoreFileAttributesUnion `json:"attributes,nullable"`
|
|
// The strategy used to chunk the file.
|
|
ChunkingStrategy FileChunkingStrategy `json:"chunking_strategy"`
|
|
JSON vectorStoreFileJSON `json:"-"`
|
|
}
|
|
|
|
// vectorStoreFileJSON contains the JSON metadata for the struct [VectorStoreFile]
|
|
type vectorStoreFileJSON struct {
|
|
ID apijson.Field
|
|
CreatedAt apijson.Field
|
|
LastError apijson.Field
|
|
Object apijson.Field
|
|
Status apijson.Field
|
|
UsageBytes apijson.Field
|
|
VectorStoreID apijson.Field
|
|
Attributes apijson.Field
|
|
ChunkingStrategy apijson.Field
|
|
raw string
|
|
ExtraFields map[string]apijson.Field
|
|
}
|
|
|
|
func (r *VectorStoreFile) UnmarshalJSON(data []byte) (err error) {
|
|
return apijson.UnmarshalRoot(data, r)
|
|
}
|
|
|
|
func (r vectorStoreFileJSON) RawJSON() string {
|
|
return r.raw
|
|
}
|
|
|
|
// The last error associated with this vector store file. Will be `null` if there
|
|
// are no errors.
|
|
type VectorStoreFileLastError struct {
|
|
// One of `server_error` or `rate_limit_exceeded`.
|
|
Code VectorStoreFileLastErrorCode `json:"code,required"`
|
|
// A human-readable description of the error.
|
|
Message string `json:"message,required"`
|
|
JSON vectorStoreFileLastErrorJSON `json:"-"`
|
|
}
|
|
|
|
// vectorStoreFileLastErrorJSON contains the JSON metadata for the struct
|
|
// [VectorStoreFileLastError]
|
|
type vectorStoreFileLastErrorJSON struct {
|
|
Code apijson.Field
|
|
Message apijson.Field
|
|
raw string
|
|
ExtraFields map[string]apijson.Field
|
|
}
|
|
|
|
func (r *VectorStoreFileLastError) UnmarshalJSON(data []byte) (err error) {
|
|
return apijson.UnmarshalRoot(data, r)
|
|
}
|
|
|
|
func (r vectorStoreFileLastErrorJSON) RawJSON() string {
|
|
return r.raw
|
|
}
|
|
|
|
// One of `server_error` or `rate_limit_exceeded`.
|
|
type VectorStoreFileLastErrorCode string
|
|
|
|
const (
|
|
VectorStoreFileLastErrorCodeServerError VectorStoreFileLastErrorCode = "server_error"
|
|
VectorStoreFileLastErrorCodeUnsupportedFile VectorStoreFileLastErrorCode = "unsupported_file"
|
|
VectorStoreFileLastErrorCodeInvalidFile VectorStoreFileLastErrorCode = "invalid_file"
|
|
)
|
|
|
|
func (r VectorStoreFileLastErrorCode) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileLastErrorCodeServerError, VectorStoreFileLastErrorCodeUnsupportedFile, VectorStoreFileLastErrorCodeInvalidFile:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The object type, which is always `vector_store.file`.
|
|
type VectorStoreFileObject string
|
|
|
|
const (
|
|
VectorStoreFileObjectVectorStoreFile VectorStoreFileObject = "vector_store.file"
|
|
)
|
|
|
|
func (r VectorStoreFileObject) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileObjectVectorStoreFile:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// The status of the vector store file, which can be either `in_progress`,
|
|
// `completed`, `cancelled`, or `failed`. The status `completed` indicates that the
|
|
// vector store file is ready for use.
|
|
type VectorStoreFileStatus string
|
|
|
|
const (
|
|
VectorStoreFileStatusInProgress VectorStoreFileStatus = "in_progress"
|
|
VectorStoreFileStatusCompleted VectorStoreFileStatus = "completed"
|
|
VectorStoreFileStatusCancelled VectorStoreFileStatus = "cancelled"
|
|
VectorStoreFileStatusFailed VectorStoreFileStatus = "failed"
|
|
)
|
|
|
|
func (r VectorStoreFileStatus) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileStatusInProgress, VectorStoreFileStatusCompleted, VectorStoreFileStatusCancelled, VectorStoreFileStatusFailed:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Union satisfied by [shared.UnionString], [shared.UnionFloat] or
|
|
// [shared.UnionBool].
|
|
type VectorStoreFileAttributesUnion interface {
|
|
ImplementsVectorStoreFileAttributesUnion()
|
|
}
|
|
|
|
func init() {
|
|
apijson.RegisterUnion(
|
|
reflect.TypeOf((*VectorStoreFileAttributesUnion)(nil)).Elem(),
|
|
"",
|
|
apijson.UnionVariant{
|
|
TypeFilter: gjson.String,
|
|
Type: reflect.TypeOf(shared.UnionString("")),
|
|
},
|
|
apijson.UnionVariant{
|
|
TypeFilter: gjson.Number,
|
|
Type: reflect.TypeOf(shared.UnionFloat(0)),
|
|
},
|
|
apijson.UnionVariant{
|
|
TypeFilter: gjson.True,
|
|
Type: reflect.TypeOf(shared.UnionBool(false)),
|
|
},
|
|
apijson.UnionVariant{
|
|
TypeFilter: gjson.False,
|
|
Type: reflect.TypeOf(shared.UnionBool(false)),
|
|
},
|
|
)
|
|
}
|
|
|
|
type VectorStoreFileDeleted struct {
|
|
ID string `json:"id,required"`
|
|
Deleted bool `json:"deleted,required"`
|
|
Object VectorStoreFileDeletedObject `json:"object,required"`
|
|
JSON vectorStoreFileDeletedJSON `json:"-"`
|
|
}
|
|
|
|
// vectorStoreFileDeletedJSON contains the JSON metadata for the struct
|
|
// [VectorStoreFileDeleted]
|
|
type vectorStoreFileDeletedJSON struct {
|
|
ID apijson.Field
|
|
Deleted apijson.Field
|
|
Object apijson.Field
|
|
raw string
|
|
ExtraFields map[string]apijson.Field
|
|
}
|
|
|
|
func (r *VectorStoreFileDeleted) UnmarshalJSON(data []byte) (err error) {
|
|
return apijson.UnmarshalRoot(data, r)
|
|
}
|
|
|
|
func (r vectorStoreFileDeletedJSON) RawJSON() string {
|
|
return r.raw
|
|
}
|
|
|
|
type VectorStoreFileDeletedObject string
|
|
|
|
const (
|
|
VectorStoreFileDeletedObjectVectorStoreFileDeleted VectorStoreFileDeletedObject = "vector_store.file.deleted"
|
|
)
|
|
|
|
func (r VectorStoreFileDeletedObject) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileDeletedObjectVectorStoreFileDeleted:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
type VectorStoreFileContentResponse struct {
|
|
// The text content
|
|
Text string `json:"text"`
|
|
// The content type (currently only `"text"`)
|
|
Type string `json:"type"`
|
|
JSON vectorStoreFileContentResponseJSON `json:"-"`
|
|
}
|
|
|
|
// vectorStoreFileContentResponseJSON contains the JSON metadata for the struct
|
|
// [VectorStoreFileContentResponse]
|
|
type vectorStoreFileContentResponseJSON struct {
|
|
Text apijson.Field
|
|
Type apijson.Field
|
|
raw string
|
|
ExtraFields map[string]apijson.Field
|
|
}
|
|
|
|
func (r *VectorStoreFileContentResponse) UnmarshalJSON(data []byte) (err error) {
|
|
return apijson.UnmarshalRoot(data, r)
|
|
}
|
|
|
|
func (r vectorStoreFileContentResponseJSON) RawJSON() string {
|
|
return r.raw
|
|
}
|
|
|
|
type VectorStoreFileNewParams struct {
|
|
// A [File](https://platform.openai.com/docs/api-reference/files) ID that the
|
|
// vector store should use. Useful for tools like `file_search` that can access
|
|
// files.
|
|
FileID param.Field[string] `json:"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, booleans, or numbers.
|
|
Attributes param.Field[map[string]VectorStoreFileNewParamsAttributesUnion] `json:"attributes"`
|
|
// 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.
|
|
ChunkingStrategy param.Field[FileChunkingStrategyParamUnion] `json:"chunking_strategy"`
|
|
}
|
|
|
|
func (r VectorStoreFileNewParams) MarshalJSON() (data []byte, err error) {
|
|
return apijson.MarshalRoot(r)
|
|
}
|
|
|
|
// Satisfied by [shared.UnionString], [shared.UnionFloat], [shared.UnionBool].
|
|
type VectorStoreFileNewParamsAttributesUnion interface {
|
|
ImplementsVectorStoreFileNewParamsAttributesUnion()
|
|
}
|
|
|
|
type VectorStoreFileUpdateParams struct {
|
|
// 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.
|
|
Attributes param.Field[map[string]VectorStoreFileUpdateParamsAttributesUnion] `json:"attributes,required"`
|
|
}
|
|
|
|
func (r VectorStoreFileUpdateParams) MarshalJSON() (data []byte, err error) {
|
|
return apijson.MarshalRoot(r)
|
|
}
|
|
|
|
// Satisfied by [shared.UnionString], [shared.UnionFloat], [shared.UnionBool].
|
|
type VectorStoreFileUpdateParamsAttributesUnion interface {
|
|
ImplementsVectorStoreFileUpdateParamsAttributesUnion()
|
|
}
|
|
|
|
type VectorStoreFileListParams struct {
|
|
// 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.Field[string] `query:"after"`
|
|
// 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.Field[string] `query:"before"`
|
|
// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
|
|
Filter param.Field[VectorStoreFileListParamsFilter] `query:"filter"`
|
|
// A limit on the number of objects to be returned. Limit can range between 1 and
|
|
// 100, and the default is 20.
|
|
Limit param.Field[int64] `query:"limit"`
|
|
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
|
|
// order and `desc` for descending order.
|
|
Order param.Field[VectorStoreFileListParamsOrder] `query:"order"`
|
|
}
|
|
|
|
// URLQuery serializes [VectorStoreFileListParams]'s query parameters as
|
|
// `url.Values`.
|
|
func (r VectorStoreFileListParams) URLQuery() (v url.Values) {
|
|
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
|
|
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
|
|
NestedFormat: apiquery.NestedQueryFormatBrackets,
|
|
})
|
|
}
|
|
|
|
// Filter by file status. One of `in_progress`, `completed`, `failed`, `cancelled`.
|
|
type VectorStoreFileListParamsFilter string
|
|
|
|
const (
|
|
VectorStoreFileListParamsFilterInProgress VectorStoreFileListParamsFilter = "in_progress"
|
|
VectorStoreFileListParamsFilterCompleted VectorStoreFileListParamsFilter = "completed"
|
|
VectorStoreFileListParamsFilterFailed VectorStoreFileListParamsFilter = "failed"
|
|
VectorStoreFileListParamsFilterCancelled VectorStoreFileListParamsFilter = "cancelled"
|
|
)
|
|
|
|
func (r VectorStoreFileListParamsFilter) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileListParamsFilterInProgress, VectorStoreFileListParamsFilterCompleted, VectorStoreFileListParamsFilterFailed, VectorStoreFileListParamsFilterCancelled:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
|
|
// order and `desc` for descending order.
|
|
type VectorStoreFileListParamsOrder string
|
|
|
|
const (
|
|
VectorStoreFileListParamsOrderAsc VectorStoreFileListParamsOrder = "asc"
|
|
VectorStoreFileListParamsOrderDesc VectorStoreFileListParamsOrder = "desc"
|
|
)
|
|
|
|
func (r VectorStoreFileListParamsOrder) IsKnown() bool {
|
|
switch r {
|
|
case VectorStoreFileListParamsOrderAsc, VectorStoreFileListParamsOrderDesc:
|
|
return true
|
|
}
|
|
return false
|
|
}
|