Teacher Gavin's Transformer live class perception - Rasa dialogue robot project actual combat education field Education Bot project Form analysis and custom full solution (75)

   This article continues to focus on the industrial-level business dialogue platform and framework Rasa, and the definition of Form in the Education Bot project of the Rasa dialogue robot project in the field of education, how to activate a form or make the currently running form into an inactive state, and how to use slots in the form , and how to verify the information collected by the form, and conduct a detailed analysis of the behavior of the dynamic form.

 1. Insider analysis and custom full solution of the Education Bot project Form in the field of education of the Rasa dialogue robot project

  1. Education Bot project Form usage analysis

Form plays a central role in the development of Rasa's conversational bot, which is currently the best way for Rasa's conversational bot to collect user information.

The following is the definition of a form used by the project. You can see that the information to be collected is defined in required_slots:

In addition, the dialog flow can also be controlled through the form. The form's runtime priority is higher than the rules, and the required content can be placed in the form to ensure that it runs in the expected way.

 2. Definition and activation analysis of Rasa form and case analysis

Form is defined in the domain file, and the name of the form is also the name of the action used to execute the form in stories or rules. You need to specify the slots that the form needs to collect in required_slots. The following is a sample form definition:

You can use key ignored_intents to define a list of intents that need to be ignored by the form. The intents defined in this key will be added to the key not_intent of each slot mapping.

Usually rules are used to activate the form and submit the form. The following rule is used to activate the form. The steps are to execute the action "utter_ask_playground_help" first, and then ask the user to confirm. After the confirmation, the action "playground_form" will be executed to activate the form:

The following rule defines how to submit a form. The condition is that the form needs to be active. After submitting, you need to set active_loop to null:

In order to use the forms provided by Rasa, you need to make sure that RulePolicy is added to the policies configuration:

When the form is activated through an action, it will use one of the following two formats of response to request the user to provide the slot information that the form is requesting:

      utter_ask_<form_name>_<slot_name>

utter_ask_<slot_name>

 3. Deactivating a form analysis and best practices

Once the information about the slots required by a form is collected, the form will automatically enter an inactive state. A rule or story can be used to describe the behavior of the chatbot when the form terminates. If this is not done, the form will enter a state of listening for the user's next message after it stops running. The following example shows that the actions utter_submit and utter_slots_values ​​are executed after the form has finished running:

 4. Analysis and case analysis of Writing stories/rules for unhappy form paths

When the user does not always provide information as required by the dialogue robot, the execution of the form action will be rejected, and the form will automatically throw an exception ActionExecutionRejection. The following are possible scenarios for this situation:

- A slot was requested, but the user did not populate the slot with the latest messages, and you did not define a custom action for slot validation or extraction

- A slot was requested, but the custom action for slot validation or extraction did not return any SlotSet events

To explicitly reject form execution, you can have your custom action return the ActionExecutionRejected event. In order to deal with the situation that the form refuses to execute, you can deal with it in the rules or stories, such as the following example:

Another situation is that when the user decides not to continue processing the business when the form collects information, the form should stop running. The specific method is to use a default action action_deactivate_loop to deactivate the form and reset the requested slot. An example is as follows:

It is strongly recommended to use the interactive learning method provided by Rasa to build these rules or stories. If you write them yourself, you may miss important content.

 5. Analysis of Form slot mappings

Since Rasa 3.0 starts, all slots are defined in the slots section of the domain file, so the same slot mapping can be used across multiple forms, thereby removing unnecessary repetitions. The following is a slot mapping used by the project, you can Seeing that conditions are added, the slot will be filled with the value of entity only when the form is running:

 6. Validating form input analysis and case analysis

Validation is required when extracting slot values ​​from user input. By default, Rasa only validates those slots that have been filled with values. You can implement a custom validation action to validate any extracted slots, make sure this action is added to the domain file:

This custom action can be inherited from FormValidationAction, which simplifies the process of validating slots. In this case, a validate_<slot_name> method needs to be written for each extracted slot. The following validation method example shows how to validate the slot, that is, the value can only be set if the value of the slot exists in the database, otherwise it is set to None:

 7. Custom slot mappings analysis and case analysis

If there is no predefined slot mapping to meet your needs, you can use the custom action validate_<form_name> to write your own slot extraction code logic, and Rasa will trigger this action when the form is running. If you use Rasa SDK, it is recommended to inherit from FormValidationAction. In order to extract custom slots, you need to complete the following steps:

- Define a method for each slot in the slot mapping using the custom type: extract_<slot_name>

-In the key required_slots in the form definition of the domain file, list all the slots that use predefined and custom slot mappings

The following example shows how to extract slot information based on the outdoor information contained in the user input text:

 8. Dynamic form behavior analysis and case analysis

By default, Rasa uses the slots listed in the form definition in the domain to sequentially request slot information that has not been filled with values ​​from the user. If you use custom slot mappings and FormValidationAction, Rasa will request information from the user based on the first empty slot returned by the method required_slots. You can dynamically add the requested slots by overriding the method required_slots. For example, when you need to obtain more content based on the information of the previously requested slots, you can also change the request order of the slots by overriding this method.

You need to define a method extract_<slot name> for each slot that does not use the predefined slot mapping. The following example demonstrates how to control whether the information of another slot needs to be requested according to the value of one slot:

If you want to remove a slot from required_slots in the form definition in domain, you should first assign the value of domain_slots in the above example to another new variable, and then remove the slot on the basis of this variable, instead of Go and modify domain_slots directly, as doing so may cause unexpected behavior, for example:

 9. Requested_slot slot analysis and case analysis

Requested_slot is a text-type slot, which will be automatically added to the domain. The value of requested_slot will be ignored by default in the dialog. If you want to change this behavior, you need to add requested_slot to the domain file, the type is categorical, and the attribute influence_conversation Set to true. If you want to handle unexpected situations based on the slot of the current request, such as the following example, when the user responds to the slot request of the current dialog with other questions:

 10. Custom action to ask for the next slot analysis and case analysis

Once the form determines the next slot that needs to be collected from the user, the relevant action utter_ask_xxx will be executed. If this conventional practice cannot meet the requirements, you can also use a custom action such as action_ask_<form_name>_<slot_name> or action_ask_<slot_name > to request the next slot from the user, for example:

Guess you like

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