Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

feat(samples): add feature values apis samples #341

Merged
merged 15 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-ai-platform

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Batch-read-feature-values-sample | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/batch-read-feature-values-sample.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/batch-read-feature-values-sample.js,samples/README.md) |
| Cancel-batch-prediction-job | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/cancel-batch-prediction-job.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/cancel-batch-prediction-job.js,samples/README.md) |
| Cancel-custom-job | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/cancel-custom-job.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/cancel-custom-job.js,samples/README.md) |
| Create-batch-prediction-job-text-classification | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/create-batch-prediction-job-text-classification.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/create-batch-prediction-job-text-classification.js,samples/README.md) |
Expand Down Expand Up @@ -114,6 +115,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-ai-platform
| Delete-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/delete-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/delete-model.js,samples/README.md) |
| Deploy-model-custom-trained-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/deploy-model-custom-trained-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/deploy-model-custom-trained-model.js,samples/README.md) |
| Deploy-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/deploy-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/deploy-model.js,samples/README.md) |
| Export-feature-values-sample | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-feature-values-sample.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-feature-values-sample.js,samples/README.md) |
| Export-feature-values-snapshot-sample | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-feature-values-snapshot-sample.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-feature-values-snapshot-sample.js,samples/README.md) |
| Export-model-tabular-classification | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-model-tabular-classification.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-model-tabular-classification.js,samples/README.md) |
| Export-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-model.js,samples/README.md) |
| Get-batch-prediction-job | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/get-batch-prediction-job.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/get-batch-prediction-job.js,samples/README.md) |
Expand Down Expand Up @@ -142,6 +145,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-ai-platform
| Import-data-video-classification | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/import-data-video-classification.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/import-data-video-classification.js,samples/README.md) |
| Import-data-video-object-tracking | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/import-data-video-object-tracking.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/import-data-video-object-tracking.js,samples/README.md) |
| Import-data | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/import-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/import-data.js,samples/README.md) |
| Import-feature-values-sample | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/import-feature-values-sample.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/import-feature-values-sample.js,samples/README.md) |
| List-endpoints | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/list-endpoints.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/list-endpoints.js,samples/README.md) |
| List-model-evaluation-slices | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/list-model-evaluation-slices.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/list-model-evaluation-slices.js,samples/README.md) |
| Predict-custom-trained-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-custom-trained-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/predict-custom-trained-model.js,samples/README.md) |
Expand All @@ -153,6 +157,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-ai-platform
| Predict-text-entity-extraction | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-text-entity-extraction.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/predict-text-entity-extraction.js,samples/README.md) |
| Predict-text-sentiment-analysis | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/predict-text-sentiment-analysis.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/predict-text-sentiment-analysis.js,samples/README.md) |
| Quickstart | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
| Read-feature-values-sample | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/read-feature-values-sample.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/read-feature-values-sample.js,samples/README.md) |
| Undeploy-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/undeploy-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/undeploy-model.js,samples/README.md) |
| Upload-model | [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/upload-model.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/upload-model.js,samples/README.md) |

Expand Down
90 changes: 90 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ machine learning development lifecycle.

* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Batch-read-feature-values-sample](#batch-read-feature-values-sample)
* [Cancel-batch-prediction-job](#cancel-batch-prediction-job)
* [Cancel-custom-job](#cancel-custom-job)
* [Create-batch-prediction-job-text-classification](#create-batch-prediction-job-text-classification)
Expand Down Expand Up @@ -52,6 +53,8 @@ machine learning development lifecycle.
* [Delete-model](#delete-model)
* [Deploy-model-custom-trained-model](#deploy-model-custom-trained-model)
* [Deploy-model](#deploy-model)
* [Export-feature-values-sample](#export-feature-values-sample)
* [Export-feature-values-snapshot-sample](#export-feature-values-snapshot-sample)
* [Export-model-tabular-classification](#export-model-tabular-classification)
* [Export-model](#export-model)
* [Get-batch-prediction-job](#get-batch-prediction-job)
Expand Down Expand Up @@ -80,6 +83,7 @@ machine learning development lifecycle.
* [Import-data-video-classification](#import-data-video-classification)
* [Import-data-video-object-tracking](#import-data-video-object-tracking)
* [Import-data](#import-data)
* [Import-feature-values-sample](#import-feature-values-sample)
* [List-endpoints](#list-endpoints)
* [List-model-evaluation-slices](#list-model-evaluation-slices)
* [Predict-custom-trained-model](#predict-custom-trained-model)
Expand All @@ -91,6 +95,7 @@ machine learning development lifecycle.
* [Predict-text-entity-extraction](#predict-text-entity-extraction)
* [Predict-text-sentiment-analysis](#predict-text-sentiment-analysis)
* [Quickstart](#quickstart)
* [Read-feature-values-sample](#read-feature-values-sample)
* [Undeploy-model](#undeploy-model)
* [Upload-model](#upload-model)

Expand All @@ -109,6 +114,23 @@ Before running the samples, make sure you've followed the steps outlined in



### Batch-read-feature-values-sample

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/batch-read-feature-values-sample.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/batch-read-feature-values-sample.js,samples/README.md)

__Usage:__


`node samples/batch-read-feature-values-sample.js`


-----




### Cancel-batch-prediction-job

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/cancel-batch-prediction-job.js).
Expand Down Expand Up @@ -738,6 +760,40 @@ __Usage:__



### Export-feature-values-sample

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-feature-values-sample.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-feature-values-sample.js,samples/README.md)

__Usage:__


`node samples/export-feature-values-sample.js`


-----




### Export-feature-values-snapshot-sample

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-feature-values-snapshot-sample.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/export-feature-values-snapshot-sample.js,samples/README.md)

__Usage:__


`node samples/export-feature-values-snapshot-sample.js`


-----




### Export-model-tabular-classification

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/export-model-tabular-classification.js).
Expand Down Expand Up @@ -1214,6 +1270,23 @@ __Usage:__



### Import-feature-values-sample

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/import-feature-values-sample.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/import-feature-values-sample.js,samples/README.md)

__Usage:__


`node samples/import-feature-values-sample.js`


-----




### List-endpoints

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/list-endpoints.js).
Expand Down Expand Up @@ -1401,6 +1474,23 @@ __Usage:__



### Read-feature-values-sample

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/read-feature-values-sample.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-ai-platform&page=editor&open_in_editor=samples/read-feature-values-sample.js,samples/README.md)

__Usage:__


`node samples/read-feature-values-sample.js`


-----




### Undeploy-model

View the [source code](https://github.com/googleapis/nodejs-ai-platform/blob/main/samples/undeploy-model.js).
Expand Down
136 changes: 136 additions & 0 deletions samples/batch-read-feature-values-sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Batch reads Feature values from a Featurestore.
* See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
* the code snippet
*/

'use strict';

async function main(
project,
featurestoreId,
inputCsvFile,
destinationTableUri,
location = 'us-central1',
apiEndpoint = 'us-central1-aiplatform.googleapis.com',
timeout = 300000
) {
// [START aiplatform_batch_read_feature_values_sample]
/**
* TODO(developer): Uncomment these variables before running the sample.\
* (Not necessary if passing values as arguments)
*/

// const project = 'YOUR_PROJECT_ID';
// const featurestoreId = 'YOUR_FEATURESTORE_ID';
// const inputCsvFile = 'YOUR_INPUT_CSV_FILE_URI';
// const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI';
// const location = 'YOUR_PROJECT_LOCATION';
// const apiEndpoint = 'YOUR_API_ENDPOINT';
// const timeout = <TIMEOUT_IN_MILLI_SECONDS>;

// Imports the Google Cloud Featurestore Service Client library
const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: apiEndpoint,
};

// Instantiates a client
const featurestoreServiceClient = new FeaturestoreServiceClient(
clientOptions
);

async function batchReadFeatureValues() {
// Configure the featurestoreId resource
const featurestore = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`;
const csvReadInstances = {
gcsSource: {
uris: [inputCsvFile],
},
};

const destination = {
bigqueryDestination: {
// # Output to BigQuery table created earlier
outputUri: destinationTableUri,
},
};

const usersFeatureSelector = {
idMatcher: {
ids: [
// features, use "*" if you want to select all features within this entity type
'age',
'gender',
'liked_genres',
],
},
};

const usersEntityTypeSpec = {
// Read the 'age', 'gender' and 'liked_genres' features from the 'perm_users' entity
entityTypeId: 'perm_users',
featureSelector: usersFeatureSelector,
};

const moviesFeatureSelector = {
idMatcher: {
ids: ['*'],
},
};

const moviesEntityTypeSpec = {
// Read the all features from the 'perm_movies' entity
entityTypeId: 'perm_movies',
featureSelector: moviesFeatureSelector,
};

const entityTypeSpecs = [usersEntityTypeSpec, moviesEntityTypeSpec];

// Construct request
const request = {
featurestore: featurestore,
csvReadInstances: csvReadInstances,
destination: destination,
entityTypeSpecs: entityTypeSpecs,
};

// Batch Read Feature Values Request
const [operation] = await featurestoreServiceClient.batchReadFeatureValues(
request,
{timeout: Number(timeout)}
);
const [response] = await operation.promise();

console.log('Batch read feature values response');
console.log('Raw response:');
console.log(JSON.stringify(response, null, 2));
}
batchReadFeatureValues();
// [END aiplatform_batch_read_feature_values_sample]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});

main(...process.argv.slice(2));
Loading