Skip to content

configuration

langroid/utils/configuration.py

update_global_settings(cfg, keys)

Update global settings so modules can access them via (as an example):

from langroid.utils.configuration import settings
if settings.debug...
Caution we do not want to have too many such global settings! Args: cfg: pydantic config, typically from a main script keys: which keys from cfg to use, to update the global settings object

Source code in langroid/utils/configuration.py
def update_global_settings(cfg: BaseSettings, keys: List[str]) -> None:
    """
    Update global settings so modules can access them via (as an example):
    ```
    from langroid.utils.configuration import settings
    if settings.debug...
    ```
    Caution we do not want to have too many such global settings!
    Args:
        cfg: pydantic config, typically from a main script
        keys: which keys from cfg to use, to update the global settings object
    """
    config_dict = cfg.dict()

    # Filter the config_dict based on the keys
    filtered_config = {key: config_dict[key] for key in keys if key in config_dict}

    # create a new Settings() object to let pydantic validate it
    new_settings = Settings(**filtered_config)

    # Update the unique global settings object
    settings.__dict__.update(new_settings.__dict__)

set_global(key_vals)

Update the unique global settings object

Source code in langroid/utils/configuration.py
def set_global(key_vals: Settings) -> None:
    """Update the unique global settings object"""
    settings.__dict__.update(key_vals.__dict__)

temporary_settings(temp_settings)

Temporarily update the global settings and restore them afterward.

Source code in langroid/utils/configuration.py
@contextmanager
def temporary_settings(temp_settings: Settings) -> Iterator[None]:
    """Temporarily update the global settings and restore them afterward."""
    original_settings = copy.deepcopy(settings)

    set_global(temp_settings)

    try:
        yield
    finally:
        settings.__dict__.update(original_settings.__dict__)

quiet_mode(quiet=True)

Temporarily set quiet=True in global settings and restore afterward.

Source code in langroid/utils/configuration.py
@contextmanager
def quiet_mode(quiet: bool = True) -> Iterator[None]:
    """Temporarily set quiet=True in global settings and restore afterward."""
    original_settings = copy.deepcopy(settings)
    if quiet:
        temp_settings = original_settings.copy(update={"quiet": True})
        set_global(temp_settings)

    try:
        yield
    finally:
        if quiet:
            settings.__dict__.update(original_settings.__dict__)

set_env(settings)

Set environment variables from a BaseSettings instance Args: settings (BaseSettings): desired settings Returns:

Source code in langroid/utils/configuration.py
def set_env(settings: BaseSettings) -> None:
    """
    Set environment variables from a BaseSettings instance
    Args:
        settings (BaseSettings): desired settings
    Returns:
    """
    for field_name, field in settings.__class__.__fields__.items():
        env_var_name = field.field_info.extra.get("env", field_name).upper()
        os.environ[env_var_name] = str(settings.dict()[field_name])