-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.go
130 lines (100 loc) · 3.2 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
package assistants
import (
"context"
"fmt"
"io"
"net/http"
"os"
)
// FileObject represents a document that has been uploaded to OpenAI.
type FileObject 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"`
}
// ListFilesResponse represents the response for listing files.
type ListFilesResponse struct {
Data []FileObject `json:"data"`
Object string `json:"object"`
}
// sendFileAPIRequest sends an HTTP request for file API and returns the response body.
func (c *Client) sendFileAPIRequest(ctx context.Context, method, endpoint string, body interface{}, result interface{}) error {
url := getRequestURL(endpoint)
headers := map[string]string{
"Content-Type": "application/json",
"Authorization": "Bearer " + c.APIKey,
}
return c.sendHTTPRequest(ctx, method, url, body, result, headers)
}
type UploadFileParams struct {
FilePath string `json:"file_path"`
Purpose string `json:"purpose"`
}
// UploadFile uploads a file to OpenAI.
func (c *Client) UploadFile(ctx context.Context, params UploadFileParams) (*FileObject, error) {
endpoint := "files"
file, err := os.Open(params.FilePath)
if err != nil {
return nil, fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
body, writer := createMultipartRequest(file, params.Purpose)
req, err := http.NewRequestWithContext(ctx, "POST", getRequestURL(endpoint), io.NopCloser(body))
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %v", err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
var result FileObject
err = c.sendFileAPIRequest(ctx, "POST", endpoint, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// ListFiles lists all files belonging to the user's organization.
func (c *Client) ListFiles(ctx context.Context, purpose string) (*ListFilesResponse, error) {
endpoint := "files"
url := getRequestURL(endpoint)
if purpose != "" {
url += "?purpose=" + purpose
}
var result ListFilesResponse
err := c.sendFileAPIRequest(ctx, "GET", endpoint, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// DeleteFile deletes a file.
func (c *Client) DeleteFile(ctx context.Context, fileID string) (*FileObject, error) {
endpoint := fmt.Sprintf("files/%s", fileID)
var result FileObject
err := c.sendFileAPIRequest(ctx, "DELETE", endpoint, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// GetFile retrieves information about a specific file.
func (c *Client) GetFile(ctx context.Context, fileID string) (*FileObject, error) {
endpoint := fmt.Sprintf("files/%s", fileID)
var result FileObject
err := c.sendFileAPIRequest(ctx, "GET", endpoint, nil, &result)
if err != nil {
return nil, err
}
return &result, nil
}
// GetFileContent retrieves the contents of the specified file.
func (c *Client) GetFileContent(ctx context.Context, fileID string) ([]byte, error) {
endpoint := fmt.Sprintf("files/%s/content", fileID)
var result []byte
err := c.sendFileAPIRequest(ctx, "GET", endpoint, nil, &result)
if err != nil {
return nil, err
}
return result, nil
}