Node.js idiomatic client for Prediction API.
The Cloud Prediction API provides a RESTful API to build Machine Learning models.
This library is deprecated. The API will be shut down on April 30, 2018. See the Prediction API End of Life FAQ for more information. |
Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.
Table of contents:
-
Select or create a Cloud Platform project.
-
Enable billing for your project.
-
Enable the Google Prediction API API.
-
Set up authentication with a service account so you can access the API from your local workstation.
npm install --save @google-cloud/prediction
var google = require('googleapis');
function auth(callback) {
google.auth.getApplicationDefault(function(err, authClient) {
if (err) {
return callback(err);
}
// The createScopedRequired method returns true when running on GAE or a
// local developer machine. In that case, the desired scopes must be passed
// in manually. When the code is running in GCE or GAE Flexible, the scopes
// are pulled from the GCE metadata server.
// See https://cloud.google.com/compute/docs/authentication for more
// information.
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
// Scopes can be specified either as an array or as a single,
// space-delimited string.
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/prediction',
]);
}
callback(null, authClient);
});
}
/**
* @param {string} phrase The phrase for which to predict sentiment,
* e.g. "good morning".
* @param {Function} callback Callback function.
*/
function predict(phrase, callback) {
auth(function(err, authClient) {
if (err) {
return callback(err);
}
var hostedmodels = google.prediction({
version: 'v1.6',
auth: authClient,
}).hostedmodels;
// Predict the sentiment for the provided phrase
hostedmodels.predict(
{
// Project id used for this sample
project: '414649711441',
hostedModelName: 'sample.sentiment',
resource: {
input: {
// Predict sentiment of the provided phrase
csvInstance: phrase.split(/\s/gi),
},
},
},
function(err, prediction) {
if (err) {
return callback(err);
}
// Received prediction result
console.log(`Sentiment for "${phrase}": ${prediction.outputLabel}`);
callback(null, prediction);
}
);
});
}
Samples are in the samples/
directory. The samples' README.md
has instructions for running the samples.
Sample | Source Code |
---|---|
Hosted Models | source code |
The Prediction API Node.js Client API Reference documentation also contains samples.
This library follows Semantic Versioning.
This library is deprecated. This means that it is no longer being actively maintained and the only updates the library will receive will be for critical security issues.
More Information: Google Cloud Platform Launch Stages
Contributions welcome! See the Contributing Guide.
Apache Version 2.0
See LICENSE