fix(image): stop appending KB custom instructions to the OCR prompt OCR提示词不再错误的附加图片解析用户自定义提示词 (#3291)
* fix(image): stop appending KB custom instructions to the OCR prompt CustomInstructions is documented as adding image interpretation guidance "without replacing the system-owned OCR and Markdown output contract" (types.VLMConfig.CustomInstructions), and the UI states the same. But image_multimodal.go appended the same custom-instruction string to both the caption prompt and the OCR prompt, so free-form business rules (e.g. output-marker rules intended for descriptions) competed with the OCR prompt's own contract that a text-free image must reply exactly "No text content". On a production knowledge base this poisoned 116/255 (45%) of image_ocr chunks: the 8B VLM leaned on the nearest output-format rule and emitted the KB's fixed marker phrase instead of "No text content", which passed the sanitizer (it only recognizes known empty replies), created enabled image_ocr child chunks with vectors, and made real photos retrievable as decorative-image hits. Section names from the custom instructions appeared verbatim in OCR output, confirming prompt cross-contamination. Caption prompts keep their custom instructions; only the OCR append is removed. * test(image): pin the OCR prompt contract against custom instructions Extract buildVLMOCRPrompt so the OCR prompt assembly is unit-testable and add a regression test asserting that KB custom instructions never reach the OCR prompt (while the caption prompt keeps them), and that the No text content contract stays intact for both source types. --------- Co-authored-by: tayadwx <tayadwx@users.noreply.github.com>main
parent
fca1cd06f8
commit
7ea5bf4349
|
|
@ -59,6 +59,20 @@ func buildVLMCaptionPrompt(ctx context.Context, cfg types.VLMConfig) string {
|
|||
return types.AppendCustomPromptInstructions(prompt, cfg.CustomInstructions, "image_description")
|
||||
}
|
||||
|
||||
// buildVLMOCRPrompt returns the system-owned OCR prompt for the given image
|
||||
// source type. cfg is part of the signature and deliberately unused: knowledge
|
||||
// base custom instructions must never reach the OCR prompt — free-form
|
||||
// business rules compete with the "No text content" output contract and turn
|
||||
// decorative images into poisoned, vectorized image_ocr chunks. Custom OCR
|
||||
// guidance, if ever needed, deserves a dedicated OCR instruction field rather
|
||||
// than reusing CustomInstructions.
|
||||
func buildVLMOCRPrompt(sourceType string, _ types.VLMConfig) string {
|
||||
if sourceType == "scanned_pdf" {
|
||||
return vlmOCRScannedPDFPrompt
|
||||
}
|
||||
return vlmOCRPrompt
|
||||
}
|
||||
|
||||
// ImageMultimodalService handles image:multimodal asynq tasks.
|
||||
// It reads images from storage (via FileService for provider:// URLs),
|
||||
// performs OCR and VLM caption, and creates child chunks.
|
||||
|
|
@ -258,15 +272,13 @@ func (s *ImageMultimodalService) Handle(ctx context.Context, task *asynq.Task) e
|
|||
}
|
||||
|
||||
if payload.EnableOCR {
|
||||
prompt := vlmOCRPrompt
|
||||
prompt := buildVLMOCRPrompt(payload.ImageSourceType, vlmCfg)
|
||||
if payload.ImageSourceType == "scanned_pdf" {
|
||||
prompt = vlmOCRScannedPDFPrompt
|
||||
logger.Infof(ctx, "[ImageMultimodal] Using scanned PDF prompt for OCR: %s", payload.ImageURL)
|
||||
imgOut["ocr_prompt"] = "scanned_pdf"
|
||||
} else {
|
||||
imgOut["ocr_prompt"] = "default"
|
||||
}
|
||||
prompt = types.AppendCustomPromptInstructions(prompt, vlmCfg.CustomInstructions, "image_ocr")
|
||||
|
||||
ocrText, ocrErr := vlmModel.Predict(ctx, [][]byte{imgBytes}, prompt)
|
||||
if ocrErr != nil {
|
||||
|
|
|
|||
|
|
@ -27,3 +27,37 @@ func TestBuildVLMCaptionPrompt(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestVLMOCRPromptExcludesCustomInstructions pins the OCR-side half of the
|
||||
// prompt contract: knowledge base custom instructions may shape *how images
|
||||
// are described* (caption prompt) but must never reach the OCR prompt, where
|
||||
// free-form business rules compete with the "No text content" output contract
|
||||
// and produce poisoned image_ocr child chunks.
|
||||
func TestVLMOCRPromptExcludesCustomInstructions(t *testing.T) {
|
||||
const marker = "UNIQUE_BUSINESS_MARKER_8f3a"
|
||||
cfg := types.VLMConfig{CustomInstructions: "For images without text output " + marker}
|
||||
|
||||
for _, sourceType := range []string{"", "scanned_pdf"} {
|
||||
got := buildVLMOCRPrompt(sourceType, cfg)
|
||||
if strings.Contains(got, marker) {
|
||||
t.Fatalf("OCR prompt for source %q leaked custom instructions: %s", sourceType, got)
|
||||
}
|
||||
if !strings.Contains(got, "No text content") {
|
||||
t.Fatalf("OCR prompt for source %q lost the No text content contract: %s", sourceType, got)
|
||||
}
|
||||
}
|
||||
|
||||
if got := buildVLMOCRPrompt("scanned_pdf", cfg); got != vlmOCRScannedPDFPrompt {
|
||||
t.Fatalf("scanned_pdf source must use the scanned-PDF prompt verbatim")
|
||||
}
|
||||
if got := buildVLMOCRPrompt("regular", cfg); got != vlmOCRPrompt {
|
||||
t.Fatalf("regular source must use the default OCR prompt verbatim")
|
||||
}
|
||||
|
||||
// The caption path keeps its custom instructions — only the OCR append was
|
||||
// removed.
|
||||
caption := buildVLMCaptionPrompt(context.Background(), cfg)
|
||||
if !strings.Contains(caption, marker) {
|
||||
t.Fatalf("caption prompt should still carry custom instructions: %s", caption)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue