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 prediction for rules with multiple entities #8446

Merged
merged 5 commits into from
Apr 13, 2021
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
1 change: 1 addition & 0 deletions changelog/8446.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed prediction for rules with multiple entities.
29 changes: 18 additions & 11 deletions rasa/core/policies/rule_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,26 +804,33 @@ def train(
def _does_rule_match_state(rule_state: State, conversation_state: State) -> bool:
for state_type, rule_sub_state in rule_state.items():
conversation_sub_state = conversation_state.get(state_type, {})
for key, value in rule_sub_state.items():
if isinstance(value, list):
# json dumps and loads tuples as lists,
# so we need to convert them back
value = tuple(value)
for key, value_from_rules in rule_sub_state.items():
# json dumps and loads tuples as lists
if isinstance(value_from_rules, list):
# sort values before comparing
# `sorted` returns a list
value_from_rules = sorted(value_from_rules)

value_from_conversation = conversation_sub_state.get(key)
if isinstance(value_from_conversation, tuple):
# sort values before comparing
# `sorted` returns a list
value_from_conversation = sorted(value_from_conversation)

if (
# value should be set, therefore
# check whether it is the same as in the state
value
and value != SHOULD_NOT_BE_SET
and conversation_sub_state.get(key) != value
value_from_rules
and value_from_rules != SHOULD_NOT_BE_SET
and value_from_conversation != value_from_rules
) or (
# value shouldn't be set, therefore
# it should be None or non existent in the state
value == SHOULD_NOT_BE_SET
and conversation_sub_state.get(key)
value_from_rules == SHOULD_NOT_BE_SET
and value_from_conversation
# during training `SHOULD_NOT_BE_SET` is provided. Hence, we also
# have to check for the value of the slot state
and conversation_sub_state.get(key) != SHOULD_NOT_BE_SET
and value_from_conversation != SHOULD_NOT_BE_SET
):
return False

Expand Down
88 changes: 69 additions & 19 deletions tests/core/policies/test_rule_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
RULE_ONLY_SLOTS,
RULE_ONLY_LOOPS,
)
from rasa.shared.nlu.constants import TEXT, INTENT, ACTION_NAME
from rasa.shared.nlu.constants import TEXT, INTENT, ACTION_NAME, ENTITY_ATTRIBUTE_TYPE
from rasa.shared.core.domain import Domain
from rasa.shared.core.events import (
ActionExecuted,
Expand Down Expand Up @@ -2101,9 +2101,7 @@ def test_hide_rule_turn():
],
)
policy = RulePolicy()
policy.train(
[GREET_RULE, chitchat_story], domain, RegexInterpreter(),
)
policy.train([GREET_RULE, chitchat_story], domain, RegexInterpreter())

conversation_events = [
ActionExecuted(ACTION_LISTEN_NAME),
Expand Down Expand Up @@ -2664,9 +2662,7 @@ def test_remove_action_listen_prediction_if_contradicts_with_story():
],
)
policy = RulePolicy()
policy.train(
[rule, story], domain, RegexInterpreter(),
)
policy.train([rule, story], domain, RegexInterpreter())
prediction_source = [{PREVIOUS_ACTION: {ACTION_NAME: utter_1}}]
key = policy._create_feature_key(prediction_source)
assert key not in policy.lookup[RULES]
Expand Down Expand Up @@ -2718,9 +2714,7 @@ def test_keep_action_listen_prediction_after_predictable_action():
# prediction of action_listen should only be removed if it occurs after the first
# action (unpredictable)
with pytest.raises(InvalidRule):
policy.train(
[rule, story], domain, RegexInterpreter(),
)
policy.train([rule, story], domain, RegexInterpreter())


def test_keep_action_listen_prediction_if_last_prediction():
Expand Down Expand Up @@ -2762,9 +2756,7 @@ def test_keep_action_listen_prediction_if_last_prediction():
policy = RulePolicy()
# prediction of action_listen should only be removed if it's not the last prediction
with pytest.raises(InvalidRule):
policy.train(
[rule, story], domain, RegexInterpreter(),
)
policy.train([rule, story], domain, RegexInterpreter())


def test_keep_action_listen_prediction_if_contradicts_with_rule():
Expand Down Expand Up @@ -2807,9 +2799,7 @@ def test_keep_action_listen_prediction_if_contradicts_with_rule():
)
policy = RulePolicy()
with pytest.raises(InvalidRule):
policy.train(
[rule, other_rule], domain, RegexInterpreter(),
)
policy.train([rule, other_rule], domain, RegexInterpreter())


def test_raise_contradiction_if_rule_contradicts_with_story():
Expand Down Expand Up @@ -2850,6 +2840,66 @@ def test_raise_contradiction_if_rule_contradicts_with_story():
)
policy = RulePolicy()
with pytest.raises(InvalidRule):
policy.train(
[rule, story], domain, RegexInterpreter(),
)
policy.train([rule, story], domain, RegexInterpreter())


def test_rule_with_multiple_entities():
intent_1 = "intent_1"
entity_1 = "entity_1"
entity_2 = "entity_2"
utter_1 = "utter_1"
domain = Domain.from_yaml(
f"""
version: "2.0"
intents:
- {intent_1}
entities:
- {entity_1}
- {entity_2}
actions:
- {utter_1}
"""
)

rule = TrackerWithCachedStates.from_events(
"rule without action_listen",
domain=domain,
slots=domain.slots,
evts=[
ActionExecuted(RULE_SNIPPET_ACTION_NAME),
ActionExecuted(ACTION_LISTEN_NAME),
UserUttered(
intent={"name": intent_1},
entities=[
{ENTITY_ATTRIBUTE_TYPE: entity_1},
{ENTITY_ATTRIBUTE_TYPE: entity_2},
],
),
ActionExecuted(utter_1),
ActionExecuted(ACTION_LISTEN_NAME),
],
is_rule_tracker=True,
)
policy = RulePolicy()
policy.train([rule], domain, RegexInterpreter())

Ghostvv marked this conversation as resolved.
Show resolved Hide resolved
# the order of entities in the entities list doesn't matter for prediction
conversation_events = [
ActionExecuted(ACTION_LISTEN_NAME),
UserUttered(
"haha",
intent={"name": intent_1},
entities=[
{ENTITY_ATTRIBUTE_TYPE: entity_2},
{ENTITY_ATTRIBUTE_TYPE: entity_1},
],
),
]
prediction = policy.predict_action_probabilities(
DialogueStateTracker.from_events(
"casd", evts=conversation_events, slots=domain.slots
),
domain,
RegexInterpreter(),
)
assert_predicted_action(prediction, domain, utter_1)