diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatStreamWithText.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatStreamWithText.java new file mode 100644 index 00000000000..6e811475223 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatStreamWithText.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_chat_stream_with_txt] + +import com.google.genai.Chat; +import com.google.genai.Client; +import com.google.genai.ResponseStream; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; + +public class TextGenerationChatStreamWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Shows how to create a new chat session stream + public static String generateContent(String modelId) { + // Client Initialization. Once created, it can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + Chat chatSession = client.chats.create(modelId); + StringBuilder responseTextBuilder = new StringBuilder(); + + try (ResponseStream response = + chatSession.sendMessageStream("Why is the sky blue?")) { + + for (GenerateContentResponse chunk : response) { + System.out.println(chunk.text()); + responseTextBuilder.append(chunk.text()); + } + + } + // Example response: + // + // The sky is blue primarily due to a phenomenon called **Rayleigh scattering**, + // named after the British physicist Lord Rayleigh. Here's a breakdown of how... + return responseTextBuilder.toString(); + } + } +} +// [END googlegenaisdk_textgen_chat_stream_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatWithText.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatWithText.java new file mode 100644 index 00000000000..a43da40a176 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationChatWithText.java @@ -0,0 +1,61 @@ +/* + * 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_chat_with_txt] + +import com.google.genai.Chat; +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; + +public class TextGenerationChatWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Shows how to create a chat session + public static String generateContent(String modelId) { + // Client Initialization. Once created, it can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + // Create a new chat session + Chat chatSession = client.chats.create(modelId); + + GenerateContentResponse response = chatSession.sendMessage("Tell me a story"); + System.out.print(response.text()); + // Example response: + // + // In the heart of the Whispering Peaks lay the Valley of Silent Echoes, a place perpetually + // shrouded in a twilight mist. No birds sang there, no rivers flowed, and the few trees that + // clung to its edges were gnarled and bare... + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_chat_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationTranscriptWithGcsAudio.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationTranscriptWithGcsAudio.java new file mode 100644 index 00000000000..ac9d0cca929 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationTranscriptWithGcsAudio.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_transcript_with_gcs_audio] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; + +public class TextGenerationTranscriptWithGcsAudio { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates transcript with audio input + public static String generateContent(String modelId) { + // Client Initialization. Once created, it can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + String prompt = + "Transcribe the interview, in the format of timecode, speaker, caption.\n" + + "Use speaker A, speaker B, etc. to identify speakers."; + + // Enable audioTimestamp to generate timestamps for audio-only files. + GenerateContentConfig contentConfig = + GenerateContentConfig.builder().audioTimestamp(true).build(); + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts( + Part.fromUri( + "gs://cloud-samples-data/generative-ai/audio/pixel.mp3", "audio/mpeg"), + Part.fromText(prompt)), + contentConfig); + + System.out.print(response.text()); + // Example response: + // 00:00 - Speaker A: your devices are getting better over time. And so we think about it... + // 00:14 - Speaker B: Welcome to the Made by Google Podcast, where we meet the people who... + // 00:41 - Speaker A: So many features. I am a singer, so I actually think recorder... + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_transcript_with_gcs_audio] \ No newline at end of file diff --git a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java index f28734d1758..773d4197695 100644 --- a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java +++ b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java @@ -63,60 +63,59 @@ public void tearDown() { } @Test - public void testTextGenerationWithTextStream() { - String prompt = "Why is the sky blue?"; - String response = TextGenerationWithTextStream.generateContent(GEMINI_FLASH, prompt); + public void testTextGenerationAsyncWithText() { + String response = TextGenerationAsyncWithText.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithSystemInstruction() { - String response = TextGenerationWithSystemInstruction.generateContent(GEMINI_FLASH); + public void testTextGenerationChatStreamWithText() { + String response = TextGenerationChatStreamWithText.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithText() { - String response = TextGenerationWithText.generateContent(GEMINI_FLASH); + public void testTextGenerationChatWithText() { + String response = TextGenerationChatWithText.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithTextAndImage() { - String response = TextGenerationWithTextAndImage.generateContent(GEMINI_FLASH); + public void testTextGenerationCodeWithPdf() { + String response = TextGenerationCodeWithPdf.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithVideo() { - - String prompt = - " Analyze the provided video file, including its audio.\n" - + " Summarize the main points of the video concisely.\n" - + " Create a chapter breakdown with timestamps for key sections or topics discussed."; - - String response = TextGenerationWithVideo.generateContent(GEMINI_FLASH, prompt); + public void testTextGenerationConfigWithText() { + String response = TextGenerationConfigWithText.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); - assertThat(response).ignoringCase().contains("Tokyo"); - assertThat(response).ignoringCase().contains("Pixel"); } @Test - public void testTextGenerationWithMultiImage() throws IOException { - - String gcsFileImagePath = "gs://cloud-samples-data/generative-ai/image/scones.jpg"; - String localImageFilePath = "resources/latte.jpg"; + public void testTextGenerationTranscriptWithGcsAudio() { + String response = TextGenerationTranscriptWithGcsAudio.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } - String response = - TextGenerationWithMultiImage.generateContent( - GEMINI_FLASH, gcsFileImagePath, localImageFilePath); + @Test + public void testTextGenerationWithGcsAudio() { + String response = TextGenerationWithGcsAudio.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } + @Test + public void testTextGenerationWithLocalVideo() throws IOException { + String response = TextGenerationWithLocalVideo.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationAsyncWithText() { - String response = TextGenerationAsyncWithText.generateContent(GEMINI_FLASH); + public void testTextGenerationWithMultiImage() throws IOException { + String gcsFileImagePath = "gs://cloud-samples-data/generative-ai/image/scones.jpg"; + String response = + TextGenerationWithMultiImage.generateContent( + GEMINI_FLASH, gcsFileImagePath, LOCAL_IMG_1); assertThat(response).isNotEmpty(); } @@ -125,7 +124,6 @@ public void testTextGenerationWithMultiLocalImage() throws IOException { String response = TextGenerationWithMultiLocalImage.generateContent( GEMINI_FLASH, LOCAL_IMG_1, LOCAL_IMG_2); - assertThat(response).isNotEmpty(); } @@ -142,32 +140,47 @@ public void testTextGenerationWithPdf() { } @Test - public void testTextGenerationWithYoutubeVideo() { - String response = TextGenerationWithYoutubeVideo.generateContent(GEMINI_FLASH); + public void testTextGenerationWithSystemInstruction() { + String response = TextGenerationWithSystemInstruction.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationCodeWithPdf() { - String response = TextGenerationCodeWithPdf.generateContent(GEMINI_FLASH); + public void testTextGenerationWithText() { + String response = TextGenerationWithText.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationConfigWithText() { - String response = TextGenerationConfigWithText.generateContent(GEMINI_FLASH); + public void testTextGenerationWithTextAndImage() { + String response = TextGenerationWithTextAndImage.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithGcsAudio() { - String response = TextGenerationWithGcsAudio.generateContent(GEMINI_FLASH); + public void testTextGenerationWithTextStream() { + String prompt = "Why is the sky blue?"; + String response = TextGenerationWithTextStream.generateContent(GEMINI_FLASH, prompt); assertThat(response).isNotEmpty(); } @Test - public void testTextGenerationWithLocalVideo() throws IOException { - String response = TextGenerationWithLocalVideo.generateContent(GEMINI_FLASH); + public void testTextGenerationWithVideo() { + String prompt = + " Analyze the provided video file, including its audio.\n" + + " Summarize the main points of the video concisely.\n" + + " Create a chapter breakdown with timestamps for key sections or topics discussed."; + + String response = TextGenerationWithVideo.generateContent(GEMINI_FLASH, prompt); + assertThat(response).isNotEmpty(); + assertThat(response).ignoringCase().contains("Tokyo"); + assertThat(response).ignoringCase().contains("Pixel"); + } + + @Test + public void testTextGenerationWithYoutubeVideo() { + String response = TextGenerationWithYoutubeVideo.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } + } \ No newline at end of file