How to consume an Azure Machine Learning Web service

Once you deploy an Azure Machine Learning predictive model as a Web service, you can use a REST API to send it data and get predictions. You can send the data in real-time or in batch mode.

You can find more information about how to create and deploy a Machine Learning Web service using Machine Learning Studio here:

Note

Try Azure Machine Learning for free

No credit card or Azure subscription required. Get started now.

Overview

With the Azure Machine Learning Web service, an external application communicates with a Machine Learning workflow scoring model in real time. A Machine Learning Web service call returns prediction results to an external application. To make a Machine Learning Web service call, you pass an API key that is created when you deploy a prediction. The Machine Learning Web service is based on REST, a popular architecture choice for web programming projects.

Azure Machine Learning has two types of services:

  • Request-Response Service (RRS) – A low latency, highly scalable service that provides an interface to the stateless models created and deployed from the Machine Learning Studio.
  • Batch Execution Service (BES) – An asynchronous service that scores a batch for data records.

For more information about Machine Learning Web services, see Deploy a Machine Learning Web service.

Get an Azure Machine Learning authorization key

When you deploy your experiment, API keys are generated for the Web service. You can retrieve the keys from several locations.

From the Microsoft Azure Machine Learning Web Services portal

Sign in to the Microsoft Azure Machine Learning Web Services portal.

To retrieve the API key for a New Machine Learning Web service:

  1. In the Azure Machine Learning Web Services portal, click Web Services the top menu.
  2. Click the Web service for which you want to retrieve the key.
  3. On the top menu, click Consume.
  4. Copy and save the Primary Key.

To retrieve the API key for a Classic Machine Learning Web service:

  1. In the Azure Machine Learning Web Services portal, click Classic Web Services the top menu.
  2. Click the Web service with which you are working.
  3. Click the endpoint for which you want to retrieve the key.
  4. On the top menu, click Consume.
  5. Copy and save the Primary Key.

Classic Web service

You can also retrieve a key for a Classic Web service from Machine Learning Studio or the Azure classic portal.

Machine Learning Studio

  1. In Machine Learning Studio, click WEB SERVICES on the left.
  2. Click a Web service. The API key is on the DASHBOARD tab.

Azure classic portal

  1. Click MACHINE LEARNING on the left.
  2. Click the workspace in which your Web service is located.
  3. Click WEB SERVICES.
  4. Click a Web service.
  5. Click an endpoint. The “API KEY” is down at the lower-right.

Connect to a Machine Learning Web service

You can connect to a Machine Learning Web service using any programming language that supports HTTP request and response. You can view examples in C#, Python, and R from a Machine Learning Web service help page.

Machine Learning API help Machine Learning API help is created when you deploy a Web service. See Azure Machine Learning Walkthrough- Deploy Web Service. The Machine Learning API help contains details about a prediction Web service.

  1. Click the Web service with which you are working.
  2. Click the endpoint for which you want to view the API Help Page.
  3. On the top menu, click Consume.
  4. Click API help page under either the Request-Response or Batch Execution endpoints.

To view Machine Learning API help for a New Web service

In the Azure Machine Learning Web Services Portal:

  1. Click WEB SERVICES on the top menu.
  2. Click the Web service for which you want to retrieve the key.

Click Use Web Service to get the URIs for the Request-Reposonse and Batch Execution Services and Sample code in C#, R, and Python.

Click Swagger API to get Swagger based documentation for the APIs called from the supplied URIs.

C# Sample

To connect to a Machine Learning Web service, use an HttpClient passing ScoreData. ScoreData contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

To connect to a Machine Learning Web service, the Microsoft.AspNet.WebApi.Client NuGet package must be installed.

Install Microsoft.AspNet.WebApi.Client NuGet in Visual Studio

  1. Publish the Download dataset from UCI: Adult 2 class dataset Web Service.
  2. Click Tools > NuGet Package Manager > Package Manager Console.
  3. Choose Install-Package Microsoft.AspNet.WebApi.Client.

To run the code sample

  1. Publish "Sample 1: Download dataset from UCI: Adult 2 class dataset" experiment, part of the Machine Learning sample collection.
  2. Assign apiKey with the key from a Web service. See Get an Azure Machine Learning authorization key above.
  3. Assign serviceUri with the Request URI.

