A Healthy Dialogflow Part II: Development, Deployment, and Analysis

Zakariya Ahmed
Slalom Build
Published in
8 min readApr 6, 2020

In Part I of A Healthy Dialogflow, we discussed the concepts and features comprising Google’s NLP service and utilizing the platform for building a chatbot application known as eVect Health. Using Dialogflow’s intents and entities, we were able to train the agent to converse with an end-user. The application also utilized context, enabling our application to converse more naturally with a contextual understanding of end-user requests.

With these Dialogflow concepts in mind, we are now able to move onto development and deployment of the Dialogflow application, the process known as fulfillment. In order for the Dialogflow agent to dynamically respond to user requests with actionable information, the agent needs to call a web hook to reach out to other services.

In Part II, we will discuss creating a Google Cloud Function which Dialogflow will call to complete user requests — bringing the flow full circle. The Cloud Function in turn will call other services, such as Google BigQuery, showing how Dialogflow can be integrated with multiple services and applications utilizing microservice architecture design patterns.

If you are not comfortable with Dialogflow at a high level yet or need a quick refresher on the basic concepts, I recommend reading our Dialogflow crash course in Part I before continuing on.

All the example code snippets are documented and available on GitHub here if you wish to follow along.

Section I: Developing a Dialog(flow)

Dialogflow dynamically responds to user requests via fulfillment. This is enabled via web hook to a GCP Cloud Function. Each intent has a corresponding function. The function handler must be mapped to the intent:

let intentMap = new Map();intentMap.set('Warning and Prevention', warningAndPreventionIntent);agent.handleRequest(intentMap);

Dialogflow Agent

Dialogflow sends in an agent object to every function handler. The agent handles conversation and context state. For example, when a user says I will be traveling to Indonesia, the agent captures the value of Indonesia as a geo-country:

