-
Notifications
You must be signed in to change notification settings - Fork 80
feat: implement rerank step in collection search API #1162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement rerank step in collection search API #1162
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, @Shushankranjan
You should use RerankNodeRunner.
You can find it in aperag/flow
package, take rag_flow2.yaml
as an example
Sure , i will change it to reranknoderunner |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- After finding the model, you need to verify if it has an API key configured. It's highly possible that an LLM provider might have tagged models without a configured API key.
- You shouldn't create a separate
FlowInstance
just to run the rerank node. Instead, you should add the rerank node and its edges to the existingFlowInstance
above.
aperag/service/collection_service.py
Outdated
# Re-execute the flow with the added rerank node | ||
result, _ = await engine.execute_flow(flow, {"query": query, "user": user}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't re-execute the flow. Instead, you should check if there's a rerank node; if so, include it in the execution. If there's no rerank node, then use the default rerank strategy.
aperag/service/collection_service.py
Outdated
else: | ||
# Log warning that the model doesn't have an API key configured | ||
import logging | ||
logger = logging.getLogger(__name__) | ||
logger.warning(f"Rerank model {rerank_model['model']} with provider {rerank_model['provider']} has no API key configured. Skipping reranking.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's no API key, the fallback rerank strategy should be used.
flow.nodes[rerank_node_id] = NodeInstance( | ||
id=rerank_node_id, | ||
type="rerank", | ||
input_values={ | ||
"model": rerank_model["model"], | ||
"model_service_provider": rerank_model["provider"], | ||
"custom_llm_provider": rerank_model.get("custom_llm_provider", rerank_model["provider"]), | ||
"docs": docs | ||
} | ||
) | ||
|
||
# Add an edge from the merge node to the rerank node | ||
flow.edges.append(Edge(source=end_node_id, target=rerank_node_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rerank node shouldn't just be added; it should form a DAG after the merge node. Our flow process has actually implemented the combination of search + rerank; you can refer to rag_flow2.yaml.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Shushankranjan Thanks for your contribution, but this implementation has several critical issues that need to be addressed:
1. Fundamental Logic Error - Duplicate DAG Execution
Your current approach executes the entire DAG twice:
- First execution:
vector_search → fulltext_search → graph_search → merge
- Then you check for rerank models and re-execute the entire flow again
This is incorrect and wasteful. The correct approach should be:
- Check rerank availability before building the flow
- Build the appropriate flow structure once (with or without rerank node)
- Execute
engine.execute_flow()
only once
2. Code Quality Issues
- Duplicate Code: The fallback sorting logic is repeated 3 times (no API key, no rerank model, exception handling)
- Excessive Nesting: Deep if-else nesting makes the code hard to maintain
if docs:
if rerank_model:
if api_key:
try:
# rerank logic
except:
# fallback logic (duplicate 1)
else:
# fallback logic (duplicate 2)
else:
# fallback logic (duplicate 3)
3. Correct Implementation Direction
The flow should be:
- Check rerank model availability and API key upfront
- Build flow structure based on availability:
- With rerank:
search_nodes → merge → rerank
- Without rerank:
search_nodes → merge
(with fallback sorting in merge node)
- With rerank:
- Execute flow once and get results from the final node
Please refactor to eliminate duplicate execution, extract the fallback logic into a separate method, and reduce nesting complexity.
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment |
Issue - [#1158]
Enhanced the create_search method in collection_service.py:
Implemented the _get_default_rerank_model method:
Added the fallback strategy when no rerank model is available:
The implementation leverages the existing rerank functionality from the flow system, ensuring consistency across
the application. When a search is performed, the system will now:
This enhancement will significantly improve search result quality by leveraging the existing rerank
infrastructure in the direct search API.
Closes #1158