feat(model_service): enhance WeKnoraCloud credential management

- Added tenantService to modelService for retrieving WeKnoraCloud credentials when not provided in model parameters.
- Implemented resolveWeKnoraCloudCredentials method to automatically fetch AppID and AppSecret from tenant configuration.
- Updated GetEmbeddingModel, GetEmbeddingModelForTenant, GetRerankModel, and GetChatModel methods to utilize the new credential resolution logic.
- Modified ModelParameters to encrypt and decrypt both APIKey and AppSecret for improved security during database operations.

This update streamlines credential management for WeKnoraCloud models, enhancing security and usability.
This commit is contained in:
wizardchen
2026-04-14 23:52:28 +08:00
committed by lyingbug
parent 457e739978
commit 3b23dccd5c
2 changed files with 71 additions and 16 deletions
+49 -8
View File
@@ -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.ModelRepository
ollamaService *ollama.OllamaService
pooler embedding.EmbedderPooler
tenantService interfaces.TenantService
}
// NewModelService creates a new model service instance
func 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) (e
logger.Infof(ctx, "Getting embedding model: %s, source: %s", model.Name, model.Source)
appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)
// Initialize the embedder with model configuration
embedder, err := embedding.NewEmbedder(embedding.Config{
Source: model.Source,
@@ -274,8 +309,8 @@ func (s *modelService) GetEmbeddingModel(ctx context.Context, modelId string) (e
TruncatePromptTokens: 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 s
logger.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 configuration
embedder, err := embedding.NewEmbedder(embedding.Config{
Source: model.Source,
@@ -332,8 +369,8 @@ func (s *modelService) GetEmbeddingModelForTenant(ctx context.Context, modelId s
TruncatePromptTokens: 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) (rera
logger.Infof(ctx, "Getting rerank model: %s, source: %s", model.Name, model.Source)
appID, appSecret := s.resolveWeKnoraCloudCredentials(ctx, &model.Parameters)
// Initialize the reranker with model configuration
reranker, err := rerank.NewReranker(&rerank.RerankerConfig{
ModelID: model.ID,
@@ -371,8 +410,8 @@ func (s *modelService) GetRerankModel(ctx context.Context, modelId string) (rera
Source: 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.C
logger.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 configuration
chatModel, err := chat.NewChat(&chat.ChatConfig{
ModelID: model.ID,
@@ -423,8 +464,8 @@ func (s *modelService) GetChatModel(ctx context.Context, modelId string) (chat.C
Source: 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{}{
+22 -8
View File
@@ -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