Skip to content

Rerankers#

ragbits.document_search.retrieval.rerankers.base.Reranker #

Reranker(default_options: OptionsT | None = None)

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: OptionsT | None DEFAULT: None

Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
def __init__(self, default_options: OptionsT | None = None) -> None:
    """
    Constructs a new ConfigurableComponent instance.

    Args:
        default_options: The default options for the component.
    """
    self.default_options: OptionsT = default_options or self.options_cls()

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls instance-attribute #

options_cls: type[RerankerOptionsT]

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict[str, Any]) -> Self

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict[str, Any]

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
@classmethod
def from_config(cls, config: dict[str, Any]) -> Self:
    """
    Initializes the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.
    """
    default_options = config.pop("default_options", None)
    options = cls.options_cls(**default_options) if default_options else None
    return cls(**config, default_options=options)

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: Sequence[Sequence[Element]]

query

The query to rerank the elements against.

TYPE: str

options

The options for reranking.

TYPE: RerankerOptionsT | None DEFAULT: None

RETURNS DESCRIPTION
Sequence[Element]

The reranked elements.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/base.py
@abstractmethod
async def rerank(
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: RerankerOptionsT | None = None,
) -> Sequence[Element]:
    """
    Rerank elements.

    Args:
        elements: The elements to rerank.
        query: The query to rerank the elements against.
        options: The options for reranking.

    Returns:
        The reranked elements.
    """

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: str

default_options

The default options for reranking.

TYPE: RerankerOptions | None DEFAULT: None

**rerankers_kwargs

Additional keyword arguments native to rerankers lib.

TYPE: Any DEFAULT: {}

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/answerai.py
def __init__(
    self,
    model: str,
    default_options: RerankerOptions | None = None,
    **rerankers_kwargs: Any,  # noqa: ANN401
) -> None:
    """
    Constructs a new AnswerDotAIRerankersReranker instance.

    Args:
        model: The reranker model to use.
        default_options: The default options for reranking.
        **rerankers_kwargs: Additional keyword arguments native to rerankers lib.
    """
    super().__init__(default_options=default_options)
    self.model = model
    self.ranker = AnswerReranker(self.model, **rerankers_kwargs)

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls class-attribute instance-attribute #

options_cls = RerankerOptions

model instance-attribute #

model = model

ranker instance-attribute #

ranker = Reranker(model, **rerankers_kwargs)

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict[str, Any]) -> Self

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict[str, Any]

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
@classmethod
def from_config(cls, config: dict[str, Any]) -> Self:
    """
    Initializes the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.
    """
    default_options = config.pop("default_options", None)
    options = cls.options_cls(**default_options) if default_options else None
    return cls(**config, default_options=options)

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: Sequence[Sequence[Element]]

query

The query to rerank the elements against.

TYPE: str

options

The options for reranking.

TYPE: RerankerOptions | None DEFAULT: None

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
async def rerank(
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: RerankerOptions | None = None,
) -> Sequence[Element]:
    """
    Rerank elements.

    Args:
        elements: The elements to rerank.
        query: The query to rerank the elements against.
        options: The options for reranking.

    Returns:
        The reranked elements.

    Raises:
        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.
    """
    merged_options = (self.default_options | options) if options else self.default_options
    flat_elements = list(chain.from_iterable(elements))
    documents = [element.text_representation or "" for element in flat_elements]

    with trace(
        query=query, documents=documents, elements=elements, model=self.model, options=merged_options
    ) as outputs:
        response = self.ranker.rank(
            query=query,
            docs=documents,
        )
        if merged_options.top_n:
            response = response.top_k(merged_options.top_n)

        results = []
        for result in response:
            if not merged_options.score_threshold or result.score >= merged_options.score_threshold:
                if merged_options.override_score:
                    flat_elements[result.document.doc_id].score = result.score
                results.append(flat_elements[result.document.doc_id])

        outputs.results = results

    return outputs.results

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: str

default_options

The default options for reranking.

TYPE: LiteLLMRerankerOptions | None DEFAULT: None

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/litellm.py
def __init__(
    self,
    model: str,
    default_options: LiteLLMRerankerOptions | None = None,
) -> None:
    """
    Constructs a new LiteLLMReranker instance.

    Args:
        model: The reranker model to use.
        default_options: The default options for reranking.
    """
    super().__init__(default_options=default_options)
    self.model = model

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls class-attribute instance-attribute #

