Skip to content

Commit d31606a

Browse files
committed
--no-edit
1 parent 6af5bc1 commit d31606a

File tree

4 files changed

+216
-51
lines changed

4 files changed

+216
-51
lines changed

genai/src/main/java/genai/GenerateContentWithRouting.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,46 @@
1616

1717
package genai;
1818

19-
// [START genai_generate_content_with_routing]
20-
import com.google.genai.Client;
21-
import com.google.genai.types.GenerateContentResponse;
22-
import com.google.genai.types.GenerateContentConfig;
23-
import com.google.genai.types.ModelSelectionConfig;
24-
import com.google.genai.types.HttpOptions;
25-
26-
19+
// [START genai_generate_content_with_routing]
20+
import com.google.genai.Client;
21+
import com.google.genai.types.GenerateContentConfig;
22+
import com.google.genai.types.GenerateContentResponse;
23+
import com.google.genai.types.HttpOptions;
24+
import com.google.genai.types.ModelSelectionConfig;
25+
2726
public class GenerateContentWithRouting {
28-
29-
public static void main(String[] args) throws Exception {
3027

28+
public static void main(String[] args) throws Exception {
29+
30+
// TODO(developer): Replace these variables before running the sample.
31+
String promptText = "Why do we have 365 days in a year?";
32+
String featureSelectionPreference = "PRIORITIZE_COST";
33+
34+
String generateContentText = generateContent(promptText, featureSelectionPreference);
35+
36+
System.out.println("Response: " + generateContentText);
37+
}
38+
39+
public static String generateContent(String promptText, String featureSelectionPreference) {
40+
41+
ModelSelectionConfig modelSelectionConfig =
42+
ModelSelectionConfig.builder()
43+
.featureSelectionPreference(featureSelectionPreference)
44+
.build();
45+
46+
GenerateContentConfig generateContentConfig =
47+
GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build();
3148

32-
String modelName = "model-optimizer-exp-04-09";
49+
String modelName = "model-optimizer-exp-04-09";
3350

34-
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build();
51+
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build();
3552

36-
Client client = Client.builder().httpOptions(httpOptions).vertexAI(true).build();
53+
Client client = Client.builder().httpOptions(httpOptions).build();
3754

38-
ModelSelectionConfig modelSelectionConfig = ModelSelectionConfig.builder().featureSelectionPreference("PRIORITIZE_COST").build();
39-
40-
GenerateContentConfig generateContentConfig = GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build();
55+
GenerateContentResponse response =
56+
client.models.generateContent(modelName, promptText, generateContentConfig);
4157

42-
GenerateContentResponse response =
43-
client.models.generateContent(modelName, "Why do we have 365 days in a year?", generateContentConfig);
44-
45-
System.out.println("Response: " + response.text());
46-
}
47-
}
48-
// [END genai_generate_content_with_routing]
58+
return response.text();
59+
}
60+
}
61+
// [END genai_generate_content_with_routing]

genai/src/main/java/genai/StreamGenerateContentWithRouting.java

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,56 @@
1414
* limitations under the License.
1515
*/
1616

17-
package genai;
17+
package genai;
1818

19-
// [START genai_stream_generate_content_with_routing]
20-
import com.google.genai.Client;
21-
import com.google.genai.types.GenerateContentResponse;
22-
import com.google.genai.types.GenerateContentConfig;
23-
import com.google.genai.types.ModelSelectionConfig;
24-
import com.google.genai.types.HttpOptions;
25-
import com.google.genai.ResponseStream;
19+
// [START genai_stream_generate_content_with_routing]
20+
import com.google.genai.Client;
21+
import com.google.genai.ResponseStream;
22+
import com.google.genai.types.GenerateContentConfig;
23+
import com.google.genai.types.GenerateContentResponse;
24+
import com.google.genai.types.HttpOptions;
25+
import com.google.genai.types.ModelSelectionConfig;
2626

