不同服务注册不同组件模块
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
@@ -14,36 +15,39 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
// globalClient 全局 RAGFlow 客户端(单例,自动初始化)
|
||||
// globalClient 全局 RAGFlow 客户端(单例,延迟初始化)
|
||||
globalClient *Client
|
||||
clientOnce sync.Once
|
||||
)
|
||||
|
||||
// init 包初始化时自动创建全局客户端
|
||||
func init() {
|
||||
ctx := context.Background()
|
||||
// initClient 延迟初始化客户端
|
||||
func initClient() {
|
||||
clientOnce.Do(func() {
|
||||
ctx := context.Background()
|
||||
|
||||
// 读取配置
|
||||
baseURL, apiKey := loadConfig(ctx)
|
||||
// 读取配置
|
||||
baseURL, apiKey := loadConfig(ctx)
|
||||
|
||||
// 如果配置不完整,跳过初始化
|
||||
if baseURL == "" || apiKey == "" {
|
||||
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
|
||||
return
|
||||
}
|
||||
// 如果配置不完整,跳过初始化
|
||||
if baseURL == "" || apiKey == "" {
|
||||
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在项目 config.yml 中添加 ragflow.base_url 和 ragflow.api_key")
|
||||
return
|
||||
}
|
||||
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
httpClient.SetTimeout(60 * time.Second) // RAGFlow AI 推理需要较长时间
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
httpClient.SetTimeout(180 * time.Second) // RAGFlow AI 推理需要较长时间
|
||||
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: httpClient,
|
||||
}
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: httpClient,
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
|
||||
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
|
||||
})
|
||||
}
|
||||
|
||||
// loadConfig 从配置文件加载 RAGFlow 配置
|
||||
@@ -54,9 +58,10 @@ func loadConfig(ctx context.Context) (baseURL, apiKey string) {
|
||||
return
|
||||
}
|
||||
|
||||
// GetGlobalClient 获取全局客户端
|
||||
// GetGlobalClient 获取全局客户端(延迟初始化)
|
||||
// 使用示例:client := ragflow.GetGlobalClient()
|
||||
func GetGlobalClient() *Client {
|
||||
initClient()
|
||||
return globalClient
|
||||
}
|
||||
|
||||
|
||||
@@ -65,8 +65,9 @@ type ChatCompletionReq struct {
|
||||
|
||||
// ChatCompletionRes 对话响应 (非流式)
|
||||
type ChatCompletionRes struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"` // 错误信息
|
||||
Data struct {
|
||||
Answer string `json:"answer"`
|
||||
Reference interface{} `json:"reference"`
|
||||
AudioBinary interface{} `json:"audio_binary"`
|
||||
@@ -163,7 +164,7 @@ func (c *Client) ChatCompletion(ctx context.Context, chatId string, req *ChatCom
|
||||
return nil, err
|
||||
}
|
||||
if res.Code != 0 {
|
||||
return nil, gerror.Newf("chat completion failed: code=%d", res.Code)
|
||||
return nil, gerror.Newf("chat completion failed: code=%d, message=%s", res.Code, res.Message)
|
||||
}
|
||||
return &res, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user