Skip to content

Commit 16520ae

Browse files
authored
Merge pull request #83 from anandmali/java-sdk-update-0.4.0-genai-api-changes
Java sdk update 0.4.0 genai api changes
2 parents 4444c62 + 23dc1a0 commit 16520ae

File tree

11 files changed

+163
-115
lines changed

11 files changed

+163
-115
lines changed

java/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This directory contains examples of working with the Gemini API using the
44
[Google Gen AI SDK for Java](https://github.com/googleapis/java-genai).
55

66
## Install dependencies
7-
open the project in intellliJ IDEA, load the maven build script.
7+
open the project in intelliJ IDEA, load the maven build script.
88

99
## Run a test file
1010
Before running the tests, set GOOGLE_API_KEY as environment variable for run/debug configuration.

java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>com.google.genai</groupId>
2020
<artifactId>google-genai</artifactId>
21-
<version>0.2.0</version>
21+
<version>0.4.0</version>
2222
</dependency>
2323

2424
<dependency>
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.genai.Chat;
20+
import com.google.genai.Client;
21+
import com.google.genai.types.Content;
22+
import com.google.genai.types.GenerateContentConfig;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.Part;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
public class ChatSession {
30+
public static List<GenerateContentResponse> chat() {
31+
// [START chat]
32+
Client client = new Client();
33+
34+
Content userContent = Content.fromParts(Part.fromText("Hello"));
35+
Content modelContent =
36+
Content.builder()
37+
.role("model")
38+
.parts(
39+
Collections.singletonList(
40+
Part.fromText("Great to meet you. What would you like to know?")
41+
)
42+
).build();
43+
44+
Chat chat = client.chats.create(
45+
"gemini-2.0-flash",
46+
GenerateContentConfig.builder()
47+
.systemInstruction(userContent)
48+
.systemInstruction(modelContent)
49+
.build()
50+
);
51+
52+
GenerateContentResponse response1 = chat.sendMessage("I have 2 dogs in my house.");
53+
System.out.println(response1.text());
54+
55+
GenerateContentResponse response2 = chat.sendMessage("How many paws are in my house?");
56+
System.out.println(response2.text());
57+
58+
// [END chat]
59+
return List.of(response1, response2);
60+
}
61+
}

java/src/main/java/com/example/gemini/CodeExecution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
@SuppressWarnings("resource")
2929
public class CodeExecution {
30-
public static GenerateContentResponse codeExecutionBasic() throws Exception {
30+
public static GenerateContentResponse codeExecutionBasic() {
3131
// [START code_execution_basic]
3232
Client client = new Client();
3333

@@ -94,7 +94,7 @@ def is_prime(n):
9494
return response;
9595
}
9696

97-
public static GenerateContentResponse codeExecutionRequestOverride() throws Exception {
97+
public static GenerateContentResponse codeExecutionRequestOverride() {
9898
// [START code_execution_request_override]
9999
Client client = new Client();
100100

java/src/main/java/com/example/gemini/ConfigureModelParameters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import java.util.List;
2525

2626
public class ConfigureModelParameters {
27-
public static @Nullable String configureModelParameters() throws Exception {
27+
public static @Nullable String configureModelParameters() {
2828
// [START configure_model_parameters]
2929
Client client = new Client();
3030

java/src/main/java/com/example/gemini/ControlledGeneration.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package com.example.gemini;
1818

19-
import com.google.common.collect.ImmutableList;
2019
import com.google.genai.Client;
21-
import com.google.genai.types.Blob;
2220
import com.google.genai.types.Content;
2321
import com.google.genai.types.GenerateContentConfig;
2422
import com.google.genai.types.GenerateContentResponse;
@@ -30,14 +28,13 @@
3028
import java.nio.file.Files;
3129
import java.nio.file.Paths;
3230
import java.util.Arrays;
33-
import java.util.Base64;
3431
import java.util.List;
3532
import java.util.Map;
3633

3734
import static com.example.gemini.BuildConfig.media_path;
3835

3936
public class ControlledGeneration {
40-
public static @Nullable String jsonControlledGeneration() throws Exception {
37+
public static @Nullable String jsonControlledGeneration() {
4138
// [START json_controlled_generation]
4239
Client client = new Client();
4340

@@ -77,7 +74,7 @@ public class ControlledGeneration {
7774
return response.text();
7875
}
7976

80-
public static @Nullable String jsonNoSchema() throws Exception {
77+
public static @Nullable String jsonNoSchema() {
8178
// [START json_no_schema]
8279
Client client = new Client();
8380

@@ -116,14 +113,11 @@ public class ControlledGeneration {
116113

117114
String path = media_path + "organ.jpg";
118115
byte[] imageData = Files.readAllBytes(Paths.get(path));
119-
String base64Image = Base64.getEncoder().encodeToString(imageData);
120-
Part imagePart = Part.builder()
121-
.inlineData(Blob.builder().data(base64Image)
122-
.mimeType("image/jpeg").build()).build();
123116

124-
Part textPart = Part.builder().text("What kind of instrument is this:").build();
125-
126-
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build();
117+
Content content =
118+
Content.fromParts(
119+
Part.fromText("What kind of instrument is this:"),
120+
Part.fromBytes(imageData, "image/jpeg"));
127121

128122
GenerateContentConfig config =
129123
GenerateContentConfig.builder()
@@ -193,14 +187,11 @@ public class ControlledGeneration {
193187

194188
String path = media_path + "organ.jpg";
195189
byte[] imageData = Files.readAllBytes(Paths.get(path));
196-
String base64Image = Base64.getEncoder().encodeToString(imageData);
197-
Part imagePart = Part.builder()
198-
.inlineData(Blob.builder().data(base64Image)
199-
.mimeType("image/jpeg").build()).build();
200-
201-
Part textPart = Part.builder().text("What kind of instrument is this:").build();
202190

203-
Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build();
191+
Content content =
192+
Content.fromParts(
193+
Part.fromText("What kind of instrument is this:"),
194+
Part.fromBytes(imageData, "image/jpeg"));
204195

205196
GenerateContentConfig config =
206197
GenerateContentConfig.builder()

java/src/main/java/com/example/gemini/FunctionCalling.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.function.BiFunction;
3434

3535
public class FunctionCalling {
36-
public static Double functionCalling() throws Exception {
36+
public static Double functionCalling() {
3737
// [START function_calling]
3838
Client client = new Client();
3939

@@ -108,12 +108,12 @@ public static Double functionCalling() throws Exception {
108108
)
109109
.build();
110110

111-
GenerateContentResponse response;
112-
try {
113-
response = client.models.generateContent("gemini-2.0-flash", "I have 57 cats, each owns 44 mittens, how many mittens is that in total?", config);
114-
} catch (HttpException e) {
115-
throw new RuntimeException(e);
116-
}
111+
GenerateContentResponse response =
112+
client.models.generateContent(
113+
"gemini-2.0-flash",
114+
"I have 57 cats, each owns 44 mittens, how many mittens is that in total?",
115+
config);
116+
117117

118118
if (response.functionCalls() == null || response.functionCalls().isEmpty()) {
119119
System.err.println("No function call received");

java/src/main/java/com/example/gemini/SafetySettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.Collections;
2626

2727
public class SafetySettings {
28-
public static GenerateContentResponse safetySettings() throws Exception {
28+
public static GenerateContentResponse safetySettings() {
2929
// [START safety_settings]
3030
Client client = new Client();
3131

java/src/main/java/com/example/gemini/SystemInstruction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.jspecify.annotations.Nullable;
2626

2727
public class SystemInstruction {
28-
public static @Nullable String systemInstruction() throws Exception {
28+
public static @Nullable String systemInstruction() {
2929
// [START system_instruction]
3030
Client client = new Client();
3131

0 commit comments

Comments
 (0)