Skip to content

Commit 232f480

Browse files
gryczjsubfuzion
andauthored
feat: generativeaionvertexai_gemini_translate (#3887)
* feat: generativeaionvertexai_gemini_translate * Fix for MultiModal samples tests --------- Co-authored-by: Tony Pujals <[email protected]>
1 parent d5ba43a commit 232f480

File tree

5 files changed

+129
-3
lines changed

5 files changed

+129
-3
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2024 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+
async function geminiTranslation(projectId) {
18+
// [START generativeaionvertexai_gemini_translate]
19+
const {
20+
VertexAI,
21+
HarmCategory,
22+
HarmBlockThreshold,
23+
} = require('@google-cloud/vertexai');
24+
/**
25+
* TODO(developer): Update/uncomment these variables before running the sample.
26+
*/
27+
// projectId = 'your-project-id';
28+
const location = 'us-central1';
29+
const modelName = 'gemini-1.0-pro';
30+
// The text to be translated.
31+
const text = 'Hello! How are you doing today?';
32+
// The language code of the target language. Defaults to "fr" (*French).
33+
// Available language codes:
34+
// https://cloud.google.com/translate/docs/languages#neural_machine_translation_model
35+
const targetLanguageCode = 'fr';
36+
37+
const generationConfig = {
38+
maxOutputTokens: 2048,
39+
temperature: 0.4,
40+
topP: 1,
41+
topK: 32,
42+
};
43+
44+
const safetySettings = [
45+
{
46+
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
47+
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
48+
},
49+
{
50+
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
51+
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
52+
},
53+
{
54+
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
55+
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
56+
},
57+
{
58+
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
59+
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
60+
},
61+
];
62+
63+
const content = `Your mission is to translate text in English to ${targetLanguageCode}`;
64+
65+
const vertexAI = new VertexAI({project: projectId, location});
66+
// Instantiate models
67+
const generativeModel = vertexAI.getGenerativeModel({
68+
model: modelName,
69+
safetySettings,
70+
generationConfig,
71+
systemInstruction: {
72+
parts: [{text: content}],
73+
},
74+
});
75+
76+
const textPart = {
77+
text: `
78+
User input:${text}
79+
Answer:`,
80+
};
81+
82+
const request = {
83+
contents: [{role: 'user', parts: [textPart]}],
84+
};
85+
86+
const result = await generativeModel.generateContent(request);
87+
const contentResponse = await result.response;
88+
console.log(JSON.stringify(contentResponse));
89+
return contentResponse;
90+
// [END generativeaionvertexai_gemini_translate]
91+
}
92+
93+
geminiTranslation(...process.argv.slice(2)).catch(err => {
94+
console.error(err.message);
95+
process.exitCode = 1;
96+
});

generative-ai/snippets/safetySettings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function setSafetySettings() {
3535
model: MODEL,
3636
// The following parameters are optional
3737
// They can also be passed to individual content generation requests
38-
safety_settings: [
38+
safetySettings: [
3939
{
4040
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
4141
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 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('node:assert/strict');
18+
const {describe, it} = require('mocha');
19+
const cp = require('child_process');
20+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
21+
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
24+
describe('Gemini translate', () => {
25+
it('should translate text', async () => {
26+
const response = execSync(`node ./gemini-translate.js ${projectId}`);
27+
28+
assert(JSON.stringify(response).match(/Bonjour/));
29+
});
30+
});

generative-ai/snippets/test/sendMultiModalPromptWithImage.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const {assert} = require('chai');
17+
const assert = require('node:assert/strict');
1818
const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

generative-ai/snippets/test/sendMultiModalPromptWithVideo.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const {assert} = require('chai');
17+
const assert = require('node:assert/strict');
1818
const {describe, it} = require('mocha');
1919
const cp = require('child_process');
2020
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

0 commit comments

Comments
 (0)