Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion genai/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>com.google.genai</groupId>
<artifactId>google-genai</artifactId>
<version>1.10.0</version>
<version>1.15.0</version>
</dependency>
<dependency>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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<GenerateContentResponse> 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]
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}