178 lines
5.0 KiB
Go
178 lines
5.0 KiB
Go
package ragflow
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
)
|
||
|
||
// 聊天助手管理
|
||
// 参考: https://ragflow.com.cn/docs/dev/http_api_reference#聊天助手管理
|
||
|
||
// Chat 聊天助手结构体
|
||
type Chat struct {
|
||
Id string `json:"id"`
|
||
Name string `json:"name"`
|
||
Avatar string `json:"avatar"`
|
||
DatasetIds []string `json:"dataset_ids"`
|
||
Llm Llm `json:"llm"`
|
||
Prompt Prompt `json:"prompt"`
|
||
Description string `json:"description"`
|
||
DoRefer string `json:"do_refer"`
|
||
Language string `json:"language"`
|
||
PromptType string `json:"prompt_type"`
|
||
Status string `json:"status"`
|
||
TenantId string `json:"tenant_id"`
|
||
TopK int `json:"top_k"`
|
||
CreateDate string `json:"create_date"`
|
||
CreateTime int64 `json:"create_time"`
|
||
UpdateDate string `json:"update_date"`
|
||
UpdateTime int64 `json:"update_time"`
|
||
}
|
||
|
||
type Llm struct {
|
||
ModelName string `json:"model_name,omitempty"`
|
||
Temperature float64 `json:"temperature,omitempty"`
|
||
TopP float64 `json:"top_p,omitempty"`
|
||
PresencePenalty float64 `json:"presence_penalty,omitempty"`
|
||
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
|
||
}
|
||
|
||
type Prompt struct {
|
||
SimilarityThreshold float64 `json:"similarity_threshold,omitempty"`
|
||
KeywordsSimilarityWeight float64 `json:"keywords_similarity_weight,omitempty"`
|
||
Opener string `json:"opener,omitempty"`
|
||
Prompt string `json:"prompt,omitempty"`
|
||
RerankModel string `json:"rerank_model,omitempty"`
|
||
TopN int `json:"top_n,omitempty"`
|
||
Variables []Variable `json:"variables,omitempty"`
|
||
EmptyResponse string `json:"empty_response,omitempty"`
|
||
}
|
||
|
||
type Variable struct {
|
||
Key string `json:"key"`
|
||
Optional bool `json:"optional"`
|
||
}
|
||
|
||
// CreateChatReq 创建聊天助手请求
|
||
type CreateChatReq struct {
|
||
Name string `json:"name"`
|
||
Avatar string `json:"avatar,omitempty"`
|
||
DatasetIds []string `json:"dataset_ids,omitempty"`
|
||
Llm *Llm `json:"llm,omitempty"`
|
||
Prompt *Prompt `json:"prompt,omitempty"`
|
||
}
|
||
|
||
// UpdateChatReq 更新聊天助手请求
|
||
type UpdateChatReq struct {
|
||
Name string `json:"name,omitempty"`
|
||
Avatar string `json:"avatar,omitempty"`
|
||
DatasetIds []string `json:"dataset_ids,omitempty"`
|
||
Llm *Llm `json:"llm,omitempty"`
|
||
Prompt *Prompt `json:"prompt,omitempty"`
|
||
}
|
||
|
||
// ListChatsReq 列出聊天助手请求
|
||
type ListChatsReq 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"`
|
||
}
|
||
|
||
// ListChatsRes 列出聊天助手响应
|
||
// 注意:API 不返回 total 字段,仅返回 data 数组
|
||
type ListChatsRes struct {
|
||
Code int `json:"code"` // 状态码,0 表示成功
|
||
Data []*Chat `json:"data"` // 聊天助手列表
|
||
}
|
||
|
||
// DeleteChatsReq 删除聊天助手请求
|
||
type DeleteChatsReq struct {
|
||
Ids []string `json:"ids"`
|
||
}
|
||
|
||
// CreateChat 创建聊天助手
|
||
func (c *Client) CreateChat(ctx context.Context, req *CreateChatReq) (*Chat, error) {
|
||
var res struct {
|
||
Code int `json:"code"`
|
||
Data *Chat `json:"data"`
|
||
Msg string `json:"message"`
|
||
}
|
||
if err := c.request(ctx, "POST", "/api/v1/chats", req, &res); err != nil {
|
||
return nil, err
|
||
}
|
||
if res.Code != 0 {
|
||
return nil, gerror.Newf("create chat failed: %s", res.Msg)
|
||
}
|
||
return res.Data, nil
|
||
}
|
||
|
||
// ListChats 列出聊天助手
|
||
func (c *Client) ListChats(ctx context.Context, req *ListChatsReq) (*ListChatsRes, error) {
|
||
path := "/api/v1/chats"
|
||
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
|
||
}
|
||
|
||
query := buildQueryString(params)
|
||
if query != "" {
|
||
path += "?" + query
|
||
}
|
||
|
||
var res ListChatsRes
|
||
if err := c.request(ctx, "GET", path, nil, &res); err != nil {
|
||
return nil, err
|
||
}
|
||
if res.Code != 0 {
|
||
return nil, gerror.Newf("list chats failed: code=%d", res.Code)
|
||
}
|
||
return &res, nil
|
||
}
|
||
|
||
// DeleteChats 删除聊天助手
|
||
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
|
||
}
|
||
if !res.IsSuccess() {
|
||
return gerror.Newf("delete chats failed: %s", res.Message)
|
||
}
|
||
return
|
||
}
|
||
|
||
// UpdateChat 更新聊天助手
|
||
func (c *Client) UpdateChat(ctx context.Context, id string, req *UpdateChatReq) (err error) {
|
||
var res CommonResponse
|
||
path := "/api/v1/chats/" + id
|
||
if err = c.request(ctx, "PUT", path, req, &res); err != nil {
|
||
return
|
||
}
|
||
if !res.IsSuccess() {
|
||
return gerror.Newf("update chat failed: %s", res.Message)
|
||
}
|
||
return
|
||
}
|