重构了一下 rag的方法, 使用 goframe的框架, 还有redis连接部分
This commit is contained in:
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// Agent AGENT 管理
|
||||
@@ -56,44 +57,44 @@ type ListAgentsRes struct {
|
||||
|
||||
// CreateAgent 创建 Agent
|
||||
// POST /api/v1/agents
|
||||
func (c *Client) CreateAgent(ctx context.Context, req *CreateAgentReq) error {
|
||||
func (c *Client) CreateAgent(ctx context.Context, req *CreateAgentReq) (err error) {
|
||||
var res CommonResponse
|
||||
if err := c.request(ctx, "POST", "/api/v1/agents", req, &res); err != nil {
|
||||
return fmt.Errorf("create agent failed: %w", err)
|
||||
if err = c.request(ctx, "POST", "/api/v1/agents", req, &res); err != nil {
|
||||
return gerror.Newf("create agent failed: %v", err)
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("create agent failed: %s", res.Message)
|
||||
return gerror.Newf("create agent failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateAgent 更新 Agent
|
||||
// PUT /api/v1/agents/{agent_id}
|
||||
func (c *Client) UpdateAgent(ctx context.Context, agentID string, req *UpdateAgentReq) error {
|
||||
path := fmt.Sprintf("/api/v1/agents/%s", agentID)
|
||||
func (c *Client) UpdateAgent(ctx context.Context, agentID string, req *UpdateAgentReq) (err error) {
|
||||
path := "/api/v1/agents/" + agentID
|
||||
var res CommonResponse
|
||||
if err := c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return fmt.Errorf("update agent failed: %w", err)
|
||||
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return gerror.Newf("update agent failed: %v", err)
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("update agent failed: %s", res.Message)
|
||||
return gerror.Newf("update agent failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteAgent 删除 Agent
|
||||
// DELETE /api/v1/agents/{agent_id}
|
||||
func (c *Client) DeleteAgent(ctx context.Context, agentID string) error {
|
||||
path := fmt.Sprintf("/api/v1/agents/%s", agentID)
|
||||
func (c *Client) DeleteAgent(ctx context.Context, agentID string) (err error) {
|
||||
path := "/api/v1/agents/" + agentID
|
||||
var res CommonResponse
|
||||
// 官方文档要求传空对象,不是 nil
|
||||
if err := c.request(ctx, "DELETE", path, map[string]interface{}{}, &res); err != nil {
|
||||
return fmt.Errorf("delete agent failed: %w", err)
|
||||
if err = c.request(ctx, "DELETE", path, map[string]interface{}{}, &res); err != nil {
|
||||
return gerror.Newf("delete agent failed: %v", err)
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete agent failed: %s", res.Message)
|
||||
return gerror.Newf("delete agent failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// ListAgents 列出 Agent
|
||||
@@ -131,10 +132,10 @@ func (c *Client) ListAgents(ctx context.Context, req *ListAgentsReq) (*ListAgent
|
||||
|
||||
var res ListAgentsRes
|
||||
if err := c.request(ctx, "GET", path, nil, &res); err != nil {
|
||||
return nil, fmt.Errorf("list agents failed: %w", err)
|
||||
return nil, gerror.Newf("list agents failed: %v", err)
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list agents failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list agents failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// 聊天助手管理
|
||||
@@ -104,7 +105,7 @@ func (c *Client) CreateChat(ctx context.Context, req *CreateChatReq) (*Chat, err
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("create chat failed: %s", res.Msg)
|
||||
return nil, gerror.Newf("create chat failed: %s", res.Msg)
|
||||
}
|
||||
return res.Data, nil
|
||||
}
|
||||
@@ -144,33 +145,33 @@ func (c *Client) ListChats(ctx context.Context, req *ListChatsReq) (*ListChatsRe
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list chats failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list chats failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// DeleteChats 删除聊天助手
|
||||
func (c *Client) DeleteChats(ctx context.Context, ids []string) error {
|
||||
func (c *Client) DeleteChats(ctx context.Context, ids []string) (err error) {
|
||||
req := DeleteChatsReq{Ids: ids}
|
||||
var res CommonResponse
|
||||
if err := c.request(ctx, "DELETE", "/api/v1/chats", req, &res); err != nil {
|
||||
return err
|
||||
if err = c.request(ctx, "DELETE", "/api/v1/chats", req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete chats failed: %s", res.Message)
|
||||
return gerror.Newf("delete chats failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateChat 更新聊天助手
|
||||
func (c *Client) UpdateChat(ctx context.Context, id string, req *UpdateChatReq) error {
|
||||
func (c *Client) UpdateChat(ctx context.Context, id string, req *UpdateChatReq) (err error) {
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/chats/%s", id)
|
||||
if err := c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return err
|
||||
path := "/api/v1/chats/" + id
|
||||
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("update chat failed: %s", res.Message)
|
||||
return gerror.Newf("update chat failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
)
|
||||
@@ -33,7 +32,7 @@ func init() {
|
||||
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
|
||||
globalClient = &Client{
|
||||
@@ -79,20 +78,19 @@ func (r *CommonResponse) IsSuccess() bool {
|
||||
}
|
||||
|
||||
// request 发送 HTTP 请求
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) error {
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
||||
fullURL := c.BaseURL + path
|
||||
|
||||
var reqBody io.Reader
|
||||
var reqBody string
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
jsonData, err := gjson.Encode(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal request body failed: %w", err)
|
||||
return gerror.Newf("marshal request body failed: %v", err)
|
||||
}
|
||||
reqBody = strings.NewReader(string(jsonData))
|
||||
reqBody = string(jsonData)
|
||||
}
|
||||
|
||||
var resp *gclient.Response
|
||||
var err error
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
@@ -104,28 +102,24 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
case "DELETE":
|
||||
resp, err = c.HTTPClient.Delete(ctx, fullURL, reqBody)
|
||||
default:
|
||||
return fmt.Errorf("unsupported method: %s", method)
|
||||
return gerror.Newf("unsupported method: %s", method)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request failed: %w", err)
|
||||
return gerror.Newf("http request failed: %v", err)
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("http request failed with status: %d", resp.StatusCode)
|
||||
return gerror.Newf("http request failed with status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
respBody := resp.ReadAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response body failed: %w", err)
|
||||
if err = gjson.DecodeTo(respBody, result); err != nil {
|
||||
return gerror.Newf("unmarshal response failed: %v", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(respBody, result); err != nil {
|
||||
return fmt.Errorf("unmarshal response failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// buildQueryString 构建查询字符串
|
||||
@@ -134,9 +128,9 @@ func buildQueryString(params map[string]interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
var parts []string
|
||||
parts := make([]string, 0, len(params))
|
||||
for k, v := range params {
|
||||
parts = append(parts, fmt.Sprintf("%s=%v", url.QueryEscape(k), url.QueryEscape(fmt.Sprintf("%v", v))))
|
||||
parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(g.NewVar(v).String()))
|
||||
}
|
||||
return strings.Join(parts, "&")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// 数据集管理
|
||||
@@ -90,7 +91,7 @@ func (c *Client) CreateDataset(ctx context.Context, req *CreateDatasetReq) (*Dat
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("create dataset failed: %s", res.Msg)
|
||||
return nil, gerror.Newf("create dataset failed: %s", res.Msg)
|
||||
}
|
||||
return res.Data, nil
|
||||
}
|
||||
@@ -134,33 +135,33 @@ func (c *Client) ListDatasets(ctx context.Context, req *ListDatasetsReq) (*ListD
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list datasets failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list datasets failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// DeleteDataset 删除数据集
|
||||
func (c *Client) DeleteDataset(ctx context.Context, ids []string) error {
|
||||
func (c *Client) DeleteDataset(ctx context.Context, ids []string) (err error) {
|
||||
req := DeleteDatasetsReq{Ids: ids}
|
||||
var res CommonResponse
|
||||
if err := c.request(ctx, "DELETE", "/api/v1/datasets", req, &res); err != nil {
|
||||
return err
|
||||
if err = c.request(ctx, "DELETE", "/api/v1/datasets", req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete dataset failed: %s", res.Message)
|
||||
return gerror.Newf("delete dataset failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateDataset 更新数据集
|
||||
func (c *Client) UpdateDataset(ctx context.Context, id string, req *UpdateDatasetReq) error {
|
||||
func (c *Client) UpdateDataset(ctx context.Context, id string, req *UpdateDatasetReq) (err error) {
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s", id)
|
||||
if err := c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return err
|
||||
path := "/api/v1/datasets/" + id
|
||||
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("update dataset failed: %s", res.Message)
|
||||
return gerror.Newf("update dataset failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// 数据集内文件管理
|
||||
@@ -70,7 +71,7 @@ type DeleteDocumentsReq struct {
|
||||
|
||||
// ListDocuments 列出文档
|
||||
func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListDocumentsReq) (*ListDocumentsRes, error) {
|
||||
path := fmt.Sprintf("/api/v1/datasets/%s/documents", datasetId)
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents"
|
||||
params := map[string]interface{}{}
|
||||
if req.Page > 0 {
|
||||
params["page"] = req.Page
|
||||
@@ -111,16 +112,14 @@ func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListD
|
||||
|
||||
// 处理数组参数:suffix(文件后缀过滤)
|
||||
// API 要求多个值时重复参数名,如:suffix=pdf&suffix=txt
|
||||
// 这里使用 fmt.Sprintf 来构造每个参数值
|
||||
for _, suffix := range req.Suffix {
|
||||
queryParts = append(queryParts, fmt.Sprintf("suffix=%s", suffix))
|
||||
queryParts = append(queryParts, "suffix="+suffix)
|
||||
}
|
||||
|
||||
// 处理数组参数:run(处理状态过滤)
|
||||
// 支持数字格式("0"-"4")或文本格式("UNSTART", "RUNNING", "CANCEL", "DONE", "FAIL")
|
||||
// 这里使用 fmt.Sprintf 来构造每个参数值
|
||||
for _, run := range req.Run {
|
||||
queryParts = append(queryParts, fmt.Sprintf("run=%s", run))
|
||||
queryParts = append(queryParts, "run="+run)
|
||||
}
|
||||
|
||||
// 构造最终请求路径
|
||||
@@ -134,7 +133,7 @@ func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListD
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list documents failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list documents failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
@@ -142,23 +141,21 @@ func (c *Client) ListDocuments(ctx context.Context, datasetId string, req *ListD
|
||||
// UploadDocument 上传文档
|
||||
// 注意:此方法需要特殊处理 multipart/form-data,目前的 request 方法可能不支持
|
||||
// 我们需要扩展 request 方法或在此处单独实现
|
||||
func (c *Client) UploadDocument(ctx context.Context, datasetId string, filePaths []string) error {
|
||||
func (c *Client) UploadDocument(ctx context.Context, datasetId string, filePaths []string) (err error) {
|
||||
// TODO: 实现文件上传逻辑,需要使用 gclient 的 UploadFile 功能
|
||||
// 由于 request 方法封装了 JSON 处理,这里可能需要绕过 request 方法直接使用 c.Client
|
||||
// 暂时留空或仅做简单提示,待完善 Client 封装以支持文件上传
|
||||
return fmt.Errorf("upload document not implemented yet")
|
||||
return gerror.New("upload document not implemented yet")
|
||||
}
|
||||
|
||||
// DeleteDocument 删除文档
|
||||
func (c *Client) DeleteDocument(ctx context.Context, datasetId string, ids []string) error {
|
||||
func (c *Client) DeleteDocument(ctx context.Context, datasetId string, ids []string) (err 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
|
||||
path := "/api/v1/datasets/" + datasetId + "/documents"
|
||||
if err = c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete document failed: %s", res.Message)
|
||||
return gerror.Newf("delete document failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,8 +2,9 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// OpenAICompatibleAPI 与 OpenAI 兼容的 API
|
||||
@@ -64,11 +65,11 @@ type ChatCompletionChunk struct {
|
||||
// CreateChatCompletion 创建聊天补全(与聊天助手)
|
||||
// POST /api/v1/chats_openai/{chat_id}/chat/completions
|
||||
func (c *Client) CreateChatCompletion(ctx context.Context, chatID string, req *ChatCompletionRequest) (*ChatCompletionResponse, error) {
|
||||
path := fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
|
||||
path := "/api/v1/chats_openai/" + chatID + "/chat/completions"
|
||||
|
||||
var resp ChatCompletionResponse
|
||||
if err := c.request(ctx, "POST", path, req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("create chat completion failed: %w", err)
|
||||
return nil, gerror.Newf("create chat completion failed: %v", err)
|
||||
}
|
||||
|
||||
return &resp, nil
|
||||
@@ -77,11 +78,11 @@ func (c *Client) CreateChatCompletion(ctx context.Context, chatID string, req *C
|
||||
// CreateAgentCompletion 创建 Agent 补全
|
||||
// POST /api/v1/agents_openai/{agent_id}/chat/completions
|
||||
func (c *Client) CreateAgentCompletion(ctx context.Context, agentID string, req *ChatCompletionRequest) (*ChatCompletionResponse, error) {
|
||||
path := fmt.Sprintf("/api/v1/agents_openai/%s/chat/completions", agentID)
|
||||
path := "/api/v1/agents_openai/" + agentID + "/chat/completions"
|
||||
|
||||
var resp ChatCompletionResponse
|
||||
if err := c.request(ctx, "POST", path, req, &resp); err != nil {
|
||||
return nil, fmt.Errorf("create agent completion failed: %w", err)
|
||||
return nil, gerror.Newf("create agent completion failed: %v", err)
|
||||
}
|
||||
|
||||
return &resp, nil
|
||||
@@ -91,31 +92,26 @@ func (c *Client) CreateAgentCompletion(ctx context.Context, agentID string, req
|
||||
// 注意:流式响应需要特殊处理,这里返回一个可用于读取流的接口
|
||||
func (c *Client) CreateChatCompletionStream(ctx context.Context, chatID string, req *ChatCompletionRequest) (*StreamReader, error) {
|
||||
req.Stream = true
|
||||
_ = fmt.Sprintf("/api/v1/chats_openai/%s/chat/completions", chatID)
|
||||
|
||||
// TODO: 实现流式读取逻辑
|
||||
return nil, fmt.Errorf("stream mode not implemented yet")
|
||||
return nil, gerror.New("stream mode not implemented yet")
|
||||
}
|
||||
|
||||
// StreamReader 流式响应读取器
|
||||
type StreamReader struct {
|
||||
decoder *json.Decoder
|
||||
close func() error
|
||||
_ *gjson.Json // TODO: 实现流式读取时使用
|
||||
close func() error
|
||||
}
|
||||
|
||||
// ReadChunk 读取下一个响应块
|
||||
// TODO: 实现流式读取逻辑
|
||||
func (sr *StreamReader) ReadChunk() (*ChatCompletionChunk, error) {
|
||||
var chunk ChatCompletionChunk
|
||||
if err := sr.decoder.Decode(&chunk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &chunk, nil
|
||||
return nil, gerror.New("stream mode not implemented yet")
|
||||
}
|
||||
|
||||
// Close 关闭流
|
||||
func (sr *StreamReader) Close() error {
|
||||
func (sr *StreamReader) Close() (err error) {
|
||||
if sr.close != nil {
|
||||
return sr.close()
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// 会话管理
|
||||
@@ -76,7 +77,7 @@ type ChatCompletionRes struct {
|
||||
|
||||
// CreateSession 创建会话
|
||||
func (c *Client) CreateSession(ctx context.Context, chatId string, req *CreateSessionReq) (*Session, error) {
|
||||
path := fmt.Sprintf("/api/v1/chats/%s/sessions", chatId)
|
||||
path := "/api/v1/chats/" + chatId + "/sessions"
|
||||
var res struct {
|
||||
Code int `json:"code"`
|
||||
Data *Session `json:"data"`
|
||||
@@ -86,14 +87,14 @@ func (c *Client) CreateSession(ctx context.Context, chatId string, req *CreateSe
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("create session failed: %s", res.Msg)
|
||||
return nil, gerror.Newf("create session failed: %s", res.Msg)
|
||||
}
|
||||
return res.Data, nil
|
||||
}
|
||||
|
||||
// ListSessions 列出会话
|
||||
func (c *Client) ListSessions(ctx context.Context, chatId string, req *ListSessionsReq) (*ListSessionsRes, error) {
|
||||
path := fmt.Sprintf("/api/v1/chats/%s/sessions", chatId)
|
||||
path := "/api/v1/chats/" + chatId + "/sessions"
|
||||
params := map[string]interface{}{}
|
||||
if req.Page > 0 {
|
||||
params["page"] = req.Page
|
||||
@@ -129,40 +130,40 @@ func (c *Client) ListSessions(ctx context.Context, chatId string, req *ListSessi
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("list sessions failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("list sessions failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
// DeleteSessions 删除会话
|
||||
func (c *Client) DeleteSessions(ctx context.Context, chatId string, ids []string) error {
|
||||
func (c *Client) DeleteSessions(ctx context.Context, chatId string, ids []string) (err error) {
|
||||
req := DeleteSessionsReq{Ids: ids}
|
||||
var res CommonResponse
|
||||
path := fmt.Sprintf("/api/v1/chats/%s/sessions", chatId)
|
||||
if err := c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return err
|
||||
path := "/api/v1/chats/" + chatId + "/sessions"
|
||||
if err = c.request(ctx, "DELETE", path, req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
if !res.IsSuccess() {
|
||||
return fmt.Errorf("delete sessions failed: %s", res.Message)
|
||||
return gerror.Newf("delete sessions failed: %s", res.Message)
|
||||
}
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// ChatCompletion 对话 (目前仅支持非流式)
|
||||
func (c *Client) ChatCompletion(ctx context.Context, chatId string, req *ChatCompletionReq) (*ChatCompletionRes, error) {
|
||||
path := fmt.Sprintf("/api/v1/chats/%s/completions", chatId)
|
||||
path := "/api/v1/chats/" + chatId + "/completions"
|
||||
var res ChatCompletionRes
|
||||
|
||||
// 如果需要流式支持,需要使用 gclient 的流式处理能力,这里暂只实现非流式
|
||||
if req.Stream {
|
||||
return nil, fmt.Errorf("stream mode not supported yet")
|
||||
return nil, gerror.New("stream mode not supported yet")
|
||||
}
|
||||
|
||||
if err := c.request(ctx, "POST", path, req, &res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, fmt.Errorf("chat completion failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("chat completion failed: code=%d", res.Code)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
)
|
||||
|
||||
// System 系统管理
|
||||
@@ -10,11 +11,11 @@ import (
|
||||
|
||||
// HealthStatus 健康状态
|
||||
type HealthStatus struct {
|
||||
DB string `json:"db"` // "ok" 或 "nok"
|
||||
Redis string `json:"redis"` // "ok" 或 "nok"
|
||||
DocEngine string `json:"doc_engine"` // "ok" 或 "nok"
|
||||
Storage string `json:"storage"` // "ok" 或 "nok"
|
||||
Status string `json:"status"` // 整体状态: "ok" 或 "nok"
|
||||
DB string `json:"db"` // "ok" 或 "nok"
|
||||
Redis string `json:"redis"` // "ok" 或 "nok"
|
||||
DocEngine string `json:"doc_engine"` // "ok" 或 "nok"
|
||||
Storage string `json:"storage"` // "ok" 或 "nok"
|
||||
Status string `json:"status"` // 整体状态: "ok" 或 "nok"
|
||||
Meta map[string]interface{} `json:"_meta,omitempty"` // 详细错误信息
|
||||
}
|
||||
|
||||
@@ -23,7 +24,7 @@ type HealthStatus struct {
|
||||
func (c *Client) CheckHealth(ctx context.Context) (*HealthStatus, error) {
|
||||
var status HealthStatus
|
||||
if err := c.request(ctx, "GET", "/v1/system/healthz", nil, &status); err != nil {
|
||||
return nil, fmt.Errorf("check health failed: %w", err)
|
||||
return nil, gerror.Newf("check health failed: %v", err)
|
||||
}
|
||||
return &status, nil
|
||||
}
|
||||
@@ -36,4 +37,3 @@ func (c *Client) IsHealthy(ctx context.Context) (bool, error) {
|
||||
}
|
||||
return status.Status == "ok", nil
|
||||
}
|
||||
|
||||
|
||||
@@ -149,12 +149,18 @@ func (q *QueueProcessor) Start(ctx context.Context) error {
|
||||
glog.Infof(ctx, "Stream 处理器启动 - Stream: %s, 消费者组: %s, 消费者: %s, 超时: %dms",
|
||||
q.streamKey, q.groupName, q.consumerName, q.timeout)
|
||||
|
||||
loopCount := 0
|
||||
for {
|
||||
select {
|
||||
case <-q.stopChan:
|
||||
glog.Info(ctx, "Stream 处理器收到停止信号")
|
||||
return nil
|
||||
default:
|
||||
loopCount++
|
||||
if loopCount%10 == 1 {
|
||||
glog.Debugf(ctx, "[DEBUG] 第 %d 次循环,准备读取消息...", loopCount)
|
||||
}
|
||||
|
||||
// 从 Redis Stream 中读取消息
|
||||
messages, err := q.fetchMessages(ctx)
|
||||
if err != nil {
|
||||
@@ -164,11 +170,17 @@ func (q *QueueProcessor) Start(ctx context.Context) error {
|
||||
|
||||
// 没有新消息,继续等待
|
||||
if len(messages) == 0 {
|
||||
if loopCount%10 == 1 {
|
||||
glog.Debugf(ctx, "[DEBUG] 第 %d 次循环,无新消息", loopCount)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
glog.Infof(ctx, "[DEBUG] 收到 %d 条消息", len(messages))
|
||||
|
||||
// 处理每条消息
|
||||
for _, msg := range messages {
|
||||
glog.Infof(ctx, "[DEBUG] 处理消息 ID: %s, Values: %+v", msg.ID, msg.Values)
|
||||
// 提交到协程池处理
|
||||
if err := q.submitTask(ctx, msg); err != nil {
|
||||
glog.Errorf(ctx, "提交任务到协程池失败: %v, 消息ID: %s", err, msg.ID)
|
||||
|
||||
Reference in New Issue
Block a user