def test_extract_requested_slot_from_text_with_not_intent(): """Test extraction of a slot value from text with certain intent """ spec = { "name": "default_form", "slots": [ { "name": "some_slot", "filling": [{"type": "from_text", "not_intent": ["some_intent"],}], } ], } form, tracker = new_form_and_tracker(spec, "some_slot") tracker.update( UserUttered(intent={"name": "some_intent", "confidence": 1.0}, text="some_text") ) slot_values = form.extract_requested_slot( OutputChannel(), nlg, tracker, Domain.empty() ) assert slot_values == {} tracker.update( UserUttered( intent={"name": "some_other_intent", "confidence": 1.0}, text="some_text" ) ) slot_values = form.extract_requested_slot( OutputChannel(), nlg, tracker, Domain.empty() ) assert slot_values == {"some_slot": "some_text"}
def test_extract_requested_slot_from_entity_with_intent(): """Test extraction of a slot value from entity with the different name and certain intent """ spec = { "name": "default_form", "slots": [{ "name": "some_slot", "filling": [{ "type": "from_entity", "entity": ["some_entity"], "intent": ["some_intent"] }] }] } form, tracker = new_form_and_tracker(spec, "some_slot") tracker.update(UserUttered( intent={"name": "some_intent", "confidence": 1.0}, entities=[{"entity": "some_entity", "value": "some_value"}] )) slot_values = form.extract_requested_slot(OutputChannel(), nlg, tracker, Domain.empty()) assert slot_values == {"some_slot": "some_value"} tracker.update(UserUttered( intent={"name": "some_other_intent", "confidence": 1.0}, entities=[{"entity": "some_entity", "value": "some_value"}] )) slot_values = form.extract_requested_slot(OutputChannel(), nlg, tracker, Domain.empty()) assert slot_values == {}
async def test_send_elements_without_buttons(): from rasa.core.channels.channel import OutputChannel async def test_message(sender, message): assert sender == "user" assert message == "a : b" channel = OutputChannel() channel.send_text_message = test_message await channel.send_elements("user", [{"title": "a", "subtitle": "b"}])
async def test_send_custom_messages_without_buttons(): from rasa.core.channels.channel import OutputChannel async def test_message(sender, message): assert sender == 'user' assert message == 'a : b' channel = OutputChannel() channel.send_text_message = test_message await channel.send_custom_message("user", [{ 'title': 'a', 'subtitle': 'b' }])
async def test_validation(value, operator, comparatum, result, caplog): spec = { "name": "default_form", "slots": [ { "name": "some_slot", "validation": {"operator": operator, "comparatum": comparatum,}, } ], } form, tracker = new_form_and_tracker(spec, "some_slot") tracker.update(UserUttered(entities=[{"entity": "some_slot", "value": value}])) events = await form.validate(OutputChannel(), nlg, tracker, Domain.empty()) if result is True: assert len(events) == 1 assert isinstance(events[0], SlotSet) and events[0].value == value else: assert len(events) == 2 assert isinstance(events[0], SlotSet) and events[0].value == None assert ( isinstance(events[1], BotUttered) and events[1].text == "utter_invalid_some_slot" ) if result is None: assert f"Validation operator '{operator}' requires" in caplog.messages[0]
def test_extract_requested_slot_default(): """Test default extraction of a slot value from entity with the same name """ spec = {"name": "default_form"} form, tracker = new_form_and_tracker(spec, "some_slot") tracker.update(UserUttered(entities=[{"entity": "some_slot", "value": "some_value"}])) slot_values = form.extract_requested_slot(OutputChannel(), nlg, tracker, Domain.empty()) assert slot_values == {"some_slot": "some_value"}
async def _send_bot_messages( events: List[Event], tracker: DialogueStateTracker, output_channel: OutputChannel, ) -> None: """Send all the bot messages that are logged in the events array.""" for e in events: if not isinstance(e, BotUttered): continue logger.debug("output_channel.name()") logger.debug(output_channel.name()) logger.debug(e) logger.debug(e.message()) await output_channel.send_response(tracker.sender_id, e.message())
async def trigger_external_user_uttered( self, intent_name: Text, entities: Optional[Union[List[Dict[Text, Any]], Dict[Text, Text]]], tracker: DialogueStateTracker, output_channel: OutputChannel, ) -> None: """Triggers an external message. Triggers an external message (like a user message, but invisible; used, e.g., by a reminder or the trigger_intent endpoint). Args: intent_name: Name of the intent to be triggered. entities: Entities to be passed on. tracker: The tracker to which the event should be added. output_channel: The output channel. """ if isinstance(entities, list): entity_list = entities elif isinstance(entities, dict): # Allow for a short-hand notation {"ent1": "val1", "ent2": "val2", ...}. # Useful if properties like 'start', 'end', or 'extractor' are not given, # e.g. for external events. entity_list = [{ "entity": ent, "value": val } for ent, val in entities.items()] elif not entities: entity_list = [] else: raise_warning( f"Invalid entity specification: {entities}. Assuming no entities." ) entity_list = [] tracker.update( UserUttered.create_external(intent_name, entity_list, output_channel.name())) await self._predict_and_execute_next_action(output_channel, tracker) # save tracker state to continue conversation from this state self._save_tracker(tracker)