RAGflow的init函数修改和mq
This commit is contained in:
@@ -9,27 +9,79 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
)
|
||||
|
||||
var (
|
||||
// globalClient 全局 RAGFlow 客户端(单例,自动初始化)
|
||||
globalClient *Client
|
||||
)
|
||||
|
||||
// init 包初始化时自动创建全局客户端
|
||||
func init() {
|
||||
ctx := context.Background()
|
||||
|
||||
// 读取配置
|
||||
baseURL, apiKey := loadConfig(ctx)
|
||||
|
||||
// 如果配置不完整,跳过初始化
|
||||
if baseURL == "" || apiKey == "" {
|
||||
g.Log().Warning(ctx, "⚠️ RAGFlow 配置未找到,请在 common/ragflow/config.yaml 中配置")
|
||||
return
|
||||
}
|
||||
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
|
||||
globalClient = &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: httpClient,
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "✅ RAGFlow 全局客户端初始化成功: baseURL=%s", baseURL)
|
||||
}
|
||||
|
||||
// loadConfig 从配置文件加载 RAGFlow 配置
|
||||
func loadConfig(ctx context.Context) (baseURL, apiKey string) {
|
||||
// 创建配置实例
|
||||
cfg, err := gcfg.New()
|
||||
if err != nil {
|
||||
g.Log().Debugf(ctx, "创建配置实例失败: %v", err)
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// 设置配置文件
|
||||
adapter, ok := cfg.GetAdapter().(*gcfg.AdapterFile)
|
||||
if !ok {
|
||||
g.Log().Debug(ctx, "配置适配器类型不匹配")
|
||||
return "", ""
|
||||
}
|
||||
|
||||
adapter.SetFileName("config.yaml")
|
||||
|
||||
// 读取配置项
|
||||
baseURL = cfg.MustGet(ctx, "ragflow.base_url").String()
|
||||
apiKey = cfg.MustGet(ctx, "ragflow.api_key").String()
|
||||
|
||||
return baseURL, apiKey
|
||||
}
|
||||
|
||||
// GetGlobalClient 获取全局客户端
|
||||
// 使用示例:client := ragflow.GetGlobalClient()
|
||||
func GetGlobalClient() *Client {
|
||||
return globalClient
|
||||
}
|
||||
|
||||
// Client RAGFlow API 客户端
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
HTTPClient *gclient.Client
|
||||
}
|
||||
|
||||
// NewClient 创建新的 RAGFlow 客户端
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
client := gclient.New()
|
||||
client.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
client.SetHeader("Content-Type", "application/json")
|
||||
|
||||
return &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: client,
|
||||
}
|
||||
HTTPClient *gclient.Client // HTTP 客户端
|
||||
}
|
||||
|
||||
// CommonResponse 通用响应结构
|
||||
|
||||
Reference in New Issue
Block a user