Here is what a complete request will look like.

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CallRequestResponseService
{
    class Program
    {
        static void Main(string[] args)
        {
            InvokeRequestResponseService().Wait();
        }

        static async Task InvokeRequestResponseService()
        {
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary<string, List<Dictionary<string, string>>> () {
                        {
                            "input1",
                            // Replace columns labels with those used in your dataset
                            new List<Dictionary<string, string>>(){new Dictionary<string, string>(){
                                    {
                                        "column1", "value1"
                                    },
                                    {
                                        "column2", "value2"
                                    },
                                    {
                                        "column3", "value3"
                                    }
                                }
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>() {}
                };

                // Replace these values with your API key and URI found on https://services.azureml.net/
                const string apiKey = "<your-api-key>"; 
                const string apiUri = "<your-api-uri>";

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
                client.BaseAddress = new Uri(apiUri);

                // WARNING: The 'await' statement below can result in a deadlock
                // if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false)
                // so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)

                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Result: {0}", result);
                }
                else
                {
                    Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));

                    // Print the headers - they include the requert ID and the timestamp,
                    // which are useful for debugging the failure
                    Console.WriteLine(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseContent);
                }
            }
        }
    }
}

Python Sample

To connect to a Machine Learning Web service, use the urllib2 library for Python 2.X and urllib.request library for Python 3.X. You will pass ScoreData, which contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

To run the code sample

  1. Deploy "Sample 1: Download dataset from UCI: Adult 2 class dataset" experiment, part of the Machine Learning sample collection.
  2. Assign apiKey with the key from a Web service. See the Get an Azure Machine Learning authorization key section near the beginning of this article.
  3. Assign serviceUri with the Request URI.

Here is what a complete request will look like.

import urllib2 # urllib.request for Python 3.X
import json

data = {
    "Inputs": {
        "input1":
        [
            {
                'column1': "value1",   
                'column2': "value2",   
                'column3': "value3"
            }
        ],
    },
    "GlobalParameters":  {}
}

body = str.encode(json.dumps(data))

# Replace this with the URI and API Key for your web service
url = '<your-api-uri>'
api_key = '<your-api-key>'
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

# "urllib.request.Request(uri, body, headers)" for Python 3.X
req = urllib2.Request(url, body, headers)

try:
    # "urllib.request.urlopen(req)" for Python 3.X
    response = urllib2.urlopen(req)

    result = response.read()
    print(result)
# "urllib.error.HTTPError as error" for Python 3.X
except urllib2.HTTPError, error: 
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
    print(error.info())
    print(json.loads(error.read())) 

R Sample

To connect to a Machine Learning Web Service, use the RCurl and rjson libraries to make the request and process the returned JSON response. You will pass ScoreData, which contains a FeatureVector, an n-dimensional vector of numerical features that represents the ScoreData. You authenticate to the Machine Learning service with an API key.

Here is what a complete request will look like.

library("RCurl")
library("rjson")

# Accept SSL certificates issued by public Certificate Authorities
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

h = basicTextGatherer()
hdr = basicHeaderGatherer()

req = list(
    Inputs = list(
            "input1" = list(
                list(
                        'column1' = "value1",
                        'column2' = "value2",
                        'column3' = "value3"
                    )
            )
        ),
        GlobalParameters = setNames(fromJSON('{}'), character(0))
)

body = enc2utf8(toJSON(req))
api_key = "<your-api-key>" # Replace this with the API key for the web service
authz_hdr = paste('Bearer', api_key, sep=' ')

h$reset()
curlPerform(url = "<your-api-uri>",
httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
postfields=body,
writefunction = h$update,
headerfunction = hdr$update,
verbose = TRUE
)

headers = hdr$value()
httpStatus = headers["status"]
if (httpStatus >= 400)
{
print(paste("The request failed with status code:", httpStatus, sep=" "))

# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(headers)
}

print("Result:")
result = h$value()
print(fromJSON(result))

JavaScript Sample

To connect to a Machine Learning Web Service, use the request npm package in your project. You will also use the JSON object to format your input and parse the result. Install using npm install request --save, or add "request": "*" to your package.json under dependencies and run npm install.

Here is what a complete request will look like.

let req = require("request");

const uri = "<your-api-uri>";
const apiKey = "<your-api-key>";

let data = {
    "Inputs": {
        "input1":
        [
            {
                'column1': "value1",
                'column2': "value2",
                'column3': "value3"
            }
        ],
    },
    "GlobalParameters": {}
}

const options = {
    uri: uri,
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer " + apiKey,
    },
    body: JSON.stringify(data)
}

req(options, (err, res, body) => {
    if (!err && res.statusCode == 200) {
        console.log(body);
    } else {
        console.log("The request failed with status code: " + res.statusCode);
    }
});