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.main
parent
9e6fee3620
commit
29c52b0342
|
|
@ -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]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue