Exemple #1
0
def test_trained_interpreter_passed_to_core_training(
    monkeypatch: MonkeyPatch,
    tmp_path: Path,
    unpacked_trained_rasa_model: Text,
    nlu_data_path: Text,
    stories_path: Text,
    config_path: Text,
    domain_path: Text,
):
    # Skip actual NLU training and return trained interpreter path from fixture
    # Patching is bit more complicated as we have a module `train` and function
    # with the same name 😬
    monkeypatch.setattr(
        rasa.model_training,
        "_train_nlu_with_validated_data",
        AsyncMock(return_value=unpacked_trained_rasa_model),
    )

    # Mock the actual Core training
    _train_core = mock_core_training(monkeypatch)

    train(
        domain_path,
        config_path,
        [stories_path, nlu_data_path],
        str(tmp_path),
    )

    _train_core.assert_called_once()
    _, _, kwargs = _train_core.mock_calls[0]
    assert isinstance(kwargs["interpreter"], RasaNLUInterpreter)
Exemple #2
0
def test_model_finetuning_with_invalid_model(
    tmp_path: Path,
    monkeypatch: MonkeyPatch,
    domain_path: Text,
    stories_path: Text,
    stack_config_path: Text,
    nlu_data_path: Text,
    model_to_fine_tune: Text,
    capsys: CaptureFixture,
):
    mocked_nlu_training = mock_nlu_training(monkeypatch)

    mocked_core_training = mock_core_training(monkeypatch)
    (tmp_path / "models").mkdir()
    output = str(tmp_path / "models")

    with pytest.raises(SystemExit):
        train(
            domain_path,
            stack_config_path,
            [stories_path, nlu_data_path],
            output=output,
            force_training=True,
            model_to_finetune=model_to_fine_tune,
            finetuning_epoch_fraction=1,
        )

    mocked_core_training.assert_not_called()
    mocked_nlu_training.assert_not_called()
    output = capsys.readouterr().out
    assert "No NLU model for finetuning found" in output
Exemple #3
0
    def test_new_nlu_data_does_not_retrain_core_if_there_are_no_e2e_stories(
        self,
        monkeypatch: MonkeyPatch,
        trained_simple_rasa_model: Text,
        domain_path: Text,
        stack_config_path: Text,
        simple_stories_path: Text,
        nlu_data_path: Text,
        tmp_path: Path,
    ):
        nlu_yaml = rasa.shared.utils.io.read_yaml_file(nlu_data_path)
        nlu_yaml["nlu"][0]["examples"] += "- surprise!\n"

        new_nlu_file = tmp_path / "new_nlu.yml"
        rasa.shared.utils.io.write_yaml(nlu_yaml, new_nlu_file)

        mocked_nlu_training = mock_nlu_training(monkeypatch)
        mocked_core_training = mock_core_training(monkeypatch)

        new_model_path = train(
            domain_path,
            stack_config_path,
            [simple_stories_path, new_nlu_file],
            output=new_model_path_in_same_dir(trained_simple_rasa_model),
        ).model
        os.remove(new_model_path)

        mocked_core_training.assert_not_called()
        mocked_nlu_training.assert_called_once()
Exemple #4
0
    def test_retrains_only_core_if_new_e2e_example_seen_before(
        self,
        monkeypatch: MonkeyPatch,
        trained_e2e_model: Text,
        domain_path: Text,
        stack_config_path: Text,
        e2e_stories_path: Text,
        nlu_data_path: Text,
        tmp_path: Path,
    ):
        stories_yaml = rasa.shared.utils.io.read_yaml_file(e2e_stories_path)
        stories_yaml["stories"][1]["steps"].append({"user": "******"})

        new_stories_file = new_stories_file = tmp_path / "new_stories.yml"
        rasa.shared.utils.io.write_yaml(stories_yaml, new_stories_file)

        mocked_nlu_training = mock_nlu_training(monkeypatch)
        mocked_core_training = mock_core_training(monkeypatch)

        new_model_path = train(
            domain_path,
            stack_config_path,
            [new_stories_file, nlu_data_path],
            output=new_model_path_in_same_dir(trained_e2e_model),
        ).model
        os.remove(new_model_path)

        mocked_core_training.assert_called_once()
        mocked_nlu_training.assert_not_called()
