How-tos

Rapid Watson Service development with new Node.js packages

Share this post:

I’m excited about a new node package that I was introduced to this past week! One of my colleagues, the venerable German Attanasio, introduced me to a Node.js package that he, along with this team, have created to simplify Watson Service development on Bluemix. This weekend I decided to sit down and play with the new package.

You can find the watson-developer-cloud package on npmjs.com. As indicated in the package documentation, it is currently alpha quality and not recommended for production. Nonetheless, the watson-developer-cloud package is impressive and extremely simplifies embedding cognitive services into a node application.

Running an example on your laptop

The sample code below demonstrates how straightforward it is to create a question answer service using restify and the watson-developer-cloud. If you want the code to run yourself, I’ve hosted it as GitHub project WDCRestifyExample.

[gist https://gist.github.com/tankcdr/82e841a77514708bae75 /]

Line 3 is where the watson-developer-cloud package is initialized using variable wdc.

I used wdc to create a question answer service on lines 6-11. The user name and password will come from a service binding in your Bluemix account. See instructions in one of my previous blog entries and follow the process to create your credentials. Just copy the credentials from you service binding into the file. Also, I am using the travel corpus in this example, so I set dataset to ‘travel’.

On line 20, the Question Answer service is invoked using the ask(options, callback) method. The options, in this case, simply sets the text to the question to be asked.  The callback accepts either an error or a positive response.

To test the service, I used curl as shown below.

[gist https://gist.github.com/tankcdr/64547a7eed67dda6c3fe /]

Running the example on Bluemix

Ok, so now let’s convert this example to run on Bluemix. The first thing I want to do is create a Question Answer service in Bluemix. You may use the Bluemix console or the Cloud Foundry tools. I use the latter on my laptop and here is how I created my service.

[gist 649b01e4057675b21f8b /]

Next, I want to create the manifest YAML to describe the service to Bluemix. Here is what it looks like:

[gist f74f433d4d2549d2d3b3 /]

On line 3, I specify the service name to bind to: cmmqa. ‘cmmqa’ was created in the previous step. I set the name of this application to ‘wdctravel’.

Now its time to convert the service to run on Bluemix!  For easy reference, I’ve hosted the code on GitHub as project WDCRestifyBluemixExample. To run in Bluemix, I modified the service code to use the VCAP_SERVICE variable as shown next.

[gist  4c4c4bd94ab6b27870bd/]

The code is very similar except that I’ve removed the username and password from the Question Answer service initialization in line 6-10. However, I did add the parameter use_vcap_services and set that to true. That instructs the watson-developer-cloud package to use the Bluemix VCAP_SERVICES environment variable to obtain binding credentials.

The other change is at line 29 where I used the process.env.VCAP_APP_PORT to configure the service to use that port instead of 8080.

Finally, I push the service to Bluemix (cf push) and test it with curl.

[gist 690fee756aeb7a2aabe0 /]

Conclusion

The watson-developer-cloud package is a new Node.js package that simplifies integration of Node.js applications with the Watson Services. While currently in alpha, it is extremely useful and allows for rapid delivery of cognitive-powered Node.js applications. So, what are you waiting for? Install it and get started using it! Even better, let’s build this package as a community effort!

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