Home → The Classics → Farai's Codelab
Dialogflow and A Bit of Node.js
Published: Updated:
For my senior project, I’ve been working on Monty the Lab Assistant for the Google Home. To make it, I used Dialogflow and a bit of Node.js
Dialogflow
“Dialogflow is a tool that abstracts the natural language processing associated with voice and chat applications allowing developers to focus on crafting rich user experiences.”
– Me once I realized that as frustrating as it is, I don’t need to mess around with NLP algorithms.
History of Dialogflow
- It was founded in 2010 by Ilya Gelfenbeyn as Speaktoit.
- In 2011, they released Assistant (by Speaktoit), a voice assistant for Android devices (think Google Assistant and Siri).
- In 2014, they got a bunch of money. They also changed their name to Api.ai and released the tool Api.ai to third-party developers. Using Api.ai, developers were now able to make third-party skills for the Assistant. It happens to be the same tool that Api.ai themselves used in-house.
- In 2016, they were acquired by Google. Google needed Api.ai to help third-party developers create actions for Google Assistant.
- In 2017, Google changed the name to Dialogflow.
How It Works
For my example, I’ll be making an agent that users will use to tell the weather. The demo I’m using has been made by Dialogflow.
Using Dialogflow
First of all, we perform an invocation to prepare the app for the user’s request. Once the agent has been invoked, the user can now make a request which in our case is.
“What is the weather in Waverly tomorrow?”
The agent now needs to figure out the intent behind this request, which in this case is finding the weather (i.e. weather intent). Since the same intent can be expressed in different ways, you need to provide the agent with variations of the intent in order to train it. Below are the various inputs possible under the weather intent when you ask “What’s The Weather Like Tomorrow”.
- forecast in Tokyo
- whats the weather for March 2nd
- weather for April 3
- I’ld like to know the weather
- today’s weather
- weather in London in 5 days
- what’s the weather for Kernsville
- what’s the weather forecast for tommorow morning
- how’s the weather in my area
- what’s the weather forecast for this morning
Note that some words are highlighted. Those highlighted words are the entities that Dialogflow looks for within an intent. Within our request, the entities are Waverly (location) and tomorrow (time which dialogflow infers).
Literal | Parameter | Value |
---|---|---|
Waverly | @sys.location | {"city":"Waverly"} |
tomorrow | @sys.time | 2018-02-14 |
Now that we have out entities, the agent then sends the parsed request to fulfillment, which is a backend that resides at any web server provided it’s capable of handling JSON. At fulfillment, the backend then processes the request which in our case is figuring out the weather through a weather API, like World Weather Online.
POST https://thisisafulfillme.nt/url
...
"entities": {
"location": {"city":"Waverly"},
"time": "2018-02-14"
}...
Once the request has been fulfilled, fulfillment sends a response back to the agent which then passes it back to the user, which in our case is a weather report. Along with a response, the agent receives context which it can then use whenever a user makes another request.
Node.js
For my project, I’m using Node.js since the SDK needed to make Google actions in JavaScript. To run my JavaScript, I’m using Cloud Functions, a serverless Function as a service (FaaS) Google Cloud Platform. Serverless meaning that I don’t need to maintain servers and FaaS meaning that the code only runs when it’s called.
Node.js is a JavaScript runtime built on Google’s V8 JavaScript engine (you can use other JS engines if you would like). Basically, JavaScript for servers. It prides itself on being single threaded, event-driven, and having non-blocking I/O.
For source code, email me at the at domain and I’ll update this post with the source code. It’s way too hard to extract from the PDF.