feat(output): enhance output formatting to XML for various tools
- Updated the formatKnowledgeBaseList function to return knowledge base information as XML, improving data structure for integration. - Modified the formatOutput function in GrepChunksTool to format search results as XML, enhancing readability and consistency. - Adjusted the formatOutput method in KnowledgeSearchTool to output search results in XML format, providing a structured response. - Refactored the buildOutput function in ListKnowledgeChunksTool to generate XML output, improving the clarity of chunk information. These changes standardize output formats across multiple tools, facilitating better data handling and integration.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/searchutil"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
)
|
||||
@@ -160,6 +161,22 @@ func (t *ListKnowledgeChunksTool) Execute(ctx context.Context, args json.RawMess
|
||||
totalChunks := total
|
||||
fetched := len(chunks)
|
||||
|
||||
// Enrich image info from child image chunks (lazy loading)
|
||||
if fetched > 0 {
|
||||
chunkIDs := make([]string, 0, fetched)
|
||||
for _, c := range chunks {
|
||||
chunkIDs = append(chunkIDs, c.ID)
|
||||
}
|
||||
infoMap := searchutil.CollectImageInfoByChunkIDs(ctx, t.chunkService.GetRepository(), effectiveTenantID, chunkIDs)
|
||||
for _, c := range chunks {
|
||||
if c.ImageInfo == "" {
|
||||
if merged, ok := infoMap[c.ID]; ok {
|
||||
c.ImageInfo = merged
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
knowledgeTitle := t.lookupKnowledgeTitle(ctx, knowledgeID)
|
||||
|
||||
output := t.buildOutput(knowledgeID, knowledgeTitle, totalChunks, fetched, chunks)
|
||||
@@ -236,7 +253,7 @@ func (t *ListKnowledgeChunksTool) lookupKnowledgeTitle(ctx context.Context, know
|
||||
return strings.TrimSpace(knowledge.Title)
|
||||
}
|
||||
|
||||
// buildOutput builds the output for the list knowledge chunks tool
|
||||
// buildOutput builds the output as XML for the list knowledge chunks tool
|
||||
func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
knowledgeID string,
|
||||
knowledgeTitle string,
|
||||
@@ -244,65 +261,54 @@ func (t *ListKnowledgeChunksTool) buildOutput(
|
||||
fetched int,
|
||||
chunks []*types.Chunk,
|
||||
) string {
|
||||
builder := &strings.Builder{}
|
||||
builder.WriteString("=== Knowledge Document Chunks ===\n\n")
|
||||
var b strings.Builder
|
||||
|
||||
titleAttr := ""
|
||||
if knowledgeTitle != "" {
|
||||
fmt.Fprintf(builder, "Document: %s (%s)\n", knowledgeTitle, knowledgeID)
|
||||
} else {
|
||||
fmt.Fprintf(builder, "Document ID: %s\n", knowledgeID)
|
||||
titleAttr = fmt.Sprintf(" title=\"%s\"", knowledgeTitle)
|
||||
}
|
||||
fmt.Fprintf(builder, "Total chunks: %d\n", total)
|
||||
fmt.Fprintf(&b, "<knowledge_chunks knowledge_id=\"%s\"%s total=\"%d\" fetched=\"%d\">\n",
|
||||
knowledgeID, titleAttr, total, fetched)
|
||||
|
||||
if fetched == 0 {
|
||||
builder.WriteString("No chunks found. Please confirm the document has been parsed.\n")
|
||||
if total > 0 {
|
||||
builder.WriteString("Document exists but the current page is empty. Please check pagination parameters.\n")
|
||||
}
|
||||
return builder.String()
|
||||
b.WriteString("</knowledge_chunks>")
|
||||
return b.String()
|
||||
}
|
||||
fmt.Fprintf(
|
||||
builder,
|
||||
"Fetched: %d chunks, range: %d - %d\n\n",
|
||||
fetched,
|
||||
chunks[0].ChunkIndex,
|
||||
chunks[len(chunks)-1].ChunkIndex,
|
||||
)
|
||||
|
||||
builder.WriteString("=== Chunk Content Preview ===\n\n")
|
||||
for idx, c := range chunks {
|
||||
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, " Type: %s\n", c.ChunkType)
|
||||
fmt.Fprintf(builder, " Content: %s\n", summarizeContent(c.Content))
|
||||
for _, c := range chunks {
|
||||
fmt.Fprintf(&b, "<chunk chunk_id=\"%s\" chunk_index=\"%d\" type=\"%s\">\n",
|
||||
c.ID, c.ChunkIndex, c.ChunkType)
|
||||
fmt.Fprintf(&b, "<content>%s</content>\n", summarizeContent(c.Content))
|
||||
|
||||
// Output associated image information
|
||||
if c.ImageInfo != "" {
|
||||
var imageInfos []types.ImageInfo
|
||||
if err := json.Unmarshal([]byte(c.ImageInfo), &imageInfos); err == nil && len(imageInfos) > 0 {
|
||||
fmt.Fprintf(builder, " Associated images (%d):\n", len(imageInfos))
|
||||
for imgIdx, img := range imageInfos {
|
||||
fmt.Fprintf(builder, " Image %d:\n", imgIdx+1)
|
||||
for _, img := range imageInfos {
|
||||
if img.URL != "" {
|
||||
fmt.Fprintf(builder, " URL: %s\n", img.URL)
|
||||
fmt.Fprintf(&b, "<image url=\"%s\">\n", img.URL)
|
||||
} else {
|
||||
b.WriteString("<image>\n")
|
||||
}
|
||||
if img.Caption != "" {
|
||||
fmt.Fprintf(builder, " Caption: %s\n", img.Caption)
|
||||
fmt.Fprintf(&b, "<image_caption>%s</image_caption>\n", img.Caption)
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
fmt.Fprintf(builder, " OCR Text: %s\n", img.OCRText)
|
||||
fmt.Fprintf(&b, "<image_ocr>%s</image_ocr>\n", img.OCRText)
|
||||
}
|
||||
b.WriteString("</image>\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
|
||||
b.WriteString("</chunk>\n")
|
||||
}
|
||||
|
||||
if int64(fetched) < total {
|
||||
builder.WriteString("Note: The document has more chunks. Adjust offset or make multiple calls to retrieve all content.\n")
|
||||
fmt.Fprintf(&b, "<pagination remaining=\"%d\" />\n", int64(total)-int64(fetched))
|
||||
}
|
||||
|
||||
return builder.String()
|
||||
b.WriteString("</knowledge_chunks>")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// summarizeContent summarizes the content of a chunk
|
||||
|
||||
Reference in New Issue
Block a user