From a55303a6efc9abc0bc864c3c709f0536e87d7fdc Mon Sep 17 00:00:00 2001 From: Anca Lita <27920906+ancalita@users.noreply.github.com> Date: Wed, 20 Sep 2023 13:16:22 +0100 Subject: [PATCH] refactor action_run_slot_rejections --- .../actions/action_run_slot_rejections.py | 65 +++++++++++-------- .../patterns/collect_information.py | 11 ++-- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/rasa/core/actions/action_run_slot_rejections.py b/rasa/core/actions/action_run_slot_rejections.py index 274ffd414052..dbf766f69dbf 100644 --- a/rasa/core/actions/action_run_slot_rejections.py +++ b/rasa/core/actions/action_run_slot_rejections.py @@ -38,6 +38,9 @@ async def run( ) -> List[Event]: """Run the predicate checks.""" events: List[Event] = [] + violation = False + utterance = None + internal_error = False dialogue_stack = DialogueStack.from_tracker(tracker) top_frame = dialogue_stack.top() @@ -69,17 +72,17 @@ async def run( document = current_context.copy() for rejection in top_frame.rejections: - check_text = rejection.if_ + condition = rejection.if_ utterance = rejection.utter - rendered_template = Template(check_text).render(current_context) + rendered_template = Template(condition).render(current_context) predicate = Predicate(rendered_template) try: - result = predicate.evaluate(document) + violation = predicate.evaluate(document) structlogger.debug( "collect.predicate.result", predicate=predicate.description(), - result=result, + violation=violation, ) except (TypeError, Exception) as e: structlogger.error( @@ -88,35 +91,41 @@ async def run( document=document, error=str(e), ) - continue + violation = True + internal_error = True - if result is False: - continue + if violation: + break - # reset slot value that was initially filled with an invalid value - events.append(SlotSet(top_frame.collect_information, None)) + if not violation: + return [] - if utterance is None: - structlogger.debug( - "collect.rejection.missing.utter", - predicate=predicate, - document=document, - ) - break + # reset slot value that was initially filled with an invalid value + events.append(SlotSet(top_frame.collect_information, None)) - message = await nlg.generate( - utterance, - tracker, - output_channel.name(), + if internal_error: + utterance = "utter_internal_error_rasa" + + if utterance is None: + structlogger.error( + "collect.rejection.missing.utter", + utterance=utterance, ) - if message is None: - structlogger.debug( - "collect.rejection.failed.finding.utter", - utterance=utterance, - ) - else: - message["utter_action"] = utterance - events.append(create_bot_utterance(message)) return events + message = await nlg.generate( + utterance, + tracker, + output_channel.name(), + ) + + if message is None: + structlogger.debug( + "collect.rejection.failed.finding.utter", + utterance=utterance, + ) + else: + message["utter_action"] = utterance + events.append(create_bot_utterance(message)) + return events diff --git a/rasa/dialogue_understanding/patterns/collect_information.py b/rasa/dialogue_understanding/patterns/collect_information.py index 31743e1758ce..f56c3bfe2c3d 100644 --- a/rasa/dialogue_understanding/patterns/collect_information.py +++ b/rasa/dialogue_understanding/patterns/collect_information.py @@ -42,14 +42,17 @@ def from_dict(data: Dict[str, Any]) -> CollectInformationPatternFlowStackFrame: Returns: The created `DialogueStackFrame`. """ + rejections = data.get("rejections") + if rejections is not None: + rejections = [ + SlotRejection.from_dict(rejection) for rejection in rejections + ] + return CollectInformationPatternFlowStackFrame( data["frame_id"], step_id=data["step_id"], collect_information=data["collect_information"], - rejections=[ - SlotRejection.from_dict(rejection) - for rejection in data.get("rejections", []) - ], + rejections=rejections, ) def context_as_dict(