Top Banner
© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Alex Ingerman Sr. Manager, Tech. Product Management, Amazon Machine Learning 1/28/2016 Building a Production-Ready Predictive Customer Service Application with AWS
47

Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Apr 13, 2017

Download

Technology

PAPIs.io
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

© 2015, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Alex IngermanSr. Manager, Tech. Product Management, Amazon Machine Learning

1/28/2016

Building a Production-Ready Predictive Customer Service Application with AWS

Page 2: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Agenda

• What is predictive customer service?

• Using machine learning to find important social media conversations

• Building an end-to-end application to act on these conversations

Page 3: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Application details

Goal: build a smart application for social media listening in the cloud

Full source code and documentation are on GitHub: http://bit.ly/AmazonMLCodeSample

Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Amazon Mechanical Turk

Page 4: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Motivation for listening to social media

Customer is reporting a possible service issue

Page 5: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Motivation for listening to social media

Customer is making a feature request

Page 6: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Motivation for listening to social media

Customer is angry or unhappy

Page 7: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Motivation for listening to social media

Customer is asking a question

Page 8: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Why do we need machine learning for this?

The social media stream is high-volume, and most of the messages are not CS-actionable

Page 9: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon Machine Learning in one slide

• Easy to use, managed machine learning service built for developers

• Robust, powerful machine learning technology based on Amazon’s internal systems

• Create models using your data already stored in the AWS cloud

• Deploy models to production in seconds

Page 10: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Formulating the Business Problem

Page 11: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Formulating the business problem

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Page 12: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the Data Flow

Page 13: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the data flow

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Twitter API

Page 14: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the data flow

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Twitter API Amazon Kinesis

Page 15: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the data flow

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Twitter API Amazon Kinesis

AWSLambda

Page 16: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the data flow

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

Page 17: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Establishing the data flow

We would like to…

Instantly find new tweets mentioning @awscloud, ingest and analyze each one to predict whether a customer service agent should act on it, and, if so, send that tweet to customer service agents.

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 18: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Picking the machine learning strategy

Page 19: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Picking the machine learning strategy

Question we want to answer: Is this tweet customer service-actionable, or not?

Our dataset: Text and metadata from past tweets mentioning @awscloud

Machine learning approach:Create a binary classification model to answer a yes/no question, and provide a confidence score

Page 20: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Obtaining the data

Page 21: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Retrieve past tweets

Twitter API can be used to search for tweets containing our company’s handle (e.g., @awscloud)

import twitter

twitter_api = twitter.Api(**twitter_credentials)

twitter_handle = ‘awscloud’

search_query = '@' + twitter_handle + ' -from:' + twitter_handle

