forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
115 lines (96 loc) · 2.63 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
package openai
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
)
type FileRequest struct {
FileName string `json:"file"`
FilePath string `json:"-"`
Purpose string `json:"purpose"`
}
// 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"`
Owner string `json:"owner"`
Purpose string `json:"purpose"`
}
// FilesList is a list of files that belong to the user or organization.
type FilesList struct {
Files []File `json:"data"`
}
// 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 io.ReadCloser, err error) {
urlSuffix := fmt.Sprintf("/files/%s/content", fileID)
req, err := c.newRequest(ctx, http.MethodGet, c.fullURL(urlSuffix))
if err != nil {
return
}
content, err = c.sendRequestRaw(req)
return
}