Skip to content

Commit 5e004d2

Browse files
committed
feat(genai): Add samples for Model Optimizer
1 parent 03ab11d commit 5e004d2

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
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('../text-generation/textgen-with-txt-routing.js');
22+
23+
describe('textgen-with-txt-routing', async () => {
24+
it('should generate text content from a text prompt and with routing configuration', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0 && output.includes('AI'));
27+
});
28+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_textgen_with_txt_routing]
18+
const {GoogleGenAI, FeatureSelectionPreference} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION =
22+
process.env.GOOGLE_CLOUD_LOCATION || 'us-central1';
23+
24+
async function generateContent(
25+
projectId = GOOGLE_CLOUD_PROJECT,
26+
location = GOOGLE_CLOUD_LOCATION
27+
) {
28+
const ai = new GoogleGenAI({
29+
vertexai: true,
30+
project: projectId,
31+
location: location,
32+
});
33+
34+
const generateContentConfig = {
35+
modelSelectionConfig: {
36+
featureSelectionPreference: FeatureSelectionPreference.PRIORITIZE_COST,
37+
},
38+
};
39+
40+
const response = await ai.models.generateContent({
41+
model: 'model-optimizer-exp-04-09',
42+
contents: 'How does AI work?',
43+
config: generateContentConfig,
44+
});
45+
46+
console.log(response.text);
47+
48+
return response.text;
49+
}
50+
// [END googlegenaisdk_textgen_with_txt_routing]
51+
52+
module.exports = {
53+
generateContent,
54+
};

0 commit comments

Comments
 (0)