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/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/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/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/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/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/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/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));