|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package vertexai.gemini; |
| 18 | + |
| 19 | +// [START aiplatform_gemini_translate] |
| 20 | + |
| 21 | +import com.google.cloud.vertexai.VertexAI; |
| 22 | +import com.google.cloud.vertexai.api.GenerateContentResponse; |
| 23 | +import com.google.cloud.vertexai.api.GenerationConfig; |
| 24 | +import com.google.cloud.vertexai.api.HarmCategory; |
| 25 | +import com.google.cloud.vertexai.api.SafetySetting; |
| 26 | +import com.google.cloud.vertexai.generativeai.ContentMaker; |
| 27 | +import com.google.cloud.vertexai.generativeai.GenerativeModel; |
| 28 | +import com.google.cloud.vertexai.generativeai.ResponseHandler; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class GeminiTranslate { |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String projectId = "your-google-cloud-project-id"; |
| 37 | + String location = "us-central1"; |
| 38 | + String modelName = "gemini-1.0-pro"; |
| 39 | + // The text to be translated. |
| 40 | + String text = "Hello! How are you doing today?
10000
"; |
| 41 | + // The language code of the target language. Defaults to "fr" (French). |
| 42 | + // Available language codes: |
| 43 | + // https://cloud.google.com/translate/docs/languages#neural_machine_translation_model |
| 44 | + String targetLanguageCode = "fr"; |
| 45 | + |
| 46 | + String output = geminiTranslate(projectId, location, modelName, text, targetLanguageCode); |
| 47 | + System.out.println(output); |
| 48 | + } |
| 49 | + |
| 50 | + // Translates the given text to the specified target language using the Gemini model. |
| 51 | + // The response from the model containing the translated text. |
| 52 | + public static String geminiTranslate( |
| 53 | + String projectId, String location, String modelName, String text, String targetLanguageCode) |
| 54 | + throws IOException { |
| 55 | + |
| 56 | + List<SafetySetting> safetySettings = Arrays.asList( |
| 57 | + SafetySetting.newBuilder() |
| 58 | + .setCategory(HarmCategory.HARM_CATEGORY_HATE_SPEECH) |
| 59 | + .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE) |
| 60 | + .build(), |
| 61 | + SafetySetting.newBuilder() |
| 62 | + .setCategory(HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT) |
| 63 | + .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE) |
| 64 | + .build(), |
| 65 | + SafetySetting.newBuilder() |
| 66 | + .setCategory(HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT) |
| 67 | + .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE) |
| 68 | + .build(), |
| 69 | + SafetySetting.newBuilder() |
| 70 | + .setCategory(HarmCategory.HARM_CATEGORY_HARASSMENT) |
| 71 | + .setThreshold(SafetySetting.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE) |
| 72 | + .build() |
| 73 | + ); |
| 74 | + GenerationConfig generationConfig = |
| 75 | + GenerationConfig.newBuilder() |
| 76 | + .setMaxOutputTokens(2048) |
| 77 | + .setTemperature(0.4F) |
| 78 | + .setTopK(32) |
| 79 | + .setTopP(1) |
| 80 | + .build(); |
| 81 | + String question = String.format( |
| 82 | + "Your mission is to translate text in English to %s.", targetLanguageCode); |
| 83 | + // Initialize client that will be used to send requests. This client only needs |
| 84 | + // to be created once, and can be reused for multiple requests. |
| 85 | + try (VertexAI vertexAI = new VertexAI(projectId, location)) { |
| 86 | + GenerativeModel model = new GenerativeModel(modelName, vertexAI) |
| 87 | + .withGenerationConfig(generationConfig) |
| 88 | + .withSafetySettings(safetySettings) |
| 89 | + .withSystemInstruction(ContentMaker.fromString(question)); |
| 90 | + |
| 91 | + GenerateContentResponse response = model.generateContent(text); |
| 92 | + return ResponseHandler.getText(response); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | +// [END aiplatform_gemini_translate] |
0 commit comments