Setting up your AI model in the Chatbot July 08, 2024 13:19 Updated Index: Overview Using AI in Builder's Output Conditions Validation Cascade AI-based Chatbot Hybrid Chatbot Using Content Assistant Overview First, ensure your artificial intelligence (AI) model is properly configured and deployed, and that the following steps have been completed: Have set up and connected an NLP provider (IBM Watson, Microsoft LUIS and Google Dialogflow). Have created at least one intent with some example texts; Have trained and published the model on the respective provider. From there, you can start making calls to artificial intelligence through your chatbot, using AI in Builder's Output Conditions. You can create three main types of AI-powered chatbots: An AI-based chatbot, where every user message is sent to the NLP provider, which identifies intentions and entities, and then redirects the flow; A hybrid chatbot, which has one or more main flows and uses AI to bring the user back into the flow if they exit it; A chatbot that uses the content assistant; in this case, the assistant responds to the user with pre-made content based on what the user said, ideal for functioning as an FAQ. Using AI in Builder's Output Conditions To integrate AI into the chatbot flow, we'll use the output conditions present in the Builder blocks. We'll set the data source to "Identified Intent" or "Identified Entity" and the condition to "Equals" to integrate with your AI model and make a decision based on that. A best practice in this tutorial is to capture messages that fall into the exception block and process them using the AI feature. A precaution when creating the list of output conditions is to place the more specific ones above the more generic ones. This should be done for the sake of order (precedence in verification) to avoid some specific conditions from never being checked. See the example below:There are two conditions that check for the existence of the "Generate duplicate" intent; however, the second condition also requires the existence of the "invoice" entity to be met. In this scenario, if the condition checking for the intent comes before the condition checking for the entity, what happens? Note that if the "Generate duplicate" intent is identified, regardless of whether the "invoice" entity exists or not, the first condition will be satisfied. With this, the second condition won't even be checked! However, if the condition checking the entity comes first and there is the intention "Generate second copy" in the user's sentence, we have two possible scenarios. If the entity is identified, this condition will be satisfied. If the entity is not identified, the next condition will be checked and correctly satisfied. Validation Cascade A good practice for using and ensuring the proper functioning of AI in chatbots is to perform pre-validation before submitting the user's message to the NLP provider. Keep in mind that the message could be an image, video, or audio and may not contain text to be processed. Additionally, the message could be too short (just a single word), too long (like a text copied and pasted multiple times), or simply a greeting or farewell. These are some common examples of messages that the chatbot may receive and fall into the exception block. These messages do not need to be sent to the provider; they can be discarded beforehand with pre-validation, typically known as "validation cascade". Firstly, let's add some conditions to validate the user input text. The aim of this action is to check if it's a common text before sending it for AI processing. If it's not a common text, we will direct the output to the default error block. Click on the Exception block and then access the "exit conditions" tab. Click on the "Add exit condition" button, and a new condition will be created at the end of the list of conditions. In this case, we will check if the Builder variable storing the type of content sent is an image, video, or audio, and then direct it to the default error block. AI-based Chatbot In this section, we will show how to create a chatbot using artificial intelligence as the main resource. The first step is to add a validation cascade to the exception block of the flow. This action serves to filter some messages before sending them to AI to prioritize their importance, thus avoiding processing unnecessary messages. Next, download the flow here and import it into the builder to follow the tutorial step-by-step. This chatbot is called FAQ, used to respond to questions with NLP (Natural Language Processing), making the conversation more fluid. The flow starts at the “Start” block, followed by the welcome message, and then moves to the “Exception” block, where all the AI handling takes place. The first image shows the flow. The second image shows the AI-powered output configuration of the “Exceptions” block, which uses entities and intents to access each specific block. When a message is sent to the chatbot, it enters the “Exception” block", where information is analyzed. This analysis checks if the message fits into one of the intent or entity cases. If one of these cases is identified, it is sent to the corresponding block, continuing the flow. If not found, an error message will be sent, explaining that the message was not recognized. Using artificial intelligence, you can enhance your chatbot. If you identify that the number of entities and intents is insufficient, we can create new ones. After any of these steps, we should always train our AI and republish. Hybrid Chatbot In this section, we will show how we can integrate AI into a chatbot that provides services for an online store. It has a simple flow, with some menus and submenus. If desired, you can download this flow here and import it into the builder to follow the tutorial step-by-step. Regarding the flow, in summary, after the welcome block, there is a menu block that displays some topics that can be handled by the chatbot. Depending on the choice, the user will be redirected to a submenu with options about that specific topic. The problem is that, in this scenario, often they may not reach these submenus and find the option they want, or even send an unexpected message. Therefore, we will configure artificial intelligence to process any message that leaves the standard flow and redirect the user to the block that best matches what they want. The following image illustrates the chatbot flow that we will use in this tutorial: First, a validation cascade will be added in the exception block of the flow to filter some messages before sending them to the AI, aiming to avoid processing unnecessary messages. Now, in the exit conditions of the exception block, we will add conditions that will be satisfied if certain intents and/or entities are recognized in the user's sentence. First, we will create a condition so that if the intent “Generate second copy” is recognized, the flow will be redirected to the “Second copy menu” block. Thus, if the user says something related to generating a second copy, and the NLP provider can identify this, they will be automatically redirected to the block that deals with it, without the chatbot displaying an error message or saying it didn’t understand. If you have difficulties creating exit conditions that consult intents and entities in the user's sentence, read the section Using AI in exit conditions again. It is also possible to create more complex exit conditions, identifying entities in the sentence in addition to the intent. For example, we will now create a condition so that if the intent “Make a change” and the entity “payment method” are recognized in the user's sentence, the flow will be redirected to the “Change payment method” block, as shown in the next image. Finally, this process of creating exit conditions involving intents and entities can be extended. You can create as many conditions as necessary, making your chatbot increasingly accurate during a conversation, allowing much more fluid and natural interactions. If you want to download the final flow, to analyze how it looks after creating the exit conditions, click here. Using Content Assistant It is also possible to have ready-made responses for a certain combination of intents and entities. For this, it is necessary to register these combinations, along with the respective response in the content assistant, in a submenu within the AI tab. To learn more about how it works and how to register new combinations, check here. After registering the desired combinations, we will configure the consultation to the content assistant in the chatbot flow, in the Builder. To do this, we will also use the exception block to capture messages that leave the conventional flow. They will be sent to the NLP provider to identify the intents and entities present in the sentence and then return the appropriate content if it exists. 1. First, enter the Builder. 2. Click on the Exceptions block and then access the “Actions” tab; 3. Click the “Add entry action” button; 4. Create a new action of the type “Consult content assistant”; 5. A new action will be created with the fields: “Variable,” “AI Reliability,” and “Variable for the return value.” 6. In the “Variable” field, we will fill in the variable that stores the content of the message sent by the user “{{input.content}}”. The AI reliability field is optional. If it is not filled in, the reliability registered in the chatbot settings will be used; therefore, we will leave this field blank. The return variable field will be filled in with “contentResult,” but it can be filled with any other name of your choice. 7. Click the “<” arrow to save your settings. Configure in the exit conditions Now, we just need to configure in the exit conditions that, if there is a identified match, it should redirect to a block that will display the content of that match. 1. Create a new block with a suggestive name (e.g., “Content assistant response”). 2. In it, add a text balloon with the “Value” attribute of the variable you used to store the content assistant’s response. To do this, use the “@” operator. If you set the variable as “contentResult,” it will be as follows: “contentResult@Value”. 3. Finally, click on the Exception block again, but now access the “Exit conditions” tab; 4. Click the “Add exit condition” button to create a new condition; 5. Create a condition where, if the variable that stores the content assistant’s response exists (e.g., “contentResult”), then go to the block that displays this content (e.g., “Content assistant response”). Remember: Position this new exit condition above the conditions that are more general than it, so it can be correctly verified and executed. For more details on this, see the section "Using AI in the Builder’s exit conditions". That’s it! Now you have a chatbot that uses AI to provide automatic responses if the user's input does not fit the expected flow. For more information, visit the discussion on the subject at our community or videos on our channel. 😃 Related articles How to use Content Assistant After all, what is AI (Artificial Intelligence)? How to configure LUIS as your AI provider Sending WhatsApp Active Messages on Blip Desk Creating interactive messages in WhatsApp