diff --git a/internal/agent/tools/knowledge_search.go b/internal/agent/tools/knowledge_search.go index 49b83fef..e6b590f9 100644 --- a/internal/agent/tools/knowledge_search.go +++ b/internal/agent/tools/knowledge_search.go @@ -1050,6 +1050,26 @@ func (t *KnowledgeSearchTool) formatOutput( result.Content, ) + // 解析并输出关联的图片信息 + if result.ImageInfo != "" { + var imageInfos []types.ImageInfo + if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { + output += fmt.Sprintf(" Related Images (%d):\n", len(imageInfos)) + for imgIdx, img := range imageInfos { + output += fmt.Sprintf(" Image %d:\n", imgIdx+1) + if img.URL != "" { + output += fmt.Sprintf(" URL: %s\n", img.URL) + } + if img.Caption != "" { + output += fmt.Sprintf(" Caption: %s\n", img.Caption) + } + if img.OCRText != "" { + output += fmt.Sprintf(" OCR Text: %s\n", img.OCRText) + } + } + } + } + if faqMeta != nil { if faqMeta.StandardQuestion != "" { output += fmt.Sprintf(" FAQ Standard Question: %s\n", faqMeta.StandardQuestion) @@ -1080,6 +1100,34 @@ func (t *KnowledgeSearchTool) formatOutput( }) last := formattedResults[len(formattedResults)-1] + + // 添加图片信息到结构化数据 + if result.ImageInfo != "" { + var imageInfos []types.ImageInfo + if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { + // 构建简化的图片信息列表 + imageList := make([]map[string]string, 0, len(imageInfos)) + for _, img := range imageInfos { + imgData := make(map[string]string) + if img.URL != "" { + imgData["url"] = img.URL + } + if img.Caption != "" { + imgData["caption"] = img.Caption + } + if img.OCRText != "" { + imgData["ocr_text"] = img.OCRText + } + if len(imgData) > 0 { + imageList = append(imageList, imgData) + } + } + if len(imageList) > 0 { + last["images"] = imageList + } + } + } + if faqMeta != nil { if faqMeta.StandardQuestion != "" { last["faq_standard_question"] = faqMeta.StandardQuestion diff --git a/internal/agent/tools/list_knowledge_chunks.go b/internal/agent/tools/list_knowledge_chunks.go index 587e7405..839d957b 100644 --- a/internal/agent/tools/list_knowledge_chunks.go +++ b/internal/agent/tools/list_knowledge_chunks.go @@ -2,6 +2,7 @@ package tools import ( "context" + "encoding/json" "fmt" "strings" @@ -139,7 +140,7 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i formattedChunks := make([]map[string]interface{}, 0, len(chunks)) for idx, c := range chunks { - formattedChunks = append(formattedChunks, map[string]interface{}{ + chunkData := map[string]interface{}{ "seq": idx + 1, "chunk_id": c.ID, "chunk_index": c.ChunkIndex, @@ -150,7 +151,35 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args map[string]i "start_at": c.StartAt, "end_at": c.EndAt, "parent_chunk_id": c.ParentChunkID, - }) + } + + // 添加图片信息 + if c.ImageInfo != "" { + var imageInfos []types.ImageInfo + if err := json.Unmarshal([]byte(c.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { + imageList := make([]map[string]string, 0, len(imageInfos)) + for _, img := range imageInfos { + imgData := make(map[string]string) + if img.URL != "" { + imgData["url"] = img.URL + } + if img.Caption != "" { + imgData["caption"] = img.Caption + } + if img.OCRText != "" { + imgData["ocr_text"] = img.OCRText + } + if len(imgData) > 0 { + imageList = append(imageList, imgData) + } + } + if len(imageList) > 0 { + chunkData["images"] = imageList + } + } + } + + formattedChunks = append(formattedChunks, chunkData) } return &types.ToolResult{ @@ -218,7 +247,28 @@ func (t *ListKnowledgeChunksTool) buildOutput( fmt.Fprintf(builder, "Chunk #%d (Index %d)\n", idx+1, c.ChunkIndex+1) fmt.Fprintf(builder, " chunk_id: %s\n", c.ID) fmt.Fprintf(builder, " 类型: %s\n", c.ChunkType) - fmt.Fprintf(builder, " 内容: %s\n\n", summarizeContent(c.Content)) + fmt.Fprintf(builder, " 内容: %s\n", summarizeContent(c.Content)) + + // 输出关联的图片信息 + if c.ImageInfo != "" { + var imageInfos []types.ImageInfo + if err := json.Unmarshal([]byte(c.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 { + fmt.Fprintf(builder, " 关联图片 (%d):\n", len(imageInfos)) + for imgIdx, img := range imageInfos { + fmt.Fprintf(builder, " 图片 %d:\n", imgIdx+1) + if img.URL != "" { + fmt.Fprintf(builder, " URL: %s\n", img.URL) + } + if img.Caption != "" { + fmt.Fprintf(builder, " 描述: %s\n", img.Caption) + } + if img.OCRText != "" { + fmt.Fprintf(builder, " OCR文本: %s\n", img.OCRText) + } + } + } + } + builder.WriteString("\n") } if int64(fetched) < total { diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 77152ed0..dbc5661f 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -1069,6 +1069,55 @@ func (s *knowledgeService) processChunks(ctx context.Context, logger.Infof(ctx, "Cleanup completed, starting to process new chunks") + // ========== DocReader 解析结果日志 ========== + logger.Infof(ctx, "[DocReader] ========== 解析结果概览 ==========") + logger.Infof(ctx, "[DocReader] 知识ID: %s, 知识库ID: %s", knowledge.ID, knowledge.KnowledgeBaseID) + logger.Infof(ctx, "[DocReader] 总Chunk数量: %d", len(chunks)) + + // 统计图片信息 + totalImages := 0 + chunksWithImages := 0 + for _, chunkData := range chunks { + if len(chunkData.Images) > 0 { + chunksWithImages++ + totalImages += len(chunkData.Images) + } + } + logger.Infof(ctx, "[DocReader] 包含图片的Chunk数: %d, 总图片数: %d", chunksWithImages, totalImages) + + // 打印每个Chunk的详细信息 + for idx, chunkData := range chunks { + contentPreview := chunkData.Content + if len(contentPreview) > 200 { + contentPreview = contentPreview[:200] + "..." + } + logger.Infof(ctx, "[DocReader] Chunk #%d (seq=%d): 内容长度=%d, 图片数=%d, 范围=[%d-%d]", + idx, chunkData.Seq, len(chunkData.Content), len(chunkData.Images), chunkData.Start, chunkData.End) + logger.Debugf(ctx, "[DocReader] Chunk #%d 内容预览: %s", idx, contentPreview) + + // 打印图片详细信息 + for imgIdx, img := range chunkData.Images { + logger.Infof(ctx, "[DocReader] 图片 #%d: URL=%s", imgIdx, img.Url) + logger.Infof(ctx, "[DocReader] 图片 #%d: OriginalURL=%s", imgIdx, img.OriginalUrl) + if img.Caption != "" { + captionPreview := img.Caption + if len(captionPreview) > 100 { + captionPreview = captionPreview[:100] + "..." + } + logger.Infof(ctx, "[DocReader] 图片 #%d: Caption=%s", imgIdx, captionPreview) + } + if img.OcrText != "" { + ocrPreview := img.OcrText + if len(ocrPreview) > 100 { + ocrPreview = ocrPreview[:100] + "..." + } + logger.Infof(ctx, "[DocReader] 图片 #%d: OCRText=%s", imgIdx, ocrPreview) + } + logger.Infof(ctx, "[DocReader] 图片 #%d: 位置=[%d-%d]", imgIdx, img.Start, img.End) + } + } + logger.Infof(ctx, "[DocReader] ========== 解析结果概览结束 ==========") + // Create chunk objects from proto chunks maxSeq := 0