Files
common/ragflow/session.go

170 lines
4.5 KiB
Go
Raw Normal View History

2025-11-27 09:50:12 +08:00
package ragflow
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
2025-11-27 09:50:12 +08:00
)
2025-11-27 17:38:42 +08:00
// 会话管理
// 参考: https://ragflow.com.cn/docs/dev/http_api_reference#会话管理
// Session 会话结构体
2025-11-27 09:50:12 +08:00
type Session struct {
Id string `json:"id"`
Name string `json:"name"`
ChatId string `json:"chat_id"` // 响应中是 "chat" 或 "chat_id",根据文档示例调整
Messages []Message `json:"messages"`
CreateDate string `json:"create_date"`
CreateTime int64 `json:"create_time"`
UpdateDate string `json:"update_date"`
UpdateTime int64 `json:"update_time"`
}
type Message struct {
Content string `json:"content"`
Role string `json:"role"`
}
// CreateSessionReq 创建会话请求
type CreateSessionReq struct {
Name string `json:"name"`
UserId string `json:"user_id,omitempty"`
}
// ListSessionsReq 列出会话请求
type ListSessionsReq struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
OrderBy string `json:"orderby,omitempty"`
Desc bool `json:"desc,omitempty"`
Name string `json:"name,omitempty"`
Id string `json:"id,omitempty"`
UserId string `json:"user_id,omitempty"`
}
// ListSessionsRes 列出会话响应
2025-12-02 14:59:07 +08:00
// 注意API 不返回 total 字段,仅返回 data 数组
2025-11-27 09:50:12 +08:00
type ListSessionsRes struct {
2025-12-02 14:59:07 +08:00
Code int `json:"code"` // 状态码0 表示成功
Data []*Session `json:"data"` // 会话列表
2025-11-27 09:50:12 +08:00
}
// DeleteSessionsReq 删除会话请求
type DeleteSessionsReq struct {
Ids []string `json:"ids"`
}
// ChatCompletionReq 对话请求
type ChatCompletionReq struct {
Question string `json:"question"`
Stream bool `json:"stream"`
SessionId string `json:"session_id,omitempty"`
UserId string `json:"user_id,omitempty"`
}
// ChatCompletionRes 对话响应 (非流式)
type ChatCompletionRes struct {
Code int `json:"code"`
Data struct {
Answer string `json:"answer"`
Reference interface{} `json:"reference"`
AudioBinary interface{} `json:"audio_binary"`
Id interface{} `json:"id"`
SessionId string `json:"session_id"`
} `json:"data"`
}
// CreateSession 创建会话
func (c *Client) CreateSession(ctx context.Context, chatId string, req *CreateSessionReq) (*Session, error) {
path := "/api/v1/chats/" + chatId + "/sessions"
2025-11-27 09:50:12 +08:00
var res struct {
Code int `json:"code"`
Data *Session `json:"data"`
Msg string `json:"message"`
}
if err := c.request(ctx, "POST", path, req, &res); err != nil {
return nil, err
}
if res.Code != 0 {
return nil, gerror.Newf("create session failed: %s", res.Msg)
2025-11-27 09:50:12 +08:00
}
return res.Data, nil
}
// ListSessions 列出会话
func (c *Client) ListSessions(ctx context.Context, chatId string, req *ListSessionsReq) (*ListSessionsRes, error) {
path := "/api/v1/chats/" + chatId + "/sessions"
2025-11-27 09:50:12 +08:00
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.Name != "" {
params["name"] = req.Name
}
if req.Id != "" {
params["id"] = req.Id
}
if req.UserId != "" {
params["user_id"] = req.UserId
}
2025-11-27 17:38:42 +08:00
query := buildQueryString(params)
if query != "" {
path += "?" + query
2025-11-27 09:50:12 +08:00
}
var res ListSessionsRes
if err := c.request(ctx, "GET", path, nil, &res); err != nil {
return nil, err
}
if res.Code != 0 {
return nil, gerror.Newf("list sessions failed: code=%d", res.Code)
2025-11-27 09:50:12 +08:00
}
return &res, nil
}
// DeleteSessions 删除会话
func (c *Client) DeleteSessions(ctx context.Context, chatId string, ids []string) (err error) {
2025-11-27 09:50:12 +08:00
req := DeleteSessionsReq{Ids: ids}
var res CommonResponse
path := "/api/v1/chats/" + chatId + "/sessions"
if err = c.request(ctx, "DELETE", path, req, &res); err != nil {
return
2025-11-27 09:50:12 +08:00
}
if !res.IsSuccess() {
return gerror.Newf("delete sessions failed: %s", res.Message)
2025-11-27 09:50:12 +08:00
}
return
2025-11-27 09:50:12 +08:00
}
// ChatCompletion 对话 (目前仅支持非流式)
func (c *Client) ChatCompletion(ctx context.Context, chatId string, req *ChatCompletionReq) (*ChatCompletionRes, error) {
path := "/api/v1/chats/" + chatId + "/completions"
2025-11-27 09:50:12 +08:00
var res ChatCompletionRes
// 如果需要流式支持,需要使用 gclient 的流式处理能力,这里暂只实现非流式
if req.Stream {
return nil, gerror.New("stream mode not supported yet")
2025-11-27 09:50:12 +08:00
}
if err := c.request(ctx, "POST", path, req, &res); err != nil {
return nil, err
}
if res.Code != 0 {
return nil, gerror.Newf("chat completion failed: code=%d", res.Code)
2025-11-27 09:50:12 +08:00
}
return &res, nil
}