Skip to content

Commit

Permalink
make changes for re-ask continuous loop until valid outcome
Browse files Browse the repository at this point in the history
  • Loading branch information
ancalita committed Sep 19, 2023
1 parent 07251a3 commit 138bd07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
18 changes: 15 additions & 3 deletions rasa/core/actions/evaluate_predicate_rejections_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ async def run(
return []

slot_name = top_frame.collect_information
slot_instance = tracker.slots.get(slot_name)
if slot_instance and not slot_instance.has_been_set:
# this is the first time the assistant asks for the slot value,
# therefore we skip the predicate validation because the slot
# value has not been provided
structlogger.debug(
"first.collect.slot.not.set",
slot_name=slot_name,
slot_value=slot_instance.value,
)
return []

slot_value = tracker.get_slot(slot_name)

current_context = dialogue_stack.current_context()
Expand All @@ -65,6 +77,7 @@ async def run(
result = predicate.evaluate(document)
structlogger.debug(
"collect.predicate.result",
predicate=predicate.description(),
result=result,
)
except (TypeError, Exception) as e:
Expand All @@ -79,9 +92,8 @@ async def run(
if result is False:
continue

if current_context.get("number_of_tries", 0) < 2:
# reset slot value that was initially filled with an invalid value
events.append(SlotSet(top_frame.collect_information, None))
# reset slot value that was initially filled with an invalid value
events.append(SlotSet(top_frame.collect_information, None))

if utterance is None:
structlogger.debug(
Expand Down
13 changes: 1 addition & 12 deletions rasa/core/policies/flow_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,14 +576,6 @@ def _run_step(

if action_name in self.domain.action_names_or_texts:
structlogger.debug("flow.step.run.action", context=context)

if step.id == "ask_collect_information":
# increment number of retries to keep track of how many times
# the question has been asked
top_frame = self.dialogue_stack.top()
if isinstance(top_frame, CollectInformationPatternFlowStackFrame):
top_frame.number_of_tries = top_frame.number_of_tries + 1

return PauseFlowReturnPrediction(ActionPrediction(action_name, 1.0))
else:
structlogger.warning("flow.step.run.action.unknown", action=action_name)
Expand Down Expand Up @@ -694,15 +686,12 @@ def trigger_pattern_ask_collect_information(
collect_information: str,
rejections: Optional[List[Dict[Text, Any]]],
) -> None:
context = self.dialogue_stack.current_context().copy()
number_of_tries = context.get("number_of_tries", 0)

"""Trigger the pattern to ask for a slot value."""
slot_value_rejections = rejections if rejections else []

self.dialogue_stack.push(
CollectInformationPatternFlowStackFrame(
collect_information=collect_information,
number_of_tries=number_of_tries,
rejections=slot_value_rejections,
)
)
Expand Down
3 changes: 0 additions & 3 deletions rasa/dialogue_understanding/patterns/collect_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class CollectInformationPatternFlowStackFrame(PatternFlowStackFrame):
collect_information: str = ""
"""The information that should be collected from the user.
this corresponds to the slot that will be filled."""
number_of_tries: int = 0
"""The number of times the question is being asked to fill the slot."""
rejections: Optional[List[Dict[str, Any]]] = None
"""The predicate check that should be applied to the collected information.
If a predicate check fails, its `utter` action indicated under rejections
Expand All @@ -47,7 +45,6 @@ def from_dict(data: Dict[str, Any]) -> CollectInformationPatternFlowStackFrame:
data["frame_id"],
step_id=data["step_id"],
collect_information=data["collect_information"],
number_of_tries=data.get("number_of_tries", 0),
rejections=data.get("rejections"),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ flows:
description: flow used to fill a slot
steps:
- id: "start"
action: action_extract_slots
next:
- if: "{{context.collect_information}} is null and {{context.number_of_tries}} < 2"
then: "ask_collect_information"
- else: "evaluate_predicate_rejections"
- id: "evaluate_predicate_rejections"
action: action_evaluate_predicate_rejections
next: "validate"
- id: "validate"
Expand Down

0 comments on commit 138bd07

Please sign in to comment.