Read this in other languages: 한국어
Watson Conversation is now Watson Assistant. Although some images in this code pattern may show the service as Watson Conversation, the steps and processes will still work.
In this developer code pattern we will create a Watson Assistant based chatbot that allows a user to: 1) find items to purchase using Watson Discovery, and 2) add and remove items from their cart by updating a Cloudant NoSQL Database.
When the reader has completed this code pattern, they will understand how to:
NOTE: Python 3 is required for this application to run locally.
NOTE: Perform steps 1-7 OR click the Deploy to IBM Cloud
button and hit Deploy
and then jump to step 6.
NOTE: The Watson Discovery service is not available in the London
region, so this application will require deployment in one of the other IBM Cloud regions.
If you encounter deployment errors, refer to Troubleshooting.
You can use the
View app
button to use a simple web UI to chat. For the Slack integration, use your Slack UI to chat after completing the additional slack configuration. Use the IBM Cloud dashboard to find and manage the app.
Clone the watson-online-store
locally. In a terminal, run:
git clone https://github.com/ibm/watson-online-store
We’ll be using the file data/workspace.json
and the folder
data/ibm_store/
Create the following services:
NOTE: When provisioning Cloudant, for
Available authentication methods
chooseUse both legacy credentials and IAM
:
As you create the IBM Cloud services, you'll need to create service credentials and get the username and password:
Copy the watson-online-store/env.sample
file to watson-online-store/.env
and populate the service
credentials and URLs as you create the credentials:
# Copy this file to .env and replace the credentials with
# your own before running run.py.
# Watson Assistant
ASSISTANT_ID=<add_assistant_workspace>
ASSISTANT_URL=<add_assistant_url>
ASSISTANT_APIKEY=<add_assistant_apikey>
# Cloudant DB
CLOUDANT_USERNAME=<add_cloudant_username>
CLOUDANT_DB_NAME=watson-online-store
CLOUDANT_URL=<add_cloudant_url>
CLOUDANT_IAM_APIKEY=<add_cloudant_iam_apikey>
# Watson Discovery
DISCOVERY_URL=<add_discovery_url>
DISCOVERY_ENVIRONMENT_ID=<add_discovery_environment>
DISCOVERY_COLLECTION_ID=<add_discovery_collection>
DISCOVERY_APIKEY=<add_discovery_apikey>
# <Optional> - Slack
# If not set, only the web UI is available to test.
SLACK_BOT_TOKEN=<add_slack_bot_token>
SLACK_BOT_USER=<add_slack_bot_username>
Launch tool
.Skills
tab.Create new
Import skill
tab.Choose JSON file
, go to your cloned repo dir, and Open
the workspace.json file in data/workspace.json
.Everything
and click Import
.To find the ASSISTANT_ID
for Watson Assistant:
Skills
tab.watson-online-store
.View API Details
.SKILL ID
GUID. Save it for the .env fileOptionally, to view the conversation dialog select the workspace and choose the Dialog tab, here's a snippet of the dialog:
NOTE: If you want to modify the dialogs, there is a important context variable called
get_input
which accepts valuesyes/no
. This controls if there is a need to wait for user input.
Launch the Watson Discovery tool.
wait for the storage to be set up.
Create a new data collection by clicking Upload your own data
Discovery will setup your storage. (You may be invited to upgrade for production quality, but you will be able to use this code pattern with the free trial version).
Seed the content by using either Drag and drop your documents here
or browse from your computer
. Choose the JSON files under data/ibm_store/
.
Click on the left menu Manage Data
icon and then click your newly created Data collection:
Under the Overview
tab, Collection Info
section, click Use this collection in API
and copy the Collection ID
and the Environment ID
into your .env
file as DISCOVERY_COLLECTION_ID
and DISCOVERY_ENVIRONMENT_ID
.
Note: This code pattern includes Slack integration, but if you are only interested in the web UI, you can skip this step.
Create a slack group or use an existing one if you
have sufficient authorization. (Refer to Slack's how-to
on creating new groups.) To add a new bot, go to the Slack group’s application settings
by navigating to https://<slack_group>.slack.com/apps/manage
and selecting the
Custom Integrations menu on the left.
Click on Bots
and then click the green Add Configuration
button.
Give the bot a meaningful name. Note that the @
symbol is pre-populated by Slack
and you do not include that in your .env
configuration file. Save this in .env
as SLACK_BOT_USER
.
Once created save the API Token that is generated into the .env
file
as SLACK_BOT_TOKEN
if you are running locally, or save this if you are using
Deploy to IBM Cloud.
Run /invite <botame>
in a channel to invite the bot, or message it directly.
If you used Deploy to IBM Cloud
, most of the setup is automatic, but not
the Slack configuration. For that, we have to update a few environment variables.
In the IBM Cloud dashboard find the App that was created. Click on Runtime
on the menu and navigate to the Environment variables
tab.
Update the three environment variables:
SLACK_BOT_TOKEN
to the token you saved in Step 6SLACK_BOT_USER
to the name of your bot from Step 6CLOUDANT_DB_NAME
set to watson-online-store
Save the new values and restart the application, watch the logs for errors.
NOTE: python 3 is required. There is no support for python 2.
The general recommendation for Python development is to use a virtual environment (venv). To install and initialize a virtual environment, use the venv
module on Python 3.
# Create the virtual environment using Python. Use one of the two commands depending on your Python version.
$ python -m venv mytestenv # Python 3.X
# Note, it may be named python3 on your system. In that case run:
$ python3 -m venv mytestenv
# Now source the virtual environment. Use one of the two commands depending on your OS.
$ source mytestenv/bin/activate # Mac or Linux
$ ./mytestenv/Scripts/activate # Windows PowerShell
Now go to the cloned repo directory:
cd watson-online-store
Install the Python requirements for this code pattern. Run:
$ pip install -r requirements.txt
# Note, it may be named pip3 on your system. In that case run:
$ pip3 install -r requirements.txt
TIP :bulb: To terminate the virtual environment use the
deactivate
command.
$ deactivate
Finally, run the application:
$ cd python-flask-server
$ python server.py
Start a conversation with your bot:
Add an item to your cart:
Error deploying to IBM Cloud:
This indicates that the Discovery service is still being provisioned. Wait a few minutes and click the Run
button to restart the application.
Large amount of Red Logging info appears.
This is expected. The color for logging in IBM Cloud will be red, regardless of the nature of the message. The log levels are set to Debug
to assist the developer in seeing how the code is executing. This can be changed to logging.WARN
or logging.ERROR
in the python code.
This code pattern is licensed under the Apache License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 and the Apache License, Version 2.