Check out the project page for more information, screenshots and source code or jump straight on to the DevDungeon Discord to chat with Chatty Cathy. Also available on GitHub at Intro Artificial intelligence chat bots are easy to write in Python with the AIML package. AIML stands for Artificial Intelligence Markup Language, but it is just simple XML. These code examples will walk you through how to create your own artificial intelligence chat bot using Python. AIML was developed by Richard Wallace. He made a bot called A. Artificial Linguistics Internet Computer Entity which won several artificial intelligence awards. Interestingly, one of the Turing tests to look for artificial intelligence is to have a human chat with a bot through a text interface for several minutes and see if they thought it was a human. AIML is a form of XML that defines rules for matching patterns and determining responses. For a full AIML primer, check out. You can also learn more about AIML and what it is capable of on the. We will create the AIML files first and then use Python to give it some life. Create Standard Startup File It is standard to create a startup file called std-startup. In this case we will create a basic file that matches one pattern and takes one action. We want to match the pattern load aiml b, and have it load our aiml brain in response. It won't work unless we actually create it. We will match two basic patterns and respond. WHAT ARE YOU I'm a bot, silly! Random Responses You can also add random responses like this. How old are you? I did not know that. Are you telling the truth? I don't know what that means. Try to tell me that another way. Are you talking about an animal, vegetable or mineral? Use Existing AIML Files It can be fun to write your own AIML files, but it can be a lot of work. I think it needs around 10,000 patterns before it starts to feel realistic. Fortunately, the ALICE foundation provides a number of AIML files for free. Browse the AIML files on the. There was one floating around before called std-65-percent. There is also one that lets you play BlackJack with the bot. Install Python AIML Module So far, everything has been AIML XML files. All of that is important and will make up the brain of the bot, but it's just information right now. The bot needs to come to life. You could use any language to implement the AIML specification, but some nice person has already done that in Python. Python 2 Install the aiml package first with pip or download from. You still import the package as aiml but when installing it with pip you use the name python-aiml. The source code is available at. It creates the aiml object, learns the startup file, and then loads the rest of the aiml files. After that, it is ready to chat, and we enter an infinite loop that will continue to prompt the user for a message. You will need to enter a pattern the bot recognizes. The patterns recognized depend on what AIML files you loaded. We create the startup file as a separate entity so that we can add more aiml files to the bot later without having to modify any of the programs source code. We can just add more files to learn in the startup xml file. This is where brain files come in. After the bot learns all the AIML files it can save its brain directly to a file which will drastically speed up load times on subsequent runs. Keep in mind that if you are using the brain method as it is written above, reloading it on the fly will not save the new changes to the brain. You will either need to delete the brain file so it rebuilds on the next startup, or you will need to modify the code so that it saves the brain at some point after reloading. See the next section on creating Python commands for the bot to do that. We could get our input from anywhere though. Perhaps a TCP socket, or a voice-to-text source. Process the message before it goes through AIML. You may want to skip the AIML processing on certain messages. For example, if one person tells the bot their name is Alice, and the other person tells the bot their name is Bob, the bot can differentiate the people. To specify which session you are using you pass it as a second parameter to respond. You will have to generate your own session Id some how and track them. Note that saving the brain file does not save all the session values. With the AIML above you could tell the bot: My dogs name is Max And the bot will respond with That is interesting that you have a dog named Max And if you ask the bot: What is my dogs name? The bot will respond: Your dog's name is Max.