User interaction now has a new set of tools: Chatbots, and they have many advantages. Chatbots have become almost ubiquitous in recent times. Not only that but many of the bots have added power by the inclusion of basic artificial intelligence and machine learning to make them much smarter and with that added smarts they can become much more useful than a simple “hi how are you” interaction, often carrying out tasks such as automated order tracking, support inquiries, and appointment management. Chatbots have come a long way since the days of Joseph Weizenbaum’s Eliza. You can create chatbots with Windows app development tools like RAD-Studio, and incorporate them into your own made applications.
Table of Contents
Is it hard to add a chatbot to my app?
No! Things have come along in leaps and bounds in recent years. Modern chatbots are easier to install, often distributed across robust, scalable cloud platforms, and are extremely portable across different operating systems and device targets. Despite this the time involved in implementing them in your code is extremely fast.
What makes up a modern chatbot?
The construction of a chatbot basically goes through two major challenges: Natural Language Processing (NLP) and natural language understanding (NLU). In this article we’re going to meet those challenges in an easy to understand and rapid-to-implement way using Wit.ai as the foundation for our AI Chatbot.
What is Wit.ai?
Wit.ai is an open NLP engine for developers. It allows you to build human-like conversational applications. Wit.ai provides an easy interface and API allowing our apps to understand human conversation from interactions, parsing the message into data we can then act on. Wit.ai is one of the most powerful APIs used to understand natural language and it’s free.
Wit.ai works with NLP and NLU. NLU is the process of understanding what a sentence means. This kind of extracted meanings are called “intents”. A typical intent might be that the user wants to view a weather forecast or they want to see or hear a collection of tasks on a todo list.
What does natural language processing (NLP) actually do?
NLP breaks a sentence into pieces called entities. They are variables that contain details of the user’s task. For example, for a weather forecast, you should be able to identify which place the user wants the forecast for.
Wit.ai offers an API that takes text or voice input and returns intents and entities. When NLP is combined with NLU – the input from the user – your application may take actions or even interact with and ask the user new questions.
How do I create an AI chatbot in my application?
Go to https://wit.ai/ create an account and a new app. You should see something like the following screen:
In the “Utterance” section, you can enter a message that a user would type. Wit.ai may not understand the intent and the entities at first, you need to train it. Choose the intent and highlight the entity manually, as my example:
Then click on the button to train your bot. Also, check ‘See how it works’ to get more information and, the API documentation here.
Add some more Utterances to train your bot, until, at least, it can get the intents and entities automatically. Now, we are ready to implement AI on a chatbot application.
How do I implement Wit.ai in my own chatbot application?
First, you will need a chatbot application. This post will not go through that because another one makes just like what you need! Go here and check how to create a chatbot application in Delphi. If you already have one, don’t worry, you still can apply the following steps to add natural language processing and natural language understanding to your existing application.
The main code that you will need is as follows:
JSONResponse : TJSONValue;
RestClient : TRESTClient;
RestRequest : TRESTRequest;
RestResponse : TRESTResponse;
OAuth2Authenticator : TOAuth2Authenticator;
Result := False;
OAuth2Authenticator := TOAuth2Authenticator.Create(nil);
RestResponse := TRestResponse.Create(nil);
RestRequest := TRestRequest.Create(nil);
RestClient := TRestClient.Create(nil);
OAuth2Authenticator.AccessToken := 'ENTER YOUR SERVER ACCESS TOKEN HERE';
RestClient.Authenticator := OAuth2Authenticator;
RestClient.BaseURL := 'https://api.wit.ai/message?v=20210616&q=' + aMessage;
RestRequest.Client := RestClient;
RestRequest.Method := rmGET;
RestRequest.AddParameter('Authorization', BEARER_WIT_AI, pkHTTPHEADER);
RestRequest.Response := RestResponse;
JSONResponse := TJSONObject.ParseJSONValue(RestResponse.Content);
Intent := JSONResponse.GetValue<string>('intents.name');
Entity := JSONResponse.GetValue<string>('entities.wit$location:location.resolved.values.name');
Because I’m using Delphi MVC Framework as a server API to ‘talk’ with WhatsApp, I could not use a Form to implement the API connection to Wit.ai. Instead I needed to create the components at runtime.
To connect to Wit.ai API you need to use Auth2 and inform your Server Access Token that you get under Management/Settings, on Wit.ai
Then, as a Header, you need to add ‘Authorization’ and inform your Bearer key that you get under the HTTP API section:
Still, on that section, you get the URL to make the GET Request, and don’t forget to append the message of the user to it.
Execute the request, and Wit.ai will return a JSON as a response. The whole JSON response can be viewed here. With that, you can get your intent and entity. Now, you can use the intent and entity to communicate with your user.
That’s it, you’ve added a natural language processing with natural language understanding to your chatbot app in under five minutes! Why not give it a try yourself today?