mirror of
https://github.com/Tencent/WeKnora.git
synced 2025-11-25 03:15:00 +08:00
兼容解析重排序服务返回score字段
This commit is contained in:
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user