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:
Claude
2026-05-02 09:06:32 +00:00
committed by lyingbug
parent d3d433574f
commit 15550d14d4
7 changed files with 183 additions and 42 deletions
+12 -7
View File
@@ -5,12 +5,16 @@ package agent
// knowledge from raw documents and build/update wiki pages.
// WikiSummaryPrompt generates a summary page for a newly ingested document.
//
// Filename and title are intentionally NOT passed to the LLM: documents
// uploaded to WeKnora often carry filenames that say nothing about the
// content (e.g. scanned PDFs named after the scanner model "MX5280.pdf"),
// and feeding such filenames to the model invites hallucinated summaries
// when the actual extracted content is thin. The model must rely solely on
// the document content provided below.
const WikiSummaryPrompt = `You are a wiki editor. Given the following document content, create a structured wiki summary page in Markdown format.
<document>
<title>{{.Title}}</title>
<file_name>{{.FileName}}</file_name>
<file_type>{{.FileType}}</file_type>
<content>
{{.Content}}
</content>
@@ -30,6 +34,7 @@ const WikiSummaryPrompt = `You are a wiki editor. Given the following document c
7. At the end, include a "## Key Takeaways" section with bullet points.
8. Write in {{.Language}}.
9. Keep the summary concise but thorough (500-1500 words depending on document length).
10. **Empty content rule**: If the <content> block above is empty, contains only image references with no extracted text, or otherwise carries no substantive information, output exactly: "SUMMARY: No textual content was extractable from this document." followed by a brief note explaining that the document could not be summarised. Do NOT invent a topic, do NOT guess from any other clue.
</instructions>
Output the SUMMARY line first, then the Markdown content. Do not include any other preamble.`
@@ -40,7 +45,6 @@ Output the SUMMARY line first, then the Markdown content. Do not include any oth
const WikiKnowledgeExtractPrompt = `You are a knowledge extraction system. Analyze the following document and extract all significant entities AND key concepts.
<document>
<title>{{.Title}}</title>
<content>
{{.Content}}
</content>
@@ -54,6 +58,8 @@ const WikiKnowledgeExtractPrompt = `You are a knowledge extraction system. Analy
Return a JSON object with two arrays: "entities" and "concepts".
**IMPORTANT: Write ALL names, descriptions, and details in {{.Language}}**.
If the <content> block above is empty, contains only image references with no extracted text, or otherwise carries no substantive information, return {"entities": [], "concepts": []}. Do NOT invent entities or concepts from any other source.
### Slug Continuity Rules
If previous slugs are provided above, you MUST follow these rules:
- If an entity or concept from the previous extraction still exists in the current document, **reuse its exact slug** from the previous list. Do NOT generate a new slug for the same thing.
@@ -121,7 +127,6 @@ Output ONLY valid JSON. Example:
const WikiCandidateSlugPrompt = `You are a knowledge extraction system. Analyze the following document and list all significant entities AND key concepts as a lightweight candidate set. Another pass will later attach concrete supporting chunks to each item, so you do NOT need to write exhaustive per-item facts here.
<document>
<title>{{.Title}}</title>
<content>
{{.Content}}
</content>
@@ -135,6 +140,8 @@ const WikiCandidateSlugPrompt = `You are a knowledge extraction system. Analyze
Return a JSON object with two arrays: "entities" and "concepts".
**IMPORTANT: Write ALL names, descriptions, and details in {{.Language}}**.
If the <content> block above is empty, contains only image references with no extracted text, or otherwise carries no substantive information, return {"entities": [], "concepts": []}. Do NOT invent entities or concepts from any other source.
### Extraction Scope (Granularity: {{.Granularity}})
{{.GranularityGuidance}}
@@ -202,8 +209,6 @@ Output ONLY valid JSON. Example:
// their verbatim form (the chunk text) instead of asking the LLM to paraphrase.
const WikiChunkCitationPrompt = `You are a precise citation system. Your job is to scan a batch of document chunks and decide, for each candidate entity/concept below, which chunks substantively discuss it.
<document_title>{{.DocTitle}}</document_title>
<candidate_slugs>
{{.CandidateSlugs}}
</candidate_slugs>