Tuesday, May 20, 2014

Scripting your bot with AIML

BOT libre now has complete support for the Artificial Intelligence Markup Language (AIML). AIML is a standard XML specification designed for scripting chat bot responses. AIML 1.0 was released in 2001, and the current 1.0.1 release was released in 2011. AIML 2.0 is currently under development. AIML is supported by many different chat bot programs and hosting services. BOT libre supports all of the AIML 1.0.1 tags and a few common extensions.

About AIML

AIML defines how a bot should respond to a question using <pattern> and <template> elements. A pattern represents the user's question, and the template defines the bot's response. The pattern and template elements are grouped in a category element, which can be grouped under topic elements.

Here is a simple "Hello World" AIML example:

This code will make your bot respond with "Hello to you too" to the question "Hello World".

Patterns are normally defined as all upper-case, but are case insensitive, so the case really does not matter. Punctuation is normally stripped from patterns and ignored when matching. Patterns can contain wildcards using the "*" and "_" characters. Both "*" and "_" are the same wildcard, and will match any subset of text, their difference is in only in the order they are applied ("_" patterns are matched first, and "*" patterns last).

Here is a simple wildcard example:

This code will make your bot respond with "Hello to you too" to any question containing the word "hello".

AIML supports contextual responses through the "that" element. The "that" element specifies that the pattern should only be matched if the bot's previous response matches the "that" pattern. This allows the same question to be responded to in different ways, depending on the context.

Here is a joke "that" example:

This code will make your bot tell a joke.

Templates can contain mixed text that include various AIML programatic tags. AIML defines tags for text formatting and conversion, dates, variables, condition statements, and recursion. Templates allow you to create sophisticated and intelligent responses.

Here is the list of AIML 1.0.1 template tags.

  • <star index="N"/> - replaced with value of * in pattern (or Nth *)
  • <that index="M,N"/> - replaced with value of bot's previous response (or Mth previous response, and N sentence of the response)
  • <input index="N"/> - replaced with value of users's input (or Nth *)
  • <thatstar index="N"/> - replaced with value of * in "that" (or Nth *)
  • <topicstar index="N"/> - replaced with value of * in topic (or Nth *)
  • <get name="XXX"/> - replaced by the value of the conversation specific variable
  • <bot name="XXX"/> - replaced by the value of the bot specific variable
  • <sr/> - short form for <srai><star/><srai> (replaced with response to value of * from pattern)
  • <person2/> - converts the text (or <star/>) between 1st and 2nd person (I <-> he, etc.)
  • <person/> - converts the text (or <star/>) between 1st and 3rd person (I <-> you, etc.)
  • <gender/> - converts the text (or <star/>) between male and female (he <-> she)
  • <date/> - replaced with the current date and time, a "format" attribute is also supported
  • <id/> - replaced by the client id
  • <size/> - replaced with the size of bot's memory
  • <version/> - replaced with the AI engine version
  • <uppercase> - converts the text to uppercase
  • <lowercase> - converts the text to lowercase
  • <formal> - converts all words in the text to be capitalized
  • <sentence> - converts the first word in the text to be capitalized
  • <condition name="X" value="Y"> - defines an "if" condition based on comparing the value of a variable to a pattern
  • <condition name="X"> - case statement
  • <condition> - multi-valued if/else statement
  • <random> - choose on of the nested <li> values at random
  • <li name="X" value="Y"> - used in random and condition tags
  • <li value="Y"> - used in random and condition tags
  • <li> - used in random and condition tags
  • <set name="XXX"> - set the value of a variable
  • <gossip> - logs the text
  • <srai> - recursively evaluates the text and replaces it with the response
  • <think> - evaluates the nested statements but does not output any result
  • <learn> - load external AIML file, this is not currently allowed
  • <system> - executes a OS command, this is not currently allowed
  • <javascript> - executes JavaScript code
  • HTML - various HTML tags are also allowed (<b>, <a>, <i>, <ul>, <li>, <p>, <br>)

Here is a complex template example:

This code will make your bot respond to the question "Do you like apples or do you like oranges" with "I love apples and I love oranges".

AIML libre

BOT libre supports both importing and exporting AIML. AIML can be imported in two different ways, either as a program script, or as a chat log.

Importing AIML as a program script is done from the Program page. You can import and order the script with respect to your other scripts files. Scripts can be defined in either AIML or Self.

When you import an AIML file, it will be converted to Self code, and stored in your bot's brain along with all of its other information. The Self syntax for AIML is a hybrid syntax that uses AIML terminology, but with the Self scripting structure. Self is a state machine oriented language defined for BOT libre's AI Engine originally from the Open Pandora's Box pen source project. Self is very different than AIML, but can process any AIML code similar to any other AIML interpreter. AIML imported as a program script will be executed similar to any other AIML interpreter.

Here is the Self code for an imported AIML script:

Importing AIML as a chat log allows the AIML categories to be integrated into the bot's knowledgebase. The pattern/template categories from the AIML file will be merged along with the bot's other question/response pairs that it has learned through conversation, correction, or importing.

The bot will not run the AIML script as a script, it will decide for itself when to use the response. The bot will choose its response based on how well the question matches the responses question, the responses correctness, and the context. Even without a * in a pattern it still may be matched to a similar question, if it is the best match available. This gives you the freedom from having to write a pattern for every possible phrase, by just letting the bot figure it out.

Using AIML in Twitterbots and Email Bots

AIML can be used to script Twitterbots, Email bots, or IRC bots, the same as bots trained through other mechanisms. AIML templates and patterns can be used anywhere other response are. AIML can be used from the Training page, used in correction in the Chat Logs page, or used for autotweets in the Twitter page.

