Skip to content

Commit 8ddf269

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add text generation samples - part 2 (#10155)
* feat: add new GenAI text generation with audio and local video samples * feat: add new GenAI text generation samples * change input order in some samples * refactor: change inputs order in textgen with local video samples * minor changes * update candidateCount config * add test cases for new samples * fix some comments * refactor: change input in textgen with local video and fix license header --------- Co-authored-by: Juan Dominguez <[email protected]>
1 parent 3d161a6 commit 8ddf269

File tree

6 files changed

+303
-9
lines changed

6 files changed

+303
-9
lines changed
1.75 MB
Binary file not shown.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 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 genai.textgeneration;
18+
19+
// [START googlegenaisdk_textgen_code_with_pdf]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
import com.google.genai.types.Part;
26+
27+
public class TextGenerationCodeWithPdf {
28+
29+
public static void main(String[] args) {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String modelId = "gemini-2.5-flash";
32+
generateContent(modelId);
33+
}
34+
35+
// Generates code with PDF file input
36+
public static String generateContent(String modelId) {
37+
// Client Initialization. Once created, it can be reused for multiple requests.
38+
try (Client client =
39+
Client.builder()
40+
.location("global")
41+
.vertexAI(true)
42+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
43+
.build()) {
44+
45+
// PDF file from GCS
46+
String fileUri =
47+
"gs://cloud-samples-data/generative-ai/text/inefficient_fibonacci_series_python_code.pdf";
48+
49+
GenerateContentResponse response =
50+
client.models.generateContent(
51+
modelId,
52+
Content.fromParts(
53+
Part.fromUri(fileUri, "application/pdf"),
54+
Part.fromText("Convert this python code to use Google Python Style Guide")),
55+
null);
56+
57+
System.out.print(response.text());
58+
// Example response:
59+
// def fibonacci_sequence(num_terms: int) -> list[int]:
60+
// """Calculates the Fibonacci sequence up to a specified number of terms...
61+
return response.text();
62+
}
63+
}
64+
}
65+
// [END googlegenaisdk_textgen_code_with_pdf]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2025 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 genai.textgeneration;
18+
19+
// [START googlegenaisdk_textgen_config_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.GenerateContentConfig;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
26+
public class TextGenerationConfigWithText {
27+
28+
public static void main(String[] args) {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String modelId = "gemini-2.5-flash";
31+
generateContent(modelId);
32+
}
33+
34+
// Generates text with text input and optional configurations
35+
public static String generateContent(String modelId) {
36+
// Client Initialization. Once created, it can be reused for multiple requests.
37+
try (Client client =
38+
Client.builder()
39+
.location("global")
40+
.vertexAI(true)
41+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
42+
.build()) {
43+
44+
// Set optional configuration parameters
45+
GenerateContentConfig contentConfig =
46+
GenerateContentConfig.builder()
47+
.temperature(0.0F)
48+
.candidateCount(1)
49+
.responseMimeType("application/json")
50+
.topP(0.95F)
51+
.topK(20F)
52+
.seed(5)
53+
.maxOutputTokens(500)
54+
.stopSequences("STOP!")
55+
.presencePenalty(0.0F)
56+
.frequencyPenalty(0.0F)
57+
.build();
58+
59+
// Generate content using optional configuration
60+
GenerateContentResponse response =
61+
client.models.generateContent(modelId, "Why is the sky blue?", contentConfig);
62+
63+
System.out.print(response.text());
64+
// Example response:
65+
// {
66+
// "explanation": "The sky appears blue due to a phenomenon called Rayleigh scattering.
67+
// Sunlight, which appears white, is actually composed of all the colors of the rainbow...
68+
// }
69+
return response.text();
70+
}
71+
}
72+
}
73+
// [END googlegenaisdk_textgen_config_with_txt]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 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 genai.textgeneration;
18+
19+
// [START googlegenaisdk_textgen_with_gcs_audio]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
import com.google.genai.types.Part;
26+
27+
public class TextGenerationWithGcsAudio {
28+
29+
public static void main(String[] args) {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String modelId = "gemini-2.5-flash";
32+
generateContent(modelId);
33+
}
34+
35+
// Generates text with audio input
36+
public static String generateContent(String modelId) {
37+
// Client Initialization. Once created, it can be reused for multiple requests.
38+
try (Client client =
39+
Client.builder()
40+
.location("global")
41+
.vertexAI(true)
42+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
43+
.build()) {
44+
45+
GenerateContentResponse response =
46+
client.models.generateContent(
47+
modelId,
48+
Content.fromParts(
49+
Part.fromUri(
50+
"gs://cloud-samples-data/generative-ai/audio/pixel.mp3", "audio/mpeg"),
51+
Part.fromText("Provide a concise summary of the main points in the audio file.")),
52+
null);
53+
54+
System.out.print(response.text());
55+
// Example response:
56+
// The audio features Google product managers Aisha Sharif and D. Carlos Love discussing Pixel
57+
// Feature Drops, emphasizing their role in continually enhancing devices across the entire
58+
// Pixel ecosystem...
59+
return response.text();
60+
}
61+
}
62+
}
63+
// [END googlegenaisdk_textgen_with_gcs_audio]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2025 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 genai.textgeneration;
18+
19+
// [START googlegenaisdk_textgen_with_local_video]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
import com.google.genai.types.Part;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
29+
30+
public class TextGenerationWithLocalVideo {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String modelId = "gemini-2.5-flash";
35+
generateContent(modelId);
36+
}
37+
38+
// Generates text with local video input
39+
public static String generateContent(String modelId) throws IOException {
40+
// Client Initialization. Once created, it can be reused for multiple requests.
41+
try (Client client =
42+
Client.builder()
43+
.location("global")
44+
.vertexAI(true)
45+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
46+
.build()) {
47+
48+
// Read content from the local video.
49+
byte[] videoData = Files.readAllBytes(Paths.get("resources/describe_video_content.mp4"));
50+
51+
GenerateContentResponse response =
52+
client.models.generateContent(
53+
modelId,
54+
Content.fromParts(
55+
Part.fromBytes(videoData, "video/mp4"),
56+
Part.fromText("Write a short and engaging blog post based on this video.")),
57+
null);
58+
59+
System.out.print(response.text());
60+
// Example response:
61+
// More Than Just a Climb: Finding Your Flow on the Wall
62+
// There's something captivating about watching a climber in their element. This short clip
63+
// offers a perfect glimpse into the focused world of indoor climbing, where precision meets
64+
// power...
65+
return response.text();
66+
}
67+
}
68+
}
69+
// [END googlegenaisdk_textgen_with_local_video]

genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,14 @@ public class TextGenerationIT {
3535
private static final String GEMINI_FLASH = "gemini-2.5-flash";
3636
private static final String LOCAL_IMG_1 = "resources/latte.jpg";
3737
private static final String LOCAL_IMG_2 = "resources/scones.jpg";
38-
3938
private ByteArrayOutputStream bout;
4039
private PrintStream out;
4140

4241
// Check if the required environment variables are set.
4342
public static void requireEnvVar(String envVarName) {
4443
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
45-
.that(System.getenv(envVarName))
46-
.isNotEmpty();
44+
.that(System.getenv(envVarName))
45+
.isNotEmpty();
4746
}
4847

4948
@BeforeClass
@@ -92,9 +91,9 @@ public void testTextGenerationWithTextAndImage() {
9291
public void testTextGenerationWithVideo() {
9392

9493
String prompt =
95-
" Analyze the provided video file, including its audio.\n"
96-
+ " Summarize the main points of the video concisely.\n"
97-
+ " Create a chapter breakdown with timestamps for key sections or topics discussed.";
94+
" Analyze the provided video file, including its audio.\n"
95+
+ " Summarize the main points of the video concisely.\n"
96+
+ " Create a chapter breakdown with timestamps for key sections or topics discussed.";
9897

9998
String response = TextGenerationWithVideo.generateContent(GEMINI_FLASH, prompt);
10099
assertThat(response).isNotEmpty();
@@ -106,10 +105,11 @@ public void testTextGenerationWithVideo() {
106105
public void testTextGenerationWithMultiImage() throws IOException {
107106

108107
String gcsFileImagePath = "gs://cloud-samples-data/generative-ai/image/scones.jpg";
108+
String localImageFilePath = "resources/latte.jpg";
109109

110110
String response =
111-
TextGenerationWithMultiImage.generateContent(
112-
GEMINI_FLASH, gcsFileImagePath, LOCAL_IMG_1);
111+
TextGenerationWithMultiImage.generateContent(
112+
GEMINI_FLASH, gcsFileImagePath, localImageFilePath);
113113

114114
assertThat(response).isNotEmpty();
115115
}
@@ -146,4 +146,28 @@ public void testTextGenerationWithYoutubeVideo() {
146146
String response = TextGenerationWithYoutubeVideo.generateContent(GEMINI_FLASH);
147147
assertThat(response).isNotEmpty();
148148
}
149-
}
149+
150+
@Test
151+
public void testTextGenerationCodeWithPdf() {
152+
String response = TextGenerationCodeWithPdf.generateContent(GEMINI_FLASH);
153+
assertThat(response).isNotEmpty();
154+
}
155+
156+
@Test
157+
public void testTextGenerationConfigWithText() {
158+
String response = TextGenerationConfigWithText.generateContent(GEMINI_FLASH);
159+
assertThat(response).isNotEmpty();
160+
}
161+
162+
@Test
163+
public void testTextGenerationWithGcsAudio() {
164+
String response = TextGenerationWithGcsAudio.generateContent(GEMINI_FLASH);
165+
assertThat(response).isNotEmpty();
166+
}
167+
168+
@Test
169+
public void testTextGenerationWithLocalVideo() throws IOException {
170+
String response = TextGenerationWithLocalVideo.generateContent(GEMINI_FLASH);
171+
assertThat(response).isNotEmpty();
172+
}
173+
}

0 commit comments

Comments
 (0)