Building a FAQ bot in DialogFlow in 15 minutes

Just upload a knowledge base!

Lak Lakshmanan
Google Cloud - Community
4 min readApr 15, 2020

--

I’ve never built a DialogFlow bot before and decided that I needed to remedy this. So, I decided to build a chat bot that would answer basic questions. But on what topic?

Everyone is going gaga over the lawyers’ description of Google Cloud services, and so I decided to build a bot that would explain any GCP service based on how the lawyers describe it in the Terms of Service:

So, if you ask the bot “What is Cloud VPN?”, it should read out the whole paragraph. Let’s get started.

1. Create an agent

First thing is to create a DialogFlow agent. Go to https://dialogflow.cloud.google.com/#/agents and click on the button to create a new agent. An agent is a bot.

Give the bot the name “google-cloud-services”, accept other defaults, and click Create.

On the left-hand menu, click on the “settings” bar. Make sure of a few things:

  • a service-account is set.
  • you are using the V2 API.
  • Turn on the beta features. We are going to use “Knowledge Base” and that’s a beta feature.

Now hit “Save”

2. Create an intent

Click on Intents. You should see two intents. As best as I can tell, intents are question-answer pairs. There is a “Fallback Intent” which is what the bot will answer when it doesn’t understand a question. And a “Welcome Intent” which is how the bot says hello back.

Let’s add a real intent. Suppose the user asks our bot “What can you do?”, we’d like the bot to be able to tell them.

So, click on “Create Intent”. Give the intent the name “reason-for-existence”.

Then, click on “Add Training Phases” and add a few ways that the user can ask this question:

Now, click on “Add response” and type in what you want the bot to say back:

I can tell you about Google Cloud Services. Try me. Say “What is BigQuery?”

Now, click “Save”.

On the right-hand side, you’ll see a “Try it now” box. Type in: “what can you do?” and you should see:

3. Create a knowledge base

We can go around typing in questions and answers, but this will get rather boring. Instead, let’s load our knowledge base into DialogFlow.

First, we have to create the knowledge base as a CSV file. I simply copy-pasted the terms of service into a file named terms.txt and then ran this Python code:

#!/usr/bin/env python3
import pandas as pd
questions = []
answers = []
with open('terms.txt') as ifp:
for line in ifp.readlines():
pieces = line.strip().split(':')
if len(pieces) >= 2:
term = pieces[0]
definition = ':'.join(pieces[1:]).strip()
if len(definition) > 10:
questions.append("What is {}?".format(term))
answers.append(definition)
df = pd.DataFrame.from_dict({
'question': questions,
'answer': answers
})
df.to_csv('knowledge_base.csv', index=False, header=False)

This creates a file named knowledge_base.csv where the first column is the question and the second column is the answer. It’s not perfect, since lines that don’t have a colon get skipped. But it’s a decent start, so let’s go with it.

If you don’t have access to Python or don’t feel like running the above program, I’ve put the file on Google Cloud Storage for you at:

gs://cloud-training-demos/termsbot/knowledge_base.csv

4. Load up the knowledge base

In DialogFlow, on the left-hand menu, click on “Knowledge” and select “Create knowledge base”.

Give it the name “services” and click Save. Then, create a new document by clicking on the hyperlink “Create the first one”.

Fill out the dialog as follows (use the “Upload file from your computer” option if you actually ran the Python code).

This will take a minute or so.

Now, click on “Add Response”

The default is to use the answer in the knowledge base.

Click “Save”.

4. Try the bot

Type in “What is BigQuery?” in the “Try it now” box and you should see:

That’s it, folks. You have a bot that will regurgitate any glossary, question-answer, FAQ, static information, etc.

Click on “See how it works in Google Assistant” to try it out in a simulated phone.

5. Deploy the service …

I didn’t do this. You should, though!

--

--

Lak Lakshmanan
Google Cloud - Community

articles are personal observations and not investment advice.