Create your own AI assistant like Alexa, Google Assistant, Siri & Cortana.

Pankaj Singh Rathore
May 6 · 8 min read
Image for post
Image for post

The world of artificially intelligent assistants is growing — Siri, Cortana, Alexa, OK Google, Facebook M, Bixby — all the big players in technology have their own. However, many developers don’t realize that it’s quite easy to build your own AI assistant too! You can customize it to your own needs, your own IoT connected devices, your own custom APIs. The sky’s the limit. Today we are going to build our own AI Assistant using Api.ai.

What is Api.ai?

Api.ai is a service that allows developers to build speech-to-text, natural language processing, artificially intelligent systems that you can train up with your own custom functionality. They have a range of existing knowledge bases that systems built with Api.ai can automatically understand called “Domains” — which is what we’ll be focusing on in this article. Domains provide a whole knowledge base of encyclopedic knowledge, language translation, weather, and more. In future articles, I’ll cover some of the more advanced aspects of Api.ai that allow you to personalize your assistant further.

Getting Started With Api.ai

To get started, we’ll head to the Api.ai website and click either the “Get Started for Free” button or the “Sign Up Free” button.

We’re then taken to a registration form which is pretty straightforward: enter your name, email, and password and click “Sign up”. For those avoiding yet another set of login credentials, you can also sign up using your GitHub or Google account using the buttons to the right.

As Api.ai was bought by Google, it’s migrated towards solely using Google accounts to log in. So if you’re new to Api.ai, you’ll need to sign in with your Google account:

Image for post
Image for post

Click Allow to grant Api.ai(Dialogflow) access to your Google account in the screen that follows:

You’ll also need to read through and agree to their terms of service:

Once you’ve signed up, you’ll be taken straight to the Api.ai(dialogflow.com) interface where you can create your virtual AI assistant. Each assistant you create and teach specific skills to is called an “agent” in Api.ai. So, to begin, you create your first agent by clicking the “Create Agent” button on the top left-hand side:

Image for post
Image for post

You may need to authorize Api.ai again to have additional permissions for your Google account. This is normal and a-okay! Click “Authorize” to continue:

Image for post
Image for post

And allow:

On the next screen, enter in your agent’s details, including:

  • Name: This is just for your own reference to differentiate agents in the api.ai interface. You could call the agent anything you like — either a person’s name (I choose Ghost) or a name that represents the tasks they’re helping out with (e.g. light-controller, Corona update,etc.).
  • Description: A human-readable description so you can remember what the agent’s responsible for. This is optional and might not be needed if your agent’s name is self-explanatory.
  • Language: The language which the agent works in. This cannot be changed once you’ve chosen it — so choose wisely! For this tutorial, choose English, as English has access to the most Api.ai domains. You can see which domains are available for each language in the Languages table in the Api.ai docs.
  • Timezone: As you’d expect, it’s the timezone for your agent. Chances are it’ll already have detected your current timezone.

It will also automatically set up a Google Cloud Platform project for your agent, so you don’t need to do anything in this regard; it’s all automated! It’s good to know this is happening, though, so if you go through a lot of testing and creating many agents, just know there are a lot of Google Cloud Platform projects being created which you might want to clean up someday.

When you have input your agent’s settings, choose “Create” next to the agent’s name to save everything:

Image for post
Image for post

The Test Console

Once your agent has been created, you can test it out with the test console on the right. You can enter in queries at the top and it will send these to your agent, showing you what would be returned after it hears those statements. Enter a question such as “How are you?” and see what it returns. Your results should appear below it:

Image for post
Image for post

If you scroll down on the right-hand side of results, you’ll see more details for how Api.ai interpreted your request (as seen in the screenshot above). Below that, there’s a button called “Diagnostic info”. Click that to see how the API will return this sort of response to you in your app.

Api.ai will open up the JSON viewer and show you a JSON response that looks similar to this one:

