Skip to content

Commit

Permalink
Merge pull request #276 from RasaHQ/form-slot-validation
Browse files Browse the repository at this point in the history
Form slot validation for 1st sentence
  • Loading branch information
wochinge authored Oct 5, 2020
2 parents 5ff0dcf + 5f9417b commit df94fb8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions changelog/238.feature.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Added the method ``form_slots_to_validate`` to ``Tracker``. This method is helpful
Added the method ``slots_to_validate`` to ``Tracker``. This method is helpful
when using a custom action to validate slots which were extracted by a Form as shown
by the following example.

Expand All @@ -11,7 +11,7 @@ by the following example.
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
extracted_slots: Dict[Text, Any] = tracker.form_slots_to_validate()
extracted_slots: Dict[Text, Any] = tracker.slots_to_validate()
validation_events = []
Expand Down
17 changes: 6 additions & 11 deletions rasa_sdk/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,33 +234,28 @@ def undo_till_previous(event_type: Text, done_events: List[Dict[Text, Any]]):
applied_events.append(event)
return applied_events

def form_slots_to_validate(self) -> Dict[Text, Any]:
"""Get form slots which need validation.
def slots_to_validate(self) -> Dict[Text, Any]:
"""Get slots which were recently set.
You can use a custom action to validate slots which were extracted during the
latest form execution. This method provides you all extracted candidates for
form slots.
This can e.g. be used to validate form slots after they were extracted.
Returns:
A mapping of extracted slot candidates and their values.
"""

slots_to_validate: Dict[Text, Any] = {}

if not self.active_loop:
return slots_to_validate
slots: Dict[Text, Any] = {}

for event in reversed(self.events):
# The `FormAction` in Rasa Open Source will append all slot candidates
# at the end of the tracker events.
if event["event"] == "slot":
slots_to_validate[event["name"]] = event["value"]
slots[event["name"]] = event["value"]
else:
# Stop as soon as there is another event type as this means that we
# checked all potential slot candidates.
break

return slots_to_validate
return slots


class Action:
Expand Down
7 changes: 5 additions & 2 deletions tests/test_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_get_extracted_slots(
):
tracker = get_tracker(events)
tracker.active_loop = {"name": "my form"}
assert tracker.form_slots_to_validate() == expected_extracted_slots
assert tracker.slots_to_validate() == expected_extracted_slots


def test_get_extracted_slots_with_no_active_loop():
Expand All @@ -129,4 +129,7 @@ def test_get_extracted_slots_with_no_active_loop():
]
tracker = get_tracker(events)

assert tracker.form_slots_to_validate() == {}
assert tracker.slots_to_validate() == {
"some_other": "some_value2",
"my_slot": "some_value",
}

0 comments on commit df94fb8

Please sign in to comment.