results = twitter_api.GetSearch(term=search_query, count=100, result_type='recent’)

# We can go further back in time by issuing additional search requests

Page 22: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Retrieve past tweets

Twitter API can be used to search for tweets containing our company’s handle (e.g., @awscloud)

import twitter

twitter_api = twitter.Api(**twitter_credentials)

twitter_handle = ‘awscloud’

search_query = '@' + twitter_handle + ' -from:' + twitter_handle

results = twitter_api.GetSearch(term=search_query, count=100, result_type='recent')

# We can go further back in time by issuing additional search requests

Good news: data is well-structured and cleanBad news: tweets are not categorized (labeled) for us

Page 23: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Labeling past tweets

Why label tweets? (Many) machine learning algorithms work by discovering patterns connecting data points and labels

How many tweets need to be labeled? Several thousands to start with

Can I pay someone to do this? Yes! Amazon Mechanical Turk is a marketplace for tasks that require human intelligence

Page 24: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Setting up the machine learning model

Page 25: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon ML process, in a nutshell

1. Create your datasourcesTwo API calls to create your training and evaluation dataSanity-check your data in service console

2. Create your ML modelOne API call to build a model, with smart default or custom setting

3. Evaluate your ML modelOne API call to compute your model’s quality metric

4. Adjust your ML modelUse console to align performance trade-offs to your business goals

Page 26: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Aligning the ML model with business requirements

Page 27: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Building the data flow

Page 28: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Reminder: Our data flow

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 29: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Create an Amazon ML endpoint for retrieving real-time predictions

import boto

ml = boto.connect_machinelearning()

ml.create_realtime_endpoint(“ml-tweets”)

# Endpoint information can be retrieved using the get_ml_model() method. Sample output: #"EndpointInfo": {

# "CreatedAt": 1424378682.266,

# "EndpointStatus": "READY",

# "EndpointUrl": ”https://realtime.machinelearning.us-east-1.amazonaws.com",

# "PeakRequestsPerSecond": 200}

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 30: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Create an Amazon Kinesis stream for receiving tweets

import boto

kinesis = boto.connect_kinesis()

kinesis.create_stream(stream_name = ‘tweetStream’, shard_count = 1)

# Each open shard can support up to 5 read transactions per second, up to a

# maximum total of 2 MB of data read per second. Each shard can support up to

# 1000 records written per second, up to a maximum total of 1 MB data written

# per second.

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 31: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Set up AWS Lambda to coordinate the data flow

The Lambda function is our application’s backbone. We will:

1. Write the code that will process and route tweets2. Configure the Lambda execution policy (what is it allowed to do?)3. Add the Kinesis stream as the data source for the Lambda function

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 32: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Create Lambda functions

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

// These are our function’s signatures and globals only. See GitHub repository for full source.

var ml = new AWS.MachineLearning();

var endpointUrl = '';

var mlModelId = ’ml-tweets';

var snsTopicArn = 'arn:aws:sns:{region}:{awsAccountId}:{snsTopic}';

var snsMessageSubject = 'Respond to tweet';

var snsMessagePrefix = 'ML model '+mlModelId+': Respond to this tweet: https://twitter.com/0/status/';

var processRecords = function() {…} // Base64 decode the Kinesis payload and parse JSON

var callPredict = function(tweetData) {…} // Call Amazon ML real-time prediction API

var updateSns = function(tweetData) {…} // Publish CS-actionable tweets to SNS topic

var checkRealtimeEndpoint = function(err, data) {…} // Get Amazon ML endpoint URI

Page 33: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Create Lambda functions

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

// These are our function’s signatures and globals only. See GitHub repository for full source.

var ml = new AWS.MachineLearning();

var endpointUrl = '';

var mlModelId = ’ml-tweets';

var snsTopicArn = 'arn:aws:sns:{region}:{awsAccountId}:{snsTopic}';

var snsMessageSubject = 'Respond to tweet';

var snsMessagePrefix = 'ML model '+mlModelId+': Respond to this tweet: https://twitter.com/0/status/';

var processRecords = function() {…} // Base64 decode the Kinesis payload and parse JSON

var callPredict = function(tweetData) {…} // Call Amazon ML real-time prediction API

var updateSns = function(tweetData) {…} // Publish CS-actionable tweets to SNS topic

var checkRealtimeEndpoint = function(err, data) {…} // Get Amazon ML endpoint URI

Page 34: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Configure Lambda execution policy

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

{ "Statement": [

{ "Action": [ "logs:*” ],

"Effect": "Allow",

"Resource": "arn:aws:logs:{region}:{awsAccountId}:log-group:/aws/lambda/{lambdaFunctionName}:*"

},

{ "Action": [ "sns:publish” ],

"Effect": "Allow",

"Resource": "arn:aws:sns:{region}:{awsAccountId}:{snsTopic}"

},

{ "Action": [ "machinelearning:GetMLModel”, "machinelearning:Predict” ],

"Effect": "Allow",

"Resource": "arn:aws:machinelearning:{region}:{awsAccountId}:mlmodel/{mlModelId}”

},

{ "Action": [ "kinesis:ReadStream”, "kinesis:GetRecords”, "kinesis:GetShardIterator”, "kinesis:DescribeStream”,"kinesis:ListStreams” ],

"Effect": "Allow",

"Resource": "arn:aws:kinesis:{region}:{awsAccountId}:stream/{kinesisStream}"

}

] }

Page 35: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Configure Lambda execution policy

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

{ "Statement": [

{ "Action": [ "logs:*” ],

"Effect": "Allow",

"Resource": "arn:aws:logs:{region}:{awsAccountId}:log-group:/aws/lambda/{lambdaFunctionName}:*"

},

{ "Action": [ "sns:publish” ],

"Effect": "Allow",

"Resource": "arn:aws:sns:{region}:{awsAccountId}:{snsTopic}"

},

{ "Action": [ "machinelearning:GetMLModel”, "machinelearning:Predict” ],

"Effect": "Allow",

"Resource": "arn:aws:machinelearning:{region}:{awsAccountId}:mlmodel/{mlModelId}”

},

{ "Action": [ "kinesis:ReadStream”, "kinesis:GetRecords”, "kinesis:GetShardIterator”, "kinesis:DescribeStream”,"kinesis:ListStreams” ],

"Effect": "Allow",

"Resource": "arn:aws:kinesis:{region}:{awsAccountId}:stream/{kinesisStream}"

}

] }

Allow request logging in Amazon

CloudWatch

Page 36: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Configure Lambda execution policy

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

{ "Statement": [

{ "Action": [ "logs:*” ],

"Effect": "Allow",

"Resource": "arn:aws:logs:{region}:{awsAccountId}:log-group:/aws/lambda/{lambdaFunctionName}:*"

},

{ "Action": [ "sns:publish” ],

"Effect": "Allow",

"Resource": "arn:aws:sns:{region}:{awsAccountId}:{snsTopic}"

},

{ "Action": [ "machinelearning:GetMLModel”, "machinelearning:Predict” ],

"Effect": "Allow",

"Resource": "arn:aws:machinelearning:{region}:{awsAccountId}:mlmodel/{mlModelId}”

},

{ "Action": [ "kinesis:ReadStream”, "kinesis:GetRecords”, "kinesis:GetShardIterator”, "kinesis:DescribeStream”,"kinesis:ListStreams” ],

"Effect": "Allow",

"Resource": "arn:aws:kinesis:{region}:{awsAccountId}:stream/{kinesisStream}"

}

] }

Allow publication of notifications to

SNS topic

Page 37: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Configure Lambda execution policy

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

{ "Statement": [

{ "Action": [ "logs:*” ],

"Effect": "Allow",

"Resource": "arn:aws:logs:{region}:{awsAccountId}:log-group:/aws/lambda/{lambdaFunctionName}:*"

},

{ "Action": [ "sns:publish” ],

"Effect": "Allow",

"Resource": "arn:aws:sns:{region}:{awsAccountId}:{snsTopic}"

},

{ "Action": [ "machinelearning:GetMLModel”, "machinelearning:Predict” ],

"Effect": "Allow",

"Resource": "arn:aws:machinelearning:{region}:{awsAccountId}:mlmodel/{mlModelId}”

},

{ "Action": [ "kinesis:ReadStream”, "kinesis:GetRecords”, "kinesis:GetShardIterator”, "kinesis:DescribeStream”,"kinesis:ListStreams” ],

"Effect": "Allow",

"Resource": "arn:aws:kinesis:{region}:{awsAccountId}:stream/{kinesisStream}"

}

] }

Allow calls to Amazon ML

real-time prediction APIs

Page 38: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Configure Lambda execution policy

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

{ "Statement": [

{ "Action": [ "logs:*” ],

"Effect": "Allow",

"Resource": "arn:aws:logs:{region}:{awsAccountId}:log-group:/aws/lambda/{lambdaFunctionName}:*"

},

{ "Action": [ "sns:publish” ],

"Effect": "Allow",

"Resource": "arn:aws:sns:{region}:{awsAccountId}:{snsTopic}"

},

{ "Action": [ "machinelearning:GetMLModel”, "machinelearning:Predict” ],

"Effect": "Allow",

"Resource": "arn:aws:machinelearning:{region}:{awsAccountId}:mlmodel/{mlModelId}”

},

{ "Action": [ "kinesis:ReadStream”, "kinesis:GetRecords”, "kinesis:GetShardIterator”, "kinesis:DescribeStream”,"kinesis:ListStreams” ],

"Effect": "Allow",

"Resource": "arn:aws:kinesis:{region}:{awsAccountId}:stream/{kinesisStream}"

}

] }

Allow reading of data from

Kinesis stream

Page 39: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Connect Kinesis stream and Lambda function

import boto

aws_lambda = boto.connect_awslambda()

aws_lambda.add_event_source(

event_source = 'arn:aws:kinesis:' + region + ':' + aws_account_id + ':stream/' + “tweetStream”,

function_name = “process_tweets”,

role = 'arn:aws:iam::' + aws_account_id + ':role/' + lambda_execution_role)

Twitter API Amazon Kinesis

AWSLambda

Amazon Machine Learning

AmazonSNS

Page 40: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

End-to-end flow

Page 41: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon ML real-time predictions test

Here is a tweet:

Page 42: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon ML real-time predictions test

Here is the same tweet…as a JSON blob:

{

"statuses_count": "8617",

"description": "Software Developer",

"friends_count": "96",

"text": "`scala-aws-s3` A Simple Amazon #S3 Wrapper for #Scala 1.10.20 available : https://t.co/q76PLTovFg",

"verified": "False",

"geo_enabled": "True",

"uid": "3800711",

"favourites_count": "36",

"screen_name": "turutosiya",

"followers_count": "640",

"user.name": "Toshiya TSURU",

"sid": "647222291672100864"

}

Page 43: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon ML real-time predictions test

Let’s use the AWS Command Line Interface to request a prediction for this tweet:

aws machinelearning predict \

--predict-endpoint https://realtime.machinelearning.us-east-1.amazonaws.com \

--ml-model-id ml-tweets \

--record ‘<json_blob>’

Page 44: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Amazon ML real-time predictions test

Let’s use the AWS Command Line Interface to request a prediction for this tweet:

aws machinelearning predict \

--predict-endpoint https://realtime.machinelearning.us-east-1.amazonaws.com \

--ml-model-id ml-tweets \

--record ‘<json_blob>’

{"Prediction": {

"predictedLabel": "0", "predictedScores": {

"0": 0.012336540967226028}, "details": {

"PredictiveModelType": "BINARY", "Algorithm": "SGD"

}}

}

Page 45: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Generalizing to more feedback channels

Amazon Kinesis

AWSLambda

Model 1 AmazonSNS

Model 2

Model 3

Page 46: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

What’s next?

Try the service:http://aws.amazon.com/machine-learning/

Download the Social Media Listening application code: http://bit.ly/AmazonMLCodeSample

Get in [email protected]

Page 47: Building a Production-ready Predictive App for Customer Service - Alex Ingerman @ PAPIs Connect

Thank you!