The purpose of the TradeBot Assistant was to build a robust chatbot system, which can identify different trading queries of users and give the appropriate response, using the IBM Watson Assistant platform.
See other images of the website in the Screenshot folder.
🏠 Homepage
Bootstrap is the most popular CSS Framework for developing responsive and mobile-first websites. The bootstrap 4 libraries are already downloaded and added in the CSS and JS folders. It is also referrenced in the Index.html file. You can also download it here.
<head>
<title>TradeBot Assistant</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/fontawesome.min.css">
<link rel="stylesheet" type="text/css" href="css/all.css">
</head>
<script src="../js/bootstrap.js"></script>
IBM Watson Assistant uses artificial intelligence that understands customers in context to provide fast, consistent, and accurate answers across any application, device, or channel. Remove the frustration of long wait times, tedious searches, and unhelpful chatbots with the leader in trustworthy AI.
The natural language processing for Watson Assistant service is defined in a dialog skill, which contains all the data to build a conversation flow. Upon creating the dialog skill, there is an option to choose from different languages that the skill will be trained to understand but English language has been chosen as the default value.
A webhook is a method that can call an external program, such as IBM Cloud Function, to the chatbot application. It is triggered when the assistant processes a node that has a webhook enabled. The webhook then collects data from user input and store them as parameter which is then sent as part of a HTTP POST request to the defined URL. This method was used to send data as parameters to the functions in the Cloud Function. The specified URL, from the web action in the section of IBM Cloud Function, is added in the dialog skill option. If the webhook call exceeds the 8 seconds, an error response will be shown on the web chat interface. Whenever a webhook is activated in a node, it provides a multi-conditioned response where different responses can be shown to the user if the condition is met.
Note: Since all responses are returned as a JSON Object, the
.json
extension needs to be added in the webhook URL to ensure that the desired content type of the response will be in a JSON format.Moreover, the web action can be invoked by using the URL without authentication. To make the web action more secure, an HTTP header value X-Request-Whisk-Auth is added for authorization to invoke the webhook.
Intents are defined as the goals expressed in user’s input such as searching for something or answering a question. By recognizing the intents, Watson Assistant can choose the correct dialog node to answer the user. Based on this technique, some intents have been added to achieve dialogs like in the use cases. The training intents should contain several examples to provide a better response coverage.
Entities are important pieces of information in the user input to perform certain tasks . The defined entities will then be trained in Watson Assistant to be detected in different context of a user input. These important keywords will be finally stored as parameters in the dialog nodes to be sent to the cloud functions.
Contextual entity model consists of annotating occurrence of the defined entities in the intent’s examples. This method helps the assistant to understand how the entity is typically used in a sentence.
Slots is a mechanism that gathers multiple pieces of information, from a user, to give an accurate response. It also provides different ways to collect information. For example, if the user enters “Trade statistics” or any in the #Trade_Statistics intent, the slot function has been configured to detect a @Country entity and a @Period entity.
if (none are found)
The chatbot will first ask the user to enter a name of a country, then ask to enter the desired year
else if (@Country is found)
The chatbot will ask to enter the year
else if (@Period is found)
The chatbot will ask for the name of the country
else if (both are found)
The chatbot will see that both required information are detected and continue the process
The dialog is structured in a decision tree and it defines what the assistant is able to say in response to customers. The decision tree contains several parent nodes which are further extended with child nodes.
IBM Db2 is a relational database which offers AI capabilities for data management and advanced analytics for workloads. It was mainly chosen because of its AI functionality which provides machine learning algorithms that were able to make faster query speed improvements. IBM cloud database improves query performance by implementing indexes, materialized views and compression, which makes Db2 more efficient. The IBM cloud service provides two ways to create tables, load data and to run SQL commands, either by using the graphical user interface or the IBM command line. For this project, the GUI will be used on the cloud website.
The dataset used for the trade data is provided by UN Comtrade. It contains an open-source collection of traded goods but however, the one implemented, in this project, has limited data of 175150 rows. The full dataset contains more than 1 million products from different countries and requires a premium access. IBM Db2 platform has the feature to load data from a single delimited text file. The delimiter used is a comma(,) which separates each value into a specific column. This feature makes it easier to load several CSV files into the table instead of using the INSERT INTO query.
In order to gain accessibility to the database, each instance needs to have a unique set of service credentials which contains hostname, username, password, port and much more. This will provide authorization to the user and will be further used in the Cloud Functions to gain access to the database.
Cloud function, also referred to as Function-as-a-Service (FaaS), is a method to execute the application codes in the cloud at massive scale. The serverless infrastructure will deploy the code in the form of a function and will be responsible for the execution, resources needed and the scaling of the runtime environment. There are 2 key terminologies to understand the basic concepts of IBM cloud function:
- Namespace - The namespace is also known as an Identity and Access Management (IAM) service instance. The IAM integration provides flexible access and control over the function’s resources.
- Web Action - A cloud action is the source code where all the tasks will be performed. An action can be written in several programming languages but for this project, Node.js 12 was used to execute all the query tasks. A web action is an action that is implemented with HTTP handlers that respond with headers, status code and body content. Upon creating a web action, the result is a URL that can be used to trigger the action. The URL will then be added in IBM Watson Assistant to send a JSON response.
The service credentials of IBM Db2 is passed as parameters in the dispatch,js web action. The values are then used in the main function to be stored in the dsn variable. From Node.js, the connection with IBM Db2 will be firstly established before executing a query. However, the dsn value from the credentials does not exists for the standard instance and has to be built manually. In the if condition, each value is stored in a const variable and passed into the dsn variable.
dsn = params.__bx_creds[Object.keys(params.__bx_creds)[0]].dsn;
// dsn does not exist in the DB2 credential for Standard instance. It must be built manually
if(!dsn) {
const dbname = params.__bx_creds[Object.keys(params.__bx_creds)[0]].connection.db2.database;
const hostname = params.__bx_creds[Object.keys(params.__bx_creds)[0]].connection.db2.hosts[0].hostname;
const port = params.__bx_creds[Object.keys(params.__bx_creds)[0]].connection.db2.hosts[0].port;
const protocol = 'TCPIP';
const uid = params.__bx_creds[Object.keys(params.__bx_creds)[0]].connection.db2.authentication.username;
const password = params.__bx_creds[Object.keys(params.__bx_creds)[0]].connection.db2.authentication.password;
dsn = `DATABASE=${dbname};HOSTNAME=${hostname};PORT=${port};PROTOCOL=${protocol};UID=${uid};PWD=${password};Security=SSL`;
}
1 - Open the Index.html file in your browser.
2 - Click on the chatbot widget at the bottom right of the screen, to initiate the chatbot.
3 - Start the conversation by choosing one of the options from the home screen or enter
help
to view all available features.
👤 Rohan Bhautoo
- Github: @rohan-bhautoo
- LinkedIn: @rohan-bhautoo
Give a ⭐️ if this project helped you!