options_cls = LiteLLMRerankerOptions

model instance-attribute #

model = model

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict[str, Any]) -> Self

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict[str, Any]

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
@classmethod
def from_config(cls, config: dict[str, Any]) -> Self:
    """
    Initializes the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.
    """
    default_options = config.pop("default_options", None)
    options = cls.options_cls(**default_options) if default_options else None
    return cls(**config, default_options=options)

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: Sequence[Sequence[Element]]

query

The query to rerank the elements against.

TYPE: str

options

The options for reranking.

TYPE: LiteLLMRerankerOptions | None DEFAULT: None

RETURNS DESCRIPTION
Sequence[Element]

The reranked elements.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/litellm.py
@traceable
async def rerank(
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: LiteLLMRerankerOptions | None = None,
) -> Sequence[Element]:
    """
    Rerank elements with LiteLLM API.

    Args:
        elements: The elements to rerank.
        query: The query to rerank the elements against.
        options: The options for reranking.

    Returns:
        The reranked elements.
    """
    merged_options = (self.default_options | options) if options else self.default_options
    flat_elements = list(chain.from_iterable(elements))
    documents = [element.text_representation or "" for element in flat_elements]

    response = await litellm.arerank(
        model=self.model,
        query=query,
        documents=documents,
        top_n=merged_options.top_n,
        max_chunks_per_doc=merged_options.max_chunks_per_doc,
    )

    results = []
    for result in response.results:
        if not merged_options.score_threshold or result["relevance_score"] >= merged_options.score_threshold:
            if merged_options.override_score:
                flat_elements[result["index"]].score = result["relevance_score"]
            results.append(flat_elements[result["index"]])

    return results

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: LiteLLM

prompt

The prompt to use for reranking elements.

TYPE: type[Prompt[RerankerInput, str]] | None DEFAULT: None

llm_options

The LLM options to override.

TYPE: LiteLLMOptions | None DEFAULT: None

default_options

The default options for reranking.

TYPE: RerankerOptions | None DEFAULT: None

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/llm.py
def __init__(
    self,
    llm: LiteLLM,
    *,
    prompt: type[Prompt[RerankerInput, str]] | None = None,
    llm_options: LiteLLMOptions | None = None,
    default_options: RerankerOptions | None = None,
) -> None:
    """
    Initialize the LLMReranker instance.

    Args:
        llm: The LLM instance to handle reranking.
        prompt: The prompt to use for reranking elements.
        llm_options: The LLM options to override.
        default_options: The default options for reranking.
    """
    super().__init__(default_options=default_options)
    self._llm = llm
    self._prompt = prompt or RerankerPrompt
    self._llm_options = LiteLLMOptions(
        temperature=0.0,
        logprobs=True,
        max_tokens=1,
        logit_bias={
            self._llm.get_token_id("Yes"): 1,
            self._llm.get_token_id("No"): 1,
        },
    )
    if llm_options:
        self._llm_options |= llm_options

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls class-attribute instance-attribute #

options_cls = RerankerOptions

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict) -> Self

Initialize the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict

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
@classmethod
def from_config(cls, config: dict) -> Self:
    """
    Initialize the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        The initialized instance of LLMReranker.

    Raises:
        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.
    """
    config["llm"] = LLM.subclass_from_config(ObjectConstructionConfig.model_validate(config["llm"]))
    config["prompt"] = import_by_path(config["prompt"]) if "prompt" in config else None
    return super().from_config(config)

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: Sequence[Sequence[Element]]

query

The query to rerank the elements against.

TYPE: str

options

The RerankerOptions to use for reranking.

TYPE: RerankerOptions | None DEFAULT: None

RETURNS DESCRIPTION
Sequence[Element]

The reranked elements.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/llm.py
@traceable
async def rerank(
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: RerankerOptions | None = None,
) -> Sequence[Element]:
    """
    Rerank elements with LLM.

    Args:
        elements: The elements to rerank.
        query: The query to rerank the elements against.
        options: The RerankerOptions to use for reranking.

    Returns:
        The reranked elements.
    """
    merged_options = (self.default_options | options) if options else self.default_options

    flat_elements = list(chain.from_iterable(elements))
    scores = await self._score_elements(flat_elements, query)

    scored_elements = list(zip(flat_elements, scores, strict=True))
    scored_elements.sort(key=lambda x: x[1], reverse=True)

    results = []
    for element, score in scored_elements[: merged_options.top_n]:
        if not merged_options.score_threshold or score >= merged_options.score_threshold:
            if merged_options.override_score:
                element.score = score
            results.append(element)
    return results

