Conversational FAQ Chatbot using RASA

Conversational FAQ Chatbot using RASA

Turning FAQs Into Fun: A Step-by-Step Guide to Building a Chatbot with RASA

·

6 min read

Backstory

A while back, I received a message from a friend. He was facing an issue that needed solving. See, Newton had a problem. He had created a bot that was to "Retrieve information from a real estate website, then if someone asks to buy a home from a particular location, it will recommend the ones that are listed on the website based on the user's location preference...". He had succeeded in all things, save for one slight problem that was hindering his migration to production. The bot was using its domain knowledge and giving recommendations that were not on the website. It was generalizing. This got me thinking: is there a way this could be mitigated, i.e., was there a way to have the best of both worlds? Use the language model's robust training set for contextual feedback and conversational edge while limiting its source of answers to a source of truth that could easily be controlled by the user.

This is what led to my discovery of RASA. A popular open-source framework for building chat and voice-based AI assistants.

In this step-by-step guide, we'll delve into the enchanting realm of RASA, a magic wand for creating conversational FAQ chatbots. If you haven't already, be sure to check out my other article in this series, where we talked about Building Your Own Private ChatGPT and the future of privacy-focused chatbots.

This is SheCodeAfricaNairobi. I am Agin; you are most welcome, and a happy new year to you.

Introduction: Embracing the AI Wave

As artificial intelligence swirls around us, chatbots have become the cool kids on the digital block. They're the witty assistants, the problem-solvers, and the virtual companions that make tech feel a bit more like magic. RASA, standing tall in this revolution, has become a go-to for developers venturing into the world of conversational chatbots.

Brewing the Magic: Let's Code!

  1. Install RASA: The Enchantment Begins

    • Open your terminal and cast the spell:
    pip install rasa
  1. Create a New Project: Your Bot's Castle

    • Initiate your project. This command will set up a complete assistant for you with some example training data. It's that easy.
    rasa init

It creates the following files:

.
├── actions
   ├── __init__.py
   └── actions.py
├── config.yml
├── credentials.yml
├── data
   ├── nlu.yml
   └── stories.yml
├── domain.yml
├── endpoints.yml
├── models
   └── <timestamp>.tar.gz
└── tests
   └── test_stories.yml

Before we embark on our journey, let's get acquainted with the major files that constitute the heart and soul of RASA. These are;

  1. **domain.yml:**The Wizard's Spell Book

    • This magical file defines the universe of your chatbot. It outlines intents (what users might say), entities (what your chatbot needs to know), and responses (how your chatbot should reply).
    intents:
      - view_transactions
      - contact
      - payment_status

    entities:
      - view_transactions
      - contact
      - payment_status

    actions:
      - affirm
      - action_view_transactions
      - action_contact
      - action_payment_status

    responses:
      utter_greet:
        - text: "Greetings, SCA Nairobi reader!"

      utter_view_transactions:
      - text: "On the Payments tab on the left of your screen."

      utter_contact:
      - text: "Reach us on Twitter/X @SCAnairobi"

     utter_iamabot:
      - text: "I am a Chatbot, powered by SheCodeAfrica - Nairobi."
      - text: "Hi there, I am SCA's AI powered chatbot. I can answer your most pressing FAQs"
  1. nlu.**yml:**The Potion Recipes

    • This is where you teach your chatbot to understand user messages. Each line represents examples of how users might express a particular intent. Be as detailed as possible. I found that the more questions you asked, the better they responded.
    - intent: payment_status
      examples: |
        - What do the payment status naming schemes mean?
        - the naming schemes in payment status tab mean what exactly?

    - intent: view_transactions
      examples: |
        - Where can I view my transactions?
        - show me my recent transactions
        - i cant see my transactions

    - intent: contact
      examples: |
        - How do i contact you?
        - how can i reach you?
        - whats the easiest way to reach you?
  1. stories**.yml:**The Epic Journeys

    • Your chatbot's adventures are scripted here. Stories map out different user interactions, helping your bot understand the context and respond appropriately.
    - story: payment status
      steps:
      - intent: greet
      - action: utter_greet
      - intent: payment_status
      - action: utter_payment_status
      - action: utter_goodbye

    - story: contact
      steps:
      - intent: greet
      - action: utter_greet
      - intent: contact
      - action: utter_contact
      - action: utter_goodbye

    - story: view transactions
      steps:
      - intent: greet
      - action: utter_greet
      - intent: view_transactions
      - action: utter_view_transactions
      - action: utter_goodbye
  1. Teach Your Bot: NLU Incantations

    • Populatenlu.yml with intents and examples tailored to your landlord bot's FAQ.
  2. Define the Universe: Editdomain.yml

    • Introduce your bot's domain by defining intents, entities, and responses.
  3. Script the Adventures: Craftstories.yml

    • Outline stories to guide your bot through different user interactions.
  4. Train the Bot: The Training Grounds

    • Train your bot's mind with:
    rasa train

rasa trainstores the trained model in the models/l folder by default. It will also assign the model a name using the convention,<timestamp>.tar.gz but you can specify your name using the --fixed-model-name flag.

  1. Summon the Chatbot: Unleash the Magic

    • Let your bot converse with the world. This command, by default, will load up the latest trained model. However, using the --model flag, you can specify a different model.

        rasa shell
      

    • Alternatively, you can start an interactive learning session by running rasa in interactive mode. This will first train a model and then start an interactive shell session. You can then correct your assistants' predictions as you talk to it.

        rasa interactive
      

    • During interactive learning, Rasa plots the current conversation and a few similar conversations from the training data to help you keep track of where you are. This trail visualization can be viewed at http://localhost:5005/visualization.html as soon as the session starts. If, however, the diagram takes a long time to generate, then simply skip it usingrasa interactive --skip-visualization.

Follow-up Steps

You can start a server running your trained model using rasa run .

Moreover, you can deploy your assistant, built with Rasa. This will allow you to make your assistant available to users and set you up with a production-ready environment. I used Docker containers for deployment but there are other options available as well

Conclusion: Your Chatbot's Grand Debut

Congratulations! You've just brought a conversational wizard to life using RASA. Your FAQ bot for landlord property management is ready to dazzle users with its witty responses and problem-solving prowess. As you embark on this magical journey, remember: the more personality you infuse into your bot, the more enchanting the conversations will be. So dig into those core files and provide as many examples as you can. Happy chatting, fellow sorcerers!

That has been it for me. See you in the next article! Be Safe. Be kind. Peace.