COVID-19 FAQ Chatbot with Rasa, Docker and Heroku
COVID-19 FAQ Chatbot with Rasa, Docker and Heroku
This is undoubtedly a unique moment in the history of the world we know. Most of us have never experienced something even close. Travelling, socialising, and just do normal things were never questioned before.
As of today (March 29th) we hardly see the end of this, but commitment, hope and dreams make us endure.
This article presents how to create a FAQ chatbot powered by Rasa, built with Docker and deployed on Heroku.
COVID-19 FAQ Chatbot
The COVID-19 FAQ Chatbot answers questions about the COVID-19 virusperosa.github.io
Rasa is an open-source machine learning Python framework to build powerful chatbots which can be deployed on various channels (a Web site, Facebook, Telegram, and many more supported).
First download and install Rasa, configure your Python virtual environment and init your project
mkdir covid-19-chatbot
rasa init
First important step is to instruct the ‘MemoizationPolicy’ (which memorises the conversations as they take place) to be forgetful (in config.yml) and only considers the last user message:
policies:
- name: MemoizationPolicy
max_history: 1
- name: MappingPolicy
From then on it is pretty straightforward: follow Rasa doc to define stories and intents, you only need to pay attention to the “Response Selector” component.
The ResponseSelector NLU component is designed to make it easier to handle dialogue elements like Small Talk and FAQ messages in a simple manner. By using the ResponseSelector, you only need one story to handle all FAQs, instead of adding new stories every time you want to increase your bot’s scope.
Each intent is associated with a question (which you should provide in different forms to take advantage of the NLP capabilities). Intent names have the ‘faq’ prefix.
## intent: faq/what_is
- what is Corona virus?
- define Corona virus
- what is COVID-19?
- define COVID-19
Link the intent to the Chatbot response still using the ‘faq’ prefix which allows the ResponseSelector to capture and process them.
## what is
* faq/what_is
- Coronavirus (COVID-19) is a new coronavirus that has not been previously identified. ‘CO’ stands for ‘corona,’ ‘VI’ for ‘virus,’ and ‘D’ for disease.
The very important point here is you only create a single story for all questions (see stories.md)
## Some question from FAQ
* faq
- respond_faq
Train the model (‘rasa train’) and test it (‘rasa shell’)… obviously add more Q&A to make it more interesting (and more useful).
Dockerizing the Chatbot
Let’s create a Docker image to run the Rasa Chatbot (and later deploy it)
# from rasa base image
FROM rasa/rasa:1.8.0
# copy all source and the Rasa generated model
COPY . /app
# inform which port will run on
EXPOSE 5005
# script to run rasa core
COPY startup.sh /app/scripts/startup.sh
# script to run rasa shell
COPY shell.sh /app/scripts/shell.sh
USER root
RUN chmod a+x /app/scripts/startup.sh
RUN chmod a+x /app/scripts/shell.sh
ENTRYPOINT []
ENV shell_mode false
# launch script (rasa shell or rasa run)
CMD sh -c 'if [ "$shell_mode" = false ]; then /app/scripts/startup.sh; else /app/scripts/shell.sh; fi'
Now take a look at ‘startup.sh’: the script conveniently defines the rasa execution and required parameters
rasa run -p $PORT --cors "*" --enable-api --debug
A very interesting thing happens in the startup script:
- define the PORT where Rasa will run: here we inject the env variables $PORT. This is extremely important when deploying on Heroku as the port is dynamic and assigned at deployment time
Build the image and run it first locally to verify you can talk to it
docker build -t perosa/rasa-covid .
docker run -p 5005:5005 -e PORT=5005 -e shell_mode=true -it --rm perosa/rasa-covid
Heroku time
It is now time to push and run it on Heroku: with a Dockerized application in place, the obvious choice is the Heroku Container Registry.
First, create the Heroku application (using the Dashboard or Heroku CLI), then make sure to log into the registry
heroku container:login
Tag the image and push it
# tag the image
docker tag perosa/rasa-covid registry.heroku.com/rasa-covid/web
# push the image
docker push registry.heroku.com/rasa-covid/web
# deploy
heroku container:release web -a rasa-covid
You can follow the deployment logs on Heroku, if it is all going well you should be able to see “State changed from starting to up”
Ping it with https://{app name}.herokuapp.com
and get a response from your Rasa chatbot :-)
Talk to the Chatbot
It is not fun if you cannot talk to your Chatbot, right?
The quickest and easiest way is to leverage on the REST API: send a POST with a message
curl -d ‘{“sender”: “Beppe”, “message”: “Hi”}’ -H “Content-Type: application/json” -X POST https://rasa-covid.herokuapp.com/webhooks/rest/webhook
---------
[{“recipient_id”:”Beppe”,”text”:”Hello”},{“recipient_id”:”Beppe”,”text”:”I can answer questions about the Corona virus.”}]
Explore the Rasa channels to choose what is the best home for your Bot and engage your audience.
Hope this was useful (source code is here), ping me for questions and stay safe 🙏