Building your own AI Assistant

Tech giants are betting big on conversational interfaces; Facebook acquired wit.ai, Google acquired api.ai, and Amazon announced Lex. These services all make it easy to parse user intention from natural language. In this tutorial, we’ll demonstrate how to connect a conversational interface with a third-party API. These steps will enable you to build rich experiences for the Google Home, Amazon Echo, Microsoft Cortana, Facebook chatbots, Slack Bots, or another AI assistant. For this example, we’ll build a Google Home action that tells us the height of Star Wars characters. We’ll use api.ai, Amazon Lambda and API Gateway, and the Star Wars API.

1. Create Your Conversational AI Assistant

  1. Sign up for api.ai. I used my Google Account.
  2. Click on “Create Agent.”
  3. Give your agent a name and description.
  1. Name: StarWars
    Description: A conversational assistant that can tell you the height of various star wars characters.
  2. Click “Save.”

2. Create A User Intent

An intent describes something a user is asking for. For example, a customer service agent may have a “returns” intent and a “tech support” intent. Our intent is for when the user asks about the height of a star wars character.

  1. Add a new intent by clicking the “+” next to “Intents.”

b. Populate the intent.

    1. Intent Name: Height Intent
    2. Under User Says, we add different ways to express the intention that the user wants to know the height of a Star wars character:
      • “How tall is Luke Skywalker?”
      • “How short is Yoda?”
      • “What is the height of Boba Fett?”
      • “Tell me how big Chewbacca is.”
    3. Tag parameters. Notice that api.ai is automatically recognizing “Luke” and “Yoda” as a given-name and “Skywalker” as a last-name. Click on “Boba” and “Chewbacca” and identify them as given-name. Assign “Fett” as last-name.
  1. Test the intent in the right hand pane.
    Type “How tall is Yoda?”
  1. Notice how the Height Intent is correctly identified, even though we didn’t train that exact wording. Also notice that “Yoda” has been correctly extracted as a given-name.
  2. Click “Save.”

At this point, we’ve successfully trained our agent to:

  • Recognize when users ask about a Star Wars character’s height, and
  • Extract the character’s name

Now, we’ll create a microservice to accept and process this information.

3. Create an Amazon IAM User

  1. Sign up for Amazon AWS. I was able to use my existing Amazon account. You will have to enter a credit card (I used the one they had on file). Amazon bills based on usage, but you get 1 million free requests per month; this is more than enough to hobby-develop for free.
  2. Create an IAM user. AWS Identity and Access Management (IAM) enables you to securely control access to AWS services and resources. We’ll create an IAM user so we aren’t using our root Amazon account.
    1. Navigate to console.aws.amzon.com/iam
    2. Select “Users” from the left pane. Click “Add User.”

c. Fill in the user info:

  1. User name: TestUser
    Access type: AWS Management Console access
    Console Password: Custom Password
    Uncheck “Require password reset”
  2. Click “Next: Permissions”
  3. Click the checkboxes to add the following Permissions:
    • AWSLambdaFullAccess

 

AmazonAPIGatewayAdministrator

IAMFullAccess

  1. Click “Next: Review.” Your overview should look like this:
    1. Click “Create User”
  1. Sign in to your new IAM user.

4. Create A Lambda Function

  1. Navigate to https://console.aws.amazon.com/lambda/home
  2. Click on “Create a Lambda function.”

Configure the Lambda function:

    1. Use the “hello-world” blueprint
    2. Under “Configure Triggers”, just press “Next.”
    3. Under “Configure Function
      Name: starWarsBot
      Description: A middleman between api.ai and swapi.co
  1. Create a Handler and Role:
  1. Handler: index.handler
    Role: Create new role from templates
    Role Name: simpleMicroservice
    Template: Simple Microservice permissions
  2. Click “Next.”

5. Code Your Lambda Function

