forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
170 lines (140 loc) · 4.01 KB
/
files.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
package openai
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
)
type FileRequest struct {
FileName string `json:"file"`
FilePath string `json:"-"`
Purpose string `json:"purpose"`
}
// PurposeType represents the purpose of the file when uploading.
type PurposeType string
const (
PurposeFineTune PurposeType = "fine-tune"
PurposeFineTuneResults PurposeType = "fine-tune-results"
PurposeAssistants PurposeType = "assistants"
PurposeAssistantsOutput PurposeType = "assistants_output"
PurposeBatch PurposeType = "batch"
)
// FileBytesRequest represents a file upload request.
type FileBytesRequest struct {
// the name of the uploaded file in OpenAI
Name string
// the bytes of the file
Bytes []byte
// the purpose of the file
Purpose PurposeType
}
// File struct represents an OpenAPI file.
type File struct {
Bytes int `json:"bytes"`
CreatedAt int64 `json:"created_at"`
ID string `json:"id"`
FileName string `json:"filename"`
Object string `json:"object"`
Status string `json:"status"`
Purpose string `json:"purpose"`
StatusDetails string `json:"status_details"`
httpHeader
}
// FilesList is a list of files that belong to the user or organization.
type FilesList struct {
Files []File `json:"data"`
httpHeader
}
// CreateFileBytes uploads bytes directly to OpenAI without requiring a local file.
func (c *Client) CreateFileBytes(ctx context.Context, request FileBytesRequest) (file File, err error) {
var b bytes.Buffer
reader := bytes.NewReader(request.Bytes)
builder := c.createFormBuilder(&b)
err = builder.WriteField("purpose", string(request.Purpose))
if err != nil {
return
}
err = builder.CreateFormFileReader("file", reader, request.Name)
if err != nil {
return
}
err = builder.Close()
if err != nil {
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"),
withBody(&b), withContentType(builder.FormDataContentType()))
if err != nil {
return
}
err = c.sendRequest(req, &file)
return
}
// CreateFile uploads a jsonl file to GPT3
// FilePath must be a local file path.
func (c *Client) CreateFile(ctx context.Context, request FileRequest) (file File, err error) {
var b bytes.Buffer
builder := c.createFormBuilder(&b)
err = builder.WriteField("purpose", request.Purpose)
if err != nil {
return
}
fileData, err := os.Open(request.FilePath)
if err != nil {
return
}
err = builder.CreateFormFile("file", fileData)
if err != nil {
return
}
err = builder.Close()
if err != nil {
return
}
req, err := c.newRequest(ctx, http.MethodPost, c.fullURL("/files"),
withBody(&b), withContentType(builder.FormDataContentType()))
if err != nil {
return
}
err = c.sendRequest(req, &file)
return
}
// DeleteFile deletes an existing file.
func (c *Client) DeleteFile(ctx context.Context, fileID string) (err error) {
req, err := c.newRequest(ctx, http.MethodDelete, c.fullURL("/files/"+fileID))
if err != nil {
return
}
err = c.sendRequest(req, nil)
return
}
// ListFiles Lists the currently available files,
// and provides basic information about each file such as the file name and purpose.
func (c *Client) ListFiles(ctx context.Context) (files FilesList, err error) {
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL("/files"))
if err != nil {
return
}
err = c.sendRequest(req, &files)
return
}
// GetFile Retrieves a file instance, providing basic information about the file
// such as the file name and purpose.
func (c *Client) GetFile(ctx context.Context, fileID string) (file File, err error) {
urlSuffix := fmt.Sprintf("/files/%s", fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
if err != nil {
return
}
err = c.sendRequest(req, &file)
return
}
func (c *Client) GetFileContent(ctx context.Context, fileID string) (content RawResponse, err error) {
urlSuffix := fmt.Sprintf("/files/%s/content", fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
if err != nil {
return
}
return c.sendRequestRaw(req)
}