diff --git a/.github/workflows/ai-platform-snippets.yaml b/.github/workflows/ai-platform-snippets.yaml new file mode 100644 index 0000000000..ef47a95a15 --- /dev/null +++ b/.github/workflows/ai-platform-snippets.yaml @@ -0,0 +1,76 @@ +name: ai-platform-snippets +on: + push: + branches: + - main + paths: + - "ai-platform/snippets/**" + pull_request: + paths: + - "ai-platform/snippets/**" + pull_request_target: + types: [labeled] + paths: + - "ai-platform/snippets/**" + schedule: + - cron: "0 0 * * 0" +jobs: + test: + if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }} + runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: "write" + pull-requests: "write" + id-token: "write" + steps: + - uses: actions/checkout@v3.1.0 + with: + ref: ${{github.event.pull_request.head.sha}} + - uses: "google-github-actions/auth@v1.0.0" + with: + workload_identity_provider: "projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider" + service_account: "kokoro-system-test@long-door-651.iam.gserviceaccount.com" + create_credentials_file: "true" + access_token_lifetime: 600s + - id: secrets + uses: "google-github-actions/get-secretmanager-secrets@v0" + with: + secrets: |- + caip_id:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id + location:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location + - uses: actions/setup-node@v3.5.1 + with: + node-version: 16 + - run: npm install + working-directory: ai-platform/snippets + - run: npm test + working-directory: ai-platform/snippets + env: + LOCATION: ${{ steps.secrets.outputs.location }} + CAIP_PROJECT_ID: ${{ steps.secrets.outputs.caip_id }} + MOCHA_REPORTER_SUITENAME: ai_platform_snippets + MOCHA_REPORTER_OUTPUT: ai_platform_snippets_sponge_log.xml + MOCHA_REPORTER: xunit + - if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }} + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + await github.rest.issues.removeLabel({ + name: 'actions:force-run', + owner: 'GoogleCloudPlatform', + repo: 'nodejs-docs-samples', + issue_number: context.payload.pull_request.number + }); + } catch (e) { + if (!e.message.includes('Label does not exist')) { + throw e; + } + } + - if: ${{ github.event_name == 'schedule'}} + run: | + curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L + chmod +x ./flakybot + ./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}} diff --git a/.github/workflows/workflows.json b/.github/workflows/workflows.json index 2a2396b673..ba3fb0282b 100644 --- a/.github/workflows/workflows.json +++ b/.github/workflows/workflows.json @@ -1,4 +1,5 @@ [ + "ai-platform/snippets", "appengine/analytics", "appengine/building-an-app/build", "appengine/building-an-app/update", diff --git a/ai-platform/snippets/batch-create-features-sample.js b/ai-platform/snippets/batch-create-features-sample.js new file mode 100644 index 0000000000..5659258134 --- /dev/null +++ b/ai-platform/snippets/batch-create-features-sample.js @@ -0,0 +1,124 @@ +/* + * 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. + */ + +/* + * Creates a batch of Features in a given EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_batch_create_features_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 batchCreateFeatures() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const ageFeature = { + valueType: 'INT64', + description: 'User age', + }; + + const ageFeatureRequest = { + feature: ageFeature, + featureId: 'age', + }; + + const genderFeature = { + valueType: 'STRING', + description: 'User gender', + }; + + const genderFeatureRequest = { + feature: genderFeature, + featureId: 'gender', + }; + + const likedGenresFeature = { + valueType: 'STRING_ARRAY', + description: 'An array of genres that this user liked', + }; + + const likedGenresFeatureRequest = { + feature: likedGenresFeature, + featureId: 'liked_genres', + }; + + const requests = [ + ageFeatureRequest, + genderFeatureRequest, + likedGenresFeatureRequest, + ]; + + const request = { + parent: parent, + requests: requests, + }; + + // Batch Create Features request + const [operation] = await featurestoreServiceClient.batchCreateFeatures( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Batch create features response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + batchCreateFeatures(); + // [END aiplatform_batch_create_features_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/batch-read-feature-values-sample.js b/ai-platform/snippets/batch-read-feature-values-sample.js new file mode 100644 index 0000000000..75a198936f --- /dev/null +++ b/ai-platform/snippets/batch-read-feature-values-sample.js @@ -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 = ; + + // 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)); diff --git a/ai-platform/snippets/cancel-batch-prediction-job.js b/ai-platform/snippets/cancel-batch-prediction-job.js new file mode 100644 index 0000000000..c9c8c8e4b7 --- /dev/null +++ b/ai-platform/snippets/cancel-batch-prediction-job.js @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_cancel_batch_prediction_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function cancelBatchPredictionJob() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Cancel batch prediction job request + await jobServiceClient.cancelBatchPredictionJob(request); + console.log('Cancel batch prediction job response :'); + } + + cancelBatchPredictionJob(); + // [END aiplatform_cancel_batch_prediction_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/cancel-custom-job.js b/ai-platform/snippets/cancel-custom-job.js new file mode 100644 index 0000000000..b7e56cafb3 --- /dev/null +++ b/ai-platform/snippets/cancel-custom-job.js @@ -0,0 +1,61 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_cancel_custom_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function cancelCustomJob() { + // Configure the name resource + const name = jobServiceClient.customJobPath(project, location, customJobId); + const request = { + name, + }; + + // Cancel custom job request + const [response] = await jobServiceClient.cancelCustomJob(request); + + console.log('Cancel custom job response:\n', response); + } + cancelCustomJob(); + // [END aiplatform_cancel_custom_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-text-classification.js b/ai-platform/snippets/create-batch-prediction-job-text-classification.js new file mode 100644 index 0000000000..f9cc5f3504 --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-text-classification.js @@ -0,0 +1,93 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_text_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobTextClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log('Create batch prediction job text classification response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobTextClassification(); + // [END aiplatform_create_batch_prediction_job_text_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-text-entity-extraction.js b/ai-platform/snippets/create-batch-prediction-job-text-entity-extraction.js new file mode 100644 index 0000000000..4497c777a9 --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-text-entity-extraction.js @@ -0,0 +1,93 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_text_entity_extraction_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobTextEntityExtraction() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log('Create batch prediction job text entity extraction response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobTextEntityExtraction(); + // [END aiplatform_create_batch_prediction_job_text_entity_extraction_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-text-sentiment-analysis.js b/ai-platform/snippets/create-batch-prediction-job-text-sentiment-analysis.js new file mode 100644 index 0000000000..db85e5996c --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-text-sentiment-analysis.js @@ -0,0 +1,93 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_text_sentiment_analysis_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobTextSentimentAnalysis() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log('Create batch prediction job text sentiment analysis response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobTextSentimentAnalysis(); + // [END aiplatform_create_batch_prediction_job_text_sentiment_analysis_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-video-action-recognition.js b/ai-platform/snippets/create-batch-prediction-job-video-action-recognition.js new file mode 100644 index 0000000000..37d5a50f00 --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-video-action-recognition.js @@ -0,0 +1,106 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_video_action_recognition_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobVideoActionRecognition() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + // For more information on how to configure the model parameters object, see + // https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions + const modelParamsObj = new params.VideoActionRecognitionPredictionParams({ + confidenceThreshold: 0.5, + }); + + const modelParameters = modelParamsObj.toValue(); + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + modelParameters, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log( + 'Create batch prediction job video action recognition response' + ); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobVideoActionRecognition(); + // [END aiplatform_create_batch_prediction_job_video_action_recognition_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-video-classification.js b/ai-platform/snippets/create-batch-prediction-job-video-classification.js new file mode 100644 index 0000000000..6208ffb89a --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-video-classification.js @@ -0,0 +1,108 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_video_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobVideoClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + // For more information on how to configure the model parameters object, see + // https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions + const modelParamsObj = new params.VideoClassificationPredictionParams({ + confidenceThreshold: 0.5, + maxPredictions: 1000, + segmentClassification: true, + shotClassification: true, + oneSecIntervalClassification: true, + }); + + const modelParameters = modelParamsObj.toValue(); + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + modelParameters, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log('Create batch prediction job video classification response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobVideoClassification(); + // [END aiplatform_create_batch_prediction_job_video_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-batch-prediction-job-video-object-tracking.js b/ai-platform/snippets/create-batch-prediction-job-video-object-tracking.js new file mode 100644 index 0000000000..0e5b898efd --- /dev/null +++ b/ai-platform/snippets/create-batch-prediction-job-video-object-tracking.js @@ -0,0 +1,104 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + batchPredictionDisplayName, + modelId, + gcsSourceUri, + gcsDestinationOutputUriPrefix, + project, + location = 'us-central1' +) { + // [START aiplatform_create_batch_prediction_job_video_object_tracking_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionDisplayName = 'YOUR_BATCH_PREDICTION_DISPLAY_NAME'; + // const modelId = 'YOUR_MODEL_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const gcsDestinationOutputUriPrefix = 'YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {params} = aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createBatchPredictionJobVideoObjectTracking() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + + // For more information on how to configure the model parameters object, see + // https://cloud.google.com/ai-platform-unified/docs/predictions/batch-predictions + const modelParamsObj = new params.VideoObjectTrackingPredictionParams({ + confidenceThreshold: 0.5, + }); + + const modelParameters = modelParamsObj.toValue(); + + const inputConfig = { + instancesFormat: 'jsonl', + gcsSource: {uris: [gcsSourceUri]}, + }; + const outputConfig = { + predictionsFormat: 'jsonl', + gcsDestination: {outputUriPrefix: gcsDestinationOutputUriPrefix}, + }; + const batchPredictionJob = { + displayName: batchPredictionDisplayName, + model: modelName, + modelParameters, + inputConfig, + outputConfig, + }; + const request = { + parent, + batchPredictionJob, + }; + + // Create batch prediction job request + const [response] = await jobServiceClient.createBatchPredictionJob(request); + + console.log('Create batch prediction job video object tracking response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createBatchPredictionJobVideoObjectTracking(); + // [END aiplatform_create_batch_prediction_job_video_object_tracking_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-custom-job.js b/ai-platform/snippets/create-custom-job.js new file mode 100644 index 0000000000..cae369024e --- /dev/null +++ b/ai-platform/snippets/create-custom-job.js @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + customJobDisplayName, + containerImageUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_custom_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const customJobDisplayName = 'YOUR_CUSTOM_JOB_DISPLAY_NAME'; + // const containerImageUri = 'YOUR_CONTAINER_IMAGE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createCustomJob() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const customJob = { + displayName: customJobDisplayName, + jobSpec: { + workerPoolSpecs: [ + { + machineSpec: { + machineType: 'n1-standard-4', + acceleratorType: 'NVIDIA_TESLA_K80', + acceleratorCount: 1, + }, + replicaCount: 1, + containerSpec: { + imageUri: containerImageUri, + command: [], + args: [], + }, + }, + ], + }, + }; + const request = {parent, customJob}; + + // Create custom job request + const [response] = await jobServiceClient.createCustomJob(request); + + console.log('Create custom job response:\n', JSON.stringify(response)); + } + createCustomJob(); + // [END aiplatform_create_custom_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-image.js b/ai-platform/snippets/create-dataset-image.js new file mode 100644 index 0000000000..0335351825 --- /dev/null +++ b/ai-platform/snippets/create-dataset-image.js @@ -0,0 +1,79 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_image_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetImage() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset image response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetImage(); + // [END aiplatform_create_dataset_image_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-tabular-bigquery.js b/ai-platform/snippets/create-dataset-tabular-bigquery.js new file mode 100644 index 0000000000..a68b3b8331 --- /dev/null +++ b/ai-platform/snippets/create-dataset-tabular-bigquery.js @@ -0,0 +1,108 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + bigquerySourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset_tabular_bigquery_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const bigquerySourceUri = 'YOUR_BIGQUERY_SOURCE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetTabularBigquery() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const metadata = { + structValue: { + fields: { + inputConfig: { + structValue: { + fields: { + bigquerySource: { + structValue: { + fields: { + uri: { + listValue: { + values: [{stringValue: bigquerySourceUri}], + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/tabular_1.0.0.yaml', + metadata: metadata, + }; + const request = { + parent, + dataset, + }; + + // Create dataset request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset tabular bigquery response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tMetadata schema uri : ${result.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(result.metadata)}`); + } + createDatasetTabularBigquery(); + // [END aiplatform_create_dataset_tabular_bigquery_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-tabular-gcs.js b/ai-platform/snippets/create-dataset-tabular-gcs.js new file mode 100644 index 0000000000..34dd8bf312 --- /dev/null +++ b/ai-platform/snippets/create-dataset-tabular-gcs.js @@ -0,0 +1,108 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset_tabular_gcs_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetTabularGcs() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const metadata = { + structValue: { + fields: { + inputConfig: { + structValue: { + fields: { + gcsSource: { + structValue: { + fields: { + uri: { + listValue: { + values: [{stringValue: gcsSourceUri}], + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/tabular_1.0.0.yaml', + metadata: metadata, + }; + const request = { + parent, + dataset, + }; + + // Create dataset request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset tabular gcs response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tMetadata schema uri : ${result.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(result.metadata)}`); + } + createDatasetTabularGcs(); + // [END aiplatform_create_dataset_tabular_gcs_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-text.js b/ai-platform/snippets/create-dataset-text.js new file mode 100644 index 0000000000..93b99be7e5 --- /dev/null +++ b/ai-platform/snippets/create-dataset-text.js @@ -0,0 +1,79 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_text_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetText() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/text_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset text response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetText(); + // [END aiplatform_create_dataset_text_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset-video.js b/ai-platform/snippets/create-dataset-video.js new file mode 100644 index 0000000000..04c6c2e74a --- /dev/null +++ b/ai-platform/snippets/create-dataset-video.js @@ -0,0 +1,79 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(datasetDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_dataset_video_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = "YOUR_DATASTE_DISPLAY_NAME"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDatasetVideo() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/video_1.0.0.yaml', + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create dataset video response'); + console.log(`Name : ${result.name}`); + console.log(`Display name : ${result.displayName}`); + console.log(`Metadata schema uri : ${result.metadataSchemaUri}`); + console.log(`Metadata : ${JSON.stringify(result.metadata)}`); + console.log(`Labels : ${JSON.stringify(result.labels)}`); + } + createDatasetVideo(); + // [END aiplatform_create_dataset_video_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-dataset.js b/ai-platform/snippets/create-dataset.js new file mode 100644 index 0000000000..bf87a34f5c --- /dev/null +++ b/ai-platform/snippets/create-dataset.js @@ -0,0 +1,89 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetDisplayName, + metadataSchemaUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_dataset_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetDisplayName = 'YOUR_DATASET_DISPLAY_NAME'; + // const metadataSchemaUri = 'YOUR_METADATA_SCHEMA_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function createDataset() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Configure the dataset resource + const dataset = { + displayName: datasetDisplayName, + metadataSchemaUri: metadataSchemaUri, + }; + const request = { + parent, + dataset, + }; + + // Create Dataset Request + const [response] = await datasetServiceClient.createDataset(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [createDatasetResponse] = await response.promise(); + + console.log('Create dataset response'); + console.log(`\tName : ${createDatasetResponse.name}`); + console.log(`\tDisplay name : ${createDatasetResponse.displayName}`); + console.log( + `\tMetadata schema uri : ${createDatasetResponse.metadataSchemaUri}` + ); + console.log( + `\tMetadata : ${JSON.stringify(createDatasetResponse.metadata)}` + ); + console.log(`\tCreate time : ${createDatasetResponse.createTime}`); + console.log(`\tUpdate time : ${createDatasetResponse.updateTime}`); + console.log(`\tLabels : ${JSON.stringify(createDatasetResponse.labels)}`); + } + createDataset(); + // [END aiplatform_create_dataset_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-endpoint.js b/ai-platform/snippets/create-endpoint.js new file mode 100644 index 0000000000..1a1abbadbd --- /dev/null +++ b/ai-platform/snippets/create-endpoint.js @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(endpointDisplayName, project, location = 'us-central1') { + // [START aiplatform_create_endpoint_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointDisplayName = 'YOUR_ENDPOINT_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function createEndpoint() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + const endpoint = { + displayName: endpointDisplayName, + }; + const request = { + parent, + endpoint, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.createEndpoint(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Create endpoint response'); + console.log(`\tName : ${result.name}`); + console.log(`\tDisplay name : ${result.displayName}`); + console.log(`\tDescription : ${result.description}`); + console.log(`\tLabels : ${JSON.stringify(result.labels)}`); + console.log(`\tCreate time : ${JSON.stringify(result.createTime)}`); + console.log(`\tUpdate time : ${JSON.stringify(result.updateTime)}`); + } + createEndpoint(); + // [END aiplatform_create_endpoint_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-entity-type-monitoring-sample.js b/ai-platform/snippets/create-entity-type-monitoring-sample.js new file mode 100644 index 0000000000..d244484751 --- /dev/null +++ b/ai-platform/snippets/create-entity-type-monitoring-sample.js @@ -0,0 +1,106 @@ +/* + * 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. + */ + +/* + * Creates a new EntityType with monitoring configuration in a given Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + description, + duration = 86400, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_create_entity_type_monitoring_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const description = 'YOUR_ENTITY_TYPE_DESCRIPTION'; + // const duration = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // Imports the Google Cloud Featurestore Service Client library + const {FeaturestoreServiceClient} = + require('@google-cloud/aiplatform').v1beta1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: apiEndpoint, + }; + + // Instantiates a client + const featurestoreServiceClient = new FeaturestoreServiceClient( + clientOptions + ); + + async function createEntityTypeMonitoring() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const entityType = { + description: description, + monitoringConfig: { + snapshotAnalysis: { + monitoringInterval: { + seconds: Number(duration), + }, + }, + }, + }; + + const request = { + parent: parent, + entityTypeId: entityTypeId, + entityType: entityType, + }; + + // Create EntityType request + const [operation] = await featurestoreServiceClient.createEntityType( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Create entity type monitoring response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createEntityTypeMonitoring(); + // [END aiplatform_create_entity_type_monitoring_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-entity-type-sample.js b/ai-platform/snippets/create-entity-type-sample.js new file mode 100644 index 0000000000..fe867db22d --- /dev/null +++ b/ai-platform/snippets/create-entity-type-sample.js @@ -0,0 +1,96 @@ +/* + * 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. + */ + +/* + * Creates a new EntityType in a given Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + description, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_create_entity_type_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const description = 'YOUR_ENTITY_TYPE_DESCRIPTION'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 createEntityType() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const entityType = { + description: description, + }; + + const request = { + parent: parent, + entityTypeId: entityTypeId, + entityType: entityType, + }; + + // Create EntityType request + const [operation] = await featurestoreServiceClient.createEntityType( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Create entity type response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createEntityType(); + // [END aiplatform_create_entity_type_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-feature-sample.js b/ai-platform/snippets/create-feature-sample.js new file mode 100644 index 0000000000..e637f89057 --- /dev/null +++ b/ai-platform/snippets/create-feature-sample.js @@ -0,0 +1,100 @@ +/* + * 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. + */ + +/* + * Creates a new Feature in a given EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + featureId, + valueType, + description, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_create_feature_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const featureId = 'YOUR_FEATURE_ID'; + // const valueType = 'FEATURE_VALUE_DATA_TYPE'; + // const description = 'YOUR_ENTITY_TYPE_DESCRIPTION'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 createFeature() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const feature = { + valueType: valueType, + description: description, + }; + + const request = { + parent: parent, + feature: feature, + featureId: featureId, + }; + + // Create Feature request + const [operation] = await featurestoreServiceClient.createFeature(request, { + timeout: Number(timeout), + }); + const [response] = await operation.promise(); + + console.log('Create feature response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createFeature(); + // [END aiplatform_create_feature_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-featurestore-fixed-nodes-sample.js b/ai-platform/snippets/create-featurestore-fixed-nodes-sample.js new file mode 100644 index 0000000000..6fcd87be62 --- /dev/null +++ b/ai-platform/snippets/create-featurestore-fixed-nodes-sample.js @@ -0,0 +1,94 @@ +/* + * 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. + */ + +/* + * Creates a new Featurestore with fixed nodes configuration in a given project and location. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + fixedNodeCount = 1, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 900000 +) { + // [START aiplatform_create_featurestore_fixed_nodes_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 fixedNodeCount = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 createFeaturestoreFixedNodes() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const featurestore = { + onlineServingConfig: {fixedNodeCount: Number(fixedNodeCount)}, + }; + + const request = { + parent: parent, + featurestore: featurestore, + featurestoreId: featurestoreId, + }; + + // Create Featurestore request + const [operation] = await featurestoreServiceClient.createFeaturestore( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Create featurestore fixed nodes response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createFeaturestoreFixedNodes(); + // [END aiplatform_create_featurestore_fixed_nodes_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-featurestore-sample.js b/ai-platform/snippets/create-featurestore-sample.js new file mode 100644 index 0000000000..a4933a544e --- /dev/null +++ b/ai-platform/snippets/create-featurestore-sample.js @@ -0,0 +1,102 @@ +/* + * 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. + */ + +/* + * Creates a new Featurestore in a given project and location. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + minNodeCount = 1, + maxNodeCount = 5, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 900000 +) { + // [START aiplatform_create_featurestore_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 minNodeCount = ; + // const maxNodeCount = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // Imports the Google Cloud Featurestore Service Client library + const {FeaturestoreServiceClient} = + require('@google-cloud/aiplatform').v1beta1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: apiEndpoint, + }; + + // Instantiates a client + const featurestoreServiceClient = new FeaturestoreServiceClient( + clientOptions + ); + + async function createFeaturestore() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const featurestore = { + onlineServingConfig: { + scaling: { + minNodeCount: minNodeCount, + maxNodeCount: maxNodeCount, + }, + }, + }; + + const request = { + parent: parent, + featurestore: featurestore, + featurestoreId: featurestoreId, + }; + + // Create Featurestore request + const [operation] = await featurestoreServiceClient.createFeaturestore( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Create featurestore response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createFeaturestore(); + // [END aiplatform_create_featurestore_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-hyperparameter-tuning-job.js b/ai-platform/snippets/create-hyperparameter-tuning-job.js new file mode 100644 index 0000000000..d6803a57e9 --- /dev/null +++ b/ai-platform/snippets/create-hyperparameter-tuning-job.js @@ -0,0 +1,113 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +function main( + displayName, + containerImageUri, + project, + location = 'us-central1' +) { + // [START aiplatform_create_hyperparameter_tuning_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample. + * (Not necessary if passing values as arguments) + */ + /* + const displayName = 'YOUR HYPERPARAMETER TUNING JOB; + const containerImageUri = 'TUNING JOB CONTAINER URI; + const project = 'YOUR PROJECT ID'; + const location = 'us-central1'; + */ + // Imports the Google Cloud Pipeline Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function createHyperParameterTuningJob() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + // Create the hyperparameter tuning job configuration + const hyperparameterTuningJob = { + displayName, + maxTrialCount: 2, + parallelTrialCount: 1, + maxFailedTrialCount: 1, + studySpec: { + metrics: [ + { + metricId: 'accuracy', + goal: 'MAXIMIZE', + }, + ], + parameters: [ + { + parameterId: 'lr', + doubleValueSpec: { + minValue: 0.001, + maxValue: 0.1, + }, + }, + ], + }, + trialJobSpec: { + workerPoolSpecs: [ + { + machineSpec: { + machineType: 'n1-standard-4', + acceleratorType: 'NVIDIA_TESLA_K80', + acceleratorCount: 1, + }, + replicaCount: 1, + containerSpec: { + imageUri: containerImageUri, + command: [], + args: [], + }, + }, + ], + }, + }; + + const [response] = await jobServiceClient.createHyperparameterTuningJob({ + parent, + hyperparameterTuningJob, + }); + + console.log('Create hyperparameter tuning job response:'); + console.log(`\tDisplay name: ${response.displayName}`); + console.log(`\tTuning job resource name: ${response.name}`); + console.log(`\tJob status: ${response.state}`); + } + + createHyperParameterTuningJob(); + // [END aiplatform_create_hyperparameter_tuning_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-image-classification.js b/ai-platform/snippets/create-training-pipeline-image-classification.js new file mode 100644 index 0000000000..bca7efde3e --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-image-classification.js @@ -0,0 +1,103 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_image_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample. + * (Not necessary if passing values as arguments) + */ + /* + const datasetId = 'YOUR DATASET'; + const modelDisplayName = 'NEW MODEL NAME; + const trainingPipelineDisplayName = 'NAME FOR TRAINING PIPELINE'; + const project = 'YOUR PROJECT ID'; + const location = 'us-central1'; + */ + // Imports the Google Cloud Pipeline Service Client library + const aiplatform = require('@google-cloud/aiplatform'); + + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + const ModelType = definition.AutoMlImageClassificationInputs.ModelType; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const {PipelineServiceClient} = aiplatform.v1; + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineImageClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + // Values should match the input expected by your model. + const trainingTaskInputsMessage = + new definition.AutoMlImageClassificationInputs({ + multiLabel: true, + modelType: ModelType.CLOUD, + budgetMilliNodeHours: 8000, + disableEarlyStopping: false, + }); + + const trainingTaskInputs = trainingTaskInputsMessage.toValue(); + + const trainingTaskDefinition = + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_classification_1.0.0.yaml'; + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition, + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = {parent, trainingPipeline}; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline image classification response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + + createTrainingPipelineImageClassification(); + // [END aiplatform_create_training_pipeline_image_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-image-object-detection.js b/ai-platform/snippets/create-training-pipeline-image-object-detection.js new file mode 100644 index 0000000000..207fa71915 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-image-object-detection.js @@ -0,0 +1,100 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_image_object_detection_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + const ModelType = definition.AutoMlImageObjectDetectionInputs.ModelType; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineImageObjectDetection() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const trainingTaskInputsObj = + new definition.AutoMlImageObjectDetectionInputs({ + disableEarlyStopping: false, + modelType: ModelType.CLOUD_HIGH_ACCURACY_1, + budgetMilliNodeHours: 20000, + }); + + const trainingTaskInputs = trainingTaskInputsObj.toValue(); + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_image_object_detection_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline image object detection response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineImageObjectDetection(); + // [END aiplatform_create_training_pipeline_image_object_detection_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-tabular-classification.js b/ai-platform/snippets/create-training-pipeline-tabular-classification.js new file mode 100644 index 0000000000..16500b61c8 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-tabular-classification.js @@ -0,0 +1,114 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + targetColumn, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_tabular_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const targetColumn = 'YOUR_TARGET_COLUMN'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineTablesClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const transformations = [ + {auto: {column_name: 'sepal_width'}}, + {auto: {column_name: 'sepal_length'}}, + {auto: {column_name: 'petal_length'}}, + {auto: {column_name: 'petal_width'}}, + ]; + const trainingTaskInputsObj = new definition.AutoMlTablesInputs({ + targetColumn: targetColumn, + predictionType: 'classification', + transformations: transformations, + trainBudgetMilliNodeHours: 8000, + disableEarlyStopping: false, + optimizationObjective: 'minimize-log-loss', + }); + const trainingTaskInputs = trainingTaskInputsObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = { + datasetId: datasetId, + fractionSplit: { + trainingFraction: 0.8, + validationFraction: 0.1, + testFraction: 0.1, + }, + }; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_tables_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline tabular classification response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineTablesClassification(); + // [END aiplatform_create_training_pipeline_tabular_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-tabular-regression.js b/ai-platform/snippets/create-training-pipeline-tabular-regression.js new file mode 100644 index 0000000000..368cc717a5 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-tabular-regression.js @@ -0,0 +1,139 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + targetColumn, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_tabular_regression_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const targetColumn = 'YOUR_TARGET_COLUMN'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineTablesRegression() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const transformations = [ + {auto: {column_name: 'STRING_5000unique_NULLABLE'}}, + {auto: {column_name: 'INTEGER_5000unique_NULLABLE'}}, + {auto: {column_name: 'FLOAT_5000unique_NULLABLE'}}, + {auto: {column_name: 'FLOAT_5000unique_REPEATED'}}, + {auto: {column_name: 'NUMERIC_5000unique_NULLABLE'}}, + {auto: {column_name: 'BOOLEAN_2unique_NULLABLE'}}, + { + timestamp: { + column_name: 'TIMESTAMP_1unique_NULLABLE', + invalid_values_allowed: true, + }, + }, + {auto: {column_name: 'DATE_1unique_NULLABLE'}}, + {auto: {column_name: 'TIME_1unique_NULLABLE'}}, + { + timestamp: { + column_name: 'DATETIME_1unique_NULLABLE', + invalid_values_allowed: true, + }, + }, + {auto: {column_name: 'STRUCT_NULLABLE.STRING_5000unique_NULLABLE'}}, + {auto: {column_name: 'STRUCT_NULLABLE.INTEGER_5000unique_NULLABLE'}}, + {auto: {column_name: 'STRUCT_NULLABLE.FLOAT_5000unique_NULLABLE'}}, + {auto: {column_name: 'STRUCT_NULLABLE.FLOAT_5000unique_REQUIRED'}}, + {auto: {column_name: 'STRUCT_NULLABLE.FLOAT_5000unique_REPEATED'}}, + {auto: {column_name: 'STRUCT_NULLABLE.NUMERIC_5000unique_NULLABLE'}}, + {auto: {column_name: 'STRUCT_NULLABLE.BOOLEAN_2unique_NULLABLE'}}, + {auto: {column_name: 'STRUCT_NULLABLE.TIMESTAMP_1unique_NULLABLE'}}, + ]; + + const trainingTaskInputsObj = new definition.AutoMlTablesInputs({ + transformations, + targetColumn, + predictionType: 'regression', + trainBudgetMilliNodeHours: 8000, + disableEarlyStopping: false, + optimizationObjective: 'minimize-rmse', + }); + const trainingTaskInputs = trainingTaskInputsObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = { + datasetId: datasetId, + fractionSplit: { + trainingFraction: 0.8, + validationFraction: 0.1, + testFraction: 0.1, + }, + }; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_tables_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline tabular regression response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineTablesRegression(); + // [END aiplatform_create_training_pipeline_tabular_regression_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-text-classification.js b/ai-platform/snippets/create-training-pipeline-text-classification.js new file mode 100644 index 0000000000..b453091fcc --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-text-classification.js @@ -0,0 +1,95 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_text_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineTextClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const trainingTaskInputObj = new definition.AutoMlTextClassificationInputs({ + multiLabel: false, + }); + const trainingTaskInputs = trainingTaskInputObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_text_classification_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline text classification response :'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineTextClassification(); + // [END aiplatform_create_training_pipeline_text_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-text-entity-extraction.js b/ai-platform/snippets/create-training-pipeline-text-entity-extraction.js new file mode 100644 index 0000000000..5c84e6cce5 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-text-entity-extraction.js @@ -0,0 +1,93 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_text_entity_extraction_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineTextEntityExtraction() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const trainingTaskInputObj = new definition.AutoMlTextExtractionInputs({}); + const trainingTaskInputs = trainingTaskInputObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_text_extraction_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline text entity extraction response :'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineTextEntityExtraction(); + // [END aiplatform_create_training_pipeline_text_entity_extraction_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-text-sentiment-analysis.js b/ai-platform/snippets/create-training-pipeline-text-sentiment-analysis.js new file mode 100644 index 0000000000..a2f6ac54d3 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-text-sentiment-analysis.js @@ -0,0 +1,95 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_text_sentiment_analysis_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineTextSentimentAnalysis() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const trainingTaskInputObj = new definition.AutoMlTextSentimentInputs({ + sentimentMax: 4, + }); + const trainingTaskInputs = trainingTaskInputObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_text_sentiment_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline text sentiment analysis response :'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineTextSentimentAnalysis(); + // [END aiplatform_create_training_pipeline_text_sentiment_analysis_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-video-action-recognition.js b/ai-platform/snippets/create-training-pipeline-video-action-recognition.js new file mode 100644 index 0000000000..d53f4aa160 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-video-action-recognition.js @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_video_action_recognition_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineVideoActionRecognition() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Values should match the input expected by your model. + const trainingTaskInputObj = + new definition.AutoMlVideoActionRecognitionInputs({ + // modelType can be either 'CLOUD' or 'MOBILE_VERSATILE_1' + modelType: 'CLOUD', + }); + const trainingTaskInputs = trainingTaskInputObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_action_recognition_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline video action recognition response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineVideoActionRecognition(); + // [END aiplatform_create_training_pipeline_video_action_recognition_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-video-classification.js b/ai-platform/snippets/create-training-pipeline-video-classification.js new file mode 100644 index 0000000000..8c3bf97703 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-video-classification.js @@ -0,0 +1,95 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_video_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineVideoClassification() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + // Values should match the input expected by your model. + const trainingTaskInputObj = new definition.AutoMlVideoClassificationInputs( + {} + ); + const trainingTaskInputs = trainingTaskInputObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_classification_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline video classification response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineVideoClassification(); + // [END aiplatform_create_training_pipeline_video_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/create-training-pipeline-video-object-tracking.js b/ai-platform/snippets/create-training-pipeline-video-object-tracking.js new file mode 100644 index 0000000000..5a8cef8893 --- /dev/null +++ b/ai-platform/snippets/create-training-pipeline-video-object-tracking.js @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + modelDisplayName, + trainingPipelineDisplayName, + project, + location = 'us-central1' +) { + // [START aiplatform_create_training_pipeline_video_object_tracking_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const trainingPipelineDisplayName = 'YOUR_TRAINING_PIPELINE_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {definition} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.trainingjob; + const ModelType = definition.AutoMlVideoObjectTrackingInputs.ModelType; + + // Imports the Google Cloud Pipeline Service Client library + const {PipelineServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function createTrainingPipelineVideoObjectTracking() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const trainingTaskInputsObj = + new definition.AutoMlVideoObjectTrackingInputs({ + modelType: ModelType.CLOUD, + }); + const trainingTaskInputs = trainingTaskInputsObj.toValue(); + + const modelToUpload = {displayName: modelDisplayName}; + const inputDataConfig = {datasetId: datasetId}; + const trainingPipeline = { + displayName: trainingPipelineDisplayName, + trainingTaskDefinition: + 'gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_object_tracking_1.0.0.yaml', + trainingTaskInputs, + inputDataConfig, + modelToUpload, + }; + const request = { + parent, + trainingPipeline, + }; + + // Create training pipeline request + const [response] = await pipelineServiceClient.createTrainingPipeline( + request + ); + + console.log('Create training pipeline video object tracking response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + createTrainingPipelineVideoObjectTracking(); + // [END aiplatform_create_training_pipeline_video_object_tracking_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-batch-prediction-job.js b/ai-platform/snippets/delete-batch-prediction-job.js new file mode 100644 index 0000000000..1616485c72 --- /dev/null +++ b/ai-platform/snippets/delete-batch-prediction-job.js @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_delete_batch_prediction_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function deleteBatchPredictionJob() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Get and print out a list of all the endpoints for this resource + await jobServiceClient.deleteBatchPredictionJob(request); + + console.log('Delete batch prediction job response :'); + } + deleteBatchPredictionJob(); + // [END aiplatform_delete_batch_prediction_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-custom-job.js b/ai-platform/snippets/delete-custom-job.js new file mode 100644 index 0000000000..c3a0cf6eef --- /dev/null +++ b/ai-platform/snippets/delete-custom-job.js @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_delete_custom_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function deleteCustomJob() { + // Configure the name resource + const name = jobServiceClient.customJobPath(project, location, customJobId); + const request = { + name, + }; + + // Delete custom job request + const [response] = await jobServiceClient.deleteCustomJob(request); + + console.log('Delete custom job response:\n', response); + } + setTimeout(deleteCustomJob, 60000); + // [END aiplatform_delete_custom_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-dataset.js b/ai-platform/snippets/delete-dataset.js new file mode 100644 index 0000000000..6b6da42e57 --- /dev/null +++ b/ai-platform/snippets/delete-dataset.js @@ -0,0 +1,65 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(datasetId, project, location = 'us-central1') { + // [START aiplatform_delete_dataset_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function deleteDataset() { + // Configure the resource + const name = datasetServiceClient.datasetPath(project, location, datasetId); + const request = {name}; + + // Delete Dataset Request + const [response] = await datasetServiceClient.deleteDataset(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete dataset response:\n', result); + } + deleteDataset(); + // [END aiplatform_delete_dataset_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-endpoint.js b/ai-platform/snippets/delete-endpoint.js new file mode 100644 index 0000000000..db5d531bb2 --- /dev/null +++ b/ai-platform/snippets/delete-endpoint.js @@ -0,0 +1,66 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(endpointId, project, location = 'us-central1') { + // [START aiplatform_delete_endpoint_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function deleteEndpoint() { + // Configure the parent resource + const endpoint = { + name: `projects/${project}/locations/${location}/endpoints/${endpointId}`, + }; + + // Delete endpoint request + const [response] = await endpointServiceClient.deleteEndpoint(endpoint); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete endpoint response:\n', result); + } + deleteEndpoint(); + // [END aiplatform_delete_endpoint_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-entity-type-sample.js b/ai-platform/snippets/delete-entity-type-sample.js new file mode 100644 index 0000000000..84f490a0f8 --- /dev/null +++ b/ai-platform/snippets/delete-entity-type-sample.js @@ -0,0 +1,90 @@ +/* + * 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. + */ + +/* + * Deletes a single EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + force, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_delete_entity_type_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const force = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 deleteEntityType() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const request = { + name: name, + force: Boolean(force), + }; + + // Delete EntityType request + const [operation] = await featurestoreServiceClient.deleteEntityType( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Delete entity type response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + deleteEntityType(); + // [END aiplatform_delete_entity_type_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-export-model.js b/ai-platform/snippets/delete-export-model.js new file mode 100644 index 0000000000..8ecb80fdda --- /dev/null +++ b/ai-platform/snippets/delete-export-model.js @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(bucketName, uriPrefix) { + // [START aiplatform_delete_export_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const bucketName = 'YOUR_BUCKET_NAME'; + // const uriPrefix = 'YOUR_GCS_URI_PREFIX' + + // Imports the Google Cloud Storage Client library + const {Storage} = require('@google-cloud/storage'); + + // Instantiates a client + const storageClient = new Storage(); + + async function deleteExportModel() { + const options = { + prefix: uriPrefix, + }; + const [files] = await storageClient + .bucket(`gs://${bucketName}`) + .getFiles(options); + for (const file of files) { + await storageClient.bucket(`gs://${bucketName}`).file(file.name).delete(); + } + console.log('Export model deleted'); + } + deleteExportModel(); + // [END aiplatform_delete_export_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-feature-sample.js b/ai-platform/snippets/delete-feature-sample.js new file mode 100644 index 0000000000..c509de9798 --- /dev/null +++ b/ai-platform/snippets/delete-feature-sample.js @@ -0,0 +1,88 @@ +/* + * 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. + */ + +/* + * Deletes a single Feature. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + featureId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_delete_feature_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const featureId = 'YOUR_FEATURE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 deleteFeature() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}/features/${featureId}`; + + const request = { + name: name, + }; + + // Delete Feature request + const [operation] = await featurestoreServiceClient.deleteFeature(request, { + timeout: Number(timeout), + }); + const [response] = await operation.promise(); + + console.log('Delete feature response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + deleteFeature(); + // [END aiplatform_delete_feature_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-featurestore-sample.js b/ai-platform/snippets/delete-featurestore-sample.js new file mode 100644 index 0000000000..e7220eba12 --- /dev/null +++ b/ai-platform/snippets/delete-featurestore-sample.js @@ -0,0 +1,88 @@ +/* + * 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. + */ + +/* + * Deletes a single Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + force = false, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 60000 +) { + // [START aiplatform_delete_featurestore_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 force = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 deleteFeaturestore() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: Boolean(force), + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Delete featurestore response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + deleteFeaturestore(); + // [END aiplatform_delete_featurestore_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/delete-model.js b/ai-platform/snippets/delete-model.js new file mode 100644 index 0000000000..a00b2b7743 --- /dev/null +++ b/ai-platform/snippets/delete-model.js @@ -0,0 +1,64 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, project, location = 'us-central1') { + // [START aiplatform_delete_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function deleteModel() { + // Configure the resource + const name = modelServiceClient.modelPath(project, location, modelId); + const request = {name}; + + // Delete Model Request + const [response] = await modelServiceClient.deleteModel(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Delete model response:\n', result); + } + deleteModel(); + // [END aiplatform_delete_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/deploy-model-custom-trained-model.js b/ai-platform/snippets/deploy-model-custom-trained-model.js new file mode 100644 index 0000000000..40956cf494 --- /dev/null +++ b/ai-platform/snippets/deploy-model-custom-trained-model.js @@ -0,0 +1,100 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +function main( + modelId, + deployedModelDisplayName, + endpointId, + project, + location = 'us-central1' +) { + // [START aiplatform_deploy_model_custom_trained_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = "YOUR_MODEL_ID"; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const deployedModelDisplayName = 'YOUR_DEPLOYED_MODEL_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint: + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function deployModelCustomTrainedModel() { + // Configure the parent resource + // key '0' assigns traffic for the newly deployed model + // Traffic percentage values must add up to 100 + // Leave dictionary empty if endpoint should not accept any traffic + const trafficSplit = {0: 100}; + const deployedModel = { + // format: 'projects/{project}/locations/{location}/models/{model}' + model: modelName, + displayName: deployedModelDisplayName, + // `dedicatedResources` must be used for non-AutoML models + dedicatedResources: { + minReplicaCount: 1, + machineSpec: { + machineType: 'n1-standard-2', + // Accelerators can be used only if the model specifies a GPU image. + // acceleratorType: 'NVIDIA_TESLA_K80', + // acceleratorCount: 1, + }, + }, + }; + const request = { + endpoint, + deployedModel, + trafficSplit, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.deployModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Deploy model response'); + const modelDeployed = result.deployedModel; + console.log(`\t\tId : ${modelDeployed.id}`); + console.log(modelDeployed); + } + deployModelCustomTrainedModel(); + // [END aiplatform_deploy_model_custom_trained_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/deploy-model.js b/ai-platform/snippets/deploy-model.js new file mode 100644 index 0000000000..aa0c2d6671 --- /dev/null +++ b/ai-platform/snippets/deploy-model.js @@ -0,0 +1,154 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + modelId, + deployedModelDisplayName, + endpointId, + project, + location = 'us-central1' +) { + // [START aiplatform_deploy_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = "YOUR_MODEL_ID"; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const deployedModelDisplayName = 'YOUR_DEPLOYED_MODEL_DISPLAY_NAME'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const modelName = `projects/${project}/locations/${location}/models/${modelId}`; + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint: + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function deployModel() { + // Configure the parent resource + // key '0' assigns traffic for the newly deployed model + // Traffic percentage values must add up to 100 + // Leave dictionary empty if endpoint should not accept any traffic + const trafficSplit = {0: 100}; + const deployedModel = { + // format: 'projects/{project}/locations/{location}/models/{model}' + model: modelName, + displayName: deployedModelDisplayName, + // AutoML Vision models require `automatic_resources` field + // Other model types may require `dedicated_resources` field instead + automaticResources: {minReplicaCount: 1, maxReplicaCount: 1}, + }; + const request = { + endpoint, + deployedModel, + trafficSplit, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.deployModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Deploy model response'); + const modelDeployed = result.deployedModel; + console.log('\tDeployed model'); + if (!modelDeployed) { + console.log('\t\tId : {}'); + console.log('\t\tModel : {}'); + console.log('\t\tDisplay name : {}'); + console.log('\t\tCreate time : {}'); + + console.log('\t\tDedicated resources'); + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMachine spec {}'); + console.log('\t\t\t\tMachine type : {}'); + console.log('\t\t\t\tAccelerator type : {}'); + console.log('\t\t\t\tAccelerator count : {}'); + + console.log('\t\tAutomatic resources'); + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMax replica count : {}'); + } else { + console.log(`\t\tId : ${modelDeployed.id}`); + console.log(`\t\tModel : ${modelDeployed.model}`); + console.log(`\t\tDisplay name : ${modelDeployed.displayName}`); + console.log(`\t\tCreate time : ${modelDeployed.createTime}`); + + const dedicatedResources = modelDeployed.dedicatedResources; + console.log('\t\tDedicated resources'); + if (!dedicatedResources) { + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMachine spec {}'); + console.log('\t\t\t\tMachine type : {}'); + console.log('\t\t\t\tAccelerator type : {}'); + console.log('\t\t\t\tAccelerator count : {}'); + } else { + console.log( + `\t\t\tMin replica count : \ + ${dedicatedResources.minReplicaCount}` + ); + const machineSpec = dedicatedResources.machineSpec; + console.log('\t\t\tMachine spec'); + console.log(`\t\t\t\tMachine type : ${machineSpec.machineType}`); + console.log( + `\t\t\t\tAccelerator type : ${machineSpec.acceleratorType}` + ); + console.log( + `\t\t\t\tAccelerator count : ${machineSpec.acceleratorCount}` + ); + } + + const automaticResources = modelDeployed.automaticResources; + console.log('\t\tAutomatic resources'); + if (!automaticResources) { + console.log('\t\t\tMin replica count : {}'); + console.log('\t\t\tMax replica count : {}'); + } else { + console.log( + `\t\t\tMin replica count : \ + ${automaticResources.minReplicaCount}` + ); + console.log( + `\t\t\tMax replica count : \ + ${automaticResources.maxReplicaCount}` + ); + } + } + } + deployModel(); + // [END aiplatform_deploy_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js new file mode 100644 index 0000000000..c3381e16ef --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job-image.test.js @@ -0,0 +1,69 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_image_test_${uuid()}`; +const datasetId = '1905673553261363200'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const annotationSpec = 'roses'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job image', () => { + it('should create a new data labeling job image', async () => { + const stdout = execSync( + `node ./create-data-labeling-job-image.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job image response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js new file mode 100644 index 0000000000..33800c8ea1 --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job-video.test.js @@ -0,0 +1,70 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_video_test_${uuid()}`; +const datasetId = '3459133949727473664'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const annotationSpec = 'cartwheel'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job video', () => { + it('should create a new data labeling job video', async () => { + const stdout = execSync( + `node ./create-data-labeling-job-video.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job video response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js b/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js new file mode 100644 index 0000000000..342c54a554 --- /dev/null +++ b/ai-platform/snippets/expensive-test/create-data-labeling-job.test.js @@ -0,0 +1,73 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_data_labeling_job_test_${uuid()}`; +const datasetId = '8268327440875520000'; +const instructionUri = + 'gs://ucaip-sample-resources/images/datalabeling_instructions.pdf'; +const inputsSchemaUri = + 'gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/image_classification.yaml'; +const annotationSpec = 'daisy'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let dataLabelingJobId; + +describe('AI platform create data labeling job', () => { + it('should create a new data labeling job', async () => { + const stdout = execSync( + `node ./create-data-labeling-job.js ${displayName} ${datasetId} \ + ${instructionUri} \ + ${inputsSchemaUri} \ + ${annotationSpec} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create data labeling job response/); + dataLabelingJobId = stdout + .split('/locations/us-central1/dataLabelingJobs/')[1] + .split('\n')[0]; + }); + after('should cancel the data labeling job and delete it', async () => { + execSync( + `node ./cancel-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-data-labeling-job.js ${dataLabelingJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/expensive-test/get-training-pipeline.test.js b/ai-platform/snippets/expensive-test/get-training-pipeline.test.js new file mode 100644 index 0000000000..1f553f8438 --- /dev/null +++ b/ai-platform/snippets/expensive-test/get-training-pipeline.test.js @@ -0,0 +1,42 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const trainingPipelineId = '1419759782528548864'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get training pipeline', () => { + it('should get the training pipeline', async () => { + const stdout = execSync( + `node ./get-training-pipeline.js ${trainingPipelineId} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get training pipeline response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js b/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js new file mode 100644 index 0000000000..bb6ca67862 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-text-entity-extraction.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '6203215905493614592'; +const gcsSourceUri = + 'gs://cloud-ml-data/NL-entity/AIPlatform-unified/entity_extraction_dataset.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data text entity extraction', () => { + it('should import data to text entity extraction dataset', async () => { + const stdout = execSync( + `node ./import-data-text-entity-extraction.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data text entity extraction response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js b/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js new file mode 100644 index 0000000000..8449d211f1 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-text-sentiment-analysis.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '5148529167758786560'; +const gcsSourceUri = + 'gs://cloud-ml-data/NL-sentiment/crowdflower-twitter-claritin-80-10-10.csv'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data text sentiment analysis', () => { + it('should import data text sentiment analysis to dataset', async () => { + const stdout = execSync( + `node ./import-data-text-sentiment-analysis.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data text sentiment analysis response/); + }); +}); diff --git a/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js b/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js new file mode 100644 index 0000000000..592390e924 --- /dev/null +++ b/ai-platform/snippets/expensive-test/import-data-video-object-tracking.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '1138566280794603520'; +const gcsSourceUri = + 'gs://ucaip-sample-resources/youtube_8m_videos_animal_full.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data video object tracking', () => { + it('should import video object tracking data to dataset', async () => { + const stdout = execSync( + `node ./import-data-video-object-tracking.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data video object tracking response/); + }); +}); diff --git a/ai-platform/snippets/export-feature-values-sample.js b/ai-platform/snippets/export-feature-values-sample.js new file mode 100644 index 0000000000..04d9c34c09 --- /dev/null +++ b/ai-platform/snippets/export-feature-values-sample.js @@ -0,0 +1,105 @@ +/* + * 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. + */ + +/* + * Exports Feature values from all the entities of a target EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + destinationTableUri, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_export_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 exportFeatureValues() { + // Configure the entityType resource + const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const destination = { + bigqueryDestination: { + // # Output to BigQuery table created earlier + outputUri: destinationTableUri, + }, + }; + + const featureSelector = { + idMatcher: { + ids: ['age', 'gender', 'liked_genres'], + }, + }; + + const request = { + entityType: entityType, + destination: destination, + featureSelector: featureSelector, + fullExport: {}, + }; + + // Export Feature Values Request + const [operation] = await featurestoreServiceClient.exportFeatureValues( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Export feature values response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + exportFeatureValues(); + // [END aiplatform_export_feature_values_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/export-feature-values-snapshot-sample.js b/ai-platform/snippets/export-feature-values-snapshot-sample.js new file mode 100644 index 0000000000..f527bfb10e --- /dev/null +++ b/ai-platform/snippets/export-feature-values-snapshot-sample.js @@ -0,0 +1,113 @@ +/* + * 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. + */ + +/* + * Exports Feature values with snapshot from all the entities of a target EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + destinationTableUri, + timestamp, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_export_feature_values_snapshot_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const destinationTableUri = 'YOUR_BQ_DESTINATION_TABLE_URI'; + // const timestamp = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 exportFeatureValuesSnapshot() { + // Configure the entityType resource + const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const destination = { + bigqueryDestination: { + // # Output to BigQuery table created earlier + outputUri: destinationTableUri, + }, + }; + + const featureSelector = { + idMatcher: { + ids: ['age', 'gender', 'liked_genres'], + }, + }; + + const snapshotExport = { + startTime: { + seconds: Number(timestamp), + }, + }; + + const request = { + entityType: entityType, + destination: destination, + featureSelector: featureSelector, + snapshotExport: snapshotExport, + }; + + // Export Feature Values Request + const [operation] = await featurestoreServiceClient.exportFeatureValues( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Export feature values snapshot response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + exportFeatureValuesSnapshot(); + // [END aiplatform_export_feature_values_snapshot_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/export-model-tabular-classification.js b/ai-platform/snippets/export-model-tabular-classification.js new file mode 100644 index 0000000000..bbdabab538 --- /dev/null +++ b/ai-platform/snippets/export-model-tabular-classification.js @@ -0,0 +1,80 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + gcsDestinationOutputUriPrefix, + modelId, + project, + location = 'us-central1' +) { + // [START aiplatform_export_model_tabular_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const gcsDestinationOutputUriPrefix ='YOUR_GCS_DESTINATION_\ + // OUTPUT_URI_PREFIX'; eg. "gs:///destination_path" + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function exportModelTabularClassification() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}`; + // Configure the outputConfig resources + const outputConfig = { + exportFormatId: 'tf-saved-model', + artifactDestination: { + outputUriPrefix: gcsDestinationOutputUriPrefix, + }, + }; + const request = { + name, + outputConfig, + }; + + // Export Model request + const [response] = await modelServiceClient.exportModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + console.log(`Export model response : ${JSON.stringify(response.result)}`); + } + exportModelTabularClassification(); + // [END aiplatform_export_model_tabular_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/export-model.js b/ai-platform/snippets/export-model.js new file mode 100644 index 0000000000..a7fb1fbdad --- /dev/null +++ b/ai-platform/snippets/export-model.js @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + modelId, + gcsDestinationOutputUriPrefix, + exportFormat, + project, + location = 'us-central1' +) { + // [START aiplatform_export_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const gcsDestinationOutputUriPrefix ='YOUR_GCS_DEST_OUTPUT_URI_PREFIX'; + // eg. "gs:///destination_path" + // const exportFormat = 'YOUR_EXPORT_FORMAT'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function exportModel() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}`; + // Configure the outputConfig resources + const outputConfig = { + exportFormatId: exportFormat, + gcsDestination: { + outputUriPrefix: gcsDestinationOutputUriPrefix, + }, + }; + const request = { + name, + outputConfig, + }; + + // Export Model request + const [response] = await modelServiceClient.exportModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log(`Export model response : ${JSON.stringify(result)}`); + } + exportModel(); + // [END aiplatform_export_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-batch-prediction-job.js b/ai-platform/snippets/get-batch-prediction-job.js new file mode 100644 index 0000000000..a263b9e139 --- /dev/null +++ b/ai-platform/snippets/get-batch-prediction-job.js @@ -0,0 +1,151 @@ +/* + * Copyright 2020 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. + */ +'use strict'; + +async function main(batchPredictionJobId, project, location = 'us-central1') { + // [START aiplatform_get_batch_prediction_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const batchPredictionJobId = 'YOUR_BATCH_PREDICTION_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function getBatchPredictionJob() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/batchPredictionJobs/${batchPredictionJobId}`; + const request = { + name, + }; + + // Get batch prediction request + const [response] = await jobServiceClient.getBatchPredictionJob(request); + + console.log('Get batch prediction job response'); + console.log(`\tName : ${response.name}`); + console.log(`\tDisplayName : ${response.displayName}`); + console.log(`\tModel : ${response.model}`); + console.log(`\tModel parameters : ${response.modelParameters}`); + console.log(`\tGenerate explanation : ${response.generateExplanation}`); + console.log(`\tState : ${response.state}`); + console.log(`\tCreate Time : ${JSON.stringify(response.createTime)}`); + console.log(`\tStart Time : ${JSON.stringify(response.startTime)}`); + console.log(`\tEnd Time : ${JSON.stringify(response.endTime)}`); + console.log(`\tUpdate Time : ${JSON.stringify(response.updateTime)}`); + console.log(`\tLabels : ${JSON.stringify(response.labels)}`); + + const inputConfig = response.inputConfig; + console.log('\tInput config'); + console.log(`\t\tInstances format : ${inputConfig.instancesFormat}`); + + const gcsSource = inputConfig.gcsSource; + console.log('\t\tGcs source'); + console.log(`\t\t\tUris : ${gcsSource.uris}`); + + const bigquerySource = inputConfig.bigquerySource; + console.log('\t\tBigQuery Source'); + if (!bigquerySource) { + console.log('\t\t\tInput Uri : {}'); + } else { + console.log(`\t\t\tInput Uri : ${bigquerySource.inputUri}`); + } + + const outputConfig = response.outputConfig; + console.log('\t\tOutput config'); + console.log(`\t\tPredictions format : ${outputConfig.predictionsFormat}`); + + const gcsDestination = outputConfig.gcsDestination; + console.log('\t\tGcs Destination'); + console.log(`\t\t\tOutput uri prefix : ${gcsDestination.outputUriPrefix}`); + + const bigqueryDestination = outputConfig.bigqueryDestination; + if (!bigqueryDestination) { + console.log('\t\tBigquery Destination'); + console.log('\t\t\tOutput uri : {}'); + } else { + console.log('\t\tBigquery Destination'); + console.log(`\t\t\tOutput uri : ${bigqueryDestination.outputUri}`); + } + + const outputInfo = response.outputInfo; + if (!outputInfo) { + console.log('\tOutput info'); + console.log('\t\tGcs output directory : {}'); + console.log('\t\tBigquery_output_dataset : {}'); + } else { + console.log('\tOutput info'); + console.log( + `\t\tGcs output directory : ${outputInfo.gcsOutputDirectory}` + ); + console.log(`\t\tBigquery_output_dataset : + ${outputInfo.bigqueryOutputDataset}`); + } + + const error = response.error; + console.log('\tError'); + console.log(`\t\tCode : ${error.code}`); + console.log(`\t\tMessage : ${error.message}`); + + const details = error.details; + console.log(`\t\tDetails : ${details}`); + + const partialFailures = response.partialFailures; + console.log('\tPartial failure'); + console.log(partialFailures); + + const resourcesConsumed = response.resourcesConsumed; + console.log('\tResource consumed'); + if (!resourcesConsumed) { + console.log('\t\tReplica Hours: {}'); + } else { + console.log(`\t\tReplica Hours: ${resourcesConsumed.replicaHours}`); + } + + const completionStats = response.completionStats; + console.log('\tCompletion status'); + if (!completionStats) { + console.log('\t\tSuccessful count: {}'); + console.log('\t\tFailed count: {}'); + console.log('\t\tIncomplete count: {}'); + } else { + console.log(`\t\tSuccessful count: ${completionStats.successfulCount}`); + console.log(`\t\tFailed count: ${completionStats.failedCount}`); + console.log(`\t\tIncomplete count: ${completionStats.incompleteCount}`); + } + } + getBatchPredictionJob(); + // [END aiplatform_get_batch_prediction_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-custom-job.js b/ai-platform/snippets/get-custom-job.js new file mode 100644 index 0000000000..7589a725af --- /dev/null +++ b/ai-platform/snippets/get-custom-job.js @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(customJobId, project, location = 'us-central1') { + // [START aiplatform_get_custom_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const customJobId = 'YOUR_CUSTOM_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Job Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function getCustomJob() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/customJobs/${customJobId}`; + const request = { + name, + }; + + // Get custom job request + const [response] = await jobServiceClient.getCustomJob(request); + + console.log('Get custom job response'); + console.log(`\t${JSON.stringify(response)}`); + } + getCustomJob(); + // [END aiplatform_get_custom_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-entity-type-sample.js b/ai-platform/snippets/get-entity-type-sample.js new file mode 100644 index 0000000000..7123bb34e5 --- /dev/null +++ b/ai-platform/snippets/get-entity-type-sample.js @@ -0,0 +1,86 @@ +/* + * 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. + */ + +/* + * Gets details of a single EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_get_entity_type_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 getEntityType() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const request = { + name: name, + }; + + // Get EntityType request + const [response] = await featurestoreServiceClient.getEntityType(request, { + timeout: Number(timeout), + }); + + console.log('Get entity type response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + getEntityType(); + // [END aiplatform_get_entity_type_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-feature-sample.js b/ai-platform/snippets/get-feature-sample.js new file mode 100644 index 0000000000..e89fcdc355 --- /dev/null +++ b/ai-platform/snippets/get-feature-sample.js @@ -0,0 +1,88 @@ +/* + * 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. + */ + +/* + * Gets details of a single Feature. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + featureId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_get_feature_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const featureId = 'YOUR_FEATURE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 getFeature() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}/features/${featureId}`; + + const request = { + name: name, + }; + + // Get Feature request + const [response] = await featurestoreServiceClient.getFeature(request, { + timeout: Number(timeout), + }); + + console.log('Get feature response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + getFeature(); + // [END aiplatform_get_feature_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-featurestore-sample.js b/ai-platform/snippets/get-featurestore-sample.js new file mode 100644 index 0000000000..fe6e38f394 --- /dev/null +++ b/ai-platform/snippets/get-featurestore-sample.js @@ -0,0 +1,85 @@ +/* + * 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. + */ + +/* + * Gets details of a single Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_get_featurestore_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 location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 getFeaturestore() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + }; + + // Get Featurestore request + const [response] = await featurestoreServiceClient.getFeaturestore( + request, + {timeout: Number(timeout)} + ); + + console.log('Get featurestore response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + getFeaturestore(); + // [END aiplatform_get_featurestore_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-hyperparameter-tuning-job.js b/ai-platform/snippets/get-hyperparameter-tuning-job.js new file mode 100644 index 0000000000..9331f0d94c --- /dev/null +++ b/ai-platform/snippets/get-hyperparameter-tuning-job.js @@ -0,0 +1,70 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main(tuningJobId, project, location = 'us-central1') { + // [START aiplatform_get_hyperparameter_tuning_job_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const tuningJobId = 'YOUR_TUNING_JOB_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {JobServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const jobServiceClient = new JobServiceClient(clientOptions); + + async function getHyperparameterTuningJob() { + // Configure the parent resource + const name = jobServiceClient.hyperparameterTuningJobPath( + project, + location, + tuningJobId + ); + const request = { + name, + }; + // Get and print out a list of all the endpoints for this resource + const [response] = await jobServiceClient.getHyperparameterTuningJob( + request + ); + + console.log('Get hyperparameter tuning job response'); + console.log(`\tDisplay name: ${response.displayName}`); + console.log(`\tTuning job resource name: ${response.name}`); + console.log(`\tJob status: ${response.state}`); + } + getHyperparameterTuningJob(); + // [END aiplatform_get_hyperparameter_tuning_job_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-image-classification.js b/ai-platform/snippets/get-model-evaluation-image-classification.js new file mode 100644 index 0000000000..0b67c1cfe3 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-image-classification.js @@ -0,0 +1,106 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_image_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationImageClassification() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation image classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + if (modelExplanation === null) { + console.log(`\tModel explanation: ${JSON.stringify(modelExplanation)}`); + } else { + const meanAttributions = modelExplanation.meanAttributions; + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + getModelEvaluationImageClassification(); + // [END aiplatform_get_model_evaluation_image_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-image-object-detection.js b/ai-platform/snippets/get-model-evaluation-image-object-detection.js new file mode 100644 index 0000000000..fc60fc7ac5 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-image-object-detection.js @@ -0,0 +1,110 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_image_object_detection_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationImageObjectDetection() { + // Configure the name resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation image object detection response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${meanAttribution.featureAttributions}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationImageObjectDetection(); + // [END aiplatform_get_model_evaluation_image_object_detection_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-slice.js b/ai-platform/snippets/get-model-evaluation-slice.js new file mode 100644 index 0000000000..e31d60c36a --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-slice.js @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + modelId, + evaluationId, + sliceId, + project, + location = 'us-central1' +) { + // [START aiplatform_get_model_evaluation_slice_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const sliceId = 'YOUR_SLICE_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + // Specifies the location of the api endpoint + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationSlice() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}/slices/${sliceId}`; + const request = { + name, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await modelServiceClient.getModelEvaluationSlice( + request + ); + + console.log('Get model evaluation slice'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics_Schema_Uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + + console.log('Slice'); + const slice = response.slice; + console.log(`\tDimension :${slice.dimension}`); + console.log(`\tValue :${slice.value}`); + } + getModelEvaluationSlice(); + // [END aiplatform_get_model_evaluation_slice_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-tabular-classification.js b/ai-platform/snippets/get-model-evaluation-tabular-classification.js new file mode 100644 index 0000000000..63f0c98c7e --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-tabular-classification.js @@ -0,0 +1,111 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_tabular_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTabularClassification() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation tabular classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (!modelExplanation) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (!meanAttributions) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTabularClassification(); + // [END aiplatform_get_model_evaluation_tabular_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-tabular-regression.js b/ai-platform/snippets/get-model-evaluation-tabular-regression.js new file mode 100644 index 0000000000..55766745a1 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-tabular-regression.js @@ -0,0 +1,111 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_tabular_regression_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTabularRegression() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation tabular regression response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (!modelExplanation) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (!meanAttributions) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTabularRegression(); + // [END aiplatform_get_model_evaluation_tabular_regression_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-classification.js b/ai-platform/snippets/get-model-evaluation-text-classification.js new file mode 100644 index 0000000000..bb28b0a21c --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-classification.js @@ -0,0 +1,109 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextClassification() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text classification response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextClassification(); + // [END aiplatform_get_model_evaluation_text_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js b/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js new file mode 100644 index 0000000000..b5182362a6 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-entity-extraction.js @@ -0,0 +1,109 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_entity_extraction_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextEntityExtraction() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text entity extraction response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextEntityExtraction(); + // [END aiplatform_get_model_evaluation_text_entity_extraction_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js b/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js new file mode 100644 index 0000000000..095651915e --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-text-sentiment-analysis.js @@ -0,0 +1,109 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_text_sentiment_analysis_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationTextSentimentAnalysis() { + // Configure the resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation text sentiment analysis response :'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + + const modelExplanation = response.modelExplanation; + console.log('\tModel explanation'); + if (modelExplanation === null) { + console.log('\t\t{}'); + } else { + const meanAttributions = modelExplanation.meanAttributions; + if (meanAttributions === null) { + console.log('\t\t\t []'); + } else { + for (const meanAttribution of meanAttributions) { + console.log('\t\tMean attribution'); + console.log( + `\t\t\tBaseline output value : \ + ${meanAttribution.baselineOutputValue}` + ); + console.log( + `\t\t\tInstance output value : \ + ${meanAttribution.instanceOutputValue}` + ); + console.log( + `\t\t\tFeature attributions : \ + ${JSON.stringify(meanAttribution.featureAttributions)}` + ); + console.log(`\t\t\tOutput index : ${meanAttribution.outputIndex}`); + console.log( + `\t\t\tOutput display name : \ + ${meanAttribution.outputDisplayName}` + ); + console.log( + `\t\t\tApproximation error : \ + ${meanAttribution.approximationError}` + ); + } + } + } + } + getModelEvaluationTextSentimentAnalysis(); + // [END aiplatform_get_model_evaluation_text_sentiment_analysis_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-video-action-recognition.js b/ai-platform/snippets/get-model-evaluation-video-action-recognition.js new file mode 100644 index 0000000000..8b67cc9fed --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-video-action-recognition.js @@ -0,0 +1,80 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_video_action_recognition_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationVideoActionRecognition() { + // Configure the parent resources + const name = modelServiceClient.modelEvaluationPath( + project, + location, + modelId, + evaluationId + ); + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation video action recognition response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluationVideoActionRecognition(); + // [END aiplatform_get_model_evaluation_video_action_recognition_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-video-classification.js b/ai-platform/snippets/get-model-evaluation-video-classification.js new file mode 100644 index 0000000000..2a98fa367e --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-video-classification.js @@ -0,0 +1,75 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_video_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationVideoClassification() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation video classification response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluationVideoClassification(); + // [END aiplatform_get_model_evaluation_video_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation-video-object-tracking.js b/ai-platform/snippets/get-model-evaluation-video-object-tracking.js new file mode 100644 index 0000000000..76198ce5a5 --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation-video-object-tracking.js @@ -0,0 +1,75 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_video_object_tracking_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluationVideoObjectTracking() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation video object tracking response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tMetrics : ${JSON.stringify(response.metrics)}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluationVideoObjectTracking(); + // [END aiplatform_get_model_evaluation_video_object_tracking_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model-evaluation.js b/ai-platform/snippets/get-model-evaluation.js new file mode 100644 index 0000000000..c8a4a8f48d --- /dev/null +++ b/ai-platform/snippets/get-model-evaluation.js @@ -0,0 +1,67 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_get_model_evaluation_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModelEvaluation() { + // Configure the parent resources + const name = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + name, + }; + + // Create get model evaluation request + const [response] = await modelServiceClient.getModelEvaluation(request); + + console.log('Get model evaluation response'); + console.log(`\tName : ${response.name}`); + console.log(`\tMetrics schema uri : ${response.metricsSchemaUri}`); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tSlice dimensions : ${response.sliceDimensions}`); + } + getModelEvaluation(); + // [END aiplatform_get_model_evaluation_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-model.js b/ai-platform/snippets/get-model.js new file mode 100644 index 0000000000..5723c2780f --- /dev/null +++ b/ai-platform/snippets/get-model.js @@ -0,0 +1,129 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, project, location = 'us-central1') { + // [START aiplatform_get_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const modelId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function getModel() { + // Configure the parent resource + const name = `projects/${project}/locations/${location}/models/${modelId}`; + const request = { + name, + }; + // Get and print out a list of all the endpoints for this resource + const [response] = await modelServiceClient.getModel(request); + + console.log('Get model response'); + console.log(`\tName : ${response.name}`); + console.log(`\tDisplayName : ${response.displayName}`); + console.log(`\tDescription : ${response.description}`); + console.log(`\tMetadata schema uri : ${response.metadataSchemaUri}`); + console.log(`\tMetadata : ${JSON.stringify(response.metadata)}`); + console.log(`\tTraining pipeline : ${response.trainingPipeline}`); + console.log(`\tArtifact uri : ${response.artifactUri}`); + console.log( + `\tSupported deployment resource types : \ + ${response.supportedDeploymentResourceTypes}` + ); + console.log( + `\tSupported input storage formats : \ + ${response.supportedInputStorageFormats}` + ); + console.log( + `\tSupported output storage formats : \ + ${response.supportedOutputStoragFormats}` + ); + console.log(`\tCreate time : ${JSON.stringify(response.createTime)}`); + console.log(`\tUpdate time : ${JSON.stringify(response.updateTime)}`); + console.log(`\tLabels : ${JSON.stringify(response.labels)}`); + + const predictSchemata = response.predictSchemata; + console.log('\tPredict schemata'); + console.log(`\tInstance schema uri : ${predictSchemata.instanceSchemaUri}`); + console.log( + `\tParameters schema uri : ${predictSchemata.prametersSchemaUri}` + ); + console.log( + `\tPrediction schema uri : ${predictSchemata.predictionSchemaUri}` + ); + + const [supportedExportFormats] = response.supportedExportFormats; + console.log('\tSupported export formats'); + console.log(`\t${supportedExportFormats}`); + + const containerSpec = response.containerSpec; + console.log('\tContainer Spec'); + if (!containerSpec) { + console.log(`\t\t${JSON.stringify(containerSpec)}`); + console.log('\t\tImage uri : {}'); + console.log('\t\tCommand : {}'); + console.log('\t\tArgs : {}'); + console.log('\t\tPredict route : {}'); + console.log('\t\tHealth route : {}'); + console.log('\t\tEnv'); + console.log('\t\t\t{}'); + console.log('\t\tPort'); + console.log('\t\t{}'); + } else { + console.log(`\t\t${JSON.stringify(containerSpec)}`); + console.log(`\t\tImage uri : ${containerSpec.imageUri}`); + console.log(`\t\tCommand : ${containerSpec.command}`); + console.log(`\t\tArgs : ${containerSpec.args}`); + console.log(`\t\tPredict route : ${containerSpec.predictRoute}`); + console.log(`\t\tHealth route : ${containerSpec.healthRoute}`); + const env = containerSpec.env; + console.log('\t\tEnv'); + console.log(`\t\t\t${JSON.stringify(env)}`); + const ports = containerSpec.ports; + console.log('\t\tPort'); + console.log(`\t\t\t${JSON.stringify(ports)}`); + } + + const [deployedModels] = response.deployedModels; + console.log('\tDeployed models'); + console.log('\t\t', deployedModels); + } + getModel(); + // [END aiplatform_get_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/get-training-pipeline.js b/ai-platform/snippets/get-training-pipeline.js new file mode 100644 index 0000000000..eca150c5eb --- /dev/null +++ b/ai-platform/snippets/get-training-pipeline.js @@ -0,0 +1,67 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +async function main(trainingPipelineId, project, location = 'us-central1') { + // [START aiplatform_get_training_pipeline_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const trainingPipelineId = 'YOUR_MODEL_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {PipelineServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const pipelineServiceClient = new PipelineServiceClient(clientOptions); + + async function getTrainingPipeline() { + // Configure the parent resource + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + const request = { + name, + }; + // Get and print out a list of all the endpoints for this resource + const [response] = await pipelineServiceClient.getTrainingPipeline(request); + + console.log('Get training pipeline response'); + console.log(`\tTraining pipeline name: ${response.displayName}`); + console.log(`\tTraining pipeline state: ${response.state}`); + } + getTrainingPipeline(); + // [END aiplatform_get_training_pipeline_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-image-classification.js b/ai-platform/snippets/import-data-image-classification.js new file mode 100644 index 0000000000..a65718500d --- /dev/null +++ b/ai-platform/snippets/import-data-image-classification.js @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_image_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataImageClassification() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/image_classification_single_label_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation: ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data image classification response : \ + ${JSON.stringify(importDataResponse)}` + ); + } + importDataImageClassification(); + // [END aiplatform_import_data_image_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-image-object-detection.js b/ai-platform/snippets/import-data-image-object-detection.js new file mode 100644 index 0000000000..fd9745603a --- /dev/null +++ b/ai-platform/snippets/import-data-image-object-detection.js @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_image_object_detection_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataImageObjectDetection() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/image_bounding_box_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log( + `Import data image object detection response : \ + ${JSON.stringify(response.result)}` + ); + } + importDataImageObjectDetection(); + // [END aiplatform_import_data_image_object_detection_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-classification-single-label.js b/ai-platform/snippets/import-data-text-classification-single-label.js new file mode 100644 index 0000000000..7114018363 --- /dev/null +++ b/ai-platform/snippets/import-data-text-classification-single-label.js @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_classification_single_label_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextClassificationSingleLabel() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_classification_single_label_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text classification single label response : \ + ${JSON.stringify(importDataResponse.result)}` + ); + } + importDataTextClassificationSingleLabel(); + // [END aiplatform_import_data_text_classification_single_label_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-entity-extraction.js b/ai-platform/snippets/import-data-text-entity-extraction.js new file mode 100644 index 0000000000..743b36468b --- /dev/null +++ b/ai-platform/snippets/import-data-text-entity-extraction.js @@ -0,0 +1,81 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_entity_extraction_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextEntityExtraction() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_extraction_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text entity extraction response : \ + ${JSON.stringify(importDataResponse.result)}` + ); + } + importDataTextEntityExtraction(); + // [END aiplatform_import_data_text_entity_extraction_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-text-sentiment-analysis.js b/ai-platform/snippets/import-data-text-sentiment-analysis.js new file mode 100644 index 0000000000..c455fdb25e --- /dev/null +++ b/ai-platform/snippets/import-data-text-sentiment-analysis.js @@ -0,0 +1,81 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_text_sentiment_analysis_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataTextSentimentAnalysis() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/text_sentiment_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Import data request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data text sentiment analysis response : \ + ${JSON.stringify(importDataResponse.result, null, 2)}` + ); + } + importDataTextSentimentAnalysis(); + // [END aiplatform_import_data_text_sentiment_analysis_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-video-action-recognition.js b/ai-platform/snippets/import-data-video-action-recognition.js new file mode 100644 index 0000000000..6c44cdc22e --- /dev/null +++ b/ai-platform/snippets/import-data-video-action-recognition.js @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_video_action_recognition_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // eg. 'gs:////[file.csv/file.jsonl]' + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataVideoActionRecognition() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/video_action_recognition_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log( + `Import data video action recognition response : \ + ${JSON.stringify(response.result)}` + ); + } + importDataVideoActionRecognition(); + // [END aiplatform_import_data_video_action_recognition_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-video-classification.js b/ai-platform/snippets/import-data-video-classification.js new file mode 100644 index 0000000000..7439bb8cce --- /dev/null +++ b/ai-platform/snippets/import-data-video-classification.js @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_video_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // eg. 'gs:////[file.csv/file.jsonl]' + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataVideoClassification() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/video_classification_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log( + `Import data video classification response : \ + ${JSON.stringify(response.result)}` + ); + } + importDataVideoClassification(); + // [END aiplatform_import_data_video_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data-video-object-tracking.js b/ai-platform/snippets/import-data-video-object-tracking.js new file mode 100644 index 0000000000..b447fa2988 --- /dev/null +++ b/ai-platform/snippets/import-data-video-object-tracking.js @@ -0,0 +1,81 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_video_object_tracking_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = 'YOUR_DATASET_ID'; + // const gcsSourceUri = 'YOUR_GCS_SOURCE_URI'; + // eg. 'gs:////[file.csv/file.jsonl]' + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importDataVideoObjectTracking() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/ioformat/video_object_tracking_io_format_1.0.0.yaml', + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation: ${JSON.stringify(response.name)}`); + + // Wait for operation to complete + const [importDataResponse] = await response.promise(); + + console.log( + `Import data video object tracking response : \ + ${JSON.stringify(importDataResponse)}` + ); + } + importDataVideoObjectTracking(); + // [END aiplatform_import_data_video_object_tracking_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-data.js b/ai-platform/snippets/import-data.js new file mode 100644 index 0000000000..4b9f46b090 --- /dev/null +++ b/ai-platform/snippets/import-data.js @@ -0,0 +1,79 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + datasetId, + gcsSourceUri, + importSchemaUri, + project, + location = 'us-central1' +) { + // [START aiplatform_import_data_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const datasetId = "YOUR_DATASET_ID"; + // const gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + // eg. "gs:////[file.csv/file.jsonl]" + // const importSchemaUri = "YOUR_IMPORT_SCHEMA_URI"; + // const project = "YOUR_PROJECT_ID"; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Dataset Service Client library + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const datasetServiceClient = new DatasetServiceClient(clientOptions); + + async function importData() { + const name = datasetServiceClient.datasetPath(project, location, datasetId); + // Here we use only one import config with one source + const importConfigs = [ + { + gcsSource: {uris: [gcsSourceUri]}, + importSchemaUri: importSchemaUri, + }, + ]; + const request = { + name, + importConfigs, + }; + + // Create Import Data Request + const [response] = await datasetServiceClient.importData(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log(`Import data response : ${JSON.stringify(response.result)}`); + } + importData(); + // [END aiplatform_import_data_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/import-feature-values-sample.js b/ai-platform/snippets/import-feature-values-sample.js new file mode 100644 index 0000000000..01a174bfcb --- /dev/null +++ b/ai-platform/snippets/import-feature-values-sample.js @@ -0,0 +1,108 @@ +/* + * 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. + */ + +/* + * Imports Feature values into the Featurestore from a source storage. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + avroGcsUri, + entityIdField, + featureTimeField, + workerCount = 2, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_import_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const avroGcsUri = 'AVRO_FILE_IN_THE_GCS_URI'; + // const entityIdField = 'ENTITY_ID_FIELD_IN_AVRO'; + // const featureTimeField = 'TIMESTAMP_FIELD_IN_AVRO'; + // const workerCount = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 importFeatureValues() { + // Configure the entityType resource + const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const avroSource = { + gcsSource: { + uris: [avroGcsUri], + }, + }; + + const featureSpecs = [{id: 'age'}, {id: 'gender'}, {id: 'liked_genres'}]; + + const request = { + entityType: entityType, + avroSource: avroSource, + entityIdField: entityIdField, + featureSpecs: featureSpecs, + featureTimeField: featureTimeField, + workerCount: Number(workerCount), + }; + + // Import Feature Values Request + const [operation] = await featurestoreServiceClient.importFeatureValues( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Import feature values response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + importFeatureValues(); + // [END aiplatform_import_feature_values_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-endpoints.js b/ai-platform/snippets/list-endpoints.js new file mode 100644 index 0000000000..01e425910c --- /dev/null +++ b/ai-platform/snippets/list-endpoints.js @@ -0,0 +1,62 @@ +/** + * Copyright 2020, 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 + * + * http://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. + */ + +'use strict'; + +async function main(projectId, location = 'us-central1') { + // [START aiplatform_list_endpoints_sample] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + const client = new EndpointServiceClient(clientOptions); + + async function listEndpoints() { + // Configure the parent resource + const parent = `projects/${projectId}/locations/${location}`; + const request = { + parent, + }; + + // Get and print out a list of all the endpoints for this resource + const [result] = await client.listEndpoints(request); + for (const endpoint of result) { + console.log(`\nEndpoint name: ${endpoint.name}`); + console.log(`Display name: ${endpoint.displayName}`); + if (endpoint.deployedModels[0]) { + console.log( + `First deployed model: ${endpoint.deployedModels[0].model}` + ); + } + } + } + listEndpoints(); + // [END aiplatform_list_endpoints_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-entity-types-async-sample.js b/ai-platform/snippets/list-entity-types-async-sample.js new file mode 100644 index 0000000000..c6b1c7401e --- /dev/null +++ b/ai-platform/snippets/list-entity-types-async-sample.js @@ -0,0 +1,86 @@ +/* + * 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. + */ + +/* + * Lists EntityTypes Asynchronously in a given Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_entity_types_async_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 location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listEntityTypesAsync() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + parent: parent, + }; + + // List EntityTypes Async request + const iterable = await featurestoreServiceClient.listEntityTypesAsync( + request, + {timeout: Number(timeout)} + ); + + console.log('List entity types async response'); + console.log('Raw response:'); + for await (const response of iterable) { + console.log(JSON.stringify(response, null, 2)); + } + } + listEntityTypesAsync(); + // [END aiplatform_list_entity_types_async_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-entity-types-sample.js b/ai-platform/snippets/list-entity-types-sample.js new file mode 100644 index 0000000000..716842a0b0 --- /dev/null +++ b/ai-platform/snippets/list-entity-types-sample.js @@ -0,0 +1,84 @@ +/* + * 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. + */ + +/* + * Lists EntityTypes in a given Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_entity_types_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 location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listEntityTypes() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + parent: parent, + }; + + // List EntityTypes request + const [response] = await featurestoreServiceClient.listEntityTypes( + request, + {timeout: Number(timeout)} + ); + + console.log('List entity types response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + listEntityTypes(); + // [END aiplatform_list_entity_types_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-entity-types-stream-sample.js b/ai-platform/snippets/list-entity-types-stream-sample.js new file mode 100644 index 0000000000..5197d79fc0 --- /dev/null +++ b/ai-platform/snippets/list-entity-types-stream-sample.js @@ -0,0 +1,94 @@ +/* + * 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. + */ + +/* + * Lists EntityTypes using streaming in a given Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_entity_types_stream_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 location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listEntityTypesStream() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + parent: parent, + }; + + // List EntityTypes stream request + const streamObject = await featurestoreServiceClient.listEntityTypesStream( + request, + {timeout: Number(timeout)} + ); + + console.log('List entity types stream response'); + console.log('Raw response:'); + streamObject.on('data', response => { + console.log(JSON.stringify(response, null, 2)); + }); + + streamObject.on('end', () => { + console.log('No more data to read'); + }); + + streamObject.on('close', () => { + console.log('Stream object listEntityTypesStream is closed'); + }); + } + listEntityTypesStream(); + // [END aiplatform_list_entity_types_stream_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-features-async-sample.js b/ai-platform/snippets/list-features-async-sample.js new file mode 100644 index 0000000000..5df6446fc2 --- /dev/null +++ b/ai-platform/snippets/list-features-async-sample.js @@ -0,0 +1,90 @@ +/* + * 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. + */ + +/* + * Lists Features Asynchronously in a given EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_list_features_async_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeaturesAsync() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const request = { + parent: parent, + }; + + // List Features async request + const iterable = await featurestoreServiceClient.listFeaturesAsync( + request, + { + timeout: Number(timeout), + } + ); + + console.log('List features async response'); + console.log('Raw response:'); + for await (const response of iterable) { + console.log(JSON.stringify(response, null, 2)); + } + } + listFeaturesAsync(); + // [END aiplatform_list_features_async_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-features-sample.js b/ai-platform/snippets/list-features-sample.js new file mode 100644 index 0000000000..da75bfdf1b --- /dev/null +++ b/ai-platform/snippets/list-features-sample.js @@ -0,0 +1,85 @@ +/* + * 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. + */ + +/* + * Lists Features in a given EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_list_features_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeatures() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const request = { + parent: parent, + }; + + // List Features request + const [response] = await featurestoreServiceClient.listFeatures(request, { + timeout: Number(timeout), + }); + + console.log('List features response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + listFeatures(); + // [END aiplatform_list_features_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-features-stream-sample.js b/ai-platform/snippets/list-features-stream-sample.js new file mode 100644 index 0000000000..c85e3f5462 --- /dev/null +++ b/ai-platform/snippets/list-features-stream-sample.js @@ -0,0 +1,98 @@ +/* + * 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. + */ + +/* + * Lists Features using streaming in a given EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_list_features_stream_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeaturesStream() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const request = { + parent: parent, + }; + + // List Features stream request + const streamObject = await featurestoreServiceClient.listFeaturesStream( + request, + { + timeout: Number(timeout), + } + ); + + console.log('List features stream response'); + console.log('Raw response:'); + streamObject.on('data', response => { + console.log(JSON.stringify(response, null, 2)); + }); + + streamObject.on('end', () => { + console.log('No more data to read'); + }); + + streamObject.on('close', () => { + console.log('Stream object listFeaturesStream is closed'); + }); + } + listFeaturesStream(); + // [END aiplatform_list_features_stream_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-featurestores-async-sample.js b/ai-platform/snippets/list-featurestores-async-sample.js new file mode 100644 index 0000000000..16bbc8f779 --- /dev/null +++ b/ai-platform/snippets/list-featurestores-async-sample.js @@ -0,0 +1,84 @@ +/* + * 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. + */ + +/* + * Lists Featurestores asynchronously in a given project and location. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_featurestores_async_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeaturestoresAsync() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const request = { + parent: parent, + }; + + // List featurestores async request + const iterable = await featurestoreServiceClient.listFeaturestoresAsync( + request, + {timeout: Number(timeout)} + ); + + console.log('List featurestores async response'); + console.log('Raw response:'); + for await (const response of iterable) { + console.log(JSON.stringify(response, null, 2)); + } + } + listFeaturestoresAsync(); + // [END aiplatform_list_featurestores_async_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-featurestores-sample.js b/ai-platform/snippets/list-featurestores-sample.js new file mode 100644 index 0000000000..1bacda4376 --- /dev/null +++ b/ai-platform/snippets/list-featurestores-sample.js @@ -0,0 +1,82 @@ +/* + * 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. + */ + +/* + * Lists Featurestores in a given project and location. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_featurestores_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeaturestores() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const request = { + parent: parent, + }; + + // List featurestores request + const [response] = await featurestoreServiceClient.listFeaturestores( + request, + {timeout: Number(timeout)} + ); + + console.log('List featurestores response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + listFeaturestores(); + // [END aiplatform_list_featurestores_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-featurestores-stream-sample.js b/ai-platform/snippets/list-featurestores-stream-sample.js new file mode 100644 index 0000000000..f8515464a6 --- /dev/null +++ b/ai-platform/snippets/list-featurestores-stream-sample.js @@ -0,0 +1,93 @@ +/* + * 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. + */ + +/* + * Lists Featurestores using stream in a given project and location. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 5000 +) { + // [START aiplatform_list_featurestores_stream_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 listFeaturestoresStream() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const request = { + parent: parent, + }; + + // List featurestores stream request + const streamObject = + await featurestoreServiceClient.listFeaturestoresStream(request, { + timeout: Number(timeout), + }); + + console.log('List featurestores stream response'); + console.log('Raw response:'); + + streamObject.on('data', response => { + console.log(JSON.stringify(response, null, 2)); + }); + + streamObject.on('end', () => { + console.log('No more data to read'); + }); + + streamObject.on('close', () => { + console.log('Stream object listFeaturestoresStream is closed'); + }); + } + listFeaturestoresStream(); + // [END aiplatform_list_featurestores_stream_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/list-model-evaluation-slices.js b/ai-platform/snippets/list-model-evaluation-slices.js new file mode 100644 index 0000000000..e59bf19162 --- /dev/null +++ b/ai-platform/snippets/list-model-evaluation-slices.js @@ -0,0 +1,72 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(modelId, evaluationId, project, location = 'us-central1') { + // [START aiplatform_list_model_evaluation_slices_sample] + /** + * TODO(developer): Uncomment these variables before running the sample + * (not necessary if passing values as arguments). To obtain evaluationId, + * instantiate the client and run the following the commands. + */ + // const parentName = `projects/${project}/locations/${location}/models/${modelId}`; + // const evalRequest = { + // parent: parentName + // }; + // const [evalResponse] = await modelServiceClient.listModelEvaluations(evalRequest); + // console.log(evalResponse); + + // const modelId = 'YOUR_MODEL_ID'; + // const evaluationId = 'YOUR_EVALUATION_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function listModelEvaluationSlices() { + // Configure the parent resources + const parent = `projects/${project}/locations/${location}/models/${modelId}/evaluations/${evaluationId}`; + const request = { + parent, + }; + + // Get and print out a list of all the evaluation slices for this resource + const [response] = await modelServiceClient.listModelEvaluationSlices( + request + ); + console.log('List model evaluation response', response); + console.log(response); + } + listModelEvaluationSlices(); + // [END aiplatform_list_model_evaluation_slices_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/package.json b/ai-platform/snippets/package.json new file mode 100644 index 0000000000..0ec292b270 --- /dev/null +++ b/ai-platform/snippets/package.json @@ -0,0 +1,25 @@ +{ + "name": "nodejs-aiplatform-samples", + "private": true, + "license": "Apache-2.0", + "author": "Google LLC", + "engines": { + "node": ">=12.0.0" + }, + "files": [ + "*.js" + ], + "scripts": { + "test": "mocha --timeout 1200000 test/*.js" + }, + "dependencies": { + "@google-cloud/aiplatform": "^2.3.0", + "@google-cloud/storage": "^5.5.0", + "@google-cloud/bigquery": "^6.0.0" + }, + "devDependencies": { + "chai": "^4.2.0", + "mocha": "^8.0.0", + "uuid": "^9.0.0" + } +} diff --git a/ai-platform/snippets/predict-custom-trained-model.js b/ai-platform/snippets/predict-custom-trained-model.js new file mode 100644 index 0000000000..24f8ccbc5c --- /dev/null +++ b/ai-platform/snippets/predict-custom-trained-model.js @@ -0,0 +1,105 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(filename, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_custom_trained_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const filename = "YOUR_PREDICTION_FILE_NAME"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const util = require('util'); + const {readFile} = require('fs'); + const readFileAsync = util.promisify(readFile); + + // Imports the Google Cloud Prediction Service Client library + const {PredictionServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictCustomTrainedModel() { + // Configure the parent resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + const parameters = { + structValue: { + fields: {}, + }, + }; + const instanceDict = await readFileAsync(filename, 'utf8'); + const instanceValue = JSON.parse(instanceDict); + const instance = { + structValue: { + fields: { + Age: {stringValue: instanceValue['Age']}, + Balance: {stringValue: instanceValue['Balance']}, + Campaign: {stringValue: instanceValue['Campaign']}, + Contact: {stringValue: instanceValue['Contact']}, + Day: {stringValue: instanceValue['Day']}, + Default: {stringValue: instanceValue['Default']}, + Deposit: {stringValue: instanceValue['Deposit']}, + Duration: {stringValue: instanceValue['Duration']}, + Housing: {stringValue: instanceValue['Housing']}, + Job: {stringValue: instanceValue['Job']}, + Loan: {stringValue: instanceValue['Loan']}, + MaritalStatus: {stringValue: instanceValue['MaritalStatus']}, + Month: {stringValue: instanceValue['Month']}, + PDays: {stringValue: instanceValue['PDays']}, + POutcome: {stringValue: instanceValue['POutcome']}, + Previous: {stringValue: instanceValue['Previous']}, + }, + }, + }; + + const instances = [instance]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict custom trained model response'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + const predictions = response.predictions; + console.log('\tPredictions :'); + for (const prediction of predictions) { + console.log(`\t\tPrediction : ${JSON.stringify(prediction)}`); + } + } + predictCustomTrainedModel(); + // [END aiplatform_predict_custom_trained_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-image-classification.js b/ai-platform/snippets/predict-image-classification.js new file mode 100644 index 0000000000..6be6685757 --- /dev/null +++ b/ai-platform/snippets/predict-image-classification.js @@ -0,0 +1,95 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +function main(filename, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_image_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const filename = "YOUR_PREDICTION_FILE_NAME"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {instance, params, prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Prediction Service Client library + const {PredictionServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictImageClassification() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + + const parametersObj = new params.ImageClassificationPredictionParams({ + confidenceThreshold: 0.5, + maxPredictions: 5, + }); + const parameters = parametersObj.toValue(); + + const fs = require('fs'); + const image = fs.readFileSync(filename, 'base64'); + const instanceObj = new instance.ImageClassificationPredictionInstance({ + content: image, + }); + const instanceValue = instanceObj.toValue(); + + const instances = [instanceValue]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict image classification response'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + const predictions = response.predictions; + console.log('\tPredictions :'); + for (const predictionValue of predictions) { + const predictionResultObj = + prediction.ClassificationPredictionResult.fromValue(predictionValue); + for (const [i, label] of predictionResultObj.displayNames.entries()) { + console.log(`\tDisplay name: ${label}`); + console.log(`\tConfidences: ${predictionResultObj.confidences[i]}`); + console.log(`\tIDs: ${predictionResultObj.ids[i]}\n\n`); + } + } + } + predictImageClassification(); + // [END aiplatform_predict_image_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-image-object-detection.js b/ai-platform/snippets/predict-image-object-detection.js new file mode 100644 index 0000000000..ed6fdd5d94 --- /dev/null +++ b/ai-platform/snippets/predict-image-object-detection.js @@ -0,0 +1,98 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(filename, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_image_object_detection_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const filename = "YOUR_PREDICTION_FILE_NAME"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {instance, params, prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Prediction Service Client library + const {PredictionServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictImageObjectDetection() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + + const parametersObj = new params.ImageObjectDetectionPredictionParams({ + confidenceThreshold: 0.5, + maxPredictions: 5, + }); + const parameters = parametersObj.toValue(); + + const fs = require('fs'); + const image = fs.readFileSync(filename, 'base64'); + const instanceObj = new instance.ImageObjectDetectionPredictionInstance({ + content: image, + }); + + const instanceVal = instanceObj.toValue(); + const instances = [instanceVal]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict image object detection response'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + const predictions = response.predictions; + console.log('Predictions :'); + for (const predictionResultVal of predictions) { + const predictionResultObj = + prediction.ImageObjectDetectionPredictionResult.fromValue( + predictionResultVal + ); + for (const [i, label] of predictionResultObj.displayNames.entries()) { + console.log(`\tDisplay name: ${label}`); + console.log(`\tConfidences: ${predictionResultObj.confidences[i]}`); + console.log(`\tIDs: ${predictionResultObj.ids[i]}`); + console.log(`\tBounding boxes: ${predictionResultObj.bboxes[i]}\n\n`); + } + } + } + predictImageObjectDetection(); + // [END aiplatform_predict_image_object_detection_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-tabular-classification.js b/ai-platform/snippets/predict-tabular-classification.js new file mode 100644 index 0000000000..c5fd9efb18 --- /dev/null +++ b/ai-platform/snippets/predict-tabular-classification.js @@ -0,0 +1,93 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_tabular_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Prediction service client + const {PredictionServiceClient} = aiplatform.v1; + + // Import the helper module for converting arbitrary protobuf.Value objects. + const {helpers} = aiplatform; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictTablesClassification() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + const parameters = helpers.toValue({}); + + const instance = helpers.toValue({ + petal_length: '1.4', + petal_width: '1.3', + sepal_length: '5.1', + sepal_width: '2.8', + }); + + const instances = [instance]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict tabular classification response'); + console.log(`\tDeployed model id : ${response.deployedModelId}\n`); + const predictions = response.predictions; + console.log('Predictions :'); + for (const predictionResultVal of predictions) { + const predictionResultObj = + prediction.TabularClassificationPredictionResult.fromValue( + predictionResultVal + ); + for (const [i, class_] of predictionResultObj.classes.entries()) { + console.log(`\tClass: ${class_}`); + console.log(`\tScore: ${predictionResultObj.scores[i]}\n\n`); + } + } + } + predictTablesClassification(); + // [END aiplatform_predict_tabular_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-tabular-regression.js b/ai-platform/snippets/predict-tabular-regression.js new file mode 100644 index 0000000000..5ef754f5d5 --- /dev/null +++ b/ai-platform/snippets/predict-tabular-regression.js @@ -0,0 +1,112 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_tabular_regression_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Prediction service client + const {PredictionServiceClient} = aiplatform.v1; + + // Import the helper module for converting arbitrary protobuf.Value objects. + const {helpers} = aiplatform; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictTablesRegression() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + const parameters = helpers.toValue({}); + + // TODO (erschmid): Make this less painful + const instance = helpers.toValue({ + BOOLEAN_2unique_NULLABLE: false, + DATETIME_1unique_NULLABLE: '2019-01-01 00:00:00', + DATE_1unique_NULLABLE: '2019-01-01', + FLOAT_5000unique_NULLABLE: 1611, + FLOAT_5000unique_REPEATED: [2320, 1192], + INTEGER_5000unique_NULLABLE: '8', + NUMERIC_5000unique_NULLABLE: 16, + STRING_5000unique_NULLABLE: 'str-2', + STRUCT_NULLABLE: { + BOOLEAN_2unique_NULLABLE: false, + DATE_1unique_NULLABLE: '2019-01-01', + DATETIME_1unique_NULLABLE: '2019-01-01 00:00:00', + FLOAT_5000unique_NULLABLE: 1308, + FLOAT_5000unique_REPEATED: [2323, 1178], + FLOAT_5000unique_REQUIRED: 3089, + INTEGER_5000unique_NULLABLE: '1777', + NUMERIC_5000unique_NULLABLE: 3323, + TIME_1unique_NULLABLE: '23:59:59.999999', + STRING_5000unique_NULLABLE: 'str-49', + TIMESTAMP_1unique_NULLABLE: '1546387199999999', + }, + TIMESTAMP_1unique_NULLABLE: '1546387199999999', + TIME_1unique_NULLABLE: '23:59:59.999999', + }); + + const instances = [instance]; + const request = { + endpoint, + instances, + parameters, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict tabular regression response'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + const predictions = response.predictions; + console.log('\tPredictions :'); + for (const predictionResultVal of predictions) { + const predictionResultObj = + prediction.TabularRegressionPredictionResult.fromValue( + predictionResultVal + ); + console.log(`\tUpper bound: ${predictionResultObj.upper_bound}`); + console.log(`\tLower bound: ${predictionResultObj.lower_bound}`); + console.log(`\tLower bound: ${predictionResultObj.value}`); + } + } + predictTablesRegression(); + // [END aiplatform_predict_tabular_regression_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-text-classification.js b/ai-platform/snippets/predict-text-classification.js new file mode 100644 index 0000000000..22433ee068 --- /dev/null +++ b/ai-platform/snippets/predict-text-classification.js @@ -0,0 +1,89 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(text, endpointId, project, location) { + // [START aiplatform_predict_text_classification_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const text = 'YOUR_PREDICTION_TEXT'; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {instance, prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Model Service Client library + const {PredictionServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictTextClassification() { + // Configure the resources + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + + const predictionInstance = + new instance.TextClassificationPredictionInstance({ + content: text, + }); + const instanceValue = predictionInstance.toValue(); + + const instances = [instanceValue]; + const request = { + endpoint, + instances, + }; + + const [response] = await predictionServiceClient.predict(request); + console.log('Predict text classification response'); + console.log(`\tDeployed model id : ${response.deployedModelId}\n\n`); + + console.log('Prediction results:'); + + for (const predictionResultValue of response.predictions) { + const predictionResult = + prediction.ClassificationPredictionResult.fromValue( + predictionResultValue + ); + + for (const [i, label] of predictionResult.displayNames.entries()) { + console.log(`\tDisplay name: ${label}`); + console.log(`\tConfidences: ${predictionResult.confidences[i]}`); + console.log(`\tIDs: ${predictionResult.ids[i]}\n\n`); + } + } + } + predictTextClassification(); + // [END aiplatform_predict_text_classification_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-text-entity-extraction.js b/ai-platform/snippets/predict-text-entity-extraction.js new file mode 100644 index 0000000000..4a6f318a30 --- /dev/null +++ b/ai-platform/snippets/predict-text-entity-extraction.js @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(text, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_text_entity_extraction_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const text = "YOUR_PREDICTION_TEXT"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {instance, prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Model Service Client library + const {PredictionServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictTextEntityExtraction() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + + const instanceObj = new instance.TextExtractionPredictionInstance({ + content: text, + }); + const instanceVal = instanceObj.toValue(); + const instances = [instanceVal]; + + const request = { + endpoint, + instances, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict text entity extraction response :'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + + console.log('\nPredictions :'); + for (const predictionResultValue of response.predictions) { + const predictionResult = + prediction.TextExtractionPredictionResult.fromValue( + predictionResultValue + ); + + for (const [i, label] of predictionResult.displayNames.entries()) { + const textStartOffset = parseInt( + predictionResult.textSegmentStartOffsets[i] + ); + const textEndOffset = parseInt( + predictionResult.textSegmentEndOffsets[i] + ); + const entity = text.substring(textStartOffset, textEndOffset); + console.log(`\tEntity: ${entity}`); + console.log(`\tEntity type: ${label}`); + console.log(`\tConfidences: ${predictionResult.confidences[i]}`); + console.log(`\tIDs: ${predictionResult.ids[i]}\n\n`); + } + } + } + predictTextEntityExtraction(); + // [END aiplatform_predict_text_entity_extraction_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/predict-text-sentiment-analysis.js b/ai-platform/snippets/predict-text-sentiment-analysis.js new file mode 100644 index 0000000000..9bbbe3cecc --- /dev/null +++ b/ai-platform/snippets/predict-text-sentiment-analysis.js @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main(text, endpointId, project, location = 'us-central1') { + // [START aiplatform_predict_text_sentiment_analysis_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const text = "YOUR_PREDICTION_TEXT"; + // const endpointId = "YOUR_ENDPOINT_ID"; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + const aiplatform = require('@google-cloud/aiplatform'); + const {instance, prediction} = + aiplatform.protos.google.cloud.aiplatform.v1.schema.predict; + + // Imports the Google Cloud Model Service Client library + const {PredictionServiceClient} = aiplatform.v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const predictionServiceClient = new PredictionServiceClient(clientOptions); + + async function predictTextSentimentAnalysis() { + // Configure the endpoint resource + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + + const instanceObj = new instance.TextSentimentPredictionInstance({ + content: text, + }); + const instanceVal = instanceObj.toValue(); + + const instances = [instanceVal]; + const request = { + endpoint, + instances, + }; + + // Predict request + const [response] = await predictionServiceClient.predict(request); + + console.log('Predict text sentiment analysis response:'); + console.log(`\tDeployed model id : ${response.deployedModelId}`); + + console.log('\nPredictions :'); + for (const predictionResultValue of response.predictions) { + const predictionResult = + prediction.TextSentimentPredictionResult.fromValue( + predictionResultValue + ); + console.log(`\tSentiment measure: ${predictionResult.sentiment}`); + } + } + predictTextSentimentAnalysis(); + // [END aiplatform_predict_text_sentiment_analysis_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/quickstart.js b/ai-platform/snippets/quickstart.js new file mode 100644 index 0000000000..4f59d857ec --- /dev/null +++ b/ai-platform/snippets/quickstart.js @@ -0,0 +1,36 @@ +// Copyright 2019 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 +// +// http://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. + +'use strict'; + +/** + * TODO: add an actual quickstart example. + */ +async function main() { + // [START aiplatform_quickstart_sample] + const {DatasetServiceClient} = require('@google-cloud/aiplatform'); + const client = new DatasetServiceClient(); + + // Do something with DatasetServiceClient. + console.info(client); + + // [END aiplatform_quickstart_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/read-feature-values-sample.js b/ai-platform/snippets/read-feature-values-sample.js new file mode 100644 index 0000000000..df4f5a379f --- /dev/null +++ b/ai-platform/snippets/read-feature-values-sample.js @@ -0,0 +1,96 @@ +/* + * 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. + */ + +/* + * Reads Feature values of a specific entity of an EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + entityId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const entityId = 'ENTITY_ID_TO_SERVE'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // Imports the Google Cloud Featurestore Service Client library + const {FeaturestoreOnlineServingServiceClient} = + require('@google-cloud/aiplatform').v1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: apiEndpoint, + }; + + // Instantiates a client + const featurestoreOnlineServingServiceClient = + new FeaturestoreOnlineServingServiceClient(clientOptions); + + async function readFeatureValues() { + // Configure the entityType resource + const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + const featureSelector = { + idMatcher: { + ids: ['age', 'gender', 'liked_genres'], + }, + }; + + const request = { + entityType: entityType, + entityId: entityId, + featureSelector: featureSelector, + }; + + // Read Feature Values Request + const [response] = + await featurestoreOnlineServingServiceClient.readFeatureValues(request, { + timeout: Number(timeout), + }); + + console.log('Read feature values response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + readFeatureValues(); + // [END aiplatform_read_feature_values_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/resources/caprese_salad.jpg b/ai-platform/snippets/resources/caprese_salad.jpg new file mode 100644 index 0000000000..7957ca94d9 Binary files /dev/null and b/ai-platform/snippets/resources/caprese_salad.jpg differ diff --git a/ai-platform/snippets/resources/daisy.jpg b/ai-platform/snippets/resources/daisy.jpg new file mode 100644 index 0000000000..ae01cae918 Binary files /dev/null and b/ai-platform/snippets/resources/daisy.jpg differ diff --git a/ai-platform/snippets/search-features-async-sample.js b/ai-platform/snippets/search-features-async-sample.js new file mode 100644 index 0000000000..c2a74fb14f --- /dev/null +++ b/ai-platform/snippets/search-features-async-sample.js @@ -0,0 +1,88 @@ +/* + * 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. + */ + +/* + * Searches Features matching a query in a given project Asyncronously. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + query, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_search_features_async_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 searchFeaturesAsync() { + // Configure the locationResource resource + const locationResource = `projects/${project}/locations/${location}`; + + const request = { + location: locationResource, + query: query, + }; + + // Search Features async request + const iterable = await featurestoreServiceClient.searchFeaturesAsync( + request, + { + timeout: Number(timeout), + } + ); + + console.log('Search features async response'); + console.log('Raw response:'); + for await (const response of iterable) { + console.log(JSON.stringify(response, null, 2)); + } + } + searchFeaturesAsync(); + // [END aiplatform_search_features_async_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/search-features-sample.js b/ai-platform/snippets/search-features-sample.js new file mode 100644 index 0000000000..56a1dff477 --- /dev/null +++ b/ai-platform/snippets/search-features-sample.js @@ -0,0 +1,83 @@ +/* + * 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. + */ + +/* + * Searches Features matching a query in a given project. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + query, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_search_features_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 searchFeatures() { + // Configure the locationResource resource + const locationResource = `projects/${project}/locations/${location}`; + + const request = { + location: locationResource, + query: query, + }; + + // Search Features request + const [response] = await featurestoreServiceClient.searchFeatures(request, { + timeout: Number(timeout), + }); + + console.log('Search features response'); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + searchFeatures(); + // [END aiplatform_search_features_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/search-features-stream-sample.js b/ai-platform/snippets/search-features-stream-sample.js new file mode 100644 index 0000000000..798dadfcb4 --- /dev/null +++ b/ai-platform/snippets/search-features-stream-sample.js @@ -0,0 +1,96 @@ +/* + * 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. + */ + +/* + * Searches Features matching a query in a given project using streaming. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + query, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_search_features_stream_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 searchFeaturesStream() { + // Configure the locationResource resource + const locationResource = `projects/${project}/locations/${location}`; + + const request = { + location: locationResource, + query: query, + }; + + // Search Features stream request + const streamObject = await featurestoreServiceClient.searchFeaturesStream( + request, + { + timeout: Number(timeout), + } + ); + + console.log('Search features stream response'); + console.log('Raw response:'); + streamObject.on('data', response => { + console.log(JSON.stringify(response, null, 2)); + }); + + streamObject.on('end', () => { + console.log('No more data to read'); + }); + + streamObject.on('close', () => { + console.log('Stream object searchFeaturesStream is closed'); + }); + } + searchFeaturesStream(); + // [END aiplatform_search_features_stream_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-text-classification.test.js b/ai-platform/snippets/test/create-batch-prediction-job-text-classification.test.js new file mode 100644 index 0000000000..ac285ad4c4 --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-text-classification.test.js @@ -0,0 +1,74 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_text_classification_test${uuid()}`; +const modelId = '7827432074230366208'; +const gcsSourceUri = + 'gs://ucaip-samples-test-output/inputs/batch_predict_TCN/tcn_inputs.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job text classification', () => { + it('should create a text classification batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-text-classification.js ${batchPredictionDisplayName} ${modelId} ${gcsSourceUri} ${gcsDestinationOutputUriPrefix} ${project} ${location}` + ); + assert.match( + stdout, + /Create batch prediction job text classification response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-text-entity-extraction.test.js b/ai-platform/snippets/test/create-batch-prediction-job-text-entity-extraction.test.js new file mode 100644 index 0000000000..bb630905bd --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-text-entity-extraction.test.js @@ -0,0 +1,74 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_text_entity_extraction_test${uuid()}`; +const modelId = '6305215400179138560'; +const gcsSourceUri = + 'gs://ucaip-samples-test-output/inputs/batch_predict_TEN/ten_inputs.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job text entity extraction', () => { + it('should create a text entity extraction batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-text-entity-extraction.js ${batchPredictionDisplayName} ${modelId} ${gcsSourceUri} ${gcsDestinationOutputUriPrefix} ${project} ${location}` + ); + assert.match( + stdout, + /Create batch prediction job text entity extraction response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-text-sentiment-analysis.test.js b/ai-platform/snippets/test/create-batch-prediction-job-text-sentiment-analysis.test.js new file mode 100644 index 0000000000..c9d1832615 --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-text-sentiment-analysis.test.js @@ -0,0 +1,74 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_text_sentiment_analysis_test${uuid()}`; +const modelId = '4792568875336073216'; +const gcsSourceUri = + 'gs://ucaip-samples-test-output/inputs/batch_predict_TSN/tsn_inputs.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job text sentiment analysis', () => { + it('should create a text sentiment analysis batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-text-sentiment-analysis.js ${batchPredictionDisplayName} ${modelId} ${gcsSourceUri} ${gcsDestinationOutputUriPrefix} ${project} ${location}` + ); + assert.match( + stdout, + /Create batch prediction job text sentiment analysis response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-video-action-recognition.test.js b/ai-platform/snippets/test/create-batch-prediction-job-video-action-recognition.test.js new file mode 100644 index 0000000000..db44780785 --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-video-action-recognition.test.js @@ -0,0 +1,77 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_video_action_recognition_test${uuid()}`; +const modelId = '3530998029718913024'; +const gcsSourceUri = 'gs://automl-video-demo-data/ucaip-var/swimrun_bp.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job video action recognition', () => { + it('should create a video action recognition batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-video-action-recognition.js \ + ${batchPredictionDisplayName} \ + ${modelId} ${gcsSourceUri} \ + ${gcsDestinationOutputUriPrefix} \ + ${project} ${location}` + ); + assert.match( + stdout, + /Create batch prediction job video action recognition response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-video-classification.test.js b/ai-platform/snippets/test/create-batch-prediction-job-video-classification.test.js new file mode 100644 index 0000000000..16f8ceaf24 --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-video-classification.test.js @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_video_classification_test${uuid()}`; +const modelId = '8596984660557299712'; +const gcsSourceUri = + 'gs://ucaip-samples-test-output/inputs/vcn_40_batch_prediction_input.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job video classification', () => { + it('should create a video classification batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-video-classification.js \ + ${batchPredictionDisplayName} \ + ${modelId} ${gcsSourceUri} \ + ${gcsDestinationOutputUriPrefix} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create batch prediction job video classification response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-batch-prediction-job-video-object-tracking.test.js b/ai-platform/snippets/test/create-batch-prediction-job-video-object-tracking.test.js new file mode 100644 index 0000000000..5c3a18887e --- /dev/null +++ b/ai-platform/snippets/test/create-batch-prediction-job-video-object-tracking.test.js @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const batchPredictionDisplayName = `temp_create_batch_prediction_video_object_tracking_test${uuid()}`; +const modelId = '8609932509485989888'; +const gcsSourceUri = + 'gs://ucaip-samples-test-output/inputs/vot_batch_prediction_input.jsonl'; +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output/'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let batchPredictionJobId; + +describe('AI platform create batch prediction job video object tracking', () => { + it('should create a video object tracking batch prediction job', async () => { + const stdout = execSync( + `node ./create-batch-prediction-job-video-object-tracking.js \ + ${batchPredictionDisplayName} \ + ${modelId} ${gcsSourceUri} \ + ${gcsDestinationOutputUriPrefix} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create batch prediction job video object tracking response/ + ); + batchPredictionJobId = stdout + .split('/locations/us-central1/batchPredictionJobs/')[1] + .split('\n')[0]; + }); + after('should cancel delete the batch prediction job', async () => { + const name = jobServiceClient.batchPredictionJobPath( + project, + location, + batchPredictionJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelBatchPredictionJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteBatchPredictionJob(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-custom-job.test.js b/ai-platform/snippets/test/create-custom-job.test.js new file mode 100644 index 0000000000..ac87d2e9bd --- /dev/null +++ b/ai-platform/snippets/test/create-custom-job.test.js @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const customJobDisplayName = `temp_create_custom_job_test${uuid()}`; +const containerImageUri = + 'gcr.io/ucaip-sample-tests/ucaip-training-test:latest'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +function parseResponse(stdout) { + let res = {}; + for (let i = 0; i < stdout.length; i++) { + if (stdout[i] === '{') { + res = JSON.parse(stdout.substr(i)); + break; + } + } + return res; +} + +let customJobId; + +describe('AI platform create custom job', async function () { + this.retries(2); + it('should create a new custom job', async () => { + const stdout = execSync( + `node ./create-custom-job.js ${customJobDisplayName} \ + ${containerImageUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create custom job response/); + customJobId = parseResponse(stdout).name.split('/').pop(); + }); + + after('should cancel the customJob and delete it', async () => { + execSync( + `node ./cancel-custom-job.js ${customJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + execSync( + `node ./delete-custom-job.js ${customJobId} ${project} \ + ${location}`, + { + cwd, + } + ); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-image.test.js b/ai-platform/snippets/test/create-dataset-image.test.js new file mode 100644 index 0000000000..bd9ea481b7 --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-image.test.js @@ -0,0 +1,54 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_image_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset image', () => { + it('should create a new image dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-image.js ${datasetDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset image response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js b/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js new file mode 100644 index 0000000000..829da9dd43 --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-tabular-bigquery.test.js @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_tables_bigquery_test_${uuid()}`; +const bigquerySourceUri = 'bq://ucaip-sample-tests.table_test.all_bq_types'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset tabular bigquery', () => { + it('should create a new bigquery tabular dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-tabular-bigquery.js ${datasetDisplayName} \ + ${bigquerySourceUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset tabular bigquery response/); + datasetId = stdout + .split(`/locations/${location}/datasets/`)[1] + .split('/')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js b/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js new file mode 100644 index 0000000000..08ab73a8c2 --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-tabular-gcs.test.js @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_tables_gcs_test_${uuid()}`; +const gcsSourceUri = 'gs://cloud-ml-tables-data/bank-marketing.csv'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset tabular gcs', () => { + it('should create a new gcs tabular dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-tabular-gcs.js ${datasetDisplayName} \ + ${gcsSourceUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset tabular gcs response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('/')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-text.test.js b/ai-platform/snippets/test/create-dataset-text.test.js new file mode 100644 index 0000000000..43e84656cc --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-text.test.js @@ -0,0 +1,53 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const displayName = `temp_create_dataset_text_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset text', () => { + it('should create a new dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-text.js ${displayName} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset text response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset-video.test.js b/ai-platform/snippets/test/create-dataset-video.test.js new file mode 100644 index 0000000000..cae62a2aff --- /dev/null +++ b/ai-platform/snippets/test/create-dataset-video.test.js @@ -0,0 +1,54 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_video_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset video', () => { + it('should create a new video dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset-video.js ${datasetDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset video response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete the created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-dataset.test.js b/ai-platform/snippets/test/create-dataset.test.js new file mode 100644 index 0000000000..99075b54db --- /dev/null +++ b/ai-platform/snippets/test/create-dataset.test.js @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetDisplayName = `temp_create_dataset_test_${uuid()}`; +const metadataSchemaUri = + 'gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let datasetId; + +describe('AI platform create dataset', () => { + it('should create a new dataset in the parent resource', async () => { + const stdout = execSync( + `node ./create-dataset.js ${datasetDisplayName} ${metadataSchemaUri} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create dataset response/); + datasetId = stdout + .split('/locations/us-central1/datasets/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('should delete created dataset', async () => { + execSync(`node ./delete-dataset.js ${datasetId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-endpoint.test.js b/ai-platform/snippets/test/create-endpoint.test.js new file mode 100644 index 0000000000..b989d96a8a --- /dev/null +++ b/ai-platform/snippets/test/create-endpoint.test.js @@ -0,0 +1,53 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const endpointDisplayName = `temp_create_endpoint_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; +let endpointId; + +describe('AI platform create endpoint', () => { + it('should create a new endpoint', async () => { + const stdout = execSync( + `node ./create-endpoint.js ${endpointDisplayName} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Create endpoint response/); + endpointId = stdout + .split('/locations/us-central1/endpoints/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('delete created endpoint', async () => { + execSync(`node ./delete-endpoint.js ${endpointId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/test/create-featurestore-fixed-nodes-sample.test.js b/ai-platform/snippets/test/create-featurestore-fixed-nodes-sample.test.js new file mode 100644 index 0000000000..566a62fd17 --- /dev/null +++ b/ai-platform/snippets/test/create-featurestore-fixed-nodes-sample.test.js @@ -0,0 +1,67 @@ +/* + * 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1; +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const featurestoreId = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const fixedNodeCount = 1; +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +// Instantiates a featurestore clients +const featurestoreServiceClient = new FeaturestoreServiceClient({ + apiEndpoint: apiEndpoint, +}); + +const deleteFeaturestore = async () => { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: true, + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: 60000} + ); + await operation.promise(); +}; + +describe('AI platform create featurestore with fixed nodes', async function () { + this.retries(2); + it('should create a featurestore', async () => { + const stdout = execSync( + `node ./create-featurestore-fixed-nodes-sample.js ${project} ${featurestoreId} ${fixedNodeCount} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Create featurestore fixed nodes response/); + }); + after('should delete the created featurestore', async () => { + await deleteFeaturestore(); + }); +}); diff --git a/ai-platform/snippets/test/create-featurestore-sample.test.js b/ai-platform/snippets/test/create-featurestore-sample.test.js new file mode 100644 index 0000000000..cb66466c26 --- /dev/null +++ b/ai-platform/snippets/test/create-featurestore-sample.test.js @@ -0,0 +1,68 @@ +/* + * 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1; +const {after, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const featurestoreId = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const minNodeCount = 1; +const maxNodeCount = 5; +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +// Instantiates a featurestore clients +const featurestoreServiceClient = new FeaturestoreServiceClient({ + apiEndpoint: apiEndpoint, +}); + +const deleteFeaturestore = async () => { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: true, + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: 60000} + ); + await operation.promise(); +}; + +describe('AI platform create featurestore', async function () { + this.retries(2); + it('should create a featurestore', async () => { + const stdout = execSync( + `node ./create-featurestore-sample.js ${project} ${featurestoreId} ${minNodeCount} ${maxNodeCount} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Create featurestore response/); + }); + after('should delete the created featurestore', async () => { + await deleteFeaturestore(); + }); +}); diff --git a/ai-platform/snippets/test/create-hyperparameter-tuning-job.test.js b/ai-platform/snippets/test/create-hyperparameter-tuning-job.test.js new file mode 100644 index 0000000000..5d37625504 --- /dev/null +++ b/ai-platform/snippets/test/create-hyperparameter-tuning-job.test.js @@ -0,0 +1,78 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const jobServiceClient = new aiplatform.v1.JobServiceClient(clientOptions); + +const containerImageUri = + 'gcr.io/ucaip-sample-tests/ucaip-training-test:latest'; +const displayName = `temp_create_hyperparameter_tuning_job_test${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let tuningJobId; + +describe('AI platform create hyperparameter tuning job', async function () { + this.retries(2); + it('should create a new hyperparameter tuning job', async () => { + const stdout = execSync( + `node ./create-hyperparameter-tuning-job.js ${displayName} ${containerImageUri} ${project} ${location}` + ); + assert.match( + stdout, + /\/locations\/us-central1\/hyperparameterTuningJobs\// + ); + tuningJobId = stdout + .split('/locations/us-central1/hyperparameterTuningJobs/')[1] + .split('\n')[0]; + }); + + after( + 'should cancel the hyperparameter tuning job and delete it', + async () => { + const name = jobServiceClient.hyperparameterTuningJobPath( + project, + location, + tuningJobId + ); + + const cancelRequest = { + name, + }; + + jobServiceClient.cancelHyperparameterTuningJob(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return jobServiceClient.deleteHyperparameterTuningJob(deleteRequest); + }); + } + ); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-image-classification.test.js b/ai-platform/snippets/test/create-training-pipeline-image-classification.test.js new file mode 100644 index 0000000000..29e66437d7 --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-image-classification.test.js @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '1084241610289446912'; +const modelDisplayName = `temp_create_training_pipeline_image_classification_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_image_classification_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline image classification', async function () { + this.retries(2); + it('should create a new image classification training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-image-classification.js ${datasetId} ${modelDisplayName} ${trainingPipelineDisplayName} ${project} ${location}` + ); + assert.match(stdout, /\/locations\/us-central1\/trainingPipelines\//); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-image-object-detection.test.js b/ai-platform/snippets/test/create-training-pipeline-image-object-detection.test.js new file mode 100644 index 0000000000..e94ff3230d --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-image-object-detection.test.js @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '3555732643297361920'; +const modelDisplayName = `temp_create_training_pipeline_image_object_detection_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_image_object_detection_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline image object detection', async function () { + this.retries(2); + it('should create a new image object detection training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-image-object-detection.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline image object detection response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-tabular-classification.test.js b/ai-platform/snippets/test/create-training-pipeline-tabular-classification.test.js new file mode 100644 index 0000000000..2409e343a7 --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-tabular-classification.test.js @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '2438839935709478912'; +const modelDisplayName = `temp_create_training_pipeline_tables_classification_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_tables_classification_test_${uuid()}`; +const targetColumn = 'species'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline tables classification', async function () { + this.retries(2); + it('should create a new tables classification training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-tabular-classification.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${targetColumn} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline tabular classification response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-tabular-regression.test.js b/ai-platform/snippets/test/create-training-pipeline-tabular-regression.test.js new file mode 100644 index 0000000000..4ded88a2ee --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-tabular-regression.test.js @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '3019804287640272896'; +const modelDisplayName = `temp_create_training_pipeline_tables_regression_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_tables_regression_test_${uuid()}`; +const targetColumn = 'FLOAT_5000unique_REQUIRED'; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline tabular regression', async function () { + this.retries(2); + it('should create a new tabular regression training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-tabular-regression.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${targetColumn} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline tabular regression response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-text-classification.test.js b/ai-platform/snippets/test/create-training-pipeline-text-classification.test.js new file mode 100644 index 0000000000..0e32c2f5fb --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-text-classification.test.js @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '7051300010322821120'; +const modelDisplayName = `temp_create_training_pipeline_text_classification_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_text_classification_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline text classification', async function () { + this.retries(2); + it('should create a new text classification training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-text-classification.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline text classification response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-text-entity-extraction.test.js b/ai-platform/snippets/test/create-training-pipeline-text-entity-extraction.test.js new file mode 100644 index 0000000000..01080bb0d4 --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-text-entity-extraction.test.js @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '6203215905493614592'; +const modelDisplayName = `temp_create_training_pipeline_entity_extraction_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_entity_extraction_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline text entity extraction', async function () { + this.retries(2); + it('should create a new text entity extraction training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-text-entity-extraction.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline text entity extraction response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-text-sentiment-analysis.test.js b/ai-platform/snippets/test/create-training-pipeline-text-sentiment-analysis.test.js new file mode 100644 index 0000000000..e8492fa19c --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-text-sentiment-analysis.test.js @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '5148529167758786560'; +const modelDisplayName = `temp_create_training_pipeline_sentiment_analysis_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_sentiment_analysis_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline text sentiment analysis', async function () { + this.retries(2); + it('should create a new text sentiment analysis training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-text-sentiment-analysis.js \ + ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline text sentiment analysis response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-video-action-recognition.test.js b/ai-platform/snippets/test/create-training-pipeline-video-action-recognition.test.js new file mode 100644 index 0000000000..33d5f472ac --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-video-action-recognition.test.js @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '6881957627459272704'; +const modelDisplayName = `temp_create_training_pipeline_node_var_model_test_${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_node_var_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline video action recognition', async function () { + this.retries(2); + it('should create a new video action-recognition training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-video-action-recognition.js ${datasetId} ${modelDisplayName} ${trainingPipelineDisplayName} ${project} ${location}` + ); + assert.match( + stdout, + /Create training pipeline video action recognition response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-video-classification.test.js b/ai-platform/snippets/test/create-training-pipeline-video-classification.test.js new file mode 100644 index 0000000000..5a818b6f17 --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-video-classification.test.js @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '3757409464110546944'; +const modelDisplayName = `temp_create_training_pipeline_video_classification_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_video_classification_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline video classification', async function () { + this.retries(2); + it('should create a new video classification training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-video-classification.js ${datasetId} \ + ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline video classification response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/create-training-pipeline-video-object-tracking.test.js b/ai-platform/snippets/test/create-training-pipeline-video-object-tracking.test.js new file mode 100644 index 0000000000..b6a893360f --- /dev/null +++ b/ai-platform/snippets/test/create-training-pipeline-video-object-tracking.test.js @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const pipelineServiceClient = new aiplatform.v1.PipelineServiceClient( + clientOptions +); + +const datasetId = '1138566280794603520'; +const modelDisplayName = `temp_create_training_pipeline_video_object_tracking_model_test${uuid()}`; +const trainingPipelineDisplayName = `temp_create_training_pipeline_video_object_tracking_test_${uuid()}`; +const location = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +let trainingPipelineId; + +describe('AI platform create training pipeline object tracking', async function () { + this.retries(2); + it('should create a new object tracking training pipeline', async () => { + const stdout = execSync( + `node ./create-training-pipeline-video-object-tracking.js \ + ${datasetId} ${modelDisplayName} \ + ${trainingPipelineDisplayName} \ + ${project} ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Create training pipeline video object tracking response/ + ); + trainingPipelineId = stdout + .split('/locations/us-central1/trainingPipelines/')[1] + .split('\n')[0]; + }); + + after('should cancel the training pipeline and delete it', async () => { + const name = pipelineServiceClient.trainingPipelinePath( + project, + location, + trainingPipelineId + ); + + const cancelRequest = { + name, + }; + + pipelineServiceClient.cancelTrainingPipeline(cancelRequest).then(() => { + const deleteRequest = { + name, + }; + + return pipelineServiceClient.deleteTrainingPipeline(deleteRequest); + }); + }); +}); diff --git a/ai-platform/snippets/test/deploy-model-custom-trained-model.test.js b/ai-platform/snippets/test/deploy-model-custom-trained-model.test.js new file mode 100644 index 0000000000..e54518718a --- /dev/null +++ b/ai-platform/snippets/test/deploy-model-custom-trained-model.test.js @@ -0,0 +1,62 @@ +/* + * Copyright 2021 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const endpointDisplayName = `temp_create_endpoint_test_${uuid()}`; + +const modelId = '6430031960164270080'; +const deployedModelDisplayName = `temp_deploy_model_custom_model_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; +let deployedModelId; +let endpointId; + +describe('AI platform deploy model custom model', () => { + it('should deploy the custom model in the specified endpoint', async () => { + const endOut = execSync( + `node ./create-endpoint.js ${endpointDisplayName} ${project} \ + ${location}` + ); + endpointId = endOut + .split('/locations/us-central1/endpoints/')[1] + .split('\n')[0] + .split('/')[0]; + const stdout = execSync( + `node ./deploy-model-custom-trained-model.js ${modelId} \ + ${deployedModelDisplayName} \ + ${endpointId} \ + ${project} ${location}` + ); + assert.match(stdout, /Deploy model response/); + deployedModelId = stdout.split('Id : ')[1].split('\n')[0]; + }); + + after('should undeploy the deployed custom model', async () => { + execSync( + `node ./undeploy-model.js ${deployedModelId} ${endpointId} ${project} \ + ${location}` + ); + execSync(`node ./delete-endpoint.js ${endpointId} ${project} ${location}`); + }); +}); diff --git a/ai-platform/snippets/test/deploy-model.test.js b/ai-platform/snippets/test/deploy-model.test.js new file mode 100644 index 0000000000..dcd84b9184 --- /dev/null +++ b/ai-platform/snippets/test/deploy-model.test.js @@ -0,0 +1,63 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const endpointDisplayName = `temp_create_endpoint_test_${uuid()}`; + +const modelId = '4190810559500779520'; +const deployedModelDisplayName = `temp_deploy_model_test_${uuid()}`; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; +let deployedModelId; +let endpointId; + +describe('AI platform deploy model', () => { + it('should deploy the model in the specified endpoint', async () => { + const endOut = + execSync(`node ./create-endpoint.js ${endpointDisplayName} ${project} \ + ${location}`); + endpointId = endOut + .split('/locations/us-central1/endpoints/')[1] + .split('\n')[0] + .split('/')[0]; + const stdout = + execSync(`node ./deploy-model.js ${modelId} ${deployedModelDisplayName} \ + ${endpointId} \ + ${project} ${location}`); + assert.match(stdout, /Deploy model response/); + deployedModelId = stdout.split('Id : ')[1].split('\n')[0]; + }); + + after('should undeploy the deployed model', async () => { + // If test failed, the attempt to undeploy will fail too. + // Check to see whether we have a deployedModelId + if (deployedModelId === undefined) { + return; + } + + execSync(`node ./undeploy-model.js ${deployedModelId} ${endpointId} ${project} \ + ${location}`); + execSync(`node ./delete-endpoint.js ${endpointId} ${project} ${location}`); + }); +}); diff --git a/ai-platform/snippets/test/entity-type-samples.test.js b/ai-platform/snippets/test/entity-type-samples.test.js new file mode 100644 index 0000000000..50b7dc90c8 --- /dev/null +++ b/ai-platform/snippets/test/entity-type-samples.test.js @@ -0,0 +1,132 @@ +/* + * 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1; +const {after, before, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const featurestoreId = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const fixedNodeCount = 1; +const entityTypeId1 = `entity_type_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const entityTypeId2 = `entity_type_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const entityType1Description = + 'created during the entity type 1 sample testing'; +const entityType2Description = + 'created during the entity type 2 sample testing'; +const duration = 86400; +const updatedDuration = 172800; +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +// Instantiates a featurestore clients +const featurestoreServiceClient = new FeaturestoreServiceClient({ + apiEndpoint: apiEndpoint, +}); + +const deleteFeaturestore = async () => { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: true, + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: 60000} + ); + await operation.promise(); +}; + +describe('AI platform entity type api samples test', async function () { + this.retries(2); + before('should create the featurestore', async () => { + execSync( + `node ./create-featurestore-fixed-nodes-sample.js ${project} ${featurestoreId} ${fixedNodeCount} ${location} ${apiEndpoint}` + ); + }); + it('should create the entity type', async () => { + const stdout = execSync( + `node ./create-entity-type-sample.js ${project} ${featurestoreId} ${entityTypeId1} "${entityType1Description}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Create entity type response/); + }); + it('should create the entity type with monitoring', async () => { + const stdout = execSync( + `node ./create-entity-type-monitoring-sample.js ${project} ${featurestoreId} ${entityTypeId2} "${entityType2Description}" ${duration} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Create entity type monitoring response/); + }); + it('should get the created entity type', async () => { + const stdout = execSync( + `node ./get-entity-type-sample.js ${project} ${featurestoreId} ${entityTypeId1} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Get entity type response/); + }); + it('should list the entity types', async () => { + const stdout = execSync( + `node ./list-entity-types-sample.js ${project} ${featurestoreId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List entity types response/); + }); + it('should list the entity types asynchronously', async () => { + const stdout = execSync( + `node ./list-entity-types-async-sample.js ${project} ${featurestoreId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List entity types async response/); + }); + it('should list the entity types in streaming', async () => { + const stdout = execSync( + `node ./list-entity-types-stream-sample.js ${project} ${featurestoreId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List entity types stream response/); + }); + it('should update the entity type', async () => { + const stdout = execSync( + `node ./update-entity-type-sample.js ${project} ${featurestoreId} ${entityTypeId1} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Update entity type response/); + }); + it('should update the entity type monitoring', async () => { + const stdout = execSync( + `node ./update-entity-type-monitoring-sample.js ${project} ${featurestoreId} ${entityTypeId2} ${updatedDuration} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Update entity type monitoring response/); + }); + it('should delete the created entity type', async () => { + const stdout = execSync( + `node ./delete-entity-type-sample.js ${project} ${featurestoreId} ${entityTypeId1} true ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Delete entity type response/); + }); + after('should delete the created featurestore', async () => { + await deleteFeaturestore(); + }); +}); diff --git a/ai-platform/snippets/test/export-model-tabular-classification.test.js b/ai-platform/snippets/test/export-model-tabular-classification.test.js new file mode 100644 index 0000000000..c929b4d2d6 --- /dev/null +++ b/ai-platform/snippets/test/export-model-tabular-classification.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const gcsDestinationOutputUriPrefix = 'gs://ucaip-samples-test-output'; +const modelId = '6036688272397172736'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform export data tabular classification', () => { + it('should export model', async () => { + const stdout = execSync( + `node ./export-model-tabular-classification.js \ + ${gcsDestinationOutputUriPrefix} \ + ${modelId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Export model response/); + }); +}); diff --git a/ai-platform/snippets/test/feature-samples.test.js b/ai-platform/snippets/test/feature-samples.test.js new file mode 100644 index 0000000000..b2de671b8f --- /dev/null +++ b/ai-platform/snippets/test/feature-samples.test.js @@ -0,0 +1,187 @@ +/* + * 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1; +const {after, before, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const featurestoreId = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const fixedNodeCount = 1; +const entityTypeId = `entity_type_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const entityTypeDescription = 'created during create feature sample testing'; +const featureId = `feature_sample_${uuid().replace(/-/g, '_').slice(10, 20)}`; +const featureDescription = 'created during create feature sample testing'; +const valueType = 'STRING'; +const query = 'valueType=STRING'; +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +// Instantiates a featurestore clients +const featurestoreServiceClient = new FeaturestoreServiceClient({ + apiEndpoint: apiEndpoint, +}); + +const createFeaturestore = async () => { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}`; + + const featurestore = { + onlineServingConfig: { + fixedNodeCount: fixedNodeCount, + }, + }; + + const request = { + parent: parent, + featurestore: featurestore, + featurestoreId: featurestoreId, + }; + + // Create Featurestore request + const [operation] = await featurestoreServiceClient.createFeaturestore( + request, + {timeout: 900000} + ); + await operation.promise(); +}; + +const deleteFeaturestore = async () => { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: true, + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: 60000} + ); + await operation.promise(); +}; + +const createEntityType = async () => { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const entityType = { + description: entityTypeDescription, + }; + + const request = { + parent: parent, + entityTypeId: entityTypeId, + entityType: entityType, + }; + + // CreateEntityType request + const [operation] = await featurestoreServiceClient.createEntityType( + request, + {timeout: 300000} + ); + await operation.promise(); +}; + +describe('AI platform feature api samples', async function () { + this.retries(2); + before('should create the featurestore', async () => { + await createFeaturestore(); + }); + before('should create the entity type', async () => { + await createEntityType(); + }); + it('should create feature', async () => { + const stdout = execSync( + `node ./create-feature-sample.js ${project} ${featurestoreId} ${entityTypeId} ${featureId} ${valueType} "${featureDescription}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Create feature response/); + }); + it('should batch create features', async () => { + const stdout = execSync( + `node ./batch-create-features-sample.js ${project} ${featurestoreId} ${entityTypeId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Batch create features response/); + }); + it('should get the created feature', async () => { + const stdout = execSync( + `node ./get-feature-sample.js ${project} ${featurestoreId} ${entityTypeId} ${featureId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Get feature response/); + }); + it('should list all the features', async () => { + const stdout = execSync( + `node ./list-features-sample.js ${project} ${featurestoreId} ${entityTypeId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List features response/); + }); + it('should list all the features asynchronously', async () => { + const stdout = execSync( + `node ./list-features-async-sample.js ${project} ${featurestoreId} ${entityTypeId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List features async response/); + }); + it('should list all the features in streaming', async () => { + const stdout = execSync( + `node ./list-features-stream-sample.js ${project} ${featurestoreId} ${entityTypeId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List features stream response/); + }); + it('should search features', async () => { + const stdout = execSync( + `node ./search-features-sample.js ${project} "${query}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Search features response/); + }); + it('should search features asynchronously', async () => { + const stdout = execSync( + `node ./search-features-async-sample.js ${project} "${query}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Search features async response/); + }); + it('should search features in streaming', async () => { + const stdout = execSync( + `node ./search-features-stream-sample.js ${project} "${query}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Search features stream response/); + }); + it('should update the feature', async () => { + const stdout = execSync( + `node ./update-feature-sample.js ${project} ${featurestoreId} ${entityTypeId} ${featureId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Update feature response/); + }); + it('should delete the created feature', async () => { + const stdout = execSync( + `node ./delete-feature-sample.js ${project} ${featurestoreId} ${entityTypeId} ${featureId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Delete feature response/); + }); + after('should delete the created featurestore', async () => { + await deleteFeaturestore(); + }); +}); diff --git a/ai-platform/snippets/test/feature-values-samples.test.js b/ai-platform/snippets/test/feature-values-samples.test.js new file mode 100644 index 0000000000..17ce0946f7 --- /dev/null +++ b/ai-platform/snippets/test/feature-values-samples.test.js @@ -0,0 +1,306 @@ +/* + * 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. + */ + +'use strict'; + +const {BigQuery} = require('@google-cloud/bigquery'); +const {FeaturestoreServiceClient} = require('@google-cloud/aiplatform').v1; +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const datasetName = `movie_predictions_nodejs_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const batchReadTableName = 'batch_serving_table'; +const exportTableName = 'export_table'; +const exportSnapshotTableName = 'export_snapshot_table'; + +const featurestoreId = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const fixedNodeCount = 1; + +const entityTypeId1 = 'perm_users'; +const entityTypeDescription1 = 'Users Entity'; +const entityTypeId2 = 'perm_movies'; +const entityTypeDescription2 = 'Movies Entity'; +const entityId = 'alice'; + +const avroGcsUri1 = + 'gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/users.avro'; +const entityIdField1 = 'user_id'; + +const avroGcsUri2 = + 'gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movies.avro'; +const entityIdField2 = 'movie_id'; + +const featureTimeField = 'update_time'; +const workerCount = 2; +const batchReadDestinationTableUri = `bq://${project}.${datasetName}.${batchReadTableName}`; +const inputCsvFile = + 'gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movie_prediction_perm.csv'; + +const timestamp = 1629493102; +const exportDestinationTableUri = `bq://${project}.${datasetName}.${exportTableName}`; +const exportSnapshotDestinationTableUri = `bq://${project}.${datasetName}.${exportSnapshotTableName}`; + +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +// Instantiates a featurestore and bigquery clients +const featurestoreServiceClient = new FeaturestoreServiceClient({ + apiEndpoint: apiEndpoint, +}); +const bigqueryClient = new BigQuery({projectId: project}); + +const createEntityType = async (entityTypeId, entityTypeDescription) => { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const entityType = { + description: entityTypeDescription, + }; + + const request = { + parent: parent, + entityTypeId: entityTypeId, + entityType: entityType, + }; + + // CreateEntityType request + const [operation] = await featurestoreServiceClient.createEntityType( + request, + {timeout: 300000} + ); + await operation.promise(); +}; + +const createPermUsersFeatures = async () => { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId1}`; + + const ageFeature = { + valueType: 'INT64', + description: 'User age', + }; + + const ageFeatureRequest = { + feature: ageFeature, + featureId: 'age', + }; + + const genderFeature = { + valueType: 'STRING', + description: 'User gender', + }; + + const genderFeatureRequest = { + feature: genderFeature, + featureId: 'gender', + }; + + const likedGenresFeature = { + valueType: 'STRING_ARRAY', + description: 'An array of genres that this user liked', + }; + + const likedGenresFeatureRequest = { + feature: likedGenresFeature, + featureId: 'liked_genres', + }; + + const requests = [ + ageFeatureRequest, + genderFeatureRequest, + likedGenresFeatureRequest, + ]; + + const request = { + parent: parent, + requests: requests, + }; + + // Batch Create Features request + const [operation] = await featurestoreServiceClient.batchCreateFeatures( + request, + {timeout: 300000} + ); + await operation.promise(); +}; + +const createPermMoviesFeatures = async () => { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId2}`; + + const titleFeatureRequest = { + feature: { + valueType: 'STRING', + description: 'The title of the movie', + }, + featureId: 'title', + }; + + const genresFeatureRequest = { + feature: { + valueType: 'STRING', + description: 'The genres of the movie', + }, + featureId: 'genres', + }; + + const averageRatingFeatureRequest = { + feature: { + valueType: 'DOUBLE', + description: 'The average rating for the movie, range is [1.0-5.0]', + }, + featureId: 'average_rating', + }; + + const requests = [ + titleFeatureRequest, + genresFeatureRequest, + averageRatingFeatureRequest, + ]; + + const request = { + parent: parent, + requests: requests, + }; + + // Batch Create Features request + const [operation] = await featurestoreServiceClient.batchCreateFeatures( + request, + {timeout: 300000} + ); + await operation.promise(); +}; + +const importPermMoviesFeatures = async () => { + // Configure the entityType resource + const entityType = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId2}`; + + const avroSource = { + gcsSource: { + uris: [avroGcsUri2], + }, + }; + + const featureSpecs = [{id: 'title'}, {id: 'genres'}, {id: 'average_rating'}]; + + const request = { + entityType: entityType, + avroSource: avroSource, + entityIdField: entityIdField2, + featureSpecs: featureSpecs, + featureTimeField: featureTimeField, + workerCount: workerCount, + }; + + // Import Feature Values Request + const [operation] = await featurestoreServiceClient.importFeatureValues( + request, + {timeout: 300000} + ); + await operation.promise(); +}; + +const deleteFeaturestore = async () => { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const request = { + name: name, + force: true, + }; + + // Delete Featurestore request + const [operation] = await featurestoreServiceClient.deleteFeaturestore( + request, + {timeout: 60000} + ); + await operation.promise(); +}; + +describe('AI platform feature values apis', async function () { + this.retries(2); + before('should create the BigQuery Dataset', async () => { + await bigqueryClient.createDataset(datasetName, location); + }); + before('should create the featurestore', async () => { + execSync( + `node ./create-featurestore-fixed-nodes-sample.js ${project} ${featurestoreId} ${fixedNodeCount} ${location} ${apiEndpoint}` + ); + }); + before('should create the perm_users entity type', async () => { + await createEntityType(entityTypeId1, entityTypeDescription1); + }); + before('should create the perm_movies entity type', async () => { + await createEntityType(entityTypeId2, entityTypeDescription2); + }); + before('should create the perm_movies batch features', async () => { + await createPermMoviesFeatures(); + }); + before('should create the perm_users batch features', async () => { + await createPermUsersFeatures(); + }); + before('should import perm_movies feature values', async () => { + await importPermMoviesFeatures(); + }); + it('should import feature values', async () => { + const stdout = execSync( + `node ./import-feature-values-sample.js ${project} ${featurestoreId} ${entityTypeId1} "${avroGcsUri1}" ${entityIdField1} ${featureTimeField} ${workerCount} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Import feature values response/); + }); + it('should batch read feature values', async () => { + const stdout = execSync( + `node ./batch-read-feature-values-sample.js ${project} ${featurestoreId} ${inputCsvFile} "${batchReadDestinationTableUri}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Batch read feature values response/); + }); + it('should read feature values', async () => { + const stdout = execSync( + `node ./read-feature-values-sample.js ${project} ${featurestoreId} ${entityTypeId1} ${entityId} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Read feature values response/); + }); + it('should export feature values', async () => { + const stdout = execSync( + `node ./export-feature-values-sample.js ${project} ${featurestoreId} ${entityTypeId1} "${exportDestinationTableUri}" ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Export feature values response/); + }); + it('should export feature values using snapshot', async () => { + const stdout = execSync( + `node ./export-feature-values-snapshot-sample.js ${project} ${featurestoreId} ${entityTypeId1} "${exportSnapshotDestinationTableUri}" ${timestamp} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Export feature values snapshot response/); + }); + after('should delete the created featurestore', async () => { + await deleteFeaturestore(); + }); + + after('should delete the created dataset', async () => { + // Create a reference to the existing dataset + const dataset = bigqueryClient.dataset(datasetName); + // Delete the dataset and its contents + await dataset.delete({force: true}); + }); +}); diff --git a/ai-platform/snippets/test/featurestore-samples.test.js b/ai-platform/snippets/test/featurestore-samples.test.js new file mode 100644 index 0000000000..c759ba53d5 --- /dev/null +++ b/ai-platform/snippets/test/featurestore-samples.test.js @@ -0,0 +1,101 @@ +/* + * 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const project = process.env.CAIP_PROJECT_ID; +const featurestoreId1 = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; +const featurestoreId2 = `featurestore_sample_${uuid() + .replace(/-/g, '_') + .slice(10, 20)}`; + +const fixedNodeCount = 1; +const updatedFixedNodeCount = 3; +const minNodeCount = 1; +const maxNodeCount = 3; +const updatedMinNodeCount = 2; +const updatedMaxNodeCount = 4; +const location = 'us-central1'; +const apiEndpoint = 'us-central1-aiplatform.googleapis.com'; + +describe('AI platform featurestore api samples test', async function () { + this.retries(2); + before('should create a featurestore with fixed nodes', async () => { + execSync( + `node ./create-featurestore-fixed-nodes-sample.js ${project} ${featurestoreId1} ${fixedNodeCount} ${location} ${apiEndpoint}` + ); + }); + before('should create a featurestore with autoscaling', async () => { + execSync( + `node ./create-featurestore-sample.js ${project} ${featurestoreId2} ${minNodeCount} ${maxNodeCount} ${location} ${apiEndpoint}` + ); + }); + it('should get the featurestore', async () => { + const stdout = execSync( + `node ./get-featurestore-sample.js ${project} ${featurestoreId1} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Get featurestore response/); + }); + it('should list the featurestores', async () => { + const stdout = execSync( + `node ./list-featurestores-sample.js ${project} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List featurestores response/); + }); + it('should list the featurestores asynchronously', async () => { + const stdout = execSync( + `node ./list-featurestores-async-sample.js ${project} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List featurestores async response/); + }); + it('should list the featurestores in streaming', async () => { + const stdout = execSync( + `node ./list-featurestores-stream-sample.js ${project} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /List featurestores stream response/); + }); + it('should update featurestores fixed nodes', async () => { + const stdout = execSync( + `node ./update-featurestore-fixed-nodes-sample.js ${project} ${featurestoreId1} ${updatedFixedNodeCount} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Update featurestore fixed nodes response/); + }); + it('should update featurestore autoscaling', async () => { + const stdout = execSync( + `node ./update-featurestore-sample.js ${project} ${featurestoreId2} ${updatedMinNodeCount} ${updatedMaxNodeCount} ${location} ${apiEndpoint}` + ); + assert.match(stdout, /Update featurestore response/); + }); + it('should delete the created featurestore', async () => { + const stdout = execSync( + `node ./delete-featurestore-sample.js ${project} ${featurestoreId1} true ${location}` + ); + assert.match(stdout, /Delete featurestore response/); + }); + after('should delete the created featurestore 2', async () => { + execSync( + `node ./delete-featurestore-sample.js ${project} ${featurestoreId2} true ${location}` + ); + }); +}); diff --git a/ai-platform/snippets/test/get-custom-job.test.js b/ai-platform/snippets/test/get-custom-job.test.js new file mode 100644 index 0000000000..f51fa2f9a2 --- /dev/null +++ b/ai-platform/snippets/test/get-custom-job.test.js @@ -0,0 +1,41 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const customJobId = '7980906305281851392'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get custom job', () => { + it('should get the specified custom job', async () => { + const stdout = execSync( + `node ./get-custom-job.js ${customJobId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get custom job response/); + }); +}); diff --git a/ai-platform/snippets/test/get-hyperparameter-tuning-job.test.js b/ai-platform/snippets/test/get-hyperparameter-tuning-job.test.js new file mode 100644 index 0000000000..a334a8e02c --- /dev/null +++ b/ai-platform/snippets/test/get-hyperparameter-tuning-job.test.js @@ -0,0 +1,36 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const tuningJobId = '2216298782247616512'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get hyperparameter tuning job', () => { + it('should get the specified hyperparameter tuning job', async () => { + const stdout = execSync( + `node ./get-hyperparameter-tuning-job.js ${tuningJobId} ${project} ${location}` + ); + assert.match(stdout, /Get hyperparameter tuning job response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-slice.test.js b/ai-platform/snippets/test/get-model-evaluation-slice.test.js new file mode 100644 index 0000000000..85033539c3 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-slice.test.js @@ -0,0 +1,45 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const evaluationId = '9035588644970168320'; +const sliceId = '6481571820677004173'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get model evaluation slice', () => { + it('should get the evaluation slice from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-slice.js ${modelId} ${evaluationId} \ + ${sliceId} ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation slice/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js b/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js new file mode 100644 index 0000000000..b4ea49e905 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-tabular-classification.test.js @@ -0,0 +1,48 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '6036688272397172736'; +const evaluationId = '1866113044163962838'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get tabular classification model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-tabular-classification.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match( + stdout, + /Get model evaluation tabular classification response/ + ); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js b/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js new file mode 100644 index 0000000000..8bed83613c --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-tabular-regression.test.js @@ -0,0 +1,45 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8842430840248991744'; +const evaluationId = '4944816689650806017'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get tabular regression model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-tabular-regression.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation tabular regression response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-video-action-recognition.test.js b/ai-platform/snippets/test/get-model-evaluation-video-action-recognition.test.js new file mode 100644 index 0000000000..e2c6699595 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-video-action-recognition.test.js @@ -0,0 +1,43 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const modelId = '3530998029718913024'; +const evaluationId = '305008923591573504'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get video action recognition model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-video-action-recognition.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}` + ); + assert.match( + stdout, + /Get model evaluation video action recognition response/ + ); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js b/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js new file mode 100644 index 0000000000..8f0cc4f1ed --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-video-classification.test.js @@ -0,0 +1,45 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8596984660557299712'; +const evaluationId = '7092045712224944128'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get video classification model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-video-classification.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location} `, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation video classification response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js b/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js new file mode 100644 index 0000000000..01e3cdedb9 --- /dev/null +++ b/ai-platform/snippets/test/get-model-evaluation-video-object-tracking.test.js @@ -0,0 +1,45 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '8609932509485989888'; +const evaluationId = '6016811301190238208'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get video object tracking model evaluation', () => { + it('should get the evaluation from the specified model', async () => { + const stdout = execSync( + `node ./get-model-evaluation-video-object-tracking.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location} `, + { + cwd, + } + ); + assert.match(stdout, /Get model evaluation video object tracking response/); + }); +}); diff --git a/ai-platform/snippets/test/get-model.test.js b/ai-platform/snippets/test/get-model.test.js new file mode 100644 index 0000000000..f2fd901df8 --- /dev/null +++ b/ai-platform/snippets/test/get-model.test.js @@ -0,0 +1,41 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get model', () => { + it('should get the specified model', async () => { + const stdout = execSync( + `node ./get-model.js ${modelId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Get model response/); + }); +}); diff --git a/ai-platform/snippets/test/get-training-pipeline.test.js b/ai-platform/snippets/test/get-training-pipeline.test.js new file mode 100644 index 0000000000..b7455d90c1 --- /dev/null +++ b/ai-platform/snippets/test/get-training-pipeline.test.js @@ -0,0 +1,36 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const trainingPipelineId = '1419759782528548864'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform get training pipeline', () => { + it('should get the specified training pipeline', async () => { + const stdout = execSync( + `node ./get-training-pipeline.js ${trainingPipelineId} ${project} ${location}` + ); + assert.match(stdout, /Get training pipeline response/); + }); +}); diff --git a/ai-platform/snippets/test/import-data-video-action-recognition.test.js b/ai-platform/snippets/test/import-data-video-action-recognition.test.js new file mode 100644 index 0000000000..ad72270ed4 --- /dev/null +++ b/ai-platform/snippets/test/import-data-video-action-recognition.test.js @@ -0,0 +1,75 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const {assert} = require('chai'); +const {after, before, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const aiplatform = require('@google-cloud/aiplatform'); +const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', +}; + +const datasetServiceClient = new aiplatform.v1.DatasetServiceClient( + clientOptions +); + +let datasetId = ''; +const datasetDisplayName = `temp_import_data_node_var_${uuid()}`; +const gcsSourceUri = 'gs://automl-video-demo-data/ucaip-var/swimrun.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data video action recognition', () => { + before('should create the new dataset', async () => { + const parent = `projects/${project}/locations/${location}`; + const [operation] = await datasetServiceClient.createDataset({ + parent, + dataset: { + displayName: datasetDisplayName, + metadataSchemaUri: + 'gs://google-cloud-aiplatform/schema/dataset/metadata/video_1.0.0.yaml', + }, + }); + const [response] = await operation.promise(); + const datasetName = response.name; + datasetId = datasetName.split('datasets/')[1]; + }); + + it('should import video action recognition data to dataset', async () => { + const stdout = execSync( + `node ./import-data-video-action-recognition.js ${datasetId} ${gcsSourceUri} ${project} ${location}` + ); + assert.match(stdout, /Import data video action recognition response/); + }); + + after('should cancel the import job and delete the dataset', async () => { + const datasetName = datasetServiceClient.datasetPath( + project, + location, + datasetId + ); + const [operation] = await datasetServiceClient.deleteDataset({ + name: datasetName, + }); + await operation.promise(); + }); +}); diff --git a/ai-platform/snippets/test/import-data-video-classification.test.js b/ai-platform/snippets/test/import-data-video-classification.test.js new file mode 100644 index 0000000000..9c4334f130 --- /dev/null +++ b/ai-platform/snippets/test/import-data-video-classification.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const datasetId = '3757409464110546944'; +const gcsSourceUri = + 'gs://ucaip-sample-resources/hmdb_split1_5classes_train.jsonl'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform import data video classification', () => { + it('should import video classification data to dataset', async () => { + const stdout = execSync( + `node ./import-data-video-classification.js ${datasetId} \ + ${gcsSourceUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Import data video classification response/); + }); +}); diff --git a/ai-platform/snippets/test/list-endpoints.test.js b/ai-platform/snippets/test/list-endpoints.test.js new file mode 100644 index 0000000000..7274916692 --- /dev/null +++ b/ai-platform/snippets/test/list-endpoints.test.js @@ -0,0 +1,37 @@ +/** + * Copyright 2020, 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 + * + * http://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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const cwd = path.join(__dirname, '..'); +const LOCATION = 'us-central1'; +const project = process.env.CAIP_PROJECT_ID; + +describe('AI platform list endpoints', () => { + it('should list all endpoints in a parent resource', async () => { + const stdout = execSync(`node ./list-endpoints.js ${project} ${LOCATION}`, { + cwd, + }); + assert.match(stdout, /Endpoint/); + }); +}); diff --git a/ai-platform/snippets/test/list-model-evaluation-slices.test.js b/ai-platform/snippets/test/list-model-evaluation-slices.test.js new file mode 100644 index 0000000000..10e0ad3af3 --- /dev/null +++ b/ai-platform/snippets/test/list-model-evaluation-slices.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelId = '3512561418744365056'; +const evaluationId = '9035588644970168320'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +describe('AI platform list model evaluation slices', () => { + it('should list all the evaluation slices from the \ + specified model', async () => { + const stdout = execSync( + `node ./list-model-evaluation-slices.js ${modelId} \ + ${evaluationId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /List model evaluation response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-image-classification.test.js b/ai-platform/snippets/test/predict-image-classification.test.js new file mode 100644 index 0000000000..4947dbd97a --- /dev/null +++ b/ai-platform/snippets/test/predict-image-classification.test.js @@ -0,0 +1,45 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); +const filename = 'resources/daisy.jpg'; +const endpointId = '71213169107795968'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict image classification', async function () { + this.retries(2); + it('should make predictions using the image classification model', async () => { + const stdout = execSync( + `node ./predict-image-classification.js ${filename} \ + ${endpointId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Predict image classification response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-image-object-detection.test.js b/ai-platform/snippets/test/predict-image-object-detection.test.js new file mode 100644 index 0000000000..d482ea5564 --- /dev/null +++ b/ai-platform/snippets/test/predict-image-object-detection.test.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const filename = 'resources/caprese_salad.jpg'; +const endpointId = '2791387344039575552'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict image object detection', async function () { + this.retries(2); + it('should make predictions using the image object detection model', async () => { + const stdout = execSync( + `node ./predict-image-object-detection.js ${filename} \ + ${endpointId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Predict image object detection response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-tabular-classification.test.js b/ai-platform/snippets/test/predict-tabular-classification.test.js new file mode 100644 index 0000000000..1bb90a85a3 --- /dev/null +++ b/ai-platform/snippets/test/predict-tabular-classification.test.js @@ -0,0 +1,44 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const endpointId = '4966625964059525120'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict tabular classification', () => { + it('should make predictions using the tabular classification model', async () => { + const stdout = execSync( + `node ./predict-tabular-classification.js ${endpointId} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + console.log(stdout); + assert.match(stdout, /Predict tabular classification response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-tabular-regression.test.js b/ai-platform/snippets/test/predict-tabular-regression.test.js new file mode 100644 index 0000000000..6e18ee6afe --- /dev/null +++ b/ai-platform/snippets/test/predict-tabular-regression.test.js @@ -0,0 +1,42 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const endpointId = '1014154341088493568'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict tabular regression', () => { + it('should make predictions using the tabular regression model', async () => { + const stdout = execSync( + `node ./predict-tabular-regression.js ${endpointId} ${project} ${location}`, + { + cwd, + } + ); + console.log(stdout); + assert.match(stdout, /Predict tabular regression response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-text-classification.test.js b/ai-platform/snippets/test/predict-text-classification.test.js new file mode 100644 index 0000000000..c4654b132d --- /dev/null +++ b/ai-platform/snippets/test/predict-text-classification.test.js @@ -0,0 +1,44 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const textInput = + 'My local greasy-spoon diner took way too long' + + 'to get my food. It also costs too much. Good food though.'; +const endpointId = '65372563341049856'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict text classification', () => { + it('should make predictions using the text classification model', async () => { + const stdout = execSync( + `node ./predict-text-classification.js "${textInput}" ${endpointId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Predict text classification response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-text-entity-extraction.test.js b/ai-platform/snippets/test/predict-text-entity-extraction.test.js new file mode 100644 index 0000000000..d2ab4e2637 --- /dev/null +++ b/ai-platform/snippets/test/predict-text-entity-extraction.test.js @@ -0,0 +1,43 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const textInput = + 'Bipolar depression and anxiety are often hereditary diseases.'; +const endpointId = '6207156555167563776'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict text entity extraction', () => { + it('should make predictions using the text extraction model', async () => { + const stdout = execSync( + `node ./predict-text-entity-extraction.js "${textInput}" ${endpointId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Predict text entity extraction response/); + }); +}); diff --git a/ai-platform/snippets/test/predict-text-sentiment-analysis.test.js b/ai-platform/snippets/test/predict-text-sentiment-analysis.test.js new file mode 100644 index 0000000000..4665941622 --- /dev/null +++ b/ai-platform/snippets/test/predict-text-sentiment-analysis.test.js @@ -0,0 +1,44 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {describe, it} = require('mocha'); + +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const textInput = + 'Economic downturns can be very scary for normal workers.' + + " I dislike how the stock market's fluctuations affect my retirement."; +const endpointId = '7811563922418302976'; +const project = process.env.CAIP_PROJECT_ID; +const location = 'us-central1'; + +describe('AI platform predict text sentiment analysis', () => { + it('should make predictions using the text sentiment model', async () => { + const stdout = execSync( + `node ./predict-text-sentiment-analysis.js "${textInput}" ${endpointId} ${project} ${location}`, + { + cwd, + } + ); + assert.match(stdout, /Predict text sentiment analysis response/); + }); +}); diff --git a/ai-platform/snippets/test/quickstart.test.js b/ai-platform/snippets/test/quickstart.test.js new file mode 100644 index 0000000000..25e3bce29c --- /dev/null +++ b/ai-platform/snippets/test/quickstart.test.js @@ -0,0 +1,29 @@ +/** + * Copyright 2017, Google, Inc. + * 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 + * + * http://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. + */ + +'use strict'; + +const assert = require('assert'); +const cp = require('child_process'); +const {describe, it} = require('mocha'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe('quickstart', () => { + it('should have functional quickstart', async () => { + const stdout = execSync('node quickstart.js'); + assert(stdout.match(/DatasetServiceClient/)); + }); +}); diff --git a/ai-platform/snippets/test/upload-model.test.js b/ai-platform/snippets/test/upload-model.test.js new file mode 100644 index 0000000000..b06c3eb1a9 --- /dev/null +++ b/ai-platform/snippets/test/upload-model.test.js @@ -0,0 +1,61 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +const path = require('path'); +const {assert} = require('chai'); +const {after, describe, it} = require('mocha'); + +const uuid = require('uuid').v4; +const cp = require('child_process'); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); +const cwd = path.join(__dirname, '..'); + +const modelDisplayName = `temp_upload_model_test${uuid()}`; +const imageUri = + 'gcr.io/cloud-ml-service-public/cloud-ml-online-prediction-model-server-cpu:v1_15py3cmle_op_images_20200229_0210_RC00'; +const artifactUri = 'gs://ucaip-samples-us-central1/model/explain/'; +const project = process.env.CAIP_PROJECT_ID; +const location = process.env.LOCATION; + +let modelId; + +describe('AI platform upload model', () => { + it('should upload the specified model', async () => { + const stdout = execSync( + `node ./upload-model.js ${modelDisplayName} \ + ${imageUri} \ + ${artifactUri} \ + ${project} \ + ${location}`, + { + cwd, + } + ); + console.log(stdout); + assert.match(stdout, /Upload model response/); + modelId = stdout + .split('/locations/us-central1/models/')[1] + .split('\n')[0] + .split('/')[0]; + }); + after('delete the model', async () => { + execSync(`node ./delete-model.js ${modelId} ${project} ${location}`, { + cwd, + }); + }); +}); diff --git a/ai-platform/snippets/undeploy-model.js b/ai-platform/snippets/undeploy-model.js new file mode 100644 index 0000000000..7081558b63 --- /dev/null +++ b/ai-platform/snippets/undeploy-model.js @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + deployedModelId, + endpointId, + project, + location = 'us-central1' +) { + // [START aiplatform_undeploy_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + * (Not necessary if passing values as arguments) + */ + + // const deployedModelId = "YOUR_MODEL_ID"; + // const endpointId = 'YOUR_ENDPOINT_ID'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + const endpoint = `projects/${project}/locations/${location}/endpoints/${endpointId}`; + // Imports the Google Cloud Endpoint Service Client library + const {EndpointServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint: + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const endpointServiceClient = new EndpointServiceClient(clientOptions); + + async function undeployModel() { + // Configure the parent resource + const request = { + deployedModelId, + endpoint, + }; + + // Get and print out a list of all the endpoints for this resource + const [response] = await endpointServiceClient.undeployModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + + console.log('Undeploy model response'); + console.log(response); + } + undeployModel(); + // [END aiplatform_undeploy_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/update-entity-type-monitoring-sample.js b/ai-platform/snippets/update-entity-type-monitoring-sample.js new file mode 100644 index 0000000000..24d797ea75 --- /dev/null +++ b/ai-platform/snippets/update-entity-type-monitoring-sample.js @@ -0,0 +1,105 @@ +/* + * 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. + */ + +/* + * Updates the parameters of a single EntityType with monitoring configuration. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + duration, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_update_entity_type_monitoring_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const duration = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // Imports the Google Cloud Featurestore Service Client library + const {FeaturestoreServiceClient} = + require('@google-cloud/aiplatform').v1beta1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: apiEndpoint, + }; + + // Instantiates a client + const featurestoreServiceClient = new FeaturestoreServiceClient( + clientOptions + ); + + async function updateEntityTypeMonitoring() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + // Constructing the monitoring configuration + const monitoringConfig = { + snapshotAnalysis: { + monitoringInterval: { + seconds: Number(duration), + }, + }, + }; + + // Constructing the entityType + const entityType = { + name: name, + monitoringConfig: monitoringConfig, + }; + + const request = { + entityType: entityType, + }; + + // Update EntityType request + const [response] = await featurestoreServiceClient.updateEntityType( + request, + {timeout: Number(timeout)} + ); + + console.log('Update entity type monitoring response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + updateEntityTypeMonitoring(); + // [END aiplatform_update_entity_type_monitoring_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/update-entity-type-sample.js b/ai-platform/snippets/update-entity-type-sample.js new file mode 100644 index 0000000000..dfb3872c86 --- /dev/null +++ b/ai-platform/snippets/update-entity-type-sample.js @@ -0,0 +1,97 @@ +/* + * 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. + */ + +/* + * Updates the parameters of a single EntityType. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_update_entity_type_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 updateEntityType() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}`; + + // Constructing the entityType + const entityType = { + name: name, + labels: { + language: 'nodejs', + project: 'vertex-ai-dev', + }, + description: 'updated description', + }; + + const request = { + entityType: entityType, + }; + + // Update EntityType request + const [response] = await featurestoreServiceClient.updateEntityType( + request, + {timeout: Number(timeout)} + ); + + console.log('Update entity type response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + updateEntityType(); + // [END aiplatform_update_entity_type_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/update-feature-sample.js b/ai-platform/snippets/update-feature-sample.js new file mode 100644 index 0000000000..a3e960268c --- /dev/null +++ b/ai-platform/snippets/update-feature-sample.js @@ -0,0 +1,96 @@ +/* + * 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. + */ + +/* + * Updates the parameters of a single Feature. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + entityTypeId, + featureId, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 300000 +) { + // [START aiplatform_update_feature_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 entityTypeId = 'YOUR_ENTITY_TYPE_ID'; + // const featureId = 'YOUR_FEATURE_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 updateFeature() { + // Configure the name resource + const name = `projects/${project}/locations/${location}/featurestores/${featurestoreId}/entityTypes/${entityTypeId}/features/${featureId}`; + + const feature = { + name: name, + labels: { + language: 'nodejs', + created_by: 'update_feature', + }, + }; + + const request = { + feature: feature, + }; + + // Update Feature request + const [response] = await featurestoreServiceClient.updateFeature(request, { + timeout: Number(timeout), + }); + + console.log('Update feature response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + updateFeature(); + // [END aiplatform_update_feature_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/update-featurestore-fixed-nodes-sample.js b/ai-platform/snippets/update-featurestore-fixed-nodes-sample.js new file mode 100644 index 0000000000..dabf8e7fab --- /dev/null +++ b/ai-platform/snippets/update-featurestore-fixed-nodes-sample.js @@ -0,0 +1,93 @@ +/* + * 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. + */ + +/* + * Updates the parameters of a single Featurestore. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + fixedNodeCount = 1, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 600000 +) { + // [START aiplatform_update_featurestore_fixed_nodes_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 fixedNodeCount = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // 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 updateFeaturestoreFixedNodes() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const featurestore = { + name: parent, + onlineServingConfig: {fixedNodeCount: Number(fixedNodeCount)}, + }; + + const request = { + featurestore: featurestore, + }; + + // Update Featurestore request + const [operation] = await featurestoreServiceClient.updateFeaturestore( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Update featurestore fixed nodes response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + updateFeaturestoreFixedNodes(); + // [END aiplatform_update_featurestore_fixed_nodes_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/update-featurestore-sample.js b/ai-platform/snippets/update-featurestore-sample.js new file mode 100644 index 0000000000..8cf89b93ed --- /dev/null +++ b/ai-platform/snippets/update-featurestore-sample.js @@ -0,0 +1,101 @@ +/* + * 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. + */ + +/* + * Updates the parameters of a single Featurestore with autoscaling configuration. + * See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +'use strict'; + +async function main( + project, + featurestoreId, + minNodeCount = 1, + maxNodeCount = 3, + location = 'us-central1', + apiEndpoint = 'us-central1-aiplatform.googleapis.com', + timeout = 600000 +) { + // [START aiplatform_update_featurestore_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 minNodeCount = ; + // const maxNodeCount = ; + // const location = 'YOUR_PROJECT_LOCATION'; + // const apiEndpoint = 'YOUR_API_ENDPOINT'; + // const timeout = ; + + // Imports the Google Cloud Featurestore Service Client library + const {FeaturestoreServiceClient} = + require('@google-cloud/aiplatform').v1beta1; + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: apiEndpoint, + }; + + // Instantiates a client + const featurestoreServiceClient = new FeaturestoreServiceClient( + clientOptions + ); + + async function updateFeaturestore() { + // Configure the parent resource + const parent = `projects/${project}/locations/${location}/featurestores/${featurestoreId}`; + + const featurestore = { + name: parent, + onlineServingConfig: { + scaling: { + minNodeCount: minNodeCount, + maxNodeCount: maxNodeCount, + }, + }, + }; + + const request = { + featurestore: featurestore, + }; + + // Update Featurestore request + const [operation] = await featurestoreServiceClient.updateFeaturestore( + request, + {timeout: Number(timeout)} + ); + const [response] = await operation.promise(); + + console.log('Update featurestore response'); + console.log(`Name : ${response.name}`); + console.log('Raw response:'); + console.log(JSON.stringify(response, null, 2)); + } + updateFeaturestore(); + // [END aiplatform_update_featurestore_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2)); diff --git a/ai-platform/snippets/upload-model.js b/ai-platform/snippets/upload-model.js new file mode 100644 index 0000000000..56e3c01c16 --- /dev/null +++ b/ai-platform/snippets/upload-model.js @@ -0,0 +1,94 @@ +/* + * Copyright 2020 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. + */ + +'use strict'; + +async function main( + modelDisplayName, + imageUri, + artifactUri, + project, + location = 'us-central1' +) { + // [START aiplatform_upload_model_sample] + /** + * TODO(developer): Uncomment these variables before running the sample.\ + */ + + // const modelDisplayName = 'YOUR_MODEL_DISPLAY_NAME'; + // const metadataSchemaUri = 'YOUR_METADATA_SCHEMA_URI'; + // const imageUri = 'YOUR_IMAGE_URI'; + // const artifactUri = 'YOUR_ARTIFACT_URI'; + // const project = 'YOUR_PROJECT_ID'; + // const location = 'YOUR_PROJECT_LOCATION'; + + // Imports the Google Cloud Model Service Client library + const {ModelServiceClient} = require('@google-cloud/aiplatform'); + + // Specifies the location of the api endpoint + const clientOptions = { + apiEndpoint: 'us-central1-aiplatform.googleapis.com', + }; + + // Instantiates a client + const modelServiceClient = new ModelServiceClient(clientOptions); + + async function uploadModel() { + // Configure the parent resources + const parent = `projects/${project}/locations/${location}`; + // Configure the model resources + const model = { + displayName: modelDisplayName, + metadataSchemaUri: '', + artifactUri: artifactUri, + containerSpec: { + imageUri: imageUri, + command: [], + args: [], + env: [], + ports: [], + predictRoute: '', + healthRoute: '', + }, + }; + const request = { + parent, + model, + }; + + console.log('PARENT AND MODEL'); + console.log(parent, model); + // Upload Model request + const [response] = await modelServiceClient.uploadModel(request); + console.log(`Long running operation : ${response.name}`); + + // Wait for operation to complete + await response.promise(); + const result = response.result; + + console.log('Upload model response '); + console.log(`\tModel : ${result.model}`); + } + uploadModel(); + // [END aiplatform_upload_model_sample] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2));