兼容解析重排序服务返回score字段

This commit is contained in:
Liwx1014
2025-08-28 10:54:01 +08:00
committed by lyingbug
parent fb1cd98392
commit 19c5e242bf
2 changed files with 131 additions and 1 deletions

View File

@@ -2,9 +2,10 @@ package rerank
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/Tencent/WeKnora/internal/types"
)
@@ -25,6 +26,33 @@ type RankResult struct {
Document DocumentInfo `json:"document"`
RelevanceScore float64 `json:"relevance_score"`
}
//Handles the RelevanceScore field by checking if RelevanceScore exists first, otherwise falls back to Score field
func (r *RankResult) UnmarshalJSON(data []byte) error {
var temp struct {
Index int `json:"index"`
Document DocumentInfo `json:"document"`
RelevanceScore *float64 `json:"relevance_score"`
Score *float64 `json:"score"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return fmt.Errorf("failed to unmarshal rank result: %w", err)
}
r.Index = temp.Index
r.Document = temp.Document
if temp.RelevanceScore != nil {
r.RelevanceScore = *temp.RelevanceScore
} else if temp.Score != nil {
r.RelevanceScore = *temp.Score
}
return nil
}
type DocumentInfo struct {
Text string `json:"text"`