Over a million developers have joined DZone.

Watson Service Chaining With OpenWhisk (Part 2 of 3)

DZone 's Guide to

Watson Service Chaining With OpenWhisk (Part 2 of 3)

Let's get down to business. By this point, you know how serverless works and its benefits, so let's create some Watson Services and chain them together using OpenWhisk.

· Cloud Zone ·
Free Resource

In Part 1 of this series, you learned the basics of Serverless computing and the building blocks behind OpenWhisk. In this post, you will create Watson Services and add them to an OpenWhisk Sequence on IBM Bluemix.

As our post is all about chaining Watson Services using OpenWhisk, in this section, you will create three Watson Services, namely:

Creating Watson Services on Bluemix

  • Log into Bluemix account with your registered Email or IBMid.
  • Click on Catalog on the top bar. On the left pane, click on Watson under Services.
Watson Services on Bluemix:
  • Select Language Translator and create a new service.
  • Provide a unique Service name. If you are not sure what to use, leave the default name provided.
  • Leave the Credential name as it is.
  • (Optional) Read the Features and Pricing Plans.
  • Click on Create.

Repeat the above steps to create Speech-to-Text and Text-to-Speech Watson Services.

Develop an OpenWhisk Sequence on a Browser

In this section of the post, you will chain the Watson Services, along with other actions, to create a sequence that translates an input in English to French.

  • Click on Develop in your browser, and you should see the Develop tab selected.
  • On the left pane, you will see tabs grouping your Actions, Sequences, Rules, and Triggers.
  • Under My Actions, you should see two sample actions created for your reference namely Hello World and Hello World with Params. Both these actions are Node.JS 6 Actions.
  • Click on Create an Action. You are creating an OpenWhisk actionmyMessageSwiftAction. Choose SwiftV3 execution runtime, and, optionally, a sample from which to start, Choose Hello World in Swift. You can also choose to change the default quota settings, for this lab lets go with default Memory and Time Limit.

  •  Click on Create Action and you should see your action under MY ACTIONS tab. Select myMessageSwiftAction and replace the existing function code with the code below
func main(args: [String:Any]) -> [String:Any] {
    if let message = args["message"] as? String {
        return [ "payload" : "\(message)!" ]
    } else {
        return [ "payload" : "Hello stranger!" ]

Note: As the output of one action is the input to the next action, the JSON with the payload will be an input to the next action you create in the sequence.

Hit Make it Live to save your changes. Now Click Run the Action on the top and change the “message” JSON input. Run with this Value should invoke the action in a console and show the output with your input as a Payload output. Also, you can see the logs, Completed in and, how many seconds are you Billed for.

  • Close the console and Click Link into a Sequence. You should see Configure a New Action Sequence with a variety of Packages to select from

Note: In IBM Bluemix OpenWhisk, you can use packages to bundle together a set of related actions, and share them with others.

  • Click on the Watson Text to Speech tile and  Click on to create a new Package binding.

Note: Although you can use the entities in a package directly, you might find yourself passing the same parameters to the action every time. You can avoid this by binding to a package and specifying default parameters. These parameters are inherited by the actions in the package.

For example, in the /whisk.system/cloudant package, you can set default username, password, and dbname values in a package binding and these values are automatically passed to any actions in the package.

  • Let’s call this myWatsonTextToSpeech (name for our configuration) and from the dropdown select the Text to Speech service which we created earlier.
  • (Optional) You can click on View Source to see the code.
  • Click on Add to Sequence to connect this to Swift Action which we created.

  • Click on This Looks Good to save your Watson Sequence and name it MyWatsonSequence. Also, run the sequence to see the output.
  • As you are not done with the chaining yet, Click on Extend and repeat the steps for adding Speech to Text package to the chain.
    • Name of the binding: myWatsonSpeechToText
    • In the dropdown, Choose the Speech to Text service which you created.
    • Click on Add to Sequence.
    • Click on This Looks Good to save your Watson Sequence.
  • Click on Actions and create a new action called myLanguageTranslator Which will convert the Speech to Text output to the JSON format of Watson Translator Package.
    • Execution Runtime – Node.JS 6
    • Start with a Blank State.
    • Create Action.
    • Replace the existing code with the code snippet below.
function main(params) {
    return { payload: params.data };

  • Click Make it Live to save your Action.
  • On the left pane under MY SEQUENCES, select MyWatsonSequence -> Extend -> My Actions Tile -> Select myLanguageTranslator and Click Add to Sequence
  • Click on Extend and Select Watson Translator
    • Select Translator as your Action.
    • Name of the Binding: myWatsonTranslator
    • Select the Language Translator Service which we created earlier.
    • Click on Add to Sequence.
  • Save your changes and Click on Run this Sequence to test your Watson Sequence.

Test: JSON Input

    "message": "hello"

Expected Output:

In this part, you completed the Watson service chaining via an OpenWhisk Sequence. You can chain other packages as well following the same steps.

In part 3, you will learn about consuming the actions via OpenWhisk API gateway and also you will create a NodeJS webUI app to see all the actions, triggers, rules and packages you created via Browser. Also, you will get a gist of OpenWhisk CLI.

cloud ,ibm watson , ,ibm bluemix ,openwhisk ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}