重构了一下 rag的方法, 使用 goframe的框架, 还有redis连接部分

This commit is contained in:
Cold
2025-12-06 18:04:29 +08:00
committed by 张斌
parent f7cb007491
commit 4b2b5e6177
16 changed files with 398 additions and 260 deletions

View File

@@ -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
}