Building an End to End Search Engine Chatbot for the website using Amazon Lex, Google Knowledge Graph, and CloudFront | by anuragbisht | Towards AI — Multidisciplinary Science Journal | Oct, 2020 | Medium

Image for post

Chabot term was coined from the original term “ChatterBot” created back in 1994, the term itself says a machine that can do human conversations. Although it’s interesting to learn about chatbot the actual power lies in solving actual business world use-cases where you can automate most of the manual labor done physically by human beings.

Imagine you interacting with a smart speaker today booking a flight or ordering food for you, nobody would have thought doing these things with the same convenience a decade back. Imagine the customer support today with automated chatbots handling customers with automated replies. How much time and money would it require humans doing the same work. That’s the power of conversation AI whether it’s a voice-based system or chat-based system.

Amazon provides a powerful service, Amazon Lex that can create conversation AI chatbot, now Amazon Lex uses the same conversational engine that powers Amazon Alexa (voice-based system).

Today we will create a Search Bot using Amazon Lex that can find information for us from the web and deploy it using web-based UI with web application hosted over AWS CloudFront.

Before we dive deep into building the conversation AI chatbot, let’s first understand the important terminologies used in conversation AI space.

Important terminologies:

We will follow the following steps to create an end to end solution.

Let’s start with the first step:

If you are new to AWS, then you can register for an AWS account, otherwise, AWS users can search for Amazon Lex under Machine Learning services within the AWS console.

Now we have a simple bot base ready, let’s go one step further adding new intent: SearchCountry. This intent allows the users to ask the chatbot to search for information about the country.

2. Setup Google Knowledge Graph service:

Now we also need a search engine that can search for information about countries for us. We will use the Google Knowledge Graph API service.

For this google has put up a well-documented section on how to start with google knowledge graph search API service.

Essentially we will obtain an API key to use the knowledge graph service API and use any programming language to send the HTTP requests with query string parameters(the same mechanism works when to search for anything on google search, observer the address URL bar).

Image for post

3. Add the AWS lambda function to fulfill the response

Once we have access to the service, we will add a new intent and link that intent with the google service.

For this, we will create a new intent for user search action: SearchQuery

So far we have created a mechanism to capture country value, but how will we return the search results. For that, we will use the AWS lambda function to get the relevant information by calling the knowledge graph API service.

We will head over to the search console and under computing services, we will select AWS lambda.

Once the function is created, let’s add the following code and comment existing function definition.

def search_query(intent_request):

query = get_slots(intent_request)[“Country”]
params = {
‘query’: query,
‘limit’: 10,
‘indent’: True,
‘key’: api_key,

# url = service_url + ‘?’ + urllib.parse.urlencode(params)
# response = json.loads(urllib.request.urlopen(url).read())

# response=json.loads(requests.get(url))
url = service_url

information=’Sorry could not find what you are looking for.’

return close(intent_request[‘sessionAttributes’],
{‘contentType’: ‘PlainText’,
‘content’: information})

def dispatch(intent_request):
Called when the user specifies an intent for this bot.

logger.debug(‘dispatch userId={}, intentName={}’.format(intent_request[‘userId’], intent_request[‘currentIntent’][‘name’]))

intent_name = intent_request[‘currentIntent’][‘name’]

# Dispatch to your bot’s intent handlers
if intent_name == ‘SearchQuery’:
return search_query(intent_request)

raise Exception(‘Intent with name ‘ + intent_name + ‘ not supported’)

“”” — — Main handler — — “””

def lambda_handler(event, context):
Route the incoming request based on intent.
The JSON body of the request is provided in the event slot.
# By default, treat the user request as coming from the America/New_York time zone.
os.environ[‘TZ’] = ‘America/New_York’

return dispatch(event)

You can find the full lambda function here.

Once the function is ready, click the deploy button to deploy the lambda function.

After deployment of the lambda function, we can select that particular lambda function to fulfill the response by the chatbot.

4. Deploying website using Cloudformation stack for Bot UI

Now we have the Search bot ready, but we have to think about hosting the user interface of this bot, the easiest way is to use a predefined template as a reference from the amazon link.

For this activity, we will create an AWS cloud formation stack that allows the deployment of the whole web application with just a few clicks. This type of deployment methodology is termed as infrastructure as a code.

Make sure you the region selected is the same as that of our bot:

You will also have to fill some details for the bot as mentioned below:

Congratulations. You have a baseline working search bot ready, now you can further enhance it by using Iframe as UI and restricting with a small dialog screen than full screen. You can check out more at SnippetUrl for the code snippet. Also, you can enable the AWS Incognito service for providing a security layer on who can access web application but that’s beyond scope of this post. Let me know your thoughts in the response section if you would like to know more.

If you really enjoyed this post, stay tuned for more tutorials on AI/machine learning, data analytics, and BI.

Check me out on LinkedIn

Translate »