Contextual Chat Bot using NLP
Overview
Contextual chat bots are bots that can chat with humans using everyday language that is appropriate to the topic humans are talking about. We will build a model for a puzzle conversation.
Prerequisites
Implementation
Transform Conversational Intent Definitions to a Tensorflow Model
A chatbot framework needs a structure in which conversational intents are defined. One clean way to do this is with a JSON file, like following bellow.
|
|
Tensorflow Engine
First we take care our imports and global variables.
|
|
Create file tensor_flow.py
for our tensorflow engine. And first fill like below to create constructor.
|
|
Add method inside our class to create document training like following below.
|
|
Create method inside class to use for set our tflearn models.
|
|
Before we can begin processing intents, we need a way to produce a bag-of-words from user input. This is the same technique as we used earlier to create our training documents. Add static method in our class.
|
|
Each sentence passed to response() is classified. Our classifier uses model.predict() and is lighting fast. The probabilities returned by the model are lined-up with our intents definitions to produce a list of potential responses. Create method classify
and response
to get predicted response.
|
|
If one or more classifications are above a threshold, we see if a tag matches an intent and then process that. We’ll treat our classification list as a stack and pop off the stack looking for a suitable match until we find one, or it’s empty. Finally, our tensorflow engine should be like following below.
|
|
Test Our Engine
Now we can test our engine by typing in command line.
|
|
Or create new file example, test.py
and fill like below.
|
|
And run our python file in our terminal python test.py
.
Build REST Application using Flask
Now we will create flask application that can be used for another application using chat bot. Create file main.py
and our project directory should be like following below.
|
|
Fill main.py
like following code.
|
|
Lets try our chat bot using rest api, we will use cURL like this.
|
|
And we will gen an output like this.
|
|
References
Contextual Chatbots with Tensorflow
What Are Contextual Chatbots? How They Can Make A World Of Difference In User Experience?