NOTE: This code pattern is based on the chatbot that is being used for the IBM Developer mobile application.
In this Code Pattern, we will create a chatbot using Watson Assistant, Elasticsearch, and Node.js. The Node.js application can run on either Kubernetes or Cloud Foundry. This chatbot uses TV shows data from TV Maze to make recommendations and provide show information.
After completing this pattern, you will understand how to:
- Create a chatbot end to end
- Deploy and run a Node.js application on Kubernetes and/or Cloud Foundry
- User interacts with chatbot from the React UI of the mobile application by asking a question via text.
- React UI sends User's message to the Node.js backend on Cloud Foundry.
- The Node.js backend sends the message to Watson Assistant to determine the intent and entities of the User's message.
- The Node.js backend querys the Elasticsearch database based on the intents and entities processed by Watson Assistant.
- The response and results are sent to the React UI.
Follow these steps to setup and run this code pattern on the Cloud. The steps are described in detail below.
Clone the elasticsearch-chatbot
repo locally. In a terminal, run:
git clone https://github.com/IBM/elasticsearch-chatbot
cd elasticsearch-chatbot
- IBM Cloud account
- IBM Cloud CLI
- NodeJS
- NPM
- Relevant Node Components: Use
npm install
in/app
and/web
- Docker (if using Kubernetes)
- To allow changes to
/app
or/web
, create a repo on Docker Cloud where the new modified containers will be pushed to.
NOTE: If a new repo is used for the Docker containers, the container
image
will need to be modified to the name of the new repo used in deploy-app.yml and/or deploy-web.yml.
export DOCKERHUB_USERNAME=<your-dockerhub-username>
docker build -t $DOCKERHUB_USERNAME/elasticsearch-chatbot-app:latest app/
docker build -t $DOCKERHUB_USERNAME/elasticsearch-chatbot-web:latest web/
docker login
docker push $DOCKERHUB_USERNAME/elasticsearch-chatbot-app:latest
docker push $DOCKERHUB_USERNAME/elasticsearch-chatbot-web:latest
- Provision the IBM Cloud Kubernetes Service and follow the set of instructions for creating a Container and Cluster based on your cluster type,
Standard
vsLite
.
- Login to the IBM Cloud using the Developer Tools CLI:
NOTE use
--sso
if you have a single sign on account, or delete for username/password login
ibmcloud login --sso
- Set the Kubernetes environment to work with your cluster:
ibmcloud cs cluster-config $CLUSTER_NAME
The output of this command will contain a KUBECONFIG environment variable that must be exported in order to set the context. Copy and paste the output in the terminal window. An example is:
export KUBECONFIG=/home/rak/.bluemix/plugins/container-service/clusters/Kate/kube-config-prod-dal10-<cluster_name>.yml
-
Provision a Database for Elasticsearch.
-
Provision a Watson Assistant.
-
Import the Skill data from
skill-TV-Shows.json
to your instance of Watson Assistant. Create an Assistant that incorporates theTV Shows
skill.
- Create Service Credentials for the Elasticsearch Database.
- Update the following in the deploy-app.yml file:
ELASTIC_BASE_URL
with theconnection -> https -> composed
URL from your generated Service Credentials from the Elasticsearch Database.
WATSON_USERNAME
with the username of the Service Credentials of your Assistant of Watson Assistant.
WATSON_PASSWORD
with the password of the Service Credentials of your Assistant of Watson Assistant.
WATSON_API_URL
with the assistant URL of the Assistant Details of your Assistant of Watson Assistant.
NOTE: Only include the part of the URL up to and including
/api
WATSON_ASSISTANT_ID
with the assistant ID of the Assistant Details of your Assistant of Watson Assistant.
- Get the workers for your Kubernetes cluster:
ibmcloud cs workers <mycluster>
and locate the Public IP
. This IP is used to access the App and UI on the Cloud. Update the env
value for REACT_APP_API_BASE_URL
in deploy-web.yml to http://<Public IP>:32001
.
- To deploy the services to the IBM Cloud Kubernetes Service, run:
kubectl apply -f deploy-app.yml
kubectl apply -f deploy-web.yml
## Confirm the services are running - this may take a minute
kubectl get pods
- To populate the Elasticsearch Database, run
curl http://PUBLIC_IP:32001/api/v1/data -H "Content-Type: application/json" -X PUT -d '{"total": <TOTAL>}'
Total is optional and if used, refers to the maximum number of potential shows to add to the database.
NOTE: This chatbot only works for US shows and streaming services. There are maximum 42,000+ shows available through the API used.
- Use
http://PUBLIC_IP:32000
to access the React UI.
-
Run
ibmcloud cs cluster-get <CLUSTER_NAME>
and locate theIngress Subdomain
andIngress Secret
. This is the domain of the URL that is to be used to access the App and UI on the Cloud. Update theenv
value forREACT_APP_API_BASE_URL
in deploy-web.yml tohttps://api.<Ingress Subdomain>
. In addition, update thehost
andsecretName
in ingress-app.yml toapi.<Ingress Subdomain>
and<Ingress Secret>
and ingress-web.yml to<Ingress Subdomain>
and<Ingress Secret>
. -
To deploy the services to the IBM Cloud Kubernetes Service, run:
kubectl apply -f deploy-app.yml
kubectl apply -f deploy-web.yml
## Confirm the services are running - this may take a minute
kubectl get pods
## Update protocol being used to https
kubectl apply -f ingress-app.yml
kubectl apply -f ingress-web.yml
- To populate the Elasticsearch Database, do one of the following:
- Run
curl https://api.<INGRESS_SUBDOMAIN>/api/v1/data -H "Content-Type: application/json" -X PUT -d '{"total": <TOTAL>}'
Total is optional and if used, refers to the maximum number of potential shows to add to the database.
NOTE: This chatbot only works for US shows and streaming services. There are maximum 42,000+ shows available through the API used.
- Use one of the provided data sets in
/data
by runningcurl https://api.<INGRESS_SUBDOMAIN>/api/v1/data -H "Content-Type: application/json" -X PUT -d '@data/<DATA_FILE>'
<DATA_FILE>
can be eithersmall-dataset.json
orlarge-dataset.json
-
Use
https://<INGRESS_SUBDOMAIN>
to access the React UI. -
Here are some example messages to send to the chatbot:
What comedy shows are on ABC tonight?
What drama shows do you recommend?
Tell me more about Game of Thrones
When is Jeopardy on?
-
Provision two SDK for Node.js applications. One will be for
./app
and the other will be for./web
. -
Provision a Database for Elasticsearch.
-
Provision a Watson Assistant.
-
Import the Skill data from
skill-TV-Shows.json
to your instance of Watson Assistant. Create an Assistant that incorporates theTV Shows
skill.
- Create Service Credentials for the Elasticsearch Database.
- Connect the Elasticsearch Database and Watson Assistant with the Node.js app by going to Connections on the dashboard of the app provisioned and clicking Create Connection. Locate the Elasticsearch Database and Watson Assistant you provisioned and press connect.
- Update
REACT_APP_API_BASE_URL
in./web/.env
tohttps://<APP-NAME>.<APP-DOMAIN>
-
From the
./web
directory, runnpm run build
so that the React front end can be run on Cloud Foundry with a static buildpack. -
Update the following in the manifest.yml file:
name
for both Cloud Foundry application names provisioned from Step 1.
services
with the name of the Elasticsearch Database and Watson Assistant services provisioned from Steps 2 and 3.
ELASTIC_BASE_URL
with theconnection -> https -> composed
URL from your generated Service Credentials from the Elasticsearch Database.
WATSON_USERNAME
with the username of the Service Credentials of your Assistant of Watson Assistant.
WATSON_PASSWORD
with the password of the Service Credentials of your Assistant of Watson Assistant.
WATSON_API_URL
with the assistant URL of the Assistant Details of your Assistant of Watson Assistant.
NOTE: Only include the part of the URL up to and including
/api
WATSON_ASSISTANT_ID
with the assistant ID of the Assistant Details of your Assistant of Watson Assistant.
-
To deploy the services to IBM Cloud Foundry, go to one of the dashboards of the apps provisioned from Step 1 and follow the Getting Started instructions for connecting and logging in to IBM Cloud from the console (Step 3 of Getting Started). Once logged in, run
ibmcloud app push
from the root directory. -
To populate the Elasticsearch Database, do one of the following:
- Run
curl https://<APP-NAME>.<APP-DOMAIN>/api/v1/data -H "Content-Type: application/json" -X PUT -d '{"total": <TOTAL>}'
Total is optional and if used, refers to the maximum number of potential shows to add to the database.
NOTE: This chatbot only works for US shows and streaming services. There are maximum 42,000+ shows available through the API used.
- Use one of the provided data sets in
/data
by runningcurl https://<APP-NAME>.<APP-DOMAIN>/api/v1/data -H "Content-Type: application/json" -X PUT -d '@data/<DATA_FILE>'
<DATA_FILE>
can be eithersmall-dataset.json
orlarge-dataset.json
-
Use
https://<WEB-NAME>.<WEB-DOMAIN>
to access the React UI. -
Here are some example messages to send to the chatbot:
What comedy shows are on ABC tonight?
What drama shows do you recommend?
Tell me more about Game of Thrones
When is Jeopardy on?
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.