diff --git a/genai/snippets/pom.xml b/genai/snippets/pom.xml index 27a35283eed..b93e29cebe3 100644 --- a/genai/snippets/pom.xml +++ b/genai/snippets/pom.xml @@ -51,7 +51,7 @@ com.google.genai google-genai - 1.10.0 + 1.15.0 junit diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRouting.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRouting.java new file mode 100644 index 00000000000..e5ab6d3468b --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRouting.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_routing] + +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.ModelSelectionConfig; + +public class TextGenerationWithRouting { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String promptText = "Why do we have 365 days in a year?"; + String featureSelectionPreference = "PRIORITIZE_COST"; + + String generateContentText = generateContent(promptText, featureSelectionPreference); + + System.out.println("Response: " + generateContentText); + } + + // Generates text with text input and routing option + public static String generateContent(String promptText, String featureSelectionPreference) { + + // Model name for Model Optimizer + String modelName = "model-optimizer-exp-04-09"; + + ModelSelectionConfig modelSelectionConfig = + ModelSelectionConfig.builder() + .featureSelectionPreference(featureSelectionPreference) + .build(); + + GenerateContentConfig generateContentConfig = + GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build(); + + // 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("us-central1") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1beta1").build()) + .build()) { + + GenerateContentResponse response = + client.models.generateContent(modelName, promptText, generateContentConfig); + + System.out.print(response.text()); + + return response.text(); + } + } +} +// [END googlegenaisdk_textgen_with_routing] diff --git a/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRoutingAndTextStream.java b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRoutingAndTextStream.java new file mode 100644 index 00000000000..4851559a921 --- /dev/null +++ b/genai/snippets/src/main/java/genai/textgeneration/TextGenerationWithRoutingAndTextStream.java @@ -0,0 +1,76 @@ +/* + * 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_routing_and_text_stream] + +import com.google.genai.Client; +import com.google.genai.ResponseStream; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.ModelSelectionConfig; + +public class TextGenerationWithRoutingAndTextStream { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String promptText = "Why do we have 365 days in a year?"; + String featureSelectionPreference = "BALANCED"; + + String generateContentStreamText = generateContent(promptText, featureSelectionPreference); + + System.out.println("Response: " + generateContentStreamText); + } + + // Generates text with text input and routing option + public static String generateContent(String promptText, String featureSelectionPreference) { + + // Model name for Model Optimizer + String modelName = "model-optimizer-exp-04-09"; + + ModelSelectionConfig modelSelectionConfig = + ModelSelectionConfig.builder() + .featureSelectionPreference(featureSelectionPreference) + .build(); + + GenerateContentConfig generateContentConfig = + GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build(); + + // 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("us-central1") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1beta1").build()) + .build()) { + + ResponseStream response = + client.models.generateContentStream(modelName, promptText, generateContentConfig); + + String streamResponse = ""; + + for (GenerateContentResponse res : response) { + streamResponse += res.text(); + } + + return streamResponse; + } + } +} +// [END googlegenaisdk_textgen_with_routing_and_text_stream] diff --git a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java index 45c0652bd72..838bf450985 100644 --- a/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java +++ b/genai/snippets/src/test/java/genai/textgeneration/TextGenerationIT.java @@ -146,4 +146,28 @@ public void testTextGenerationWithYoutubeVideo() { String response = TextGenerationWithYoutubeVideo.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); } + + @Test + public void testTextGenerationWithRouting() throws IOException { + String textPrompt = + "What's a good name for a flower shop that specializes in selling bouquets of" + + " dried flowers?"; + String featureSelectionPreference = "PRIORITIZE_COST"; + String response = + TextGenerationWithRouting.generateContent(textPrompt, featureSelectionPreference); + assertThat(response).isNotEmpty(); + } + + @Test + public void testTextGenerationWithRoutingAndTextStream() throws IOException { + String textPrompt = + "What's a good name for a flower shop that specializes in selling bouquets of" + + " dried flowers?"; + String featureSelectionPreference = "PRIORITIZE_COST"; + + String response = + TextGenerationWithRoutingAndTextStream.generateContent( + textPrompt, featureSelectionPreference); + assertThat(response).isNotEmpty(); + } }