Skip to content

Commit 70c2dc2

Browse files
jdomingrJuan Dominguez
andauthored
feat(genai): add text generation samples (#10144)
* refactor: remove unused imports * feat: add new GenAI text generation samples * feat: add new tests for text gen samples * refactor: update some samples * refactor: change textgen with pdf sample * fix controlled generation test case * refactor: update test for text gen samples * refactor: update text gen async output to make it consistent with other samples * refactor: change input order in textgen with pdf * refactor: change input order in some samples --------- Co-authored-by: Juan Dominguez <jdomin@softserveinc.com>
1 parent ffc0b86 commit 70c2dc2

File tree

9 files changed

+381
-7
lines changed

9 files changed

+381
-7
lines changed
384 KB
Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_async_with_txt]
20+
21+
import com.google.genai.Client;
22+
import com.google.genai.types.GenerateContentResponse;
23+
import com.google.genai.types.HttpOptions;
24+
import java.util.concurrent.CompletableFuture;
25+
26+
public class TextGenerationAsyncWithText {
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 asynchronously with text input
35+
public static String generateContent(String modelId) {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and 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+
CompletableFuture<GenerateContentResponse> asyncResponse =
46+
client.async.models.generateContent(
47+
modelId, "Compose a song about the adventures of a time-traveling squirrel.", null);
48+
49+
String response = asyncResponse.join().text();
50+
System.out.print(response);
51+
// Example response:
52+
// (Verse 1)
53+
// In an oak tree, so leafy and green,
54+
// Lived Squeaky the squirrel, a critter unseen.
55+
// Just burying nuts, a routine so grand,
56+
// ...
57+
58+
return response;
59+
}
60+
}
61+
}
62+
// [END googlegenaisdk_textgen_async_with_txt]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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_multi_local_img]
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 TextGenerationWithMultiLocalImage {
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+
String localImageFilePath1 = "your/local/img1.jpg";
36+
String localImageFilePath2 = "your/local/img2.jpg";
37+
generateContent(modelId, localImageFilePath1, localImageFilePath2);
38+
}
39+
40+
// Generates text using multiple local images
41+
public static String generateContent(
42+
String modelId, String localImageFilePath1, String localImageFilePath2) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
try (Client client =
46+
Client.builder()
47+
.location("global")
48+
.vertexAI(true)
49+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
50+
.build()) {
51+
52+
// Read content from local files.
53+
byte[] localFileImg1Bytes = Files.readAllBytes(Paths.get(localImageFilePath1));
54+
byte[] localFileImg2Bytes = Files.readAllBytes(Paths.get(localImageFilePath2));
55+
56+
GenerateContentResponse response =
57+
client.models.generateContent(
58+
modelId,
59+
Content.fromParts(
60+
Part.fromBytes(localFileImg1Bytes, "image/jpeg"),
61+
Part.fromBytes(localFileImg2Bytes, "image/jpeg"),
62+
Part.fromText("Generate a list of all the objects contained in both images")),
63+
null);
64+
65+
System.out.print(response.text());
66+
// Example response:
67+
// Based on both images, here are the objects contained in both:
68+
//
69+
// 1. **Coffee cups (or mugs)**: Both images feature one or more cups containing a beverage.
70+
// 2. **Coffee (or a similar beverage)**: Both images contain a liquid beverage in the cups,
71+
// appearing to be coffee or a coffee-like drink.
72+
// 3. **Table (or a flat surface)**: Both compositions are set on a flat surface, likely a
73+
// table or countertop.
74+
return response.text();
75+
}
76+
}
77+
}
78+
// [END googlegenaisdk_textgen_with_multi_local_img]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_mute_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+
27+
public class TextGenerationWithMuteVideo {
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 mute video input
36+
public static String generateContent(String modelId) {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
try (Client client =
40+
Client.builder()
41+
.location("global")
42+
.vertexAI(true)
43+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
44+
.build()) {
45+
46+
GenerateContentResponse response =
47+
client.models.generateContent(
48+
modelId,
49+
Content.fromParts(
50+
Part.fromUri(
51+
"gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4",
52+
"video/mp4"),
53+
Part.fromText("What is in this video?")),
54+
null);
55+
56+
System.out.print(response.text());
57+
// Example response:
58+
// This video features **surfers in the ocean**.
59+
//
60+
// The main focus is on **one individual who catches and rides a wave**, executing various
61+
// turns and maneuvers as the wave breaks and dissipates into whitewater...
62+
return response.text();
63+
}
64+
}
65+
}
66+
// [END googlegenaisdk_textgen_with_mute_video]
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_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 TextGenerationWithPdf {
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 PDF file input
36+
public static String generateContent(String modelId) {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
try (Client client =
40+
Client.builder()
41+
.location("global")
42+
.vertexAI(true)
43+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
44+
.build()) {
45+
46+
String prompt =
47+
"You are a highly skilled document summarization specialist.\n"
48+
+ " Your task is to provide a concise executive summary of no more than 300 words.\n"
49+
+ " Please summarize the given document for a general audience";
50+
51+
GenerateContentResponse response =
52+
client.models.generateContent(
53+
modelId,
54+
Content.fromParts(
55+
Part.fromUri(
56+
"gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf",
57+
"application/pdf"),
58+
Part.fromText(prompt)),
59+
null);
60+
61+
System.out.print(response.text());
62+
// Example response:
63+
// The document introduces the Transformer, a novel neural network architecture designed for
64+
// sequence transduction tasks, such as machine translation. Unlike previous dominant models
65+
// that rely on complex recurrent or convolutional neural networks, the Transformer proposes a
66+
// simpler, more parallelizable design based *solely* on attention mechanisms, entirely
67+
// dispensing with recurrence and convolutions...
68+
69+
return response.text();
70+
}
71+
}
72+
}
73+
// [END googlegenaisdk_textgen_with_pdf]

genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithText.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
// [START googlegenaisdk_textgen_with_txt]
2020

2121
import com.google.genai.Client;
22-
import com.google.genai.types.Content;
2322
import com.google.genai.types.GenerateContentResponse;
2423
import com.google.genai.types.HttpOptions;
25-
import com.google.genai.types.Part;
2624

2725
public class TextGenerationWithText {
2826

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_with_youtube_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+
27+
public class TextGenerationWithYoutubeVideo {
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 YouTube video input
36+
public static String generateContent(String modelId) {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
try (Client client =
40+
Client.builder()
41+
.location("global")
42+
.vertexAI(true)
43+
.httpOptions(HttpOptions.builder().apiVersion("v1").build())
44+
.build()) {
45+
46+
GenerateContentResponse response =
47+
client.models.generateContent(
48+
modelId,
49+
Content.fromParts(
50+
Part.fromUri("https://www.youtube.com/watch?v=3KtWfp0UopM", "video/mp4"),
51+
Part.fromText("Write a short and engaging blog post based on this video.")),
52+
null);
53+
54+
System.out.print(response.text());
55+
// Example response:
56+
// 25 Years of Curiosity: A Google Anniversary Dive into What the World Searched For
57+
//
58+
// Remember a time before instant answers were just a click away? 25 years ago, Google
59+
// launched, unleashing a wave of curiosity that has since charted the collective interests,
60+
// anxieties, and celebrations of humanity...
61+
return response.text();
62+
}
63+
}
64+
}
65+
// [END googlegenaisdk_textgen_with_youtube_video]

genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
import com.google.genai.types.Schema;
2727
import com.google.genai.types.Tool;
2828
import com.google.genai.types.Type;
29-
import java.util.List;
3029
import java.util.Map;
31-
import java.util.Objects;
32-
import java.util.Optional;
3330

3431
public class ToolFunctionDescriptionWithText {
3532

0 commit comments

Comments
 (0)