fix(ingest): stop hallucinating summaries from filenames on empty PDFs
Scanned PDFs without a successful OCR pass (e.g. when no VLM is configured on the KB, or when the VLM returned a "no text content" reply that the sanitizer discarded) reach the summary and wiki-ingest pipelines with nothing but markdown image references for content. The downstream LLM prompts used to receive the filename, file type, and title alongside that empty content, and the generate_summary template explicitly forbade the model from refusing — so the model would invent a topic from the filename alone, producing e.g. a "Canon scanner manual" summary for an MX5280.pdf that is actually a scanned legal letter. Two changes shut this down: 1. Drop filename / title / file-type from every LLM prompt input. The wiki summary, knowledge extract, candidate slug, and chunk citation prompts now see only the document content. The knowledge summary path no longer prepends the "Document Type: X / File Name: Y" intro. 2. Add a content-sufficiency guard in front of the LLM call. A new helper strips markdown image references and HTML image tags and refuses to invoke the model when fewer than 50 real-text runes remain. getSummary returns a sentinel error in that case and the caller marks the knowledge as SummaryStatusFailed instead of surfacing the first chunk's bare image reference. Wiki ingest skips LLM extraction with a clear log message. The generate_summary.yaml prompt is also updated: the "NEVER output refusal phrases" clause is removed and replaced with an explicit empty-content rule that returns a deterministic placeholder line. Real OCR (Tesseract / PaddleOCR) and filename-pattern sanitisation are intentionally left for separate PRs.
This commit is contained in:
@@ -106,3 +106,70 @@ func TestReconstructContentEmpty(t *testing.T) {
|
||||
t.Errorf("Empty chunks should produce empty content, got %q", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripImageMarkup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{"plain text untouched", "Hello world.", "Hello world."},
|
||||
{"single markdown image removed", "", ""},
|
||||
{
|
||||
"scanned-pdf style page references all stripped",
|
||||
"\n\n",
|
||||
"\n\n",
|
||||
},
|
||||
{"mixed text and image keeps text", "Intro paragraph.\n\nConclusion.", "Intro paragraph.\n\nConclusion."},
|
||||
{"html img tag stripped", `Before <img src="x.png" alt="y"/> after`, "Before after"},
|
||||
{"images xml block stripped", "Body <images><image>x</image></images> tail", "Body tail"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := stripImageMarkup(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("stripImageMarkup(%q) = %q, want %q", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasSufficientTextContent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want bool
|
||||
}{
|
||||
{"empty string", "", false},
|
||||
{"only whitespace", " \n\n\t ", false},
|
||||
{
|
||||
"only image references (scanned PDF without OCR)",
|
||||
"\n",
|
||||
false,
|
||||
},
|
||||
{"short text below threshold", "tiny", false},
|
||||
{
|
||||
"short text mixed with images still below threshold",
|
||||
"\nshort\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"sufficient text passes",
|
||||
"This is a substantial paragraph of real content describing a legal matter and the parties involved.",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"sufficient text mixed with images still passes",
|
||||
"\nThis is a substantial paragraph of real content describing a legal matter and the parties involved.\n",
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := hasSufficientTextContent(tt.input)
|
||||
if got != tt.want {
|
||||
t.Errorf("hasSufficientTextContent(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user