Using context for intent reuse

Last updated last month

Context is used to reuse intents across several bot dialogs. To learn more about the concept of context, go to this page.

When the intent 'who are you' is recognised our train traveling assistant introduces himself and asks if the user would like to order a ticket.

At the end of the 'Book train ticket' conversation flow the chatbot asks a booking confirmation.

Both bot questions expect a user intent answer of yes or no. To support reuse of intents we can define a bot dialog intent linked to a certain context. The user will only be redirected to the linked bot dialog if the intent is recognized and the user is in a specific context.

  • Add the 'yes' and 'no' intents and train them with expressions

  • Add output context 'confirm_booking' in the bot dialog 'Confirm booking' with a lifespan of 1. When a user reaches this bot dialog the output context with the initial lifespan value is added to the user session context. For each user message the lifespan of a context is decreased by one. A user can have multiple contexts with different lifespan values.

  • Add the 'Confirmed Booking' bot dialog with required context 'confirm_booking' and intent 'yes'. When the 'yes' intent is returned by the NLP model and the user has the context 'confirm booking', he will be redirected to this bot dialog. When multiple intent and input context combinations are found, the user's context with the highest lifespan value is taken. A bot dialog intent can also be linked to multiple input contexts for intent re-use in subflows of flows.

  • Add the 'Cancel booking' bot dialog with the required context 'confirm_booking' and intent 'no'

  • Add output context 'who_are_you' in the bot dialog 'Who are you' with a lifespan of 1

  • Add the 'Yes book ticket' bot dialog with required context 'who_are_you' and intent 'yes'.

  • Add the 'No book ticket' bot dialog with required context 'who_are_you' and intent 'no'

Test the flow in the emulator

  • Click on the Emulator tab to test your flow. Let us ask our traveling assistent who he is.

  • Go to the debug mode and select the first message in the messages list 'who are you' to view the received information after sending this user message.

  • The context who_are_you has been added to the user session with an initial lifespan value of 1 as you can see in the context section.

  • The user is redirected to the bot dialog

  • The NLP result section shows that the who are you intent has been recognised as top scoring intent.

  • In the Message Data section we see the message being sent by the bot as answer

  • In the User Session section we see the context list of the user with name and lifespan

In the next tutorial we will use the Clear Session plugin to remove stored session data.