Skip to content

Commit c4eca46

Browse files
committed
Added java sample for text generation
1 parent d7b5834 commit c4eca46

18 files changed

+234
-114
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.vscode
22
node_modules
3+
java/target
4+
.idea

.idea/.gitignore

-3
This file was deleted.

.idea/api-examples-anandct.iml

-9
This file was deleted.

.idea/compiler.xml

-13
This file was deleted.

.idea/encodings.xml

-7
This file was deleted.

.idea/jarRepositories.xml

-20
This file was deleted.

.idea/misc.xml

-14
This file was deleted.

.idea/modules.xml

-8
This file was deleted.

.idea/vcs.xml

-6
This file was deleted.

java/pom.xml

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.google.gemini;
2+
3+
public class BuildConfig {
4+
public static String media_path = "third_party/";
5+
}

java/src/main/java/org/google/gemini/Main.java

-17
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 org.google.gemini;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.*;
22+
import org.apache.http.HttpException;
23+
24+
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Paths;
27+
import java.util.Base64;
28+
29+
import static org.google.gemini.BuildConfig.media_path;
30+
31+
public class TextGenMultimodalOneImagePrompt {
32+
public static void main(String[] args) throws IOException, HttpException {
33+
// [START text_gen_multimodal_one_image_prompt]
34+
Client client = new Client();
35+
36+
String path = media_path + "organ.jpg";
37+
byte[] imageData = Files.readAllBytes(Paths.get(path));
38+
String base64Image = Base64.getEncoder().encodeToString(imageData);
39+
Part imagePart = Part.builder()
40+
.inlineData(Blob.builder().data(base64Image)
41+
.mimeType("image/jpeg").build()).build();
42+
43+
Part textPart = Part.builder().text("Tell me about this instrument").build();
44+
45+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build();
46+
47+
GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null);
48+
49+
System.out.println(response.text());
50+
// [END text_gen_multimodal_one_image_prompt]
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.google.gemini;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import com.google.genai.Client;
5+
import com.google.genai.ResponseStream;
6+
import com.google.genai.types.Blob;
7+
import com.google.genai.types.Content;
8+
import com.google.genai.types.GenerateContentResponse;
9+
import com.google.genai.types.Part;
10+
import org.apache.http.HttpException;
11+
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
import java.util.Base64;
16+
17+
import static org.google.gemini.BuildConfig.media_path;
18+
19+
public class TextGenMultimodalOneImagePromptStreaming {
20+
public static void main(String[] args) throws IOException, HttpException {
21+
// [START text_gen_multimodal_one_image_prompt_streaming]
22+
Client client = new Client();
23+
24+
String path = media_path + "organ.jpg";
25+
byte[] imageData = Files.readAllBytes(Paths.get(path));
26+
String base64Image = Base64.getEncoder().encodeToString(imageData);
27+
Part imagePart = Part.builder()
28+
.inlineData(Blob.builder().data(base64Image)
29+
.mimeType("image/jpeg").build()).build();
30+
31+
Part textPart = Part.builder().text("Tell me about this instrument").build();
32+
33+
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build();
34+
35+
ResponseStream<GenerateContentResponse> responseStream =
36+
client.models.generateContentStream(
37+
"gemini-2.0-flash",
38+
content,
39+
null);
40+
41+
for (GenerateContentResponse res : responseStream) {
42+
System.out.print(res.text());
43+
}
44+
45+
responseStream.close();
46+
// [END text_gen_multimodal_one_image_prompt_streaming]
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 org.google.gemini;
18+
19+
import com.google.genai.Client;
20+
import com.google.genai.types.GenerateContentResponse;
21+
import org.apache.http.HttpException;
22+
23+
import java.io.IOException;
24+
25+
public class TextGenerationOnlyPrompt {
26+
public static void main(String[] args) throws IOException, HttpException {
27+
// [START text_gen_text_only_prompt]
28+
Client client = new Client();
29+
30+
GenerateContentResponse response =
31+
client.models.generateContent(
32+
"gemini-2.0-flash",
33+
"Write a story about a magic backpack.",
34+
null);
35+
36+
System.out.println(response.text());
37+
// [END text_gen_text_only_prompt]
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 org.google.gemini;
18+
19+
import com.google.genai.Client;
20+
import com.google.genai.ResponseStream;
21+
import com.google.genai.types.GenerateContentResponse;
22+
import org.apache.http.HttpException;
23+
24+
import java.io.IOException;
25+
26+
public class TextGenerationOnlyPromptStreaming {
27+
public static void main(String[] args) throws IOException, HttpException {
28+
// [START text_gen_text_only_prompt_streaming]
29+
Client client = new Client();
30+
31+
ResponseStream<GenerateContentResponse> responseStream =
32+
client.models.generateContentStream(
33+
"gemini-2.0-flash",
34+
"Write a story about a magic backpack.",
35+
null);
36+
37+
for (GenerateContentResponse res : responseStream) {
38+
System.out.print(res.text());
39+
}
40+
41+
responseStream.close();
42+
// [END text_gen_text_only_prompt_streaming]
43+
}
44+
45+
}
-1.14 KB
Binary file not shown.

pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.google.gemini</groupId>
8+
<artifactId>java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>24</maven.compiler.source>
13+
<maven.compiler.target>24</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.google.genai</groupId>
20+
<artifactId>google-genai</artifactId>
21+
<version>0.2.0</version>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-war-plugin</artifactId>
30+
<version>3.4.0</version>
31+
</plugin>
32+
</plugins>
33+
<resources>
34+
<resource>
35+
<directory>java/src/main/resources</directory>
36+
<includes>
37+
<include>.env</include>
38+
</includes>
39+
</resource>
40+
</resources>
41+
</build>
42+
43+
</project>

0 commit comments

Comments
 (0)