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"])
|
||||
}
|
||||
|
||||
@@ -270,16 +270,27 @@ func FormatIMRagPipelineLine(step IMToolStep) string {
|
||||
}
|
||||
return "已完成问题理解"
|
||||
case "knowledge_search", "search_knowledge":
|
||||
source := imRetrievalSearchSource(step)
|
||||
if step.Pending {
|
||||
if query != "" {
|
||||
return fmt.Sprintf("正在检索知识库:「%s」", query)
|
||||
switch source {
|
||||
case imRetrievalSourceWeb:
|
||||
if query != "" {
|
||||
return fmt.Sprintf("正在检索网络:「%s」", query)
|
||||
}
|
||||
return "正在检索网络..."
|
||||
case imRetrievalSourceMixed:
|
||||
if query != "" {
|
||||
return fmt.Sprintf("正在检索知识库和网络:「%s」", query)
|
||||
}
|
||||
return "正在检索知识库和网络..."
|
||||
default:
|
||||
if query != "" {
|
||||
return fmt.Sprintf("正在检索知识库:「%s」", query)
|
||||
}
|
||||
return "正在检索知识库..."
|
||||
}
|
||||
return "正在检索知识库..."
|
||||
}
|
||||
base := "检索知识库"
|
||||
if !step.Success {
|
||||
base = "检索知识库失败"
|
||||
}
|
||||
base := imRetrievalDoneTitle(source, step.Success)
|
||||
line := imAppendQueryTitle(base, query)
|
||||
if summary := imKnowledgeSearchSummary(step.Data); summary != "" {
|
||||
return line + " · " + summary
|
||||
@@ -464,6 +475,70 @@ func imToolResultSummary(step IMToolStep) string {
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
imRetrievalSourceKnowledge = "knowledge"
|
||||
imRetrievalSourceWeb = "web"
|
||||
imRetrievalSourceMixed = "mixed"
|
||||
)
|
||||
|
||||
func imRetrievalSearchSource(step IMToolStep) string {
|
||||
if source := imSearchSourceFromData(step.Data); source != "" {
|
||||
return source
|
||||
}
|
||||
if step.Arguments != nil {
|
||||
if source, ok := step.Arguments["search_source"].(string); ok && source != "" {
|
||||
return source
|
||||
}
|
||||
}
|
||||
return imRetrievalSourceKnowledge
|
||||
}
|
||||
|
||||
func imSearchSourceFromData(data map[string]interface{}) string {
|
||||
if data == nil {
|
||||
return ""
|
||||
}
|
||||
if source, ok := data["search_source"].(string); ok {
|
||||
return strings.TrimSpace(source)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func imRetrievalDoneTitle(source string, success bool) string {
|
||||
switch source {
|
||||
case imRetrievalSourceWeb:
|
||||
if success {
|
||||
return "网络检索"
|
||||
}
|
||||
return "网络检索失败"
|
||||
case imRetrievalSourceMixed:
|
||||
if success {
|
||||
return "检索知识库和网络"
|
||||
}
|
||||
return "检索失败"
|
||||
default:
|
||||
if success {
|
||||
return "检索知识库"
|
||||
}
|
||||
return "检索知识库失败"
|
||||
}
|
||||
}
|
||||
|
||||
func imIntField(data map[string]interface{}, key string) int {
|
||||
if data == nil {
|
||||
return 0
|
||||
}
|
||||
switch v := data[key].(type) {
|
||||
case int:
|
||||
return v
|
||||
case int64:
|
||||
return int(v)
|
||||
case float64:
|
||||
return int(v)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func imKnowledgeSearchSummary(data map[string]interface{}) string {
|
||||
if data == nil {
|
||||
return ""
|
||||
@@ -472,9 +547,18 @@ func imKnowledgeSearchSummary(data map[string]interface{}) string {
|
||||
if count == 0 {
|
||||
return "未找到匹配的内容"
|
||||
}
|
||||
source := imSearchSourceFromData(data)
|
||||
webCount := imIntField(data, "web_count")
|
||||
docCount := imIntField(data, "doc_count")
|
||||
if source == imRetrievalSourceWeb || (webCount > 0 && docCount == 0) {
|
||||
return fmt.Sprintf("找到 %d 条网页", count)
|
||||
}
|
||||
if kbCounts, ok := data["kb_counts"].(map[string]interface{}); ok && len(kbCounts) > 0 {
|
||||
return fmt.Sprintf("找到 %d 个结果,来自 %d 个文件", count, len(kbCounts))
|
||||
}
|
||||
if source == imRetrievalSourceMixed && docCount > 0 && webCount > 0 {
|
||||
return fmt.Sprintf("找到 %d 个结果(%d 篇文档,%d 条网页)", count, docCount, webCount)
|
||||
}
|
||||
return fmt.Sprintf("找到 %d 个结果", count)
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,17 @@ func TestFormatIMRagPipelineLine_searchWithQuery(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatIMRagPipelineLine_webSearchWithQuery(t *testing.T) {
|
||||
line := FormatIMRagPipelineLine(IMToolStep{
|
||||
ToolName: "knowledge_search",
|
||||
Pending: true,
|
||||
Arguments: map[string]any{"query": "任素汐演唱会", "search_source": "web"},
|
||||
})
|
||||
if line != "正在检索网络:「任素汐演唱会」" {
|
||||
t.Fatalf("line = %q", line)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIMGetQueryText_joinsUniqueQueries(t *testing.T) {
|
||||
got := imGetQueryText(map[string]any{
|
||||
"query": "foo",
|
||||
|
||||
Reference in New Issue
Block a user