Teacher Gavin's Transformer Live Lessons - Rasa Dialogue Robot Project Practical Education Bot Project FormValidationAction Mechanism and Source Code Decryption Line by Line (76)

   This article continues to focus on the industrial-level business dialogue platform and framework Rasa, and analyzes in detail how to use FormValidationAction in the Education Bot project in the education field of the Rasa dialogue robot project to customize the form validation action, and the architecture and application practice of FormValidationAction and ValidationAction.

1. The inside mechanism of the FormValidationAction of the Education Bot project in the education field of the Rasa dialogue robot project and the source code decryption line by line

  1. ValidationAction Architecture Analysis of Action Based on Rasa Microservices

In the Education Bot project, custom actions to validate the form are used. These actions inherit from FormValidationAction, which simplifies the process of validating slots. In the action of a custom validation form, a method named validate_<slot_name> needs to be written for each extracted slot. In the following example, you can see that the method validate_email is defined to verify the email information entered by the user:

Here is a custom validation action for another form with the method validate_business_email defined:

ValidationAction extracts and validates around slots, while FormValidationAction validates the collected information for the form, which can be regarded as a sub-framework of the validation framework provided by ValidationAction.

 2. Line-by-line analysis of the code for extracting slots information in ValidationAction

Call the method extract_<slot_name> in the method get_extraction_events to extract custom slots.

Parameters include dispatcher, tracker, domain

Return type: list of SlotSet

In the following code, first obtain the slot information defined by key required_slots from the form definition that currently needs to be verified in the domain, then loop the method _extract_slot to extract the slot and update the tracker state, and finally return the SlotSet event:

The following is the method of extracting a slot. First, obtain the corresponding extract_<slot_name> method according to the slot name. If there is no such method and the slot does not exist in the domain, a prompt is given and an empty result is returned; if a method is found, the extraction is called. The method gets the slot, and if the result is of type Dict, it returns the result, otherwise it returns an empty result:

 3. Line-by-line analysis of the validation operation source code in ValidationAction

This is how the validation is performed in the run method of the ValidationAction:

Execute the validation operation and return the event in the following method:

First, according to the required_slots in the domain to obtain the information of the slots to be verified and assign it to the variable slots_to_validate, then obtain the information of the slots that need to be verified from the tracker and loop to check whether the slots obtained from the tracker exist in the slots_to_validate, if not, skip the verification operation, otherwise call the method validate_<slot_name> for validation and return the result:

 4. Line-by-line analysis of the source code of the run method in ValidationAction

Regarding the run method, the code for extracting the slot defined in the method extract_<slot name> can be executed, and then the tracker is updated with the returned events. The Run method can also execute the validation code defined in the method validate_<slot name>, and then add the returned events to the tracker.

parameter:

-dispatcher: dispatcher is used to send messages to users. The usage is dispatcher.utter_message() or refer to CollectingDispatcher related API instructions

-tracker: The status tracker of the current user, you can use tracker.get_slot(slot_name) to access the value of slots, and you can use tracker.latest_message.text to get the latest user messages from tracker.

-domain: the domain system configuration of the chatbot

Returns a list of events of type List[Dict[str, Any]]

 5. Analysis of the domain_slots method code of FormValidationAction line by line

In the method domain_slots, the parameter domain is passed in, and the form is obtained from the forms part of the domain according to the form_name method of the FormValidationAction, and then it is judged that if the key required_slots exists, it returns the slots list, otherwise it returns an empty list:

 6. The _extract_validation_events code of FormValidationAction is analyzed line by line

This is the _extract_validation_events method. The incoming parameters include dispatcher, tracker, and domain. First, the method get_validation_events is called. In this method, each slot is verified, and then the validation events are returned, and then these events are added to the tracker. Next Call the method next_requested_slot to set the next slot that needs to be requested from the user:

 7. The next_requested_slot code of FormValidationAction is analyzed line by line

This is the method next_requested_slot, which is used to set the next slot that needs to be requested from the user. In the method, it will be judged that if the user does not override the required_slots method (usually used in dynamic form behavior scenarios), then Rasa will be asked to use FormAction according to the form definition. The slots specified by the required_slot key sequentially request the next slot from the user:

 8. ValidationAction in Rasa document is parsed sentence by sentence

ValidationAction is the parent class for all custom actions that perform extraction and validation of slots that can be set or updated outside of a form context. To implement custom slot extraction and validation logic, you can inherit from ValidationAction or FormValidationAction. Depends on whether you want to set or update slots based on a form context.

 The ValidationAction class is used to extract slots outside of a form context. It ignores the extraction and validation methods of any slots in a form context (specified by slot mapping's conditions). ValidationAction will not run these methods when the specified form is active, nor will these methods run when no form is active. In order to apply custom slot mappings in the form context, you need to extend FormValidationAction.

 How to inherit ValidationAction:

- The action name "action_validate_slot_mappings" must be added to the action list of the domain configuration. The name method does not need to be implemented in the custom action that inherits ValidationAction. Since the name method has been implemented in ValidationAction, the name used in the method is hardcoded when the action server calls the default action "action_extract_slots", so if you override this method in a custom action, it will cause This custom action will not be run.

You should only create a custom action that inherits ValidationAction, and then create all the slot extraction and validation methods for different slots in this action according to your business scenario. There is no need to specify the action key in the custom slot mapping, because the default action "action_extract_slots" will automatically run the action "action_validate_slot_mappings", provided that the action is added to the domain's actions (that is, registered in the domain).

Regarding the validation of slots in predefined mappings:

In order to validate the slots that appear in the predefined mappings, you must create a method named "validate_<slot_name>". The following is an action that customizes the validation of slots. In the validation method, check whether the value extracted from the slot "location" is str, and if so, perform the corresponding operation on the value:

About the extraction of slots in custom slot mappings:

A method needs to be created for each slot in the slot mapping, the method name is extract_<slot_name>. The following example shows how a custom action can extract values ​​from the slot "count_of_insults":

                                          

 9. FormValidationAction in Rasa document is parsed sentence by sentence

A custom action of a FormValidationAction will only run when the form is activated. If a form-based context needs to extract slots from custom slot mapping and/or validate slots, the custom action needs to inherit methods from FormValidationAction instead of ValidationAction.

As long as the form is active, the custom action inherited from FormValidationAction will run on each user turn, so there is no need to use mapping conditions.

FormValidationAction inherits from ValidationAction and the Python abstract interface ABC. FormValidationAction inherits most of the methods of ValidationAction, but overrides the name and domain_slots methods, and implements a new method next_requested_slot to extend the implementation of the run method.

Guess you like

Origin blog.csdn.net/m0_49380401/article/details/123451800
Recommended