From 313860bea4b621fb0964463b37441c836d186905 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 3 Dec 2025 11:56:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BAVLM=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=EF=BC=8C=E6=94=AF=E6=8C=81=E6=96=B0?= =?UTF-8?q?=E6=97=A7=E7=89=88=E6=9C=AC=E9=85=8D=E7=BD=AE=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/application/service/knowledge.go | 22 ++++++++++++++++---- internal/handler/initialization.go | 2 +- internal/types/knowledgebase.go | 25 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 5a54540b..e52d8c49 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -279,7 +279,7 @@ func (s *knowledgeService) CreateKnowledgeFromFile(ctx context.Context, if enableMultimodel != nil { enableMultimodelValue = *enableMultimodel } else { - enableMultimodelValue = kb.VLMConfig.Enabled + enableMultimodelValue = kb.VLMConfig.IsEnabled() } // Check question generation config @@ -413,7 +413,7 @@ func (s *knowledgeService) CreateKnowledgeFromURL(ctx context.Context, if enableMultimodel != nil { enableMultimodelValue = *enableMultimodel } else { - enableMultimodelValue = kb.VLMConfig.Enabled + enableMultimodelValue = kb.VLMConfig.IsEnabled() } // Check question generation config @@ -4155,7 +4155,7 @@ func (s *knowledgeService) triggerManualProcessing(ctx context.Context, fileType := "md" // 检查是否需要启用多模态(对于手动内容通常不需要,但保持一致性) - enableMultimodel := kb.VLMConfig.Enabled && kb.StorageConfig.Provider != "" + enableMultimodel := kb.VLMConfig.IsEnabled() && kb.StorageConfig.Provider != "" var vlmConfig *proto.VLMConfig if enableMultimodel { @@ -4285,7 +4285,21 @@ func (s *knowledgeService) cleanupKnowledgeResources(ctx context.Context, knowle } func (s *knowledgeService) getVLMProtoConfig(ctx context.Context, kb *types.KnowledgeBase) (*proto.VLMConfig, error) { - if kb == nil || !kb.VLMConfig.Enabled || kb.VLMConfig.ModelID == "" { + if kb == nil { + return nil, nil + } + // 兼容老版本:直接使用 ModelName 和 BaseURL + if kb.VLMConfig.ModelName != "" && kb.VLMConfig.BaseURL != "" { + return &proto.VLMConfig{ + ModelName: kb.VLMConfig.ModelName, + BaseUrl: kb.VLMConfig.BaseURL, + ApiKey: kb.VLMConfig.APIKey, + InterfaceType: kb.VLMConfig.InterfaceType, + }, nil + } + + // 新版本:未启用或无模型ID时返回nil + if !kb.VLMConfig.Enabled || kb.VLMConfig.ModelID == "" { return nil, nil } diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index 6bdb4189..43129d72 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -1251,7 +1251,7 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models } // 判断多模态是否启用:有VLM模型ID或有存储配置 - hasMultimodal := ((kb.VLMConfig.Enabled && kb.VLMConfig.ModelID != "") || + hasMultimodal := (kb.VLMConfig.IsEnabled() || kb.StorageConfig.SecretID != "" || kb.StorageConfig.BucketName != "") if config["multimodal"] == nil { config["multimodal"] = map[string]interface{}{ diff --git a/internal/types/knowledgebase.go b/internal/types/knowledgebase.go index 512c71fa..02cd9043 100644 --- a/internal/types/knowledgebase.go +++ b/internal/types/knowledgebase.go @@ -180,6 +180,31 @@ func (c *ImageProcessingConfig) Scan(value interface{}) error { type VLMConfig struct { Enabled bool `yaml:"enabled" json:"enabled"` ModelID string `yaml:"model_id" json:"model_id"` + + // 兼容老版本 + // Model Name + ModelName string `yaml:"model_name" json:"model_name"` + // Base URL + BaseURL string `yaml:"base_url" json:"base_url"` + // API Key + APIKey string `yaml:"api_key" json:"api_key"` + // Interface Type: "ollama" or "openai" + InterfaceType string `yaml:"interface_type" json:"interface_type"` +} + +// IsEnabled 判断多模态是否启用(兼容新老版本) +// 新版本:Enabled && ModelID != "" +// 老版本:ModelName != "" && BaseURL != "" +func (c VLMConfig) IsEnabled() bool { + // 新版本配置 + if c.Enabled && c.ModelID != "" { + return true + } + // 兼容老版本配置 + if c.ModelName != "" && c.BaseURL != "" { + return true + } + return false } // QuestionGenerationConfig represents the question generation configuration for document knowledge bases