Thursday, November 1, 2012

Build a chat bot with ALMOST no "code"

In this post I will introduce you to PyAIML which is an AIML interpreter coded in pure Python. What I mean about almost no code here is that, starting a bot with PyAIML requires very little code in a python script file. Though, you might write a lot for the dialogues !

What is AIML ?

AIML is an XML dialect used to interact with a bot. The most popular example of a bot who understands that dialect is A.L.I.C.E. (Artificial Linguistic Internet Conputer Entity) developped by Dr. Richard Wallace.

You will need:

  • Python 2.7
  • PyAIML (Install instruction will follow);
  • Good dialogue writing skills :).
Installation

For Windows users, just download the latest version of PyAIML from the official website. Save it on your hard drive and install it.

For Linux users, I only know Arch Linux and I know you can find it in the AUR.


The code

Building a chatbot with PyAIML requires minimally two files:
  • A .py file to start the interpreter and make it learn replies;
  • A .aiml file that describes what the bot can reply to what.
Here is a simple example of a bot who recognizes simple greetings and bye's.

.py file:

.aiml file:
In the python code, I simply instantiate a new Kernel object form the aiml library that represents my bot. I then set up a few predicates or what the bot knows which in this case is its own name (Pedro) and the name of its master (Me !). Then I tell the bot to learn the content of the .aiml file I give him (you can tell your bot to learn more than one file) and I infinitely loop responding to any user input.

As for the AIML file, I will not go through every lines or tags but I will tell you about the very basic of the idea. If you never read/wrote XML files in your life, it is not hard and I recommend W3School's Tutorial

  • An AIML file must begin with the aiml root element. The version property is optional;
  • A category element is every known exchange of words between you and the bot;
  • pattern element is one of the user's possible input. Punctuation is ignored. Always uppercase;
  • A that element refers to what the bot already said and puts a context to the conversation;
  • A template element is what the bot will reply. You can have random responses from a given list;
  • A srai element means it redirects to another pattern element;
  • <bot name="something" /> refers to one of the predicates the bot knows;
  • You can use wildcards such as *. Note: a * has less priority than a precise word;
  • You can refer to what was written as * with the element <star index="n"/>
The only thing that I did not cover here is the topic element and everything that goes with it. That elements puts a subject on the conversation !

Here is what a conversation looks like with Pedro:


Loading basic.aiml... done (0.03 seconds)
> Hello !
Buon Giorno !
>  What's your name ?
My creator called me Pedro. What about you ?
> My name is Nylo !
Nice to meet you Nylo
> Bye !
Talk to you next time then even if I might not remember