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

Cancel initiated slot correction - [ENG 414] #12718

Closed
wants to merge 7 commits into from
Closed
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
61 changes: 61 additions & 0 deletions docs/docs/llms/flow-policy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,67 @@ prior input. The `FlowPolicy` depends on another component, like the
and set the corresponding slots. Based on this, the `FlowPolicy` initiates the
correction flow.

### Cancelling an initiated correction

Users have the option to cancel a correction initiated by them by adjusting the default
flow for pattern correction. The provided example demonstrates how to modify the
default flow effectively. The confirm_correction question can be used to confirm if
the user wants to accept the correction that was initiated. A custom action is
used to set the values of the slot before correction.

````yaml title="flows.yml"
flows:
pattern_correction:
description: A meta flow that should be started to correct a previous user input.

steps:
- id: "0"
question: confirm_correction
next:
- if: confirm_correction
then: "1"
- else: "2"
- id: "1"
action: utter_corrected_previous_input
- id: "2"
action: action_cancel_correction
next: "3"
- id: "3"
action: utter_not_corrected_previous_input
```

```python
from typing import Any, Dict
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher


class CancelCorrectionTemplate(Action):

def name(self) -> str:
return "action_cancel_correction"

def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker, domain: Dict[str, Any]):
events = []
CORRECTED_SLOTS_SLOT = "rasa_corrected_slots"
corrected_slots = tracker.get_slot(CORRECTED_SLOTS_SLOT)
if corrected_slots and tracker.latest_message.intent["name"] == "deny":
for corrected_slot in corrected_slots:
found_first_event = False
for event in reversed(tracker.events):
if isinstance(event, SlotSet) and event.key == corrected_slot:
if not found_first_event:
found_first_event = True
else:
events.append(SlotSet(event.key, event.value))
break
events.append(SlotSet(CORRECTED_SLOTS_SLOT, None))
return events

```

## Configuration

No additional configuration is required for the FlowPolicy beyond what's already
Expand Down
33 changes: 0 additions & 33 deletions rasa/core/policies/default_flows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,11 @@ responses:
- text: Let's continue with the topic {rasa_previous_flow}.
metadata:
rephrase: True

utter_ask_confirm_correction:
- text: "Do you want to update your information?"
buttons:
- payload: yes
title: Yes
- payload: no
title: No, please keep the previous information
metadata:
rephrase: True

utter_corrected_previous_input:
- text: "Ok, I corrected the previous input."
metadata:
rephrase: True

utter_not_corrected_previous_input:
- text: "Ok, I did not correct the previous input."
metadata:
rephrase: True

utter_flow_cancelled_rasa:
- text: Okay, stopping the flow {rasa_cancelled_flow}.
Expand All @@ -36,12 +21,6 @@ responses:
utter_internal_error_rasa:
- text: Sorry, I'm having trouble understanding you right now. Please try again later.

slots:
confirm_correction:
type: bool
mappings:
- type: custom

flows:
pattern_continue_interrupted:
description: A meta flow that should be started to continue an interrupted flow.
Expand All @@ -55,19 +34,7 @@ flows:

steps:
- id: "0"
question: confirm_correction
next:
- if: confirm_correction
then: "1"
- else: "2"
- id: "1"
action: utter_corrected_previous_input
- id: "2"
set_slots:
varunshankar marked this conversation as resolved.
Show resolved Hide resolved
- rasa_corrected_slots: None
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.
Expand Down
7 changes: 1 addition & 6 deletions rasa/core/policies/flow_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,12 @@ def _run_step(
and previous_flow_step
and current_frame.frame_type == StackFrameType.CORRECTION
):
# TODO: we need to figure out how to actually
# "undo" the changed slots
corrected_slots = tracker.get_slot(CORRECTED_SLOTS_SLOT)
if corrected_slots:
self._correct_flow_position(
corrected_slots, previous_flow_step, previous_flow, tracker
)
else:
# TODO: we need to figure out how to actually "undo" the
# changed slots
pass

return ActionPrediction(None, 0.0, events=events)
else:
raise FlowException(f"Unknown flow step type {type(step)}")
Loading