def test_nlg_fill_template_text_w_bad_slot_name2(slot_name, slot_value): template_text = "{" + slot_name + "}" t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template_text( template={"text": template_text}, filled_slots={slot_name: slot_value} ) assert result["text"] == template_text
def test_nlg_fill_template_text_and_custom(text_slot_name, text_slot_value, cust_slot_name, cust_slot_value): template = { "text": f"{{{text_slot_name}}}", "custom": { "field": f"{{{cust_slot_name}}}", "properties": { "field_prefixed": f"prefix_{{{cust_slot_name}}}" }, }, } t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={ text_slot_name: text_slot_value, cust_slot_name: cust_slot_value }, ) assert result == { "text": str(text_slot_value), "custom": { "field": str(cust_slot_value), "properties": { "field_prefixed": f"prefix_{str(cust_slot_value)}" }, }, }
def test_nlg_fill_template_attachment(attach_slot_name, attach_slot_value): template = {"attachment": "{" + attach_slot_name + "}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={attach_slot_name: attach_slot_value} ) assert result == {"attachment": str(attach_slot_value)}
def test_nlg_fill_template_text(slot_name, slot_value): template = {"text": "{" + slot_name + "}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template_text( template=template, filled_slots={slot_name: slot_value} ) assert result == {"text": str(slot_value)}
def test_nlg_fill_template_custom(slot_name: Text, slot_value: Any): template = { "custom": { "field": f"{{{slot_name}}}", "properties": { "field_prefixed": f"prefix_{{{slot_name}}}" }, "bool_field": True, "int_field:": 42, "empty_field": None, } } t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template(template=template, filled_slots={slot_name: slot_value}) assert result == { "custom": { "field": str(slot_value), "properties": { "field_prefixed": f"prefix_{slot_value}" }, "bool_field": True, "int_field:": 42, "empty_field": None, } }
def test_nlg_fill_template_button(button_slot_name, button_slot_value): template = {"button": f"{{{button_slot_name}}}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={button_slot_name: button_slot_value} ) assert result == {"button": str(button_slot_value)}
def test_nlg_fill_template_with_bad_slot_name(slot_name, slot_value): template_text = f"{{{slot_name}}}" t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template={"text": template_text}, filled_slots={slot_name: slot_value} ) assert result["text"] == template_text
def test_nlg_fill_template_text_with_json(template_text, expected): template = {"text": template_text} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={"slot_1": "foo", "slot_2": "bar"} ) assert result == {"text": expected}
def test_nlg_fill_template_image(img_slot_name: Text, img_slot_value: Text): template = {"image": f"{{{img_slot_name}}}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={img_slot_name: img_slot_value} ) assert result == {"image": str(img_slot_value)}
def test_nlg_fill_template_quick_replies(quick_replies_slot_name, quick_replies_slot_value): template = {"quick_replies": f"{{{quick_replies_slot_name}}}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={quick_replies_slot_name: quick_replies_slot_value}, ) assert result == {"quick_replies": str(quick_replies_slot_value)}
def test_nlg_fill_template_image_and_text( text_slot_name, text_slot_value, img_slot_name, img_slot_value ): template = {"text": f"{{{text_slot_name}}}", "image": f"{{{img_slot_name}}}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={text_slot_name: text_slot_value, img_slot_name: img_slot_value}, ) assert result == {"text": str(text_slot_value), "image": str(img_slot_value)}
def test_nlg_fill_template_custom_with_list(): template = { "custom": { "blocks": [{"fields": [{"text": "*Departure date:*\n{test}"}]}], "other": ["{test}"], } } t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template(template=template, filled_slots={"test": 5}) assert result == { "custom": { "blocks": [{"fields": [{"text": "*Departure date:*\n5"}]}], "other": ["5"], } }
def test_nlg_fill_template_custom(slot_name, slot_value): template = {"text": "{" + slot_name + "}"} template = { "custom": { "field": "{" + slot_name + "}", "properties": {"field_prefixed": "prefix_{" + slot_name + "}"}, } } t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template(template=template, filled_slots={slot_name: slot_value}) assert result == { "custom": { "field": str(slot_value), "properties": {"field_prefixed": "prefix_" + str(slot_value)}, } }
def template_nlg(): templates = { "utter_ask_rephrase": [{ "text": "can you rephrase that?" }], "utter_restart": [{ "text": "congrats, you've restarted me!" }], "utter_back": [{ "text": "backing up..." }], "utter_invalid": [{ "text": "a template referencing an invalid {variable}." }], "utter_buttons": [{ "text": "button message", "buttons": [ { "payload": "button1", "title": "button1" }, { "payload": "button2", "title": "button2" }, ], }], } return TemplatedNaturalLanguageGenerator(templates)
def test_nlg_fill_template_button(button_slot_name, button_slot_value): template = { "buttons": [{ "payload": f'/choose{{{{"some_slot": "{{{button_slot_name}}}"}}}}', "title": f"{{{button_slot_name}}}", }] } t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template( template=template, filled_slots={button_slot_name: button_slot_value}) assert result == { "buttons": [{ "payload": f'/choose{{"some_slot": "{button_slot_value}"}}', "title": f"{button_slot_value}", }] }
async def test_run_end_to_end_utterance_action(): end_to_end_utterance = "Hi" domain = Domain.from_yaml( textwrap.dedent(f""" actions: - my_action {KEY_E2E_ACTIONS}: - {end_to_end_utterance} - Bye Bye """)) e2e_action = action.action_for_name_or_text("Hi", domain, None) events = await e2e_action.run( CollectingOutputChannel(), TemplatedNaturalLanguageGenerator(domain.templates), DialogueStateTracker.from_events("sender", evts=[]), domain, ) assert events == [ BotUttered( end_to_end_utterance, { "elements": None, "quick_replies": None, "buttons": None, "attachment": None, "image": None, "custom": None, }, {}, ) ]
async def test_remote_action_utterances_with_none_values( default_channel, default_tracker, default_domain): endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) response = { "events": [ { "event": "form", "name": "restaurant_form", "timestamp": None }, { "event": "slot", "timestamp": None, "name": "requested_slot", "value": "cuisine", }, ], "responses": [{ "text": None, "buttons": None, "elements": [], "custom": None, "template": "utter_ask_cuisine", "image": None, "attachment": None, }], } nlg = TemplatedNaturalLanguageGenerator( {"utter_ask_cuisine": [{ "text": "what dou want to eat?" }]}) with aioresponses() as mocked: mocked.post("https://example.com/webhooks/actions", payload=response) events = await remote_action.run(default_channel, nlg, default_tracker, default_domain) assert events == [ BotUttered("what dou want to eat?", metadata={"template_name": "utter_ask_cuisine"}), ActiveLoop("restaurant_form"), SlotSet("requested_slot", "cuisine"), ]
def test_nlg_fill_template_text(slot_name: Text, slot_value: Any): template = {"text": f"{{{slot_name}}}"} t = TemplatedNaturalLanguageGenerator(templates=dict()) result = t._fill_template(template=template, filled_slots={slot_name: slot_value}) assert result == {"text": str(slot_value)}