/assistant-with-discovery-openwhisk

Integrate Watson Conversation and Discovery with OpenWhisk to explore the possibilities of "serverless" architecture.

Primary LanguageCSSApache License 2.0Apache-2.0

Watson Assistant (formerly Conversation) with Discovery - Cloud Functions

Build Status codecov

This application shows the capabilities of Watson Assistant and Discovery services to work together to find answers on a given query. In this sample, the Assistant is empty and only calls out to Discovery. It can work with any documents loaded into Discovery that contain certain fields.

This demo is a reworking of a previous one but with an cloud functions back-end and React front-end. Cloud Functions is IBM's "serverless" offering, allowing users to upload functions to the cloud, call them via REST API, and pay only by the millisecond of usage.

Table of Contents

How it Works

Flow diagram

Under the hood, there are two components to this app:

  • One is the front-end, which is simply static assets (HTML, CSS, and React), it uses CSS with Sass for cleaner, more maintainable source code.
  • The other is the Cloud Function actions:
    • When the user inputs text, the UI sends the current context and input to the Cloud Function sequence. These are processed by the Watson Assistant service and returned, with an output and new context. The results are sent to the next action.
    • The Discovery action checks for a flag from the Watson Assistant output, and if it is present takes the original input and queries the manual with it. If there is no flag, the Watson Assistant results pass through the function unchanged. The Sequence returns the output and updated context back to the UI.

Run Locally

Getting Started

  1. If you don't already have an IBM Cloud account, you can sign up here

Make sure you have at least 2 services available in your IBM Cloud account.

  1. Clone (or fork) this repository, and go to the new directory
git clone https://github.com/watson-developer-cloud/assistant-with-discovery-openwhisk.git
cd assistant-with-discovery-openwhisk
  1. Install Node.js (Versions >= 6).

  2. In the root directory of your repository, install the dependencies.

npm install

Note:
If you receive the following error during npm install:

gyp ERR! stack Error: Python executable "python" is v3.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.

Install Python 2.7.x and set npm to use. e.g.
npm config set python /path/to/python

Setting up Watson Services

Skip this section if you have downloaded the app from Watson Console and already have a credentials.json file

  1. Create an instance of the Assistant service and get your credentials:

    • Go to the Assistant page in the IBM Cloud Catalog.
    • Log in to your IBM Cloud account.
    • Click Create.
    • Click Show to view the service credentials.
    • Copy the apikey and iam_apikey_name value, or copy the username and password values if your service instance doesn't provide an apikey.
    • Copy the url value.
  2. Create an instance of the Discovery service and get your credentials:

    • Go to the Discovery page in the IBM Cloud Catalog.
    • Log in to your IBM Cloud account.
    • Click Create.
    • Click Show to view the service credentials.
    • Copy the apikey and iam_apikey_name value, or copy the username and password values if your service instance doesn't provide an apikey.
    • Copy the url value.
  3. Create a credentials.json in the top-level directory.

  4. Copy the following credentials into the file, amending for the instance name, url, apikey, and username/password; which can be found on each instance in the IBM Cloud console

Example credentials.json file that configures the apikey and url for a Assitant and Discovery service instances

{
  "discovery": [
    {
      "name": "Discovery-inst",
      "plan": "lite",
      "credentials": {
        "url": "enterUrl",
        "apikey": "enterApiKey",
        "iam_apikey_name": "enterApiKeyName"
      }
    }
  ],
  "conversation": [
    {
      "name": "Assistant-inst",
      "plan": "free",
      "credentials": {
        "url": "enterUrl",
        "apikey": "enterApiKey",
        "iam_apikey_name": "enterApiKeyName"
      }
    }
  ]
}

Train Watson Services

Run following commands to setup Watson Assistant:

npm run train 

Follow the steps here to setup the Discovery instance:

Enter the Discovery details into the app:

  1. Open the .env file

  2. Enter the environment_id, collection_id, and configuration_id for the collection used in Discovery

Setting up the OpenWhisk Back-end

  1. Install the Openwhisk Command Line Interface.

  2. Download and install the IBM Cloud CLI.

  3. Login by running the following:

ibmcloud login
ibmcloud target --cf
  1. Install jq as a dependency.

  2. Run the provided shell script create-openwhisk-actions.sh to create your OpenWhisk actions & sequence. The syntax to do so may vary by system, but for example:

   sh create-openwhisk-actions.sh

Setting up the React Front-end

Create an optimized build of your app. During this stage, your environment variable will be inserted into App.js for use by your components.

npm run build

Running the App

All that's left is to serve your static files locally. You should see the app running in a new tab!

npm start

The app can now answer questions from the documents that were ingested in Discovery.

License

Licensed under Apache 2.0.