@@ -113,22 +113,17 @@ def rank_document(
113113 """
114114 if not self ._loaded or self .model is None :
115115 self .load ()
116-
116+
117117 try :
118- ranking_results = self .model .rank (
119- query ,
120- documents ,
121- top_k = top_k ,
122- ** kwargs
123- )
124-
118+ ranking_results = self .model .rank (query , documents , top_k = top_k , ** kwargs )
119+
125120 # Normalize scores to 0-1 range for consistency
126121 normalized_results = self ._normalize_rerank_scores (ranking_results )
127-
122+
128123 logger .debug (
129124 f"Reranked { len (documents )} docs, returned top { len (normalized_results )} "
130125 )
131-
126+
132127 return normalized_results
133128
134129 except Exception as e :
@@ -137,9 +132,7 @@ def rank_document(
137132 raise RerankingDocumentError (self .model_id , error_msg )
138133
139134 def _normalize_rerank_scores (
140- self ,
141- rankings : List [Dict ],
142- target_range : tuple = (0 , 1 )
135+ self , rankings : List [Dict ], target_range : tuple = (0 , 1 )
143136 ) -> List [Dict ]:
144137 """
145138 Normalize reranking scores using min-max normalization.
@@ -154,35 +147,30 @@ def _normalize_rerank_scores(
154147 """
155148 if not rankings :
156149 return []
157-
150+
158151 raw_scores = [ranking ["score" ] for ranking in rankings ]
159-
152+
160153 min_score = min (raw_scores )
161154 max_score = max (raw_scores )
162-
155+
163156 if max_score == min_score :
164157 return [
165- {
166- "corpus_id" : r ["corpus_id" ],
167- "score" : target_range [1 ]
168- }
158+ {"corpus_id" : r ["corpus_id" ], "score" : target_range [1 ]}
169159 for r in rankings
170160 ]
171-
161+
172162 target_min , target_max = target_range
173163 normalized_rankings = []
174-
164+
175165 for ranking in rankings :
176166 score = ranking ["score" ]
177- normalized_score = (
178- target_min +
179- (score - min_score ) * (target_max - target_min ) / (max_score - min_score )
167+ normalized_score = target_min + (score - min_score ) * (
168+ target_max - target_min
169+ ) / (max_score - min_score )
170+ normalized_rankings .append (
171+ {"corpus_id" : ranking ["corpus_id" ], "score" : float (normalized_score )}
180172 )
181- normalized_rankings .append ({
182- "corpus_id" : ranking ["corpus_id" ],
183- "score" : float (normalized_score )
184- })
185-
173+
186174 return normalized_rankings
187175
188176 @property
@@ -222,4 +210,4 @@ def __repr__(self) -> str:
222210 f"id={ self .model_id } , "
223211 f"type={ self .model_type } , "
224212 f"loaded={ self .is_loaded } )"
225- )
213+ )
0 commit comments