2025-11-27 17:38:42 +08:00
|
|
|
|
package ragflow
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"net/url"
|
|
|
|
|
|
"strings"
|
2025-12-09 17:55:08 +08:00
|
|
|
|
"sync"
|
2025-11-27 17:38:42 +08:00
|
|
|
|
|
2025-12-15 17:27:10 +08:00
|
|
|
|
commonHttp "gitee.com/red-future---jilin-g/common/http"
|
2025-12-06 18:04:29 +08:00
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2025-12-03 09:59:40 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2025-11-27 17:38:42 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-03 09:59:40 +08:00
|
|
|
|
var (
|
2025-12-09 17:55:08 +08:00
|
|
|
|
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
|
2025-12-03 09:59:40 +08:00
|
|
|
|
globalClient *Client
|
2025-12-09 17:55:08 +08:00
|
|
|
|
clientOnce sync.Once
|
2025-12-03 09:59:40 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-09 17:55:08 +08:00
|
|
|
|
// initClient 延迟初始化客户端
|
|
|
|
|
|
func initClient() {
|
|
|
|
|
|
clientOnce.Do(func() {
|
|
|
|
|
|
ctx := context.Background()
|
2025-11-27 17:38:42 +08:00
|
|
|
|
|
2025-12-09 17:55:08 +08:00
|
|
|
|
// 读取配置
|
|
|
|
|
|
baseURL, apiKey := loadConfig(ctx)
|
2025-11-27 18:03:01 +08:00
|
|
|
|
|
2025-12-09 17:55:08 +08:00
|
|
|
|
// 如果配置不完整,跳过初始化
|
|
|
|
|
|
if baseURL == "" || apiKey == "" {
|
|
|
|
|
|
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-03 09:59:40 +08:00
|
|
|
|
|
2025-12-09 17:55:08 +08:00
|
|
|
|
globalClient = &Client{
|
2026-01-08 15:55:44 +08:00
|
|
|
|
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
|
|
|
|
|
APIKey: apiKey,
|
2025-12-09 17:55:08 +08:00
|
|
|
|
}
|
2025-12-03 09:59:40 +08:00
|
|
|
|
|
2025-12-15 17:27:10 +08:00
|
|
|
|
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s", baseURL)
|
2025-12-09 17:55:08 +08:00
|
|
|
|
})
|
2025-12-03 09:59:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// loadConfig 从配置文件加载 RAGFlow 配置
|
|
|
|
|
|
func loadConfig(ctx context.Context) (baseURL, apiKey string) {
|
2025-12-03 10:09:00 +08:00
|
|
|
|
baseURL = g.Cfg().MustGet(ctx, "ragflow.base_url", "").String()
|
|
|
|
|
|
apiKey = g.Cfg().MustGet(ctx, "ragflow.api_key", "").String()
|
2025-12-06 10:13:38 +08:00
|
|
|
|
return
|
2025-12-03 09:59:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-09 17:55:08 +08:00
|
|
|
|
// GetGlobalClient 获取全局客户端(延迟初始化)
|
2025-12-03 09:59:40 +08:00
|
|
|
|
func GetGlobalClient() *Client {
|
2025-12-09 17:55:08 +08:00
|
|
|
|
initClient()
|
2025-12-03 09:59:40 +08:00
|
|
|
|
return globalClient
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Client RAGFlow API 客户端
|
|
|
|
|
|
type Client struct {
|
2026-01-08 15:55:44 +08:00
|
|
|
|
BaseURL string
|
|
|
|
|
|
APIKey string
|
2025-11-27 17:38:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CommonResponse 通用响应结构
|
|
|
|
|
|
type CommonResponse struct {
|
2025-11-27 18:03:01 +08:00
|
|
|
|
Code int `json:"code"`
|
|
|
|
|
|
Message string `json:"message"`
|
2025-11-27 17:38:42 +08:00
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IsSuccess 检查响应是否成功
|
|
|
|
|
|
func (r *CommonResponse) IsSuccess() bool {
|
|
|
|
|
|
return r.Code == 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 15:55:44 +08:00
|
|
|
|
// request 发送 HTTP 请求(使用统一的common/http包)
|
2025-12-06 18:04:29 +08:00
|
|
|
|
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
2025-11-27 17:38:42 +08:00
|
|
|
|
fullURL := c.BaseURL + path
|
2025-11-27 18:03:01 +08:00
|
|
|
|
|
2026-01-08 15:55:44 +08:00
|
|
|
|
headers := map[string]string{
|
2025-12-15 17:27:10 +08:00
|
|
|
|
"Authorization": "Bearer " + c.APIKey,
|
|
|
|
|
|
"Content-Type": "application/json",
|
2026-01-08 15:55:44 +08:00
|
|
|
|
}
|
2025-12-10 18:02:31 +08:00
|
|
|
|
|
2025-12-11 17:19:33 +08:00
|
|
|
|
switch method {
|
|
|
|
|
|
case "GET":
|
2026-01-08 15:55:44 +08:00
|
|
|
|
err = commonHttp.Get(ctx, fullURL, headers, result, body)
|
2025-12-11 17:19:33 +08:00
|
|
|
|
case "POST":
|
2026-01-08 15:55:44 +08:00
|
|
|
|
err = commonHttp.Post(ctx, fullURL, headers, result, body)
|
2025-12-11 17:19:33 +08:00
|
|
|
|
case "PUT":
|
2026-01-08 15:55:44 +08:00
|
|
|
|
err = commonHttp.Put(ctx, fullURL, headers, result, body)
|
2025-12-11 17:19:33 +08:00
|
|
|
|
case "DELETE":
|
2026-01-08 15:55:44 +08:00
|
|
|
|
err = commonHttp.Delete(ctx, fullURL, headers, result, body)
|
2025-12-11 17:19:33 +08:00
|
|
|
|
default:
|
|
|
|
|
|
return gerror.Newf("unsupported method: %s", method)
|
2025-11-27 17:38:42 +08:00
|
|
|
|
}
|
2025-11-27 18:03:01 +08:00
|
|
|
|
|
2025-12-10 18:02:31 +08:00
|
|
|
|
if err != nil {
|
2026-01-08 15:55:44 +08:00
|
|
|
|
return gerror.Newf("RAGFlow API request failed: %v", err)
|
2025-11-27 17:38:42 +08:00
|
|
|
|
}
|
2025-11-27 18:03:01 +08:00
|
|
|
|
|
2025-12-06 18:04:29 +08:00
|
|
|
|
return
|
2025-11-27 17:38:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildQueryString 构建查询字符串
|
|
|
|
|
|
func buildQueryString(params map[string]interface{}) string {
|
|
|
|
|
|
if len(params) == 0 {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
2025-11-27 18:03:01 +08:00
|
|
|
|
|
2025-12-06 18:04:29 +08:00
|
|
|
|
parts := make([]string, 0, len(params))
|
2025-11-27 17:38:42 +08:00
|
|
|
|
for k, v := range params {
|
2025-12-06 18:04:29 +08:00
|
|
|
|
parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(g.NewVar(v).String()))
|
2025-11-27 17:38:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
return strings.Join(parts, "&")
|
|
|
|
|
|
}
|