feat: Enhance wiki content processing with enriched image information and XML output

- Introduced `enrichChunkImageInfo` function to populate image information for text chunks by retrieving data from child chunks, ensuring that image-related content is not lost during processing.
- Updated `reconstructContent` to `reconstructEnrichedContent`, allowing for the inclusion of OCR text and captions from images, improving the quality of content sent to the LLM.
- Added `writeDedupItemXML` and `xmlEscape` functions to format entity and concept entries as structured XML, enhancing clarity and preventing nonsensical merges in deduplication prompts.
- Refined the `deduplicateExtractedBatch` method to utilize the new XML formatting for better output consistency.

These changes significantly improve the handling of image data and the overall clarity of deduplication prompts, contributing to a more effective wiki content management system.
This commit is contained in:
wizardchen
2026-04-20 17:55:30 +08:00
parent 173547a17a
commit 53ecd5df5d
4 changed files with 155 additions and 62 deletions
@@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
@@ -53,6 +54,41 @@ If neither query nor range is provided, it returns the beginning of the document
}
}
// enrichChunkImageInfo populates chunk.ImageInfo for a batch of parent text
// chunks by looking up their image_ocr / image_caption children. Chunks that
// already have a non-empty ImageInfo are left untouched.
func enrichChunkImageInfo(
ctx context.Context,
chunkRepo interfaces.ChunkRepository,
tenantID uint64,
chunks []*types.Chunk,
) {
if len(chunks) == 0 || chunkRepo == nil {
return
}
ids := make([]string, 0, len(chunks))
for _, c := range chunks {
if c.ImageInfo == "" && c.ID != "" {
ids = append(ids, c.ID)
}
}
if len(ids) == 0 {
return
}
infoMap := searchutil.CollectImageInfoByChunkIDs(ctx, chunkRepo, tenantID, ids)
if len(infoMap) == 0 {
return
}
for _, c := range chunks {
if c.ImageInfo != "" {
continue
}
if merged, ok := infoMap[c.ID]; ok && merged != "" {
c.ImageInfo = merged
}
}
}
func enrichChunkContent(c *types.Chunk) string {
content := c.Content
if c.ImageInfo != "" {
@@ -164,6 +200,12 @@ func (t *wikiReadSourceDocTool) Execute(ctx context.Context, args json.RawMessag
break
}
// Lazily enrich ImageInfo from image_ocr / image_caption child chunks.
// Parent text chunks don't carry image_info themselves (see
// image_multimodal.go); without this the source-doc reader would
// silently drop OCR text and captions for image-heavy documents.
enrichChunkImageInfo(ctx, t.chunkService.GetRepository(), knowledge.TenantID, chunks)
for _, c := range chunks {
chunkNum := c.ChunkIndex + 1
chunkContent := enrichChunkContent(c)