At this point we have a hosted node.js function that we can edit on the web. Now, we’ll change the code to accept our request from api.ai and query swapi.co.

  1. Replace the Lambda code with the following code:
    'use strict';
    let http = require('http');
    let starWarsAPI = `www.swapi.co`;
    
    exports.handler = function(event, context, callback) {
      let firstName = event.result.parameters['given-name'];
      let lastName = event.result.parameters['last-name'];
      
      let options = searchPeopleRequestOptions(firstName, lastName);
      
      makeRequest(options, function( data, error) {
        let person = data.results[0];
        if (person) {
            let height = person.height;
            let response = person.name + " is " + height + " centimeters tall.";
            callback(null, {"speech": response});
        }
        else {
            callback(null, {"speech": "I'm not sure!"});
        }
      });
    };
    
    function searchPeopleRequestOptions(firstName, lastName) {
        return {
            host: starWarsAPI,
            path: `/api/people/?search=`+firstName+'+'+lastName
        };
    }
    
    function makeRequest(options, callback) {
        var request = http.request(options, 
        function(response) {
            var responseString = '';
            response.on('data', function(data) {
                responseString += data;
            });
             response.on('end', function() {
                var responseJSON = JSON.parse(responseString);
                callback(responseJSON, null);
            });
        });
        request.end();
    }

    This code extracts the given-name and last-name from the inbound event and formats a web request to www.swapi.co/api/people/?search. It then extracts the height from the response and returns {"speech":" "} to api.ai.

  2. Press “Save.”
  3. Test Your Lambda Function.
    1. Back at api.ai, run a test query, and press “show JSON.”
  1. Copy the JSON
  2. Configure a Test Event for your Lambda
      1. Click Actions > Configure Test Event.
      2. Paste the JSON.
      3. Click “Save and Test.” Your test should succeed and you should see the output:
        {
          "speech": "Chewbacca is 228 centimeters tall."
        }

6. Create an Endpoint in API Gateway

At this point, we have:

  • An api.ai agent that collects user input and POSTs JSON.
  • A Lambda function that can consume that JSON, query swapi.co, and return a speech response.

Now, we need to enable the two pieces to communicate with each other.

  1. Navigate to http://console.aws.amazon.com/apigateway/home
  2. Create a New API:
  1. API name: Starwars
    Description: A microservice for finding information about star wars
  2. Create an API endpoint:

    Actions > Create Resource
    Resource Name: height
    Actions > Create Method > POST
    Integration Type: Lambda Function
    Lambda Region: **Must match your Lambda region**. (You should be able to figure out the region from the page url.)
    Lambda Function: starWarsBot
  3. Deploy your API:

    Actions > Deploy API
    Deployment Stage: [New Stage]
    Stage Name: beta
    Click Deploy
  4. Copy the “invoke URL”

7. Use Your New API for Intent Fulfillment

  1. Back at api.ai, choose “Fulfillment” from the left pane.
  1. Enable the switch, paste the URL from your API, and complete the url to point at the “/height” endpoint.
  2. Click “Save.”
  3. Navigate to our intent (Intents > Height Intent), expand the fulfillment section, and check the “Use webhook” box.
  4. In the “Try it now” field in the right pane, try another query.

    You should see the correct response: “Yoda is 66 centimeters tall.”

8. Test On The Google Home

  1. Navigate to “Integrations” from the left pane.
  2. Turn on “Actions on Google.”

    Invocation name for testing: Star Wars
  3. Press “Authenticate.” This will have you sign in to your Google Account. You’ll want this to be the same as the Google Account that the Google Home is set up with.
  4. Press “Preview.” A Message should pop up on the bottom of the screen:
  5. Click on the link in the pop up. (Or here.)
  6. Test on the Web Simulator.

    Note: You must first ask to talk to the Star Wars agent, or phrase your question with the agent’s name: (“Ask Star Wars how tall Yoda is.”)
  7. Test on the Google Home!
    Try out the agent on any Google Home set up with the same Google Account that you developed with. If the package preview runs out, you can refresh it for 24 hours on the Web Simulator by clicking Action Package > Preview for 24 hours.

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link