From c8295c53eb0c96a9ddc0ed79f7d45cd619e49f3c Mon Sep 17 00:00:00 2001 From: anandmali Date: Mon, 7 Apr 2025 17:57:07 +0530 Subject: [PATCH 1/3] Add tests for Java examples --- java/pom.xml | 7 + .../java/com/example/gemini/BuildConfig.java | 2 +- ...equestOverride.java => CodeExecution.java} | 85 ++++- .../example/gemini/CodeExecutionBasic.java | 92 ----- .../gemini/ConfigureModelParameters.java | 6 +- .../example/gemini/ControlledGeneration.java | 222 ++++++++++++ .../java/com/example/gemini/EnumInJson.java | 66 ---- .../com/example/gemini/FunctionCalling.java | 16 +- .../gemini/JasonControlledGeneration.java | 67 ---- .../java/com/example/gemini/JsonEnum.java | 70 ---- .../java/com/example/gemini/JsonNoSchema.java | 51 --- .../com/example/gemini/SafetySettings.java | 45 ++- .../example/gemini/SafetySettingsMulti.java | 66 ---- .../com/example/gemini/SystemInstruction.java | 7 +- .../gemini/TextGenMultimodalAudio.java | 55 --- .../TextGenMultimodalAudioStreaming.java | 61 ---- .../TextGenMultimodalMultiImagePrompt.java | 62 ---- ...enMultimodalMultiImagePromptStreaming.java | 68 ---- .../TextGenMultimodalOneImagePrompt.java | 52 --- ...tGenMultimodalOneImagePromptStreaming.java | 64 ---- .../example/gemini/TextGenMultimodalPdf.java | 55 --- .../gemini/TextGenMultimodalPdfStreaming.java | 61 ---- .../gemini/TextGenMultimodalVideoPrompt.java | 55 --- ...TextGenMultimodalVideoPromptStreaming.java | 61 ---- .../example/gemini/TextGenTextOnlyPrompt.java | 39 -- .../TextGenTextOnlyPromptStreaming.java | 45 --- .../com/example/gemini/TextGeneration.java | 343 ++++++++++++++++++ .../main/java/com/example/gemini/XEnum.java | 70 ---- .../com/example/gemini/CodeExecutionTest.java | 56 +++ .../gemini/ConfigureModelParametersTest.java | 34 ++ .../gemini/ControlledGenerationTest.java | 72 ++++ .../example/gemini/FunctionCallingTest.java | 32 ++ .../example/gemini/SafetySettingsTest.java | 56 +++ .../example/gemini/SystemInstructionTest.java | 34 ++ .../example/gemini/TextGenerationTest.java | 134 +++++++ 35 files changed, 1127 insertions(+), 1184 deletions(-) rename java/src/main/java/com/example/gemini/{CodeExecutionRequestOverride.java => CodeExecution.java} (54%) delete mode 100644 java/src/main/java/com/example/gemini/CodeExecutionBasic.java create mode 100644 java/src/main/java/com/example/gemini/ControlledGeneration.java delete mode 100644 java/src/main/java/com/example/gemini/EnumInJson.java delete mode 100644 java/src/main/java/com/example/gemini/JasonControlledGeneration.java delete mode 100644 java/src/main/java/com/example/gemini/JsonEnum.java delete mode 100644 java/src/main/java/com/example/gemini/JsonNoSchema.java delete mode 100644 java/src/main/java/com/example/gemini/SafetySettingsMulti.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalAudio.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalAudioStreaming.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePrompt.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePromptStreaming.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePrompt.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePromptStreaming.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalPdf.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalPdfStreaming.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalVideoPrompt.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenMultimodalVideoPromptStreaming.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenTextOnlyPrompt.java delete mode 100644 java/src/main/java/com/example/gemini/TextGenTextOnlyPromptStreaming.java create mode 100644 java/src/main/java/com/example/gemini/TextGeneration.java delete mode 100644 java/src/main/java/com/example/gemini/XEnum.java create mode 100644 java/src/test/java/com/example/gemini/CodeExecutionTest.java create mode 100644 java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java create mode 100644 java/src/test/java/com/example/gemini/ControlledGenerationTest.java create mode 100644 java/src/test/java/com/example/gemini/FunctionCallingTest.java create mode 100644 java/src/test/java/com/example/gemini/SafetySettingsTest.java create mode 100644 java/src/test/java/com/example/gemini/SystemInstructionTest.java create mode 100644 java/src/test/java/com/example/gemini/TextGenerationTest.java diff --git a/java/pom.xml b/java/pom.xml index 25a1efc..0769632 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -20,6 +20,13 @@ google-genai 0.2.0 + + + org.junit.jupiter + junit-jupiter-engine + 5.9.1 + test + diff --git a/java/src/main/java/com/example/gemini/BuildConfig.java b/java/src/main/java/com/example/gemini/BuildConfig.java index 1a6f443..6c48fcd 100644 --- a/java/src/main/java/com/example/gemini/BuildConfig.java +++ b/java/src/main/java/com/example/gemini/BuildConfig.java @@ -17,5 +17,5 @@ package com.example.gemini; public class BuildConfig { - public static String media_path = "third_party/"; + public static String media_path = "../third_party/"; } diff --git a/java/src/main/java/com/example/gemini/CodeExecutionRequestOverride.java b/java/src/main/java/com/example/gemini/CodeExecution.java similarity index 54% rename from java/src/main/java/com/example/gemini/CodeExecutionRequestOverride.java rename to java/src/main/java/com/example/gemini/CodeExecution.java index a2e1cca..e298e8d 100644 --- a/java/src/main/java/com/example/gemini/CodeExecutionRequestOverride.java +++ b/java/src/main/java/com/example/gemini/CodeExecution.java @@ -17,14 +17,84 @@ package com.example.gemini; import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.Part; +import com.google.genai.types.Tool; +import com.google.genai.types.ToolCodeExecution; -import java.io.IOException; import java.util.Collections; -public class CodeExecutionRequestOverride { - public static void main(String[] args) throws IOException, HttpException { +@SuppressWarnings("resource") +public class CodeExecution { + public static GenerateContentResponse CodeExecutionBasic() throws Exception { + // [START code_execution_basic] + Client client = new Client(); + + String prompt = """ + Write and execute code that calculates the sum of the first 50 prime numbers. + Ensure that only the executable code and its resulting output are generated. + """; + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-pro-exp-02-05", + prompt, + null); + + for (Part part : response.candidates().get().getFirst().content().get().parts().get()) { + System.out.println(part + "\n"); + } + + System.out.println("-".repeat(80)); + System.out.println(response.text()); + // [END code_execution_basic] + + /* + [START code_execution_basic_return] + Expected output: + -------------------------------------------------------------------------------- + ```python + import math + + def is_prime(n): + """Checks if a number is prime.""" + if n < 2: + return False + if n == 2: + return True + if n % 2 == 0: + return False + # Check only odd divisors up to the square root + for i in range(3, int(math.sqrt(n)) + 1, 2): + if n % i == 0: + return False + return True + + count = 0 + num = 2 + prime_sum = 0 + target_count = 50 + + while count < target_count: + if is_prime(num): + prime_sum += num + count += 1 + num += 1 + + print(prime_sum) + ``` + + Output: + ``` + 5117 + ``` + [END code_execution_basic_return] + */ + return response; + } + + public static GenerateContentResponse CodeExecutionRequestOverride() throws Exception { // [START code_execution_request_override] Client client = new Client(); @@ -42,13 +112,13 @@ public static void main(String[] args) throws IOException, HttpException { ) ).build(); - GenerateContentResponse response = client.models.generateContent( "gemini-2.0-flash", prompt, config); + System.out.println("-".repeat(80)); System.out.println(response.executableCode()); System.out.println(response.codeExecutionResult()); // [END code_execution_request_override] @@ -57,7 +127,6 @@ public static void main(String[] args) throws IOException, HttpException { [START code_execution_request_override_return] Expected output: -------------------------------------------------------------------------------- - def is_prime(n): if n <= 1: return False @@ -84,8 +153,8 @@ def is_prime(n): primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229] sum(primes)=5117 - [END code_execution_request_override_return] */ + return response; } } diff --git a/java/src/main/java/com/example/gemini/CodeExecutionBasic.java b/java/src/main/java/com/example/gemini/CodeExecutionBasic.java deleted file mode 100644 index 2514aaa..0000000 --- a/java/src/main/java/com/example/gemini/CodeExecutionBasic.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; - -public class CodeExecutionBasic { - public static void main(String[] args) throws IOException, HttpException { - // [START code_execution_basic] - Client client = new Client(); - - String prompt = """ - Write and execute code that calculates the sum of the first 50 prime numbers. - Ensure that only the executable code and its resulting output are generated. - """; - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-pro-exp-02-05", - prompt, - null); - - for (Part part : response.candidates().get().getFirst().content().get().parts().get()) { - System.out.println(part + "\n"); - } - - System.out.println(response.text()); - // [END code_execution_basic] - - /* - [START code_execution_basic_return] - Expected output: - -------------------------------------------------------------------------------- - - ```python - import math - - def is_prime(n): - """Checks if a number is prime.""" - if n < 2: - return False - if n == 2: - return True - if n % 2 == 0: - return False - # Check only odd divisors up to the square root - for i in range(3, int(math.sqrt(n)) + 1, 2): - if n % i == 0: - return False - return True - - count = 0 - num = 2 - prime_sum = 0 - target_count = 50 - - while count < target_count: - if is_prime(num): - prime_sum += num - count += 1 - num += 1 - - print(prime_sum) - ``` - - Output: - ``` - 5117 - ``` - [END code_execution_basic_return] - */ - } -} diff --git a/java/src/main/java/com/example/gemini/ConfigureModelParameters.java b/java/src/main/java/com/example/gemini/ConfigureModelParameters.java index 5c0e29c..a98b478 100644 --- a/java/src/main/java/com/example/gemini/ConfigureModelParameters.java +++ b/java/src/main/java/com/example/gemini/ConfigureModelParameters.java @@ -19,13 +19,12 @@ import com.google.genai.Client; import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.GenerateContentResponse; -import org.apache.http.HttpException; +import org.jspecify.annotations.Nullable; -import java.io.IOException; import java.util.List; public class ConfigureModelParameters { - public static void main(String[] args) throws IOException, HttpException { + public static @Nullable String ContentGneConfigureModelParameters() throws Exception { // [START configure_model_parameters] Client client = new Client(); @@ -45,5 +44,6 @@ public static void main(String[] args) throws IOException, HttpException { System.out.println(response.text()); // [END configure_model_parameters] + return response.text(); } } diff --git a/java/src/main/java/com/example/gemini/ControlledGeneration.java b/java/src/main/java/com/example/gemini/ControlledGeneration.java new file mode 100644 index 0000000..167a798 --- /dev/null +++ b/java/src/main/java/com/example/gemini/ControlledGeneration.java @@ -0,0 +1,222 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import com.google.common.collect.ImmutableList; +import com.google.genai.Client; +import com.google.genai.types.Blob; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.Part; +import com.google.genai.types.Schema; +import org.jspecify.annotations.Nullable; + +import java.lang.reflect.Array; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Base64; +import java.util.List; +import java.util.Map; + +import static com.example.gemini.BuildConfig.media_path; + +public class ControlledGeneration { + public static @Nullable String JsonControlledGeneration() throws Exception { + // [START json_controlled_generation] + Client client = new Client(); + + Schema recipeSchema = Schema.builder() + .type(Array.class.getSimpleName()) + .items(Schema.builder() + .type(Object.class.getSimpleName()) + .properties( + Map.of("recipe_name", Schema.builder() + .type(String.class.getSimpleName()) + .build(), + "ingredients", Schema.builder() + .type(Array.class.getSimpleName()) + .items(Schema.builder() + .type(String.class.getSimpleName()) + .build()) + .build()) + ) + .required(List.of("recipe_name", "ingredients")) + .build()) + .build(); + + GenerateContentConfig config = + GenerateContentConfig.builder() + .responseMimeType("application/json") + .responseSchema(recipeSchema) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + "List a few popular cookie recipes.", + config); + + System.out.println(response.text()); + // [END json_controlled_generation] + return response.text(); + } + + public static @Nullable String JsonNoSchema() throws Exception { + // [START json_no_schema] + Client client = new Client(); + + String prompt = """ + List a few popular cookie recipes in JSON format. + + + Use this JSON schema: + + + Recipe = {'recipe_name': string, 'ingredients': list[string]} + + Return: list[Recipe]. + """; + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + prompt, + null); + + System.out.println(response.text()); + // [END json_no_schema] + return response.text(); + } + + public static @Nullable String JsonEnum() throws Exception { + // [START json_enum] + Client client = new Client(); + + Schema schema = + Schema.builder() + .type(String.class.getSimpleName()) + .enum_(Arrays.asList("Percussion", "String", "Woodwind", "Brass", "Keyboard")) + .build(); + + String path = media_path + "organ.jpg"; + byte[] imageData = Files.readAllBytes(Paths.get(path)); + String base64Image = Base64.getEncoder().encodeToString(imageData); + Part imagePart = Part.builder() + .inlineData(Blob.builder().data(base64Image) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("What kind of instrument is this:").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); + + GenerateContentConfig config = + GenerateContentConfig.builder() + .responseMimeType("application/json") + .candidateCount(1) + .responseSchema(schema) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + content, + config); + + System.out.println(response.text()); + // [END json_enum] + return response.text(); + } + + public static @Nullable String EnumInJson() throws Exception { + // [START enum_in_json] + Client client = new Client(); + + Schema recipeSchema = Schema.builder() + .type(Array.class.getSimpleName()) + .items(Schema.builder() + .type(Object.class.getSimpleName()) + .properties( + Map.of("recipe_name", Schema.builder() + .type(String.class.getSimpleName()) + .build(), + "grade", Schema.builder() + .type(String.class.getSimpleName()) + .enum_(Arrays.asList("a+", "a", "b", "c", "d", "f")) + .build()) + ) + .required(List.of("recipe_name", "grade")) + .build()) + .build(); + + GenerateContentConfig config = + GenerateContentConfig.builder() + .responseMimeType("application/json") + .responseSchema(recipeSchema) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + "List about 10 cookie recipes, grade them based on popularity", + config); + + System.out.println(response.text()); + // [END enum_in_json] + return response.text(); + } + + public static @Nullable String XEnum() throws Exception { + // [START x_enum] + Client client = new Client(); + + Schema schema = + Schema.builder() + .type(String.class.getSimpleName()) + .enum_(Arrays.asList("Percussion", "String", "Woodwind", "Brass", "Keyboard")) + .build(); + + String path = media_path + "organ.jpg"; + byte[] imageData = Files.readAllBytes(Paths.get(path)); + String base64Image = Base64.getEncoder().encodeToString(imageData); + Part imagePart = Part.builder() + .inlineData(Blob.builder().data(base64Image) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("What kind of instrument is this:").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); + + GenerateContentConfig config = + GenerateContentConfig.builder() + .responseMimeType("text/x.enum") + .candidateCount(1) + .responseSchema(schema) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + content, + config); + + System.out.println(response.text()); + // [END x_enum] + return response.text(); + } +} diff --git a/java/src/main/java/com/example/gemini/EnumInJson.java b/java/src/main/java/com/example/gemini/EnumInJson.java deleted file mode 100644 index 2938e43..0000000 --- a/java/src/main/java/com/example/gemini/EnumInJson.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.lang.reflect.Array; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -public class EnumInJson { - public static void main(String[] args) throws HttpException, IOException { - // [START enum_in_json] - Client client = new Client(); - - Schema recipeSchema = Schema.builder() - .type(Array.class.getSimpleName()) - .items(Schema.builder() - .type(Object.class.getSimpleName()) - .properties( - Map.of("recipe_name", Schema.builder() - .type(String.class.getSimpleName()) - .build(), - "grade", Schema.builder() - .type(String.class.getSimpleName()) - .enum_(Arrays.asList("a+", "a", "b", "c", "d", "f")) - .build()) - ) - .required(List.of("recipe_name", "grade")) - .build()) - .build(); - - GenerateContentConfig config = - GenerateContentConfig.builder() - .responseMimeType("application/json") - .responseSchema(recipeSchema) - .build(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - "List about 10 cookie recipes, grade them based on popularity", - config); - - System.out.println(response.text()); - // [END enum_in_json] - } -} diff --git a/java/src/main/java/com/example/gemini/FunctionCalling.java b/java/src/main/java/com/example/gemini/FunctionCalling.java index 625beb9..e18d9b6 100644 --- a/java/src/main/java/com/example/gemini/FunctionCalling.java +++ b/java/src/main/java/com/example/gemini/FunctionCalling.java @@ -17,10 +17,15 @@ package com.example.gemini; import com.google.genai.Client; -import com.google.genai.types.*; +import com.google.genai.types.FunctionCallingConfig; +import com.google.genai.types.FunctionDeclaration; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.Schema; +import com.google.genai.types.Tool; +import com.google.genai.types.ToolConfig; import org.apache.http.HttpException; -import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -28,7 +33,7 @@ import java.util.function.BiFunction; public class FunctionCalling { - public static void main(String[] args) throws IOException { + public static Double ContentGenFunctionCalling() throws Exception { // [START function_calling] Client client = new Client(); @@ -111,8 +116,8 @@ public static void main(String[] args) throws IOException { } if (response.functionCalls() == null || response.functionCalls().isEmpty()) { - System.err.println("No function call received."); - return; + System.err.println("No function call received"); + return null; } var functionCall = response.functionCalls().getFirst(); @@ -133,5 +138,6 @@ public static void main(String[] args) throws IOException { System.out.println(result); // [END function_calling] + return result; } } diff --git a/java/src/main/java/com/example/gemini/JasonControlledGeneration.java b/java/src/main/java/com/example/gemini/JasonControlledGeneration.java deleted file mode 100644 index 425768c..0000000 --- a/java/src/main/java/com/example/gemini/JasonControlledGeneration.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.lang.reflect.Array; -import java.util.List; -import java.util.Map; - -public class JasonControlledGeneration { - public static void main(String[] args) throws HttpException, IOException { - // [START json_controlled_generation] - Client client = new Client(); - - Schema recipeSchema = Schema.builder() - .type(Array.class.getSimpleName()) - .items(Schema.builder() - .type(Object.class.getSimpleName()) - .properties( - Map.of("recipe_name", Schema.builder() - .type(String.class.getSimpleName()) - .build(), - "ingredients", Schema.builder() - .type(Array.class.getSimpleName()) - .items(Schema.builder() - .type(String.class.getSimpleName()) - .build()) - .build()) - ) - .required(List.of("recipe_name", "ingredients")) - .build()) - .build(); - - GenerateContentConfig config = - GenerateContentConfig.builder() - .responseMimeType("application/json") - .responseSchema(recipeSchema) - .build(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - "List a few popular cookie recipes.", - config); - - System.out.println(response.text()); - // [END json_controlled_generation] - } -} diff --git a/java/src/main/java/com/example/gemini/JsonEnum.java b/java/src/main/java/com/example/gemini/JsonEnum.java deleted file mode 100644 index 1bd8f1f..0000000 --- a/java/src/main/java/com/example/gemini/JsonEnum.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class JsonEnum { - public static void main(String[] args) throws IOException, HttpException { - // [START json_enum] - Client client = new Client(); - - Schema schema = - Schema.builder() - .type(String.class.getSimpleName()) - .enum_(Arrays.asList("Percussion", "String", "Woodwind", "Brass", "Keyboard")) - .build(); - - String path = media_path + "organ.jpg"; - byte[] imageData = Files.readAllBytes(Paths.get(path)); - String base64Image = Base64.getEncoder().encodeToString(imageData); - Part imagePart = Part.builder() - .inlineData(Blob.builder().data(base64Image) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("What kind of instrument is this:").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); - - GenerateContentConfig config = - GenerateContentConfig.builder() - .responseMimeType("application/json") - .candidateCount(1) - .responseSchema(schema) - .build(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - content, - config); - - System.out.println(response.text()); - // [END json_enum] - } -} diff --git a/java/src/main/java/com/example/gemini/JsonNoSchema.java b/java/src/main/java/com/example/gemini/JsonNoSchema.java deleted file mode 100644 index 7a91e7a..0000000 --- a/java/src/main/java/com/example/gemini/JsonNoSchema.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.GenerateContentResponse; -import org.apache.http.HttpException; - -import java.io.IOException; - -public class JsonNoSchema { - public static void main(String[] args) throws HttpException, IOException { - // [START json_no_schema] - Client client = new Client(); - - String prompt = """ - List a few popular cookie recipes in JSON format. - - - Use this JSON schema: - - - Recipe = {'recipe_name': string, 'ingredients': list[string]} - - Return: list[Recipe]. - """; - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - prompt, - null); - - System.out.println(response.text()); - // [END json_no_schema] - } -} diff --git a/java/src/main/java/com/example/gemini/SafetySettings.java b/java/src/main/java/com/example/gemini/SafetySettings.java index 8398603..56a77ac 100644 --- a/java/src/main/java/com/example/gemini/SafetySettings.java +++ b/java/src/main/java/com/example/gemini/SafetySettings.java @@ -20,13 +20,12 @@ import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.SafetySetting; -import org.apache.http.HttpException; -import java.io.IOException; +import java.util.Arrays; import java.util.Collections; public class SafetySettings { - public static void main(String[] args) throws IOException, HttpException { + public static GenerateContentResponse ContentGenSafetySettings() throws Exception { // [START safety_settings] Client client = new Client(); @@ -53,5 +52,45 @@ public static void main(String[] args) throws IOException, HttpException { System.out.println(response.candidates().get().getFirst().finishReason()); System.out.println(response.candidates().get().getFirst().safetyRatings()); // [END safety_settings] + return response; + } + + public static GenerateContentResponse SafetySettingsMulti() throws Exception { + // [START safety_settings_multi] + Client client = new Client(); + + String unsafePrompt = """ + I support Martians Soccer Club and I think Jupiterians Football Club sucks! + Write a ironic phrase about them including expletives. + """; + + GenerateContentConfig config = + GenerateContentConfig.builder() + .safetySettings(Arrays.asList( + SafetySetting.builder() + .category("HARM_CATEGORY_HATE_SPEECH") + .threshold("BLOCK_MEDIUM_AND_ABOVE") + .build(), + SafetySetting.builder() + .category("HARM_CATEGORY_HARASSMENT") + .threshold("BLOCK_ONLY_HIGH") + .build() + )).build(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + unsafePrompt, + config); + + try { + System.out.println(response.text()); + } catch (Exception e) { + System.out.println("No information generated by the model"); + } + + System.out.println(response.candidates().get().getFirst().safetyRatings()); + // [END safety_settings_multi] + return response; } } diff --git a/java/src/main/java/com/example/gemini/SafetySettingsMulti.java b/java/src/main/java/com/example/gemini/SafetySettingsMulti.java deleted file mode 100644 index eeb46ed..0000000 --- a/java/src/main/java/com/example/gemini/SafetySettingsMulti.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.GenerateContentConfig; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.SafetySetting; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.util.Arrays; - -public class SafetySettingsMulti { - public static void main(String[] args) throws IOException, HttpException { - // [START safety_settings_multi] - Client client = new Client(); - - String unsafePrompt = """ - I support Martians Soccer Club and I think Jupiterians Football Club sucks! - Write a ironic phrase about them including expletives. - """; - - GenerateContentConfig config = - GenerateContentConfig.builder() - .safetySettings(Arrays.asList( - SafetySetting.builder() - .category("HARM_CATEGORY_HATE_SPEECH") - .threshold("BLOCK_MEDIUM_AND_ABOVE") - .build(), - SafetySetting.builder() - .category("HARM_CATEGORY_HARASSMENT") - .threshold("BLOCK_ONLY_HIGH") - .build() - )).build(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - unsafePrompt, - config); - - try { - System.out.println(response.text()); - } catch (Exception e) { - System.out.println("No information generated by the model."); - } - - System.out.println(response.candidates().get().getFirst().safetyRatings()); - // [END safety_settings_multi] - } -} diff --git a/java/src/main/java/com/example/gemini/SystemInstruction.java b/java/src/main/java/com/example/gemini/SystemInstruction.java index 2b41ced..8bd8f0b 100644 --- a/java/src/main/java/com/example/gemini/SystemInstruction.java +++ b/java/src/main/java/com/example/gemini/SystemInstruction.java @@ -22,12 +22,10 @@ import com.google.genai.types.GenerateContentConfig; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; +import org.jspecify.annotations.Nullable; public class SystemInstruction { - public static void main(String[] args) throws IOException, HttpException { + public static @Nullable String ContentGenSystemInstruction() throws Exception { // [START system_instruction] Client client = new Client(); @@ -47,5 +45,6 @@ public static void main(String[] args) throws IOException, HttpException { System.out.println(response.text()); // [END system_instruction] + return response.text(); } } diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalAudio.java b/java/src/main/java/com/example/gemini/TextGenMultimodalAudio.java deleted file mode 100644 index 322fccb..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalAudio.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalAudio { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_audio] - Client client = new Client(); - - String path = media_path + "sample.mp3"; - byte[] audioData = Files.readAllBytes(Paths.get(path)); - String audioBase64 = Base64.getEncoder().encodeToString(audioData); - Part audioPart = Part.builder() - .inlineData(Blob.builder().data(audioBase64) - .mimeType("audio/mpeg").build()).build(); - - Part textPart = Part.builder().text("Give me a summary of this audio file.").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build(); - - GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); - - System.out.println(response.text()); - // [END text_gen_multimodal_audio] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalAudioStreaming.java b/java/src/main/java/com/example/gemini/TextGenMultimodalAudioStreaming.java deleted file mode 100644 index ea0259f..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalAudioStreaming.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalAudioStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_audio_streaming] - Client client = new Client(); - - String path = media_path + "sample.mp3"; - byte[] audioData = Files.readAllBytes(Paths.get(path)); - String audioBase64 = Base64.getEncoder().encodeToString(audioData); - Part audioPart = Part.builder() - .inlineData(Blob.builder().data(audioBase64) - .mimeType("audio/mpeg").build()).build(); - - Part textPart = Part.builder().text("Give me a summary of this audio file.").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build(); - - ResponseStream responseStream = - client.models.generateContentStream("gemini-2.0-flash", content, null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_multimodal_audio_streaming] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePrompt.java b/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePrompt.java deleted file mode 100644 index 11785f0..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePrompt.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalMultiImagePrompt { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_multi_image_prompt] - Client client = new Client(); - - String organPath = media_path + "organ.jpg"; - byte[] organImageData = Files.readAllBytes(Paths.get(organPath)); - String organImageBase64 = Base64.getEncoder().encodeToString(organImageData); - Part organImagePart = Part.builder() - .inlineData(Blob.builder().data(organImageBase64) - .mimeType("image/jpeg").build()).build(); - - String cajunPath = media_path + "Cajun_instruments.jpg"; - byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath)); - String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData); - Part cajunImagePart = Part.builder() - .inlineData(Blob.builder().data(cajunImageBase64) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("What is the difference between both of these instruments?").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build(); - - GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); - - System.out.println(response.text()); - // [END text_gen_multimodal_multi_image_prompt] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePromptStreaming.java b/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePromptStreaming.java deleted file mode 100644 index 0957269..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalMultiImagePromptStreaming.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalMultiImagePromptStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_multi_image_prompt_streaming] - Client client = new Client(); - - String organPath = media_path + "organ.jpg"; - byte[] organImageData = Files.readAllBytes(Paths.get(organPath)); - String organBase64Image = Base64.getEncoder().encodeToString(organImageData); - Part organImagePart = Part.builder() - .inlineData(Blob.builder().data(organBase64Image) - .mimeType("image/jpeg").build()).build(); - - String cajunPath = media_path + "Cajun_instruments.jpg"; - byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath)); - String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData); - Part cajunImagePart = Part.builder() - .inlineData(Blob.builder().data(cajunImageBase64) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("What is the difference between both of these instruments?").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build(); - - ResponseStream responseStream = - client.models.generateContentStream("gemini-2.0-flash", content, null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_multimodal_multi_image_prompt_streaming] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePrompt.java b/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePrompt.java deleted file mode 100644 index c826925..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePrompt.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalOneImagePrompt { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_one_image_prompt] - Client client = new Client(); - - String path = media_path + "organ.jpg"; - byte[] imageData = Files.readAllBytes(Paths.get(path)); - String base64Image = Base64.getEncoder().encodeToString(imageData); - Part imagePart = Part.builder() - .inlineData(Blob.builder().data(base64Image) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("Tell me about this instrument").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); - - GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); - - System.out.println(response.text()); - // [END text_gen_multimodal_one_image_prompt] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePromptStreaming.java b/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePromptStreaming.java deleted file mode 100644 index ab5ea6d..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalOneImagePromptStreaming.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalOneImagePromptStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_one_image_prompt_streaming] - Client client = new Client(); - - String path = media_path + "organ.jpg"; - byte[] imageData = Files.readAllBytes(Paths.get(path)); - String base64Image = Base64.getEncoder().encodeToString(imageData); - Part imagePart = Part.builder() - .inlineData(Blob.builder().data(base64Image) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("Tell me about this instrument").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); - - ResponseStream responseStream = - client.models.generateContentStream( - "gemini-2.0-flash", - content, - null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_multimodal_one_image_prompt_streaming] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalPdf.java b/java/src/main/java/com/example/gemini/TextGenMultimodalPdf.java deleted file mode 100644 index 762d209..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalPdf.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalPdf { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_pdf] - Client client = new Client(); - - String path = media_path + "test.pdf"; - byte[] pdfData = Files.readAllBytes(Paths.get(path)); - String pdfBase64 = Base64.getEncoder().encodeToString(pdfData); - Part pdfPart = Part.builder() - .inlineData(Blob.builder().data(pdfBase64) - .mimeType("application/pdf").build()).build(); - - Part textPart = Part.builder().text("Give me a summary of this document.").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build(); - - GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); - - System.out.println(response.text()); - // [END text_gen_multimodal_pdf] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalPdfStreaming.java b/java/src/main/java/com/example/gemini/TextGenMultimodalPdfStreaming.java deleted file mode 100644 index ba5239a..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalPdfStreaming.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalPdfStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_pdf_streaming] - Client client = new Client(); - - String path = media_path + "test.pdf"; - byte[] pdfData = Files.readAllBytes(Paths.get(path)); - String pdfBase64 = Base64.getEncoder().encodeToString(pdfData); - Part pdfPart = Part.builder() - .inlineData(Blob.builder().data(pdfBase64) - .mimeType("application/pdf").build()).build(); - - Part textPart = Part.builder().text("Give me a summary of this document.").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build(); - - ResponseStream responseStream = - client.models.generateContentStream("gemini-2.0-flash", content, null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_multimodal_pdf_streaming] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPrompt.java b/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPrompt.java deleted file mode 100644 index ff8d892..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPrompt.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalVideoPrompt { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_video_prompt] - Client client = new Client(); - - String path = media_path + "Big_Buck_Bunny.mp4"; - byte[] videoData = Files.readAllBytes(Paths.get(path)); - String videoBase64 = Base64.getEncoder().encodeToString(videoData); - Part videoPart = Part.builder() - .inlineData(Blob.builder().data(videoBase64) - .mimeType("video/mp4").build()).build(); - - Part textPart = Part.builder().text("Describe this video clip").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, videoPart)).build(); - - GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); - - System.out.println(response.text()); - // [END text_gen_multimodal_video_prompt] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPromptStreaming.java b/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPromptStreaming.java deleted file mode 100644 index 581c2f3..0000000 --- a/java/src/main/java/com/example/gemini/TextGenMultimodalVideoPromptStreaming.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.Blob; -import com.google.genai.types.Content; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.Part; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class TextGenMultimodalVideoPromptStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_multimodal_video_prompt_streaming] - Client client = new Client(); - - String path = media_path + "Big_Buck_Bunny.mp4"; - byte[] videoData = Files.readAllBytes(Paths.get(path)); - String videoBase64 = Base64.getEncoder().encodeToString(videoData); - Part videoPart = Part.builder() - .inlineData(Blob.builder().data(videoBase64) - .mimeType("video/mp4").build()).build(); - - Part textPart = Part.builder().text("Describe this video clip").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, videoPart)).build(); - - ResponseStream responseStream = - client.models.generateContentStream("gemini-2.0-flash", content, null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_multimodal_video_prompt_streaming] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenTextOnlyPrompt.java b/java/src/main/java/com/example/gemini/TextGenTextOnlyPrompt.java deleted file mode 100644 index f4d8585..0000000 --- a/java/src/main/java/com/example/gemini/TextGenTextOnlyPrompt.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.types.GenerateContentResponse; -import org.apache.http.HttpException; - -import java.io.IOException; - -public class TextGenTextOnlyPrompt { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_text_only_prompt] - Client client = new Client(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - "Write a story about a magic backpack.", - null); - - System.out.println(response.text()); - // [END text_gen_text_only_prompt] - } -} diff --git a/java/src/main/java/com/example/gemini/TextGenTextOnlyPromptStreaming.java b/java/src/main/java/com/example/gemini/TextGenTextOnlyPromptStreaming.java deleted file mode 100644 index 283236f..0000000 --- a/java/src/main/java/com/example/gemini/TextGenTextOnlyPromptStreaming.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.genai.Client; -import com.google.genai.ResponseStream; -import com.google.genai.types.GenerateContentResponse; -import org.apache.http.HttpException; - -import java.io.IOException; - -public class TextGenTextOnlyPromptStreaming { - public static void main(String[] args) throws IOException, HttpException { - // [START text_gen_text_only_prompt_streaming] - Client client = new Client(); - - ResponseStream responseStream = - client.models.generateContentStream( - "gemini-2.0-flash", - "Write a story about a magic backpack.", - null); - - for (GenerateContentResponse res : responseStream) { - System.out.print(res.text()); - } - - responseStream.close(); - // [END text_gen_text_only_prompt_streaming] - } - -} diff --git a/java/src/main/java/com/example/gemini/TextGeneration.java b/java/src/main/java/com/example/gemini/TextGeneration.java new file mode 100644 index 0000000..82b5a5d --- /dev/null +++ b/java/src/main/java/com/example/gemini/TextGeneration.java @@ -0,0 +1,343 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import com.google.common.collect.ImmutableList; +import com.google.genai.Client; +import com.google.genai.ResponseStream; +import com.google.genai.types.Blob; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.Part; +import org.jspecify.annotations.Nullable; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Base64; + +import static com.example.gemini.BuildConfig.media_path; + +@SuppressWarnings("resource") +public class TextGeneration { + public static @Nullable String TextGenTextOnlyPrompt() throws Exception { + // [START text_gen_text_only_prompt] + Client client = new Client(); + + GenerateContentResponse response = + client.models.generateContent( + "gemini-2.0-flash", + "Write a story about a magic backpack.", + null); + + System.out.println(response.text()); + // [END text_gen_text_only_prompt] + return response.text(); + } + + public static String TextGenTextOnlyPromptStreaming() throws Exception { + // [START text_gen_text_only_prompt_streaming] + Client client = new Client(); + + ResponseStream responseStream = + client.models.generateContentStream( + "gemini-2.0-flash", + "Write a story about a magic backpack.", + null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_text_only_prompt_streaming] + return response.toString(); + } + + public static @Nullable String TextGenMultimodalOneImagePrompt() throws Exception { + // [START text_gen_multimodal_one_image_prompt] + Client client = new Client(); + + String path = media_path + "organ.jpg"; + byte[] imageData = Files.readAllBytes(Paths.get(path)); + String base64Image = Base64.getEncoder().encodeToString(imageData); + Part imagePart = Part.builder() + .inlineData(Blob.builder().data(base64Image) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("Tell me about this instrument").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); + + GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); + + System.out.println(response.text()); + // [END text_gen_multimodal_one_image_prompt] + return response.text(); + } + + public static String TextGenMultimodalOneImagePromptStreaming() throws Exception { + // [START text_gen_multimodal_one_image_prompt_streaming] + Client client = new Client(); + + String path = media_path + "organ.jpg"; + byte[] imageData = Files.readAllBytes(Paths.get(path)); + String base64Image = Base64.getEncoder().encodeToString(imageData); + Part imagePart = Part.builder() + .inlineData(Blob.builder().data(base64Image) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("Tell me about this instrument").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); + + ResponseStream responseStream = + client.models.generateContentStream( + "gemini-2.0-flash", + content, + null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_multimodal_one_image_prompt_streaming] + return response.toString(); + } + + public static @Nullable String TextGenMultimodalMultiImagePrompt() throws Exception { + // [START text_gen_multimodal_multi_image_prompt] + Client client = new Client(); + + String organPath = media_path + "organ.jpg"; + byte[] organImageData = Files.readAllBytes(Paths.get(organPath)); + String organImageBase64 = Base64.getEncoder().encodeToString(organImageData); + Part organImagePart = Part.builder() + .inlineData(Blob.builder().data(organImageBase64) + .mimeType("image/jpeg").build()).build(); + + String cajunPath = media_path + "Cajun_instruments.jpg"; + byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath)); + String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData); + Part cajunImagePart = Part.builder() + .inlineData(Blob.builder().data(cajunImageBase64) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("What is the difference between both of these instruments?").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build(); + + GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); + + System.out.println(response.text()); + // [END text_gen_multimodal_multi_image_prompt] + return response.text(); + } + + public static String TextGenMultimodalMultiImagePromptStreaming() throws Exception { + // [START text_gen_multimodal_multi_image_prompt_streaming] + Client client = new Client(); + + String organPath = media_path + "organ.jpg"; + byte[] organImageData = Files.readAllBytes(Paths.get(organPath)); + String organBase64Image = Base64.getEncoder().encodeToString(organImageData); + Part organImagePart = Part.builder() + .inlineData(Blob.builder().data(organBase64Image) + .mimeType("image/jpeg").build()).build(); + + String cajunPath = media_path + "Cajun_instruments.jpg"; + byte[] cajunImageData = Files.readAllBytes(Paths.get(cajunPath)); + String cajunImageBase64 = Base64.getEncoder().encodeToString(cajunImageData); + Part cajunImagePart = Part.builder() + .inlineData(Blob.builder().data(cajunImageBase64) + .mimeType("image/jpeg").build()).build(); + + Part textPart = Part.builder().text("What is the difference between both of these instruments?").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, organImagePart, cajunImagePart)).build(); + + ResponseStream responseStream = + client.models.generateContentStream("gemini-2.0-flash", content, null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_multimodal_multi_image_prompt_streaming] + return response.toString(); + } + + public static @Nullable String TextGenMultimodalAudio() throws Exception { + // [START text_gen_multimodal_audio] + Client client = new Client(); + + String path = media_path + "sample.mp3"; + byte[] audioData = Files.readAllBytes(Paths.get(path)); + String audioBase64 = Base64.getEncoder().encodeToString(audioData); + Part audioPart = Part.builder() + .inlineData(Blob.builder().data(audioBase64) + .mimeType("audio/mpeg").build()).build(); + + Part textPart = Part.builder().text("Give me a summary of this audio file.").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build(); + + GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); + + System.out.println(response.text()); + // [END text_gen_multimodal_audio] + return response.text(); + } + + public static String TextGenMultimodalAudioStreaming() throws Exception { + // [START text_gen_multimodal_audio_streaming] + Client client = new Client(); + + String path = media_path + "sample.mp3"; + byte[] audioData = Files.readAllBytes(Paths.get(path)); + String audioBase64 = Base64.getEncoder().encodeToString(audioData); + Part audioPart = Part.builder() + .inlineData(Blob.builder().data(audioBase64) + .mimeType("audio/mpeg").build()).build(); + + Part textPart = Part.builder().text("Give me a summary of this audio file.").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, audioPart)).build(); + + ResponseStream responseStream = + client.models.generateContentStream("gemini-2.0-flash", content, null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_multimodal_audio_streaming] + return response.toString(); + } + + public static @Nullable String TextGenMultimodalVideoPrompt() throws Exception { + // [START text_gen_multimodal_video_prompt] + Client client = new Client(); + + String path = media_path + "Big_Buck_Bunny.mp4"; + byte[] videoData = Files.readAllBytes(Paths.get(path)); + String videoBase64 = Base64.getEncoder().encodeToString(videoData); + Part videoPart = Part.builder() + .inlineData(Blob.builder().data(videoBase64) + .mimeType("video/mp4").build()).build(); + + Part textPart = Part.builder().text("Describe this video clip").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, videoPart)).build(); + + GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); + + System.out.println(response.text()); + // [END text_gen_multimodal_video_prompt] + return response.text(); + } + + public static String TextGenMultimodalVideoPromptStreaming() throws Exception { + // [START text_gen_multimodal_video_prompt_streaming] + Client client = new Client(); + + String path = media_path + "Big_Buck_Bunny.mp4"; + byte[] videoData = Files.readAllBytes(Paths.get(path)); + String videoBase64 = Base64.getEncoder().encodeToString(videoData); + Part videoPart = Part.builder() + .inlineData(Blob.builder().data(videoBase64) + .mimeType("video/mp4").build()).build(); + + Part textPart = Part.builder().text("Describe this video clip").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, videoPart)).build(); + + ResponseStream responseStream = + client.models.generateContentStream("gemini-2.0-flash", content, null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_multimodal_video_prompt_streaming] + return response.toString(); + } + + public static @Nullable String TextGenMultimodalPdf() throws Exception { + // [START text_gen_multimodal_pdf] + Client client = new Client(); + + String path = media_path + "test.pdf"; + byte[] pdfData = Files.readAllBytes(Paths.get(path)); + String pdfBase64 = Base64.getEncoder().encodeToString(pdfData); + Part pdfPart = Part.builder() + .inlineData(Blob.builder().data(pdfBase64) + .mimeType("application/pdf").build()).build(); + + Part textPart = Part.builder().text("Give me a summary of this document.").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build(); + + GenerateContentResponse response = client.models.generateContent("gemini-2.0-flash", content, null); + + System.out.println(response.text()); + // [END text_gen_multimodal_pdf] + return response.text(); + } + + public static String TextGenMultimodalPdfStreaming() throws Exception { + // [START text_gen_multimodal_pdf_streaming] + Client client = new Client(); + + String path = media_path + "test.pdf"; + byte[] pdfData = Files.readAllBytes(Paths.get(path)); + String pdfBase64 = Base64.getEncoder().encodeToString(pdfData); + Part pdfPart = Part.builder() + .inlineData(Blob.builder().data(pdfBase64) + .mimeType("application/pdf").build()).build(); + + Part textPart = Part.builder().text("Give me a summary of this document.").build(); + + Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, pdfPart)).build(); + + ResponseStream responseStream = + client.models.generateContentStream("gemini-2.0-flash", content, null); + + StringBuilder response = new StringBuilder(); + for (GenerateContentResponse res : responseStream) { + System.out.print(res.text()); + response.append(res.text()); + } + + responseStream.close(); + // [END text_gen_multimodal_pdf_streaming] + return response.toString(); + } +} diff --git a/java/src/main/java/com/example/gemini/XEnum.java b/java/src/main/java/com/example/gemini/XEnum.java deleted file mode 100644 index 094557a..0000000 --- a/java/src/main/java/com/example/gemini/XEnum.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2025 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.gemini; - -import com.google.common.collect.ImmutableList; -import com.google.genai.Client; -import com.google.genai.types.*; -import org.apache.http.HttpException; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Base64; - -import static com.example.gemini.BuildConfig.media_path; - -public class XEnum { - public static void main(String[] args) throws HttpException, IOException { - // [START x_enum] - Client client = new Client(); - - Schema schema = - Schema.builder() - .type(String.class.getSimpleName()) - .enum_(Arrays.asList("Percussion", "String", "Woodwind", "Brass", "Keyboard")) - .build(); - - String path = media_path + "organ.jpg"; - byte[] imageData = Files.readAllBytes(Paths.get(path)); - String base64Image = Base64.getEncoder().encodeToString(imageData); - Part imagePart = Part.builder() - .inlineData(Blob.builder().data(base64Image) - .mimeType("image/jpeg").build()).build(); - - Part textPart = Part.builder().text("What kind of instrument is this:").build(); - - Content content = Content.builder().role("user").parts(ImmutableList.of(textPart, imagePart)).build(); - - GenerateContentConfig config = - GenerateContentConfig.builder() - .responseMimeType("text/x.enum") - .candidateCount(1) - .responseSchema(schema) - .build(); - - GenerateContentResponse response = - client.models.generateContent( - "gemini-2.0-flash", - content, - config); - - System.out.println(response.text()); - // [END x_enum] - } -} diff --git a/java/src/test/java/com/example/gemini/CodeExecutionTest.java b/java/src/test/java/com/example/gemini/CodeExecutionTest.java new file mode 100644 index 0000000..521f504 --- /dev/null +++ b/java/src/test/java/com/example/gemini/CodeExecutionTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.Part; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +public class CodeExecutionTest { + @Test + public void test_CodeExecutionBasic() { + GenerateContentResponse response = assertDoesNotThrow(CodeExecution::CodeExecutionBasic, + "CodeExecutionBasic returned an error"); + + assertNotNull(response, "Response should not be null"); + + List parts = response.candidates().get().getFirst().content().get().parts().get(); + assertNotNull(parts, "Response parts should not be null"); + assert !parts.isEmpty(); + + assertNotNull(response.text(), "Response text should not be null"); + assertFalse(response.text().trim().isEmpty(), "Response text should not be empty"); + } + + @Test + public void test_CodeExecutionRequestOverride() { + GenerateContentResponse response = assertDoesNotThrow(CodeExecution::CodeExecutionRequestOverride, + "CodeExecutionRequestOverride returned an error"); + + assertNotNull(response, "Response should not be null"); + + assertNotNull(response.executableCode(), "ExecutableCode response should not be null"); + assertNotNull(response.codeExecutionResult(), "CodeExecutionResult response should not be null"); + } +} diff --git a/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java b/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java new file mode 100644 index 0000000..13ae973 --- /dev/null +++ b/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class ConfigureModelParametersTest { + @Test + public void test_ContentGneConfigureModelParameters() { + String result = assertDoesNotThrow(ConfigureModelParameters::ContentGneConfigureModelParameters, + "ContentGneConfigureModelParameters returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } +} diff --git a/java/src/test/java/com/example/gemini/ControlledGenerationTest.java b/java/src/test/java/com/example/gemini/ControlledGenerationTest.java new file mode 100644 index 0000000..f11231f --- /dev/null +++ b/java/src/test/java/com/example/gemini/ControlledGenerationTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ControlledGenerationTest { + @Test + public void test_JsonControlledGeneration() { + String result = assertDoesNotThrow(ControlledGeneration::JsonControlledGeneration, + "JsonControlledGeneration returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_JsonNoSchema() { + String result = assertDoesNotThrow(ControlledGeneration::JsonNoSchema, + "JsonNoSchema returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_JsonEnum() { + String result = assertDoesNotThrow(ControlledGeneration::JsonEnum, + "JsonEnum returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + assertTrue(result.trim().contains("Keyboard"), "Response should contain word `Keyboard`"); + } + + @Test + public void test_EnumInJson() { + String result = assertDoesNotThrow(ControlledGeneration::EnumInJson, + "EnumInJson returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_XEnum() { + String result = assertDoesNotThrow(ControlledGeneration::XEnum, + "XEnum returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } +} diff --git a/java/src/test/java/com/example/gemini/FunctionCallingTest.java b/java/src/test/java/com/example/gemini/FunctionCallingTest.java new file mode 100644 index 0000000..217ae1f --- /dev/null +++ b/java/src/test/java/com/example/gemini/FunctionCallingTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class FunctionCallingTest { + @Test + public void test_ContentGenFunctionCalling() { + Double result = assertDoesNotThrow(FunctionCalling::ContentGenFunctionCalling, + "ContentGenFunctionCalling returned an error"); + + assertNotNull(result, "Response should not be null"); + } +} diff --git a/java/src/test/java/com/example/gemini/SafetySettingsTest.java b/java/src/test/java/com/example/gemini/SafetySettingsTest.java new file mode 100644 index 0000000..09a02a8 --- /dev/null +++ b/java/src/test/java/com/example/gemini/SafetySettingsTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import com.google.genai.types.GenerateContentResponse; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SafetySettingsTest { + @Test + public void test_ContentGenSafetySettings() { + GenerateContentResponse response = assertDoesNotThrow(SafetySettings::ContentGenSafetySettings, + "ContentGenSafetySettings returned an error"); + + assertNotNull(response, "Response should not be null"); + + assertTrue( + response.candidates().get().getFirst().finishReason().isPresent(), + "Safety finish reason should be present" + ); + assertTrue( + response.candidates().get().getFirst().safetyRatings().isPresent(), + "Safety ratings should be present" + ); + } + + @Test + public void test_SafetySettingsMulti() { + GenerateContentResponse response = assertDoesNotThrow(SafetySettings::SafetySettingsMulti, + "SafetySettingsMulti returned an error"); + + assertNotNull(response, "Response should not be null"); + + assertTrue( + response.candidates().get().getFirst().safetyRatings().isPresent(), + "Safety ratings should be present" + ); + } +} diff --git a/java/src/test/java/com/example/gemini/SystemInstructionTest.java b/java/src/test/java/com/example/gemini/SystemInstructionTest.java new file mode 100644 index 0000000..e62a507 --- /dev/null +++ b/java/src/test/java/com/example/gemini/SystemInstructionTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class SystemInstructionTest { + @Test + public void test_ContentGenSystemInstruction() { + String result = assertDoesNotThrow(SystemInstruction::ContentGenSystemInstruction, + "ContentGenSystemInstruction returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } +} diff --git a/java/src/test/java/com/example/gemini/TextGenerationTest.java b/java/src/test/java/com/example/gemini/TextGenerationTest.java new file mode 100644 index 0000000..c23e503 --- /dev/null +++ b/java/src/test/java/com/example/gemini/TextGenerationTest.java @@ -0,0 +1,134 @@ +/* + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.gemini; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertFalse; + + +public class TextGenerationTest { + @Test + public void test_TextGenTextOnlyPrompt() { + String result = assertDoesNotThrow(TextGeneration::TextGenTextOnlyPrompt, + "TextGenTextOnlyPrompt returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenTextOnlyPromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenTextOnlyPromptStreaming, + "TextGenTextOnlyPromptStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalOneImagePrompt() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalOneImagePrompt, + "TextGenMultimodalOneImagePrompt returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalOneImagePromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalOneImagePromptStreaming, + "TextGenMultimodalOneImagePromptStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalMultiImagePrompt() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalMultiImagePrompt, + "TextGenMultimodalMultiImagePrompt returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalMultiImagePromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalMultiImagePromptStreaming, + "TextGenMultimodalMultiImagePromptStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalAudio() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalAudio, + "TextGenMultimodalAudio returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalAudioStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalAudioStreaming, + "TextGenMultimodalAudioStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalVideoPrompt() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalVideoPrompt, + "TextGenMultimodalVideoPrompt returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalVideoPromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalVideoPromptStreaming, + "TextGenMultimodalVideoPromptStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalPdf() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalPdf, + "TextGenMultimodalPdf returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } + + @Test + public void test_TextGenMultimodalPdfStreaming() { + String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalPdfStreaming, + "TextGenMultimodalPdfStreaming returned an error"); + + assertNotNull(result, "Response should not be null"); + assertFalse(result.trim().isEmpty(), "Response should not be empty"); + } +} From b0d718755966c9764cbbb1fa4eb1c3ac3d67c49f Mon Sep 17 00:00:00 2001 From: anandmali Date: Wed, 9 Apr 2025 17:35:57 +0530 Subject: [PATCH 2/3] Add README for java module --- java/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 java/README.md diff --git a/java/README.md b/java/README.md new file mode 100644 index 0000000..f0c4320 --- /dev/null +++ b/java/README.md @@ -0,0 +1,17 @@ +# Java examples + +This directory contains examples of working with the Gemini API using the +[Google Gen AI SDK for Java](https://github.com/googleapis/java-genai). + +## Install dependencies +open the project in intellliJ IDEA, load the maven build script. + +## Run a test file +Before running the tests, set GOOGLE_API_KEY as environment variable for run/debug configuration. + + mvn -Dtest= test + +For example: + + mvn -Dtest=CodeExecutionTest test + From 7371eceee082fc1d06b3408e32d68a5edc0224ff Mon Sep 17 00:00:00 2001 From: anandmali Date: Wed, 9 Apr 2025 18:33:51 +0530 Subject: [PATCH 3/3] Update java examples naming --- .../com/example/gemini/CodeExecution.java | 4 +- .../gemini/ConfigureModelParameters.java | 2 +- .../example/gemini/ControlledGeneration.java | 10 +-- .../com/example/gemini/FunctionCalling.java | 2 +- .../com/example/gemini/SafetySettings.java | 4 +- .../com/example/gemini/SystemInstruction.java | 2 +- .../com/example/gemini/TextGeneration.java | 24 +++---- .../com/example/gemini/CodeExecutionTest.java | 12 ++-- .../gemini/ConfigureModelParametersTest.java | 6 +- .../gemini/ControlledGenerationTest.java | 30 ++++---- .../example/gemini/FunctionCallingTest.java | 6 +- .../example/gemini/SafetySettingsTest.java | 12 ++-- .../example/gemini/SystemInstructionTest.java | 6 +- .../example/gemini/TextGenerationTest.java | 72 +++++++++---------- 14 files changed, 96 insertions(+), 96 deletions(-) diff --git a/java/src/main/java/com/example/gemini/CodeExecution.java b/java/src/main/java/com/example/gemini/CodeExecution.java index e298e8d..ee86d2b 100644 --- a/java/src/main/java/com/example/gemini/CodeExecution.java +++ b/java/src/main/java/com/example/gemini/CodeExecution.java @@ -27,7 +27,7 @@ @SuppressWarnings("resource") public class CodeExecution { - public static GenerateContentResponse CodeExecutionBasic() throws Exception { + public static GenerateContentResponse codeExecutionBasic() throws Exception { // [START code_execution_basic] Client client = new Client(); @@ -94,7 +94,7 @@ def is_prime(n): return response; } - public static GenerateContentResponse CodeExecutionRequestOverride() throws Exception { + public static GenerateContentResponse codeExecutionRequestOverride() throws Exception { // [START code_execution_request_override] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/ConfigureModelParameters.java b/java/src/main/java/com/example/gemini/ConfigureModelParameters.java index a98b478..4a9478a 100644 --- a/java/src/main/java/com/example/gemini/ConfigureModelParameters.java +++ b/java/src/main/java/com/example/gemini/ConfigureModelParameters.java @@ -24,7 +24,7 @@ import java.util.List; public class ConfigureModelParameters { - public static @Nullable String ContentGneConfigureModelParameters() throws Exception { + public static @Nullable String configureModelParameters() throws Exception { // [START configure_model_parameters] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/ControlledGeneration.java b/java/src/main/java/com/example/gemini/ControlledGeneration.java index 167a798..2bbcb42 100644 --- a/java/src/main/java/com/example/gemini/ControlledGeneration.java +++ b/java/src/main/java/com/example/gemini/ControlledGeneration.java @@ -37,7 +37,7 @@ import static com.example.gemini.BuildConfig.media_path; public class ControlledGeneration { - public static @Nullable String JsonControlledGeneration() throws Exception { + public static @Nullable String jsonControlledGeneration() throws Exception { // [START json_controlled_generation] Client client = new Client(); @@ -77,7 +77,7 @@ public class ControlledGeneration { return response.text(); } - public static @Nullable String JsonNoSchema() throws Exception { + public static @Nullable String jsonNoSchema() throws Exception { // [START json_no_schema] Client client = new Client(); @@ -104,7 +104,7 @@ public class ControlledGeneration { return response.text(); } - public static @Nullable String JsonEnum() throws Exception { + public static @Nullable String jsonEnum() throws Exception { // [START json_enum] Client client = new Client(); @@ -143,7 +143,7 @@ public class ControlledGeneration { return response.text(); } - public static @Nullable String EnumInJson() throws Exception { + public static @Nullable String enumInJson() throws Exception { // [START enum_in_json] Client client = new Client(); @@ -181,7 +181,7 @@ public class ControlledGeneration { return response.text(); } - public static @Nullable String XEnum() throws Exception { + public static @Nullable String xEnum() throws Exception { // [START x_enum] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/FunctionCalling.java b/java/src/main/java/com/example/gemini/FunctionCalling.java index e18d9b6..62f8275 100644 --- a/java/src/main/java/com/example/gemini/FunctionCalling.java +++ b/java/src/main/java/com/example/gemini/FunctionCalling.java @@ -33,7 +33,7 @@ import java.util.function.BiFunction; public class FunctionCalling { - public static Double ContentGenFunctionCalling() throws Exception { + public static Double functionCalling() throws Exception { // [START function_calling] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/SafetySettings.java b/java/src/main/java/com/example/gemini/SafetySettings.java index 56a77ac..358b945 100644 --- a/java/src/main/java/com/example/gemini/SafetySettings.java +++ b/java/src/main/java/com/example/gemini/SafetySettings.java @@ -25,7 +25,7 @@ import java.util.Collections; public class SafetySettings { - public static GenerateContentResponse ContentGenSafetySettings() throws Exception { + public static GenerateContentResponse safetySettings() throws Exception { // [START safety_settings] Client client = new Client(); @@ -55,7 +55,7 @@ public static GenerateContentResponse ContentGenSafetySettings() throws Exceptio return response; } - public static GenerateContentResponse SafetySettingsMulti() throws Exception { + public static GenerateContentResponse safetySettingsMulti() throws Exception { // [START safety_settings_multi] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/SystemInstruction.java b/java/src/main/java/com/example/gemini/SystemInstruction.java index 8bd8f0b..adc9f15 100644 --- a/java/src/main/java/com/example/gemini/SystemInstruction.java +++ b/java/src/main/java/com/example/gemini/SystemInstruction.java @@ -25,7 +25,7 @@ import org.jspecify.annotations.Nullable; public class SystemInstruction { - public static @Nullable String ContentGenSystemInstruction() throws Exception { + public static @Nullable String systemInstruction() throws Exception { // [START system_instruction] Client client = new Client(); diff --git a/java/src/main/java/com/example/gemini/TextGeneration.java b/java/src/main/java/com/example/gemini/TextGeneration.java index 82b5a5d..5d48a4d 100644 --- a/java/src/main/java/com/example/gemini/TextGeneration.java +++ b/java/src/main/java/com/example/gemini/TextGeneration.java @@ -33,7 +33,7 @@ @SuppressWarnings("resource") public class TextGeneration { - public static @Nullable String TextGenTextOnlyPrompt() throws Exception { + public static @Nullable String textGenTextOnlyPrompt() throws Exception { // [START text_gen_text_only_prompt] Client client = new Client(); @@ -48,7 +48,7 @@ public class TextGeneration { return response.text(); } - public static String TextGenTextOnlyPromptStreaming() throws Exception { + public static String textGenTextOnlyPromptStreaming() throws Exception { // [START text_gen_text_only_prompt_streaming] Client client = new Client(); @@ -69,7 +69,7 @@ public static String TextGenTextOnlyPromptStreaming() throws Exception { return response.toString(); } - public static @Nullable String TextGenMultimodalOneImagePrompt() throws Exception { + public static @Nullable String textGenMultimodalOneImagePrompt() throws Exception { // [START text_gen_multimodal_one_image_prompt] Client client = new Client(); @@ -91,7 +91,7 @@ public static String TextGenTextOnlyPromptStreaming() throws Exception { return response.text(); } - public static String TextGenMultimodalOneImagePromptStreaming() throws Exception { + public static String textGenMultimodalOneImagePromptStreaming() throws Exception { // [START text_gen_multimodal_one_image_prompt_streaming] Client client = new Client(); @@ -123,7 +123,7 @@ public static String TextGenMultimodalOneImagePromptStreaming() throws Exception return response.toString(); } - public static @Nullable String TextGenMultimodalMultiImagePrompt() throws Exception { + public static @Nullable String textGenMultimodalMultiImagePrompt() throws Exception { // [START text_gen_multimodal_multi_image_prompt] Client client = new Client(); @@ -152,7 +152,7 @@ public static String TextGenMultimodalOneImagePromptStreaming() throws Exception return response.text(); } - public static String TextGenMultimodalMultiImagePromptStreaming() throws Exception { + public static String textGenMultimodalMultiImagePromptStreaming() throws Exception { // [START text_gen_multimodal_multi_image_prompt_streaming] Client client = new Client(); @@ -188,7 +188,7 @@ public static String TextGenMultimodalMultiImagePromptStreaming() throws Excepti return response.toString(); } - public static @Nullable String TextGenMultimodalAudio() throws Exception { + public static @Nullable String textGenMultimodalAudio() throws Exception { // [START text_gen_multimodal_audio] Client client = new Client(); @@ -210,7 +210,7 @@ public static String TextGenMultimodalMultiImagePromptStreaming() throws Excepti return response.text(); } - public static String TextGenMultimodalAudioStreaming() throws Exception { + public static String textGenMultimodalAudioStreaming() throws Exception { // [START text_gen_multimodal_audio_streaming] Client client = new Client(); @@ -239,7 +239,7 @@ public static String TextGenMultimodalAudioStreaming() throws Exception { return response.toString(); } - public static @Nullable String TextGenMultimodalVideoPrompt() throws Exception { + public static @Nullable String textGenMultimodalVideoPrompt() throws Exception { // [START text_gen_multimodal_video_prompt] Client client = new Client(); @@ -261,7 +261,7 @@ public static String TextGenMultimodalAudioStreaming() throws Exception { return response.text(); } - public static String TextGenMultimodalVideoPromptStreaming() throws Exception { + public static String textGenMultimodalVideoPromptStreaming() throws Exception { // [START text_gen_multimodal_video_prompt_streaming] Client client = new Client(); @@ -290,7 +290,7 @@ public static String TextGenMultimodalVideoPromptStreaming() throws Exception { return response.toString(); } - public static @Nullable String TextGenMultimodalPdf() throws Exception { + public static @Nullable String textGenMultimodalPdf() throws Exception { // [START text_gen_multimodal_pdf] Client client = new Client(); @@ -312,7 +312,7 @@ public static String TextGenMultimodalVideoPromptStreaming() throws Exception { return response.text(); } - public static String TextGenMultimodalPdfStreaming() throws Exception { + public static String textGenMultimodalPdfStreaming() throws Exception { // [START text_gen_multimodal_pdf_streaming] Client client = new Client(); diff --git a/java/src/test/java/com/example/gemini/CodeExecutionTest.java b/java/src/test/java/com/example/gemini/CodeExecutionTest.java index 521f504..d06dd97 100644 --- a/java/src/test/java/com/example/gemini/CodeExecutionTest.java +++ b/java/src/test/java/com/example/gemini/CodeExecutionTest.java @@ -29,9 +29,9 @@ public class CodeExecutionTest { @Test - public void test_CodeExecutionBasic() { - GenerateContentResponse response = assertDoesNotThrow(CodeExecution::CodeExecutionBasic, - "CodeExecutionBasic returned an error"); + public void test_codeExecutionBasic() { + GenerateContentResponse response = assertDoesNotThrow(CodeExecution::codeExecutionBasic, + "codeExecutionBasic returned an error"); assertNotNull(response, "Response should not be null"); @@ -44,9 +44,9 @@ public void test_CodeExecutionBasic() { } @Test - public void test_CodeExecutionRequestOverride() { - GenerateContentResponse response = assertDoesNotThrow(CodeExecution::CodeExecutionRequestOverride, - "CodeExecutionRequestOverride returned an error"); + public void test_codeExecutionRequestOverride() { + GenerateContentResponse response = assertDoesNotThrow(CodeExecution::codeExecutionRequestOverride, + "codeExecutionRequestOverride returned an error"); assertNotNull(response, "Response should not be null"); diff --git a/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java b/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java index 13ae973..452d069 100644 --- a/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java +++ b/java/src/test/java/com/example/gemini/ConfigureModelParametersTest.java @@ -24,9 +24,9 @@ public class ConfigureModelParametersTest { @Test - public void test_ContentGneConfigureModelParameters() { - String result = assertDoesNotThrow(ConfigureModelParameters::ContentGneConfigureModelParameters, - "ContentGneConfigureModelParameters returned an error"); + public void test_configureModelParameters() { + String result = assertDoesNotThrow(ConfigureModelParameters::configureModelParameters, + "configureModelParameters returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); diff --git a/java/src/test/java/com/example/gemini/ControlledGenerationTest.java b/java/src/test/java/com/example/gemini/ControlledGenerationTest.java index f11231f..afa3d6a 100644 --- a/java/src/test/java/com/example/gemini/ControlledGenerationTest.java +++ b/java/src/test/java/com/example/gemini/ControlledGenerationTest.java @@ -25,27 +25,27 @@ public class ControlledGenerationTest { @Test - public void test_JsonControlledGeneration() { - String result = assertDoesNotThrow(ControlledGeneration::JsonControlledGeneration, - "JsonControlledGeneration returned an error"); + public void test_jsonControlledGeneration() { + String result = assertDoesNotThrow(ControlledGeneration::jsonControlledGeneration, + "jsonControlledGeneration returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_JsonNoSchema() { - String result = assertDoesNotThrow(ControlledGeneration::JsonNoSchema, - "JsonNoSchema returned an error"); + public void test_jsonNoSchema() { + String result = assertDoesNotThrow(ControlledGeneration::jsonNoSchema, + "jsonNoSchema returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_JsonEnum() { - String result = assertDoesNotThrow(ControlledGeneration::JsonEnum, - "JsonEnum returned an error"); + public void test_jsonEnum() { + String result = assertDoesNotThrow(ControlledGeneration::jsonEnum, + "jsonEnum returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); @@ -53,18 +53,18 @@ public void test_JsonEnum() { } @Test - public void test_EnumInJson() { - String result = assertDoesNotThrow(ControlledGeneration::EnumInJson, - "EnumInJson returned an error"); + public void test_enumInJson() { + String result = assertDoesNotThrow(ControlledGeneration::enumInJson, + "enumInJson returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_XEnum() { - String result = assertDoesNotThrow(ControlledGeneration::XEnum, - "XEnum returned an error"); + public void test_xEnum() { + String result = assertDoesNotThrow(ControlledGeneration::xEnum, + "xEnum returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); diff --git a/java/src/test/java/com/example/gemini/FunctionCallingTest.java b/java/src/test/java/com/example/gemini/FunctionCallingTest.java index 217ae1f..e6892f4 100644 --- a/java/src/test/java/com/example/gemini/FunctionCallingTest.java +++ b/java/src/test/java/com/example/gemini/FunctionCallingTest.java @@ -23,9 +23,9 @@ public class FunctionCallingTest { @Test - public void test_ContentGenFunctionCalling() { - Double result = assertDoesNotThrow(FunctionCalling::ContentGenFunctionCalling, - "ContentGenFunctionCalling returned an error"); + public void test_functionCalling() { + Double result = assertDoesNotThrow(FunctionCalling::functionCalling, + "functionCalling returned an error"); assertNotNull(result, "Response should not be null"); } diff --git a/java/src/test/java/com/example/gemini/SafetySettingsTest.java b/java/src/test/java/com/example/gemini/SafetySettingsTest.java index 09a02a8..74d0f77 100644 --- a/java/src/test/java/com/example/gemini/SafetySettingsTest.java +++ b/java/src/test/java/com/example/gemini/SafetySettingsTest.java @@ -25,9 +25,9 @@ public class SafetySettingsTest { @Test - public void test_ContentGenSafetySettings() { - GenerateContentResponse response = assertDoesNotThrow(SafetySettings::ContentGenSafetySettings, - "ContentGenSafetySettings returned an error"); + public void test_safetySettings() { + GenerateContentResponse response = assertDoesNotThrow(SafetySettings::safetySettings, + "safetySettings returned an error"); assertNotNull(response, "Response should not be null"); @@ -42,9 +42,9 @@ public void test_ContentGenSafetySettings() { } @Test - public void test_SafetySettingsMulti() { - GenerateContentResponse response = assertDoesNotThrow(SafetySettings::SafetySettingsMulti, - "SafetySettingsMulti returned an error"); + public void test_safetySettingsMulti() { + GenerateContentResponse response = assertDoesNotThrow(SafetySettings::safetySettingsMulti, + "safetySettingsMulti returned an error"); assertNotNull(response, "Response should not be null"); diff --git a/java/src/test/java/com/example/gemini/SystemInstructionTest.java b/java/src/test/java/com/example/gemini/SystemInstructionTest.java index e62a507..996c615 100644 --- a/java/src/test/java/com/example/gemini/SystemInstructionTest.java +++ b/java/src/test/java/com/example/gemini/SystemInstructionTest.java @@ -24,9 +24,9 @@ public class SystemInstructionTest { @Test - public void test_ContentGenSystemInstruction() { - String result = assertDoesNotThrow(SystemInstruction::ContentGenSystemInstruction, - "ContentGenSystemInstruction returned an error"); + public void test_systemInstruction() { + String result = assertDoesNotThrow(SystemInstruction::systemInstruction, + "systemInstruction returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); diff --git a/java/src/test/java/com/example/gemini/TextGenerationTest.java b/java/src/test/java/com/example/gemini/TextGenerationTest.java index c23e503..761f7a0 100644 --- a/java/src/test/java/com/example/gemini/TextGenerationTest.java +++ b/java/src/test/java/com/example/gemini/TextGenerationTest.java @@ -25,108 +25,108 @@ public class TextGenerationTest { @Test - public void test_TextGenTextOnlyPrompt() { - String result = assertDoesNotThrow(TextGeneration::TextGenTextOnlyPrompt, - "TextGenTextOnlyPrompt returned an error"); + public void test_textGenTextOnlyPrompt() { + String result = assertDoesNotThrow(TextGeneration::textGenTextOnlyPrompt, + "textGenTextOnlyPrompt returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenTextOnlyPromptStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenTextOnlyPromptStreaming, - "TextGenTextOnlyPromptStreaming returned an error"); + public void test_textGenTextOnlyPromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenTextOnlyPromptStreaming, + "textGenTextOnlyPromptStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalOneImagePrompt() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalOneImagePrompt, - "TextGenMultimodalOneImagePrompt returned an error"); + public void test_textGenMultimodalOneImagePrompt() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalOneImagePrompt, + "textGenMultimodalOneImagePrompt returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalOneImagePromptStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalOneImagePromptStreaming, - "TextGenMultimodalOneImagePromptStreaming returned an error"); + public void test_textGenMultimodalOneImagePromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalOneImagePromptStreaming, + "textGenMultimodalOneImagePromptStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalMultiImagePrompt() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalMultiImagePrompt, - "TextGenMultimodalMultiImagePrompt returned an error"); + public void test_textGenMultimodalMultiImagePrompt() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalMultiImagePrompt, + "textGenMultimodalMultiImagePrompt returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalMultiImagePromptStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalMultiImagePromptStreaming, - "TextGenMultimodalMultiImagePromptStreaming returned an error"); + public void test_textGenMultimodalMultiImagePromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalMultiImagePromptStreaming, + "textGenMultimodalMultiImagePromptStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalAudio() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalAudio, - "TextGenMultimodalAudio returned an error"); + public void test_textGenMultimodalAudio() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalAudio, + "textGenMultimodalAudio returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalAudioStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalAudioStreaming, - "TextGenMultimodalAudioStreaming returned an error"); + public void test_textGenMultimodalAudioStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalAudioStreaming, + "textGenMultimodalAudioStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalVideoPrompt() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalVideoPrompt, - "TextGenMultimodalVideoPrompt returned an error"); + public void test_textGenMultimodalVideoPrompt() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalVideoPrompt, + "textGenMultimodalVideoPrompt returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalVideoPromptStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalVideoPromptStreaming, - "TextGenMultimodalVideoPromptStreaming returned an error"); + public void test_textGenMultimodalVideoPromptStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalVideoPromptStreaming, + "textGenMultimodalVideoPromptStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalPdf() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalPdf, - "TextGenMultimodalPdf returned an error"); + public void test_textGenMultimodalPdf() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalPdf, + "textGenMultimodalPdf returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty"); } @Test - public void test_TextGenMultimodalPdfStreaming() { - String result = assertDoesNotThrow(TextGeneration::TextGenMultimodalPdfStreaming, - "TextGenMultimodalPdfStreaming returned an error"); + public void test_textGenMultimodalPdfStreaming() { + String result = assertDoesNotThrow(TextGeneration::textGenMultimodalPdfStreaming, + "textGenMultimodalPdfStreaming returned an error"); assertNotNull(result, "Response should not be null"); assertFalse(result.trim().isEmpty(), "Response should not be empty");