def test_test_core_comparison_after_train( run_in_simple_project: Callable[..., RunResult], trained_rasa_model: str, tmp_path: Path, ): path = Path(tmp_path / "comparison_models") path.mkdir() run_one = Path(path / "run_1") run_one.mkdir() shutil.copy(trained_rasa_model, run_one) run_two = Path(path / "run_2") run_two.mkdir() shutil.copy(trained_rasa_model, run_two) write_text_file("[1]", path / "num_stories.json") run_in_simple_project( "test", "core", "-m", str(path), "--stories", "data/stories", "--evaluate-model-directory", ) assert os.path.exists(os.path.join(DEFAULT_RESULTS_PATH, RESULTS_FILE)) assert os.path.exists( os.path.join(DEFAULT_RESULTS_PATH, "core_model_comparison_graph.pdf"))
def test_story_file_with_minimal_story_is_story_file(tmpdir: Path): p = tmpdir / "story.md" s = """ ## my story """ write_text_file(s, p) assert rasa.shared.data.is_story_file(str(p))
def test_conversation_tests_in_a_directory(tmp_path: Path): parent = tmp_path / "tests" Path(parent).mkdir(parents=True) e2e_path = parent / "test_stories.yml" e2e_story = """stories:""" write_text_file(e2e_story, e2e_path) assert rasa.shared.data.is_test_stories_file(str(e2e_path))
def test_default_conversation_tests_are_conversation_tests_yml(tmpdir: Path): parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) e2e_path = parent / "test_stories.yml" e2e_story = """stories:""" write_text_file(e2e_story, e2e_path) assert rasa.shared.data.is_test_stories_file(str(e2e_path))
def test_default_conversation_tests_are_conversation_tests_md(tmpdir: Path): # can be removed once conversation tests MD support is removed parent = tmpdir / "tests" Path(parent).mkdir(parents=True) e2e_path = parent / "test_stories.yml" e2e_story = """stories:""" write_text_file(e2e_story, e2e_path) assert rasa.shared.data.is_test_stories_file(str(e2e_path))
def test_default_conversation_tests_are_conversation_tests_md(tmpdir: Path): # can be removed once conversation tests MD support is removed parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) e2e_path = parent / "conversation_tests.md" e2e_story = """## my story test""" write_text_file(e2e_story, e2e_path) assert rasa.shared.data.is_test_stories_file(str(e2e_path))
def test_nlu_data_files_are_not_conversation_tests(tmpdir: Path): nlu_path = tmpdir / "nlu.md" nlu_data = """ ## intent: greet - hello - hi - hallo """ write_text_file(nlu_data, nlu_path) assert not rasa.shared.data.is_test_stories_file(str(nlu_path))
def json_pickle(file_name: Union[Text, Path], obj: Any) -> None: """Pickle an object to a file using json. Args: file_name: the file to store the object to obj: the object to store """ import jsonpickle.ext.numpy as jsonpickle_numpy import jsonpickle jsonpickle_numpy.register_handlers() write_text_file(jsonpickle.dumps(obj), file_name)
def test_nlu_data_files_are_not_conversation_tests(tmpdir: Path): parent = tmpdir / DEFAULT_E2E_TESTS_PATH Path(parent).mkdir(parents=True) nlu_path = parent / "nlu.md" nlu_data = """ ## intent: greet - hello - hi - hallo """ write_text_file(nlu_data, nlu_path) assert not rasa.shared.data.is_test_stories_file(str(nlu_path))
def test_is_nlu_file_with_json(): test = { "rasa_nlu_data": { "lookup_tables": [ {"name": "plates", "elements": ["beans", "rice", "tacos", "cheese"]} ] } } directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") write_text_file(json_to_string(test), file) assert rasa.shared.data.is_nlu_file(file)
def persist( state: StateMachineState, is_initial_state: bool, domain_folder: str, nlu_folder: str, ): domain, nlu_data = get_domain_nlu( state=state, is_initial_state=is_initial_state ) # Generate filename filename = "".join( e.lower() for e in state.name if e.isalnum() or e.isspace() or e in ["-", "_"] ) filename = "_".join(filename.split(" ")) + ".yaml" # Persist domain domain_filename = os.path.join(domain_folder, filename) Path(domain_filename).parent.mkdir(parents=True, exist_ok=True) rasa.shared.utils.validation.validate_yaml_schema( domain.as_yaml(), rasa.shared.constants.DOMAIN_SCHEMA_FILE ) # Delete domain_filename if os.path.exists(domain_filename): os.remove(domain_filename) domain.persist(domain_filename) # Persist NLU nlu_filename = os.path.join(nlu_folder, filename) nlu_data_yaml = dump_obj_as_yaml_to_string( nlu_data, should_preserve_key_order=True ) RasaYAMLReader().validate(nlu_data_yaml) Path(nlu_filename).parent.mkdir(parents=True, exist_ok=True) if os.path.exists(nlu_filename): os.remove(nlu_filename) write_text_file(nlu_data_yaml, nlu_filename)
def test_default_conversation_tests_are_conversation_tests_yml(tmp_path: Path): e2e_path = tmp_path / "test_stories.yml" e2e_story = """stories:""" write_text_file(e2e_story, e2e_path) assert rasa.shared.data.is_test_stories_file(str(e2e_path))
def dump_obj_as_json_to_file(filename: Union[Text, Path], obj: Any) -> None: """Dump an object as a json string to a file.""" write_text_file(json.dumps(obj, indent=2), filename)
def test_is_not_nlu_file_with_json(): directory = tempfile.mkdtemp() file = os.path.join(directory, "test.json") write_text_file('{"test": "a"}', file) assert not rasa.shared.data.is_nlu_file(file)
def persist( stories: List[Story], domain_filename: str, nlu_filename: str, additional_intents: List[Intent], additional_utterances: List[Utterance], slots: List[Slot], use_rules: bool = False, ): all_domain = Domain.empty() all_intents: Set[Intent] = set(additional_intents) all_stories: List[Story] = [] all_slot_was_sets: Set[SlotWasSet] = set() for story in stories: domain, sub_stories, intents, slot_was_sets = story.get_domain_nlu( use_rules=use_rules) all_domain = all_domain.merge(domain) all_intents.update(intents) all_stories.extend(sub_stories) all_slot_was_sets.update(slot_was_sets) # Append consolidated slots domain_slots = Domain( intents=set([intent.name for intent in all_intents]), entities=[slot.name for slot in slots], slots=slots, responses={ utterance.name: [{ "text": utterance.text }] for utterance in additional_utterances }, action_names=[], forms={}, ) all_domain = all_domain.merge(domain_slots) # Validate domain rasa.shared.utils.validation.validate_yaml_schema( all_domain.as_yaml(), rasa.shared.constants.DOMAIN_SCHEMA_FILE) # Write domain if os.path.exists(domain_filename): os.remove(domain_filename) Path(domain_filename).parent.mkdir(parents=True, exist_ok=True) all_domain.persist(domain_filename) # Write NLU nlu_data = { "version": "2.0", "nlu": [ intent.as_nlu_yaml() for intent in all_intents if isinstance(intent, IntentWithExamples) ], "rules" if use_rules else "stories": all_stories, } nlu_data_yaml = dump_obj_as_yaml_to_string(nlu_data, should_preserve_key_order=True) RasaYAMLReader().validate(nlu_data_yaml) # TODO: Create folders if not existent if os.path.exists(nlu_filename): os.remove(nlu_filename) Path(nlu_filename).parent.mkdir(parents=True, exist_ok=True) write_text_file(nlu_data_yaml, nlu_filename)