Ai chatbots are easy to implement with Python’s AIML package. AIML stands for Artificial Intelligence Markup Language, which is nothing more than a simple XML-able form. The sample code in this article will give you a taste of how to create your own ai chatbot with Python.
What is AIML?
AIML was invented by Richard Wallace. He designed a robot called A.L.I.C.E. (Artificial Linguistics Internet Computer Entity), which has won several ai awards. Interestingly, one of the Turing tests looks for artificial intelligence in which a human and a robot communicate for several minutes through a text interface to see if the robot will pass for a human. AIML is an XML format for rule definition for matching patterns and determining responses.
For a detailed Primer on AIML, visit Alice Bot’s AIML Primer. You can also learn more about AIML and what it can do at the AIML Wikipedia page. We’ll start by creating an AIML file and bringing it to life in Python.
Create a standard startup file
It is standard practice to create a startup file std-startup. XML as the main entry point for reading AIML files. Here, an initial file will be created to match a pattern and perform an action. We want to match the pattern load AIML B and make it load our AIML brain in response. We will create the basic_chat.aiml file in real time.
LOAD AIML B
Copy the code
Create an AIML file
Above we have created an AIML file with only one mode handle, load AIML B. When we run the robot from the command line, it will try to read basic_chat.aiml. The load fails unless we have completed the creation. The following sample code will show you what can be added to the basic_chat.aiml file. We will match the two underlying patterns and responses.
HELLO
WHAT ARE YOU
Copy the code
Random response
You can also add random responses as shown in the sample code below. When a message starting with “One time I” is received, the wildcard character “*” can be used for fuzzy matching.
ONE TIME I *
Copy the code
Use existing AIML files
Writing your own AIML files is certainly fun, but it’s also a lot of work. I think it needs at least 10,000 patterns before it can perceive reality. Fortunately, the ALICE Foundation has made some of the AIML files freely available. These files are available on the Alice Bot website. One theory is that stD-65-percent. XML contains 65% of the most commonly used phrases. Another says it lets you play blackjack with a robot.
Using the Python
So far, all the AIML files in XML format are ready. They’re all important as part of a robot’s brain, but right now they’re just information. The robot needs to come to life. You can customize AIML in any language, but some good samaritans have already done so in Python.
First install the AIML package with PIP.
pip install aimlCopy the code
Note that the aiml package can only run in a Python2 environment. You can also choose Py3kAiml on GitHub
The simplest Python program
We can get started with the simplest program. It creates the AIML class, learns the startup file, and then reads the rest of the AIML file. Next, it’s ready to chat, and we’re in a constant loop of prompting the user for input. You need to enter a pattern that the robot can recognize. Pattern recognition depends on the AIML file you load.
Because we set up the startup file as a separate entity, we can later add more AIML files to the robot without debugging any of the program’s source code. Only under STARup in XML format can we add more files.
Kernel = aiml.Kernel() kernel.learn("std-startup. XML ") kernel.respond("load aiml b") Respond (raw_input("Enter your message >> "))Copy the code
Speed up brain loading
As you get a lot of AIML files, it takes a lot of time for the robot to learn. That’s where the brain files come in. After the robot has learned all the AIML files, it can store the brain directly as a file, which can greatly improve the load time when it runs again.
import aiml import os kernel = aiml.Kernel() if os.path.isfile("bot_brain.brn"): kernel.bootstrap(brainFile = "bot_brain.brn") else: kernel.bootstrap(learnFiles = "std-startup.xml", SaveBrain ("bot_brain.brn") # kernel() has been waiting to use while True: print kernel.respond(raw_input("Enter your message >> "))Copy the code
Reload AIML at runtime
At runtime, you can send loading information to the robot, which will then reload the AIML file. Notice if you’re using your brain the way you used it above. You can either delete the brain file so it can be rebuilt the next time you boot up; Or modify the code so that it can store the brain at some point after reloading. The next section uses new Python commands to make robots perform these operations.
load aiml bCopy the code
Add Python commands
If you want to add special commands to your robot by running Python functions, you should intercept the input and process it before sending kernel.respond(). In the example above, we use the raw_input function to get input from the user. So we can get our input anyway. It could be like a TCP socket, or converting a sound source to a written source. You may not want AIML to handle certain information. So they are processed when they are passed to AIML.
while True: message = raw_input("Enter your message to the bot: ") if message == "quit": exit() elif message == "save": SaveBrain ("bot_brain.brn") else: bot_response = kernel.respond(message) # bot_response(Copy the code
Sessions and Predicates
By specifying sessions, AIML can adapt to different interlocutors. For example, if someone tells the robot their name is Alice and another person tells the robot its name is Bob, the robot can tell them apart. Specify the session you want and pass it to respond() as the second argument.
sessionId = 12345
kernel.respond(raw_input(">>>"), sessionId)Copy the code
Having a personal conversation with each customer — it’s great. You have to generate your own session ID and track it. Remember to save the brain file and not all the session values.
SessionData = kernel.getsessiondata (sessionId) # each sessionId requires a unique value # Name predicates with people or things the robot already knows in the conversation # The robot already knows your name is "Billy" and your dog is called "Brandy" kernel.setPredicate("dog", "Brandy", sessionId) clients_dogs_name = kernel.getPredicate("dog", sessionId) kernel.setBotPredicate("hometown", "127.0.0.1) bot_hometown = kernel. GetBotPredicate (" hometown")Copy the code
In AIML, we can do it in
MY DOGS NAME IS *
WHAT IS MY DOGS NAME
Copy the code
With AIML above you can tell the robot:
My dogs name is MaxCopy the code
The robot would reply:
That is interesting that you have a dog named MaxCopy the code
And if you ask the robot:
What is my dogs name?Copy the code
The robot will respond like this:
Your dog's name is Max.Copy the code