Creating a conversational chatbot using Wit.ai

filipeguelber
bawilabs
Published in
6 min readJul 27, 2017

[Clique aqui para a versão em português]

The user interaction passed through some phases: desktop apps, web, mobile e now chatbots. Maybe it is too early to say that chatbots are the new apps, but we can’t hide the advantages of chatbots:

  1. Bots are much easier to install than mobile apps.
  2. Bots are easily distributed.
  3. Quality apps are expensive to build, maintain, and deploy.
  4. There are numerous very specific “long tail” use cases that do not justify the use of a mobile app.
  5. Messaging apps are ubiquitous and dominate the engagement of mobile customers.
  6. People are going through an application fatigue.
  7. Interactions with Bots are intrinsically bi-directional.
  8. Moving complexity to the cloud reduces the user’s cognitive load. Everyone knows, for example, how to find a good Japanese restaurant opened close to you: just go to google, search, check the reviews, find the address and look at google maps. However, it would not be easier to ask a bot: ‘Find the best-rated Japanese restaurant near me’?
  9. Bots are extremely portable for several platforms.
  10. Bots development cycle are extremely fast.
  11. Human beings are very connected and naturally made for language and conversation.
  12. Do not underestimate the power of emoji. 👊

The construction of a chatbot basically involves two major challenges: natural language processing (NLP) and natural language understanding (NLU).

The first concerns to breaking up a sentence into parts called entities and the second one means understanding what the sentence means. It would be as if the NLP were the pre-school and NLU the university.

Doing all this from scratch is certainly a very big job, but there are services like Wit.ai that make things a lot easier.

Wit.ai offers an API that takes a text or voice input and returns intents and entities.

  • Intents: means the user’s intention, what he intended with that sentence. For example, weather forecasting, listing tasks etc.
  • Entities: are variables that contain details of the user’s task. For example, considering the entity weather forecast, you should be able to identify from the input to which place the user wants the forecast. Wit offers several built-in entity types like location, numbers, cash values, age, distance, date and time. Besides these you can also create your own.

Depending on the intents and entities you get as user input, your application can take action or even ask new questions to the user.

Hands on

Wit has the concept of stories. It is from them that everything begins and where Wit’s tricks appear.

A story has basically 4 components:

1. User says

In this component, you write what the user is expected to type into a conversation. You will notice that Wit will automatically attempt to identify the entities.

In this case, Wit already identified “Filipe” as being a contact.

Notice that the Wit’s own Entities have the “wit /” before. Whenever possible use native entities, they work incredibly well!

Notice that in this example, Wit does not identify my entities because they are too specific. In this case, just teach him :)

2. Bot sends

This component represents what your bot should send to the user.

3. Jump

The Jump allows you to go to any part of the story. To do this you need to create a bookmark in any part of the story and then configure a “Jump” to it.

4. Bot executes

These are the actions. Each action corresponds to a function of your code. This function describes the business rule to be executed and is not done directly in the Wit interface, but rather in your code.

node,js example

The complete source code could be found at: https://github.com/filipeguelber/node-wit/blob/master/examples/quickstart.js

We’ll talk later about that part.

This function has 2 arguments: the context and the entities that Wit discovered from user input. After executing the necessary actions on your code, you should update the keys that are desired on context, so that they will be available on the Wit along the chat.

These context variables will be available on Wit, and you can use on a branch of the story.

In the above example, the branch “pics && achei” means that in the case of this keys are present on context (updated by you business rule) Wit will follow this way.

Note that any context-key can be used on the bot actions. In this case, I am using them on the answer.

Training your bot

Although Wit is very clever, in some situations you may want to teach him. To do this, click on the understanding tab.

It’s here the place you teach which sentences the user can input and the meaning of them related to the intents and entities.

You can, for example, teach the bot the synonyms:

And can verify what you have taught by clicking on each entity:

Put it on

Now that your stories are created and your bot is trained, let’s get it working! The fastest way is to clone the repository:

git clone https://github.com/filipeguelber/node-wit

Install node-wit:

npm install --save node-wit

And execute:

# Node.js <= 6.x.x, add the flag --harmony_destructuring
node --harmony_destructuring examples/basic.js <MY_TOKEN>
# Node.js >= v6.x.x
node examples/basic.js <MY_TOKEN>

Remember to replace <MY_TOKEN> by your token. It can be found on configs on your wit project.

Ready! You will see your chat running from your terminal. Just start your conversation with him and you will see the magic happening.

If you enter something that your Bot does not recognize, the entry can be found in the “inbox” tab and there you will teach your Bot.

If someone enters “cat pictures” and you had not thought about this possibility, just go to the Wit, inbox tab and teach him that “cat pictures” means the intent viewPictures the category is cats. Done, in the next interaction your user will notice the correct answer.

So what?

Integrating your Bot into applications is truly easy. It is possible to do with a web interface or app, and even integrate it with Slack or Facebook Messenger (which you can do in 10 minutes).

--

--