Skip to content

Commit e39ab3c

Browse files
Merge branch 'main' into parametermanager-regional-samples-create-list-render
2 parents 8109702 + 375215f commit e39ab3c

13 files changed

+1072
-14
lines changed

ai-platform/snippets/predict-text-embeddings.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// [START generativeaionvertexai_sdk_embedding]
2121
async function main(
2222
project,
23-
model = 'text-embedding-005',
23+
model = 'gemini-embedding-001',
2424
texts = 'banana bread?;banana muffins?',
2525
task = 'QUESTION_ANSWERING',
2626
dimensionality = 0,
@@ -37,19 +37,29 @@ async function main(
3737
const instances = texts
3838
.split(';')
3939
.map(e => helpers.toValue({content: e, task_type: task}));
40+
41+
const client = new PredictionServiceClient(clientOptions);
4042
const parameters = helpers.toValue(
4143
dimensionality > 0 ? {outputDimensionality: parseInt(dimensionality)} : {}
4244
);
43-
const request = {endpoint, instances, parameters};
44-
const client = new PredictionServiceClient(clientOptions);
45-
const [response] = await client.predict(request);
46-
const predictions = response.predictions;
47-
const embeddings = predictions.map(p => {
48-
const embeddingsProto = p.structValue.fields.embeddings;
49-
const valuesProto = embeddingsProto.structValue.fields.values;
50-
return valuesProto.listValue.values.map(v => v.numberValue);
51-
});
52-
console.log('Got embeddings: \n' + JSON.stringify(embeddings));
45+
const allEmbeddings = []
46+
// gemini-embedding-001 takes one input at a time.
47+
for (const instance of instances) {
48+
const request = {endpoint, instances: [instance], parameters};
49+
const [response] = await client.predict(request);
50+
const predictions = response.predictions;
51+
52+
const embeddings = predictions.map(p => {
53+
const embeddingsProto = p.structValue.fields.embeddings;
54+
const valuesProto = embeddingsProto.structValue.fields.values;
55+
return valuesProto.listValue.values.map(v => v.numberValue);
56+
});
57+
58+
allEmbeddings.push(embeddings[0])
59+
}
60+
61+
62+
console.log('Got embeddings: \n' + JSON.stringify(allEmbeddings));
5363
}
5464

5565
callPredict();

ai-platform/snippets/test/predict-text-embeddings.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const texts = [
3737
describe('predict text embeddings', () => {
3838
it('should get text embeddings using the latest model', async () => {
3939
const stdout = execSync(
40-
`node ./predict-text-embeddings.js ${project} text-embedding-004 '${texts.join(';')}' QUESTION_ANSWERING ${dimensionality}`,
40+
`node ./predict-text-embeddings.js ${project} gemini-embedding-001 '${texts.join(';')}' QUESTION_ANSWERING ${dimensionality}`,
4141
{cwd}
4242
);
4343
const embeddings = JSON.parse(stdout.trimEnd().split('\n').at(-1));

parametermanager/deleteParam.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Deletes a parameter from the global location of the specified project using the Google Cloud Parameter Manager SDK.
19+
*
20+
* @param {string} projectId - The Google Cloud project ID where the parameter is located.
21+
* @param {string} parameterId - The ID of the parameter to delete.
22+
*/
23+
async function main(projectId, parameterId) {
24+
// [START parametermanager_delete_param]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'my-project';
29+
// const parameterId = 'my-parameter';
30+
31+
// Imports the Parameter Manager library
32+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
33+
34+
// Instantiates a client
35+
const client = new ParameterManagerClient();
36+
37+
async function deleteParam() {
38+
// Construct the fully qualified parameter name
39+
const name = client.parameterPath(projectId, 'global', parameterId);
40+
41+
// Delete the parameter
42+
await client.deleteParameter({
43+
name: name,
44+
});
45+
46+
console.log(`Deleted parameter: ${name}`);
47+
return name;
48+
}
49+
50+
return await deleteParam();
51+
// [END parametermanager_delete_param]
52+
}
53+
module.exports.main = main;
54+
55+
/* c8 ignore next 10 */
56+
if (require.main === module) {
57+
main(...process.argv.slice(2)).catch(err => {
58+
console.error(err.message);
59+
process.exitCode = 1;
60+
});
61+
process.on('unhandledRejection', err => {
62+
console.error(err.message);
63+
process.exitCode = 1;
64+
});
65+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Deletes a specific version of an existing parameter in the global location
19+
* of the specified project using the Google Cloud Parameter Manager SDK.
20+
*
21+
* @param {string} projectId - The Google Cloud project ID where the parameter is located.
22+
* @param {string} parameterId - The ID of the parameter for which version is to be deleted.
23+
* @param {string} versionId - The version ID of the parameter to delete.
24+
*/
25+
async function main(projectId, parameterId, versionId) {
26+
// [START parametermanager_delete_param_version]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const projectId = 'my-project';
31+
// const parameterId = 'my-parameter';
32+
// const versionId = 'v1';
33+
34+
// Imports the Parameter Manager library
35+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
36+
37+
// Instantiates a client
38+
const client = new ParameterManagerClient();
39+
40+
async function deleteParamVersion() {
41+
// Construct the fully qualified parameter version name
42+
const name = client.parameterVersionPath(
43+
projectId,
44+
'global',
45+
parameterId,
46+
versionId
47+
);
48+
49+
// Delete the parameter version
50+
await client.deleteParameterVersion({
51+
name: name,
52+
});
53+
console.log(`Deleted parameter version: ${name}`);
54+
return name;
55+
}
56+
57+
return await deleteParamVersion();
58+
// [END parametermanager_delete_param_version]
59+
}
60+
module.exports.main = main;
61+
62+
/* c8 ignore next 10 */
63+
if (require.main === module) {
64+
main(...process.argv.slice(2)).catch(err => {
65+
console.error(err.message);
66+
process.exitCode = 1;
67+
});
68+
process.on('unhandledRejection', err => {
69+
console.error(err.message);
70+
process.exitCode = 1;
71+
});
72+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Disables a specific version of a global parameter in Google Cloud Parameter Manager.
19+
* This function demonstrates how to disable a global parameter version by setting
20+
* its 'disabled' field to true using the Parameter Manager client library.
21+
*
22+
* @param {string} projectId - The Google Cloud project ID where the parameter is located.
23+
* @param {string} parameterId - The ID of the parameter for which version is to be disabled.
24+
* @param {string} versionId - The version ID of the parameter to be disabled.
25+
*/
26+
async function main(projectId, parameterId, versionId) {
27+
// [START parametermanager_disable_param_version]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'my-project';
32+
// const parameterId = 'my-parameter';
33+
// const versionId = 'v1';
34+
35+
// Imports the Parameter Manager library
36+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
37+
38+
// Instantiates a client
39+
const client = new ParameterManagerClient();
40+
41+
async function disableParamVersion() {
42+
// Construct the full resource name
43+
const name = client.parameterVersionPath(
44+
projectId,
45+
'global',
46+
parameterId,
47+
versionId
48+
);
49+
50+
// Construct the request
51+
const request = {
52+
parameterVersion: {
53+
name: name,
54+
disabled: true,
55+
},
56+
updateMask: {
57+
paths: ['disabled'],
58+
},
59+
};
60+
61+
// Make the API call to update the parameter version
62+
const [parameterVersion] = await client.updateParameterVersion(request);
63+
64+
console.log(
65+
`Disabled parameter version ${parameterVersion.name} for parameter ${parameterId}`
66+
);
67+
return parameterVersion;
68+
}
69+
70+
return await disableParamVersion();
71+
// [END parametermanager_disable_param_version]
72+
}
73+
module.exports.main = main;
74+
75+
/* c8 ignore next 10 */
76+
if (require.main === module) {
77+
main(...process.argv.slice(2)).catch(err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Enables a specific version of a parameter in Google Cloud Parameter Manager.
19+
* This function demonstrates how to enable a parameter version by setting
20+
* its 'disabled' field to false using the Parameter Manager client library.
21+
*
22+
* @param {string} projectId - The Google Cloud project ID where the parameter is located.
23+
* @param {string} parameterId - The ID of the parameter for which version is to be enabled.
24+
* @param {string} versionId - The version ID of the parameter to be enabled.
25+
*/
26+
async function main(projectId, parameterId, versionId) {
27+
// [START parametermanager_enable_param_version]
28+
/**
29+
* TODO(developer): Uncomment these variables before running the sample.
30+
*/
31+
// const projectId = 'my-project';
32+
// const parameterId = 'my-parameter';
33+
// const versionId = 'v1';
34+
35+
// Imports the Parameter Manager library
36+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
37+
38+
// Instantiates a client
39+
const client = new ParameterManagerClient();
40+
41+
async function enableParamVersion() {
42+
// Construct the full resource name
43+
const name = client.parameterVersionPath(
44+
projectId,
45+
'global',
46+
parameterId,
47+
versionId
48+
);
49+
50+
// Construct the request
51+
const request = {
52+
parameterVersion: {
53+
name: name,
54+
disabled: false,
55+
},
56+
updateMask: {
57+
paths: ['disabled'],
58+
},
59+
};
60+
61+
// Make the API call to update the parameter version
62+
const [parameterVersion] = await client.updateParameterVersion(request);
63+
64+
console.log(
65+
`Enabled parameter version ${parameterVersion.name} for parameter ${parameterId}`
66+
);
67+
return parameterVersion;
68+
}
69+
70+
return await enableParamVersion();
71+
// [END parametermanager_enable_param_version]
72+
}
73+
module.exports.main = main;
74+
75+
/* c8 ignore next 10 */
76+
if (require.main === module) {
77+
main(...process.argv.slice(2)).catch(err => {
78+
console.error(err.message);
79+
process.exitCode = 1;
80+
});
81+
process.on('unhandledRejection', err => {
82+
console.error(err.message);
83+
process.exitCode = 1;
84+
});
85+
}

0 commit comments

Comments
 (0)