Creating a Watson Conversation Service
While searching on the internet, you may find out about more blogs or topics discussing Watson Conversation Service. The best guide document is the official document page. The document page link is given here.
I will demonstrate the process to create a really simple Conversation Service (chatbot) hosted in the Bluemix cloud.
Step 1
Create an account on Bluemix or login with your existing account.
Step 2
If you are signing up, then you have to confirm your email address. After your email confirmation, when you login for the first time , you will get the screen, as shown below.
- Accept the terms and click on continue. Now, you have to setup the organization and the Workspace.
- Select the Deployment Server location and name your organization.
- Click on the create.
- Name your space. I selected the suggestion Dev.
- Click Create.
- Click I am Ready. This will bring you to the Dashboard. This screen is the same, when you are returning logging after this initial setup of your account.
Step 3
Click Create app.
Step 4
Search for Conversation.
Step 5
Click on the Conversation Service (First Result).
Step 6
Click on the Create button.
Step 7
Go-to Service Credential tab.
Step 8
Click on the View Credential. It will display the endpoint information, which will be required in future to connect to this Service from our .NET code. Please note down the details.
Step 9
Go back to Manage Tab and click on the Launch Tool button
It will open a new Window (or tab). This is called the workspace. Here, we have to define the intent, entities and the dialog. For more information, please refer to the official documentation link mentioned above.
Step 10
Click on create. It will open a popup. Provide the Workspace name and description(optional).
Step 11
Click Create.
Step 12
Click Create new button to create a new Intent.
Step 13
Provide an intent name and the sample example.
Step 14
Click Create.
Create another Intent.
Step 15
Now, go to Entity tab.
Step 16
Click Dialog tab.
Step 17
Click Create. Edit the selected node, as per the screen.
Step 18
Create a conversation start node, as shown below.
Create other dialog nodes.
Create fallback node (AnythingElse).
Step 19
Now, test the Service. Click on the top right corner small chat icon. Now, chat with BOT in the right hand panel.
Step 20
Click on the Hamburger menu and click on the Back to Workspace.
Step 21
Click on the more button.
Step 22
Click on the view details.
Step 23
Note down the Workspace Id. This Workspace Id is required to connect to the Service.
Consume the Service from the .NET code
Step 1
Let’s create a class called ConversationHelper. Sample code, which is given below.
- public class ConversationHelper {
- private readonly string _Server;
- private readonly NetworkCredential _NetCredential;
- public ConversationHelper(string workSpaceId, string userId, string password) {
- _Server = string.Format("https://gateway.watsonplatform.net/conversation/api/v1/workspaces/{0}/message?version={1}", workSpaceId, DateTime.Today.ToString("yyyy-MM-dd"));
- _NetCredential = new NetworkCredential(userId, password);
- }
- public async Task < string > GetResponse(string input, string context = null) {
- string req = null;
- if (string.IsNullOrEmpty(context)) req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}";
- else req = "{\"input\": {\"text\": \"" + input + "\"}, \"alternate_intents\": true}, \"context\": \"" + context + "\"";
- using(var handler = new HttpClientHandler {
- Credentials = _NetCredential
- })
- using(var client = new HttpClient(handler)) {
- var cont = new HttpRequestMessage();
- cont.Content = new StringContent(req.ToString(), Encoding.UTF8, "application/json");
- var result = await client.PostAsync(_Server, cont.Content);
- return await result.Content.ReadAsStringAsync();
- }
- }
- }
Step 2
Now, from the caller class, create an instance of this class by providing the workspace Id, user Id and password. Now, call the method GetResponse. Now, you have successfully created a chatbot Service and consumed from .NET code.
The sample consume code is given below.
- class Program
- {
- static void Main(string[] args) {
- ConversationHelper helper = new ConversationHelper("62a2a278-894d-42eb-999d-5aedaad8218d", "194757ef-a260-4da3-a343-fe48bf5b87ab", "grUFElBwPT2O");
- var res = helper.GetResponse("Hi").GetAwaiter().GetResult();
- }
- }
Please refer to the documentation for the model details of the input to the conversation Service and the response from the Service. The API documentation is here - https://watson-api-explorer.mybluemix.net/apis/conversation-v1#/
Conclusion
IBM Watson Conversation Service is very easy to learn and create on Bluemix. As the communication to the Service is via JSON, this Service can be consumed from almost all the languages and platforms.