Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat(model_service): enhance WeKnoraCloud credential management
wizardchen3b23dccd5cd0
diff --git a/internal/application/service/model.go b/internal/application/service/model.goindex 65ec1756a..8df937020 100644--- a/internal/application/service/model.go+++ b/internal/application/service/model.go@@ -8,6 +8,7 @@ import ("github.com/Tencent/WeKnora/internal/models/asr""github.com/Tencent/WeKnora/internal/models/chat""github.com/Tencent/WeKnora/internal/models/embedding"+ "github.com/Tencent/WeKnora/internal/models/provider""github.com/Tencent/WeKnora/internal/models/rerank""github.com/Tencent/WeKnora/internal/models/utils/ollama""github.com/Tencent/WeKnora/internal/models/vlm"@@ -24,17 +25,20 @@ type modelService struct {repo interfaces.ModelRepositoryollamaService *ollama.OllamaServicepooler embedding.EmbedderPooler+ tenantService interfaces.TenantService}// NewModelService creates a new model service instancefunc NewModelService(repo interfaces.ModelRepository,ollamaService *ollama.OllamaService,pooler embedding.EmbedderPooler,+ tenantService interfaces.TenantService,) interfaces.ModelService {return &modelService{repo: repo,ollamaService: ollamaService,pooler: pooler,+ tenantService: tenantService,}}@@ -51,6 +55,35 @@ func (s *modelService) decryptAppSecret(encrypted string) string {return encrypted}+// resolveWeKnoraCloudCredentials 为 WeKnoraCloud 厂商模型补全 AppID/AppSecret。+// 当模型自身参数中未存储凭证时,自动从租户配置中获取(SaveCredentials 保存的凭证)。+func (s *modelService) resolveWeKnoraCloudCredentials(ctx context.Context, params *types.ModelParameters) (appID, appSecret string) {+ appID = params.AppID+ appSecret = s.decryptAppSecret(params.AppSecret)++ if provider.ProviderName(params.Provider) != provider.ProviderWeKnoraCloud {+ return+ }+ if appID != "" && appSecret != "" {+ return+ }++ if s.tenantService == nil {+ return+ }+ creds := s.tenantService.GetDocreaderCredentials(ctx)+ if creds == nil {+ return+ }+ if appID == "" {+ appID = creds.AppID+ }+ if appSecret == "" {+ appSecret = creds.APIKey+ }+ return+}+// CreateModel creates a new model in the repository// For local models, it initiates an asynchronous download process// Remote models are immediately set to active status@@ -263,6 +296,8 @@ func (s *modelService) GetEmbeddingModel(ctx context.Context, modelId string) (elogger.Infof(ctx, "Getting embedding model: %s, source: %s", model.Name, model.Source)+ appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)+// Initialize the embedder with model configurationembedder, err := embedding.NewEmbedder(embedding.Config{Source: model.Source,@@ -274,8 +309,8 @@ func (s *modelService) GetEmbeddingModel(ctx context.Context, modelId string) (eTruncatePromptTokens: model.Parameters.EmbeddingParameters.TruncatePromptTokens,Provider: model.Parameters.Provider,ExtraConfig: model.Parameters.ExtraConfig,- AppID: model.Parameters.AppID,- AppSecret: s.decryptAppSecret(model.Parameters.AppSecret),+ AppID: appID,+ AppSecret: appSecret,}, s.pooler, s.ollamaService)if err != nil {logger.ErrorWithFields(ctx, err, map[string]interface{}{@@ -321,6 +356,8 @@ func (s *modelService) GetEmbeddingModelForTenant(ctx context.Context, modelId slogger.Infof(ctx, "Getting cross-tenant embedding model: %s, source: %s, tenant: %d", model.Name, model.Source, tenantID)+ appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)+// Initialize the embedder with model configurationembedder, err := embedding.NewEmbedder(embedding.Config{Source: model.Source,@@ -332,8 +369,8 @@ func (s *modelService) GetEmbeddingModelForTenant(ctx context.Context, modelId sTruncatePromptTokens: model.Parameters.EmbeddingParameters.TruncatePromptTokens,Provider: model.Parameters.Provider,ExtraConfig: model.Parameters.ExtraConfig,- AppID: model.Parameters.AppID,- AppSecret: s.decryptAppSecret(model.Parameters.AppSecret),+ AppID: appID,+ AppSecret: appSecret,}, s.pooler, s.ollamaService)if err != nil {logger.ErrorWithFields(ctx, err, map[string]interface{}{@@ -362,6 +399,8 @@ func (s *modelService) GetRerankModel(ctx context.Context, modelId string) (reralogger.Infof(ctx, "Getting rerank model: %s, source: %s", model.Name, model.Source)+ appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)+// Initialize the reranker with model configurationreranker, err := rerank.NewReranker(&rerank.RerankerConfig{ModelID: model.ID,@@ -371,8 +410,8 @@ func (s *modelService) GetRerankModel(ctx context.Context, modelId string) (reraSource: model.Source,Provider: model.Parameters.Provider,ExtraConfig: model.Parameters.ExtraConfig,- AppID: model.Parameters.AppID,- AppSecret: s.decryptAppSecret(model.Parameters.AppSecret),+ AppID: appID,+ AppSecret: appSecret,})if err != nil {logger.ErrorWithFields(ctx, err, map[string]interface{}{@@ -414,6 +453,8 @@ func (s *modelService) GetChatModel(ctx context.Context, modelId string) (chat.Clogger.Infof(ctx, "Getting chat model: %s, source: %s", model.Name, model.Source)+ appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)+// Initialize the chat model with model configurationchatModel, err := chat.NewChat(&chat.ChatConfig{ModelID: model.ID,@@ -423,8 +464,8 @@ func (s *modelService) GetChatModel(ctx context.Context, modelId string) (chat.CSource: model.Source,Provider: model.Parameters.Provider,ExtraConfig: model.Parameters.ExtraConfig,- AppID: model.Parameters.AppID,- AppSecret: s.decryptAppSecret(model.Parameters.AppSecret),+ AppID: appID,+ AppSecret: appSecret,}, s.ollamaService)if err != nil {logger.ErrorWithFields(ctx, err, map[string]interface{}{diff --git a/internal/types/model.go b/internal/types/model.goindex 6f1ffa4ff..b9ef81224 100644--- a/internal/types/model.go+++ b/internal/types/model.go@@ -104,18 +104,25 @@ type Model struct {}// Value implements the driver.Valuer interface, used to convert ModelParameters to database value.-// Encrypts APIKey before persisting to database (value receiver = no memory pollution).+// Encrypts APIKey and AppSecret before persisting to database (value receiver = no memory pollution).func (c ModelParameters) Value() (driver.Value, error) {- if key := utils.GetAESKey(); key != nil && c.APIKey != "" {- if encrypted, err := utils.EncryptAESGCM(c.APIKey, key); err == nil {- c.APIKey = encrypted+ if key := utils.GetAESKey(); key != nil {+ if c.APIKey != "" {+ if encrypted, err := utils.EncryptAESGCM(c.APIKey, key); err == nil {+ c.APIKey = encrypted+ }+ }+ if c.AppSecret != "" {+ if encrypted, err := utils.EncryptAESGCM(c.AppSecret, key); err == nil {+ c.AppSecret = encrypted+ }}}return json.Marshal(c)}// Scan implements the sql.Scanner interface, used to convert database value to ModelParameters.-// Decrypts APIKey after loading from database; legacy plaintext is returned as-is.+// Decrypts APIKey and AppSecret after loading from database; legacy plaintext is returned as-is.func (c *ModelParameters) Scan(value interface{}) error {if value == nil {return nil@@ -127,9 +134,16 @@ func (c *ModelParameters) Scan(value interface{}) error {if err := json.Unmarshal(b, c); err != nil {return err}- if key := utils.GetAESKey(); key != nil && c.APIKey != "" {- if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {- c.APIKey = decrypted+ if key := utils.GetAESKey(); key != nil {+ if c.APIKey != "" {+ if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {+ c.APIKey = decrypted+ }+ }+ if c.AppSecret != "" {+ if decrypted, err := utils.DecryptAESGCM(c.AppSecret, key); err == nil {+ c.AppSecret = decrypted+ }}}return nil