Saturday, June 13, 2009

Technical Explanation of Simple Conversational Interface in Python

In an earlier blog post, I provided a five line Python program that demonstrated the Amy Iris Conversational Interface. The mission of the Amy Iris System is to provide a single working Conversational Interface that developers can easily and freely implement into their websites and applications, that advances the usability of applications, while advancing the state of the art in chat-bot software for customer service and software usability.

I truly feel that Python programmers can do for the field of artificial intelligence what Wikipedia did for general knowledge and encyclopedias. By building a collection of "conversation parsing and processing" into one open source, free resource, an extremely smart Conversational Interface can be collectively built with the wisdom of crowds that makes today's chat-bots laughable.

In this blog post, I walk through the five lines of code, focusing on the Python Language, aimed at users who might not be familiar with Python. In later blog posts, I'll show you how you can put this to work for you, by extending the bot and by embedding her into your applications.

Here were the five lines of code (admittedly using a simple API file that is described later, an in more detail in an upcoming blog post).

import amyirisapi
a=amyirisapi.AmyIris()
for i in range(3):
....kwargs={"textin":raw_input("You: ")}
....print "Amy: "+a.textin.submit(**kwargs)



This simple program prompts You for some input, and submits your input to the Amy Iris system, displaying her response.

Here's a sample execution:

You: Hi, how are you?
Amy: Hello there. I'm doing fine thanks how are you?
You: say I have an emergency in spanish
Amy: tengo una emergencia is i have an emergency in spanish.
You: Where is the nearest Best Buy to 45249?
Amy: The nearest Best Buy store to 45249 is the Fields Ertel OH store, which is 1.47 miles away, at 9871 Waterstone Boulevard.




What's going on technically?

First, let's examine the five lines of Python Code. The first line imports the Amy Iris API. You'll need to download this software to give this a try. The second line creates an instance of the api object, cleverly named "a". The api class is simply an interface to the RESTful Amy Iris System located at Amy Iris.com.

The third statement is simply a "for" loop that will execute 3 times.

The fourth statement sets up a dictionary called kwargs. "kwargs" is a Python idiom meaning "keyword arguments". The concept here is that you can call a function or method specifying a list of arguments, by name, and store them in a dictionary. "kwargs" is a commonly used name for this dictionary, but you could use any valid dictionary name (such as dict1, x, fred, r2d2, etc.).

In Python, a dictionary is an object like a hash table. It stores key-value pairs. And in this case, the key will be the name of an argument (textin, in this case), and the value will be the value of the argument.

So we're prompting the user for raw input, with the prompt of "You: ". And the user can type something in. We will store whatever they key in, into the dictionary with the lookup-key of "textin". The first time through the loop, in my above example, the dictionary "kwargs" holds the value of {"textin":"Hi, how are you?"}. This is how dictionaries in Python typically are represented - in curly braces, with key-value pairs separated by a colon. This dictionary has only one key-value pair.

The final statement calls the api with the arguments stored in kwargs, and prints the results. The "**kwargs" format means that Python should take the dictionary and treat it as a list of parameters, by name/value pair. In other words, the following two lines would be equivalent, the first time through the loop:

a.textin.submit(**kwargs)
a.textin.submit(textin="Hi, how are you?")


So call the api, submitting the user's text to Amy Iris, and print the results.

The api uses the very clever, simplified technique used by Mike Verdone's Python Twitter Toolbox. (As an aside, I was totally impresed by Mike's Python Twitter toolbox - what a great way to access Twitter with Python! Beautiful code! Simple code! I've begun using it instead of the old Python-Twitter API. So, Thanks, Mike, for publishing it as open source!) Similar to Mike's code for interfacing with Twitter (or perhaps as a direct rip off of Mike's code...), The Amy Iris API simply takes the methods used, and passes them along to the Amy Iris RESTful interface.

In other words, the api is called using this line,
a.textin.submit(**kwargs)
The api takes the key-value pairs in the dictionary "kwargs" and submits them via a POST to the url
http://<some server at amyiris.com>/textin/submit.json
[server is defined in the API file]
(note how the api simply rearranges the methods .textin.submit, into the URL of the same name.)

This URL accepts input, runs it through the Amy Iris system, and provides a response. And in this case, it's printed to the user.

This blog post focused on walking through the Python code, so that you can build and implement your own Conversational Interface using the Amy Iris system. Note, if you are not using Python, all is not lost. You can accomplish the same thing in other languages, simply by calling the Amy Iris RESTful interface. Use the url above (that is,
http://<some server at amyiris.com>/textin/submit.json
[server is defined in the API file]) and create software that POSTs to this url, and uses the result that is returned to you (which will be Amy Iris's response to the input supplied in the POST variable "textin").

I'd be interested int seeing how you'd achieve the same access to RESTful interfaces in other languages. I'm specifically interested in seeing implementations in ASP, ASP .net, PHP, Ruby, and as a Facebook app! So if you're up to the challenge, post it in the comments section!

If you made it this far, you may also be interested in reading my earlier blog post about what's going on behind the scenes, and how Amy Iris comes up with her answers. And in upcoming blog posts, I'll describe how you can add Python Code to Amy Iris' logic processing. You'll see how she looks up the language translation and the Best Buy information. You'll learn how you can program her to behave how you want her to for your application.

If you like this blog post, please ReTweet it on Twitter. I'm monitoring the number of clicks on the page, and releasing additional details once people have had a chance to digest the information that I have already published. Thanks!

0 comments: