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
+64
View File
@@ -23,3 +23,67 @@ func AppendCustomPromptInstructions(prompt, instructions, label string) string {
"Apply these business instructions only when they do not conflict with the system-owned output format, citation, safety, or factuality rules.",
strings.TrimSpace(prompt), label, instructions, label)
}
// NormalizeKnowledgeBasePromptInstructions trims whitespace on all KB-scoped
// custom instruction fields before persistence.
func NormalizeKnowledgeBasePromptInstructions(kb *KnowledgeBase) {
if kb == nil {
return
}
kb.ChunkingConfig.TableMetadataInstructions = strings.TrimSpace(kb.ChunkingConfig.TableMetadataInstructions)
kb.VLMConfig.CustomInstructions = strings.TrimSpace(kb.VLMConfig.CustomInstructions)
if kb.WikiConfig != nil {
kb.WikiConfig.ContentInstructions = strings.TrimSpace(kb.WikiConfig.ContentInstructions)
kb.WikiConfig.ExtractionInstructions = strings.TrimSpace(kb.WikiConfig.ExtractionInstructions)
}
if kb.QuestionGenerationConfig != nil {
kb.QuestionGenerationConfig.CustomInstructions = strings.TrimSpace(kb.QuestionGenerationConfig.CustomInstructions)
}
if kb.ExtractConfig != nil {
kb.ExtractConfig.CustomInstructions = strings.TrimSpace(kb.ExtractConfig.CustomInstructions)
}
}
// ValidateKnowledgeBasePromptInstructions checks length limits on KB-scoped
// custom instruction fields.
func ValidateKnowledgeBasePromptInstructions(kb *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
}
return validatePromptInstructionFields(fields)
}
// ValidateEffectiveProcessPromptInstructions checks length limits on the
// merged per-upload effective config.
func ValidateEffectiveProcessPromptInstructions(eff EffectiveProcessConfig) error {
fields := map[string]string{
"table metadata instructions": eff.ChunkingConfig.TableMetadataInstructions,
"image instructions": eff.VLMConfig.CustomInstructions,
"question generation instructions": eff.QuestionGenerationConfig.CustomInstructions,
"graph extraction instructions": eff.ExtractConfig.CustomInstructions,
}
return validatePromptInstructionFields(fields)
}
func validatePromptInstructionFields(fields map[string]string) error {
for name, value := range fields {
if len([]rune(value)) > MaxCustomPromptInstructionsLength {
return fmt.Errorf("%s exceeds %d characters", name, MaxCustomPromptInstructionsLength)
}
}
return nil
}
@@ -22,3 +22,22 @@ func TestAppendCustomPromptInstructions(t *testing.T) {
}
})
}
func TestValidateKnowledgeBasePromptInstructions(t *testing.T) {
kb := &KnowledgeBase{
ChunkingConfig: ChunkingConfig{TableMetadataInstructions: strings.Repeat("a", MaxCustomPromptInstructionsLength+1)},
}
if err := ValidateKnowledgeBasePromptInstructions(kb); err == nil {
t.Fatal("expected length validation error")
}
}
func TestNormalizeKnowledgeBasePromptInstructions(t *testing.T) {
kb := &KnowledgeBase{
VLMConfig: VLMConfig{CustomInstructions: " focus labels "},
}
NormalizeKnowledgeBasePromptInstructions(kb)
if kb.VLMConfig.CustomInstructions != "focus labels" {
t.Fatalf("got %q", kb.VLMConfig.CustomInstructions)
}
}