-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
541 lines (445 loc) · 12.9 KB
/
file.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package zhipu
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"strconv"
"github.com/go-resty/resty/v2"
)
const (
FilePurposeFineTune = "fine-tune"
FilePurposeRetrieval = "retrieval"
FilePurposeBatch = "batch"
KnowledgeTypeArticle = 1
KnowledgeTypeQADocument = 2
KnowledgeTypeQASpreadsheet = 3
KnowledgeTypeProductDatabaseSpreadsheet = 4
KnowledgeTypeCustom = 5
)
// FileCreateService is a service to create a file.
type FileCreateService struct {
client *Client
purpose string
localFile string
file io.Reader
filename string
customSeparator *string
sentenceSize *int
knowledgeID *string
}
// FileCreateKnowledgeSuccessInfo is the success info of the FileCreateKnowledgeResponse.
type FileCreateKnowledgeSuccessInfo struct {
Filename string `json:"fileName"`
DocumentID string `json:"documentId"`
}
// FileCreateKnowledgeFailedInfo is the failed info of the FileCreateKnowledgeResponse.
type FileCreateKnowledgeFailedInfo struct {
Filename string `json:"fileName"`
FailReason string `json:"failReason"`
}
// FileCreateKnowledgeResponse is the response of the FileCreateService.
type FileCreateKnowledgeResponse struct {
SuccessInfos []FileCreateKnowledgeSuccessInfo `json:"successInfos"`
FailedInfos []FileCreateKnowledgeFailedInfo `json:"failedInfos"`
}
// FileCreateFineTuneResponse is the response of the FileCreateService.
type FileCreateFineTuneResponse struct {
Bytes int64 `json:"bytes"`
CreatedAt int64 `json:"created_at"`
Filename string `json:"filename"`
Object string `json:"object"`
Purpose string `json:"purpose"`
ID string `json:"id"`
}
// FileCreateResponse is the response of the FileCreateService.
type FileCreateResponse struct {
FileCreateFineTuneResponse
FileCreateKnowledgeResponse
}
// NewFileCreateService creates a new FileCreateService.
func NewFileCreateService(client *Client) *FileCreateService {
return &FileCreateService{client: client}
}
// SetLocalFile sets the local_file parameter of the FileCreateService.
func (s *FileCreateService) SetLocalFile(localFile string) *FileCreateService {
s.localFile = localFile
return s
}
// SetFile sets the file parameter of the FileCreateService.
func (s *FileCreateService) SetFile(file io.Reader, filename string) *FileCreateService {
s.file = file
s.filename = filename
return s
}
// SetPurpose sets the purpose parameter of the FileCreateService.
func (s *FileCreateService) SetPurpose(purpose string) *FileCreateService {
s.purpose = purpose
return s
}
// SetCustomSeparator sets the custom_separator parameter of the FileCreateService.
func (s *FileCreateService) SetCustomSeparator(customSeparator string) *FileCreateService {
s.customSeparator = &customSeparator
return s
}
// SetSentenceSize sets the sentence_size parameter of the FileCreateService.
func (s *FileCreateService) SetSentenceSize(sentenceSize int) *FileCreateService {
s.sentenceSize = &sentenceSize
return s
}
// SetKnowledgeID sets the knowledge_id parameter of the FileCreateService.
func (s *FileCreateService) SetKnowledgeID(knowledgeID string) *FileCreateService {
s.knowledgeID = &knowledgeID
return s
}
// Do makes the request.
func (s *FileCreateService) Do(ctx context.Context) (res FileCreateResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
body := map[string]string{"purpose": s.purpose}
if s.customSeparator != nil {
body["custom_separator"] = *s.customSeparator
}
if s.sentenceSize != nil {
body["sentence_size"] = strconv.Itoa(*s.sentenceSize)
}
if s.knowledgeID != nil {
body["knowledge_id"] = *s.knowledgeID
}
file, filename := s.file, s.filename
if file == nil && s.localFile != "" {
var f *os.File
if f, err = os.Open(s.localFile); err != nil {
return
}
defer f.Close()
file = f
filename = filepath.Base(s.localFile)
}
if file == nil {
err = errors.New("no file specified")
return
}
if resp, err = s.client.request(ctx).
SetFileReader("file", filename, file).
SetMultipartFormData(body).
SetResult(&res).
SetError(&apiError).
Post("files"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FileEditService is a service to edit a file.
type FileEditService struct {
client *Client
documentID string
knowledgeType *int
customSeparator []string
sentenceSize *int
}
// NewFileEditService creates a new FileEditService.
func NewFileEditService(client *Client) *FileEditService {
return &FileEditService{client: client}
}
// SetDocumentID sets the document_id parameter of the FileEditService.
func (s *FileEditService) SetDocumentID(documentID string) *FileEditService {
s.documentID = documentID
return s
}
// SetKnowledgeType sets the knowledge_type parameter of the FileEditService.
func (s *FileEditService) SetKnowledgeType(knowledgeType int) *FileEditService {
s.knowledgeType = &knowledgeType
return s
}
// SetSentenceSize sets the sentence_size parameter of the FileEditService.
func (s *FileEditService) SetCustomSeparator(customSeparator ...string) *FileEditService {
s.customSeparator = customSeparator
return s
}
// SetSentenceSize sets the sentence_size parameter of the FileEditService.
func (s *FileEditService) SetSentenceSize(sentenceSize int) *FileEditService {
s.sentenceSize = &sentenceSize
return s
}
// Do makes the request.
func (s *FileEditService) Do(ctx context.Context) (err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
body := M{}
if s.knowledgeType != nil {
body["knowledge_type"] = strconv.Itoa(*s.knowledgeType)
}
if len(s.customSeparator) > 0 {
body["custom_separator"] = s.customSeparator
}
if s.sentenceSize != nil {
body["sentence_size"] = strconv.Itoa(*s.sentenceSize)
}
if resp, err = s.client.request(ctx).
SetPathParam("document_id", s.documentID).
SetBody(body).
SetError(&apiError).
Put("document/{document_id}"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FileListService is a service to list files.
type FileListService struct {
client *Client
purpose string
knowledgeID *string
page *int
limit *int
after *string
orderAsc *bool
}
// FileFailInfo is the failed info of the FileListKnowledgeItem.
type FileFailInfo struct {
EmbeddingCode int `json:"embedding_code"`
EmbeddingMsg string `json:"embedding_msg"`
}
// FileListKnowledgeItem is the item of the FileListKnowledgeResponse.
type FileListKnowledgeItem struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
Length int64 `json:"length"`
SentenceSize int64 `json:"sentence_size"`
CustomSeparator []string `json:"custom_separator"`
EmbeddingStat int `json:"embedding_stat"`
FailInfo *FileFailInfo `json:"failInfo"`
WordNum int64 `json:"word_num"`
ParseImage int `json:"parse_image"`
}
// FileListKnowledgeResponse is the response of the FileListService.
type FileListKnowledgeResponse struct {
Total int `json:"total"`
List []FileListKnowledgeItem `json:"list"`
}
// FileListFineTuneItem is the item of the FileListFineTuneResponse.
type FileListFineTuneItem struct {
Bytes int64 `json:"bytes"`
CreatedAt int64 `json:"created_at"`
Filename string `json:"filename"`
ID string `json:"id"`
Object string `json:"object"`
Purpose string `json:"purpose"`
}
// FileListFineTuneResponse is the response of the FileListService.
type FileListFineTuneResponse struct {
Object string `json:"object"`
Data []FileListFineTuneItem `json:"data"`
}
// FileListResponse is the response of the FileListService.
type FileListResponse struct {
FileListKnowledgeResponse
FileListFineTuneResponse
}
// NewFileListService creates a new FileListService.
func NewFileListService(client *Client) *FileListService {
return &FileListService{client: client}
}
// SetPurpose sets the purpose parameter of the FileListService.
func (s *FileListService) SetPurpose(purpose string) *FileListService {
s.purpose = purpose
return s
}
// SetKnowledgeID sets the knowledge_id parameter of the FileListService.
func (s *FileListService) SetKnowledgeID(knowledgeID string) *FileListService {
s.knowledgeID = &knowledgeID
return s
}
// SetPage sets the page parameter of the FileListService.
func (s *FileListService) SetPage(page int) *FileListService {
s.page = &page
return s
}
// SetLimit sets the limit parameter of the FileListService.
func (s *FileListService) SetLimit(limit int) *FileListService {
s.limit = &limit
return s
}
// SetAfter sets the after parameter of the FileListService.
func (s *FileListService) SetAfter(after string) *FileListService {
s.after = &after
return s
}
// SetOrder sets the order parameter of the FileListService.
func (s *FileListService) SetOrder(asc bool) *FileListService {
s.orderAsc = &asc
return s
}
// Do makes the request.
func (s *FileListService) Do(ctx context.Context) (res FileListResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
m := map[string]string{
"purpose": s.purpose,
}
if s.knowledgeID != nil {
m["knowledge_id"] = *s.knowledgeID
}
if s.page != nil {
m["page"] = strconv.Itoa(*s.page)
}
if s.limit != nil {
m["limit"] = strconv.Itoa(*s.limit)
}
if s.after != nil {
m["after"] = *s.after
}
if s.orderAsc != nil {
if *s.orderAsc {
m["order"] = "asc"
} else {
m["order"] = "desc"
}
}
if resp, err = s.client.request(ctx).
SetQueryParams(m).
SetResult(&res).
SetError(&apiError).
Get("files"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FileDeleteService is a service to delete a file.
type FileDeleteService struct {
client *Client
documentID string
}
// NewFileDeleteService creates a new FileDeleteService.
func NewFileDeleteService(client *Client) *FileDeleteService {
return &FileDeleteService{client: client}
}
// SetDocumentID sets the document_id parameter of the FileDeleteService.
func (s *FileDeleteService) SetDocumentID(documentID string) *FileDeleteService {
s.documentID = documentID
return s
}
// Do makes the request.
func (s *FileDeleteService) Do(ctx context.Context) (err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
if resp, err = s.client.request(ctx).
SetPathParam("document_id", s.documentID).
SetError(&apiError).
Delete("document/{document_id}"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FileGetService is a service to get a file.
type FileGetService struct {
client *Client
documentID string
}
// FileGetResponse is the response of the FileGetService.
type FileGetResponse = FileListKnowledgeItem
// NewFileGetService creates a new FileGetService.
func NewFileGetService(client *Client) *FileGetService {
return &FileGetService{client: client}
}
// SetDocumentID sets the document_id parameter of the FileGetService.
func (s *FileGetService) SetDocumentID(documentID string) *FileGetService {
s.documentID = documentID
return s
}
// Do makes the request.
func (s *FileGetService) Do(ctx context.Context) (res FileGetResponse, err error) {
var (
resp *resty.Response
apiError APIErrorResponse
)
if resp, err = s.client.request(ctx).
SetPathParam("document_id", s.documentID).
SetResult(&res).
SetError(&apiError).
Get("document/{document_id}"); err != nil {
return
}
if resp.IsError() {
err = apiError
return
}
return
}
// FileDownloadService is a service to download a file.
type FileDownloadService struct {
client *Client
fileID string
writer io.Writer
filename string
}
// NewFileDownloadService creates a new FileDownloadService.
func NewFileDownloadService(client *Client) *FileDownloadService {
return &FileDownloadService{client: client}
}
// SetFileID sets the file_id parameter of the FileDownloadService.
func (s *FileDownloadService) SetFileID(fileID string) *FileDownloadService {
s.fileID = fileID
return s
}
// SetOutput sets the output parameter of the FileDownloadService.
func (s *FileDownloadService) SetOutput(w io.Writer) *FileDownloadService {
s.writer = w
return s
}
// SetOutputFile sets the output_file parameter of the FileDownloadService.
func (s *FileDownloadService) SetOutputFile(filename string) *FileDownloadService {
s.filename = filename
return s
}
// Do makes the request.
func (s *FileDownloadService) Do(ctx context.Context) (err error) {
var resp *resty.Response
writer := s.writer
if writer == nil && s.filename != "" {
var f *os.File
if f, err = os.Create(s.filename); err != nil {
return
}
defer f.Close()
writer = f
}
if writer == nil {
return errors.New("no output specified")
}
if resp, err = s.client.request(ctx).
SetDoNotParseResponse(true).
SetPathParam("file_id", s.fileID).
Get("files/{file_id}/content"); err != nil {
return
}
defer resp.RawBody().Close()
_, err = io.Copy(writer, resp.RawBody())
return
}