Exemple #5
0
    def test_nlu_and_core_trained_if_no_nlu_data_but_e2e_stories(
        self,
        monkeypatch: MonkeyPatch,
        domain_path: Text,
        stack_config_path: Text,
        e2e_stories_path: Text,
        tmp_path: Path,
    ):
        mocked_nlu_training = mock_nlu_training(monkeypatch)
        mocked_core_training = mock_core_training(monkeypatch)

        output = self.make_tmp_model_dir(tmp_path)
        train(
            domain_path, stack_config_path, [e2e_stories_path], output=output,
        )

        mocked_core_training.assert_called_once()
        mocked_nlu_training.assert_called_once()
Exemple #6
0
    def test_models_not_retrained_if_no_new_data(
        self,
        monkeypatch: MonkeyPatch,
        trained_e2e_model: Text,
        domain_path: Text,
        stack_config_path: Text,
        e2e_stories_path: Text,
        nlu_data_path: Text,
    ):
        mocked_nlu_training = mock_nlu_training(monkeypatch)
        mocked_core_training = mock_core_training(monkeypatch)

        train(
            domain_path,
            stack_config_path,
            [e2e_stories_path, nlu_data_path],
            output=new_model_path_in_same_dir(trained_e2e_model),
        )

        mocked_core_training.assert_not_called()
        mocked_nlu_training.assert_not_called()
Exemple #7
0
def interactive_learning(serve_forever=True):
    import logging

    from rasa.core import utils, train

    logger = logging.getLogger(__name__)

    return train(domain_file="domain.yml",
                 output_path="model/dialogue",
                 policy_config = "config_nlu.yml",
                 kwargs={"batch_size": 50,
                         "epochs": 200,
                         "max_training_samples": 300
                         },
                 training_resource='data/stories.md')
Exemple #8
0
def test_model_finetuning(
    tmp_path: Path,
    monkeypatch: MonkeyPatch,
    domain_path: Text,
    stories_path: Text,
    stack_config_path: Text,
    nlu_data_path: Text,
    trained_rasa_model: Text,
    use_latest_model: bool,
):
    mocked_nlu_training = mock_nlu_training(monkeypatch)
    mocked_core_training = mock_core_training(monkeypatch)

    (tmp_path / "models").mkdir()
    output = str(tmp_path / "models")

    if use_latest_model:
        trained_rasa_model = str(Path(trained_rasa_model).parent)

    train(
        domain_path,
        stack_config_path,
        [stories_path, nlu_data_path],
        output=output,
        force_training=True,
        model_to_finetune=trained_rasa_model,
        finetuning_epoch_fraction=0.1,
    )

    mocked_core_training.assert_called_once()
    _, kwargs = mocked_core_training.call_args
    assert isinstance(kwargs["model_to_finetune"], Agent)

    mocked_nlu_training.assert_called_once()
    _, kwargs = mocked_nlu_training.call_args
    assert isinstance(kwargs["model_to_finetune"], Interpreter)
Exemple #9
0
    rasa.shared.utils.cli.print_info(
        f"Loading NLU model from {path_to_archive} for finetuning...",
    )
    with model.unpack_model(path_to_archive) as unpacked:
        _, old_nlu = model.get_model_subdirectories(unpacked)
        new_fingerprint = await model.model_fingerprint(file_importer)
        old_fingerprint = model.fingerprint_from_path(unpacked)
        if not model.can_finetune(
            old_fingerprint,
            new_fingerprint,
            nlu=True,
            core=called_from_combined_training,
        ):
            rasa.shared.utils.cli.print_error_and_exit(
                "NLU model can not be finetuned."
            )

        config = await file_importer.get_config()
        model_to_finetune = Interpreter.load(
            old_nlu,
            new_config=config,
            finetuning_epoch_fraction=finetuning_epoch_fraction,
        )
        if not model_to_finetune:
            return None
    return model_to_finetune

if __name__ == '__main__':
    train(domain = 'domain.yml',config='config.yml',training_files='data',force_training='true')