Skip to content

Utils

Utility functions for ragpill.

merge_settings

ragpill.utils.merge_settings

merge_settings(settings_prefixes)

Merge multiple pydantic settings into a single dict with prefixed keys for MLflow logging.

Each settings object's fields are flattened and prefixed with the given string, producing a dict suitable for passing as model_params to evaluate_testset_with_mlflow.

Parameters:

Name Type Description Default
settings_prefixes Sequence[tuple[BaseSettings | dict[str, Any], str]]

Sequence of (settings_object, prefix) tuples. Each settings object (a BaseSettings instance or plain dict) is flattened and its keys are prefixed with prefix_.

required

Returns:

Type Description
dict[str, Any]

A single merged dictionary with prefixed keys from all settings objects.

Example
from ragpill import merge_settings

params = merge_settings([
    (mlflow_settings, "mlflow"),
    (agent_settings, "agent"),
    (llm_settings, "llm"),
])
Source code in src/ragpill/utils.py
def merge_settings(settings_prefixes: Sequence[tuple[BaseSettings | dict[str, Any], str]]) -> dict[str, Any]:
    """Merge multiple pydantic settings into a single dict with prefixed keys for MLflow logging.

    Each settings object's fields are flattened and prefixed with the given string,
    producing a dict suitable for passing as ``model_params`` to
    [`evaluate_testset_with_mlflow`][ragpill.mlflow_helper.evaluate_testset_with_mlflow].

    Args:
        settings_prefixes: Sequence of ``(settings_object, prefix)`` tuples. Each
            settings object (a ``BaseSettings`` instance or plain dict) is flattened
            and its keys are prefixed with ``prefix_``.

    Returns:
        A single merged dictionary with prefixed keys from all settings objects.

    Example:
        ```python
        from ragpill import merge_settings

        params = merge_settings([
            (mlflow_settings, "mlflow"),
            (agent_settings, "agent"),
            (llm_settings, "llm"),
        ])
        ```
    """
    return reduce(lambda x, y: x | y, map(_prefix_settings_key, settings_prefixes))