-
Notifications
You must be signed in to change notification settings - Fork 82
LCORE-1723: Cross-Encoder Reranking for enhanced RAG #1566
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1812,6 +1812,29 @@ class OkpConfiguration(ConfigurationBase): | |
| ) | ||
|
|
||
|
|
||
| class RerankerConfiguration(ConfigurationBase): | ||
| """Reranker configuration for RAG chunk reranking.""" | ||
|
|
||
| enabled: bool = True | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: can be initialized as |
||
| model: str = Field( | ||
| default="cross-encoder/ms-marco-MiniLM-L6-v2", | ||
| title="Reranker model", | ||
| description="Cross-encoder model name for reranking RAG chunks. " | ||
| "Defaults to 'cross-encoder/ms-marco-MiniLM-L6-v2' from sentence-transformers.", | ||
| ) | ||
|
|
||
| # Private attribute to track if this was explicitly configured | ||
| _explicitly_configured: bool = PrivateAttr(default=False) | ||
|
|
||
| @model_validator(mode="after") | ||
| def mark_as_explicitly_configured(self) -> Self: | ||
| """Mark this configuration as explicitly set when instantiated from user input.""" | ||
| if self.model_fields_set: | ||
| self._explicitly_configured = True | ||
|
|
||
| return self | ||
|
Anxhela21 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| class AzureEntraIdConfiguration(ConfigurationBase): | ||
| """Microsoft Entra ID authentication attributes for Azure.""" | ||
|
|
||
|
|
@@ -1970,6 +1993,12 @@ class Configuration(ConfigurationBase): | |
| "in rag.inline or rag.tool.", | ||
| ) | ||
|
|
||
| reranker: RerankerConfiguration = Field( | ||
| default_factory=RerankerConfiguration, | ||
| title="Reranker configuration", | ||
| description="Configuration for neural reranking of RAG chunks using cross-encoder.", | ||
| ) | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_mcp_auth_headers(self) -> Self: | ||
| """ | ||
|
|
@@ -2072,6 +2101,46 @@ def validate_rlsapi_v1_quota_configuration(self) -> Self: | |
|
|
||
| return self | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_reranker_auto_enable(self) -> Self: | ||
| """Automatically enable reranker when both BYOK and OKP RAG are configured. | ||
|
|
||
| When users have both BYOK (Bring Your Own Key) entries in byok_rag and OKP | ||
| (OpenShift Knowledge Platform) configured in the RAG strategies, automatically | ||
| enable the reranker if it's not explicitly disabled. This improves result | ||
| quality when multiple knowledge sources are available. | ||
|
|
||
| Returns: | ||
| Self: The validated configuration instance with reranker potentially enabled. | ||
| """ | ||
| # Check if BYOK RAG entries are configured | ||
| has_byok = len(self.byok_rag) > 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also check that there are any entries registered in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh good point! |
||
|
|
||
| # Check if OKP is configured in either inline or tool RAG strategies | ||
| # pylint: disable=no-member | ||
| has_okp = ( | ||
| constants.OKP_RAG_ID in self.rag.inline | ||
| or constants.OKP_RAG_ID in self.rag.tool | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should the re-ranked be active if OKP is in RAG as a Tool?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is looking if okp is enabled either as inline or tool since we can configure okp either way unless I'm mistaken? |
||
| ) | ||
|
|
||
| # If both BYOK and OKP are present and reranker is using default settings, | ||
| # ensure it's enabled for optimal results | ||
| if ( | ||
| has_byok | ||
| and has_okp | ||
| and not self.reranker.enabled | ||
| ): | ||
|
|
||
| logger.info( | ||
| "Automatically enabling reranker: Both BYOK RAG (%d entries) and OKP " | ||
| "are configured. Reranking improves result quality when multiple " | ||
| "knowledge sources are available.", | ||
| len(self.byok_rag), | ||
| ) | ||
| self.reranker.enabled = True | ||
|
|
||
| return self | ||
|
|
||
| def dump(self, filename: str | Path = "configuration.json") -> None: | ||
| """ | ||
| Write the current Configuration model to a JSON file. | ||
|
|
||
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.
Should the
sentence-transformerdependency be added @syedriko @tisnik? In that case uv.lock will need to be updated tooThere 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.
yes, it needs to be updated to pass CI etc.
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.
thanks missed that - will update uv.lock