Skip to content

Commit

Permalink
Merge pull request RasaHQ#9991 from RasaHQ/merge-2.8.x-main-5174b23
Browse files Browse the repository at this point in the history
Merge 2.8.x into main
  • Loading branch information
Tayfun Sen authored Oct 29, 2021
2 parents 691d857 + c8f8257 commit 9196c4c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ prepare-transformers:
i=0;\
while read -r URL; do read -r CACHE_FILE; if { [ $(CI) ] && [ $$i -gt 4 ]; } || ! [ $(CI) ]; then wget $$URL -O $$CACHE_DIR/$$CACHE_FILE; fi; i=$$((i + 1)); done < "data/test/hf_transformers_models.txt"

prepare-tests-files: prepare-spacy prepare-mitie prepare-transformers
prepare-tests-files: prepare-spacy prepare-mitie install-mitie prepare-transformers

prepare-wget-macos:
brew install wget || true
Expand Down
3 changes: 3 additions & 0 deletions changelog/9949.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed new intent creation in `rasa interactive` command. Previously, this failed with 500
from the server due to `UnexpecTEDIntentPolicy` trying to predict with the new intent not in
domain.
3 changes: 3 additions & 0 deletions changelog/9982.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Install mitie library when preparing test runs. This step was missing before
and tests were thus failing as we have many tests which rely on mitie library.
Previously, `make install-full` was required.
18 changes: 12 additions & 6 deletions rasa/core/policies/unexpected_intent_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,14 @@ def predict_action_probabilities(

# Prediction through the policy is skipped if:
# 1. If the tracker does not contain any event of type `UserUttered`
# till now.
# till now or the intent of such event is not in domain.
# 2. There is at least one event of type `ActionExecuted`
# after the last `UserUttered` event.
if self._should_skip_prediction(tracker):
if self._should_skip_prediction(tracker, domain):
logger.debug(
f"Skipping predictions for {self.__class__.__name__} "
f"as either there is no event of type `UserUttered` or "
f"as either there is no event of type `UserUttered`, "
f"event's intent is new and not in domain or "
f"there is an event of type `ActionExecuted` after "
f"the last `UserUttered`."
)
Expand Down Expand Up @@ -628,14 +629,17 @@ def predict_action_probabilities(
)

@staticmethod
def _should_skip_prediction(tracker: DialogueStateTracker) -> bool:
def _should_skip_prediction(tracker: DialogueStateTracker, domain: Domain,) -> bool:
"""Checks if the policy should skip making a prediction.
A prediction can be skipped if:
1. There is no event of type `UserUttered` in the tracker.
2. There is an event of type `ActionExecuted` after the last
2. If the `UserUttered` event's intent is new and not in domain
(a new intent can be created from rasa interactive and not placed in
domain yet)
3. There is an event of type `ActionExecuted` after the last
`UserUttered` event. This is to prevent the dialogue manager
from getting stuck in a prediction loop.
from getting stuck in a prediction loop.
For example, if the last `ActionExecuted` event
contained `action_unlikely_intent` predicted by
`UnexpecTEDIntentPolicy` and
Expand All @@ -653,6 +657,8 @@ def _should_skip_prediction(tracker: DialogueStateTracker) -> bool:
if isinstance(event, ActionExecuted):
return True
elif isinstance(event, UserUttered):
if event.intent_name not in domain.intents:
return True
return False
# No event of type `ActionExecuted` and `UserUttered` means
# that there is nothing for `UnexpecTEDIntentPolicy` to predict on.
Expand Down
63 changes: 58 additions & 5 deletions tests/core/policies/test_unexpected_intent_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,17 +634,17 @@ def test_skip_predictions_to_prevent_loop(
should_skip: bool,
tmp_path: Path,
):
caplog.set_level(logging.DEBUG)
"""Skips predictions to prevent loop."""
loaded_policy = self.persist_and_load_policy(
trained_policy, model_storage, resource, execution_context
)
precomputations = None
tracker = DialogueStateTracker(sender_id="init", slots=default_domain.slots)
tracker.update_with_events(tracker_events, default_domain)

prediction = loaded_policy.predict_action_probabilities(
tracker, default_domain, precomputations
)
with caplog.at_level(logging.DEBUG):
prediction = loaded_policy.predict_action_probabilities(
tracker, default_domain, precomputations
)

assert (
"Skipping predictions for UnexpecTEDIntentPolicy" in caplog.text
Expand All @@ -655,6 +655,59 @@ def test_skip_predictions_to_prevent_loop(
default_domain
)

@pytest.mark.parametrize(
"tracker_events",
[
[
ActionExecuted("action_listen"),
UserUttered("hi", intent={"name": "inexistent_intent"}),
],
[
ActionExecuted("action_listen"),
UserUttered("hi", intent={"name": "inexistent_intent"}),
EntitiesAdded([{"name": "dummy"}]),
],
[
ActionExecuted("action_listen"),
UserUttered("hi", intent={"name": "inexistent_intent"}),
SlotSet("name"),
],
[
ActiveLoop("loop"),
ActionExecuted("action_listen"),
UserUttered("hi", intent={"name": "inexistent_intent"}),
ActionExecutionRejected("loop"),
],
],
)
def test_skip_predictions_if_new_intent(
self,
trained_policy: UnexpecTEDIntentPolicy,
model_storage: ModelStorage,
resource: Resource,
execution_context: ExecutionContext,
default_domain: Domain,
caplog: LogCaptureFixture,
tracker_events: List[Event],
):
"""Skips predictions if there's a new intent created."""
loaded_policy = self.persist_and_load_policy(
trained_policy, model_storage, resource, execution_context
)
tracker = DialogueStateTracker(sender_id="init", slots=default_domain.slots)
tracker.update_with_events(tracker_events, default_domain)

with caplog.at_level(logging.DEBUG):
prediction = loaded_policy.predict_action_probabilities(
tracker, default_domain, precomputations=None,
)

assert "Skipping predictions for UnexpecTEDIntentPolicy" in caplog.text

assert prediction.probabilities == loaded_policy._default_predictions(
default_domain
)

@pytest.mark.parametrize(
"tracker_events_with_action, tracker_events_without_action",
[
Expand Down

0 comments on commit 9196c4c

Please sign in to comment.