How-tos

Leveraging IBM Bluemix’s Watson Service on the Desktop

Share this post:

There has been significant interest in the IBM Watson Platform Services in Bluemix since their inception in October 2014. One common question asked by partners or on forums is “How do I integrate these services with a desktop application? For example, can a .NET application integrate with the Bluemix-hosted services?” And that is the focus of this article—how to use a Watson Platform Service with a desktop application. I will walk through the process to create the necessary artifacts in Bluemix and use a sample C# application with Watson’s Question and Answer service.

Below is what I used to build the app:

  • A Bluemix Account
  • Visual Studio 2013
  • A JSON Library (I used the library from Json.NET)
  • Windows 7

The source code is located in my git repository WatsonWithWindowsForms.

Let’s Get Started

The first step is adding Watson’s Question and Answer service to my Bluemix account. In order to use the Question and Answer service, I also need to create an application that binds to the service in order to obtain credentials. Although our desktop application isn’t running on Bluemix, it can nonetheless use these credentials to authenticate with the Question and Answer service. Make sense? Great! As shown in the next sections, creating the service, application and binding is easy in Bluemix.

Create Bluemix Application

The first thing I need to do is create an application in Bluemix. To do that, after logging into Bluemix, I selected Catalog to view the available runtimes. As you can see in the image below, Bluemix has a variety of available runtimes. I created a node application by selecting the SDK for Node.js.

Bluemix Runtime Catalog

After the application is created, Bluemix displays the application dashboard. The application dashboard displays attributes of the application, such as number of instances running and the memory quota.

Add Question and Answer Service

For the purposes of this example, we’re interested in the ability to add a service to our application (highlighted in red below):

Add Service

Once I select Add A Service, Bluemix will display the Service Catalog. It just so happens that the available Watson Services are displayed at the top of the catalog!

Watson Platform Services Catalog

The above image displays the available Watson Services. I selected the Question and Answer service for this demonstration. Bluemix will create the service and automatically bind it to the Node.js application. As explained earlier, the binding between the application and the service provides us the credentials we need to authenticate our desktop application with the Question and Answer service! The credentials contain a URL to the service, a randomly generated user id and randomly generated password. Copy your credentials as you will need them in your desktop application:

Application-Service Bindings

If you are following along, your Node.js application should be running in Bluemix now. However, for the purposes of this demonstration, you may stop the Node.js application as it will not be used. With a production service, you could use a Bluemix application to filter and enhance the raw QA API response, but we’ll skip that step in this demonstration for simplicity’s sake.

Coding the Desktop Application

The application I want to build is a simple Windows Form based application that sends a question to the public Healthcare corpus hosted on Watson Developer Cloud. Once I receive the answers from Watson, I just want to display them, along with their confidence. See my form below! Pretty simple functionality: Enter a question, select “Ask Watson” and the text box is populated with answers and their confidence scores:

Simple Q&A form

I created a WatsonQA class that encapsulates the logic to communicate with Watson. The WatsonQA constructor is show below. The baseURL, uid and pwd parameters should be populated with the service binding credentials created in Bluemix. The corpus parameter must either be “travel” or “healthcare”. The “healthcare” option is used in the example application, but you may also use “travel” to use the travel content instead.

[gist https://gist.github.com/tankcdr/3e78d4c65aa1e753dc55 /]

The most interesting thing occurring in the constructor is the value assigned to the class instance’s url attribute. The baseURL provided with the service-binding credentials is appended with the question uri and the corpus. This creates the fully formed URL to invoke the question API on the target corpus. The Question and Answer service routes the request to the appropriate corpus hosted in the Watson Developer Cloud.

I also print out the credentials—but you do not want to do that in a production application! The method I created, AskQuestion(), handles communicating with Watson to obtain answers for the supplied question. I’ll cover the code in two sections. The first section below sets up the call to the Watson Healthcare corpus. The variable data is assigned the question to be asked using the Question Answer API format.

To communicate with Watson, I create an instance of the .NET class WebRequest, which is assigned to variable qaCall. The WebRequest is then configured by setting header and authentication information and the HTTP communication method. Finally, I write the question to qaCall‘s Request Stream.

[gist https://gist.github.com/tankcdr/f3071f65a92eff274256 /]

The second half of method AskQuestion obtains the response from Watson and simply reads the full request. The answers are returned to the calling application.

[gist https://gist.github.com/tankcdr/baa9b9477027a2a2c737 /]

The answers are returned to the following method in Form1.cs. I’m using the evidencelist to obtain the answers (text) and associated confidence (value). I’m using the evidencelist and not the answers directly because the Healthcare corpus is running on a Watson Engagement Advisor (WEA) instance. WEA parses content into passage answer units (PAUs). The answer element in the Watson response may contain PAU IDs instead of the text of the answer. However, the corresponding evidencelist entry will contain the answer text value.

[gist https://gist.github.com/tankcdr/10a03c8bf261c1bc47d0 /]

Finished Application

Here is a screenshot of the application displaying answers and confidence:

answers

So that’s it! If you want to learn more about the Question and Answer API, here are some additional resources:

More stories
May 1, 2019

Two Tutorials: Plan, Create, and Update Deployment Environments with Terraform

Multiple environments are pretty common in a project when building a solution. They support the different phases of the development cycle and the slight differences between the environments, like capacity, networking, credentials, and log verbosity. These two tutorials will show you how to manage the environments with Terraform.

Continue reading

April 29, 2019

Transforming Customer Experiences with AI Services (Part 1)

This is an experience from a recent customer engagement on transcribing customer conversations using IBM Watson AI services.

Continue reading

April 26, 2019

Analyze Logs and Monitor the Health of a Kubernetes Application with LogDNA and Sysdig

This post is an excerpt from a tutorial that shows how the IBM Log Analysis with LogDNA service can be used to configure and access logs of a Kubernetes application that is deployed on IBM Cloud.

Continue reading