Wit.ai Explained — Part 1 — Bot Engine, Stories, and Actions

Timi Ajiboye
chunks of code*
Published in
5 min readSep 17, 2016

I’ve been experimenting with bots for a couple of years now and I’ve gone through IBM Watson’s Dialog API, api.ai and Wit.ai for NLU As A Service.

Somehow, I keep coming back to Wit.ai.

What Wit Used to Do

For the longest time, what Wit.ai did for you was provide an API that takes your text or voice input and returns intents and entities:

  • Intents: These are pretty self explanatory. An intent is simply what the user intends to do. This could be something like changeTemperature or getNews.
  • Entities: Entities are variables that contain details of the user’s task. With an intent like changeTemperature, you would want to be able to extract the exact temperature the user is trying to change it to from their text/voice input. Wit comes with built-in entity types like location, number, and amount_of_money, plus you can create your own.

Depending on the intent and entities you get from user input, your application can take actions or ask more questions to fulfill the user’s request.

What Wit Does Now

However, sometime in April, Wit.ai released something they call Bot Engine and it changed everything.

Now, there’s this new paradigm called “Stories”. With stories, Wit does a lot more for you. You can now manage the entire conversation flow with Wit’s Dashboard and interact with your code using something called Actions.

Now, I’m going to show you all the new things Stories brings to the table, and explain how they are used. Before we put anything into practice, we’ll take a look at some of Wit’s controls and terms.

Overview of Wit’s New Powers

Upon creating a new application and a new story on Wit. There are four main controls that make the magic happen.

1. User says

This is where you identify what you expect the user will say to give your bot an instruction or a piece of information. It is from here that Wit will extract Entities, which work as I described above.

When you type in expected user input, you will have to highlight bits of the text and map them to Entity names. Initially, Wit may ask you to go to the “Understanding” tab to type varying versions of the input and do the Entity mapping thing a couple of times.

2. Bot sends

This is what your bot should send to your user at certain points, either to let the user know what it’s going to do or to ask for more information.

3. Jump

This lets you jump to any point in the story. You can create a bookmark at any point and jump to it.

4. Bot executes

This is where Actions come in. You can tell the bot to execute a certain action at a particular point. This action however, corresponds to a similarly named function/method in your code. It is at points like this that the developer (you) shall write code to actually do what the user wants. It could, for example, be to do a database search to find gas stations nearby or call an API to get them an Uber.

This function in your code shall be passed two arguments; the context, and the Entities Wit has managed to extract from the last User says input.

After performing whatever operations are necessary, you’ll need to update the context object’s keys with information to be used to continue the conversation.

Here, something interesting happens.

You can create branches in your story. By creating a branch, you’re telling Wit to act differently based on the context keys you returned with your code.

The context object (similar to JavaScript Objects or Ruby Hashes), consists of keys and values.

You can tell Wit what to do when your action function adds the location, crowd and budget keys to context object.

You can also tell it what to do if your action function adds the missingCrowd key.

and what to do if your action function adds the missingBudget key.

You get the picture.

Putting It To Practice

We’ll be looking at one of the official Wit recipes called Slot Based Bot. You can find the complete Wit app here. You can also download my own copy of the Wit app and import it after creating a new app.

(Another reason why Wit is awesome: One can have public apps so that people can come see how you did it on their dashboard. Like GitHub)

Anyway, the compete tutorial is here and you need to go through it before reading the subsequent (soon to come) parts of this post.

What’s Next?

  • In the second part of this series, we shall be looking at implementing the story in a Rails application using the official Wit Ruby library.
  • In the third part, we shall do the exact same thing but with Sails.js and the offical Wit Node library.
  • In the fourth part, we’re doing the exact same thing AGAIN, but this time with the HTTP API. I feel like it makes sense to show how something like this can be used in a native Android or iOS app. Anyway, I’ll choose one mobile operating system depending on how I feel at the time.

--

--

Timi Ajiboye
chunks of code*

I make stuff, mostly things that work on computers. Building Gandalf.