ragbits.document_search.retrieval.rerankers.noop.NoopReranker #

NoopReranker(default_options: OptionsT | None = None)

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: OptionsT | None DEFAULT: None

Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
def __init__(self, default_options: OptionsT | None = None) -> None:
    """
    Constructs a new ConfigurableComponent instance.

    Args:
        default_options: The default options for the component.
    """
    self.default_options: OptionsT = default_options or self.options_cls()

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls class-attribute instance-attribute #

options_cls = RerankerOptions

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict[str, Any]) -> Self

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict[str, Any]

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
@classmethod
def from_config(cls, config: dict[str, Any]) -> Self:
    """
    Initializes the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.
    """
    default_options = config.pop("default_options", None)
    options = cls.options_cls(**default_options) if default_options else None
    return cls(**config, default_options=options)

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: Sequence[Sequence[Element]]

query

The query to rerank the elements against.

TYPE: str

options

The options for reranking.

TYPE: RerankerOptions | None DEFAULT: None

RETURNS DESCRIPTION
Sequence[Element]

The reranked elements.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/noop.py
@traceable
async def rerank(  # noqa: PLR6301
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: RerankerOptions | None = None,
) -> Sequence[Element]:
    """
    No reranking, returning the elements in the same order.

    Args:
        elements: The elements to rerank.
        query: The query to rerank the elements against.
        options: The options for reranking.

    Returns:
        The reranked elements.
    """
    return [*{element.id: element for element in chain.from_iterable(elements)}.values()]

ragbits.document_search.retrieval.rerankers.rrf.ReciprocalRankFusionReranker #

ReciprocalRankFusionReranker(default_options: OptionsT | None = None)

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: OptionsT | None DEFAULT: None

Source code in packages/ragbits-core/src/ragbits/core/utils/config_handling.py
def __init__(self, default_options: OptionsT | None = None) -> None:
    """
    Constructs a new ConfigurableComponent instance.

    Args:
        default_options: The default options for the component.
    """
    self.default_options: OptionsT = default_options or self.options_cls()

default_module class-attribute instance-attribute #

default_module: ClassVar = rerankers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'reranker'

default_options instance-attribute #

default_options: OptionsT = default_options or options_cls()

options_cls class-attribute instance-attribute #

options_cls = RerankerOptions

subclass_from_config classmethod #

subclass_from_config(config: ObjectConstructionConfig) -> Self

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: ObjectConstructionConfig

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
@classmethod
def subclass_from_config(cls, config: ObjectConstructionConfig) -> Self:
    """
    Initializes the class with the provided configuration. May return a subclass of the class,
    if requested by the configuration.

    Args:
        config: A model containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.

    Raises:
        InvalidConfigError: The class can't be found or is not a subclass of the current class.
    """
    subclass = import_by_path(config.type, cls.default_module)
    if not issubclass(subclass, cls):
        raise InvalidConfigError(f"{subclass} is not a subclass of {cls}")

    return subclass.from_config(config.config)

subclass_from_factory classmethod #

subclass_from_factory(factory_path: str) -> Self

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: str

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
@classmethod
def subclass_from_factory(cls, factory_path: str) -> Self:
    """
    Creates the class using the provided factory function. May return a subclass of the class,
    if requested by the factory.

    Args:
        factory_path: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".

    Returns:
        An instance of the class initialized with the provided factory function.

    Raises:
        InvalidConfigError: The factory can't be found or the object returned
            is not a subclass of the current class.
    """
    factory = import_by_path(factory_path, cls.default_module)
    obj = factory()
    if not isinstance(obj, cls):
        raise InvalidConfigError(f"The object returned by factory {factory_path} is not an instance of {cls}")
    return obj

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: CoreConfig

factory_path_override

A string representing the path to the factory function in the format of "module.submodule:factory_name".

TYPE: str | None DEFAULT: None

yaml_path_override

A string representing the path to the YAML file containing the Ragstack instance configuration.

