|
| 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 | +}); |
0 commit comments