Error handling and re-prompts¶
While talking with the AI agent, the user may say something unexpected, and the AI agent will be unable to proceed with the conversation. To handle error situations gracefully and get the user back on track, you can use the fallback() function.
Fallbacks can help you to:
Customizing default error messages¶
If the AI agent fails to recognize the user’s input, Alan AI replies with one of the default error phrases. You can provide a set of custom phrases with which Alan AI must reply in no-match cases. To do this, add the fallback()
function to the dialog script and specify one or more phrases in it.
For example:
fallback('Please say it again', 'Sorry, I did not get it this time', 'Could you please rephrase that?');
Now, if the user gives a command that is not available in the dialog script or cannot be recognized, this default fallback will be matched, and Alan AI will reply with one of the defined phrases picked at random.
You can add more complex logic to the fallback()
function. For example, you can use the p.inputText
object to capture the user input. For details, see inputText.
fallback(p => {
p.play(`Sorry, I have no answer to the question: ${p.inputText}.`);
console.log('An error in the dialog is encountered');
});
Prompting for the correct input¶
When placed in user-defined contexts, fallbacks provide a user-friendly way to ‘lock’ the user at a certain point in the dialog. They can be helpful if you expect a specific input from the user and cannot move forward in the dialog without it.
To handle an error, add the fallback()
function to the context and specify one or more prompt phrases in it. If the user’s input cannot be matched to any command in the context, Alan AI will prompt the user for the correct input using one of the defined phrases and continue re-prompting until the required command is obtained.
Note
Fallbacks in user-defined contexts are context-dependent — they are active only when contexts to which they are added are activated.
Check the chooseDish
context in the example below:
intent('What do you have?', p => {
p.play('We have pizza and burgers');
p.then(chooseDish);
});
let chooseDish = context(() => {
intent('Get me a $(ITEM: pizza, burger...)', p => {
p.play(`Adding ${p.ITEM.value} to your order...`);
p.resolve();
});
intent('Cancel the order', p => {
p.play('Your order is cancelled');
p.resolve();
});
fallback('You need to order something. Say: get me a pizza or get me a burger. Or say cancel to cancel the order.');
});
Now, when users enter the chooseDish
context by saying: What do you have?
, they will need to finish this step either by ordering something or by cancelling the order. Until then, they will remain in the context, and Alan AI will keep on prompting for the correct command.