Skip to content

Commit

Permalink
Merge pull request #11390 from RasaHQ/ATO-219-priority-of-slot-mappin…
Browse files Browse the repository at this point in the history
…g-opposite-from-rasa-2

Fix the priority order of slot mappings in which slots are filled
  • Loading branch information
ancalita authored Jul 27, 2022
2 parents edc99dd + 41dd618 commit 537e655
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelog/11390.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes regression in which slot mappings were prioritized according to reverse order as they were listed in the domain, instead of in order from first to last, as was implicitly expected in `2.x`.
Clarifies this implicit priority order in the docs.
3 changes: 3 additions & 0 deletions docs/docs/domain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ based on the latest user message.
In addition to the predefined mappings, you can define [custom slot mappings](./domain.mdx#custom-slot-mappings).
All custom slot mappings should contain a mapping of type `custom`.

Slot mappings are specified as a YAML list of dictionaries under the key `mappings` in the domain file.
Slot mappings are prioritized in the order they are listed in the domain. The first slot mapping found to apply will be used to fill the slot.

The default behavior is for slot mappings to apply after every user message, regardless of the dialogue context.
To make a slot mapping apply only within the context of a form see [Mapping Conditions](./domain.mdx#mapping-conditions).
There is one additional default limitation on applying `from_entity` slot mappings in the context of a form;
Expand Down
1 change: 1 addition & 0 deletions rasa/core/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ async def run(

if value is not None or tracker.get_slot(slot.name) is not None:
slot_events.append(SlotSet(slot.name, value))
break

should_fill_custom_slot = mapping_type == SlotMappingType.CUSTOM

Expand Down
53 changes: 53 additions & 0 deletions tests/core/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2732,3 +2732,56 @@ async def test_action_extract_slots_emits_necessary_slot_set_events(
assert events[0].value == value_to_set
else:
assert len(events) == 0


async def test_action_extract_slots_priority_of_slot_mappings():
slot_name = "location_slot"
entity_name = "location"
entity_value = "Berlin"

domain_yaml = textwrap.dedent(
f"""
version: "{LATEST_TRAINING_DATA_FORMAT_VERSION}"
intents:
- inform
entities:
- {entity_name}
slots:
{slot_name}:
type: text
influence_conversation: false
mappings:
- type: from_entity
entity: {entity_name}
- type: from_intent
value: 42
intent: inform
responses:
utter_ask_location:
- text: "where are you located?"
"""
)
domain = Domain.from_yaml(domain_yaml)
initial_events = [
UserUttered(
"I am located in Berlin",
intent={"name": "inform"},
entities=[{"entity": entity_name, "value": entity_value}],
),
]
tracker = DialogueStateTracker.from_events(sender_id="test_id", evts=initial_events)

action_extract_slots = ActionExtractSlots(None)

events = await action_extract_slots.run(
CollectingOutputChannel(),
TemplatedNaturalLanguageGenerator(domain.responses),
tracker,
domain,
)
tracker.update_with_events(events, domain=domain)
assert tracker.get_slot("location_slot") == entity_value

0 comments on commit 537e655

Please sign in to comment.