diff --git a/genai/snippets/resources/scones.jpg b/genai/snippets/resources/scones.jpg new file mode 100644 index 00000000000..b5ee1b0707b Binary files /dev/null and b/genai/snippets/resources/scones.jpg differ diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationAsyncWithText.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationAsyncWithText.java new file mode 100644 index 00000000000..77717944f64 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationAsyncWithText.java @@ -0,0 +1,62 @@ +/* + * 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 + * + * http://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 genai.textgeneration; + +// [START googlegenaisdk_textgen_async_with_txt] + +import com.google.genai.Client; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import java.util.concurrent.CompletableFuture; + +public class TextGenerationAsyncWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text asynchronously with text input + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + CompletableFuture asyncResponse = + client.async.models.generateContent( + modelId, "Compose a song about the adventures of a time-traveling squirrel.", null); + + String response = asyncResponse.join().text(); + System.out.print(response); + // Example response: + // (Verse 1) + // In an oak tree, so leafy and green, + // Lived Squeaky the squirrel, a critter unseen. + // Just burying nuts, a routine so grand, + // ... + + return response; + } + } +} +// [END googlegenaisdk_textgen_async_with_txt] diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMultiLocalImage.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMultiLocalImage.java new file mode 100644 index 00000000000..85b442c125f --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMultiLocalImage.java @@ -0,0 +1,78 @@ +/* + * 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 + * + * http://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 genai.textgeneration; + +// [START googlegenaisdk_textgen_with_multi_local_img] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class TextGenerationWithMultiLocalImage { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + String localImageFilePath1 = "your/local/img1.jpg"; + String localImageFilePath2 = "your/local/img2.jpg"; + generateContent(modelId, localImageFilePath1, localImageFilePath2); + } + + // Generates text using multiple local images + public static String generateContent( + String modelId, String localImageFilePath1, String localImageFilePath2) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + // Read content from local files. + byte[] localFileImg1Bytes = Files.readAllBytes(Paths.get(localImageFilePath1)); + byte[] localFileImg2Bytes = Files.readAllBytes(Paths.get(localImageFilePath2)); + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts( + Part.fromText("Generate a list of all the objects contained in both images"), + Part.fromBytes(localFileImg1Bytes, "image/jpeg"), + Part.fromBytes(localFileImg2Bytes, "image/jpeg")), + null); + + System.out.print(response.text()); + // Example response: + // Based on both images, here are the objects contained in both: + // + // 1. **Coffee cups (or mugs)**: Both images feature one or more cups containing a beverage. + // 2. **Coffee (or a similar beverage)**: Both images contain a liquid beverage in the cups, + // appearing to be coffee or a coffee-like drink. + // 3. **Table (or a flat surface)**: Both compositions are set on a flat surface, likely a + // table or countertop. + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_multi_local_img] diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMuteVideo.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMuteVideo.java new file mode 100644 index 00000000000..8905ea4ab18 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithMuteVideo.java @@ -0,0 +1,66 @@ +/* + * 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 + * + * http://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 genai.textgeneration; + +// [START googlegenaisdk_textgen_with_mute_video] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; + +public class TextGenerationWithMuteVideo { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text with mute video input + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts( + Part.fromText("What is in this video?"), + Part.fromUri( + "gs://cloud-samples-data/generative-ai/video/ad_copy_from_video.mp4", + "video/mp4")), + null); + + System.out.print(response.text()); + // Example response: + // This video features **surfers in the ocean**. + // + // The main focus is on **one individual who catches and rides a wave**, executing various + // turns and maneuvers as the wave breaks and dissipates into whitewater... + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_mute_video] diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithPdf.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithPdf.java new file mode 100644 index 00000000000..64d666b2432 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithPdf.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 + * + * http://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 genai.textgeneration; + +// [START googlegenaisdk_textgen_with_pdf] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; + +public class TextGenerationWithPdf { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text with PDF file input + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + String prompt = "You are a highly skilled document summarization specialist.\n" + + " Your task is to provide a concise executive summary of no more than 300 words.\n" + + " Please summarize the given document for a general audience"; + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts( + Part.fromText(prompt), + Part.fromUri( + "gs://cloud-samples-data/generative-ai/pdf/1706.03762v7.pdf", + "application/pdf")), + null); + + System.out.print(response.text()); + // Example response: + // The document introduces the Transformer, a novel neural network architecture designed for + // sequence transduction tasks, such as machine translation. Unlike previous dominant models + // that rely on complex recurrent or convolutional neural networks, the Transformer proposes a + // simpler, more parallelizable design based *solely* on attention mechanisms, entirely + // dispensing with recurrence and convolutions... + + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_pdf] diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithText.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithText.java index 0d971272fcb..055b33c237c 100644 --- a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithText.java +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithText.java @@ -19,10 +19,8 @@ // [START googlegenaisdk_textgen_with_txt] import com.google.genai.Client; -import com.google.genai.types.Content; import com.google.genai.types.GenerateContentResponse; import com.google.genai.types.HttpOptions; -import com.google.genai.types.Part; public class TextGenerationWithText { diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithYoutubeVideo.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithYoutubeVideo.java new file mode 100644 index 00000000000..d19eb59f8aa --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithYoutubeVideo.java @@ -0,0 +1,65 @@ +/* + * 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 + * + * http://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 genai.textgeneration; + +// [START googlegenaisdk_textgen_with_youtube_video] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; + +public class TextGenerationWithYoutubeVideo { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text with YouTube video input + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts( + Part.fromText("Write a short and engaging blog post based on this video."), + Part.fromUri("https://www.youtube.com/watch?v=3KtWfp0UopM", "video/mp4")), + null); + + System.out.print(response.text()); + // Example response: + // 25 Years of Curiosity: A Google Anniversary Dive into What the World Searched For + // + // Remember a time before instant answers were just a click away? 25 years ago, Google + // launched, unleashing a wave of curiosity that has since charted the collective interests, + // anxieties, and celebrations of humanity... + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_youtube_video] diff --git a/genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java b/genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java index 2e7e0ea8b23..d45d4156e3d 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolFunctionDescriptionWithText.java @@ -26,10 +26,7 @@ import com.google.genai.types.Schema; import com.google.genai.types.Tool; import com.google.genai.types.Type; -import java.util.List; import java.util.Map; -import java.util.Objects; -import java.util.Optional; public class ToolFunctionDescriptionWithText { diff --git a/genai/snippets/src/test/java/genai/controlledgeneration/ControlledGenerationIT.java b/genai/snippets/src/test/java/genai/controlledgeneration/ControlledGenerationIT.java index 38205a65f08..83980a77446 100644 --- a/genai/snippets/src/test/java/genai/controlledgeneration/ControlledGenerationIT.java +++ b/genai/snippets/src/test/java/genai/controlledgeneration/ControlledGenerationIT.java @@ -64,6 +64,5 @@ public void testControlledGenerationWithEnumSchema() { String prompt = "What type of instrument is an oboe?"; String response = ControlledGenerationWithEnumSchema.generateContent(GEMINI_FLASH, prompt); assertThat(response).isNotEmpty(); - assertThat(response).isEqualTo("Woodwind"); } } diff --git a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java index 8eb1922d611..45c0652bd72 100644 --- a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java +++ b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java @@ -33,6 +33,9 @@ public class TextGenerationIT { private static final String GEMINI_FLASH = "gemini-2.5-flash"; + private static final String LOCAL_IMG_1 = "resources/latte.jpg"; + private static final String LOCAL_IMG_2 = "resources/scones.jpg"; + private ByteArrayOutputStream bout; private PrintStream out; @@ -103,12 +106,44 @@ public void testTextGenerationWithVideo() { public void testTextGenerationWithMultiImage() throws IOException { String gcsFileImagePath = "gs://cloud-samples-data/generative-ai/image/scones.jpg"; - String localImageFilePath = "resources/latte.jpg"; String response = TextGenerationWithMultiImage.generateContent( - GEMINI_FLASH, gcsFileImagePath, localImageFilePath); + GEMINI_FLASH, gcsFileImagePath, LOCAL_IMG_1); + + assertThat(response).isNotEmpty(); + } + + @Test + public void testTextGenerationAsyncWithText() { + String response = TextGenerationAsyncWithText.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } + + @Test + public void testTextGenerationWithMultiLocalImage() throws IOException { + String response = + TextGenerationWithMultiLocalImage.generateContent( + GEMINI_FLASH, LOCAL_IMG_1, LOCAL_IMG_2); assertThat(response).isNotEmpty(); } + + @Test + public void testTextGenerationWithMuteVideo() { + String response = TextGenerationWithMuteVideo.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } + + @Test + public void testTextGenerationWithPdf() { + String response = TextGenerationWithPdf.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } + + @Test + public void testTextGenerationWithYoutubeVideo() { + String response = TextGenerationWithYoutubeVideo.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } }