Original article was published on Artificial Intelligence on Medium
F̶i̶n̶d̶i̶n̶g̶ Building A Friend
It’s pretty surprising to think that building a chatbot without a degree in CS would’ve been almost impossible just a few years ago — but now, it’s something anybody could build in less than an hour or so.
To build out the (extremely basic) functionality behind COBUD-19, I’ll be using Dialogflow (previously known as Api.ai until Google bought it out). Compared to every other AI chatbot builder, I had the easiest time using it and getting help from its documentation.
First off, you’ll need to head over to dialogflow.com and sign in with your Google account. If you don’t have a Google account, forget that this article ever existed and continue browsing Microsoft Edge.
To start creating your ‘friend’, create a chatbot by clicking on the ‘New Agent’ button. I’ve named my agent ‘Friend’, to both remind me what the chatbot’s for, and also to remind me that I don’t have any:
Make sure to select the language you want your bot to interact in and edit your timezone to the right area. Once you’re done, click ‘Create’.
Remember how I mentioned that chatbots are pattern recognition machines? Well, here’s where they get their patterns from. In this step, we’re essentially creating data for Google’s pre-built NLP to train itself on, so that it can identify similar sentences in the future.
Just like its sounds, intents help our NLP figure out the user’s intent when saying something, and learns to find responses to them. Luckily, you’ll automatically be redirected over to the ‘Intents’ section after creating your first agent.
For now, let’s give Google’s NLP a couple of basic intents to chew on. Maybe some human-sounding responses to the text ‘How’s it going?’, as opposed to the usual answer ‘How may I be of assistance today?’ Start by creating a new intent and filling it out with words you might commonly use:
I designed COBUD to be ominously similar to a friend, so I added casual phrases as inputs and casual responses to go with them. Now’s the time where you can give your bot a little bit of humour and personality — so feel free to play around with yours. Save your changes and Dialogflow’s AI should train itself on the data you just gave it.
Now that your model’s been updated, go ahead and test it out using the console on the right side of your screen. Dialogflow gives you the ability to communicate using both text and speech — try out both and see which one works better:
Now just rinse and repeat that process for any phrase you might want to prepare your friend to answer. You want your virtual friend to talk about life? Program it! Do you want to have a political debate? Why not? You want to discuss the new refrigerators coming out? Go ahead and do that (okay maybe not).
Trust me, there’s a LOT of paths that conversations can take, but you don’t have to cover every single possible question or comment someone might have — just the basics. Remember, we’re trying to make a friend, not IBM Watson.
And technically, you could say we’re done here — you’ve created a chatbot that recognizes what you say and replies how you want it to. There’s just one thing missing — context.
Keep in mind that the processors running our NLP’s code have no idea what any of these words mean. All they’re good at is recognizing patterns in sentences and outputting set results.
If you programmed your bot to respond specifically to the phrase “How’s lockdown going?”, it might give you the same response if you missed a letter or apostrophe — like “Hows lockdown going?”.
But since NLPs don’t actually know a word’s dictionary definition, they wouldn’t understand anything along the lines of “How’s quarantine going?”. If you trained your model using one word or phrase, it’ll have no idea what another phrase with the same message means.
Without adding context or basic understanding of vocabulary, your bot’s going to act…robotic.
To get around that, we’ll have to feed the model with common words and their synonyms, so that it can identify your phrases much better. Go over to the ‘Entities’ tab and add a row. Think of each row as a word, and its contents as its synonyms. After you’re done filling your rows out, you should be left with something like this:
When filling out new entities, the best practice is to almost always enable ‘Fuzzy matching’ and ‘Define synonyms’, since they allow your bot to recognize both the synonyms you’ve entered and some common typos or grammar errors that someone might make.
**Pro-Tip: Now would be a great time to save your updated model. Make sure to save regularly anyway though)**
And finally, your bot’s ready — fully decked out with the ability to understand sentences, context, and a (nonexistent) sense of humour. Thanks, Google!
So, now that you have a machine learning model (and a friend), how do you actually use it? After all, a true friend’s someone that’s there for you as soon as you need them — you shouldn’t have to log in to a site every time you want to get in touch.
The good news is you’ve already done the hard work — it’s just a matter of bringing your bot to life! Wow — it felt like only a couple of hours ago when you started programming its responses, and now it’s already off to the world wide web. They grow up so fast…
Well, life goes on — it’s time to publish your friend to the web. Navigate to the ‘Integrations’ tab and select the web demo option. Of course, you can choose to ship your friend as a Slack, Twitter, or Viber bot (whatever that is) — but you can never go wrong with a good old website:
There’s just one step left to creating your very own locally hosted friend — just tap on the ‘Web Demo’ option you enabled earlier. Then, just select the demo site link, and you’ll be redirected to your very own online pal!