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:
+33
-38
@@ -105,79 +105,74 @@ func AvailablePlaceholders() []PlaceholderDefinition {
|
||||
return result
|
||||
}
|
||||
|
||||
// formatKnowledgeBaseList formats knowledge base information for the prompt
|
||||
// formatKnowledgeBaseList formats knowledge base information as XML for the prompt
|
||||
func formatKnowledgeBaseList(kbInfos []*KnowledgeBaseInfo) string {
|
||||
if len(kbInfos) == 0 {
|
||||
return "None"
|
||||
return "<knowledge_bases />"
|
||||
}
|
||||
|
||||
var builder strings.Builder
|
||||
builder.WriteString("\nThe following knowledge bases have been selected by the user for this conversation. ")
|
||||
builder.WriteString("You should search within these knowledge bases to find relevant information.\n\n")
|
||||
for i, kb := range kbInfos {
|
||||
// Display knowledge base name and ID
|
||||
builder.WriteString(fmt.Sprintf("%d. **%s** (knowledge_base_id: `%s`)\n", i+1, kb.Name, kb.ID))
|
||||
|
||||
// Display knowledge base type
|
||||
var b strings.Builder
|
||||
b.WriteString("<knowledge_bases>\n")
|
||||
for _, kb := range kbInfos {
|
||||
kbType := kb.Type
|
||||
if kbType == "" {
|
||||
kbType = "document" // Default type
|
||||
kbType = "document"
|
||||
}
|
||||
builder.WriteString(fmt.Sprintf(" - Type: %s\n", kbType))
|
||||
|
||||
b.WriteString(fmt.Sprintf("<knowledge_base id=\"%s\" name=\"%s\" type=\"%s\" doc_count=\"%d\">\n",
|
||||
kb.ID, kb.Name, kbType, kb.DocCount))
|
||||
if kb.Description != "" {
|
||||
builder.WriteString(fmt.Sprintf(" - Description: %s\n", kb.Description))
|
||||
b.WriteString(fmt.Sprintf("<description>%s</description>\n", kb.Description))
|
||||
}
|
||||
builder.WriteString(fmt.Sprintf(" - Document count: %d\n", kb.DocCount))
|
||||
|
||||
// Display recent documents if available
|
||||
// For FAQ type knowledge bases, adjust the display format
|
||||
if len(kb.RecentDocs) > 0 {
|
||||
if kbType == "faq" {
|
||||
// FAQ knowledge base: show Q&A pairs in a more compact format
|
||||
builder.WriteString(" - Recent FAQ entries:\n\n")
|
||||
builder.WriteString(" | # | Question | Answers | Chunk ID | Knowledge ID | Created At |\n")
|
||||
builder.WriteString(" |---|-------------------|---------|----------|--------------|------------|\n")
|
||||
b.WriteString("<faq_entries>\n")
|
||||
for j, doc := range kb.RecentDocs {
|
||||
if j >= 10 { // Limit to 10 documents
|
||||
if j >= 10 {
|
||||
break
|
||||
}
|
||||
question := doc.FAQStandardQuestion
|
||||
if question == "" {
|
||||
question = doc.FileName
|
||||
}
|
||||
answers := "-"
|
||||
b.WriteString(fmt.Sprintf("<faq chunk_id=\"%s\" knowledge_id=\"%s\" created_at=\"%s\">\n",
|
||||
doc.ChunkID, doc.KnowledgeID, doc.CreatedAt))
|
||||
b.WriteString(fmt.Sprintf("<question>%s</question>\n", question))
|
||||
if len(doc.FAQAnswers) > 0 {
|
||||
answers = strings.Join(doc.FAQAnswers, " | ")
|
||||
for _, ans := range doc.FAQAnswers {
|
||||
b.WriteString(fmt.Sprintf("<answer>%s</answer>\n", ans))
|
||||
}
|
||||
}
|
||||
builder.WriteString(fmt.Sprintf(" | %d | %s | %s | `%s` | `%s` | %s |\n",
|
||||
j+1, question, answers, doc.ChunkID, doc.KnowledgeID, doc.CreatedAt))
|
||||
b.WriteString("</faq>\n")
|
||||
}
|
||||
b.WriteString("</faq_entries>\n")
|
||||
} else {
|
||||
// Document knowledge base: show documents in standard format
|
||||
builder.WriteString(" - Recently added documents:\n\n")
|
||||
builder.WriteString(" | # | Document Name | Type | Created At | Knowledge ID | File Size | Summary |\n")
|
||||
builder.WriteString(" |---|---------------|------|------------|--------------|----------|---------|\n")
|
||||
b.WriteString("<recent_documents>\n")
|
||||
for j, doc := range kb.RecentDocs {
|
||||
if j >= 10 { // Limit to 10 documents
|
||||
if j >= 10 {
|
||||
break
|
||||
}
|
||||
docName := doc.Title
|
||||
if docName == "" {
|
||||
docName = doc.FileName
|
||||
}
|
||||
// Format file size
|
||||
fileSize := formatFileSize(doc.FileSize)
|
||||
summary := formatDocSummary(doc.Description, 120)
|
||||
builder.WriteString(fmt.Sprintf(" | %d | %s | %s | %s | `%s` | %s | %s |\n",
|
||||
j+1, docName, doc.Type, doc.CreatedAt, doc.KnowledgeID, fileSize, summary))
|
||||
b.WriteString(fmt.Sprintf("<document knowledge_id=\"%s\" type=\"%s\" file_size=\"%s\" created_at=\"%s\">\n",
|
||||
doc.KnowledgeID, doc.Type, fileSize, doc.CreatedAt))
|
||||
b.WriteString(fmt.Sprintf("<name>%s</name>\n", docName))
|
||||
if doc.Description != "" {
|
||||
summary := formatDocSummary(doc.Description, 120)
|
||||
b.WriteString(fmt.Sprintf("<summary>%s</summary>\n", summary))
|
||||
}
|
||||
b.WriteString("</document>\n")
|
||||
}
|
||||
b.WriteString("</recent_documents>\n")
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
builder.WriteString("\n")
|
||||
b.WriteString("</knowledge_base>\n")
|
||||
}
|
||||
return builder.String()
|
||||
b.WriteString("</knowledge_bases>")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// renderPromptPlaceholders renders placeholders in the prompt template
|
||||
|
||||
@@ -374,7 +374,7 @@ func (t *GrepChunksTool) searchChunks(
|
||||
return results, int64(len(results)), nil
|
||||
}
|
||||
|
||||
// formatOutput formats the search results for display (grep-style output)
|
||||
// formatOutput formats the search results as XML
|
||||
func (t *GrepChunksTool) formatOutput(
|
||||
ctx context.Context,
|
||||
results []knowledgeAggregation,
|
||||
@@ -382,46 +382,37 @@ func (t *GrepChunksTool) formatOutput(
|
||||
patterns []string,
|
||||
countOnly bool,
|
||||
) string {
|
||||
var output strings.Builder
|
||||
var b strings.Builder
|
||||
|
||||
// If count_only mode, just return the count
|
||||
if countOnly {
|
||||
output.WriteString(fmt.Sprintf("%d\n", totalCount))
|
||||
return output.String()
|
||||
b.WriteString(fmt.Sprintf("<grep_results count=\"%d\" />\n", totalCount))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Show search info
|
||||
if len(patterns) == 1 {
|
||||
output.WriteString(fmt.Sprintf("Pattern: '%s' (case-insensitive)\n", patterns[0]))
|
||||
} else {
|
||||
output.WriteString(fmt.Sprintf("Patterns (%d): %v (case-insensitive, OR logic)\n", len(patterns), patterns))
|
||||
b.WriteString(fmt.Sprintf("<grep_results match_count=\"%d\">\n", len(results)))
|
||||
for _, p := range patterns {
|
||||
b.WriteString(fmt.Sprintf("<pattern>%s</pattern>\n", p))
|
||||
}
|
||||
output.WriteString(fmt.Sprintf("Matches: %d knowledge item(s)\n\n", len(results)))
|
||||
|
||||
if len(results) == 0 {
|
||||
output.WriteString("No matches found.\n")
|
||||
return output.String()
|
||||
b.WriteString("</grep_results>")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
for idx, result := range results {
|
||||
var patternSummaries []string
|
||||
for _, result := range results {
|
||||
b.WriteString(fmt.Sprintf("<match knowledge_id=\"%s\" title=\"%s\" chunk_hits=\"%d\" chunk_total=\"%d\">\n",
|
||||
result.KnowledgeID, result.KnowledgeTitle, result.ChunkHitCount, result.TotalChunkCount))
|
||||
for _, pattern := range patterns {
|
||||
count := result.PatternCounts[pattern]
|
||||
patternSummaries = append(patternSummaries, fmt.Sprintf("%s=%d", pattern, count))
|
||||
if count > 0 {
|
||||
b.WriteString(fmt.Sprintf("<pattern_hit pattern=\"%s\" count=\"%d\" />\n", pattern, count))
|
||||
}
|
||||
}
|
||||
|
||||
output.WriteString(
|
||||
fmt.Sprintf("%d) knowledge_id=%s | title=%s | chunk_hits=%d | chunk_total=%d | pattern_hits=[%s]\n",
|
||||
idx+1,
|
||||
result.KnowledgeID,
|
||||
result.KnowledgeTitle,
|
||||
result.ChunkHitCount,
|
||||
result.TotalChunkCount,
|
||||
strings.Join(patternSummaries, ", "),
|
||||
),
|
||||
)
|
||||
b.WriteString("</match>\n")
|
||||
}
|
||||
return output.String()
|
||||
|
||||
b.WriteString("</grep_results>")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
type knowledgeAggregation struct {
|
||||
|
||||
@@ -395,6 +395,21 @@ func (t *KnowledgeSearchTool) Execute(ctx context.Context, args json.RawMessage)
|
||||
}
|
||||
}
|
||||
|
||||
// Enrich image info for search results (lazy-loaded from child image chunks)
|
||||
if t.chunkService != nil && len(deduplicatedResults) > 0 {
|
||||
byTenant := make(map[uint64][]*types.SearchResult)
|
||||
for _, r := range deduplicatedResults {
|
||||
tid := t.searchTargets.GetTenantIDForKB(r.KnowledgeBaseID)
|
||||
if tid == 0 {
|
||||
continue
|
||||
}
|
||||
byTenant[tid] = append(byTenant[tid], r.SearchResult)
|
||||
}
|
||||
for tid, batch := range byTenant {
|
||||
searchutil.EnrichSearchResultsImageInfo(ctx, t.chunkService.GetRepository(), tid, batch)
|
||||
}
|
||||
}
|
||||
|
||||
// Build output
|
||||
logger.Infof(ctx, "[Tool][KnowledgeSearch] Formatting output with %d final results", len(deduplicatedResults))
|
||||
result, err := t.formatOutput(ctx, deduplicatedResults, kbIDs, queries)
|
||||
@@ -1065,144 +1080,106 @@ func (t *KnowledgeSearchTool) formatOutput(
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Build output header
|
||||
output := "=== Search Results ===\n"
|
||||
output += fmt.Sprintf("Found %d relevant results", len(results))
|
||||
output += "\n\n"
|
||||
|
||||
// Count results by KB
|
||||
kbCounts := make(map[string]int)
|
||||
for _, r := range results {
|
||||
kbCounts[r.KnowledgeID]++
|
||||
}
|
||||
|
||||
output += "Knowledge Base Coverage:\n"
|
||||
for kbID, count := range kbCounts {
|
||||
output += fmt.Sprintf(" - %s: %d results\n", kbID, count)
|
||||
}
|
||||
output += "\n=== Detailed Results ===\n\n"
|
||||
// Format individual results as XML
|
||||
var ob strings.Builder
|
||||
ob.WriteString(fmt.Sprintf("<search_results count=\"%d\">\n", len(results)))
|
||||
|
||||
// Format individual results
|
||||
formattedResults := make([]map[string]interface{}, 0, len(results))
|
||||
currentKB := ""
|
||||
|
||||
faqMetadataCache := make(map[string]*types.FAQChunkMetadata)
|
||||
|
||||
// Track chunks per knowledge for statistics
|
||||
knowledgeChunkMap := make(map[string]map[int]bool) // knowledge_id -> set of chunk_index
|
||||
knowledgeTotalMap := make(map[string]int64) // knowledge_id -> total chunks
|
||||
knowledgeTitleMap := make(map[string]string) // knowledge_id -> title
|
||||
knowledgeChunkMap := make(map[string]map[int]bool)
|
||||
knowledgeTotalMap := make(map[string]int64)
|
||||
knowledgeTitleMap := make(map[string]string)
|
||||
|
||||
for i, result := range results {
|
||||
var faqMeta *types.FAQChunkMetadata
|
||||
if result.KnowledgeBaseType == types.KnowledgeBaseTypeFAQ {
|
||||
meta, err := t.getFAQMetadata(ctx, result.ID, faqMetadataCache)
|
||||
if err != nil {
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
"[Tool][KnowledgeSearch] Failed to load FAQ metadata for chunk %s: %v",
|
||||
result.ID,
|
||||
err,
|
||||
)
|
||||
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to load FAQ metadata for chunk %s: %v", result.ID, err)
|
||||
} else {
|
||||
faqMeta = meta
|
||||
}
|
||||
}
|
||||
|
||||
// Track chunk indices per knowledge
|
||||
if knowledgeChunkMap[result.KnowledgeID] == nil {
|
||||
knowledgeChunkMap[result.KnowledgeID] = make(map[int]bool)
|
||||
}
|
||||
knowledgeChunkMap[result.KnowledgeID][result.ChunkIndex] = true
|
||||
knowledgeTitleMap[result.KnowledgeID] = result.KnowledgeTitle
|
||||
|
||||
// Group by knowledge base
|
||||
if result.KnowledgeID != currentKB {
|
||||
currentKB = result.KnowledgeID
|
||||
if i > 0 {
|
||||
output += "\n"
|
||||
}
|
||||
output += fmt.Sprintf("[Source Document: %s]\n", result.KnowledgeTitle)
|
||||
|
||||
// Get total chunk count for this knowledge (cache it)
|
||||
// Use KB's tenant_id from searchTargets to support cross-tenant shared KB
|
||||
if _, exists := knowledgeTotalMap[result.KnowledgeID]; !exists {
|
||||
// Get tenant_id from searchTargets using the KB ID from the result
|
||||
effectiveTenantID := t.searchTargets.GetTenantIDForKB(result.KnowledgeBaseID)
|
||||
if effectiveTenantID == 0 {
|
||||
logger.Warnf(ctx, "[Tool][KnowledgeSearch] KB %s not found in searchTargets, skipping chunk count", result.KnowledgeBaseID)
|
||||
// Cache total chunk count per knowledge
|
||||
if _, exists := knowledgeTotalMap[result.KnowledgeID]; !exists {
|
||||
effectiveTenantID := t.searchTargets.GetTenantIDForKB(result.KnowledgeBaseID)
|
||||
if effectiveTenantID == 0 {
|
||||
logger.Warnf(ctx, "[Tool][KnowledgeSearch] KB %s not found in searchTargets, skipping chunk count", result.KnowledgeBaseID)
|
||||
knowledgeTotalMap[result.KnowledgeID] = 0
|
||||
} else {
|
||||
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
|
||||
effectiveTenantID, result.KnowledgeID,
|
||||
&types.Pagination{Page: 1, PageSize: 1},
|
||||
[]types.ChunkType{types.ChunkTypeText}, "", "", "", "", "",
|
||||
)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v", result.KnowledgeID, err)
|
||||
knowledgeTotalMap[result.KnowledgeID] = 0
|
||||
} else {
|
||||
_, total, err := t.chunkService.GetRepository().ListPagedChunksByKnowledgeID(ctx,
|
||||
effectiveTenantID, result.KnowledgeID,
|
||||
&types.Pagination{Page: 1, PageSize: 1},
|
||||
[]types.ChunkType{types.ChunkTypeText}, "", "", "", "", "",
|
||||
)
|
||||
if err != nil {
|
||||
logger.Warnf(
|
||||
ctx,
|
||||
"[Tool][KnowledgeSearch] Failed to get total chunks for knowledge %s: %v",
|
||||
result.KnowledgeID,
|
||||
err,
|
||||
)
|
||||
knowledgeTotalMap[result.KnowledgeID] = 0
|
||||
} else {
|
||||
knowledgeTotalMap[result.KnowledgeID] = total
|
||||
}
|
||||
knowledgeTotalMap[result.KnowledgeID] = total
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// relevanceLevel := GetRelevanceLevel(result.Score)
|
||||
output += fmt.Sprintf("\nResult #%d:\n", i+1)
|
||||
output += fmt.Sprintf(
|
||||
" [chunk_id: %s][chunk_index: %d]\nContent: %s\n",
|
||||
result.ID,
|
||||
result.ChunkIndex,
|
||||
result.Content,
|
||||
)
|
||||
ob.WriteString(fmt.Sprintf("<result id=\"%d\" chunk_id=\"%s\" chunk_index=\"%d\" knowledge_id=\"%s\" source=\"%s\">\n",
|
||||
i+1, result.ID, result.ChunkIndex, result.KnowledgeID, result.KnowledgeTitle))
|
||||
ob.WriteString(fmt.Sprintf("<content>%s</content>\n", 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)
|
||||
}
|
||||
for _, img := range imageInfos {
|
||||
ob.WriteString(fmt.Sprintf("<image url=\"%s\">\n", img.URL))
|
||||
if img.Caption != "" {
|
||||
output += fmt.Sprintf(" Caption: %s\n", img.Caption)
|
||||
ob.WriteString(fmt.Sprintf("<image_caption>%s</image_caption>\n", img.Caption))
|
||||
}
|
||||
if img.OCRText != "" {
|
||||
output += fmt.Sprintf(" OCR Text: %s\n", img.OCRText)
|
||||
ob.WriteString(fmt.Sprintf("<image_ocr>%s</image_ocr>\n", img.OCRText))
|
||||
}
|
||||
ob.WriteString("</image>\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if faqMeta != nil {
|
||||
ob.WriteString("<faq>\n")
|
||||
if faqMeta.StandardQuestion != "" {
|
||||
output += fmt.Sprintf(" FAQ Standard Question: %s\n", faqMeta.StandardQuestion)
|
||||
ob.WriteString(fmt.Sprintf("<question>%s</question>\n", faqMeta.StandardQuestion))
|
||||
}
|
||||
if len(faqMeta.SimilarQuestions) > 0 {
|
||||
output += fmt.Sprintf(" FAQ Similar Questions: %s\n", strings.Join(faqMeta.SimilarQuestions, "; "))
|
||||
}
|
||||
if len(faqMeta.Answers) > 0 {
|
||||
output += " FAQ Answers:\n"
|
||||
for ansIdx, ans := range faqMeta.Answers {
|
||||
output += fmt.Sprintf(" Answer Choice %d: %s\n", ansIdx+1, ans)
|
||||
for _, sq := range faqMeta.SimilarQuestions {
|
||||
ob.WriteString(fmt.Sprintf("<similar_question>%s</similar_question>\n", sq))
|
||||
}
|
||||
}
|
||||
if len(faqMeta.Answers) > 0 {
|
||||
for _, ans := range faqMeta.Answers {
|
||||
ob.WriteString(fmt.Sprintf("<answer>%s</answer>\n", ans))
|
||||
}
|
||||
}
|
||||
ob.WriteString("</faq>\n")
|
||||
}
|
||||
|
||||
ob.WriteString("</result>\n")
|
||||
|
||||
formattedResults = append(formattedResults, map[string]interface{}{
|
||||
"result_index": i + 1,
|
||||
"chunk_id": result.ID,
|
||||
"content": result.Content,
|
||||
// "score": result.Score,
|
||||
// "relevance_level": relevanceLevel,
|
||||
"result_index": i + 1,
|
||||
"chunk_id": result.ID,
|
||||
"content": result.Content,
|
||||
"knowledge_id": result.KnowledgeID,
|
||||
"knowledge_title": result.KnowledgeTitle,
|
||||
"match_type": result.MatchType,
|
||||
@@ -1213,11 +1190,9 @@ 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)
|
||||
@@ -1253,36 +1228,23 @@ func (t *KnowledgeSearchTool) formatOutput(
|
||||
}
|
||||
}
|
||||
|
||||
// Add statistics and recommendations for each knowledge
|
||||
output += "\n=== Retrieval Statistics ===\n\n"
|
||||
// Retrieval statistics
|
||||
ob.WriteString("<retrieval_statistics>\n")
|
||||
for knowledgeID, retrievedChunks := range knowledgeChunkMap {
|
||||
totalChunks := knowledgeTotalMap[knowledgeID]
|
||||
retrievedCount := len(retrievedChunks)
|
||||
title := knowledgeTitleMap[knowledgeID]
|
||||
|
||||
if totalChunks > 0 {
|
||||
percentage := float64(retrievedCount) / float64(totalChunks) * 100
|
||||
remaining := totalChunks - int64(retrievedCount)
|
||||
|
||||
output += fmt.Sprintf("Document: %s (%s)\n", title, knowledgeID)
|
||||
output += fmt.Sprintf(" Total Chunks: %d\n", totalChunks)
|
||||
output += fmt.Sprintf(" Retrieved: %d (%.1f%%)\n", retrievedCount, percentage)
|
||||
output += fmt.Sprintf(" Remaining: %d\n", remaining)
|
||||
|
||||
percentage := float64(retrievedCount) / float64(totalChunks) * 100
|
||||
ob.WriteString(fmt.Sprintf("<document_stat knowledge_id=\"%s\" title=\"%s\" total_chunks=\"%d\" retrieved=\"%d\" remaining=\"%d\" coverage=\"%.1f%%\" />\n",
|
||||
knowledgeID, title, totalChunks, retrievedCount, remaining, percentage))
|
||||
}
|
||||
}
|
||||
ob.WriteString("</retrieval_statistics>\n")
|
||||
ob.WriteString("</search_results>")
|
||||
|
||||
// // Add usage guidance
|
||||
// output += "\n\n=== Usage Guidelines ===\n"
|
||||
// output += "- High relevance (>=0.8): directly usable for answering\n"
|
||||
// output += "- Medium relevance (0.6-0.8): use as supplementary reference\n"
|
||||
// output += "- Low relevance (<0.6): use with caution, may not be accurate\n"
|
||||
// if totalBeforeFilter > len(results) {
|
||||
// output += "- Results below threshold have been automatically filtered\n"
|
||||
// }
|
||||
// output += "- Full content is already included in search results above\n"
|
||||
// output += "- Results are deduplicated across knowledge bases and sorted by relevance\n"
|
||||
// output += "- Use list_knowledge_chunks to expand context if needed\n"
|
||||
output := ob.String()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"knowledge_base_ids": kbsToSearch,
|
||||
|
||||
@@ -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