fix(quick-qa): distinguish web and knowledge base retrieval in pipeline UI
Emit search_source metadata from RAG progress events so quick-answer steps, summaries, and IM labels no longer mislabel web-only results as knowledge base search.
This commit is contained in:
@@ -3,6 +3,7 @@ package chatpipeline
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
@@ -13,6 +14,10 @@ import (
|
||||
const (
|
||||
retrievalProgressTool = "knowledge_search"
|
||||
queryUnderstandProgressTool = "query_understand"
|
||||
|
||||
retrievalSourceKnowledge = "knowledge"
|
||||
retrievalSourceWeb = "web"
|
||||
retrievalSourceMixed = "mixed"
|
||||
)
|
||||
|
||||
// StageProgress tracks an in-flight pipeline progress tool_call.
|
||||
@@ -67,7 +72,9 @@ func BeginRetrievalProgress(ctx context.Context, chatManage *types.ChatManage) *
|
||||
}
|
||||
|
||||
toolCallID := uuid.New().String()
|
||||
args := map[string]any{}
|
||||
args := map[string]any{
|
||||
"search_source": retrievalSearchSource(chatManage),
|
||||
}
|
||||
if chatManage.RewriteQuery != "" {
|
||||
args["query"] = chatManage.RewriteQuery
|
||||
} else if chatManage.Query != "" {
|
||||
@@ -164,7 +171,18 @@ func EndRetrievalProgress(
|
||||
return
|
||||
}
|
||||
|
||||
count := retrievalResultCount(chatManage)
|
||||
count, docCount, webCount := retrievalResultBreakdown(chatManage)
|
||||
searchSource := retrievalSearchSource(chatManage)
|
||||
if count > 0 {
|
||||
switch {
|
||||
case docCount > 0 && webCount > 0:
|
||||
searchSource = retrievalSourceMixed
|
||||
case webCount > 0:
|
||||
searchSource = retrievalSourceWeb
|
||||
default:
|
||||
searchSource = retrievalSourceKnowledge
|
||||
}
|
||||
}
|
||||
success := stageErr == nil || stageErr == ErrSearchNothing
|
||||
output := ""
|
||||
if success {
|
||||
@@ -191,19 +209,66 @@ func EndRetrievalProgress(
|
||||
Success: success,
|
||||
Duration: time.Since(start).Milliseconds(),
|
||||
Data: map[string]interface{}{
|
||||
"count": count,
|
||||
"count": count,
|
||||
"doc_count": docCount,
|
||||
"web_count": webCount,
|
||||
"search_source": searchSource,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func retrievalResultCount(chatManage *types.ChatManage) int {
|
||||
func hasKBRetrievalTargets(chatManage *types.ChatManage) bool {
|
||||
if chatManage == nil {
|
||||
return false
|
||||
}
|
||||
return len(chatManage.SearchTargets) > 0 ||
|
||||
len(chatManage.KnowledgeBaseIDs) > 0 ||
|
||||
len(chatManage.KnowledgeIDs) > 0
|
||||
}
|
||||
|
||||
func retrievalSearchSource(chatManage *types.ChatManage) string {
|
||||
hasKB := hasKBRetrievalTargets(chatManage)
|
||||
hasWeb := chatManage != nil && chatManage.WebSearchEnabled
|
||||
switch {
|
||||
case len(chatManage.MergeResult) > 0:
|
||||
return len(chatManage.MergeResult)
|
||||
case len(chatManage.RerankResult) > 0:
|
||||
return len(chatManage.RerankResult)
|
||||
case hasKB && hasWeb:
|
||||
return retrievalSourceMixed
|
||||
case hasWeb:
|
||||
return retrievalSourceWeb
|
||||
default:
|
||||
return len(chatManage.SearchResult)
|
||||
return retrievalSourceKnowledge
|
||||
}
|
||||
}
|
||||
|
||||
func retrievalResults(chatManage *types.ChatManage) []*types.SearchResult {
|
||||
switch {
|
||||
case len(chatManage.MergeResult) > 0:
|
||||
return chatManage.MergeResult
|
||||
case len(chatManage.RerankResult) > 0:
|
||||
return chatManage.RerankResult
|
||||
default:
|
||||
return chatManage.SearchResult
|
||||
}
|
||||
}
|
||||
|
||||
func retrievalResultBreakdown(chatManage *types.ChatManage) (total, docCount, webCount int) {
|
||||
for _, result := range retrievalResults(chatManage) {
|
||||
if result == nil {
|
||||
continue
|
||||
}
|
||||
total++
|
||||
if isWebSearchResult(result) {
|
||||
webCount++
|
||||
} else {
|
||||
docCount++
|
||||
}
|
||||
}
|
||||
return total, docCount, webCount
|
||||
}
|
||||
|
||||
func isWebSearchResult(result *types.SearchResult) bool {
|
||||
if strings.EqualFold(result.ChunkType, "web_search") {
|
||||
return true
|
||||
}
|
||||
return strings.EqualFold(result.KnowledgeSource, "web_search")
|
||||
}
|
||||
|
||||
@@ -95,9 +95,46 @@ func TestRetrievalProgressEmitsSingleToolCallAndResult(t *testing.T) {
|
||||
callData, ok := bus.events[0].Data.(event.AgentToolCallData)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "knowledge_search", callData.ToolName)
|
||||
assert.Equal(t, retrievalSourceKnowledge, callData.Arguments["search_source"])
|
||||
|
||||
resultData, ok := bus.events[1].Data.(event.AgentToolResultData)
|
||||
require.True(t, ok)
|
||||
assert.True(t, resultData.Success)
|
||||
assert.Equal(t, 3, resultData.Data["count"])
|
||||
assert.Equal(t, 3, resultData.Data["doc_count"])
|
||||
assert.Equal(t, 0, resultData.Data["web_count"])
|
||||
assert.Equal(t, retrievalSourceKnowledge, resultData.Data["search_source"])
|
||||
}
|
||||
|
||||
func TestRetrievalProgressWebOnlySearchSource(t *testing.T) {
|
||||
bus := &recordingEventBus{}
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{
|
||||
SessionID: "sess-web",
|
||||
WebSearchEnabled: true,
|
||||
},
|
||||
PipelineContext: types.PipelineContext{EventBus: bus},
|
||||
PipelineState: types.PipelineState{
|
||||
MergeResult: []*types.SearchResult{
|
||||
{ID: "w1", ChunkType: "web_search"},
|
||||
{ID: "w2", KnowledgeSource: "web_search"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
progress := BeginRetrievalProgress(context.Background(), cm)
|
||||
require.NotNil(t, progress)
|
||||
EndRetrievalProgress(context.Background(), cm, progress, start, nil)
|
||||
|
||||
callData, ok := bus.events[0].Data.(event.AgentToolCallData)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, retrievalSourceWeb, callData.Arguments["search_source"])
|
||||
|
||||
resultData, ok := bus.events[1].Data.(event.AgentToolResultData)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, 2, resultData.Data["count"])
|
||||
assert.Equal(t, 0, resultData.Data["doc_count"])
|
||||
assert.Equal(t, 2, resultData.Data["web_count"])
|
||||
assert.Equal(t, retrievalSourceWeb, resultData.Data["search_source"])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user