-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Model optimizer #10086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nardosalemu
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
nardosalemu:model-optimizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+493
−0
Draft
Model optimizer #10086
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2025 Google Inc. | ||
|
||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>jar</packaging> | ||
<groupId>com.example.genai</groupId> | ||
<artifactId>genai-snippets</artifactId> | ||
<name>Google Cloud Gen AI Snippets</name> | ||
<url>https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/vision</url> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in any way. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.2.0</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<scope>import</scope> | ||
<type>pom</type> | ||
<version>26.43.0</version> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.genai</groupId> | ||
<artifactId>google-genai</artifactId> | ||
<version>0.5.0</version> | ||
</dependency> | ||
|
||
<!-- test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.4.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
// [START genai_generate_content_with_routing] | ||
import com.google.genai.Client; | ||
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 GenerateContentWithRouting { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// 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); | ||
} | ||
|
||
public static String generateContent(String promptText, String featureSelectionPreference) { | ||
|
||
ModelSelectionConfig modelSelectionConfig = | ||
ModelSelectionConfig.builder() | ||
.featureSelectionPreference(featureSelectionPreference) | ||
.build(); | ||
|
||
GenerateContentConfig generateContentConfig = | ||
GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build(); | ||
|
||
String modelName = "model-optimizer-exp-04-09"; | ||
|
||
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build(); | ||
|
||
Client client = Client.builder().httpOptions(httpOptions).build(); | ||
|
||
GenerateContentResponse response = | ||
client.models.generateContent(modelName, promptText, generateContentConfig); | ||
|
||
return response.text(); | ||
} | ||
} | ||
// [END genai_generate_content_with_routing] |
69 changes: 69 additions & 0 deletions
69
genai/src/main/java/genai/StreamGenerateContentWithRouting.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* 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; | ||
|
||
// [START genai_stream_generate_content_with_routing] | ||
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 StreamGenerateContentWithRouting { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
// 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 = | ||
generateContentStream(promptText, featureSelectionPreference); | ||
|
||
System.out.println("Response: " + generateContentStreamText); | ||
} | ||
|
||
public static String generateContentStream(String promptText, String featureSelectionPreference) { | ||
|
||
ModelSelectionConfig modelSelectionConfig = | ||
ModelSelectionConfig.builder() | ||
.featureSelectionPreference(featureSelectionPreference) | ||
.build(); | ||
|
||
GenerateContentConfig generateContentConfig = | ||
GenerateContentConfig.builder().modelSelectionConfig(modelSelectionConfig).build(); | ||
|
||
String modelName = "model-optimizer-exp-04-09"; | ||
|
||
HttpOptions httpOptions = HttpOptions.builder().apiVersion("v1beta1").build(); | ||
|
||
Client client = Client.builder().httpOptions(httpOptions).build(); | ||
|
||
ResponseStream<GenerateContentResponse> responseStream = | ||
client.models.generateContentStream(modelName, promptText, generateContentConfig); | ||
|
||
String streamResponse = ""; | ||
|
||
for (GenerateContentResponse res : responseStream) { | ||
streamResponse += res.text(); | ||
} | ||
|
||
return streamResponse; | ||
} | ||
} | ||
// [END genai_stream_generate_content_with_routing] |
70 changes: 70 additions & 0 deletions
70
genai/src/test/java/genai/GenerateContentWithRoutingIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* 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. | ||
*/ | ||
|
||
// Tests for Gemini code samples. | ||
|
||
package genai; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static com.google.common.truth.Truth.assertWithMessage; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class GenerateContentWithRoutingIT { | ||
|
||
// Check if the required environment variables are set. | ||
public static void requireEnvVar(String envVarName) { | ||
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) | ||
.that(System.getenv(envVarName)) | ||
.isNotEmpty(); | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() throws IOException { | ||
try (PrintStream out = System.out) { | ||
ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(stdOut)); | ||
|
||
requireEnvVar("GOOGLE_GENAI_USE_VERTEXAI"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
requireEnvVar("GOOGLE_CLOUD_LOCATION"); | ||
|
||
stdOut.close(); | ||
System.setOut(out); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGenerateContentWithRouting() throws Exception { | ||
String textPrompt = | ||
"What's a good name for a flower shop that specializes in selling bouquets of" | ||
+ " dried flowers?"; | ||
|
||
String featureSelectionPreference = "PRIORITIZE_COST"; | ||
|
||
String output = | ||
GenerateContentWithRouting.generateContent(textPrompt, featureSelectionPreference); | ||
assertThat(output).isNotEmpty(); | ||
System.out.println(output); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
genai/src/test/java/genai/StreamGenerateContentWithRoutingIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* | ||
* 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. | ||
*/ | ||
|
||
// Tests for Gemini code samples. | ||
|
||
package genai; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static com.google.common.truth.Truth.assertWithMessage; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class StreamGenerateContentWithRoutingIT { | ||
|
||
// Check if the required environment variables are set. | ||
public static void requireEnvVar(String envVarName) { | ||
assertWithMessage(String.format("Missing environment variable '%s' ", envVarName)) | ||
.that(System.getenv(envVarName)) | ||
.isNotEmpty(); | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() throws IOException { | ||
try (PrintStream out = System.out) { | ||
ByteArrayOutputStream stdOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(stdOut)); | ||
|
||
requireEnvVar("GOOGLE_GENAI_USE_VERTEXAI"); | ||
requireEnvVar("GOOGLE_CLOUD_PROJECT"); | ||
requireEnvVar("GOOGLE_CLOUD_LOCATION"); | ||
|
||
stdOut.close(); | ||
System.setOut(out); | ||
} | ||
} | ||
|
||
@Test | ||
public void testStreamGenerateContentWithRouting() throws Exception { | ||
String textPrompt = | ||
"What's a good name for a flower shop that specializes in selling bouquets of" | ||
+ " dried flowers?"; | ||
|
||
String featureSelectionPreference = "PRIORITIZE_QUALITY"; | ||
|
||
String output = | ||
StreamGenerateContentWithRouting.generateContentStream( | ||
textPrompt, featureSelectionPreference); | ||
assertThat(output).isNotEmpty(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The copyright year is set to 2025. Please confirm that this is the correct year, or update it to the current year if appropriate.