Skip to content

Commit

Permalink
refactor action_run_slot_rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
ancalita committed Sep 20, 2023
1 parent 2523e6f commit a55303a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
65 changes: 37 additions & 28 deletions rasa/core/actions/action_run_slot_rejections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand All @@ -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
11 changes: 7 additions & 4 deletions rasa/dialogue_understanding/patterns/collect_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a55303a

Please sign in to comment.