-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatasets.go
259 lines (231 loc) · 7.85 KB
/
datasets.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
package coze
import (
"context"
"fmt"
"net/http"
"strconv"
)
type datasets struct {
client *core
Documents *datasetsDocuments
Images *datasetsImages
}
func newDatasets(core *core) *datasets {
return &datasets{
client: core,
Documents: newDatasetsDocuments(core),
Images: newDatasetsImages(core),
}
}
func (r *datasets) Create(ctx context.Context, req *CreateDatasetsReq) (*CreateDatasetResp, error) {
method := http.MethodPost
uri := "/v1/datasets"
resp := &createDatasetResp{}
err := r.client.Request(ctx, method, uri, req, resp)
if err != nil {
return nil, err
}
resp.Data.setHTTPResponse(resp.HTTPResponse)
return resp.Data, nil
}
func (r *datasets) List(ctx context.Context, req *ListDatasetsReq) (NumberPaged[Dataset], error) {
if req.PageSize == 0 {
req.PageSize = 10 // 设置默认值为10
}
if req.PageNum == 0 {
req.PageNum = 1
}
return NewNumberPaged[Dataset](
func(request *pageRequest) (*pageResponse[Dataset], error) {
uri := "/v1/datasets"
resp := &listDatasetsResp{}
queries := []RequestOption{}
if req.SpaceID != "" {
queries = append(queries, withHTTPQuery("space_id", req.SpaceID))
}
if req.Name != "" {
queries = append(queries, withHTTPQuery("name", req.Name))
}
if req.FormatType != 0 {
queries = append(queries, withHTTPQuery("format_type", strconv.Itoa(int(req.FormatType))))
}
queries = append(queries,
withHTTPQuery("page_num", strconv.Itoa(request.PageNum)),
withHTTPQuery("page_size", strconv.Itoa(request.PageSize)),
)
err := r.client.Request(ctx, http.MethodGet, uri, nil, resp, queries...)
if err != nil {
return nil, err
}
return &pageResponse[Dataset]{
Total: resp.Data.TotalCount,
HasMore: len(resp.Data.DatasetList) >= request.PageSize,
Data: resp.Data.DatasetList,
LogID: resp.HTTPResponse.LogID(),
}, nil
}, req.PageSize, req.PageNum)
}
func (r *datasets) Update(ctx context.Context, req *UpdateDatasetsReq) (*UpdateDatasetsResp, error) {
method := http.MethodPut
uri := fmt.Sprintf("/v1/datasets/%s", req.DatasetID)
resp := &updateDatasetResp{}
err := r.client.Request(ctx, method, uri, req, resp)
if err != nil {
return nil, err
}
result := &UpdateDatasetsResp{}
result.setHTTPResponse(resp.HTTPResponse)
return result, nil
}
func (r *datasets) Delete(ctx context.Context, req *DeleteDatasetsReq) (*DeleteDatasetsResp, error) {
method := http.MethodDelete
uri := fmt.Sprintf("/v1/datasets/%s", req.DatasetID)
resp := &deleteDatasetResp{}
err := r.client.Request(ctx, method, uri, nil, resp)
if err != nil {
return nil, err
}
result := &DeleteDatasetsResp{}
result.setHTTPResponse(resp.HTTPResponse)
return result, nil
}
func (r *datasets) Process(ctx context.Context, req *ProcessDocumentsReq) (*ProcessDocumentsResp, error) {
method := http.MethodPost
uri := fmt.Sprintf("/v1/datasets/%s/process", req.DatasetID)
resp := &processDocumentsResp{}
err := r.client.Request(ctx, method, uri, req, resp)
if err != nil {
return nil, err
}
resp.Data.setHTTPResponse(resp.HTTPResponse)
return resp.Data, nil
}
// DatasetStatus 表示数据集状态
type DatasetStatus int
const (
DatasetStatusEnabled DatasetStatus = 1
DatasetStatusDisabled DatasetStatus = 3
)
// Dataset 表示数据集信息
type Dataset struct {
ID string `json:"dataset_id"`
Name string `json:"name"`
Description string `json:"description"`
SpaceID string `json:"space_id"`
Status DatasetStatus `json:"status"`
FormatType DocumentFormatType `json:"format_type"`
CanEdit bool `json:"can_edit"`
IconURL string `json:"icon_url"`
DocCount int `json:"doc_count"`
FileList []string `json:"file_list"`
HitCount int `json:"hit_count"`
BotUsedCount int `json:"bot_used_count"`
SliceCount int `json:"slice_count"`
AllFileSize string `json:"all_file_size"`
ChunkStrategy *DocumentChunkStrategy `json:"chunk_strategy,omitempty"`
FailedFileList []string `json:"failed_file_list"`
ProcessingFileList []string `json:"processing_file_list"`
ProcessingFileIDList []string `json:"processing_file_id_list"`
AvatarURL string `json:"avatar_url"`
CreatorID string `json:"creator_id"`
CreatorName string `json:"creator_name"`
CreateTime int `json:"create_time"`
UpdateTime int `json:"update_time"`
}
// CreateDatasetsReq 表示创建数据集的请求
type CreateDatasetsReq struct {
Name string `json:"name"`
SpaceID string `json:"space_id"`
FormatType DocumentFormatType `json:"format_type"`
Description string `json:"description,omitempty"`
IconFileID string `json:"file_id,omitempty"`
}
// CreateDatasetResp 表示创建数据集的响应
type createDatasetResp struct {
baseResponse
Data *CreateDatasetResp `json:"data"`
}
type CreateDatasetResp struct {
baseModel
DatasetID string `json:"dataset_id"`
}
// ListDatasetsReq 表示列出数据集的请求
type ListDatasetsReq struct {
SpaceID string `json:"space_id"`
Name string `json:"name,omitempty"`
FormatType DocumentFormatType `json:"format_type,omitempty"`
PageNum int `json:"page_num"`
PageSize int `json:"page_size"`
}
func NewListDatasetsReq(spaceID string) *ListDatasetsReq {
return &ListDatasetsReq{
SpaceID: spaceID,
PageNum: 1,
PageSize: 10,
}
}
// ListDatasetsResp 表示列出数据集的响应
type listDatasetsResp struct {
baseResponse
Data *ListDatasetsResp `json:"data"`
}
type ListDatasetsResp struct {
baseModel
TotalCount int `json:"total_count"`
DatasetList []*Dataset `json:"dataset_list"`
}
// UpdateDatasetsReq 表示更新数据集的请求
type UpdateDatasetsReq struct {
DatasetID string `json:"-"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
IconFileID string `json:"file_id,omitempty"`
}
// UpdateDatasetResp 表示更新数据集的响应
type updateDatasetResp struct {
baseResponse
Data *UpdateDatasetsResp `json:"data"`
}
type UpdateDatasetsResp struct {
baseModel
}
// DeleteDatasetsReq 表示删除数据集的请求
type DeleteDatasetsReq struct {
DatasetID string `json:"-"`
}
// DeleteDatasetResp 表示删除数据集的响应
type deleteDatasetResp struct {
baseResponse
Data *DeleteDatasetsResp `json:"data"`
}
type DeleteDatasetsResp struct {
baseModel
}
// DocumentProgress 表示文档处理进度
type DocumentProgress struct {
DocumentID string `json:"document_id"`
URL string `json:"url"`
Size int `json:"size"`
Type string `json:"type"`
Status DocumentStatus `json:"status"`
Progress int `json:"progress"`
UpdateType DocumentUpdateType `json:"update_type"`
DocumentName string `json:"document_name"`
RemainingTime int `json:"remaining_time"`
StatusDescript string `json:"status_descript"`
UpdateInterval int `json:"update_interval"`
}
// ProcessDocumentsReq 表示处理文档的请求
type ProcessDocumentsReq struct {
DatasetID string `json:"-"`
DocumentIDs []string `json:"document_ids"`
}
// ProcessDocumentsResp 表示处理文档的响应
type processDocumentsResp struct {
baseResponse
Data *ProcessDocumentsResp `json:"data"`
}
type ProcessDocumentsResp struct {
baseModel
Data []*DocumentProgress `json:"data"`
}