AI Chatbot In Healthcare: The inevitable necessity (Part 1)

on
July 21, 2023

Technologies such as artificial intelligence can greatly assist the health sector in moving forward. WhatsApp bots embedded with natural language processing(NLP), have the potential to make life easier for patients, doctors, nurses, and other health care clinic staff in several ways. These bots go beyond mere rule-based answers to analyze text and speech, understand intent and context, generate responses and continually learn from queries to carry out actual conversations with a user like a human.

AI Chatbot use cases in Healthcare

1. Booking Vaccine slot appointments

Users can converse with the AI bot over Whatsapp or Eka app to check for availability of vaccine centres around them and book a slot for their Covid vaccine dose.

2. Doctor Appointment Booking

Patients can conveniently check the availability of their doctors and book appointments without having to call up the clinic or talk to a human.

3. Assessment of Symptoms

A  chatbot for healthcare industry can be used to ease the process of symptom assessment. A patient can share his or her symptoms with a secure AI bot.

4. Sending Lab Report Updates

WhatsApp chatbot can be used to track lab reports. A doctor might ask a patient to upload their Lab reports before a follow up visit. This entire conversation can happen with the aid of a chatbot.

5. Providing Medicine or Appointment followup Reminders

Bots can directly message patients on their mobile phones to remind them about taking medicines or an upcoming appointments.

6. Answering basic FAQ Questions

A WhatsApp chatbot for healthcare can answer all kinds of patient queries when powered by AI/ML technology. It makes patient experience satisfying, while doctors and medical staff find more time at their disposal.

7. Assistance with Payments

Online payments can become easier for patients with bot assistance while booking a doctor's appointment. Similarly, other multi-layer processes of paying for healthcare facilities can be simplified for patients.

Rasa to the rescue 🤖

We at Eka care are using Rasa, an Open source framework for NLU, dialogue management, and integrations, to build powerful text-based assistants and chatbots for healthcare needs.

Let's get technical now.

Rasa’s API uses ideas from scikit-learn and Keras, and indeed both of these libraries are components of a Rasa application and the text classification is loosely based on the fastText approach. Sentences are represented by pooling word vectors for each constituent token. Using pre-trained word embedding such as GloVe , the trained intent classifiers are remarkably robust to variations in phrasing when trained with just a few examples for each intent.

Rasa NLU

Rasa NLU is an open-source natural language processing tool for intent classification and entity extraction in Conversational AI chatbots. There are some predefined pipelines like spacy_sklearn, tensorflow_embedding, mitie, mitie_sklearn with sensible defaults which work well for most use cases.

For example, take a sentence like,

"I want to book an appointment with an ENT specialist on 1st august at 5 pm"


After passing the command through

python -m rasa_nlu.train -c sample_configs/config_spacy.json

we will get the structured data like

{
   "intent": "book_appointment",
   "entities": {
       "doctor_type": "ENT specialist",
       "date": "01-08-2021",
       "time": "5 pm",
   }
}

Rasa NLU runs wherever you want, so you don’t have to make an extra network request for every message that comes in.

Rasa Core - Dialogue Handling

Rasa Core predicts which action to take from a predefined list. An action can be a simple utterance, i.e. sending a message to the user,  an arbitrary function to execute or an external API call to some other service. When an action is executed, it is passed a tracker instance, and so can make use of any relevant information collected over the history of the dialogue: slots, previous utterances, and the results of previous actions. Actions cannot directly mutate the tracker, but when executed may return a list of events. The tracker consumes these events to update its state. There are a number of different event types, such as SlotSet, AllSlotsReset, Restarted, etc.

Rasa NLU interaction with Core
Rasa NLU interaction with Core

Components of a rasa project

  • data/nlu.yml – This is the file where you will save your training data for extracting the user intent.
  • data/stories.yml - Stories are a type of training data used to train your assistant's dialogue management model. Stories can be used to train models that are able to generalize to unseen conversation paths
  • Actions.py - A custom action can run any code you want, including API calls, database queries etc. They can book appointments, download CoWin certificates, or anything else you can imagine. Any custom action that you want to use in your stories should be added into the actions section of your domain.

If you have multiple form based actions in your code, a general rule of thumb that worked for us was to:

  1. Take the user input in a slot based form in domain.yml

forms:
  cowin_menu_form:
    required_slots:
      menu_select_number:
        - type: from_text

  1. Define the slot such that it influences conversation,

slots:
  menu_select_number:
    type: text
    influence_conversation: true
    auto_fill: false

  1. Write a corresponding action class for the slot in actions.py

class MenuService(Action):

   def name(self) -> Text:
       return "action_ask_menu_select_number"

   def run(self, dispatcher: CollectingDispatcher,
           tracker: Tracker,
           domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

       sender_number = tracker.current_state()['sender_id']
       menu_language_select = tracker.get_slot("menu_language_active")

       button_resp = [
           {
               "title": "Download Cowin certificate",
               "payload": "1"
           },
           {
               "title": "Book Cowin slots",
               "payload": "2"
           }
       ]
       dispatcher.utter_message(text="Select one of the following options", buttons=button_resp)

       return []

  1. Validate the user input message for the same slot. This helps in deciding whether to close the form ( by setting another slot that influences conversation in a story) or keep it open so that the user can be asked same question again.

class ValidateMenuForm(FormValidationAction):

   def name(self) -> Text:
       return "validate_cowin_menu_form"

   def validate_menu_select_number(
       self,
       slot_value: Any,
       dispatcher: CollectingDispatcher,
       tracker: Tracker,
       domain: DomainDict,
   ) -> Dict[Text, Any]:
       """Validate Menu value."""


       if slot_value.lower() in self.menu_db():
           if slot_value.lower() == "1" or slot_value.lower() == "1." or slot_value.lower() == "one":
               return {"menu_select_number": slot_value, "menu_select_cowin_self": True}

           elif slot_value.lower() == "2" or slot_value.lower() == "2." or slot_value.lower() == "two":
               return {"menu_select_number": slot_value, "menu_select_cowin_other": True}

           else:
               return {"menu_select_number": slot_value, "menu_select_exit": True}
       else:
           # validation failed, set this slot to None so that the
           # user will be asked for the slot again

  1. Add a story where the input selected for the slot influences the bot's decisions.


  - story: download cowin certificate current number happy path
    steps:
      - intent: greet
      - action: cowin_menu_form
      - active_loop: cowin_menu_form
      - active_loop:
      - slot_was_set:
          - menu_select_number: 1
      - slot_was_set:
          - menu_select_cowin_self: true
      - action: action_otp_has_been_generated

The flow diagram for our use case looks something like this

Rasa generated story visualizations

In the next part, I'll write about how we integrated Rasa chatbot with whatsapp as input and output channel and enabled services like a) booking CoWin appointment slots b) downloading CoWin vaccine certificates c) Downloading appointment slips in over 9 native Indian languages.