27-
2827
public class StreamGenerateContentWithRouting {
29-
30-
public static void main(String[] args) throws Exception {
3128

29+
public static void main(String[] args) throws Exception {
3230

33-
String modelName = "model-optimizer-exp-04-09";
31+
// TODO(developer): Replace these variables before running the sample.
32+
String promptText = "Why do we have 365 days in a year?";
33+
String featureSelectionPreference = "BALANCED";
3434

35-
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build();
35+
String generateContentStreamText =
36+
generateContentStream(promptText, featureSelectionPreference);
3637

37-
Client client = Client.builder().httpOptions(httpOptions).vertexAI(true).build();
38+
System.out.println("Response: " + generateContentStreamText);
39+
}
3840

39-
ModelSelectionConfig modelSelectionConfig = ModelSelectionConfig.builder().featureSelectionPreference("BALANCED").build();
40-
41-
GenerateContentConfig generateContentConfig = GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build();
41+
public static String generateContentStream(String promptText, String featureSelectionPreference) {
4242

43-
ResponseStream<GenerateContentResponse> responseStream =
44-
client.models.generateContentStream(modelName, "Why do we have 365 days in a year?", generateContentConfig);
45-
46-
System.out.println("Streaming response: ");
47-
for (GenerateContentResponse res : responseStream) {
48-
System.out.print(res.text());
49-
}
43+
ModelSelectionConfig modelSelectionConfig =
44+
ModelSelectionConfig.builder()
45+
.featureSelectionPreference(featureSelectionPreference)
46+
.build();
5047

51-
responseStream.close();
52-
}
53-
}
54-
// [END genai_stream_generate_content_with_routing]
48+
GenerateContentConfig generateContentConfig =
49+
GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build();
50+
51+
String modelName = "model-optimizer-exp-04-09";
52+
53+
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build();
54+
55+
Client client = Client.builder().httpOptions(httpOptions).build();
56+
57+
ResponseStream<GenerateContentResponse> responseStream =
58+
client.models.generateContentStream(modelName, promptText, generateContentConfig);
59+
60+
String streamResponse = "";
61+
62+
for (GenerateContentResponse res : responseStream) {
63+
streamResponse += res.text();
64+
}
65+
66+
return streamResponse;
67+
}
68+
}
69+
// [END genai_stream_generate_content_with_routing]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Tests for Gemini code samples.
18+
19+
package genai;
20+
21+
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth.assertWithMessage;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public class GenerateContentWithRoutingIT {
34+
35+
// Check if the required environment variables are set.
36+
public static void requireEnvVar(String envVarName) {
37+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
38+
.that(System.getenv(envVarName))
39+
.isNotEmpty();
40+
}
41+
42+
@BeforeClass
43+
public static void setUp() throws IOException {
44+
try (PrintStream out = System.out) {
45+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
46+
System.setOut(new PrintStream(stdOut));
47+
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
51+
stdOut.close();
52+
System.setOut(out);
53+
}
54+
}
55+
56+
@Test
57+
public void testGenerateContentWithRouting() throws Exception {
58+
String textPrompt =
59+
"What's a good name for a flower shop that specializes in selling bouquets of"
60+
+ " dried flowers?";
61+
62+
String featureSelectionPreference = "PRIORITIZE_COST";
63+
64+
String output =
65+
GenerateContentWithRouting.generateContent(textPrompt, featureSelectionPreference);
66+
assertThat(output).isNotEmpty();
67+
}
68+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Tests for Gemini code samples.
18+
19+
package genai;
20+
21+
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth.assertWithMessage;
23+
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.IOException;
26+
import java.io.PrintStream;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
import org.junit.runners.JUnit4;
31+
32+
@RunWith(JUnit4.class)
33+
public class StreamGenerateContentWithRoutingIT {
34+
35+
// Check if the required environment variables are set.
36+
public static void requireEnvVar(String envVarName) {
37+
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName))
38+
.that(System.getenv(envVarName))
39+
.isNotEmpty();
40+
}
41+
42+
@BeforeClass
43+
public static void setUp() throws IOException {
44+
try (PrintStream out = System.out) {
45+
ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
46+
System.setOut(new PrintStream(stdOut));
47+
48+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
49+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
50+
51+
stdOut.close();
52+
System.setOut(out);
53+
}
54+
}
55+
56+
@Test
57+
public void testStreamGenerateContentWithRouting() throws Exception {
58+
String textPrompt =
59+
"What's a good name for a flower shop that specializes in selling bouquets of"
60+
+ " dried flowers?";
61+
62+
String featureSelectionPreference = "PRIORITIZE_QUALITY";
63+
64+
String output =
65+
StreamGenerateContentWithRouting.generateContentStream(
66+
textPrompt, featureSelectionPreference);
67+
assertThat(output).isNotEmpty();
68+
}
69+
}

0 commit comments

Comments
 (0)