/assistant-simple

A simple sample application demonstrating the Watson Assistant api.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

Watson Assistant (formerly Conversation) Sample Application Build Status codecov.io

This Node.js app demonstrates the Watson Assistant service in a simple chat interface simulating a cognitive car dashboard.

Demo

You can view a demo of this app.

Before you begin

  • Create an IBM Cloud account
    • Sign up in IBM Cloud, or use an existing account. Your account must have available space for at least 1 app and 1 service.
  • Make sure that you have the following prerequisites installed:
    • The Node.js runtime, including the npm package manager

    • The Cloud Foundry command-line client

      Note: Ensure that you Cloud Foundry version is up to date

Installing locally

If you want to modify the app or use it as a basis for building your own app, install it locally. You can then deploy your modified version of the app to IBM Cloud.

Getting the files

Use GitHub to clone the repository locally, or download the .zip file of the repository and extract the files.

Setting up the Watson Assistant service

You can use an exisiting instance of the Watson Assistant service. Otherwise, follow these steps.

  1. At the command line, go to the local project directory (assistant-simple).

  2. Connect to IBM Cloud with the Cloud Foundry command-line tool. For more information, see the Watson Developer Cloud documentation.

    cf login
  3. Create an instance of the Watson Assistant service in IBM Cloud (our CLI is being updated, for now, use the create-service conversation command). For example:

    cf create-service conversation free my-watson-assistant-service

Importing the Watson Assistant workspace

  1. In your browser, navigate to your IBM Cloud console.

  2. From the All Items tab, click the newly created Watson Assistant service in the Services list.

    Screen capture of Services list

  3. On the Service Details page, click Launch tool.

  4. Click the Import workspace icon in the Watson Assistant service tool. Specify the location of the workspace JSON file in your local copy of the app project:

    <project_root>/training/car_workspace.json

  5. Select Everything (Intents, Entities, and Dialog) and then click Import. The car dashboard workspace is created.

Configuring the app environment

  1. Copy or rename the .env.example file to .env (nothing before the dot).

  2. In your IBM Cloud Dashboard, open the Watson Assistant service. Click show to retrieve your credentials.

  3. If your credentials contain a username and password, copy the credentials to the relevant fields.

    {
      "url": "https://gateway.watsonplatform.net/conversation/api",
      "username": "ca2905e6-7b5d-4408-9192-e4d54d83e604",
      "password": "87iT7aqpvU7l"
    }
    ASSISTANT_USERNAME=ca2905e6-7b5d-4408-9192-e4d54d83e604
    ASSISTANT_PASSWORD=87iT7aqpvU7l
    

    However, if your credentials contain an IAM API key, copy the apikey and url to the relevant fields.

      {
        "apikey": "ca2905e6-7b5d-4408-9192-e4d54d83e604",
        "iam_apikey_description": "Auto generated apikey during resource-key ...",
        "iam_apikey_name": "auto-generated-apikey-62b71334-3ae3-4609-be26-846fa59ece42",
        "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
        "iam_serviceid_crn": "crn:v1:bluemix:public:iam...",
        "url": "https://gateway-syd.watsonplatform.net/assistant/api"
      }
    ASSISTANT_IAM_APIKEY=ca2905e6-7b5d-4408-9192-e4d54d83e604
    ASSISTANT_IAM_URL=https://gateway-syd.watsonplatform.net/assistant/api
    
  4. In your IBM Cloud console, open the Watson Assistant service instance where you imported the workspace.

  5. Click the menu icon in the upper-right corner of the workspace tile, and then select View details.

    Screen capture of workspace tile menu

  6. Click the Copy icon to copy the workspace ID to the clipboard.

  7. On the local system, paste the workspace ID into the WORKSPACE_ID variable in the .env file. Save and close the file.

Installing and starting the app

  1. Install the demo app package into the local Node.js runtime environment:

    npm install
  2. Start the app:

    npm start
  3. Point your browser to http://localhost:3000 to try out the app.

Testing the app

After your app is installed and running, experiment with it to see how it responds.

The chat interface is on the left, and the JSON that the JavaScript code receives from the Watson Assistant service is on the right. Your questions and commands are interpreted using a small set of sample data trained with the following intents:

turn_on
turn_off
turn_up
turn_down
traffic_update
locate_amenity
weather
phone
capabilities
greetings
goodbyes

Type a request, such as music on or I want to turn on the windshield wipers. The system understands your intent and responds. You can see the details of how your input was understood by examining the JSON data in the Watson understands section on the right side.

For example, if you type Turn on some music, the JSON data shows that the system understood the turn_on intent with a high level of confidence, along with the appliance entity with a value of music.

For more information about intents, see the Watson Assistant service documentation.

To see details of how these intents are defined, including sample input for each intent, launch the Watson Assistant tool.

Modifying the app

After you have the app deployed and running, you can explore the source files and make changes. Try the following:

  • Modify the .js files to change the app logic.
  • Modify the .html file to change the appearance of the app page.
  • Use the Watson Assistant tool to train the service for new intents, or to modify the dialog flow. For more information, see the Watson Assistant service documentation.

Deploying to IBM Cloud

You can use Cloud Foundry to deploy your local version of the app to IBM Cloud.

  1. In the project root directory, open the manifest.yml file:
  • In the applications section of the manifest.yml file, change the name value to a unique name for your version of the demo app.
  • In the services section, specify the name of the Watson Assistant service instance you created for the demo app. If you do not remember the service name, use the cf services command to list all services you have created.

The following example shows a modified manifest.yml file:

---
declared-services:
 my-watson-assistant-service:
   label: conversation
   plan: free
applications:
- name: conversation-simple-app-test1
 command: npm start
 path: .
 memory: 256M
 instances: 1
 services:
 - my-watson-assistant-service
 env:
   NPM_CONFIG_PRODUCTION: false
  1. Push the app to IBM Cloud:
cf push

Access your app on IBM Cloud at the URL specified in the command output.

Troubleshooting

If you encounter a problem, you can check the logs for more information. To see the logs, run the cf logs command:

cf logs <application-name> --recent

License

This sample code is licensed under Apache 2.0. Full license text is available in LICENSE.

Contributing

See CONTRIBUTING.

Open Source @ IBM

Find more open source projects on the IBM Github Page.