Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the priority order of slot mappings in which slots are filled #11390

Merged
merged 3 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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