Building a Rasa chat bot on Google Colab
Rasa is a conversational AI platform for personalized conversations, it is a tool used to build AI chatbots using python and natural language understanding. It provides a smooth way to train personalized chatbots which can be hosted online on several platforms.
Conversational chatbots have become very common today and are widely used by companies to give instant feedback to customers requiring assistance or information. They have reduced the annoying wait time which used to be the norm for inquiries to be answered.
The objective of the chatbot I would be training is to respond to questions travelers new to a city would ask at a train station. The Rasa chatbot consists of two components, Rasa nlu, and rasa core. Rasa nlu is a component of Rasa that is used to classify intent and extract entities (details), while Rasa core is a component that takes the structured input from Rasa nlu and predicts the next best action using a probabilistic model. More information can be gotten from the Rasa official documentation. This project is run on google colab.
Libraries and dependencies
Install rasa_nlu[spacy], rasa_core, nest_asyncio and ipython
!pip install nest_asyncio==1.3.3!pip install rasa_nlu[spacy] #restart runtime after installation and rerun the cell.!pip install rasa_corepip install -U ipython #restart runtime after installation and rerun the cell.
Import rasa_core, rasa_nlu spacy and sys
import rasa_coreimport rasa_nluimport spacyimport syspython = sys.executable
download en_core_web_md, a medium-sized english model trained on written web text.
!python -m spacy download en_core_web_md
Rasa NLU
Next is the declaration of intent and sample data for each intent. After that, I train a model that accurately predicts the intention of a phrase or sentence.
# Writing various intents with examples to nlu.md filenlu_md = """## intent:greet- hey- hello there- hi- hello there- good morning- good evening- how far- hey there- whats up- hey dude- goodmorning- goodevening- good afternoon## intent:goodbye- good by- later- good night- good afternoon- bye- goodbye- have a nice day- see you around- bye bye- see you later## intent:thanks- thanks- thank you- appreciated- gracias## intent:directions- how do i get to- i need to get to- is there a bus to- how i go take reach- I need directions to- how do i get a cab- where can i get a taxi- where can i get a bus-i need to get somewhere-I am going somewhere## intent:choosing_item- 1- 2- 3- 4- 5- 6- 7## intent:accomodation_enquiry- Where is the nearest hotel ?- is there a hotel near by ?- shey hotel they this area ?- Where i fit crash for here ?- i want to get to a hotel- i need to get to a hotel- where can i spend the night ?## intent: restaurant_enquiry- i need to get food- i am hungry- where is the restaurant ?- is there a restaurant ?- where can i get water ?- where i fit buy food for here ?- is there a food court nearby ?- where them they sell food ?- I want chop- where is the nearest shop?- where can i eat ?- i need to eat- are there eatries around ?- where can i buy snacks ?- I want to buy food## intent:toilet_enquiry- i neeed to pee- where is the rest room?- where is the toilet- is there a nearby toilet?- i they find toilet- where is the bathroom- i need to use the bathroom- where can i pee- i want peace- i am pressed"""%store nlu_md >nlu.md
Set up the configuration for our pipeline and policies. The fallback threshold can be adjusted to fit personal use.
# Adding the NLU components to the pipeline in config.yml fileconfig = """language: "en_core_web_md"pipeline:- name: "nlp_spacy" # loads the spacy language model- name: "tokenizer_spacy" # splits the sentence into tokens- name: "ner_crf" # uses the pretrained spacy NER model- name: "intent_featurizer_spacy" # transform the sentence into a vector representation- name: "intent_classifier_sklearn" # uses the vector representation to classify using SVM- name: "ner_synonyms" # trains the synonymspolicies:- name: "RulePolicy"# a confidence >= core_fallback_thresholdcore_fallback_threshold: 0.68 # Confidence threshold for the `core_fallback_action_name` to apply.core_fallback_action_name: "action_default_fallback" # The action will apply if no other action was predicted withenable_fallback_prediction: True # a confidence >= core_fallback_threshold"""%store config >config.yml
Training the model
Import the ‘load_data’ and ‘config, modules, load the training data file and config file, and feed them to the Trainer module to train the model.
# Import modules for trainingfrom rasa_nlu.training_data import load_datafrom rasa_nlu.config import RasaNLUModelConfigfrom rasa_nlu.model import Trainerfrom rasa_nlu import config# loading the nlu training samplestraining_data = load_data("nlu.md")trainer = Trainer(config.load("config.yml"))# training the nluinterpreter = trainer.train(training_data)model_directory = trainer.persist("./models/nlu", fixed_model_name="current")
Test the model.
The aim is to see how well it can predict intents, and an appropriate response is determined with the predictions made. The output should be the intent and its confidence value.
# Testing the NLU model with an input messageimport jsondef pprint(o): print(json.dumps(o, indent=2))pprint(interpreter.parse("x"))
Rasa core
This is the second part of the project where we would be dealing with stories, actions, and templates so our bot can respond to queries appropriately. A story is a path that contains intents and their respective actions to be taken. Actions are the responses we want our bot to give after identifying intentions. Templates are where we store all the actions. Slots are our bot’s memories which help it keep track of events.
# Writing stories and saving it in the stories.md filestories_md = """## Greeting path* greet- utter_greet## directions path* directions- utter_menu* choosing_destination- utter_destination_received## Enquires path* accomodation_enquiry- utter_enquiry## thanks path* thanks- utter_thanks## restaurant path* restaurant_enquiry- utter_restaurant_enquiry## toilet path* toilet_enquiry- utter_toilet_location## say goodbye* goodbye- utter_goodbye## thanks* thanks- utter_thanks## fallback rule* bot_fallback- action_default_fallback"""%store stories_md >stories.md
The domain is where we link together all our files for the chatbot. The uttar_default action is the action to be taken when the confidence value of all possible intentions is below the threshold value set in our configuration file under the fallback policy.
# Writing all the intents,slots,entities,actions and templates to domain.ymldomain_yml = """intents:- greet- goodbye- directions- choosing_item- accomodation_enquiry- restaurant_enquiry- toilet_enquiry- thanksslots:group:type: textentities:- groupactions:- utter_greet- utter_menu- utter_destination_received- utter_enquiry- utter_toilet_location- utter_restaurant_enquiry- utter_goodbye- utter_thanks- utter_defaulttemplates:utter_greet:- text: "\n\n NRC BOT: \n\n Hey! Welcome to NRC Kubwa terminal . How may I help you ? \n\n \n\n You:"utter_menu:- text: "\n\n NRC BOT: \n\n There is a bus terminal outside the station, it's just accross the road. You can board a bus to the following destinations: \n\n 1. Wuse 2. Airport 3. Nyanya 4. Zuba 5. Asokoro 6. Maitama 7. Area one 8. Central area 8. Secretariat. \n\n Enter the appropraite number to know when the next bus leaves the terminal. \n\n \n\n You:"utter_destination_received:- text: "\n\n NRC BOT: \n\n The next bus leaves in 15 minutes, do have a safe trip. \n\n \n\n You:"utter_goodbye:- text: "\n\n NRC BOT: \n\n Thank you for choosing to transit with us. Waiting for your next visit. \n\n \n\n You:"utter_enquiry:- text: "\n\n NRC BOT: \n\n Take a right turn from the exit, there are several hotels at your service. \n\n \n\n You:"utter_toilet_location:- text: "\n\n NRC BOT: \n\n There is a toilet symbol on the extreme right from where you stand. \n\n \n\n You:"utter_restaurant_enquiry:- text: "\n\n NRC BOT: \n\n There is a food court filled with a variety of rastaurants at the extreme left after the entrance. \n\n \n\n You: "utter_thanks:- text: "\n\n NRC BOT: \n\n my pleasure \n\n \n\n You:"utter_default:- text: "\n\n NRC BOT: \n\n I'm sorry, I didn't quite understand that. Could you rephrase? \n\n You:""""%store domain_yml >domain.yml
Here we import our policies and the Agent module. The Agent module is used to load the file and passed to the ‘agent’ model. The ‘agent’ model is trained with the data from the stories file.
# Import the policies and agentfrom rasa_core.policies import FallbackPolicy, MemoizationPolicy,KerasPolicyfrom rasa_core.agent import Agent# Initialize the model with `domain.yml`agent = Agent('domain.yml', policies=[MemoizationPolicy(), KerasPolicy(), ])# loading our training dialogues from `stories.md`training_data = agent.load_data('stories.md')# Training the modelagent.train(training_data)#validation_split=0.0,#epochs=200)agent.persist('models/dialogue')
Install version 2.10.0 of h5py package, restart the runtime after installation and rerun all previous cells.
!pip uninstall h5py!pip install h5py==2.10.0
Load the trained model
#Starting the Botfrom rasa_core.agent import Agentagent = Agent.load('models/dialogue', interpreter=model_directory)
Write a function to tale inputs for the chatbot and gives out an output while stopping when ‘stop’ is typed in. Type stop to end the session.
print("Your bot is ready to talk! Type your messages here or send 'stop'" + "\n\n" + "You:")while True: a = input() if a == 'stop': break responses = agent.handle_message(a) for response in responses: print(response["text"])
Video demo
You can get the complete notebook on my github repo:
References:
Building chatbot with Rasa and spaCy : https://www.machinelearningplus.com/nlp/chatbot-with-rasa-and-spacy/
Rasa official documentation: https://rasa.com/docs/rasa/