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.

Monday, January 2, 2017

The Geek's Guide to Dieting, Part 4: Surviving the Holidays

This is Part 4 in my series on dieting for geeks. This time we're talking about surviving the holidays, the biggest challenge a dieter faces.

The holidays are a wonderful time of year: they bring together celebration, fellowship, and food. Unfortunately, the holidays also seek, relentlessly, to undermine your diet. Everywhere you look, there's an excuse to over-indulge. All if takes is a few days of over-eating and old habits quickly return.

For me, the holiday season starts with an annual chili cook-off with longtime friends in late October or early November. As we near the Thanksgiving season, all sorts of special foods arrive, many of them pumpkin-spice flavored. Holiday pies are on sale. Thanksgiving itself, of course, is the super bowl of eating. A month later we're eating again for Christmas, and just a week after that there's the all-night eat-a-thon of New Year's Eve.

This year, I entered the holiday season four months into my diet having shed 40 pounds. I had gotten into a routine where I was keeping my calories to 1200 a day, and losing 10 pounds a month. I was justifiably nervous as Fall approached: I certainly had every intention of enjoying the Holiday festivities, but I also didn't want to throw away all of my weight loss progress. I decided I would indulge, but not recklessly. If I could make it to end of year and continue to lose weight at half the usual rate, I would be satisfied; then I could go back to my normal routine in January.

Initially, this worked out fine. We attended the chili cook off and of course over-ate. But that was only one day, and since I otherwise stayed on my diet plan everything was okay. By the time Thanksgiving arrived, I was feeling confident. By now I'd now lost 45 pounds since July, so my plan to continue losing weight but a bit more slowly seemed like it was working. In fact, I'd upped my daily calories from 1200 to 1300. Of course Thanksgiving and the next few days involved a lot of eating, but I again reverted back to form without too much difficulty afterward and only gained a couple of pounds from all that Turkey, mashed potatoes, and stuffing.

By the time Christmas arrived, however, I was starting to crack. Food was being offered to me left and right! There were parties and candy and treats seemingly every day. I did my best, but knew I was slipping into some bad habits and eating more than I should far too often. My wife and I sing in our church choir, and Christmas Eve means singing for three services, with a pot-luck in-between each service to keep our energy up. I lost track of how many meatballs I ate.

Finally, New Years Eve. We were hosting a party for 20 people, and that involved having a lot of food available (such as homemade pretzels and pizza and chips and guacamole) and staying up past midnight eating it. I ate, and ate, and ate. In the morning I weighed myself, and found my 45-pound weight loss had reverted to only a 40-pound weight loss: I'd gained 5 pounds over the course of December.

On January 1st, I knew I had to reset things and work to get back to where I had been. I got a lot more strict about staying on my diet plan and stopped making exceptions. Now that the holidays are over, the constant temptations are gone.

Looking back, I survived the holidays but could have done better. I give myself a B instead of an A. I did gain a bit more weight than I'd wanted to (5 pounds), but I didn't completely unravel things. My plan for next year is to have less in the way of left-overs: left-overs can turn a day of over-eating into several.