Today we are going to build a Python 3 ChatBot API and web interface. ChatBots are challenging to build because there are an infinite number of inputs. Because of that, a ChatBot that can consistently come up with good answers needs immense knowledge.
It is common for developers to apply machine learning algorithms, NLP, and corpora of predefined answers into their ChatBot system design. We are going to keep our code basic, so we will bypass creating a complex “brain” for our ChatBot.
Instead of building an AI brain, we will use one that is free and already built: Google Search.
Our ChatBot will perform a Google Search of a user’s query, scrape the text from the first result, and reply to the user with the first sentence of that page’s text.
Let’s get started! By the way, all the code mentioned is in the Python ChatBot GitHub repository.
Querying Google In Python for ChatBot Replies
In order to program our simple ChatBot with omniscience (infinite knowledge), we will do Google searches within the Python API. Fortunately there is a Google search Python library that we can install with pip.
After you have installed the Google library locally, you can write Python code like this:
Once we have a list of URLs from the search results, we can do a GET request of that web page using the Python requests library. We can also parse the HTML by using
html from lxml and also BeautifulSoup.
Here is a completed file that our HTTP server can import as a dependency. I made a method that does a Google search, gets the first
<p> on the web page, and returns its contents as a string. If the search fails in any way, the ChatBot will reply with “Sorry, I cannot think of a reply for that.”
Now we can accept user input and do a Google search. We’ll make a HTTP GET request to the first result of the search. Then we parse through the HTML that was returned and isolate the first sentence in the first
<p> on that page. This is our ChatBot’s reply algorithm, no machine learning required.
Python API for a Simple ChatBot
Next we need to build a server app that will be our API for ChatBot queries. It will serve responses to HTTP requests. To start, those requests will come from a simple HTML page which we’ll make later.
To start, we will import the Python 3 HTTP server and socket server libraries along with the Google search file we made earlier.
Our API will be served on port 8080 and we will serve web page assets from a folder called
public in our project’s parent directory. Next we will make our own handler for GET and POST requests.
HTTP GET requests will attempt to return a corresponding file from the
Lastly, we will start up the server and use our handler. Here is the entire file, including the above code snippets.
We can use CURL to test out the ChatBot API with POST requests.
curl -d "how old is samuel l jackson" http://localhost:8080
Next we will make an HTML page that can query this API. By the end, we’ll have an end-to-end ChatBot that provides sophisticated answers.
Building the ChatBot Web Page
Our web page will be very simple. It will contain a picture of a bot, a text input field, and a submit button. Whenever the user submits an input, the chatbot API will be reached through a POST request. The text answer that is returned from the API will be filled-in on the web page.
Here is the HTML page. Save this as
index.html in the
public folder we mentioned earlier. The image file of the bot is also in the full Python ChatBot GitHub repository.
Next we will do some quick styles for this web page. Save this CSS file in the
public folder too. It is already referenced in the HTML file’s
Now you should have a simple ChatBot web page ready for user input. Here is a screenshot:
We’ll write some JS that detects a user pressing the Return key and also clicking the submit button. When either of those events happen, we’ll get the text inside the user input field and include it as a POST body for our Python server.
We’ll make a HTTP POST request to the Python API server using the
fetch method. The Fetch API is now included by default in modern web browsers.
app.js in the public folder.
We’re almost ready to ship our Python ChatBot to production.
Running our Simple Python ChatBot Made From Scratch
Now that we have written all the code, we have one more step before we can run the full-stack Python ChatBot. If you have not already, make a
requirements.txt file in the parent directory of the project, alongside the 2 Python files. This file is a Python paradigm for easily installing a project’s dependencies.
Go to the parent directory of the ChatBot project using your command line. run the Python library install command.
pip install -r requirements.txt
Your machine now has all the required libraries to run the ChatBot! Let’s run the full-stack app.
Next open your web browser and go to http://localhost:8080/. If you see the ChatBot image, it is working.
Try some inputs!
who played iron man
how old is samuel l jackson
what is the weather like on mars
You can see, our ChatBot replies aren’t perfect, but pretty good for a few minutes of work.