IBM Watson Assistant V2 — Handling Context Variables
Recently IBM upgraded their Watson Assistant cloud service from V1 to V2. In this article I will be discussing on creating a V2 service and how to manage the context variables in it.
According to the official documentation on the website, we will see that they have added something called “sessions” in the new service and to access the service we first must create this session and use the session_id to access the dialog flow.
Apart from that, there is an assistant_id that is new to V2 service.
Now lets focus on what all is new in WatsonV2 and what is happening in our code.
To start, lets see the new functions that we have in our new API that are different from Watson V1.
Setting up the service
var watson = require('watson-developer-cloud');var assistant = new watson.AssistantV2({
version: '2018-xx-xx',
username: '{username}',
password: '{password}',
url: 'https://gateway.watsonplatform.net/assistant/api'
});
Methods
Create Session
assistant.createSession({
assistant_id: '{assistant_id}',
}, function(err, response) {
if (err) console.log('error:', err);
else {
session = response.session_id
}
}
});
Message
assistant.message({
assistant_id: '{assistant_id}',
session_id: '{session_id}',
context: {
skills: {
"main skill": {
user_defined: {
city: city
}
}
}
},
input: {
'message_type': 'text',
'text': '{input text}'
}
}, function(err, response) {
if (err) console.log('error:', err);
else {
...
}
});
If we look closely, we will see that to pass the variable we need to define a structure for context.
That structure will be sent while calling assistant.message.
In our case, we are passing in the city name to our service and that is being handled in our dialog flow.
Also, the weather will remain the same throughout the dialog (kept it static).
Now going into the code, we are extracting the city name from our input text using a simple regex —
var city = null;var firstvariable = “in “;
var secondvariable = “”;if (req.body.text.match(new RegExp(firstvariable + “(.*)” + secondvariable)))
{
city = req.body.text.match(new RegExp(firstvariable + “(.*)” + secondvariable))[1];
}
After extracting the city name we will pass it to the context variable.
Now let’s look at creating a new WA V2 Service. I am assuming you guys have IBM Cloud account and are able to create new services on that account.
Go to https://cloud.ibm.com/catalog and search for Watson Assistant. Select the result in Catalog Results.
You can see this in the image below —
You will then be taken to this screen. Select region and a resource group. You can modify the Service name if you want to. In my case I set it to — Watson Assistant-testV2
Next you can select a pricing plan. Lite should work fine in this case. After that click on Create.
Next click on Hamburger menu on left and go to Resource List. Then search for your service.
Click on service found and then click View Full Details. Click Launch Tool on the new screen.
After that on Top you will see a tab for Skills. Click on that.
Click on Create skill.
Go to Import Skill and upload the JSON file given on this link — https://github.com/prav10194/Watson-Assistant-V2-Context-Sharing/blob/master/skill-Watson-Assistant-Weather-Bot.json
After that click Import.
After that you will be automatically taken to the skill page.
Click Skills on Top-Left and you will be taken to main screen where you can see your skill.
Next step will be to add an Assistant to your skill. Simply go to the Assistants tab on top of your main screen. You can then click on Create Assistant.
Provide a name and description to your assistant and click Create Assistant.
Go to Add Dialog Skill and add the skill we created before.
You can see a new assistant on the main page.
To get Assistant id — click on the following icon as shown. Click Settings.
Click on API Details on the left.
Copy the assistant id and save it. We will use it later on.
Also go to your assistant page and select the assistant that we created. Click the icon and click again on View API Details.
You need to save the username and password. We will use it later.
Now go to your terminal and use the following command to clone the directory —
git clone https://github.com/prav10194/Watson-Assistant-V2-Context-Sharing
Add the credentials that we have saved before in these 2 files —
Watson-Assistant-V2-Context-Sharing/server/watsonAssistant.js
Watson-Assistant-V2-Context-Sharing/server/index.js
To install dependencies, run
npm install
in folders server and client.
Run the code now by typing the following command in terminal in folder Watson-Assistant-V2-Context-Sharing
npm start
Hopefully you won’t have any issues running the code. Just message me below, if you encounter any problems.