Skip to content

Commit

Permalink
Merge pull request #12268 from RasaHQ/ATO-919-define-intent-as-list-i…
Browse files Browse the repository at this point in the history
…n-from-intent-sm-3.5.x

Allow intents as list in from_intent in slot mapping
  • Loading branch information
Tawakalt authored Apr 13, 2023
2 parents cffd901 + 86605ac commit 21bbac1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/12268.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow slot mapping parameter `intent` to accept a list of intent names (as strings), in addition to accepting an intent name as a single string.
16 changes: 9 additions & 7 deletions rasa/shared/core/slot_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ def check_mapping_validity(
if (
mapping_type == SlotMappingType.FROM_INTENT
and mapping.get(INTENT) is not None
and mapping.get(INTENT) not in domain.intents
):
rasa.shared.utils.io.raise_warning(
f"Slot '{slot_name}' uses a 'from_intent' mapping for "
f"a non-existent intent '{mapping.get('intent')}'. "
f"Skipping slot extraction because of invalid mapping."
)
return False
intent_list = SlotMapping.to_list(mapping.get(INTENT))
for intent in intent_list:
if intent and intent not in domain.intents:
rasa.shared.utils.io.raise_warning(
f"Slot '{slot_name}' uses a 'from_intent' mapping for "
f"a non-existent intent '{mapping.get('intent')}'. "
f"Skipping slot extraction because of invalid mapping."
)
return False

return True

Expand Down
84 changes: 84 additions & 0 deletions tests/shared/core/test_slot_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,87 @@ def test_slot_mappings_check_mapping_validity_from_intent():
mapping=mappings_for_slot[0],
domain=domain,
)


@pytest.mark.parametrize(
"intent, expected",
[
(["goodbye", "mood_great", "greet"], True),
([], True),
("", True),
({}, True),
("null", True),
],
)
def test_slot_mappings_check_mapping_validity_valid_intent_list(
intent: Text, expected: bool
):
slot_name = "mood"
domain = Domain.from_yaml(
f"""
version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
intents:
- greet
- goodbye
- mood_great
- mood_unhappy
slots:
{slot_name}:
type: any
influence_conversation: false
mappings:
- type: from_intent
value: "testing 123"
intent: {intent}
forms:
test_form:
required_slots:
- test_slot
"""
)
mappings_for_slot = domain.as_dict().get("slots").get(slot_name).get("mappings")
assert (
SlotMapping.check_mapping_validity(
slot_name=slot_name,
mapping_type=SlotMappingType.FROM_INTENT,
mapping=mappings_for_slot[0],
domain=domain,
)
is expected
)


def test_slot_mappings_check_mapping_validity_invalid_intent_list():
slot_name = "mood"
domain = Domain.from_yaml(
f"""
version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
intents:
- greet
- goodbye
- mood_great
- mood_unhappy
slots:
{slot_name}:
type: any
influence_conversation: false
mappings:
- type: from_intent
value: "testing 123"
intent:
- aaaa
- bbbb
- cccc
forms:
test_form:
required_slots:
- test_slot
"""
)
mappings_for_slot = domain.as_dict().get("slots").get(slot_name).get("mappings")
assert not SlotMapping.check_mapping_validity(
slot_name=slot_name,
mapping_type=SlotMappingType.FROM_INTENT,
mapping=mappings_for_slot[0],
domain=domain,
)

0 comments on commit 21bbac1

Please sign in to comment.