Rerankers#
ragbits.document_search.retrieval.rerankers.base.Reranker
#
Bases: ConfigurableComponent[RerankerOptionsT]
, ABC
Reranks elements retrieved from vector store.
Constructs a new ConfigurableComponent instance.
PARAMETER | DESCRIPTION |
---|---|
default_options |
The default options for the component.
TYPE:
|
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initializes the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
rerank
abstractmethod
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: RerankerOptionsT | None = None) -> Sequence[Element]
Rerank elements.
PARAMETER | DESCRIPTION |
---|---|
elements |
The elements to rerank.
TYPE:
|
query |
The query to rerank the elements against.
TYPE:
|
options |
The options for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/base.py
ragbits.document_search.retrieval.rerankers.answerai.AnswerAIReranker
#
AnswerAIReranker(model: str, default_options: RerankerOptions | None = None, **rerankers_kwargs: Any)
Bases: Reranker[RerankerOptions]
A rerankers re-ranker covering most popular re-ranking methods.
Constructs a new AnswerDotAIRerankersReranker instance.
PARAMETER | DESCRIPTION |
---|---|
model |
The reranker model to use.
TYPE:
|
default_options |
The default options for reranking.
TYPE:
|
**rerankers_kwargs |
Additional keyword arguments native to rerankers lib.
TYPE:
|
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/answerai.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initializes the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
rerank
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: RerankerOptions | None = None) -> Sequence[Element]
Rerank elements.
PARAMETER | DESCRIPTION |
---|---|
elements |
The elements to rerank.
TYPE:
|
query |
The query to rerank the elements against.
TYPE:
|
options |
The options for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raised if the input query is empty or if the list of candidate documents is empty. |
TypeError
|
Raised if the input types are incorrect, such as if the query is not a string, or List[str]. |
IndexError
|
Raised if docs is an empty List. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/answerai.py
ragbits.document_search.retrieval.rerankers.litellm.LiteLLMReranker
#
LiteLLMReranker(model: str, default_options: LiteLLMRerankerOptions | None = None)
Bases: Reranker[LiteLLMRerankerOptions]
A LiteLLM reranker for providers such as Cohere, Together AI, Azure AI.
Constructs a new LiteLLMReranker instance.
PARAMETER | DESCRIPTION |
---|---|
model |
The reranker model to use.
TYPE:
|
default_options |
The default options for reranking.
TYPE:
|
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/litellm.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initializes the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
rerank
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: LiteLLMRerankerOptions | None = None) -> Sequence[Element]
Rerank elements with LiteLLM API.
PARAMETER | DESCRIPTION |
---|---|
elements |
The elements to rerank.
TYPE:
|
query |
The query to rerank the elements against.
TYPE:
|
options |
The options for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/litellm.py
ragbits.document_search.retrieval.rerankers.llm.LLMReranker
#
LLMReranker(llm: LiteLLM, *, prompt: type[Prompt[RerankerInput, str]] | None = None, llm_options: LiteLLMOptions | None = None, default_options: RerankerOptions | None = None)
Bases: Reranker[RerankerOptions]
Reranker based on LLM.
Initialize the LLMReranker instance.
PARAMETER | DESCRIPTION |
---|---|
llm |
The LLM instance to handle reranking.
TYPE:
|
prompt |
The prompt to use for reranking elements.
TYPE:
|
llm_options |
The LLM options to override.
TYPE:
|
default_options |
The default options for reranking.
TYPE:
|
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/llm.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initialize the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The initialized instance of LLMReranker. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If the configuration doesn't follow the expected format. |
InvalidConfigError
|
If llm or prompt can't be found or are not the correct type. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/llm.py
rerank
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: RerankerOptions | None = None) -> Sequence[Element]
Rerank elements with LLM.
PARAMETER | DESCRIPTION |
---|---|
elements |
The elements to rerank.
TYPE:
|
query |
The query to rerank the elements against.
TYPE:
|
options |
The RerankerOptions to use for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/llm.py
ragbits.document_search.retrieval.rerankers.noop.NoopReranker
#
Bases: Reranker[RerankerOptions]
A no-op reranker that does not change the order of the elements.
Constructs a new ConfigurableComponent instance.
PARAMETER | DESCRIPTION |
---|---|
default_options |
The default options for the component.
TYPE:
|
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initializes the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
rerank
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: RerankerOptions | None = None) -> Sequence[Element]
No reranking, returning the elements in the same order.
PARAMETER | DESCRIPTION |
---|---|
elements |
The elements to rerank.
TYPE:
|
query |
The query to rerank the elements against.
TYPE:
|
options |
The options for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/noop.py
ragbits.document_search.retrieval.rerankers.rrf.ReciprocalRankFusionReranker
#
Bases: Reranker[RerankerOptions]
A reranker that implements the Reciprocal Rank Fusion (RRF) algorithm to combine multiple ranked result sets into a single reranked list.
RRF is a method that assigns scores to documents based on their positions in multiple ranked lists, allowing for fusion of diverse ranking sources without the need for tuning.
The score for each document is calculated using the formula:
score = sum(1.0 / (k + rank(q, d)))
where
- k is a ranking constant (1 is used here)
- q is a query in the set of queries
- d is a document in the result set
- rank(q, d) is the position of d in the ranking list for q (starting from 1)
Constructs a new ConfigurableComponent instance.
PARAMETER | DESCRIPTION |
---|---|
default_options |
The default options for the component.
TYPE:
|
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_config
classmethod
#
Initializes the class with the provided configuration. May return a subclass of the class, if requested by the configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A model containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The class can't be found or is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
subclass_from_factory
classmethod
#
Creates the class using the provided factory function. May return a subclass of the class, if requested by the factory.
PARAMETER | DESCRIPTION |
---|---|
factory_path |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided factory function. |
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
The factory can't be found or the object returned is not a subclass of the current class. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
preferred_subclass
classmethod
#
preferred_subclass(config: CoreConfig, factory_path_override: str | None = None, yaml_path_override: Path | None = None) -> Self
Tries to create an instance by looking at project's component preferences, either from YAML or from the factory. Takes optional overrides for both, which takes a higher precedence.
PARAMETER | DESCRIPTION |
---|---|
config |
The CoreConfig instance containing preferred factory and configuration details.
TYPE:
|
factory_path_override |
A string representing the path to the factory function in the format of "module.submodule:factory_name".
TYPE:
|
yaml_path_override |
A string representing the path to the YAML file containing the Ragstack instance configuration.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
InvalidConfigError
|
If the default factory or configuration can't be found. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
from_config
classmethod
#
Initializes the class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
config |
A dictionary containing configuration details for the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
An instance of the class initialized with the provided configuration. |
Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
rerank
async
#
rerank(elements: Sequence[Sequence[Element]], query: str, options: RerankerOptions | None = None) -> Sequence[Element]
Reranks elements using the Reciprocal Rank Fusion (RRF) algorithm.
PARAMETER | DESCRIPTION |
---|---|
elements |
A list of ranked lists of elements to be fused.
TYPE:
|
query |
The query string for reranking.
TYPE:
|
options |
Options for reranking.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Sequence[Element]
|
The reranked elements. |
Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/rrf.py
ragbits.document_search.retrieval.rerankers.base.RerankerOptions
#
Bases: Options
An object representing the options for the reranker.
ATTRIBUTE | DESCRIPTION |
---|---|
top_n |
The number of entries to return.
TYPE:
|
score_threshold |
The minimum relevance score for an entry to be returned.
TYPE:
|
override_score |
If True reranking will override element score.
TYPE:
|
model_config
class-attribute
instance-attribute
#
dict
#
dict() -> dict[str, Any]
Creates a dictionary representation of the Options instance. If a value is None, it will be replaced with a provider-specific not-given sentinel.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
A dictionary representation of the Options instance. |
Source code in packages/ragbits-core/src/ragbits/core/options.py
ragbits.document_search.retrieval.rerankers.litellm.LiteLLMRerankerOptions
#
Bases: RerankerOptions
An object representing the options for the litellm reranker.
ATTRIBUTE | DESCRIPTION |
---|---|
top_n |
The number of entries to return.
|
score_threshold |
The minimum relevance score for an entry to be returned.
|
override_score |
If True reranking will override element score.
|
max_chunks_per_doc |
The maximum amount of tokens a document can have before truncation.
TYPE:
|
model_config
class-attribute
instance-attribute
#
dict
#
dict() -> dict[str, Any]
Creates a dictionary representation of the Options instance. If a value is None, it will be replaced with a provider-specific not-given sentinel.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
A dictionary representation of the Options instance. |