TYPE: Path | None DEFAULT: None

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
@classmethod
def preferred_subclass(
    cls, 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.

    Args:
        config: The CoreConfig instance containing preferred factory and configuration details.
        factory_path_override: A string representing the path to the factory function
            in the format of "module.submodule:factory_name".
        yaml_path_override: A string representing the path to the YAML file containing
            the Ragstack instance configuration.

    Raises:
        InvalidConfigError: If the default factory or configuration can't be found.
    """
    if yaml_path_override:
        preferences = get_config_from_yaml(yaml_path_override)
        if type_config := preferences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectConstructionConfig.model_validate(type_config))

    if factory_path_override:
        return cls.subclass_from_factory(factory_path_override)

    if preferred_factory := config.component_preference_factories.get(cls.configuration_key):
        return cls.subclass_from_factory(preferred_factory)

    if preferred_config := config.preferred_instances_config.get(cls.configuration_key):
        return cls.subclass_from_config(ObjectConstructionConfig.model_validate(preferred_config))

    raise NoPreferredConfigError(f"Could not find preferred factory or configuration for {cls.configuration_key}")

from_config classmethod #

from_config(config: dict[str, Any]) -> Self

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict[str, Any]

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
@classmethod
def from_config(cls, config: dict[str, Any]) -> Self:
    """
    Initializes the class with the provided configuration.

    Args:
        config: A dictionary containing configuration details for the class.

    Returns:
        An instance of the class initialized with the provided configuration.
    """
    default_options = config.pop("default_options", None)
    options = cls.options_cls(**default_options) if default_options else None
    return cls(**config, default_options=options)

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: Sequence[Sequence[Element]]

query

The query string for reranking.

TYPE: str

options

Options for reranking.

TYPE: RerankerOptions | None DEFAULT: None

RETURNS DESCRIPTION
Sequence[Element]

The reranked elements.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rerankers/rrf.py
@traceable
async def rerank(
    self,
    elements: Sequence[Sequence[Element]],
    query: str,
    options: RerankerOptions | None = None,
) -> Sequence[Element]:
    """
    Reranks elements using the Reciprocal Rank Fusion (RRF) algorithm.

    Args:
        elements: A list of ranked lists of elements to be fused.
        query: The query string for reranking.
        options: Options for reranking.

    Returns:
        The reranked elements.
    """
    if len(elements) == 1:
        return elements[0]

    merged_options = (self.default_options | options) if options else self.default_options

    scores: dict[str, float] = defaultdict(float)
    elements_map: dict[str, Element] = {}

    for query_elements in elements:
        for rank, element in enumerate(query_elements):
            if not element.key:
                continue
            scores[element.key] += 1 / (rank + 1 + 1)
            elements_map[element.key] = element

    sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)

    results = []
    for element_id, score in sorted_scores[: merged_options.top_n]:
        if not merged_options.score_threshold or score >= merged_options.score_threshold:
            if merged_options.override_score:
                elements_map[element_id].score = score
            results.append(elements_map[element_id])

    return results

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: int | None

score_threshold

The minimum relevance score for an entry to be returned.

TYPE: float | None

override_score

If True reranking will override element score.

TYPE: bool

model_config class-attribute instance-attribute #

model_config = ConfigDict(extra='allow', arbitrary_types_allowed=True)

top_n class-attribute instance-attribute #

top_n: int | None = None

score_threshold class-attribute instance-attribute #

score_threshold: float | None = None

override_score class-attribute instance-attribute #

override_score: bool = True

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
def dict(self) -> dict[str, Any]:  # type: ignore # mypy complains about overriding BaseModel.dict
    """
    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:
        A dictionary representation of the Options instance.
    """
    options = self.model_dump()

    return {
        key: self._not_given if value is None or isinstance(value, NotGiven) else value
        for key, value in options.items()
    }

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: int | None

model_config class-attribute instance-attribute #

model_config = ConfigDict(extra='allow', arbitrary_types_allowed=True)

top_n class-attribute instance-attribute #

top_n: int | None = None

score_threshold class-attribute instance-attribute #

score_threshold: float | None = None

override_score class-attribute instance-attribute #

override_score: bool = True

max_chunks_per_doc class-attribute instance-attribute #

max_chunks_per_doc: int | None = None

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
def dict(self) -> dict[str, Any]:  # type: ignore # mypy complains about overriding BaseModel.dict
    """
    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:
        A dictionary representation of the Options instance.
    """
    options = self.model_dump()

    return {
        key: self._not_given if value is None or isinstance(value, NotGiven) else value
        for key, value in options.items()
    }