forked from openai/openai-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
337 lines (299 loc) · 11.1 KB
/
file.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"github.com/openai/openai-go/internal/apiform"
"github.com/openai/openai-go/internal/apijson"
"github.com/openai/openai-go/internal/apiquery"
"github.com/openai/openai-go/internal/pagination"
"github.com/openai/openai-go/internal/param"
"github.com/openai/openai-go/internal/requestconfig"
"github.com/openai/openai-go/option"
)
// FileService 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 [NewFileService] method instead.
type FileService struct {
Options []option.RequestOption
}
// NewFileService 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 NewFileService(opts ...option.RequestOption) (r *FileService) {
r = &FileService{}
r.Options = opts
return
}
// Upload a file that can be used across various endpoints. Individual files can be
// up to 512 MB, and the size of all files uploaded by one organization can be up
// to 100 GB.
//
// The Assistants API supports files up to 2 million tokens and of specific file
// types. See the
// [Assistants Tools guide](https://platform.openai.com/docs/assistants/tools) for
// details.
//
// The Fine-tuning API only supports `.jsonl` files. The input also has certain
// required formats for fine-tuning
// [chat](https://platform.openai.com/docs/api-reference/fine-tuning/chat-input) or
// [completions](https://platform.openai.com/docs/api-reference/fine-tuning/completions-input)
// models.
//
// The Batch API only supports `.jsonl` files up to 100 MB in size. The input also
// has a specific required
// [format](https://platform.openai.com/docs/api-reference/batch/request-input).
//
// Please [contact us](https://help.openai.com/) if you need to increase these
// storage limits.
func (r *FileService) New(ctx context.Context, body FileNewParams, opts ...option.RequestOption) (res *FileObject, err error) {
opts = append(r.Options[:], opts...)
path := "files"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Returns information about a specific file.
func (r *FileService) Get(ctx context.Context, fileID string, opts ...option.RequestOption) (res *FileObject, err error) {
opts = append(r.Options[:], opts...)
if fileID == "" {
err = errors.New("missing required file_id parameter")
return
}
path := fmt.Sprintf("files/%s", fileID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Returns a list of files that belong to the user's organization.
func (r *FileService) List(ctx context.Context, query FileListParams, opts ...option.RequestOption) (res *pagination.Page[FileObject], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "files"
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 files that belong to the user's organization.
func (r *FileService) ListAutoPaging(ctx context.Context, query FileListParams, opts ...option.RequestOption) *pagination.PageAutoPager[FileObject] {
return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
}
// Delete a file.
func (r *FileService) Delete(ctx context.Context, fileID string, opts ...option.RequestOption) (res *FileDeleted, err error) {
opts = append(r.Options[:], opts...)
if fileID == "" {
err = errors.New("missing required file_id parameter")
return
}
path := fmt.Sprintf("files/%s", fileID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &res, opts...)
return
}
// Returns the contents of the specified file.
func (r *FileService) Content(ctx context.Context, fileID string, opts ...option.RequestOption) (res *http.Response, err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("Accept", "application/binary")}, opts...)
if fileID == "" {
err = errors.New("missing required file_id parameter")
return
}
path := fmt.Sprintf("files/%s/content", fileID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
type FileDeleted struct {
ID string `json:"id,required"`
Deleted bool `json:"deleted,required"`
Object FileDeletedObject `json:"object,required"`
JSON fileDeletedJSON `json:"-"`
}
// fileDeletedJSON contains the JSON metadata for the struct [FileDeleted]
type fileDeletedJSON struct {
ID apijson.Field
Deleted apijson.Field
Object apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *FileDeleted) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r fileDeletedJSON) RawJSON() string {
return r.raw
}
type FileDeletedObject string
const (
FileDeletedObjectFile FileDeletedObject = "file"
)
func (r FileDeletedObject) IsKnown() bool {
switch r {
case FileDeletedObjectFile:
return true
}
return false
}
// The `File` object represents a document that has been uploaded to OpenAI.
type FileObject struct {
// The file identifier, which can be referenced in the API endpoints.
ID string `json:"id,required"`
// The size of the file, in bytes.
Bytes int64 `json:"bytes,required"`
// The Unix timestamp (in seconds) for when the file was created.
CreatedAt int64 `json:"created_at,required"`
// The name of the file.
Filename string `json:"filename,required"`
// The object type, which is always `file`.
Object FileObjectObject `json:"object,required"`
// The intended purpose of the file. Supported values are `assistants`,
// `assistants_output`, `batch`, `batch_output`, `fine-tune`, `fine-tune-results`
// and `vision`.
Purpose FileObjectPurpose `json:"purpose,required"`
// Deprecated. The current status of the file, which can be either `uploaded`,
// `processed`, or `error`.
Status FileObjectStatus `json:"status,required"`
// Deprecated. For details on why a fine-tuning training file failed validation,
// see the `error` field on `fine_tuning.job`.
StatusDetails string `json:"status_details"`
JSON fileObjectJSON `json:"-"`
}
// fileObjectJSON contains the JSON metadata for the struct [FileObject]
type fileObjectJSON struct {
ID apijson.Field
Bytes apijson.Field
CreatedAt apijson.Field
Filename apijson.Field
Object apijson.Field
Purpose apijson.Field
Status apijson.Field
StatusDetails apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *FileObject) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r fileObjectJSON) RawJSON() string {
return r.raw
}
// The object type, which is always `file`.
type FileObjectObject string
const (
FileObjectObjectFile FileObjectObject = "file"
)
func (r FileObjectObject) IsKnown() bool {
switch r {
case FileObjectObjectFile:
return true
}
return false
}
// The intended purpose of the file. Supported values are `assistants`,
// `assistants_output`, `batch`, `batch_output`, `fine-tune`, `fine-tune-results`
// and `vision`.
type FileObjectPurpose string
const (
FileObjectPurposeAssistants FileObjectPurpose = "assistants"
FileObjectPurposeAssistantsOutput FileObjectPurpose = "assistants_output"
FileObjectPurposeBatch FileObjectPurpose = "batch"
FileObjectPurposeBatchOutput FileObjectPurpose = "batch_output"
FileObjectPurposeFineTune FileObjectPurpose = "fine-tune"
FileObjectPurposeFineTuneResults FileObjectPurpose = "fine-tune-results"
FileObjectPurposeVision FileObjectPurpose = "vision"
)
func (r FileObjectPurpose) IsKnown() bool {
switch r {
case FileObjectPurposeAssistants, FileObjectPurposeAssistantsOutput, FileObjectPurposeBatch, FileObjectPurposeBatchOutput, FileObjectPurposeFineTune, FileObjectPurposeFineTuneResults, FileObjectPurposeVision:
return true
}
return false
}
// Deprecated. The current status of the file, which can be either `uploaded`,
// `processed`, or `error`.
type FileObjectStatus string
const (
FileObjectStatusUploaded FileObjectStatus = "uploaded"
FileObjectStatusProcessed FileObjectStatus = "processed"
FileObjectStatusError FileObjectStatus = "error"
)
func (r FileObjectStatus) IsKnown() bool {
switch r {
case FileObjectStatusUploaded, FileObjectStatusProcessed, FileObjectStatusError:
return true
}
return false
}
// The intended purpose of the uploaded file.
//
// Use "assistants" for
// [Assistants](https://platform.openai.com/docs/api-reference/assistants) and
// [Message](https://platform.openai.com/docs/api-reference/messages) files,
// "vision" for Assistants image file inputs, "batch" for
// [Batch API](https://platform.openai.com/docs/guides/batch), and "fine-tune" for
// [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning).
type FilePurpose string
const (
FilePurposeAssistants FilePurpose = "assistants"
FilePurposeBatch FilePurpose = "batch"
FilePurposeFineTune FilePurpose = "fine-tune"
FilePurposeVision FilePurpose = "vision"
)
func (r FilePurpose) IsKnown() bool {
switch r {
case FilePurposeAssistants, FilePurposeBatch, FilePurposeFineTune, FilePurposeVision:
return true
}
return false
}
type FileNewParams struct {
// The File object (not file name) to be uploaded.
File param.Field[io.Reader] `json:"file,required" format:"binary"`
// The intended purpose of the uploaded file.
//
// Use "assistants" for
// [Assistants](https://platform.openai.com/docs/api-reference/assistants) and
// [Message](https://platform.openai.com/docs/api-reference/messages) files,
// "vision" for Assistants image file inputs, "batch" for
// [Batch API](https://platform.openai.com/docs/guides/batch), and "fine-tune" for
// [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning).
Purpose param.Field[FilePurpose] `json:"purpose,required"`
}
func (r FileNewParams) MarshalMultipart() (data []byte, contentType string, err error) {
buf := bytes.NewBuffer(nil)
writer := multipart.NewWriter(buf)
err = apiform.MarshalRoot(r, writer)
if err != nil {
writer.Close()
return nil, "", err
}
err = writer.Close()
if err != nil {
return nil, "", err
}
return buf.Bytes(), writer.FormDataContentType(), nil
}
type FileListParams struct {
// Only return files with the given purpose.
Purpose param.Field[string] `query:"purpose"`
}
// URLQuery serializes [FileListParams]'s query parameters as `url.Values`.
func (r FileListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}