Skip to content

Commit

Permalink
cancelling flows
Browse files Browse the repository at this point in the history
  • Loading branch information
twerkmeister committed Jun 20, 2023
1 parent af2d35e commit d179bd1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
12 changes: 12 additions & 0 deletions rasa/core/policies/default_flows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ responses:
metadata:
rephrase: True

utter_flow_cancelled:
- text: Okay, stopping the flow {rasa_cancelled_flow}.
metadata:
rephrase: True

slots:
confirm_correction:
type: text
Expand Down Expand Up @@ -65,4 +70,11 @@ flows:
next: "3"
- id: "3"
action: utter_not_corrected_previous_input

pattern_cancel_flow:
description: A meta flow that's started when a flow is cancelled.

steps:
- id: "0"
action: utter_flow_cancelled

30 changes: 27 additions & 3 deletions rasa/core/policies/flow_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
POLICY_PRIORITY,
)
from pypred import Predicate
from rasa.shared.constants import FLOW_PREFIX
from rasa.shared.constants import FLOW_PREFIX, CORRECTION_INTENT, CANCEL_FLOW_INTENT
from rasa.shared.nlu.constants import (
ACTION_NAME,
ENTITY_ATTRIBUTE_TYPE,
INTENT_NAME_KEY,
CORRECTION_INTENT,
)
from rasa.shared.core.constants import (
ACTION_LISTEN_NAME,
CORRECTED_SLOTS_SLOT,
FLOW_STACK_SLOT,
PREVIOUS_FLOW_SLOT,
PREVIOUS_FLOW_SLOT, CANCELLED_FLOW_SLOT,
)
from rasa.shared.core.events import ActiveLoop, Event, SlotSet, UserUttered
from rasa.shared.core.flows.flow import (
Expand Down Expand Up @@ -633,6 +632,22 @@ def _find_earliest_updated_question(
return question_step
return None

def should_flow_be_cancelled(self, tracker: DialogueStateTracker) -> bool:
"""Test whether the current flow should be cancelled.
Args:
tracker: the conversation state tracker
Returns:
Whether the current flow should be cancelled
"""
if (
not tracker.latest_message
or tracker.latest_action_name != ACTION_LISTEN_NAME
):
# flows can only be cancelled as a response to a user message
return False
return tracker.latest_message.intent.get(INTENT_NAME_KEY) == CANCEL_FLOW_INTENT

def consider_flow_switch(self, tracker: DialogueStateTracker) -> ActionPrediction:
"""Consider switching to a new flow.
Expand Down Expand Up @@ -671,6 +686,15 @@ def advance_flows(self, tracker: DialogueStateTracker) -> ActionPrediction:
if self.flow_stack.is_empty():
# if there are no flows, there is nothing to do
return ActionPrediction(None, 0.0)
elif self.should_flow_be_cancelled(tracker) and \
not self.flow_stack.is_empty():
top_flow = self.flow_stack.pop()
return ActionPrediction(
FLOW_PREFIX + "pattern_cancel_flow",
1.0,
metadata={"slots": {CANCELLED_FLOW_SLOT: top_flow.flow_id}},
events=[SlotSet(FLOW_STACK_SLOT, self.flow_stack.as_dict())],
)
else:
prediction = self._select_next_action(tracker)
if FlowStack.from_tracker(tracker).as_dict() != self.flow_stack.as_dict():
Expand Down
4 changes: 2 additions & 2 deletions rasa/nlu/classifiers/llm_flow_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
ENTITY_ATTRIBUTE_END,
ENTITY_ATTRIBUTE_TEXT,
ENTITY_ATTRIBUTE_CONFIDENCE,
CORRECTION_INTENT,
)
from rasa.shared.constants import CORRECTION_INTENT, CANCEL_FLOW_INTENT
from rasa.shared.nlu.training_data.message import Message
from rasa.shared.nlu.training_data.training_data import TrainingData
from rasa.utils.llm import (
Expand Down Expand Up @@ -227,7 +227,7 @@ def parse_action_list(
if len(slot_sets) == 0 and not cancel_flow:
return "comment", []
elif len(slot_sets) == 0 and cancel_flow:
return "cancel_flow", []
return CANCEL_FLOW_INTENT, []
elif (
len(slot_sets) == 1
and isinstance(top_flow_step, QuestionFlowStep)
Expand Down
3 changes: 3 additions & 0 deletions rasa/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,6 @@
CHANNEL = "channel"

OPENAI_API_KEY_ENV_VAR = "OPENAI_API_KEY"

CORRECTION_INTENT = "rasa_correction"
CANCEL_FLOW_INTENT = "rasa_cancel_flow"
5 changes: 5 additions & 0 deletions rasa/shared/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
USER_INTENT_BACK,
USER_INTENT_OUT_OF_SCOPE,
USER_INTENT_SESSION_START,
constants.CANCEL_FLOW_INTENT,
constants.CORRECTION_INTENT,
constants.DEFAULT_NLU_FALLBACK_INTENT_NAME,
]

Expand Down Expand Up @@ -81,11 +83,13 @@
REQUESTED_SLOT = "requested_slot"
FLOW_STACK_SLOT = "flow_stack"
PREVIOUS_FLOW_SLOT = "rasa_previous_flow"
CANCELLED_FLOW_SLOT = "rasa_cancelled_flow"
CORRECTED_SLOTS_SLOT = "rasa_corrected_slots"

FLOW_SLOT_NAMES = [
FLOW_STACK_SLOT,
PREVIOUS_FLOW_SLOT,
CANCELLED_FLOW_SLOT,
CORRECTED_SLOTS_SLOT,
]

Expand All @@ -99,6 +103,7 @@
REQUESTED_SLOT,
FLOW_STACK_SLOT,
PREVIOUS_FLOW_SLOT,
CANCELLED_FLOW_SLOT,
CORRECTED_SLOTS_SLOT,
SESSION_START_METADATA_SLOT,
SLOT_LISTED_ITEMS,
Expand Down
1 change: 0 additions & 1 deletion rasa/shared/nlu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@
SPLIT_ENTITIES_BY_COMMA_DEFAULT_VALUE = True
SINGLE_ENTITY_ALLOWED_INTERLEAVING_CHARSET = {".", ",", " ", ";"}

CORRECTION_INTENT = "correction"

0 comments on commit d179bd1

Please sign in to comment.