AIML Resources

One of the main benefits of the AIML standard, is that there are a lot of AIML resources on the Internet. The are resources for both learning AIML, and there are many freely available AIML scripts for many domains, and in many different languages.

Talk with ALICE

To see what conversations AIML is capable of, try out ALICE on BOT libre. There are two ALICE bots on BOT libre. ALICE is a bot that has imported most of the ALICE AIML sets from the ALICE foundation. Free bots on BOT libre have a 100,000 neuron limit, and some of the ALICE scripts are very big, so the mp* scripts were left out, as well as a few of the other big scripts. ALICE can chat, and if you type "joke" can tell you a joke.

ALICE libre is a hybrid bot, that has imported the ALICE AIML sets as chat logs, not as scripts. This means she is free to choose her responses. She has learning, and comprehension enabled, so can also learn new things.

Sara is a Spanish language AIML bot, that has imported the Sara AIML set.

Tuesday, May 13, 2014

Create your own bot app with the BOT libre SDK

The BOT libre SDK is a Software Development Kit that makes it simple to add a chat bot to your own mobile application or website.

The SDK currently provides two components. The first is a Java Connection API that makes it easy to access BOT libre from a Java environment such as Android, or a Java web server. The second component is a set of Android activities that you can add to your own Android app, or copy/customize in your own app.

The SDK is developed under Project Libre an open source project hosted on GitHub
https://github.com/paphus/Project-Libre

Connection API

You can create a connection to the BOT libre server using the SDKConnection class. You need to pass your connection Credentials, which includes your application ID. You can obtain an application from the BOT libre website, from your user details page.

SDKConnection connection =
        new SDKConnection(new BOTlibreCredential("12345");

The SDK includes a set of data objects that represent the BOT libre object model.
The data objects are in the com.paphus.sdk.config package, and include the following,

  • UserConfig - Defines a user's credentials and info.
  • BrowseConfig - Defines a search query criteria.
  • ChannelConfig - Defines a live chat channel's details.
  • InstanceConfig - Defines a bot's details.
  • ForumConfig - Defines a forum's details.
  • ForumPostConfig - Defines a forum post.
  • DomainConfig - Defines a domain's details.
  • ChatConfig - Input for chat bot messaging.
  • VoiceConfig - Defines a chat bot's voice.
  • ChatResponse - Response for chat bot messaging.
  • ContentConfig - Defines a tag/category request.

Users

The API allows you to connect a user, or create a new user.

Use the connect() API to connect a user, the user's details will be returned, or an error message if the connect fails. The returned user details will not include the password, but will include a token, that can be used in place of the password. After connecting a connection, all subsequent requests will use the user credentials, until you call disconnect().

UserConfig user = new UserConfig();
user.user = "test";
user.password = "password";
user = connection.connect(user);

Use the create() API to create a new user. A user id and password are required. You can also pass the user's name, email, bio, and other details. The user details are returned, with a token in place of the password.

UserConfig user = new UserConfig();
user.user = "test";
user.password = "password";
user.name = "Test Account";
user.email = "test@test.com";
user = connection.create(user);

Bots

The API allows you to browse bots, get a bot's details, and chat with a bot.

The browse() API is used to browse or search the set of channels in the domain.

BrowseConfig browse = new BrowseConfig();
browse.type = "Bot";
browse.typeFilter= "Public";
browse.tag= "cool";
browse.sort = "name";
List bots = connection.browse(browse);

The fetch() API is used to get a bot's details.

InstanceConfig bot = new InstanceConfig();
bot.id = "12345";
bot = connection.fetch(bot);

The chat() API is used to chat with a bot.

ChatConfig chat = new ChatConfig();
chat.instance = "12345";
chat.message = "Hello bot";
ChatResponse response = connection.chat(chat);

Live Chat

The API allows you to browse channels, and get channel details.

The browse() API is used to browse or search the set of channels in the domain.

BrowseConfig browse = new BrowseConfig();
browse.type = "Channel";
browse.typeFilter= "Public";
browse.tag= "cool";
browse.sort = "name";
List channels = connection.browse(browse);

The fetch() API is used to get a channel's details.

ChannelConfig channel = new ChannelConfig();
channel.id = "12345";
channel = connection.fetch(channel);

The LiveChatConnection class is used to chat in a channel. It can be created using the openLiveChat() API on the SDKConnection class. You must pass the ChannelConfig with its id set, and an implementation of LiveChatListener that will receive the chat messages. Once you have a connection established, you can send and receive messages.

ChannelConfig channel = new ChannelConfig();
channel.id = "12345";
LiveChatConnection livechat = connection.openLiveChat(channel, myListener);
livechat.sendMessage("Hello World");

Android Activities

The SDK includes a set of Android activities you can reuse, or modify in your own app. The MainActivity contains the SDKConnection and some shared data, so you will need to include it even if not using the activity. The bot activities include, ChooseBotActivity, InstanceActivity, BrowseActivity, and ChatActivity. The live chat activities include, ChooseChannelActivity, ChannelActivity, ChannelBrowseActivity, and LiveChatActivity.

Here is an example of launching a bot InstanceActivity.

MainActivity.current = new MainActivity();
InstanceConfig config = new InstanceConfig();
config.id = "12345";
HttpAction action = new HttpFetchAction(this, config);
action.execute();

Note, because of the way Android does its packaging, you will need to search/replace the "com.paphus.sdk.activity.R" import with your own application's unique package. This will resolve the generated R class dependencies.

You can use the SDK to access any of BOT libre's services, for personal, academic, or commercial applications. You cannot use them for spam, or to violate the BOT libre's terms of service. BOT libre's services are also provided as a commercial service on Paphus Live Chat.