@@ -79,7 +79,7 @@ def _remove_metadata_suffix(chunk: InferenceChunkUncleaned) -> str:
7979@log_function_time (print_only = True )
8080def semantic_reranking (
8181 query_str : str ,
82- rerank_settings : RerankingDetails | None ,
82+ rerank_settings : RerankingDetails ,
8383 chunks : list [InferenceChunk ],
8484 model_min : int = CROSS_ENCODER_RANGE_MIN ,
8585 model_max : int = CROSS_ENCODER_RANGE_MAX ,
@@ -90,9 +90,9 @@ def semantic_reranking(
9090
9191 Note: this updates the chunks in place, it updates the chunk scores which came from retrieval
9292 """
93- if not rerank_settings or not rerank_settings . rerank_model_name :
94- # Should never reach this part of the flow without reranking settings
95- raise RuntimeError ( "Reranking flow should not be running" )
93+ assert (
94+ rerank_settings . rerank_model_name
95+ ), "Reranking flow cannot run without a specific model"
9696
9797 chunks_to_rerank = chunks [: rerank_settings .num_rerank ]
9898
@@ -165,9 +165,20 @@ def semantic_reranking(
165165 return list (ranked_chunks ), list (ranked_indices )
166166
167167
168+ def reranking_is_runnable (rerank_settings : RerankingDetails | None ) -> bool :
169+ """Based on the RerankingDetails model, only run rerank if the following conditions are met:
170+ - rerank_model_name is not None
171+ - num_rerank is greater than 0
172+ """
173+ if not rerank_settings :
174+ return False
175+
176+ return bool (rerank_settings .rerank_model_name and rerank_settings .num_rerank > 0 )
177+
178+
168179def rerank_sections (
169180 query_str : str ,
170- rerank_settings : RerankingDetails | None ,
181+ rerank_settings : RerankingDetails ,
171182 sections_to_rerank : list [InferenceSection ],
172183 rerank_metrics_callback : Callable [[RerankMetricsContainer ], None ] | None = None ,
173184) -> list [InferenceSection ]:
@@ -182,10 +193,6 @@ def rerank_sections(
182193 """
183194 chunks_to_rerank = [section .center_chunk for section in sections_to_rerank ]
184195
185- if not rerank_settings :
186- # Should never reach this part of the flow without reranking settings
187- raise RuntimeError ("Reranking settings not found" )
188-
189196 ranked_chunks , _ = semantic_reranking (
190197 query_str = query_str ,
191198 rerank_settings = rerank_settings ,
@@ -262,17 +269,13 @@ def search_postprocessing(
262269
263270 rerank_task_id = None
264271 sections_yielded = False
265- if (
266- search_query .rerank_settings
267- and search_query .rerank_settings .rerank_model_name
268- and search_query .rerank_settings .num_rerank > 0
269- ):
272+ if reranking_is_runnable (search_query .rerank_settings ):
270273 post_processing_tasks .append (
271274 FunctionCall (
272275 rerank_sections ,
273276 (
274277 search_query .query ,
275- search_query .rerank_settings ,
278+ search_query .rerank_settings , # Cannot be None here
276279 retrieved_sections ,
277280 rerank_metrics_callback ,
278281 ),
0 commit comments