Enhance knowledge base configuration by normalizing and validating custom instruction fields. Implemented length checks for prompt instructions and added tests for validation logic. Updated GraphSettings to retain custom instructions when extraction is disabled.

This commit is contained in:
wizardchen
2026-07-13 12:14:36 +08:00
committed by lyingbug
parent c1cddb4d4d
commit 5eefa70e6f
9 changed files with 158 additions and 35 deletions
+5 -1
View File
@@ -626,7 +626,11 @@ func (s *DataTableSummaryService) processTableData(ctx context.Context, resource
// 使用AI生成表格摘要和列描述
customInstructions := ""
if resources.knowledgeBase != nil {
customInstructions = resources.knowledgeBase.ChunkingConfig.TableMetadataInstructions
var processOverrides *types.KnowledgeProcessOverrides
if resources.knowledge != nil {
processOverrides, _ = resources.knowledge.ProcessOverrides()
}
customInstructions = ResolveProcessConfig(resources.knowledgeBase, processOverrides).ChunkingConfig.TableMetadataInstructions
}
tableDescription, err := s.generateTableDescription(ctx, resources.chatModel, tableSchema.TableName,
schemaDesc, sampleDesc, customInstructions)
@@ -1457,6 +1457,10 @@ func (s *knowledgeService) processQuestionGenerationForKnowledge(ctx context.Con
questionCount = 10
}
processOverrides, _ := knowledge.ProcessOverrides()
questionGenCfg := ResolveProcessConfig(kb, processOverrides).QuestionGenerationConfig
customInstructions := questionGenCfg.CustomInstructions
// Collect image info for all text chunks so question generation can
// see caption / OCR text instead of bare image links.
textChunkIDs := make([]string, len(textChunks))
@@ -1490,10 +1494,6 @@ func (s *knowledgeService) processQuestionGenerationForKnowledge(ctx context.Con
}
llmCallAttempts++
customInstructions := ""
if kb.QuestionGenerationConfig != nil {
customInstructions = kb.QuestionGenerationConfig.CustomInstructions
}
questions, err := s.generateQuestionsWithContext(ctx, chatModel, enrichContent(chunk), prevContent, nextContent,
knowledge.Title, questionCount, customInstructions)
if err != nil {
@@ -1758,6 +1758,10 @@ func (s *knowledgeService) processQuestionGenerationForChunks(ctx context.Contex
questionCount = 10
}
processOverrides, _ := knowledge.ProcessOverrides()
questionGenCfg := ResolveProcessConfig(kb, processOverrides).QuestionGenerationConfig
customInstructions := questionGenCfg.CustomInstructions
// Fetch the batch chunks (in payload order) plus the two boundary
// neighbors so we can rebuild the same surrounding context the legacy
// loop used, all enriched with image OCR / caption info. A vanished
@@ -1820,10 +1824,6 @@ func (s *knowledgeService) processQuestionGenerationForChunks(ctx context.Contex
continue
}
customInstructions := ""
if kb.QuestionGenerationConfig != nil {
customInstructions = kb.QuestionGenerationConfig.CustomInstructions
}
questions, gerr := s.generateQuestionsWithContext(
ctx, chatModel, enrich(chunk), prevContentAt(i), nextContentAt(i), knowledge.Title, questionCount,
customInstructions)
@@ -103,6 +103,10 @@ func ValidateProcessOverrides(
return werrors.NewBadRequestError("上传音频文件需要设置ASR语音识别模型")
}
if err := types.ValidateEffectiveProcessPromptInstructions(eff); err != nil {
return werrors.NewBadRequestError(err.Error())
}
return nil
}
@@ -2,6 +2,7 @@ package service
import (
"context"
"strings"
"testing"
werrors "github.com/Tencent/WeKnora/internal/errors"
@@ -227,6 +228,21 @@ func TestResolveProcessConfig_PreservesKnowledgeBasePromptInstructions(t *testin
require.Equal(t, "contract entities", eff.ExtractConfig.CustomInstructions)
}
func TestValidateProcessOverrides_RejectsOversizedInstructions(t *testing.T) {
t.Parallel()
kb := &types.KnowledgeBase{}
overrides := &types.KnowledgeProcessOverrides{
VLMConfig: &types.VLMConfig{
CustomInstructions: strings.Repeat("x", types.MaxCustomPromptInstructionsLength+1),
},
}
err := ValidateProcessOverrides(context.Background(), kb, overrides, []string{"txt"})
require.Error(t, err)
var badReq *werrors.AppError
require.ErrorAs(t, err, &badReq)
}
func TestValidateProcessOverrides_NilOverrides(t *testing.T) {
t.Parallel()