-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.go
167 lines (147 loc) · 4.33 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
package v1
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
)
const (
Files = "https://api.openai.com/v1/files"
UploadFile = "https://api.openai.com/v1/files"
File = "https://api.openai.com/v1/files/%s"
FileContent = "https://api.openai.com/v1/files/%s/content"
)
type FilesResponse struct {
Data []struct {
Id string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
} `json:"data"`
Object string `json:"object"`
}
// Files are used to upload documents that can be used with features like Fine-tuning.
func (chat *ChatGpt) Files() (ctx context.Context, response FilesResponse, err error) {
resp, err := chat.Get(ctx, Files, nil)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(resp, &response)
if err != nil {
fmt.Println(err)
return
}
return
}
type UploadFileRequest struct {
File *os.File `json:"file"` // If the purpose is set to "fine-tune", each line is a JSON record with "prompt" and "completion" fields representing your training examples.
Purpose string `json:"purpose"` // The intended purpose of the uploaded documents. Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file.
}
type UploadFileResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
}
// UploadFile Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
func (chat *ChatGpt) UploadFile(ctx context.Context, req UploadFileRequest) (response UploadFileResponse, err error) {
// 创建multipart/form-data格式的body
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
// 设置file参数
filePart, err := writer.CreateFormFile("image", req.File.Name())
if err != nil {
fmt.Println(err)
return
}
_, err = io.Copy(filePart, req.File)
// 设置其他参数
if req.Purpose != "" {
_ = writer.WriteField("purpose", req.Purpose)
}
// 结束body的编写
_ = writer.Close()
// 创建http请求
request, err := http.NewRequestWithContext(ctx, "POST", UploadFile, &requestBody)
if err != nil {
fmt.Println(err)
return
}
// 设置header中的Authorization字段
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", chat.Authorization))
request.Header.Set("Content-Type", writer.FormDataContentType())
// 发送http请求
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// 读取响应体
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(responseBody, &response)
if err != nil {
fmt.Println(err)
return
}
return
}
type DeleteFileResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
}
func (chat *ChatGpt) DeleteFile(ctx context.Context, id string) (response DeleteFileResponse, err error) {
resp, err := chat.Delete(ctx, fmt.Sprintf(File, id), nil)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(resp, &response)
if err != nil {
fmt.Println(err)
return
}
return
}
type RetrieveFileResponse struct {
Id string `json:"id"`
Object string `json:"object"`
Bytes int `json:"bytes"`
CreatedAt int `json:"created_at"`
Filename string `json:"filename"`
Purpose string `json:"purpose"`
}
// RetrieveFile Returns information about a specific file.
func (chat *ChatGpt) RetrieveFile(ctx context.Context, id string) (response FilesResponse, err error) {
resp, err := chat.Get(ctx, fmt.Sprintf(File, id), nil)
if err != nil {
fmt.Println(err)
return
}
err = json.Unmarshal(resp, &response)
if err != nil {
fmt.Println(err)
return
}
return
}
// RetrieveFileContent Returns the contents of the specified file
func (chat *ChatGpt) RetrieveFileContent(ctx context.Context, id string) ([]byte, error) {
return chat.Get(ctx, fmt.Sprintf(FileContent, id), nil)
}