Using AIML files with Microsoft Bot Framework

A few days ago, I was pitching Bot Framework to an interested party, when a question came up: “Does the Bot Framework support AIML files? We have a lot of them and we are wondering whether we could use them”. Honestly, I didn’t have a clue what AIML is, so I decided to run a quick search.

Turns out that AIML stands for Artificial Intelligence Markup Language and, as Wikipedia nicely mentions it, it is an XML dialect for creating natural language software agents. AIML was used to power Alice Bot back in 1995 (this was one of the first chat bots available to the public). Moreover, AIML powered a discussion with Captain Kirk of Enterprise (ho there, Star Trek fans!). Alice AIML files are open source, you can find them here.

AIML

Let’s take a look at how an AIML file is  written:

<category>
  <pattern>WHAT IS YOUR NAME</pattern>
  <template>My name is Michael N.S Evanious.</template>
</category>

Pattern is the question asked by the user whereas template is the answer to be responded by the chat bot. So, with input “What is your name”, bot will respond with “My name is Michael N.S Evanious”. As you can imagine, pattern string is case-insensitive. You can also use a pattern like “WHAT IS YOUR *”, in this case the bot will much any input in the wildcard position, like “what is your age”,”what is your shoe number” etc. AIML files can also operate as base for a finite state machine via specific XML tags. They also can contain variables, such as ones provided by the bot or by the user herself. To find more about AIML, you should check ALICE AIML Primer and ALICE documentation.

Parsing AIML on Node.js

If you’re using Bot Builder Node.js SDK, there is a really nice and easy to use npm package that you can use to parse AIML files, called aiml.js, located here on GitHub. Just “npm install aimlinterpreter”. Check how easy it is to use via a simple Promise function:

const AIMLInterpreter = require('AIMLInterpreter');
const builder = require('botbuilder');

const aimlInterpreter = new AIMLInterpreter({ name: 'John', age: '25' });
aimlInterpreter.loadAIMLFilesIntoArray(['./test.aiml.xml']);

const aimlPromise = function (question) {
    return new Promise(function (resolve, reject) {
        aimlInterpreter.findAnswerInLoadedAIMLFiles(question, function (answer, wildCardArray, input) {
            return resolve(answer);
//should also handle reject!!! this is demo code only :)
        });
    })
};

const connector = new builder.ConsoleConnector().listen();
const bot = new builder.UniversalBot(connector);
bot.dialog('/', [
    function (session) {
        builder.Prompts.text(session, 'Hi! Ask me something...');
    },
    function (session, results) {
        aimlPromise(results.response).then(res => session.send(`Response is ${res}`));
    }
]);

You can also modify the last function in the array to work with async/await JavaScript keywords:

    async function (session, results) {
        let res = await aimlPromise(results.response);
        console.log(res);
    }

Easy, right?

If you’re using C# Bot Framework SDK, check this project here, should help you roll.

Hope this helps,

Dimitris

10 thoughts on “Using AIML files with Microsoft Bot Framework

  1. how to handle the session. when i connect two clients to bot, bot is using same AIML info ( name , age )? if client 1 says “my age is 20” and client 2 ask “how old i am” bot replies to client 2 by saying “20”.. anyway to fix this.. that means serving multiple clients at one time?

    Liked by 1 person

    • What you could do is initialize the AIMLInterpreter with information given by the user. For instance, you could ask him/her about name and age and create the AIMLInterpreter object accordingly.

      e.g. const aimlInterpreter = new AIMLInterpreter({ name: ‘NameGivenByTheUser’ , age: ‘AgeGivenByTheUser’ });

      Like

Leave a comment