This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
/
files.go
190 lines (146 loc) · 4.79 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package oacg
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
)
type FileUploadResponse struct {
Object string `json:"object"`
ID string `json:"id"`
Purpose string `json:"purpose"`
Bytes int64 `json:"bytes"`
CreatedAt string `json:"created_at"`
}
type FileResponse struct {
Object string `json:"object"`
ID string `json:"id"`
Purpose string `json:"purpose"`
Bytes int64 `json:"bytes"`
CreatedAt string `json:"created_at"`
URL string `json:"url"`
}
func ListFiles(apiKey string) ([]FileResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://api.openai.com/v1/files", nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var response []FileResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode response body: %v", err)
}
return response, nil
}
func UploadFile(apiKey string, purpose string, filePath string) (*FileUploadResponse, error) {
client := &http.Client{}
file, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %v", err)
}
reqBody := new(bytes.Buffer)
writer := multipart.NewWriter(reqBody)
part, err := writer.CreateFormFile("file", filePath)
if err != nil {
return nil, fmt.Errorf("failed to create form file: %v", err)
}
part.Write(file)
purposeField, err := writer.CreateFormField("purpose")
if err != nil {
return nil, fmt.Errorf("failed to create form field: %v", err)
}
purposeField.Write([]byte(purpose))
writer.Close()
req, err := http.NewRequest("POST", "https://api.openai.com/v1/files", reqBody)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var response FileUploadResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode response body: %v", err)
}
return &response, nil
}
func DeleteFile(apiKey string, fileId string) error {
client := &http.Client{}
req, err := http.NewRequest("DELETE", fmt.Sprintf("https://api.openai.com/v1/files/%s", fileId), nil)
if err != nil {
return fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to perform HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func GetFile(apiKey string, fileId string) (*FileResponse, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.openai.com/v1/files/%s", fileId), nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
var response FileResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("failed to decode response body: %v", err)
}
return &response, nil
}
func GetFileContent(apiKey string, fileId string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.openai.com/v1/files/%s/content", fileId), nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to perform HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
fileContent, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
return fileContent, nil
}