知识库隔离与话术管理方案

This commit is contained in:
Cold
2025-12-22 17:50:53 +08:00
committed by 张斌
parent 3683aae9af
commit 08f8b2248a
3 changed files with 186 additions and 23 deletions

View File

@@ -1,10 +1,15 @@
// Package ragflow - RAGFlow文档管理
// 功能RAGFlow知识库文档的上传、列表、删除操作
package ragflow
import (
"bytes"
"context"
"strings"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
)
// 数据集内文件管理
@@ -138,12 +143,62 @@ func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListD
return &res, nil
}
// UploadDocument 上传文档
// 注意:此方法需要特殊处理 multipart/form-data目前的 request 方法可能不支持
// 我们需要扩展 request 方法或在此处单独实现
// UploadDocumentFromText 上传文本内容作为文档
func (c *Client) UploadDocumentFromText(ctx context.Context, datasetId, content, filename string) (documentId string, err error) {
if datasetId == "" {
return "", gerror.New("datasetId不能为空")
}
if content == "" {
return "", gerror.New("文档内容不能为空")
}
if filename == "" {
filename = "document.txt"
}
// 构造URL
url := c.BaseURL + "/api/v1/datasets/" + datasetId + "/documents"
// 使用gclient上传文本作为文件
client := c.HTTPClient.Clone()
client.SetHeader("Authorization", "Bearer "+c.APIKey)
// 使用ContentType方法上传multipart表单
resp, err := client.Post(ctx, url, g.Map{
"file": bytes.NewReader([]byte(content)),
})
if err != nil {
return "", err
}
defer resp.Close()
// 解析响应
var result struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Id string `json:"id"`
} `json:"data"`
}
bodyBytes := resp.ReadAll()
if err = gjson.DecodeTo(bodyBytes, &result); err != nil {
return "", err
}
if result.Code != 0 {
return "", gerror.Newf("上传文档失败 (code=%d): %s", result.Code, result.Message)
}
if result.Data.Id == "" {
return "", gerror.New("上传成功但未返回文档ID")
}
return result.Data.Id, nil
}
// UploadDocument 上传文档(保留兼容)
func (c *Client) UploadDocument(ctx context.Context, datasetId string, filePaths []string) (err error) {
// TODO: 实现文件上传逻辑,需要使用 gclient 的 UploadFile 功能
return gerror.New("upload document not implemented yet")
return gerror.New("upload document from file not implemented yet, use UploadDocumentFromText instead")
}
// DeleteDocument 删除文档