From 29c52b0342ff33b0d1b0ec32b51caa31a106dd54 Mon Sep 17 00:00:00 2001 From: lazyayuan <2219014504@qq.com> Date: Mon, 27 Jul 2026 21:59:54 +0100 Subject: [PATCH] fix(evaluation): fix retrieval metrics by mapping chunks back to passage IDs Two issues caused retrieval metrics (precision, recall, NDCG, MRR, MAP) to always be 0: 1. recordFinish compared SearchResult.ChunkIndex (the chunk's ordinal position in the knowledge base) against QAPair.PIDs (the dataset's original passage IDs). These are different ID spaces that never match. Fix: use content-based matching to map each retrieved chunk back to its source passage, with deduplication for multi-chunk passages. 2. getPassageList had an off-by-one error: make([]string, maxPID) with loop i < maxPID dropped the passage with the highest PID. Fix: use maxPID+1 and i <= maxPID. Also adds fallback from rerankResult to searchResult when no rerank model is configured, matching the pattern in the working copy. --- internal/application/service/evaluation.go | 4 +-- internal/application/service/metric_hook.go | 40 +++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/internal/application/service/evaluation.go b/internal/application/service/evaluation.go index 80b9814b..184613f2 100644 --- a/internal/application/service/evaluation.go +++ b/internal/application/service/evaluation.go @@ -466,8 +466,8 @@ func getPassageList(dataset []*types.QAPair) []string { maxPID = max(maxPID, qaPair.PIDs[i]) } } - passages := make([]string, maxPID) - for i := 0; i < maxPID; i++ { + passages := make([]string, maxPID+1) + for i := 0; i <= maxPID; i++ { if _, ok := pIDMap[i]; ok { passages[i] = pIDMap[i] } diff --git a/internal/application/service/metric_hook.go b/internal/application/service/metric_hook.go index 88b1f32f..f63d04f8 100644 --- a/internal/application/service/metric_hook.go +++ b/internal/application/service/metric_hook.go @@ -2,6 +2,7 @@ package service import ( "context" + "strings" "sync" "github.com/Tencent/WeKnora/internal/application/service/metric" @@ -132,10 +133,37 @@ func (h *HookMetric) recordChatResponse(index int, chatResponse *types.ChatRespo // recordFinish finalizes metrics for a QA pair func (h *HookMetric) recordFinish(index int) { - // Prepare retrieval IDs from rerank results - retrievalIDs := make([]int, len(h.qaPairMetricList[index].rerankResult)) - for i, r := range h.qaPairMetricList[index].rerankResult { - retrievalIDs[i] = r.ChunkIndex + // Prepare retrieval source: prefer rerank results, fall back to search results + retrievalSource := h.qaPairMetricList[index].rerankResult + if len(retrievalSource) == 0 { + retrievalSource = h.qaPairMetricList[index].searchResult + } + + // Map retrieved chunks back to original passage IDs via content matching. + // ChunkIndex is the chunk's ordinal position in the knowledge base, which + // does NOT correspond to the dataset's passage IDs. Instead, we match each + // retrieved chunk's content against the ground truth passages to determine + // which passage it came from. + qaPair := h.qaPairMetricList[index].qaPair + retrievalIDs := make([]int, 0, len(retrievalSource)) + seen := make(map[int]struct{}) + for _, r := range retrievalSource { + if r.Content == "" { + continue + } + for i, passage := range qaPair.Passages { + if passage == "" { + continue + } + if strings.Contains(passage, r.Content) || strings.Contains(r.Content, passage) { + pid := qaPair.PIDs[i] + if _, ok := seen[pid]; !ok { + seen[pid] = struct{}{} + retrievalIDs = append(retrievalIDs, pid) + } + break + } + } } // Get generated text if available @@ -146,10 +174,10 @@ func (h *HookMetric) recordFinish(index int) { // Prepare metric input data metricInput := &types.MetricInput{ - RetrievalGT: [][]int{h.qaPairMetricList[index].qaPair.PIDs}, + RetrievalGT: [][]int{qaPair.PIDs}, RetrievalIDs: retrievalIDs, GeneratedTexts: generatedTexts, - GeneratedGT: h.qaPairMetricList[index].qaPair.Answer, + GeneratedGT: qaPair.Answer, } // Thread-safe append of metrics