Skip to content

Commit b2f99d8

Browse files
authored
feat(genai): Add GenAI SDK samples (2) (#4094)
* feat(genai): Add GenAI SDK samples (2) * Add controlled generation example * Fix the region tag
1 parent f2da173 commit b2f99d8

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_ctrlgen_with_enum_schema]
18+
const {GoogleGenAI, Type} = 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 responseSchema = {
34+
type: Type.STRING,
35+
enum: ['Percussion', 'String', 'Woodwind', 'Brass', 'Keyboard'],
36+
};
37+
38+
const response = await ai.models.generateContent({
39+
model: 'gemini-2.0-flash',
40+
contents: 'What type of instrument is an oboe?',
41+
config: {
42+
responseMimeType: 'text/x.enum',
43+
responseSchema: responseSchema,
44+
},
45+
});
46+
47+
console.log(response.text);
48+
49+
return response.text;
50+
}
51+
// [END googlegenaisdk_ctrlgen_with_enum_schema]
52+
53+
module.exports = {
54+
generateContent,
55+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_counttoken_with_txt_vid]
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 countTokens(
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 video = {
34+
fileData: {
35+
fileUri: 'gs://cloud-samples-data/generative-ai/video/pixel8.mp4',
36+
mimeType: 'video/mp4',
37+
},
38+
};
39+
40+
const response = await ai.models.countTokens({
41+
model: 'gemini-2.0-flash',
42+
contents: [video, 'Provide a description of the video.'],
43+
});
44+
45+
console.log(response);
46+
47+
return response.totalTokens;
48+
}
49+
// [END googlegenaisdk_counttoken_with_txt_vid]
50+
51+
module.exports = {
52+
countTokens,
53+
};
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('../count-tokens/counttoken-with-txt-vid.js');
22+
23+
describe('counttoken-with-txt-vid', async () => {
24+
it('should return the total token count for a text and video prompt', async () => {
25+
const output = await sample.countTokens(projectId);
26+
assert(output > 0);
27+
});
28+
});
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('../controlled-generation/ctrlgen-with-enum-schema.js');
22+
23+
describe('ctrlgen-with-enum-schema', async () => {
24+
it('should generate text content in Json', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0 && output.includes('Woodwind'));
27+
});
28+
});

0 commit comments

Comments
 (0)