Add instruction fields to knowledge base configuration

This commit is contained in:
wizardchen
2026-07-13 11:57:30 +08:00
committed by lyingbug
parent def92bb74f
commit c1cddb4d4d
28 changed files with 634 additions and 81 deletions
+37 -18
View File
@@ -111,9 +111,10 @@ type KBModelConfigRequest struct {
// from "field present with empty/zero value" (clear / disable).
// Without that distinction, users could set strategy="auto" once
// but never reset it back to legacy / unset.
Strategy *string `json:"strategy,omitempty"`
TokenLimit *int `json:"tokenLimit,omitempty"`
Languages *[]string `json:"languages,omitempty"`
Strategy *string `json:"strategy,omitempty"`
TokenLimit *int `json:"tokenLimit,omitempty"`
Languages *[]string `json:"languages,omitempty"`
TableMetadataInstructions *string `json:"tableMetadataInstructions,omitempty"`
} `json:"documentSplitting"`
// 多模态配置(仅模型相关;存储引擎在 storageProvider 中配置)
@@ -126,17 +127,19 @@ type KBModelConfigRequest struct {
// 知识图谱配置
NodeExtract struct {
Enabled bool `json:"enabled"`
Text string `json:"text"`
Tags []string `json:"tags"`
Nodes []types.GraphNode `json:"nodes"`
Relations []types.GraphRelation `json:"relations"`
Enabled bool `json:"enabled"`
Text string `json:"text"`
Tags []string `json:"tags"`
Nodes []types.GraphNode `json:"nodes"`
Relations []types.GraphRelation `json:"relations"`
CustomInstructions string `json:"customInstructions"`
} `json:"nodeExtract"`
// 问题生成配置
QuestionGeneration struct {
Enabled bool `json:"enabled"`
QuestionCount int `json:"questionCount"`
Enabled bool `json:"enabled"`
QuestionCount int `json:"questionCount"`
CustomInstructions string `json:"customInstructions"`
} `json:"questionGeneration"`
}
@@ -344,6 +347,9 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
if req.DocumentSplitting.Languages != nil {
kb.ChunkingConfig.Languages = *req.DocumentSplitting.Languages
}
if req.DocumentSplitting.TableMetadataInstructions != nil {
kb.ChunkingConfig.TableMetadataInstructions = strings.TrimSpace(*req.DocumentSplitting.TableMetadataInstructions)
}
// 更新多模态配置
if req.Multimodal.Enabled {
@@ -351,6 +357,10 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
} else {
kb.VLMConfig.ModelID = ""
}
if req.VLMConfig != nil {
kb.VLMConfig.DescriptionLanguage = strings.TrimSpace(req.VLMConfig.DescriptionLanguage)
kb.VLMConfig.CustomInstructions = strings.TrimSpace(req.VLMConfig.CustomInstructions)
}
// 存储引擎:仅写入 provider 到新字段,参数从租户全局 StorageEngineConfig 读取
provider := strings.ToLower(strings.TrimSpace(req.StorageProvider))
@@ -387,11 +397,12 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
}
kb.ExtractConfig = &types.ExtractConfig{
Enabled: req.NodeExtract.Enabled,
Text: req.NodeExtract.Text,
Tags: req.NodeExtract.Tags,
Nodes: nodes,
Relations: relations,
Enabled: req.NodeExtract.Enabled,
Text: req.NodeExtract.Text,
Tags: req.NodeExtract.Tags,
Nodes: nodes,
Relations: relations,
CustomInstructions: strings.TrimSpace(req.NodeExtract.CustomInstructions),
}
} else {
kb.ExtractConfig = &types.ExtractConfig{Enabled: false}
@@ -412,11 +423,19 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
questionCount = 10
}
kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{
Enabled: true,
QuestionCount: questionCount,
Enabled: true,
QuestionCount: questionCount,
CustomInstructions: strings.TrimSpace(req.QuestionGeneration.CustomInstructions),
}
} else {
kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{Enabled: false}
kb.QuestionGenerationConfig = &types.QuestionGenerationConfig{
Enabled: false,
CustomInstructions: strings.TrimSpace(req.QuestionGeneration.CustomInstructions),
}
}
if err := validateKnowledgeBasePromptInstructions(kb); err != nil {
c.Error(err)
return
}
// 保存更新后的知识库
+42
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
stderrors "errors"
"fmt"
"net/http"
"strconv"
"strings"
@@ -361,6 +362,10 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
c.Error(err)
return
}
if err := validateKnowledgeBasePromptInstructions(&req); err != nil {
c.Error(err)
return
}
provider := strings.ToLower(strings.TrimSpace(req.GetStorageProvider()))
if provider != "" && !isStorageProviderAllowed(provider) {
c.Error(apperrors.NewBadRequestError("Storage provider is not allowed by STORAGE_ALLOW_LIST"))
@@ -853,6 +858,17 @@ func (h *KnowledgeBaseHandler) UpdateKnowledgeBase(c *gin.Context) {
c.Error(apperrors.NewBadRequestError("Invalid request parameters").WithDetails(err.Error()))
return
}
if req.Config != nil {
probe := &types.KnowledgeBase{
ChunkingConfig: req.Config.ChunkingConfig,
ImageProcessingConfig: req.Config.ImageProcessingConfig,
WikiConfig: req.Config.WikiConfig,
}
if err := validateKnowledgeBasePromptInstructions(probe); err != nil {
c.Error(err)
return
}
}
logger.Infof(ctx, "Updating knowledge base, ID: %s, name: %s",
secutils.SanitizeForLog(id), secutils.SanitizeForLog(req.Name))
@@ -1293,6 +1309,32 @@ func validateExtractConfig(config *types.ExtractConfig) error {
return nil
}
func validateKnowledgeBasePromptInstructions(kb *types.KnowledgeBase) error {
if kb == nil {
return nil
}
fields := map[string]string{
"table metadata instructions": kb.ChunkingConfig.TableMetadataInstructions,
"image instructions": kb.VLMConfig.CustomInstructions,
}
if kb.WikiConfig != nil {
fields["wiki content instructions"] = kb.WikiConfig.ContentInstructions
fields["wiki extraction instructions"] = kb.WikiConfig.ExtractionInstructions
}
if kb.QuestionGenerationConfig != nil {
fields["question generation instructions"] = kb.QuestionGenerationConfig.CustomInstructions
}
if kb.ExtractConfig != nil {
fields["graph extraction instructions"] = kb.ExtractConfig.CustomInstructions
}
for name, value := range fields {
if len([]rune(value)) > types.MaxCustomPromptInstructionsLength {
return apperrors.NewBadRequestError(fmt.Sprintf("%s exceeds %d characters", name, types.MaxCustomPromptInstructionsLength))
}
}
return nil
}
// ListMoveTargets returns knowledge bases eligible as move targets for the given source KB.
// Filters: same Type, same EmbeddingModelID, different ID, not temporary.
//