function warningAndPreventionIntent(agent) {
let userCountry = agent.parameters['geo-country'];

The agent also responds to user inputs and queries. Use the agent.add() function to respond to the user:

const gotCountry = userCountry.length > 0;

if(gotCountry) {
agent.add('Im looking into your trip');
}

Cloud Function BigQuery Integration

Once Dialogflow captures the proper input arguments from the user, the Cloud Function will send a request to GCP BigQuery:

const OPTIONS = {
query: 'SELECT disease.name FROM
`la-hackathon-agent.evect_health.cdc_disease`,
unnest(disease) disease WHERE country = @country',
timeoutMs: 10000,
useLegacySql: false,
params: {country: userCountry[0]}
};
return bigquery
.query(OPTIONS)
.then(results => {
const ROWS = results[0];
let diseaseList = [];
if(ROWS.length > 1) {
agent.add(`Here is a list of active diseases and
contagions in ${userCountry}. \n -
${diseaseList.join('\n - ')} \nIf you would
like prevention tips on a disease, respond
with the name of the disease.`);

Using agent.add(), Dialogflow can respond to the user request dynamically with BigQuery results.

Intent Context

In order for Dialogflow follow-up intents to understand conversation flow, context must be used. Context acts as an event bus, being used to pass user input values to follow-up intents. For example, in Warning and Prevention the agent captures the geo-country the user mentions. The country parameter is then added to the context object to be used by a future intent:

let userCountry = agent.parameters['geo-country'];agent.context.set({
name: 'prevention-followup',
lifespan: 2,
parameters: {
country: userCountry
}
});

The country context parameter is utilized in the follow-up intent’s BigQuery request:

function warningAndPreventionFollowup(agent) {
const preventionContext =
agent.context.get('prevention-followup');
const disease = agent.parameters['Disease'];
const userCountry = preventionContext.parameters['country'];
const OPTIONS = {
query: 'SELECT disease.description FROM `la-hackathon-
agent.evect_health.cdc_disease`,
unnest(disease) disease where disease.name=
@dis and country = @country',
timeoutMs: 10000,
useLegacySql: false,
params: {dis: disease, country: userCountry[0]}
};

Fulfillment Deployment

This Dialogflow web hook is hosted on Cloud Functions for Firebase, which can be deployed via the following steps (NOTE: Node.js is required to deploy the fulfillment):

  1. Setup Firebase CLI
    npm install -g firebase-tools
  2. Authenticate
    firebase login
  3. Initialize Firebase
    firebase init
  4. Deploy the Cloud Function
    npm install
    firebase deploy — only functions

Once the Cloud Function is deployed, head to the Dialogflow console navigation menu. Click Fulfillment, toggle Webhook to ENABLED , and replace the url in the URL field with the Cloud Function address.

The console makes it simple for the agent to use web hooks to fulfill requests

eVect Health BigQuery Database

The agent’s responses and features are powered by data stored in Google Cloud Platform’s BigQuery. For more information on how to setup BigQuery, see the documentation here.

Next Steps

Seamless deployments make it easier to iterate on your DialogFlow fulfillment solution

While this was a simple POC to showcase the power of Dialogflow backed by BigQuery and Cloud Functions, several feature enhancements could make the agent even more powerful. Here are some potential next steps:

  1. Gather more data for BigQuery and automate ingestion.

The data used by this project is a small sample set of all vector borne diseases tracked across the globe. Ingesting global data would make the agent more robust and able to assist a diverse set of users across the world. Data could be pulled in a number of ways, such as Cloud Functions triggered by Cloud Scheduler to access API endpoints, or streamed and written to BigQuery using Pub/Sub and Dataflow.

2. Persist user conversations.

To better tailor the agent to information users are requesting, user input should be persisted after the conversation for use in training the agent. Using this information to iteratively train the agent will lead to more fluid and natural interactions between end-user and agent.

Furthermore, entities mentioned by the user such as Symptom and geo-city should also be persisted. Storing entity data would allow for the creation of a native analytics service, providing descriptive and predictive analysis about where and when outbreaks of diseases occur based on user input. These early detection systems would allow the Dialogflow agent to utilize an in-house custom model in addition to publicly available data. Such a data repository would also be helpful to public health researchers and government agencies seeking to track the spread of vector borne diseases.

3. Multilingual agent support.

eVect Health provides health and medical information for countries and cities throughout the world. The agent currently only interprets English inputs. Future iterations may include support for other languages, enabling non-English speakers throughout the globe to interact with the application. Dialogflow currently supports speech-to-text/speech recognition for 20 of the most common global languages in addition to regional dialects.

Code Base

The codebase can be found here. If you are interested in replicating this project, Dialogflow has an Import/Export function that will allow you to import an entire agent into a new project. You can find the eVect Health Agent under dialogflow_agent/eVect.zip.

Section II: DialogFlow Performance Analysis

Overall, the Dialogflow platform performed well given the training data used. Here are some highlights:

  1. Latency from Cloud Function backed fulfillment is very responsive. When querying BigQuery, responses returned within 400–1200ms. If there is no query required for the Intent, fulfillment responded in approximately 10ms.
  2. Dialogflow has excellent scalability when backed by serverless Cloud Functions.
  3. Dialogflow integrates with many popular platforms such as Facebook Messenger, Slack, and Google Assistant. Testing on Google Assistant is as easy as logging in with your GCP project email account and saying:
    Talk to my test app.
    This makes deployments of test code and training data incredibly easy with one-click integrations and can massively speed up development time.
  4. The agent ingests training data within minutes, allowing for quick testing and development cycles.
  5. Training Dialogflow does not take an in-depth understanding of machine learning. Training can be done directly from the console, allowing developers or business analysts without mathematical backgrounds to build on this NLP platform.
  6. The agent has difficulty understanding native English speakers when pronouncing non-native-English city locations. For example, the agent could not understand Mogi Guaçu when pronounced by a native English speaker. Fortunately, the agent will accept both speech and text, so the remedy for these scenarios could be to simply use the keyboard when speaking to the agent over Google Assistant.
  7. There have been some cases of inconsistencies with the built in entities on Dialogflow based on which platform the user is testing with.
    For example, when a user mentions Tanzania, the Dialogflow console will resolve the value to Tanzania but when testing on Google Assistant, Tanzania resolved to Tanzania, United Republic of. This has to be manually handled in the codebase.

Conclusion

From a developer perspective, working with Dialogflow is a seamless and enjoyable experience. The web console makes training the agent and building intents and entities straightforward while providing an easy way to deploy the application to various testing environments. Allowing fulfillment to work via web hooks gives the developer flexibility on the languages and services used to implement functionality.

From a business perspective, Dialogflow is a top-tier AI-driven chatbot platform supporting integrations for Twitter, Facebook Messenger, Slack, Amazon Alexa, and others, allowing businesses to reach customers on their preferred platforms. Dialogflow also has a Service Level Agreement (SLA) backed by one of the biggest cloud providers, GCP. It is easy to envision an organization utilizing this platform for any of the following:

  1. Automating FAQ responses on a website.
  2. Providing users product support via Google Assistant or other integrated third party platforms without the need for human resources.
  3. Integrating with Facebook or Twitter to assist with handling customer support tickets and boosting social media reach and responsiveness.

While most AI/ML platforms are still in the early stages of growth and development, Dialogflow can tackle the challenges of business scaleability and responsiveness today. Dialogflow provides the opportunity and capacity to deliver cost savings, increase end-user engagement, and provide satisfying natural user experiences when interacting with chat bots.

Thanks

Special thanks to the team of technologists at Slalom LA that worked together to build this Dialogflow POC. If you want to dive deeper into this Dialogflow project, feel free to check out the codebase. If you have comments or questions about Dialogflow, Google Cloud Platform, moving your business to the cloud, or just the cloud in general, feel free to post in the comments section. Thank you!

--

--

Zakariya Ahmed
Slalom Build

Talking to myself because I am my own consultant.