This sample provides a tutorial and the code to set up an FAQ chatbot in SAP Conversational AI (CAI) that learns question and answer pairs from Stack Overflow. The learning process is based on a Kubernetes CronJob. It receives all questions with a specific tag from Stack Overflow, stores the question IDs in a simple SQL database, and adds new questions to the SAP CAI bot. The SQL database is necessary to keep track of which question from Stack Overflow corresponds to which question in the bot knowledge.
The following code is included in this sample:
- Knowledge database: simple MS SQL database that stores all IDs of the questions the bot can answer
- Bot observer tool: node.js server that displays the current bot knowledge, the Stack Overflow content, and the knowledge database content. This server can also be used to verify that everything was set up correctly.
- Update functionality: Kubernetes CronJob and JS-script to update the bot automatically
This sample demonstrates how to:
- Set up an FAQ chatbot in SAP Conversational AI
- Connect the FAQ chatbot to a communication platform, e.g., Slack
- Deploy the SQL knowledge database to the Kyma runtime
- Deploy the bot observer tool to the Kyma runtime and connect everything together
- Deploy the Kubernetes CronJob to the Kyma runtime that updates the bot automatically
- SAP BTP, Kyma runtime instance
- Docker
- kubectl configured to use the
KUBECONFIG
file downloaded from the Kyma runtime - SAP Conversational AI
-
Create a new FAQ chatbot in SAP CAI by performing Step 3 and Step 4 of the Conversational AI FAQ chatbot beginner tutorial:
- Go to https://cai.tools.sap/ and sign in.
- Create a new bot and choose
Retrieve Answers from FAQ documents
. - Give the bot a name, e.g.,
karl-kyma
orkyma-faq-bot
. - For the data policy choose
non-personal
andnon-vulnerable
. - Choose the bot visibility to be
public
and press theCreate
button.
-
On the Train tab, click the
Upload Document
button and upload the kymafaq.csv that you can find in this Git repository. After the upload you should be able to click on the document and see that there is already one question-answer pair. -
On the Build tab, go to
customer-satisfaction-reply
and scroll down to the sectionif user chooses no
. There, delete the two existing actions, click onChoose Message Type
, then onText
and paste the following message:Sorry, that I couldn't find an answer to your question. Please post it on Stack Overflow.
-
On the Build tab, go to
faq
and make the following three changes to the actions:-
Edit the first action and replace the pairs of two asterisks by single asterisks
*{{qna.faq.answers.0.question}}* {{qna.faq.answers.0.answer}}
-
Edit the second action and also replace the pairs of two asterisks by single asterisks
*Did you mean to ask?* 1) {{qna.faq.answers.0.question}} 2) {{qna.faq.answers.1.question}} 3) {{qna.faq.answers.2.question}}
-
Delete the third action. Scroll down to
if ?qna.faq.max_confidence lower-than .05
. There, delete the existing replyI was not able to find what you were looking for in my document.
by clicking on the trash can with the labelDelete action
.
-
The FAQ chatbot can be connected to multiple communication platforms. In the previously created FAQ chatbot, go to the Connect tab and explore the options. In this tutorial, the bot is connected to Slack following the Deploy an SAP Conversational AI Chatbot on Slack tutorial.
-
In the Connect tab, click on
Slack
. -
Click on
Create a Slack app
and follow the instructions there:- Click on the link which will bring you to https://api.slack.com/apps.
- There, choose
Create New App
and selectFrom scratch
. - Give the app a name (e.g.,
Karl Kyma
) and select your workspace. - Click
Create App
.
-
Click on
Get your credentials
(again at the CAI platform) and follow the instructions there:- In the Slack API go to
Settings
andBasic Information
. - In the section
App Credentials
copy theClient ID
and theClient Secret
.
- In the Slack API go to
-
On the CAI platform, insert the
Client ID
and theClient Secret
, and clickGET TOKENS
. -
Copy the
OAuth URL
and theEvent URL
to your clipboard. -
Click on
Set the OAuth redirect URL
and follow the instructions there:- In the Slack API go to
OAuth & Permissions
. - Click
Add New Redirect URL
and add theOAuth URL
you just copied. - Click
Save URLs
.
- In the Slack API go to
-
Click on
Enable Interactive Components
and follow the instructions there:- In the Slack API go to
Interactivity & Shortcuts
. - Turn on the
Interactivity
. - In the field
Request URL
paste theEvents URL
you just copied. - Click
Save Changes
.
- In the Slack API go to
-
Click on
Enable OAuth Permissions
and follow the instructions there:- In the Slack API go to
OAuth & Permissions
. - Via the
Add an OAuth Scope
button add the followingBot Token Scopes
to the bot:- channels:history
- channels:read
- chat:write
- groups:history
- groups:read
- groups:write
- im:history
- im:read
- users:read
- users:write
- In the Slack API go to
-
Click on
Fill the Events redirect URL
and follow the instructions there:- In the Slack API go to
Event Subcriptions
. - Enable Events and fill the
Event URL
you copied earlier into theRequest URL
field. - In the section
Subscribe to bot events
, add the following three events:- message.im
- message.groups
- message.channels
- Click
Save Changes
.
- In the Slack API go to
-
Give the bot a discription, add an app icon, and add it to the Slack workspace:
- In the Slack API go to
Basic Information
. - Scroll down to
Display Information
and add a short discription and an app icon. - Click
Save Changes
. - Scroll up again and click
Install to Workspace
. ClickAllow
and then you are done in the Slack API.
- In the Slack API go to
-
Go back to the CAI platform and click on
Add my bot to Slack
. There is a link, you need to follow. Then, click again onallow
and you will get the feedback that the bot was successfully added to the workspace. -
Open Slack, go to the workspace you selected further above and go to a channel you want to add the bot to.
-
Click on the cluster of profile pictures at the top right. Go to the tab
Integrations
, click onAdd an App
, and search for the name of your bot. Then, simply clickAdd
and the bot will be added to this channel. -
Verify that everything was set up correctly by sending some greeting like "Hello" and the question "What is your name?" in the Slack channel. The bot should respond to both messages.
The first step will be to deploy the knowledge database. Make sure that you have a running Docker installation and a Docker Hub account. The proceed with the following steps:
-
Switch to the directory
knowledge-database/database-mssql
. -
Build the database image:
docker build -t yourDockerHubAccount/bot-db -f docker/Dockerfile .
-
Push the database image to your Docker Hub account:
docker push yourDockerHubAccount/bot-db
If you do not have an own Docker Hub account, you can also pull the built images from my account
lasseurban
. -
Within the
deployment.yaml
in the k8s directory, adjust the value ofspec.template.spec.containers.image
, commented with#change it to your image
, to use your Docker image. -
Go to the SAP BTB Cockpit and from there to your Kyma dashboard.
-
Download the Kubeconfig and export it. If you don't know how to do this, check step 3 of this tutorial.
export KUBECONFIG=<KUBECONFIG_FILE_PATH>
-
Create a namespace and a context for this namespace (make sure that you replace ******* with your cluster ID):
kubectl create namespace chatbot
kubectl get namespaces
kubectl config set-context chatbot-context --namespace=chatbot --cluster=*******.kyma-stage.shoot.live.k8s-hana.ondemand.com --user=OIDCUser
kubectl config use-context chatbot-context
-
From the Command Line, deploy the database secret, the persistent volume claim, and the database itself:
kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/pvc.yaml
kubectl apply -f k8s/deployment.yaml
NOTE: The order of deploying is important and it might take a few minutes until everything is up and running.
If you want to have more details about what is going on here, please check out this tutorial.
Next, an API function must be deployed to allow manipulation of the database from outside. This is very similar to the api-mssql-function in this Git repository.
-
Change to the directory
knowledge-database/api-mssql-function
. -
Within the
k8s/deployment.yaml
in the k8s directory, adjust the value ofspec.env.host.value
, commented with#change chatbot to your namespace
. -
Deploy the function:
kubectl apply -f k8s/deployment.yaml
-
Verify that the Function is up and running:
kubectl get function api-mssql-function
You should get something like this:
NAME CONFIGURED BUILT RUNNING RUNTIME VERSION AGE api-mssql-function True True True nodejs14 1 XXs -
You can now use the API function (ensure that you replace
{cluster-domain}
with your domain):-
You can get all questions in the database (should be empty at the moment):
curl -X GET "https://api-mssql-function.{cluster-domain}/"
-
You can also get a single question with a specific ID:
curl -X GET "https://api-mssql-function.{cluster-domain}/ID"
-
Or, you can delete one or multiple questions from the database:
for x in {1..3}; do curl -X DELETE "https://api-mssql-function.{cluster-domain}/$x"; done
-
Now, it is time to deploy the bot observer tool. This is a simple node.js server that displays the current bot knowledge (requested from SAP CAI), the Stack Overflow content, and the knowledge database content.
-
Change to the directory
bot-observer-tool
. -
Build the bot observer tool image:
docker build -t yourDockerHubAccount/bot-observer-tool -f docker/Dockerfile .
-
Push the bot observer tool image to your Docker Hub account:
docker push yourDockerHubAccount/bot-observer-tool
If you do not have an own Docker Hub account, you can also pull the built images from my account
lasseurban
. -
Open the file
k8s/secret.yaml
with your favorite editor and make the following adjustments:Name value cai-credentials-id 1. Go to your chatbot in SAP Conversational AI.
2. Select your bot, click onSettings
and go to the tabTokens
.
3. Next toDesigntime APIs
click on theGenerate
button.
4. SelectClient Credentials
and clickNext
.
5. Copy theClient ID
and paste it in the secret.yaml as the value ofcai-credentials-id
.cai-credentials-secret Copy the Client Secret
and paste it in the secret.yaml as the value ofcai-credentials-secret
.bot-url The bot URL is needed to communicate with the FAQ bot in SAP Conversational AI and needs to be of this format: {user_slug}/bots/{bot_slug}/versions/{version_slug}/qna/topic/knowledge_sources/{knowledge_source_id}/answers
- The user_slug can be found in theSettings
of the CAI bot on the tabTokens
at the bottom.
- The bot_slug is the name of the bot and can be found in theSettings
of the CAI bot on the tabBot Options
.
- The version_slug can be found in theSettings
of the CAI bot on the tabVersions
(the value is most likelyv1
).
- The knowledge_source_id is the name of the csv-file that you uploaded in step 1.2
(if you took the one from this repo, the value iskymafaq
).
Please check the SAP CAI API reference for more information.cai-x-token 1. Go back to the Settings
of the CAI bot and to the tabTokens
.
2. Copy theDeveloper token
and paste it in the secret.yaml as the value ofcai-x-token
.stack-tag Choose a tag that is used for questions the bot should learn, e.g., kyma-runtime
.stack-key 1. Go to the Stack Overflow instance you want to connect to.
2. Next to the search bar at the top click on your profile picture to access your profile.
3. Click onEdit profile and settings
.
4. Click onAPI access keys
.
5. Choose a name and clickCreate access key
.
6. Copy theKey
and paste it in the secret.yaml as the value ofstack-key
.db-name Use the value spec.env.database.value
from theknowledge-database/api-mssql-function/k8s/deployment.yaml
file.db-host Use the value spec.env.host.value
from theknowledge-database/api-mssql-function/k8s/deployment.yaml
file.db-username Use the value spec.env.username.value
from theknowledge-database/api-mssql-function/k8s/deployment.yaml
file.db-password Use the value spec.env.password.value
from theknowledge-database/api-mssql-function/k8s/deployment.yaml
file. -
From the Command Line, deploy the bot secret, the bot observer tool itself and the apirule to access the tool:
kubectl apply -f k8s/secret.yaml
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/apirule.yaml
NOTE: The order of deploying is important and it might take a minute until everything is up and running.
-
In the Kyma dashboard, select the namespace and click on
API Rules
in the menu bar on the left. -
There should be two entries. Click on the link next to
bot-observer-apirule
. -
A new tab should be opened and you should see the main API endpoint of the node.js server
bot-observer
. -
Check if everything is working by accessing all the endpoints (you should just append the path of every endpoint to the URL of the node.js server):
-
You can check all the knowledge the bot currently has:
https://bot-observer.{cluster-domain}/bot
(should return one default question about the bots name)
-
You can also check the bot credentials that are automatically requested from SAP CAI:
https://bot-observer.{cluster-domain}/botcredentials
-
You can also request all the knowledge from Stack Overflow that is tagged with the tag that you specified in the secret.yaml:
https://bot-observer.{cluster-domain}/stack
-
Or, you can list all the entries of the database:
https://bot-observer.{cluster-domain}/db
(should be empty at the moment)
-
-
Change to the directory
update-bot
. -
Build the bot update image:
docker build -t yourDockerHubAccount/bot-update -f docker/Dockerfile .
-
Push the bot observer tool image to your Docker Hub account:
docker push yourDockerHubAccount/bot-update
If you do not have an own Docker Hub account, you can also pull the built images from my account
lasseurban
. -
From the Command Line, deploy the bot secret:
NOTE: You only need to do this, if you have not already deployed the bot secret while deploying the bot observer tool in step 5.5. Please doublecheck now wether you have correctly deployed the bot secret. If you are unsure you can use the command
kubectl get secrets
. You should see the bot-secret of the TypeOpaque
with 10 data items.If you have not already done the deployment of the secret, go to step 5.5 and make the described adjustments in
k8s/secret.yaml
. Afterwards, use the following command to deploy the secret:kubectl apply -f k8s/secret.yaml
-
Deploy the Kubernetes Job or the CronJob. Decide wether you want to update the chatbot immediately (use the job.yaml) or repetitively with a fixed schedule (use the cronjob.yaml). Both execute the same update-bot functionality.To use the simple Kubernetes Job, do the following steps:
-
Within the
job.yaml
in the k8s directory, adjust the value ofspec.template.spec.containers.image
, commented with#change it to your image
, to use your Docker image. -
From the Command Line, deploy the Job:
kubectl apply -f k8s/job.yaml
-
If you want to kill the job, use the following command:
kubectl delete -f k8s/job.yaml
To use the Kubernetes CronJob, do the following steps:
-
Within the
cronjob.yaml
in the k8s directory, adjust the value ofspec.template.spec.containers.image
, commented with#change it to your image
, to use your Docker image. -
In the same file, adjust the value of
spec.schedule
, commented with#change it to your schedule
. Information on how you do that, can be found here. For example, you could set the schedule to "0 0 * * *" to update the bot every day at midnight. -
From the Command Line, deploy the CronJob:
kubectl apply -f k8s/cronjob.yaml
-
You can check the status of the CronJob:
kubectl get cronjobs
-
If you want to kill the CronJob, use the following command:
kubectl delete -f k8s/cronjob.yaml
-
The development of this chatbot and the tutorial was a project contributed by Lasse Urban, who is a vocational training student at SAP. He spent one rotation in the STAR (Student Training and Rotation) Program with the Kyma Runtime Adoption team from April 2021 until October 2021.