-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfile.go
184 lines (150 loc) · 3.99 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
package chatgpt
import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
)
type FileStatus string
const (
FILESTATUS_UPLOADED FileStatus = "uploaded"
FILESTATUS_PROCESSED FileStatus = "processed"
FILESTATUS_PENDING FileStatus = "pending"
FILESTATUS_ERROR FileStatus = "error"
FILESTATUS_DELETING FileStatus = "deleting"
FILESTATUS_DELETED FileStatus = "deleted"
)
type FilePurpose string
const (
FILEPURPOSE_FINETUNE FilePurpose = "fine-tune"
)
type File struct {
Id string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose FilePurpose `json:"purpose"`
Status FileStatus `json:"status"`
StatusDetails *string `json:"status_details"`
}
type FileList struct {
Data []File `json:"data"`
Object string `json:"object"`
}
type DeleteFileResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
}
func (c *Client) ListFiles(ctx context.Context) (*FileList, error) {
endpoint := "/files"
httpReq, err := http.NewRequest("GET", c.config.BaseURL+endpoint, nil)
if err != nil {
return nil, err
}
httpReq = httpReq.WithContext(ctx)
res, err := c.sendRequest(ctx, httpReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
var fileList FileList
if err := json.NewDecoder(res.Body).Decode(&fileList); err != nil {
return nil, err
}
return &fileList, nil
}
func (c *Client) UploadFile(ctx context.Context, file io.Reader) (*File, error) {
endpoint := "/files"
httpReq, err := http.NewRequest("POST", c.config.BaseURL+endpoint, nil)
if err != nil {
return nil, err
}
httpReq = httpReq.WithContext(ctx)
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
err = writer.WriteField("purpose", string(FILEPURPOSE_FINETUNE))
if err != nil {
return nil, err
}
part, err := writer.CreateFormFile("file", "mydata.jsonl")
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
httpReq.Body = io.NopCloser(&requestBody)
res, err := c.sendRequest(ctx, httpReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
var responseFile File
if err := json.NewDecoder(res.Body).Decode(&responseFile); err != nil {
return nil, err
}
return &responseFile, nil
}
func (c *Client) DeleteFile(ctx context.Context, fileId string) (*DeleteFileResponse, error) {
endpoint := "/files/" + fileId
httpReq, err := http.NewRequest("DELETE", c.config.BaseURL+endpoint, nil)
if err != nil {
return nil, err
}
httpReq = httpReq.WithContext(ctx)
res, err := c.sendRequest(ctx, httpReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
var deletedFile DeleteFileResponse
if err := json.NewDecoder(res.Body).Decode(&deletedFile); err != nil {
return nil, err
}
return &deletedFile, nil
}
func (c *Client) RetrieveFile(ctx context.Context, fileId string) (*File, error) {
endpoint := "/files/" + fileId
httpReq, err := http.NewRequest("GET", c.config.BaseURL+endpoint, nil)
if err != nil {
return nil, err
}
httpReq = httpReq.WithContext(ctx)
res, err := c.sendRequest(ctx, httpReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
var file File
if err := json.NewDecoder(res.Body).Decode(&file); err != nil {
return nil, err
}
return &file, nil
}
func (c *Client) RetrieveFileContent(ctx context.Context, fileId string) (*string, error) {
endpoint := "/files/" + fileId + "/content"
httpReq, err := http.NewRequest("GET", c.config.BaseURL+endpoint, nil)
if err != nil {
return nil, err
}
httpReq = httpReq.WithContext(ctx)
res, err := c.sendRequest(ctx, httpReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
var fileContent string
if err := json.NewDecoder(res.Body).Decode(&fileContent); err != nil {
return nil, err
}
return &fileContent, nil
}