import copy


def apply_average_nbest(cfg, nbest=3):
    """Apply the average checkpoint trick to the basic configuration.

    Parameters
    ----------
    cfg
        The basic configuration
    nbest
        The number of best checkpoints to average

    Returns
    -------
    new_cfg
        The new configuration
    """
    new_cfg = copy.deepcopy(cfg)
    search_space = new_cfg['models']['MultimodalTextModel']['search_space']
    search_space['model.use_avg_nbest'] = True
    search_space['optimization.nbest'] = nbest
    return new_cfg