def test_merge_yaml_domains(): test_yaml_1 = """config: store_entities_as_slots: true entities: [] intents: [] slots: {} responses: utter_greet: - text: hey there!""" test_yaml_2 = """config: store_entities_as_slots: false session_config: session_expiration_time: 20 carry_over_slots: true entities: - cuisine intents: - greet slots: cuisine: type: text responses: utter_goodbye: - text: bye! utter_greet: - text: hey you!""" domain_1 = Domain.from_yaml(test_yaml_1) domain_2 = Domain.from_yaml(test_yaml_2) domain = domain_1.merge(domain_2) # single attribute should be taken from domain_1 assert domain.store_entities_as_slots # conflicts should be taken from domain_1 assert domain.templates == { "utter_greet": [{"text": "hey there!"}], "utter_goodbye": [{"text": "bye!"}], } # lists should be deduplicated and merged assert domain.intents == sorted(["greet", *DEFAULT_INTENTS]) assert domain.entities == ["cuisine"] assert isinstance(domain.slots[0], TextSlot) assert domain.slots[0].name == "cuisine" assert sorted(domain.user_actions) == sorted(["utter_greet", "utter_goodbye"]) assert domain.session_config == SessionConfig(20, True) domain = domain_1.merge(domain_2, override=True) # single attribute should be taken from domain_2 assert not domain.store_entities_as_slots # conflicts should take value from domain_2 assert domain.templates == { "utter_greet": [{"text": "hey you!"}], "utter_goodbye": [{"text": "bye!"}], } assert domain.session_config == SessionConfig(20, True)
def test_domain_as_dict_with_session_config(): session_config = SessionConfig(123, False) domain = Domain.empty() domain.session_config = session_config serialized = domain.as_dict() deserialized = Domain.from_dict(serialized) assert deserialized.session_config == session_config
def test_merge_session_config_if_first_is_not_default(): yaml1 = """ session_config: session_expiration_time: 20 carry_over_slots: true""" yaml2 = """ session_config: session_expiration_time: 40 carry_over_slots: true """ domain1 = Domain.from_yaml(yaml1) domain2 = Domain.from_yaml(yaml2) merged = domain1.merge(domain2) assert merged.session_config == SessionConfig(20, True) merged = domain1.merge(domain2, override=True) assert merged.session_config == SessionConfig(40, True)
def __prepare_training_session_config(self, bot: Text): session_config = self.fetch_session_config(bot) if session_config: return { SESSION_CONFIG.SESSION_EXPIRATION_TIME.value: session_config.sesssionExpirationTime, SESSION_CONFIG.CARRY_OVER_SLOTS.value: session_config.carryOverSlots, } else: default_session = SessionConfig.default() return { SESSION_CONFIG.SESSION_EXPIRATION_TIME.value: default_session.session_expiration_time, SESSION_CONFIG.CARRY_OVER_SLOTS.value: default_session.carry_over_slots, }
async def test_has_session_expired( event_to_apply: Optional[Event], session_expiration_time_in_minutes: float, has_expired: bool, default_processor: MessageProcessor, ): sender_id = uuid.uuid4().hex default_processor.domain.session_config = SessionConfig( session_expiration_time_in_minutes, True) # create new tracker without events tracker = default_processor.tracker_store.get_or_create_tracker(sender_id) tracker.events.clear() # apply desired event if event_to_apply: tracker.update(event_to_apply) # noinspection PyProtectedMember assert default_processor._has_session_expired(tracker) == has_expired
async def default_agent(_default_agent: Agent) -> Agent: # Clean tracker store after each test so tests don't affect each other _default_agent.tracker_store = InMemoryTrackerStore(_default_agent.domain) _default_agent.domain.session_config = SessionConfig.default() return _default_agent
def reset_conversation_state(agent: Agent) -> Agent: # Clean tracker store after each test so tests don't affect each other agent.tracker_store = InMemoryTrackerStore(agent.domain) agent.domain.session_config = SessionConfig.default() return agent
def test_are_sessions_enabled(session_config: SessionConfig, enabled: bool): assert session_config.are_sessions_enabled() == enabled
def test_domain_as_dict_with_session_config(): session_config = SessionConfig(123, False) domain = Domain.empty() domain.session_config = session_config serialized = domain.as_dict() deserialized = Domain.from_dict(serialized) assert deserialized.session_config == session_config @pytest.mark.parametrize( "session_config, enabled", [ (SessionConfig(0, True), False), (SessionConfig(1, True), True), (SessionConfig(-1, False), False), ], ) def test_are_sessions_enabled(session_config: SessionConfig, enabled: bool): assert session_config.are_sessions_enabled() == enabled def test_domain_from_dict_does_not_change_input(): input_before = { "intents": [ {"greet": {USE_ENTITIES_KEY: ["name"]}}, {"default": {IGNORE_ENTITIES_KEY: ["unrelated_recognized_entity"]}}, {"goodbye": {USE_ENTITIES_KEY: None}}, {"thank": {USE_ENTITIES_KEY: False}},
default_channel: CollectingOutputChannel, template_nlg: TemplatedNaturalLanguageGenerator, template_sender_tracker: DialogueStateTracker, default_domain: Domain, ): events = await ActionSessionStart().run(default_channel, template_nlg, template_sender_tracker, default_domain) assert events == [SessionStarted(), ActionExecuted(ACTION_LISTEN_NAME)] @pytest.mark.parametrize( "session_config, expected_events", [ ( SessionConfig(123, True), [ SessionStarted(), SlotSet("my_slot", "value"), SlotSet("another-slot", "value2"), ActionExecuted(action_name=ACTION_LISTEN_NAME), ], ), ( SessionConfig(123, False), [SessionStarted(), ActionExecuted(action_name=ACTION_LISTEN_NAME)], ), ], ) async def test_action_session_start_with_slots(