From 7ea5bf4349509a352b2cf9584a4ada3316c074b3 Mon Sep 17 00:00:00 2001 From: tayadwx <42323260+tayadwx@users.noreply.github.com> Date: Sat, 19 Sep 2026 23:37:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(image):=20stop=20appending=20KB=20custom=20?= =?UTF-8?q?instructions=20to=20the=20OCR=20prompt=20OCR=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=8D=E4=B8=8D=E5=86=8D=E9=94=99=E8=AF=AF=E7=9A=84=E9=99=84?= =?UTF-8?q?=E5=8A=A0=E5=9B=BE=E7=89=87=E8=A7=A3=E6=9E=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8F=90=E7=A4=BA=E8=AF=8D=20(#3291?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- .../application/service/image_multimodal.go | 18 ++++++++-- .../service/image_multimodal_prompt_test.go | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/internal/application/service/image_multimodal.go b/internal/application/service/image_multimodal.go index aedd671c..631425f0 100644 --- a/internal/application/service/image_multimodal.go +++ b/internal/application/service/image_multimodal.go @@ -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 { diff --git a/internal/application/service/image_multimodal_prompt_test.go b/internal/application/service/image_multimodal_prompt_test.go index 4eaa1eaf..578af662 100644 --- a/internal/application/service/image_multimodal_prompt_test.go +++ b/internal/application/service/image_multimodal_prompt_test.go @@ -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) + } +}