The most direct procedure is to add a fully-compliant AIML document in a cell, preceded by the %aiml
magic. This magic must be present alone in the first cell line, save for comment lines, i.e. lines starting with #
(which are ignored).
The XML preamble is optional; if missing it will be added automatically. Encoding is always UTF-8
In [1]:
%aiml
# Receiving a greeting and answering back
<?xml version="1.0" encoding="utf-8"?>
<aiml version="1.0">
# The "good NNN" greeting allows us to infer the time of the day
<category>
<pattern>GOOD *</pattern>
<template>
<think><set name="tod"><star/></set></think>
<srai>HI</srai>
</template>
</category>
# A Hello greeting is less defining
<category>
<pattern>HELLO</pattern>
<template><srai>HI</srai></template>
</category>
# This is the template creating the greeting response
<category>
<pattern>HI</pattern>
<template>
<random>
<li>Hi there</li>
<li>Hello</li>
<li>
<condition name="tod">
<li value="morning">Good morning</li>
<li value="afternoon">Good afternoon</li>
<li value="evening">Good evening</li>
<li value="night">Good night</li>
<li>Hello</li>
</condition>
</li>
</random>
</template>
</category>
</aiml>
There can also be comment lines within the AIML document; they are ignored (of course, given that AIML is XML, we can also add comments as XML comments)
In [2]:
Good morning
In [3]:
Hello
We can inspect the session predicates, to check the "tod" (time-of-day) predicate that was (supposedly) assigned
In [4]:
%show session
Given that XML is rather verbose, a slightly simplified syntax is also possible by removing the outer wrapper tags: <aiml>
, <category>
, <pattern>
, <template>
.
This mode is automatically triggered if the first non-comment & non-empty line of an %aiml
cell does not begin with an XML tag (i.e. an angle bracket element). In this mode:
<that>
, in which case this line is considered as a pattern-side THAT and the template is formed by all lines starting from the third. There is no need to close the <that>
element in the second lineAs an example, the following cell contains exactly the same AIML code as the one above in the "full AIML" format. In fact, if we execute it the answer will be loaded 0 new patterns, since those patterns were already defined above and hence they are already in the bot (if we execute a %forget
cell before, then they will be loaded new).
As an additional bonus, automatic normalization (uppercasing and punctuation removal) is done on patterns and on text fragments of <srai>
elements, so we can use "normal" text there.
If you want to include the defined categories within a topic, use the topic name as an additional parameter to the header magic, i.e.
%aiml <topicname>
In [5]:
%aiml
# The "good NNN" greeting allows us to infer the time of the day
good *
<think><set name="tod"><star/></set></think>
<srai>HI</srai>
# A Hello greeting is less defining
hello
<srai>hi</srai>
# This is the template creating the greeting response
hi
<random>
<li>Hi there</li>
<li>Hello</li>
<li>
<condition name="tod">
<li value="morning">Good morning</li>
<li value="afternoon">Good afternoon</li>
<li value="evening">Good evening</li>
<li value="night">Good night</li>
<li>Hello</li>
</condition>
</li>
</random>
In [6]:
good morning
In [7]:
%aiml
# Note we use the full form, since substitution patterns will have "what's" expanded
# We refer this to the HI category defined above
what is up
<srai>hi</srai>
In [8]:
what's up?
We can modify an existing category. It will say loaded 0 new patterns because the pattern already existed, but the result will be modified
In [9]:
%aiml
hello
How do you do?
In [10]:
hello
However we cannot delete an existing pattern (the most we can do is assign an empty output to it). Currently the only way to completely remove it is to execute a %forget
cell and then re-execute all notebook cells defining AIML (%aiml
, %load
, %learm
).
The %save
& %load
commands can be used to save to disk and load from it the full bot state (optionally we can omit saving/loading part of this state)
In [11]:
%save myexample.bot
The saving process by default creates one *.bot
file. This is a ZIP file with two files inside:
.ini
file that contains session predicates, bot predicates and all substitution strings. Plus a pointer to the brain file..brain
file that contains the bot categories, in a compiled formThere is a variant, indicated by the rawfiles
option, in which those two files are left unpacked.
The saved state can be now loaded back by using the %load
command
In [12]:
%forget
In [13]:
%load myexample
%save
and %load
accept additional parameters that make them skip saving/loading part of the data: noses
, nobot
, nosub
, nobrain
. They can be mixed freely
In [14]:
%show session
A final option for reusing the AIML cells in other notebooks (or other systems) is to use the %record
magic.
This command has three forms:
%record on
starts saving. All AIML cells executed afterwards will be kept in a special list%record save <name>
saves all cells stored so far as an AIML file (the .aiml
suffix is added automatically)%record off
stops saving AIML cells and cleans up all cells stored in memoryNote that only the AIML cells since %record on
was executed will be saved. Rules loaded via %learn
or %load
commands will not be included.
In [15]:
%record on
In [16]:
%aiml
# Note we use the full form, since substitution patterns will have "what's" expanded
what is up
Howdy
In [17]:
%aiml
<category>
<pattern>HI</pattern>
<template>Hello</template>
</category>
In [18]:
%record save aimlexample
In [19]:
%record off
AIML files saved can be loaded again with the %learn
magic. They can also be used in any system that understands AIML 1.0
In [20]:
%forget
In [21]:
%learn aimlexample.aiml
In [22]:
Hi
This definition of AIML categories done inline in notebook cells can also be done on top of a bot loaded via %learn
or %load
. See 04-chatbot-alice-augment for an example.