Skip to content

Commit b5b8278

Browse files
committed
touchups
1 parent 9eebd05 commit b5b8278

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

backend/onyx/agents/agent_search/deep_search/shared/expanded_retrieval/nodes/rerank_documents.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
from onyx.configs.agent_configs import AGENT_RERANKING_MAX_QUERY_RETRIEVAL_RESULTS
2222
from onyx.configs.agent_configs import AGENT_RERANKING_STATS
2323
from onyx.context.search.models import InferenceSection
24-
from onyx.context.search.models import SearchRequest
24+
from onyx.context.search.models import RerankingDetails
2525
from onyx.context.search.postprocessing.postprocessing import rerank_sections
26+
from onyx.context.search.postprocessing.postprocessing import reranking_is_runnable
2627

2728

2829
def rerank_documents(
@@ -48,27 +49,20 @@ def rerank_documents(
4849
graph_config.tooling.search_tool
4950
), "search_tool must be provided for agentic search"
5051

51-
search_request = SearchRequest(
52-
query=question,
53-
persona=graph_config.inputs.search_request.persona,
54-
rerank_settings=graph_config.inputs.search_request.rerank_settings,
55-
)
52+
rerank_settings = graph_config.inputs.search_request.rerank_settings
5653

57-
if (
58-
search_request.rerank_settings
59-
and search_request.rerank_settings.rerank_model_name
60-
and search_request.rerank_settings.num_rerank > 0
61-
and len(verified_documents) > 0
62-
):
54+
if reranking_is_runnable(rerank_settings) and len(verified_documents) > 0:
6355
if len(verified_documents) > 1:
6456
reranked_documents = rerank_sections(
6557
query_str=question,
66-
rerank_settings=search_request.rerank_settings,
58+
# if runnable, then rerank_settings is not None
59+
rerank_settings=cast(RerankingDetails, rerank_settings),
6760
sections_to_rerank=verified_documents,
6861
)
6962
else:
70-
num = "No" if len(verified_documents) == 0 else "One"
71-
logger.warning(f"{num} verified document(s) found, skipping reranking")
63+
logger.warning(
64+
f"{len(verified_documents)} verified document(s) found, skipping reranking"
65+
)
7266
reranked_documents = verified_documents
7367
else:
7468
logger.warning("No reranking settings found, using unranked documents")

backend/onyx/context/search/postprocessing/postprocessing.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _remove_metadata_suffix(chunk: InferenceChunkUncleaned) -> str:
7979
@log_function_time(print_only=True)
8080
def 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+
168179
def 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

Comments
 (0)