ragflow的dto
This commit is contained in:
132
ragflow/document.go
Normal file
132
ragflow/document.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Document 结构体
|
||||
type Document struct {
|
||||
Id string `json:"id"`
|
||||
DatasetId string `json:"dataset_id"`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
Location string `json:"location"`
|
||||
CreatedBy string `json:"created_by"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
Thumbnail string `json:"thumbnail"`
|
||||
Type string `json:"type"`
|
||||
RunStatus string `json:"run_status"` // 对应 API 返回的 "run" 字段,可能需要确认
|
||||
Status string `json:"status"`
|
||||
ChunkMethod string `json:"chunk_method"`
|
||||
ParserConfig map[string]interface{} `json:"parser_config"`
|
||||
TokenNum int `json:"token_num"`
|
||||
ChunkCount int `json:"chunk_count"`
|
||||
ProcessBegin int64 `json:"process_begin"`
|
||||
ProcessDu int64 `json:"process_du"`
|
||||
Progress float64 `json:"progress"`
|
||||
ProgressMsg string `json:"progress_msg"`
|
||||
}
|
||||
|
||||
// UploadDocumentReq 上传文档请求
|
||||
// 注意:上传文件通常需要 multipart/form-data,这里仅定义结构,实际逻辑在方法中处理
|
||||
type UploadDocumentReq struct {
|
||||
FilePaths []string // 本地文件路径列表
|
||||
}
|
||||
|
||||
// ListDocumentsReq 列出文档请求
|
||||
type ListDocumentsReq struct {
|
||||
Page int `json:"page,omitempty"`
|
||||
PageSize int `json:"page_size,omitempty"`
|
||||
OrderBy string `json:"orderby,omitempty"`
|
||||
Desc bool `json:"desc,omitempty"`
|
||||
Keywords string `json:"keywords,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
CreateTimeFrom int64 `json:"create_time_from,omitempty"`
|
||||
CreateTimeTo int64 `json:"create_time_to,omitempty"`
|
||||
}
|
||||
|
||||
// ListDocumentsRes 列出文档响应
|
||||
type ListDocumentsRes struct {
|
||||
Code int `json:"code"`
|
||||
Data []*Document `json:"data"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// DeleteDocumentsReq 删除文档请求
|
||||
type DeleteDocumentsReq struct {
|
||||
Ids []string `json:"ids"`
|
||||
}
|
||||
|
||||
// ListDocuments 列出文档
|
||||
func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListDocumentsReq) (*ListDocumentsRes, error) {
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents?", datasetId)
|
||||
params := map[string]interface{}{}
|
||||
if req.Page > 0 {
|
||||
params["page"] = req.Page
|
||||
}
|
||||
if req.PageSize > 0 {
|
||||
params["page_size"] = req.PageSize
|
||||
}
|
||||
if req.OrderBy != "" {
|
||||
params["orderby"] = req.OrderBy
|
||||
}
|
||||
if req.Desc {
|
||||
params["desc"] = "true"
|
||||
} else {
|
||||
params["desc"] = "false"
|
||||
}
|
||||
if req.Keywords != "" {
|
||||
params["keywords"] = req.Keywords
|
||||
}
|
||||
if req.Id != "" {
|
||||
params["id"] = req.Id
|
||||
}
|
||||
if req.Name != "" {
|
||||
params["name"] = req.Name
|
||||
}
|
||||
if req.CreateTimeFrom > 0 {
|
||||
params["create_time_from"] = req.CreateTimeFrom
|
||||
}
|
||||
if req.CreateTimeTo > 0 {
|
||||
params["create_time_to"] = req.CreateTimeTo
|
||||
}
|
||||
|
||||
for k, v := range params {
|
||||
path += fmt.Sprintf("%s=%v&", k, v)
|
||||
}
|
||||
|
||||
var res ListDocumentsRes
|
||||
if err := c.request(ctx, "GET", path, nil, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list documents failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// UploadDocument 上传文档
|
||||
// 注意:此方法需要特殊处理 multipart/form-data,目前的 request 方法可能不支持
|
||||
// 我们需要扩展 request 方法或在此处单独实现
|
||||
func (c *Client) UploadDocument(ctx context.Context, datasetId string, filePaths []string) error {
|
||||
// TODO: 实现文件上传逻辑,需要使用 gclient 的 UploadFile 功能
|
||||
// 由于 request 方法封装了 JSON 处理,这里可能需要绕过 request 方法直接使用 c.Client
|
||||
// 暂时留空或仅做简单提示,待完善 Client 封装以支持文件上传
|
||||
return fmt.Errorf("upload document not implemented yet")
|
||||
}
|
||||
|
||||
// DeleteDocument 删除文档
|
||||
func (c *Client) DeleteDocument(ctx context.Context, datasetId string, ids []string) error {
|
||||
req := DeleteDocumentsReq{Ids: ids}
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents", datasetId)
|
||||
if err := c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete document failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user