重构了一下 rag的方法, 使用 goframe的框架, 还有redis连接部分
This commit is contained in:
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// 数据集内知识块管理
|
||||
@@ -90,7 +91,7 @@ type RetrieveChunksRes struct {
|
||||
|
||||
// AddChunk 添加知识块
|
||||
func (c *Client) AddChunk(ctx context.Context, datasetId, documentId string, req *AddChunkReq) (*Chunk, error) {
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents/%s/chunks", datasetId, documentId)
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents/" + documentId + "/chunks"
|
||||
var res struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
@@ -102,14 +103,14 @@ func (c *Client) AddChunk(ctx context.Context, datasetId, documentId string, req
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("add chunk failed: %s", res.Msg)
|
||||
return nil, gerror.Newf("add chunk failed: %s", res.Msg)
|
||||
}
|
||||
return res.Data.Chunk, nil
|
||||
}
|
||||
|
||||
// ListChunks 列出知识块
|
||||
func (c *Client) ListChunks(ctx context.Context, datasetId, documentId string, req *ListChunksReq) (*ListChunksRes, error) {
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents/%s/chunks", datasetId, documentId)
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents/" + documentId + "/chunks"
|
||||
params := map[string]interface{}{}
|
||||
if req.Keywords != "" {
|
||||
params["keywords"] = req.Keywords
|
||||
@@ -134,36 +135,36 @@ func (c *Client) ListChunks(ctx context.Context, datasetId, documentId string, r
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list chunks failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list chunks failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// DeleteChunks 删除知识块
|
||||
func (c *Client) DeleteChunks(ctx context.Context, datasetId, documentId string, chunkIds []string) error {
|
||||
func (c *Client) DeleteChunks(ctx context.Context, datasetId, documentId string, chunkIds []string) (err error) {
|
||||
req := DeleteChunksReq{ChunkIds: chunkIds}
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents/%s/chunks", datasetId, documentId)
|
||||
if err := c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return err
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents/" + documentId + "/chunks"
|
||||
if err = c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete chunks failed: %s", res.Message)
|
||||
return gerror.Newf("delete chunks failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateChunk 更新知识块
|
||||
func (c *Client) UpdateChunk(ctx context.Context, datasetId, documentId, chunkId string, req *UpdateChunkReq) error {
|
||||
func (c *Client) UpdateChunk(ctx context.Context, datasetId, documentId, chunkId string, req *UpdateChunkReq) (err error) {
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents/%s/chunks/%s", datasetId, documentId, chunkId)
|
||||
if err := c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return err
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents/" + documentId + "/chunks/" + chunkId
|
||||
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("update chunk failed: %s", res.Message)
|
||||
return gerror.Newf("update chunk failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// RetrieveChunks 检索知识块
|
||||
@@ -173,7 +174,7 @@ func (c *Client) RetrieveChunks(ctx context.Context, req *RetrieveChunksReq) (*R
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("retrieve chunks failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("retrieve chunks failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user