fix: inject KB document listing on fallback for broad queries (#959)
When users ask broad queries like "请整理知识库中的数据" in RAG mode,
vector/keyword search returns nothing because the query has no specific
content to match. The user needs to see what documents exist, not search
results.
- Add buildKBDocumentListing() to fetch document titles/filenames from
the knowledge base and inject them into the fallback prompt via
{{kb_documents}} placeholder
- Update fallback prompt templates (model_fallback, default_fallback_prompt)
to include document listing context so the LLM can guide users
- Improve rewrite prompt: tighten summarize vs kb_search classification,
expand kb_search to cover browse/organize/list operations, add examples
Closes #959
This commit is contained in:
@@ -767,9 +767,13 @@ func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *t
|
||||
if rq := strings.TrimSpace(chatManage.RewriteQuery); rq != "" {
|
||||
query = rq
|
||||
}
|
||||
|
||||
kbDocuments := s.buildKBDocumentListing(ctx, chatManage)
|
||||
|
||||
result := types.RenderPromptPlaceholders(chatManage.FallbackPrompt, types.PlaceholderValues{
|
||||
"query": query,
|
||||
"language": chatManage.Language,
|
||||
"query": query,
|
||||
"language": chatManage.Language,
|
||||
"kb_documents": kbDocuments,
|
||||
})
|
||||
|
||||
if chatManage.ImageDescription != "" && !chatManage.ChatModelSupportsVision {
|
||||
@@ -781,6 +785,76 @@ func (s *sessionService) renderFallbackPrompt(ctx context.Context, chatManage *t
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// buildKBDocumentListing returns a concise listing of documents in the knowledge bases
|
||||
// associated with the current pipeline. This gives the LLM visibility into KB contents
|
||||
// when vector/keyword search returns empty (e.g., broad browse queries).
|
||||
func (s *sessionService) buildKBDocumentListing(ctx context.Context, chatManage *types.ChatManage) string {
|
||||
// Collect unique KB IDs from search targets
|
||||
kbIDs := make(map[string]struct{})
|
||||
for _, t := range chatManage.SearchTargets {
|
||||
kbIDs[t.KnowledgeBaseID] = struct{}{}
|
||||
}
|
||||
for _, id := range chatManage.KnowledgeBaseIDs {
|
||||
kbIDs[id] = struct{}{}
|
||||
}
|
||||
if len(kbIDs) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
const maxDocuments = 50
|
||||
var b strings.Builder
|
||||
total := 0
|
||||
|
||||
for kbID := range kbIDs {
|
||||
if total >= maxDocuments {
|
||||
break
|
||||
}
|
||||
knowledges, err := s.knowledgeService.ListKnowledgeByKnowledgeBaseID(ctx, kbID)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "buildKBDocumentListing: failed to list knowledge for KB %s: %v", kbID, err)
|
||||
continue
|
||||
}
|
||||
for _, k := range knowledges {
|
||||
if total >= maxDocuments {
|
||||
break
|
||||
}
|
||||
if k.EnableStatus != "enabled" {
|
||||
continue
|
||||
}
|
||||
title := k.Title
|
||||
if title == "" {
|
||||
title = k.FileName
|
||||
}
|
||||
if title == "" {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s", title)
|
||||
if k.FileType != "" {
|
||||
fmt.Fprintf(&b, " (%s)", k.FileType)
|
||||
}
|
||||
if k.Description != "" {
|
||||
desc := k.Description
|
||||
if len([]rune(desc)) > 100 {
|
||||
desc = string([]rune(desc)[:100]) + "..."
|
||||
}
|
||||
fmt.Fprintf(&b, ": %s", desc)
|
||||
}
|
||||
b.WriteString("\n")
|
||||
total++
|
||||
}
|
||||
}
|
||||
|
||||
if b.Len() == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if total >= maxDocuments {
|
||||
fmt.Fprintf(&b, "... (showing first %d documents)\n", maxDocuments)
|
||||
}
|
||||
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// consumeFallbackStream consumes the streaming response and emits events
|
||||
func (s *sessionService) consumeFallbackStream(
|
||||
ctx context.Context,
|
||||
|
||||
Reference in New Issue
Block a user