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:
@@ -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{}{
|
||||
|
||||
Reference in New Issue
Block a user