ChatBot Simplified
Ashish Lotake
Business Development Executive || Driving Digital Transformation || Expert in AI, Big Data & Cloud || Empowering Businesses through Cutting-Edge IT Solutions
Today we will be building a simple chatbot using the open-source tool RASA.
Let's broadly classify different types of chatbots:
Let's get started
$ python3 -m venv /venv/rasa_venv
$ source /venv/rasa_venv/bin/activate
$ pip3 install rasa
$ rasa init
Let's briefly understand the input of files
Let's briefly understand each file use
nlu:
- intent: greet
examples: |
- hi
- welcome
- good morning
- good evening
- hello
- intent: goodbye
examples: |
- bye
- goodbye
rules:
- rule: Say goodbye anytime the user says goodbye
steps:
- intent: goodbye
- action: utter_goodbye
intents:
?? greet
?? goodbye
entities:
?? name
?? email
responses:
?? utter_greet:
?? - text: “Hello”
?? utter_acknowledge_provided_info:
?? - text: “Thank you {name} for your response and write your querry!”
?? utter_goodbye:
?? - text: “Goodbye”
stories:
? - story: greet_user
? steps:
? - intent: greet
? - action: utter_greet
???? entities:
???? - name
???? - email
? - action: utter_acknowledge_provided_info
Let's see the bot in action
Let's understand what's happening under the hood
user -> hi
hereafter user replay bot, classify this as "greet" intent and then ask for info
领英推荐
bot -> Hello !May I know your name and email.
user -> Dave [email protected]
hereafter the user gave his name and the email bot acknowledges the user and asks the user for his/her query
bot -> Thank Dave you for your response write your querry!
user -> are you a bot
user asks for "bot/human check", then the bot gives an appropriate response
bot -> I am bot, powered by Ashish
user -> about author
user asks about the author, it gives the author's website
bot -> To read more please visit ashishlotake.pages.dev
user -> Bye
bot -> Bye
we have defined a rule when the user says "Bye", the bot replies "Bye"
Now I guess you want to keep your customer chat saved!
We will be using opensource Postgres SQL to save our chat records
tracker_store
type: SQL
dialect: "postgresql" # the dialect used to interact with the db
url: localhost #(optional) host of the sql db, e.g. "localhost"
db: "rasa" # path to your db
username: "" # username used for authentication
password: "" # password used for authentication
query: # optional dictionary to be added as a query string to the connection URL
driver: my-driver:
In the above code just replace your username and password for your database user, and if your database is hosted on another server, then please do change the URL also.
Data PreProcessing
The data that is stored in the database is unfiltered, so we need to perform some data pre-processing to make it more readable.
df = pd.read_csv("./events.csv") ## reading the csv file
df["data"] = df.data.apply(lambda x: json.loads(x)) ## string to dict
df["timestamp"]= df["timestamp"].apply(lambda x : datetime.fromtimestamp(x).ctime()) ## timestamp to datetime
def retrive_txt(c):
'''
extract the message in "text" key from the dict
and for the rest just return NaN
'''
try:
return c["text"]
except:
return "NaN"
df["text"]= df["data"].apply(retrive_txt)
final_df = df[df["text"] != "NaN"]
final_df = final_df.loc[:, final_df.columns != "data"]
final_df
Conclusion
Rasa is a great open-source tool to build chatbots fast and easy. We can also implement advanced machine learning models on our own, instead of using the default one.
There’s a lot more in Rasa, all we have done is scratch the surface, like actions, forms, rules, regex, synonyms, interactive learning, config files, pipelines, and so much more. But the thing covered till far will be more than enough to get started.
refer to below for the source code