Skip to content

Commit 6779d28

Browse files
authored
Merge pull request #56 from anandCT/java-text-generation-multimodal-samples
Java text generation multimodal samples
2 parents e18897d + b439818 commit 6779d28

8 files changed

+478
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Blob;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
import org.apache.http.HttpException;
26+
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
30+
import java.util.Base64;
31+
32+
import static com.example.gemini.BuildConfig.media_path;
33+
34+
public class TextGenMultimodalAudio {
35+
public static void main(String[] args) throws IOException, HttpException {
36+
// [START text_gen_multimodal_audio]
37+
Client client = new Client();
38+
39+
String path = media_path + "sample.mp3";
40+
byte[] audioData = Files.readAllBytes(Paths.get(path));
41+
String audioBase64 = Base64.getEncoder().encodeToString(audioData);
42+
Part audioPart = Part.builder()
43+
.inlineData(Blob.builder().data(audioBase64)
44+
.mimeType("audio/mpeg").build()).build();
45+
46+
Part textPart = Part.builder().text("Give me a summary of this audio file.").build();
47+
48+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build();
49+
50+
GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);
51+
52+
System.out.println(response.text());
53+
// [END text_gen_multimodal_audio]
54+
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.ResponseStream;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Content;
24+
import com.google.genai.types.GenerateContentResponse;
25+
import com.google.genai.types.Part;
26+
import org.apache.http.HttpException;
27+
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Paths;
31+
import java.util.Base64;
32+
33+
import static com.example.gemini.BuildConfig.media_path;
34+
35+
public class TextGenMultimodalAudioStreaming {
36+
public static void main(String[] args) throws IOException, HttpException {
37+
// [START text_gen_multimodal_audio_streaming]
38+
Client client = new Client();
39+
40+
String path = media_path + "sample.mp3";
41+
byte[] audioData = Files.readAllBytes(Paths.get(path));
42+
String audioBase64 = Base64.getEncoder().encodeToString(audioData);
43+
Part audioPart = Part.builder()
44+
.inlineData(Blob.builder().data(audioBase64)
45+
.mimeType("audio/mpeg").build()).build();
46+
47+
Part textPart = Part.builder().text("Give me a summary of this audio file.").build();
48+
49+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build();
50+
51+
ResponseStream<GenerateContentResponse> responseStream =
52+
client.models.generateContentStream("gemini-2.0-flash", content, null);
53+
54+
for (GenerateContentResponse res : responseStream) {
55+
System.out.print(res.text());
56+
}
57+
58+
responseStream.close();
59+
// [END text_gen_multimodal_audio_streaming]
60+
}
61+
}
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Blob;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
import org.apache.http.HttpException;
26+
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
30+
import java.util.Base64;
31+
32+
import static com.example.gemini.BuildConfig.media_path;
33+
34+
public class TextGenMultimodalMultiImagePrompt {
35+
public static void main(String[] args) throws IOException, HttpException {
36+
// [START text_gen_multimodal_multi_image_prompt]
37+
Client client = new Client();
38+
39+
String organPath = media_path + "organ.jpg";
40+
byte[] organImageData = Files.readAllBytes(Paths.get(organPath));
41+
String organImageBase64 = Base64.getEncoder().encodeToString(organImageData);
42+
Part organImagePart = Part.builder()
43+
.inlineData(Blob.builder().data(organImageBase64)
44+
.mimeType("image/jpeg").build()).build();
45+
46+
String cajunPath = media_path + "Cajun_instruments.jpg";
47+
byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath));
48+
String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData);
49+
Part cajunImagePart = Part.builder()
50+
.inlineData(Blob.builder().data(cajunImageBase64)
51+
.mimeType("image/jpeg").build()).build();
52+
53+
Part textPart = Part.builder().text("What is the difference between both of these instruments?").build();
54+
55+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build();
56+
57+
GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);
58+
59+
System.out.println(response.text());
60+
// [END text_gen_multimodal_multi_image_prompt]
61+
}
62+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.ResponseStream;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Content;
24+
import com.google.genai.types.GenerateContentResponse;
25+
import com.google.genai.types.Part;
26+
import org.apache.http.HttpException;
27+
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Paths;
31+
import java.util.Base64;
32+
33+
import static com.example.gemini.BuildConfig.media_path;
34+
35+
public class TextGenMultimodalMultiImagePromptStreaming {
36+
public static void main(String[] args) throws IOException, HttpException {
37+
// [START text_gen_multimodal_multi_image_prompt_streaming]
38+
Client client = new Client();
39+
40+
String organPath = media_path + "organ.jpg";
41+
byte[] organImageData = Files.readAllBytes(Paths.get(organPath));
42+
String organBase64Image = Base64.getEncoder().encodeToString(organImageData);
43+
Part organImagePart = Part.builder()
44+
.inlineData(Blob.builder().data(organBase64Image)
45+
.mimeType("image/jpeg").build()).build();
46+
47+
String cajunPath = media_path + "Cajun_instruments.jpg";
48+
byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath));
49+
String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData);
50+
Part cajunImagePart = Part.builder()
51+
.inlineData(Blob.builder().data(cajunImageBase64)
52+
.mimeType("image/jpeg").build()).build();
53+
54+
Part textPart = Part.builder().text("What is the difference between both of these instruments?").build();
55+
56+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build();
57+
58+
ResponseStream<GenerateContentResponse> responseStream =
59+
client.models.generateContentStream("gemini-2.0-flash", content, null);
60+
61+
for (GenerateContentResponse res : responseStream) {
62+
System.out.print(res.text());
63+
}
64+
65+
responseStream.close();
66+
// [END text_gen_multimodal_multi_image_prompt_streaming]
67+
}
68+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Blob;
22+
import com.google.genai.types.Content;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
import org.apache.http.HttpException;
26+
27+
import java.io.IOException;
28+
import java.nio.file.Files;
29+
import java.nio.file.Paths;
30+
import java.util.Base64;
31+
32+
import static com.example.gemini.BuildConfig.media_path;
33+
34+
public class TextGenMultimodalPdf {
35+
public static void main(String[] args) throws IOException, HttpException {
36+
// [START text_gen_multimodal_pdf]
37+
Client client = new Client();
38+
39+
String path = media_path + "test.pdf";
40+
byte[] pdfData = Files.readAllBytes(Paths.get(path));
41+
String pdfBase64 = Base64.getEncoder().encodeToString(pdfData);
42+
Part pdfPart = Part.builder()
43+
.inlineData(Blob.builder().data(pdfBase64)
44+
.mimeType("application/pdf").build()).build();
45+
46+
Part textPart = Part.builder().text("Give me a summary of this document.").build();
47+
48+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build();
49+
50+
GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);
51+
52+
System.out.println(response.text());
53+
// [END text_gen_multimodal_pdf]
54+
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* https://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 com.example.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.ResponseStream;
22+
import com.google.genai.types.Blob;
23+
import com.google.genai.types.Content;
24+
import com.google.genai.types.GenerateContentResponse;
25+
import com.google.genai.types.Part;
26+
import org.apache.http.HttpException;
27+
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Paths;
31+
import java.util.Base64;
32+
33+
import static com.example.gemini.BuildConfig.media_path;
34+
35+
public class TextGenMultimodalPdfStreaming {
36+
public static void main(String[] args) throws IOException, HttpException {
37+
// [START text_gen_multimodal_pdf_streaming]
38+
Client client = new Client();
39+
40+
String path = media_path + "test.pdf";
41+
byte[] pdfData = Files.readAllBytes(Paths.get(path));
42+
String pdfBase64 = Base64.getEncoder().encodeToString(pdfData);
43+
Part pdfPart = Part.builder()
44+
.inlineData(Blob.builder().data(pdfBase64)
45+
.mimeType("application/pdf").build()).build();
46+
47+
Part textPart = Part.builder().text("Give me a summary of this document.").build();
48+
49+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build();
50+
51+
ResponseStream<GenerateContentResponse> responseStream =
52+
client.models.generateContentStream("gemini-2.0-flash", content, null);
53+
54+
for (GenerateContentResponse res : responseStream) {
55+
System.out.print(res.text());
56+
}
57+
58+
responseStream.close();
59+
// [END text_gen_multimodal_pdf_streaming]
60+
}
61+
}

0 commit comments

Comments
 (0)