Monday, January 9, 2017

Developing for Google Home: Building a Knowledge Base in API.AI

In my previous post, I described how to get started developing for Google Home. We looked at the walk-through sample Google provides and created the not-terribly-useful Silly Name Maker agent. In this post, I'll describe my first solo project in API.AI, a knowledge base that provides Bible facts.

When you create projects for Google Home in API.AI, you have the option of also furnishing a back end. The API.AI project can interact with your back end using webhooks. But we won't be using a back-end today: for my first solo project, I wanted to see what I could do just in API.AI by itself. Accordingly, my knowledge base project will be based solely on intents (an intent is a mapping between what a user says and what action the agent takes in response). Rather than use a database, the intents will contain both the questions asked and the responses returned.

API.AI also has some other goodies we won't be using in this first simple project: entities let you extract parameter values from input; contexts are like properties that help you tailor intents to the context of a conversation. Although today's project doesn't require entities or contexts, you can be sure they'll come into play when we move on to more complex projects.

Knowledge Base Design


Since this knowledge base is about Bible facts, the place to start is the first chapter of Genesis -- which describes the creation account. The approach I'm using is to distill each piece of information I want to provide--such as what God created on Day 1--and create an intent for it.


My first intent, then, is called creation_day_1 and looks like this:

creation_day_1 intent

Similar intents follow for the rest of the creation week:


creation_day_2 intent

creation_day_3 intent
creation_day_4 intent

creation_day_5 intent

creation_day_6 intent

creation_day_7 intent

Since this is a knowledge base agent, the scope is simply providing answers to questions. Accordingly, there's no attempt to guide the user from one intent to another. However, the answers to some questions may naturally cause the user to ask follow-up questions.

Anticipating Multiple Phrasings


The idea is that a user can ask "What did God create on the first day?" and hear back "On the first day, God created the heavens and the earth." However, there are many ways a user might make this inquiry, and the knowledge base won't be very useful unless it anticipates more than one way of asking the question. In my intent, I've got the following inquiries listed, all of which trigger the same intent and the same information:

What did God create on the first day?
What did God create first?
When were the heavens created?
When was the earth created?
What was created first?
What was created on the first day?
What was created on day 1?

By the way, you can also include multiple variations in an intent's response. When there's more than one possible response, API.AI will randomly select one of the possible responses. Doing this makes your agent seem more conversational and less predictably--in other words, more alive.

After defining intents for the first couple of chapters of Genesis, I'm able to conduct a dialog like the following:

"Hey Google, go to Bible Answers."
"Welcome to Bible Answers. Ask me a question about Genesis."
"What did God create on the first day?"
"On the first day, God created the heavens and the earth, and also light and darkness."
"What did God create second?"
"On the second day God created the sky, and separated the waters above from the waters below."
"When were the animals created?"
"On the sixth day, God created land animals. He also created Man in His image, and declared it good."
"Who was the first child?"
"Adam and Eve's first child was Cain. Cain was a tiller of the ground."
"Who was the second child?"
"Adam and Eve's second child was Abel. Abel was a keeper of sheep."

Well, after all that I've only covered a tiny portion of the Bible--and there are 1,189 chapters in total! I can see I'll be incrementally adding more and more intents to this project for quite some time.

This simple project, while not yet complete, demonstrates how easy is to write agents for Google Home that conversationally answer questions.


No comments: