Skip to content

Commit 63494c9

Browse files
author
shenhaofang
committed
feat: api add list files and delete files
1 parent 12b8129 commit 63494c9

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

client.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,44 @@ func (c *AIClient) UpdateFile(method string, param OpenAIFileCreateParam) (*File
109109
return &res.FileInfo, err
110110
}
111111

112+
func (c *AIClient) ListFiles(param OpenAIListFilesParam) (*ListFilesResp, error) {
113+
query := param.ToQuery()
114+
if query != "" {
115+
query = "?" + query
116+
}
117+
url := c.BaseURL + "/files" + query
118+
119+
// 发送请求
120+
httpReq, err := http.NewRequest("GET", url, nil)
121+
if err != nil {
122+
return nil, errors.Wrap(err, "[ai_client]make request to send msg error")
123+
}
124+
httpReq.Header.Set("Authorization", "Bearer "+c.Key)
125+
// req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
126+
127+
res := new(ListFilesResp)
128+
httpResp, err := c.client.Do(httpReq)
129+
if err != nil {
130+
return nil, errors.Wrap(err, "[ai_client]send request to send msg error")
131+
}
132+
defer httpResp.Body.Close()
133+
bodyBytes, err := io.ReadAll(httpResp.Body)
134+
// fmt.Println(string(bodyBytes))
135+
if err != nil {
136+
return nil, errors.Wrap(err, "[ai_resp]read resp body failed")
137+
}
138+
err = json.Unmarshal(bodyBytes, res)
139+
if err != nil {
140+
return nil, errors.Wrap(err, "[ai_resp]unmarshal resp body failed")
141+
}
142+
143+
if res.Error != nil {
144+
return res, errors.New(res.Error.Message)
145+
}
146+
147+
return res, err
148+
}
149+
112150
func (c *AIClient) RetrieveFile(method string, fileID string) (*FileInfo, error) {
113151
if fileID == "" {
114152
return nil, errors.New("[ai_client]file id is empty")
@@ -148,6 +186,44 @@ func (c *AIClient) RetrieveFile(method string, fileID string) (*FileInfo, error)
148186
return &res.FileInfo, err
149187
}
150188

189+
func (c *AIClient) DeleteFile(fileID string) (*DeleteFileResp, error) {
190+
if fileID == "" {
191+
return nil, errors.New("[ai_client]file id is empty")
192+
}
193+
url := c.BaseURL + "/files/" + fileID
194+
195+
// 发送请求
196+
httpReq, err := http.NewRequest("DELETE", url, nil)
197+
if err != nil {
198+
return nil, errors.Wrap(err, "[ai_client]make request to send msg error")
199+
}
200+
httpReq.Header.Set("Authorization", "Bearer "+c.Key)
201+
// req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15")
202+
203+
res := new(DeleteFileResp)
204+
205+
httpResp, err := c.client.Do(httpReq)
206+
if err != nil {
207+
return nil, errors.Wrap(err, "[ai_client]send request to send msg error")
208+
}
209+
defer httpResp.Body.Close()
210+
211+
bodyBytes, err := io.ReadAll(httpResp.Body)
212+
if err != nil {
213+
return nil, errors.Wrap(err, "[ai_resp]read resp body failed")
214+
}
215+
err = json.Unmarshal(bodyBytes, res)
216+
if err != nil {
217+
return nil, errors.Wrap(err, "[ai_resp]unmarshal resp body failed")
218+
}
219+
220+
if res.Error != nil {
221+
return res, errors.New(res.Error.Message)
222+
}
223+
224+
return res, err
225+
}
226+
151227
func (c *AIClient) MakeChatReqBytes(param OpenAIChatParam) (reqByts []byte, err error) {
152228
if param.Model == "" {
153229
param.Model = "gpt-3.5-turbo-0301" //gpt-3.5-turbo or gpt-3.5-turbo-0301

example/qwen-vl/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ func main() {
176176
}
177177
fmt.Println(fileInfo2)
178178

179+
fileListResp, err := aiClient.ListFiles(openai.OpenAIListFilesParam{
180+
Limit: 10,
181+
Order: "desc",
182+
})
183+
if err != nil {
184+
log.Fatalf("Error fileList request: %v", err)
185+
return
186+
}
187+
fmt.Println(fileListResp)
188+
179189
// 请求ai大模型
180190
chatParam = openai.OpenAIChatParam{
181191
Model: "qwen-long",
@@ -217,4 +227,11 @@ func main() {
217227
}
218228
// Print the response content
219229
fmt.Println(resChat.Choices[0].Message.Content)
230+
231+
delFileResp, err := aiClient.DeleteFile(fileInfo.ID)
232+
if err != nil {
233+
log.Fatalf("Error delete file request: %v", err)
234+
return
235+
}
236+
fmt.Println(delFileResp)
220237
}

request.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"io"
77
"mime/multipart"
88
"net/http"
9+
"net/url"
910
"os"
1011
"path"
12+
"strconv"
1113

1214
jsoniter "github.com/json-iterator/go"
1315
"github.com/pkg/errors"
@@ -103,6 +105,30 @@ func (p *OpenAIFileCreateParam) buildMultipartWriter(body io.Writer) error {
103105
return err
104106
}
105107

108+
type OpenAIListFilesParam struct {
109+
Purpose string
110+
Limit int
111+
Order string
112+
After string
113+
}
114+
115+
func (o OpenAIListFilesParam) ToQuery() string {
116+
vals := make(url.Values, 4)
117+
if o.Purpose != "" {
118+
vals.Set("purpose", o.Purpose)
119+
}
120+
if o.Limit != 0 {
121+
vals.Set("limit", strconv.Itoa(o.Limit))
122+
}
123+
if o.Order != "" {
124+
vals.Set("order", o.Order)
125+
}
126+
if o.After != "" {
127+
vals.Set("after", o.After)
128+
}
129+
return vals.Encode()
130+
}
131+
106132
// https://platform.openai.com/docs/api-reference/files/object
107133
type FileInfo struct {
108134
ID string `json:"id"` // The file identifier, which can be referenced in the API endpoints.

response.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ type FileResp struct {
5353
Error *AIError `json:"error,omitempty"`
5454
}
5555

56+
type ListFilesResp struct {
57+
Data []FileInfo `json:"data"`
58+
Object string `json:"object"`
59+
NextPage int `json:"next_page"`
60+
HasMore bool `json:"has_more"`
61+
Error *AIError `json:"error,omitempty"`
62+
}
63+
64+
type DeleteFileResp struct {
65+
ID string `json:"id"`
66+
Object string `json:"object"`
67+
Deleted bool `json:"deleted"`
68+
Error *AIError
69+
}
70+
5671
type FinishReason string
5772

5873
// https://platform.openai.com/docs/api-reference/chat/object

0 commit comments

Comments
 (0)