Skip to content

Commit 6cbf025

Browse files
feat: inject per-chunk metadata (page, source) as XML into summarization input
Each chunk now includes its page number and source path as XML metadata before the text content, giving the LLM context for citations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8f72df6 commit 6cbf025

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

wdoc/utils/tasks/summarize.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,28 @@ def _summarize(
523523
for ird, rd in tqdm(enumerate(docs), desc="Summarising splits", total=len(docs)):
524524
fixed_index = f"{ird + 1}/{len(docs)}"
525525

526+
# Build chunk text with per-chunk metadata if available
527+
chunk_text = rd.page_content
528+
chunk_meta_parts = []
529+
if hasattr(rd, "metadata") and rd.metadata:
530+
if "page" in rd.metadata:
531+
chunk_meta_parts.append(f"<page>{rd.metadata['page']}</page>")
532+
if "source" in rd.metadata:
533+
chunk_meta_parts.append(f"<source>{rd.metadata['source']}</source>")
534+
if chunk_meta_parts:
535+
chunk_metadata_xml = (
536+
"<chunk_metadata>\n"
537+
+ "\n".join(chunk_meta_parts)
538+
+ "\n</chunk_metadata>\n\n"
539+
)
540+
chunk_text = chunk_metadata_xml + chunk_text
541+
526542
messages = BASE_SUMMARY_PROMPT.format_messages(
527543
language=language,
528544
metadata=metadata.replace("[PROGRESS]", fixed_index),
529545
previous_summary=previous_summary,
530546
recursion_instruction="" if not n_recursion else RECURSION_INSTRUCTION,
531-
text=rd.page_content,
547+
text=chunk_text,
532548
)
533549
if " object at " in llm._get_llm_string():
534550
logger.warning(

0 commit comments

Comments
 (0)