Skip to content

Query Rephrasers#

ragbits.document_search.retrieval.rephrasers.QueryRephraser #

Bases: WithConstructionConfig, ABC

Rephrases a query. Can provide multiple rephrased queries from one sentence / question.

default_module class-attribute instance-attribute #

default_module: ClassVar = rephrasers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'rephraser'

subclass_from_config classmethod #

subclass_from_config(config: ObjectContructionConfig) -> 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: ObjectContructionConfig

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: ObjectContructionConfig) -> 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 prefferences, 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 prefferences, 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:
        preferrences = get_config_from_yaml(yaml_path_override)
        if type_config := preferrences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectContructionConfig.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(ObjectContructionConfig.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

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict

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) -> 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.
    """
    return cls(**config)

rephrase abstractmethod async #

rephrase(query: str) -> list[str]

Rephrase a query.

PARAMETER DESCRIPTION
query

The query to rephrase.

TYPE: str

RETURNS DESCRIPTION
list[str]

The rephrased queries.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/base.py
@abstractmethod
async def rephrase(self, query: str) -> list[str]:
    """
    Rephrase a query.

    Args:
        query: The query to rephrase.

    Returns:
        The rephrased queries.
    """

ragbits.document_search.retrieval.rephrasers.LLMQueryRephraser #

LLMQueryRephraser(llm: LLM, prompt: type[Prompt[QueryRephraserInput, Any]] | None = None)

Bases: QueryRephraser

A rephraser class that uses a LLM to rephrase queries.

Initialize the LLMQueryRephraser with a LLM.

PARAMETER DESCRIPTION
llm

A LLM instance to handle query rephrasing.

TYPE: LLM

prompt

The prompt to use for rephrasing queries.

TYPE: type[Prompt[QueryRephraserInput, Any]] | None DEFAULT: None

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/llm.py
def __init__(self, llm: LLM, prompt: type[Prompt[QueryRephraserInput, Any]] | None = None):
    """
    Initialize the LLMQueryRephraser with a LLM.

    Args:
        llm: A LLM instance to handle query rephrasing.
        prompt: The prompt to use for rephrasing queries.
    """
    self._llm = llm
    self._prompt = prompt or QueryRephraserPrompt

default_module class-attribute instance-attribute #

default_module: ClassVar = rephrasers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'rephraser'

subclass_from_config classmethod #

subclass_from_config(config: ObjectContructionConfig) -> 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: ObjectContructionConfig

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: ObjectContructionConfig) -> 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 prefferences, 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 prefferences, 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:
        preferrences = get_config_from_yaml(yaml_path_override)
        if type_config := preferrences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectContructionConfig.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(ObjectContructionConfig.model_validate(preferred_config))

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

rephrase async #

rephrase(query: str) -> list[str]

Rephrase a given query using the LLM.

PARAMETER DESCRIPTION
query

The query to be rephrased. If not provided, a custom prompt must be given.

TYPE: str

RETURNS DESCRIPTION
list[str]

A list containing the rephrased query.

RAISES DESCRIPTION
LLMConnectionError

If there is a connection error with the LLM API.

LLMStatusError

If the LLM API returns an error status code.

LLMResponseError

If the LLM API response is invalid.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/llm.py
@traceable
async def rephrase(self, query: str) -> list[str]:
    """
    Rephrase a given query using the LLM.

    Args:
        query: The query to be rephrased. If not provided, a custom prompt must be given.

    Returns:
        A list containing the rephrased query.

    Raises:
        LLMConnectionError: If there is a connection error with the LLM API.
        LLMStatusError: If the LLM API returns an error status code.
        LLMResponseError: If the LLM API response is invalid.
    """
    input_data = self._prompt.input_type(query=query)  # type: ignore
    prompt = self._prompt(input_data)
    response = await self._llm.generate(prompt)
    return response if isinstance(response, list) else [response]

from_config classmethod #

from_config(config: dict) -> LLMQueryRephraser

Create an instance of LLMQueryRephraser from a configuration dictionary.

PARAMETER DESCRIPTION
config

A dictionary containing configuration settings for the rephraser.

TYPE: dict

RETURNS DESCRIPTION
LLMQueryRephraser

An instance of the rephraser class initialized with the provided configuration.

RAISES DESCRIPTION
ValidationError

If the LLM or prompt configuration doesn't follow the expected format.

InvalidConfigError

If an LLM or prompt class can't be found or is not the correct type.

ValueError

If the prompt class is not a subclass of Prompt.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/llm.py
@classmethod
def from_config(cls, config: dict) -> "LLMQueryRephraser":
    """
    Create an instance of `LLMQueryRephraser` from a configuration dictionary.

    Args:
        config: A dictionary containing configuration settings for the rephraser.

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

    Raises:
       ValidationError: If the LLM or prompt configuration doesn't follow the expected format.
       InvalidConfigError: If an LLM or prompt class can't be found or is not the correct type.
       ValueError: If the prompt class is not a subclass of `Prompt`.

    """
    llm: LLM = LLM.subclass_from_config(ObjectContructionConfig.model_validate(config["llm"]))
    prompt_cls = None
    if "prompt" in config:
        prompt_config = ObjectContructionConfig.model_validate(config["prompt"])
        prompt_cls = get_rephraser_prompt(prompt_config.type)
    return cls(llm=llm, prompt=prompt_cls)

ragbits.document_search.retrieval.rephrasers.MultiQueryRephraser #

MultiQueryRephraser(llm: LLM, n: int | None = None, prompt: type[Prompt[MultiQueryRephraserInput, Any]] | None = None)

Bases: QueryRephraser

A rephraser class that uses a LLM to generate reworded versions of input query.

Initialize the MultiQueryRephraser with a LLM.

PARAMETER DESCRIPTION
llm

A LLM instance to handle query rephrasing.

TYPE: LLM

n

The number of rephrasings to generate.

TYPE: int | None DEFAULT: None

prompt

The prompt to use for rephrasing queries.

TYPE: type[Prompt[MultiQueryRephraserInput, Any]] | None DEFAULT: None

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/multi.py
def __init__(
    self, llm: LLM, n: int | None = None, prompt: type[Prompt[MultiQueryRephraserInput, Any]] | None = None
):
    """
    Initialize the MultiQueryRephraser with a LLM.

    Args:
        llm: A LLM instance to handle query rephrasing.
        n: The number of rephrasings to generate.
        prompt: The prompt to use for rephrasing queries.
    """
    self._llm = llm
    self._n = n if n else 5
    self._prompt = prompt or MultiQueryRephraserPrompt

default_module class-attribute instance-attribute #

default_module: ClassVar = rephrasers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'rephraser'

subclass_from_config classmethod #

subclass_from_config(config: ObjectContructionConfig) -> 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: ObjectContructionConfig

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: ObjectContructionConfig) -> 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 prefferences, 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 prefferences, 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:
        preferrences = get_config_from_yaml(yaml_path_override)
        if type_config := preferrences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectContructionConfig.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(ObjectContructionConfig.model_validate(preferred_config))

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

rephrase async #

rephrase(query: str) -> list[str]

Rephrase a given query using the LLM.

PARAMETER DESCRIPTION
query

The query to be rephrased. If not provided, a custom prompt must be given.

TYPE: str

RETURNS DESCRIPTION
list[str]

A list containing the reworded versions of input query.

RAISES DESCRIPTION
LLMConnectionError

If there is a connection error with the LLM API.

LLMStatusError

If the LLM API returns an error status code.

LLMResponseError

If the LLM API response is invalid.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/multi.py
@traceable
async def rephrase(self, query: str) -> list[str]:
    """
    Rephrase a given query using the LLM.

    Args:
        query: The query to be rephrased. If not provided, a custom prompt must be given.

    Returns:
        A list containing the reworded versions of input query.

    Raises:
        LLMConnectionError: If there is a connection error with the LLM API.
        LLMStatusError: If the LLM API returns an error status code.
        LLMResponseError: If the LLM API response is invalid.
    """
    input_data = self._prompt.input_type(query=query, n=self._n)  # type: ignore
    prompt = self._prompt(input_data)
    response = await self._llm.generate(prompt)
    return [query] + response

from_config classmethod #

from_config(config: dict) -> MultiQueryRephraser

Create an instance of MultiQueryRephraser from a configuration dictionary.

PARAMETER DESCRIPTION
config

A dictionary containing configuration settings for the rephraser.

TYPE: dict

RETURNS DESCRIPTION
MultiQueryRephraser

An instance of the rephraser class initialized with the provided configuration.

RAISES DESCRIPTION
ValidationError

If the LLM or prompt configuration doesn't follow the expected format.

InvalidConfigError

If an LLM or prompt class can't be found or is not the correct type.

ValueError

If the prompt class is not a subclass of Prompt.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/multi.py
@classmethod
def from_config(cls, config: dict) -> "MultiQueryRephraser":
    """
    Create an instance of `MultiQueryRephraser` from a configuration dictionary.

    Args:
        config: A dictionary containing configuration settings for the rephraser.

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

    Raises:
       ValidationError: If the LLM or prompt configuration doesn't follow the expected format.
       InvalidConfigError: If an LLM or prompt class can't be found or is not the correct type.
       ValueError: If the prompt class is not a subclass of `Prompt`.

    """
    llm: LLM = LLM.subclass_from_config(ObjectContructionConfig.model_validate(config["llm"]))
    prompt_cls = None
    if "prompt" in config:
        prompt_config = ObjectContructionConfig.model_validate(config["prompt"])
        prompt_cls = get_rephraser_prompt(prompt_config.type)
    n = config.get("n", 5)
    return cls(llm=llm, n=n, prompt=prompt_cls)

ragbits.document_search.retrieval.rephrasers.NoopQueryRephraser #

Bases: QueryRephraser

A no-op query paraphraser that does not change the query.

default_module class-attribute instance-attribute #

default_module: ClassVar = rephrasers

configuration_key class-attribute instance-attribute #

configuration_key: ClassVar = 'rephraser'

subclass_from_config classmethod #

subclass_from_config(config: ObjectContructionConfig) -> 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: ObjectContructionConfig

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: ObjectContructionConfig) -> 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 prefferences, 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 prefferences, 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:
        preferrences = get_config_from_yaml(yaml_path_override)
        if type_config := preferrences.get(cls.configuration_key):
            return cls.subclass_from_config(ObjectContructionConfig.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(ObjectContructionConfig.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

Initializes the class with the provided configuration.

PARAMETER DESCRIPTION
config

A dictionary containing configuration details for the class.

TYPE: dict

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) -> 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.
    """
    return cls(**config)

rephrase async #

rephrase(query: str) -> list[str]

Mock implementation which outputs the same query as in input.

PARAMETER DESCRIPTION
query

The query to rephrase.

TYPE: str

RETURNS DESCRIPTION
list[str]

The list with non-transformed query.

Source code in packages/ragbits-document-search/src/ragbits/document_search/retrieval/rephrasers/noop.py
@traceable
async def rephrase(self, query: str) -> list[str]:  # noqa: PLR6301
    """
    Mock implementation which outputs the same query as in input.

    Args:
        query: The query to rephrase.

    Returns:
        The list with non-transformed query.
    """
    return [query]