async def test_memorise(self, trained_policy: FormPolicy, default_domain: Domain): domain = Domain.load("data/test_domains/form.yml") trackers = await training.load_data("data/test_stories/stories_form.md", domain) trained_policy.train(trackers, domain, RegexInterpreter()) ( all_states, all_actions, ) = trained_policy.featurizer.training_states_and_actions(trackers, domain) for tracker, states, actions in zip(trackers, all_states, all_actions): for state in states: if state is not None: # check that 'form: inform' was ignored if state.get(USER): assert not state.get(USER).get(INTENT) == "inform" recalled = trained_policy.recall(states, tracker, domain) active_form = trained_policy._get_active_form_name(states[-1]) if states[0] is not None and states[-1] is not None: # explicitly set intents and actions before listen after # which FormPolicy should not predict a form action and # should add FormValidation(False) event # @formatter:off is_no_validation = ( self._test_for_previous_action_and_intent( states, "default", "some_form" ) or self._test_for_previous_action_and_intent( states, "stop", "some_form" ) or self._test_for_previous_action_and_intent( states, "affirm", "utter_ask_continue" ) or self._test_for_previous_action_and_intent( states, "deny", "utter_ask_continue" ) # comes from the fact that intent_inform after utter_ask_continue # is not read from stories or self._test_for_previous_action_and_intent( states, "stop", "utter_ask_continue" ) ) # @formatter:on else: is_no_validation = False if "intent_start_form" in states[-1]: # explicitly check that intent that starts the form # is not memorized as non validation intent assert recalled is None elif is_no_validation: assert recalled == active_form else: assert recalled is None nums = np.random.randn(domain.num_states) random_states = [{f: num for f, num in zip(domain.input_states, nums)}] assert trained_policy.recall(random_states, None, domain) is None
def test_mapping_wins_over_form(events: List[Event]): domain = """ forms: - test-form """ domain = Domain.from_yaml(domain) tracker = DialogueStateTracker.from_events("test", events, []) ensemble = SimplePolicyEnsemble( [ MappingPolicy(), ConstantPolicy(priority=1, predict_index=0), FormPolicy(), FallbackPolicy(), ] ) result, best_policy = ensemble.probabilities_using_best_policy( tracker, domain, RegexInterpreter() ) max_confidence_index = result.index(max(result)) next_action = domain.action_for_index(max_confidence_index, None) index_of_mapping_policy = 0 assert best_policy == f"policy_{index_of_mapping_policy}_{MappingPolicy.__name__}" assert next_action.name() == ACTION_RESTART_NAME
async def test_train_form_policy(): policy = FormPolicy() trackers = await training.load_data( "{}/data/stories_form.md".format(prj_dir), domain, augmentation_factor=0) policy.train(trackers, domain) policy.persist("{}/models/form".format(prj_dir))
async def test_infer_form_policy(): policy = FormPolicy.load("{}/models/form".format(prj_dir)) trackers = await training.load_data( "{}/data/stories_form.md".format(prj_dir), domain, augmentation_factor=0) all_states, all_actions = policy.featurizer.training_states_and_actions( trackers, domain) for tracker, states, actions in zip(trackers, all_states, all_actions): for state in states: if state is not None: # check that 'form: inform' was ignored assert "intent_inform" not in state.keys() recalled = policy.recall(states, tracker, domain) active_form = policy._get_active_form_name(states[-1]) print(states) print(recalled) print(active_form)
) max_confidence_index = result.index(max(result)) next_action = domain.action_for_index(max_confidence_index, None) index_of_mapping_policy = 0 assert best_policy == f"policy_{index_of_mapping_policy}_{MappingPolicy.__name__}" assert next_action.name() == ACTION_RESTART_NAME @pytest.mark.parametrize( "ensemble", [ SimplePolicyEnsemble( [ FormPolicy(), ConstantPolicy(FORM_POLICY_PRIORITY - 1, 0), FallbackPolicy(), ] ), SimplePolicyEnsemble([FormPolicy(), MappingPolicy()]), ], ) def test_form_wins_over_everything_else(ensemble: SimplePolicyEnsemble): form_name = "test-form" domain = f""" forms: - {form_name} """ domain = Domain.from_yaml(domain)
def create_policy(self, featurizer, priority): p = FormPolicy(priority=priority) return p
def create_policy( self, featurizer: Optional[TrackerFeaturizer], priority: int ) -> Policy: return FormPolicy(priority=priority)