8000 feat(vertexai): add aiplatform gemini translate sample (#9525) · suztomo/java-docs-samples@3d1194b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d1194b

Browse files
feat(vertexai): add aiplatform gemini translate sample (GoogleCloudPlatform#9525)
* Implemented aiplatform_gemini_translate sample, created test * Fixed return * Fixed comment
1 parent 5cb4201 commit 3d1194b

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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]

vertexai/snippets/src/test/java/vertexai/gemini/SnippetsIT.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public class SnippetsIT {
5151
private static final String GEMINI_PRO = "gemini-1.5-pro-001";
5252
private static final String DATASTORE_ID = "grounding-test-datastore_1716831150046";
5353
private static final int MAX_ATTEMPT_COUNT = 3;
54-
private static final int INITIAL_BACKOFF_MILLIS = 120000; // 2 minutes
54+
private static final int INITIAL_BACKOFF_MILLIS = 120000;
55+
private static final String TARGET_LANGUAGE_CODE = "fr";
56+
private static final String TEXT_TO_TRANSLATE = "Hello! How are you doing today?";
57+
58+
59+
// 2 minutes
5560

5661
@Rule
5762
public final MultipleAttemptsRule multipleAttemptsRule =
@@ -494,4 +499,13 @@ public void testControlledGenerationWithJsonSchema6() throws Exception {
494499
assertThat(recognizedObjects).contains("passport");
495500
assertThat(recognizedObjects).contains("pot");
496501
}
502+
503+
@Test
504+
public void testGeminiTranslate() throws Exception {
505+
String output = GeminiTranslate.geminiTranslate(
506+
PROJECT_ID, LOCATION, GEMINI_PRO, TEXT_TO_TRANSLATE, TARGET_LANGUAGE_CODE);
507+
508+
assertThat(output).ignoringCase().contains("Bonjour");
509+
assertThat(output).ignoringCase().contains("aujourd'hui");
510+
}
497511
}

0 commit comments

Comments
 (0)
0