{
"responseId": "3fee8695-9411-42a6-a63f-8bd75a7cb960-c1292b34",
"queryResult": {
"queryText": "How are you ?",
"action": "input.unknown",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentText": "Sorry, could you say that again?",
"fulfillmentMessages": [
{
"text": {
"text": [
"Sorry, could you say that again?"
]
}
}
],
"outputContexts": [
{
"name": "projects/ghost-iakkek/agent/sessions/22639ce1-0b3e-d1a8-9036-282dd478c82c/contexts/__system_counters__",
"lifespanCount": 1,
"parameters": {
"no-match": 2,
"no-input": 0
}
}
],
"intent": {
"name": "projects/ghost-iakkek/agent/intents/92535f11-5ad9-4df3-bdda-32bf7b68f330",
"displayName": "Default Fallback Intent",
"isFallback": true
},
"intentDetectionConfidence": 1,
"languageCode": "en"
}
}

As you’ll see … your agent doesn’t know how to respond! Right now, it’s not exactly “intelligent” artificial intelligence: it still needs the intelligence bit to be added in. The input.unknown value in the action field tells you that it’s not sure how to proceed. Above, it’s returning a message of “Sorry, can you say that again?”, which is one of its default fallbacks. Rather than telling the human that it doesn’t understand, it just asks them to say it again … over and over. This isn’t ideal, and I’d prefer to change it to something that makes it clear when the bot doesn’t understand. If you’re picky about this sort of thing too and want to change what it says here, you can find that on the “Intents” page by clicking the “Default Fallback Intent” item there.

A note for those who’ve used Api.ai a little while ago (or had seen it in action): you may have actually been expecting it to have a bit more available out of the box. Previously, it was able to answer queries like “Who is Steve Jobs?” by default. This is no longer the case! You’ve got to add your own integrations with third-party APIs to take action and source information. Api.ai provides the sentence parsing and interpreting side of things.

Adding Small Talk

There’s one bit of default functionality you can add which gives your bot a little hint of intelligence — the “Small Talk” feature. This provides a range of answers to commonly asked questions … including “How are you?” from above. This isn’t on by default though. To turn it on, go to the “Small Talk” menu item on the left and click “Enable”:

Image for post
Image for post

After enabling, if you scroll down you can see a range of categories of common small talk phrases. Find the “Hello/Goodbye” section and click on it to expand it out. Add some different responses to the question “How are you?” and then click “Save” at the top right. Upon adding phrases, you’ll see the percentage figure next to the “Hello/Goodbye” section increase to show you how much you’ve customized your chatbot.

Image for post
Image for post

If you then go to the test console and ask it “How are you?” again, it should now answer with one of the responses that you entered in!

Image for post
Image for post

If it doesn’t respond correctly, check that you did actually click “Save” before giving that a go! It doesn’t save automatically.

Ideally, you’ll want to customize as much of your small talk responses as you can: this is what will give your Api.ai bot a more unique personality. You can choose the tone and structure of its responses. Is it a grumpy chatbot that hates humans talking to it? Is it a chatbot obsessed with cats? Or maybe a chatbot that responds in pre-teen internet/text talk? You get to decide!

Now that you at least have some small talk elements going on, your agent is now ready for you to integrate them into your own web app interface. To do so, you’ll need to get your API keys to give you remote access to your agent.

Finding your Api.ai(Dialogflow) API keys

The API keys you’ll need are on the agent’s settings page. To find that, click on the cog icon next to your agent’s name. On the page that appears, copy and paste the “Client access token” somewhere safe. That’s what we’ll need to make queries to Api.ai’s service:

Image for post
Image for post

In the next part, we will integrate our custom AI Assistant, with our website or app.

Conclusion

As I’m sure you can see, Api.ai is a really simple way to get a chatbot-style AI personal assistant up and running.

If you build your own personal assistant using Api.ai, I’d love to hear about it! Did you name your Ghost too? What questions have you set up for it? Let me know in the comments below, or get in touch with me on Instagram at @maskman.95

Integraate

A Software Development Company

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store