Skip to content

Commit 7ee95d2

Browse files
Merge branch 'main' into model-armor-floor-settings
2 parents 49f96b8 + 47ab9d6 commit 7ee95d2

17 files changed

+1249
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// [START googlegenaisdk_imggen_mmflash_with_txt]
18+
const fs = require('fs');
19+
const {GoogleGenAI, Modality} = require('@google/genai');
20+
21+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
22+
const GOOGLE_CLOUD_LOCATION =
23+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
24+
25+
async function generateContent(
26+
projectId = GOOGLE_CLOUD_PROJECT,
27+
location = GOOGLE_CLOUD_LOCATION
28+
) {
29+
const ai = new GoogleGenAI({
30+
vertexai: true,
31+
project: projectId,
32+
location: location,
33+
});
34+
35+
const response = await ai.models.generateContentStream({
36+
model: 'gemini-2.0-flash-exp',
37+
contents:
38+
'Generate an image of the Eiffel tower with fireworks in the background.',
39+
config: {
40+
responseModalities: [Modality.TEXT, Modality.IMAGE],
41+
},
42+
});
43+
44+
const generatedFileNames = [];
45+
let imageIndex = 0;
46+
for await (const chunk of response) {
47+
const text = chunk.text;
48+
const data = chunk.data;
49+
if (text) {
50+
console.debug(text);
51+
} else if (data) {
52+
const fileName = `generate_content_streaming_image_${imageIndex++}.png`;
53+
console.debug(`Writing response image to file: ${fileName}.`);
54+
try {
55+
fs.writeFileSync(fileName, data);
56+
generatedFileNames.push(fileName);
57+
} catch (error) {
58+
console.error(`Failed to write image file ${fileName}:`, error);
59+
}
60+
}
61+
}
62+
63+
return generatedFileNames;
64+
}
65+
// [END googlegenaisdk_imggen_mmflash_with_txt]
66+
67+
module.exports = {
68+
generateContent,
69+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../image-generation/imggen-mmflash-with-txt.js');
22+
23+
describe('imggen-mmflash-with-txt', async () => {
24+
it('should generate images from a text prompt', async () => {
25+
const generatedFileNames = await sample.generateContent(projectId);
26+
assert(Array.isArray(generatedFileNames));
27+
assert(generatedFileNames.length > 0);
28+
});
29+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../tools/tools-code-exec-with-txt.js');
22+
23+
describe('tools-code-exec-with-txt', async () => {
24+
it('should generate code and execution result', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0);
27+
});
28+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
// [START googlegenaisdk_tools_code_exec_with_txt]
18+
const {GoogleGenAI} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateContent(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const ai = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const response = await ai.models.generateContent({
34+
model: 'gemini-2.5-flash-preview-05-20',
35+
contents:
36+
'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.',
37+
config: {
38+
tools: [{codeExecution: {}}],
39+
temperature: 0,
40+
},
41+
});
42+
43+
console.debug(response.executableCode);
44+
console.debug(response.codeExecutionResult);
45+
46+
return response.codeExecutionResult;
47+
}
48+
// [END googlegenaisdk_tools_code_exec_with_txt]
49+
50+
module.exports = {
51+
generateContent,
52+
};

parametermanager/createParam.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
* Creates a global parameter using the Parameter Manager SDK.
19+
*
20+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be created.
21+
* @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project.
22+
*/
23+
async function main(projectId, parameterId) {
24+
// [START parametermanager_create_param]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const parameterId = 'YOUR_PARAMETER_ID';
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 createParam() {
38+
const parent = client.locationPath(projectId, 'global');
39+
const request = {
40+
parent: parent,
41+
parameterId: parameterId,
42+
};
43+
44+
const [parameter] = await client.createParameter(request);
45+
console.log(`Created parameter: ${parameter.name}`);
46+
return parameter;
47+
}
48+
49+
return await createParam();
50+
// [END parametermanager_create_param]
51+
}
52+
module.exports.main = main;
53+
54+
/* c8 ignore next 10 */
55+
if (require.main === module) {
56+
main(...process.argv.slice(2)).catch(err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* Creates a parameter version globally for unstructured data.
19+
*
20+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be created
21+
* @param {string} parameterId - The ID of the parameter for which the version is to be created.
22+
* @param {string} parameterVersionId - The ID of the parameter version to be created.
23+
* @param {string} payload - The unformatted string payload to be stored in the new parameter version.
24+
*/
25+
async function main(projectId, parameterId, parameterVersionId, payload) {
26+
// [START parametermanager_create_param_version]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const projectId = 'YOUR_PROJECT_ID';
31+
// const parameterId = 'YOUR_PARAMETER_ID';
32+
// const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID';
33+
// const payload = 'This is unstructured data';
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 createParamVersion() {
42+
// Construct the parent resource name
43+
const parent = client.parameterPath(projectId, 'global', parameterId);
44+
45+
// Construct the parameter version
46+
const parameterVersion = {
47+
payload: {
48+
data: Buffer.from(payload, 'utf8'),
49+
},
50+
};
51+
52+
// Construct the request
53+
const request = {
54+
parent: parent,
55+
parameterVersionId: parameterVersionId,
56+
parameterVersion: parameterVersion,
57+
};
58+
59+
// Create the parameter version
60+
const [paramVersion] = await client.createParameterVersion(request);
61+
console.log(`Created parameter version: ${paramVersion.name}`);
62+
return paramVersion;
63+
}
64+
65+
return await createParamVersion();
66+
// [END parametermanager_create_param_version]
67+
}
68+
module.exports.main = main;
69+
70+
/* c8 ignore next 10 */
71+
if (require.main === module) {
72+
main(...process.argv.slice(2)).catch(err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});
76+
process.on('unhandledRejection', err => {
77+
console.error(err.message);
78+
process.exitCode = 1;
79+
});
80+
}

0 commit comments

Comments
 (0)