This application shows the capabilities of Watson Conversation and Discovery services to work together to find answers on a given query. In this sample app, the user is chatting with a virtual car dashboard, giving it commands in plain English such as "Turn on the wipers," "Play me some music," or "Let's find some food." If the user makes a request and Conversation is not confident in its answer (e.g. "How do I check my tire pressure?"), Discovery will search the car manual and return the most relevant results, if relevant materials exist.
This demo is a reworking of a previous one but with an OpenWhisk back-end and React front-end. OpenWhisk 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.
Under the hood, there are two components to this app:
- One is the front-end, which is simply static assets (HTML, CSS, and React). I wrote the CSS with Sass for cleaner, more maintainable source code.
- The other is the OpenWhisk actions:
- When the user inputs text, the UI sends the current context and input to the OpenWhisk sequence. These are processed by the Conversation 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 Conversation output, and if it is present takes the original input and queries the manual with it. If there is no flag, the Conversation results pass through the function unchanged. The Sequence returns the output and updated context back to the UI.
- IBM Bluemix account. Sign up for Bluemix, or use an existing account.
- Node.js >= 7.9.0
With just a few steps, you can get this demo application up to the cloud and running in your own Bluemix account.
- Ensure your organization has enough quota for one web application using 128MB of memory and 2 services
- Click the button below to start the Bluemix DevOps wizard:
- Click Tool Integrations. Decide Whether you want to fork/clone the GitHub repository. If you decide to Clone, set a name for your GitHub repository.
- Click the Delivery Pipeline box.
- Choose a unique name for the application, or keep the one generated.
- Choose "US South" for your region.
- Make sure your "organization" and "space" do not contain spaces.
- Click Deploy.
- Click Delivery Pipeline to watch your build. (This may take over ~10 minutes)
- Return to the Toolchain page once your build completes
- Click View App
⚠️ The 'Deploy to Bluemix' button is meant to be run once on your account. If you would like to redeploy the pipeline script, or rebuild the entire app, you should delete your Conversation and Discovery service instances, and the OpenWhisk package that the script creates.
- If you don't already have a Bluemix account, you can sign up here
Make sure you have at least 2 services available in your Bluemix account.
- Clone (or fork) this repository, and go to the new directory
git clone https://github.com/watson-developer-cloud/conversation-with-discovery-openwhisk.git
cd conversation-with-discovery-openwhisk
- Download and install the Cloud-Foundry CLI. This will be used to set up your Watson Services in Bluemix.
- Connect the CLI to Bluemix by running the following:
cf api https://api.ng.bluemix.net
cf login
- Follow the prompts for logging in to Bluemix. You may have to use single sign-on (SSO).
- Create an instance of Watson Conversation, here named
conversation-for-demo
.
cf create-service conversation free conversation-for-demo
- Create a service key for your Conversation instance, here called
my-key
.
cf create-service-key conversation-for-demo my-key
- Retrieve the
username
andpassword
for your Conversation instance. Save these in a note.
cf service-key conversation-for-demo my-key
- Do the same for Discovery. Note the plan name is
lite
.
cf create-service discovery lite discovery-for-demo
⚠️ You may have to wait until Bluemix is done creating your service instance before running the other two commands. Check on the status of your service by runningcf service discovery-for-demo
and checking for aStatus
ofcreate succeeded
.
Here we called the instance discovery-for-demo
and the key my-other-key
. Save the username
and password
in a note.
cf create-service-key discovery-for-demo my-other-key
cf service-key discovery-for-demo my-other-key
- Create a new Conversation workspace using your Conversation username and password (not your Bluemix password) from the previous steps. Make sure you are in the root directory of your repository. Save the
workspace_id
in a note.
curl -H "Content-Type: application/json" -X POST \
-u <CONVERSATION_USERNAME>:<CONVERSATION_PASSWORD> \
-d "@workspace_blank.json" \
"https://gateway.watsonplatform.net/conversation/api/v1/workspaces?version=2017-05-26"
- Train your workspace with the data provided. Substitute in your Conversation workspace_id.
curl -H "Content-Type: application/json" -X POST \
-u <CONVERSATION_USERNAME>:<CONVERSATION_PASSWORD> \
-d "@workspace.json" \
"https://gateway.watsonplatform.net/conversation/api/v1/workspaces/<CONVERSATION_WORKSPACE_ID>?version=2017-05-26"
- Create a new Discovery environment by using your Discovery username and password (not your Bluemix credentials or your Conversation credentials). Save the
environment_id
in a note.
curl -H "Content-Type: application/json" -X POST \
-u <DISCOVERY_USERNAME>:<DISCOVERY_PASSWORD> \
-d '{ "name": "demoEnvironment", "description": "from Conversation with Discovery - OpenWhisk", "size": 0}' \
"https://gateway.watsonplatform.net/discovery/api/v1/environments?version=2017-07-19"
- Get the configuration for your environment. Here, we are just using the default configuration. Substitute your
environment_id
from the last step. Save theconfiguration_id
in a note.
curl -X GET \
-u <DISCOVERY_USERNAME>:<DISCOVERY_PASSWORD> \
"https://gateway.watsonplatform.net/discovery/api/v1/environments/<ENVIRONMENT_ID>/configurations?version=2017-07-19"
Your environment might still be configuring, so wait about a minute before continuing.
- Create a new collection by using your Discovery username, password, configuration_id, and environment_id.
curl -X POST -H "Content-Type: application/json" \
-u <DISCOVERY_USERNAME>:<DISCOVERY_PASSWORD> \
-d '{ "name": "demoCollection", "description": "from Conversation with Discovery - OpenWhisk", "configuration_id": "<CONFIGURATION_ID>" }" \
"https://gateway.watsonplatform.net/discovery/api/v1/environments/<ENVIRONMENT_ID>/collections?version=2017-07-19"
- From the root directory of your repository, unzip the manual training documents.
unzip manualdocs.zip
- There are >200 files, so use a for-loop to send them to Watson. Substitute your Discovery username, password, environment_id, and collection_id.
cd manualdocs
for file in *.json
do
curl -X POST -u <DISCOVERY_USERNAME>:<DISCOVERY_PASSWORD> \
-F "file=@$file" \
"https://gateway.watsonplatform.net/discovery/api/v1/environments/<ENVIRONMENT_ID>/collections/<COLLECTION_ID>/documents?version=2017-07-19"
done
- Install the Openwhisk Command Line Interface
- Return to the home directory. Add the two actions to OpenWhisk
cd ..
wsk action create conversation actions/conversation.js --web true
wsk action create discovery actions/discovery.js --web true
- Edit actions/conversationParams.json and actions/discoveryParams.json to include your usernames, passwords, workspace_id, environment_id and collection_id for the Conversation and Discovery services.
{
"username": "<CONVERSATION_USERNAME>",
"password": "<CONVERSASTION_PASSWORD",
"workspace_id": "<WORKSPACE_ID>"
}
{
"username": "<DISCOVERY_USERNAME>",
"password": "<DISCOVERY_PASSWORD",
"environment_id": "<ENVIRONMENT_ID>",
"collection_id": "<COLLECTION_ID"
}
- Use these documents to create default parameters from the command line:
wsk action update conversation --param-file action/conversationParams.json
wsk action update discovery --param-file action/discoveryParams.json
- Create a sequence using the two actions:
wsk action create conversation-with-discovery-sequence --sequence conversation,discovery --web true
-
Navigate to the API Management page on OpenWhisk.
-
Make a path for your operation, change the HTTP verb to POST, and select your sequence as the action
-
Activate the slider next to "Require applications to autheticate via API key"
-
Ensure that the slider next to "Enable CORS so that browser-based applications..." is also activated
-
Click "Save & expose"
-
Under "Sharing Outside of Bluemix Organization", click "Create API Key"
-
Navigate to the "Summary" tab on the left-hand side
-
Copy the link under "Route" to a note, and add "/submit" or the name of the path associated with your POST action to the end of the URL.
-
Link your API to your React App:
Create a file named.env
. Copy the following and paste it into your.env
, substituting your API URL.
REACT_APP_API_URL="<Your API URL>"
We have to add REACT_APP_ to the name of the environment variable so React will substitute in the value during the build.
- In the root directory of your repository, install the project dependencies.
npm install
- Create an optimized build of your project. During this stage, your environment variable will be inserted into App.js for use by your components.
npm run build
All that's left is to serve your static files locally. You should see the project running in a new tab!
npm start
See CONTRIBUTING.
This sample code is licensed under Apache 2.0. Full license text is available in LICENSE.