Building a FAQ bot in DialogFlow in 15 minutes

Just upload a knowledge base!

Lak Lakshmanan
Apr 15, 2020 · 4 min read

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

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

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

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

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

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 …

Google Cloud - Community

Google Cloud community articles and blogs

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Lak Lakshmanan

Written by

Data Analytics & AI @ Google Cloud

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store