8000 Add controlled generation support with response mime type and JSON sc… · suztomo/java-docs-samples@1488406 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1488406

Browse files
authored
Add controlled generation support with response mime type and JSON schema (GoogleCloudPlatform#9427)
* feat(vertexai): Multimodal snippets using Gemini 1.5 * feat(vertexai): Multimodal snippets using Gemini 1.5 (checkstyle and header tweaks) * feat(vertexai): Multimodal snippets using Gemini 1.5 (incorporating feedback, fixing region tags, and clarifying comments) * feat(vertexai): Use gemini-1.5-flash-001 instead of the preview model * Reset the initial backoff time to its original setting * feat(vertexai): Updated to use Gemini 1.5 Flash and new samples - added some advanced variants of existing snippets - added grounding with private data and with Google Search web results - added new multimodality streaming/non-streaming examples - updated some samples with Gemini 1.5 Flash - updated/added some region tags for integration in the documentation * fix assertion in the grounding with private data test case * - split the token count and grounding samples to have one file per feature - disable a flaky test assertion * - added some extra comments for clarity - updated the region tag locations * - added a couple more comments for clarity and reference * feat(vertexai): Add controlled generation (constrained decoding) * feat(vertexai): Add controlled generation (constrained decoding) (small typo)
1 parent 54a2cfc commit 1488406

9 files changed

+648
-3
lines changed

vertexai/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<groupId>com.google.cloud</groupId>
4747
<scope>import</scope>
4848
<type>pom</type>
49-
<version>26.39.0</version>
49+
<version>26.43.0</version>
5050
</dependency>
5151
</dependencies>
5252
</dependencyManagement>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 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+
package vertexai.gemini;
18+
19+
// [START generativeaionvertexai_gemini_controlled_generation_response_mime_type]
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.GenerateContentResponse;
22+
import com.google.cloud.vertexai.api.GenerationConfig;
23+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
24+
import com.google.cloud.vertexai.generativeai.ResponseHandler;
25+
import java.io.IOException;
26+
27+
public class ControlledGenerationMimeType {
28+
public static void main(String[] args) throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "genai-java-demos";
31+
String location = "us-central1";
32+
String modelName = "gemini-1.5-flash-001";
33+
34+
controlGenerationWithMimeType(projectId, location, modelName);
35+
}
36+
37+
// Generate responses that are always valid JSON
38+
public static String controlGenerationWithMimeType(
39+
String projectId, String location, String modelName)
40+
throws IOException {
41+
// Initialize client that will be used to send requests. This client only needs
42+
// to be created once, and can be reused for multiple requests.
43+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
44+
GenerationConfig generationConfig = GenerationConfig.newBuilder()
45+
.setResponseMimeType("application/json")
46+
.build();
47+
48+
GenerativeModel model = new GenerativeModel(modelName, vertexAI)
49+
.withGenerationConfig(generationConfig);
50+
51+
GenerateContentResponse response = model.generateContent(
52+
"List a few popular cookie recipes using this JSON schema:\n"
53+
+ "Recipe = {\"recipe_name\": str}\n"
54+
+ "Return: list[Recipe]"
55+
);
56+
57+
String output = ResponseHandler.getText(response);
58+
System.out.println(output);
59+
return output;
60+
}
61+
}
62+
}
63+
// [END generativeaionvertexai_gemini_controlled_generation_response_mime_type]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2024 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+
package vertexai.gemini;
18+
19+
// [START generativeaionvertexai_gemini_controlled_generation_response_schema]
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.GenerateContentResponse;
22+
import com.google.cloud.vertexai.api.GenerationConfig;
23+
import com.google.cloud.vertexai.api.Schema;
24+
import com.google.cloud.vertexai.api.Type;
25+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
26+
import com.google.cloud.vertexai.generativeai.ResponseHandler;
27+
import java.io.IOException;
28+
import java.util.Collections;
29+
30+
public class ControlledGenerationSchema {
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "genai-java-demos";
34+
String location = "us-central1";
35+
String modelName = "gemini-1.5-pro-001";
36+
37+
controlGenerationWithJsonSchema(projectId, location, modelName);
38+
}
39+
40+
// Generate responses that are always valid JSON and comply with a JSON schema
41+
public static String controlGenerationWithJsonSchema(
42+
String projectId, String location, String modelName)
43+
throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs
45+
// to be created once, and can be reused for multiple requests.
46+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
47+
GenerationConfig generationConfig = GenerationConfig.newBuilder()
48+
.setResponseMimeType("application/json")
49+
.setResponseSchema(Schema.newBuilder()
50+
.setType(Type.ARRAY)
51+
.setItems(Schema.newBuilder()
52+
.setType(Type.OBJECT)
53+
.putProperties("recipe_name", Schema.newBuilder().setType(Type.STRING).build())
54+
.addAllRequired(Collections.singletonList("recipe_name"))
55+
.build())
56+
.build())
57+
.build();
58+
59+
GenerativeModel model = new GenerativeModel(modelName, vertexAI)
60+
.withGenerationConfig(generationConfig);
61+
62+
GenerateContentResponse response = model.generateContent(
63+
"List a few popular cookie recipes."
64+
);
65+
66+
String output = ResponseHandler.getText(response);
67+
System.out.println(output);
68+
return output;
69+
}
70+
}
71+
}
72+
// [END generativeaionvertexai_gemini_controlled_generation_response_schema]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 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+
package vertexai.gemini;
18+
19+
// [START generativeaionvertexai_gemini_controlled_generation_response_schema_2]
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.GenerateContentResponse;
22+
import com.google.cloud.vertexai.api.GenerationConfig;
23+
import com.google.cloud.vertexai.api.Schema;
24+
import com.google.cloud.vertexai.api.Type;
25+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
26+
import com.google.cloud.vertexai.generativeai.ResponseHandler;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
30+
public class ControlledGenerationSchema2 {
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "genai-java-demos";
34+
String location = "us-central1";
35+
String modelName = "gemini-1.5-pro-001";
36+
37+
controlGenerationWithJsonSchema2(projectId, location, modelName);
38+
}
39+
40+
// Generate responses that are always valid JSON and comply with a JSON schema
41+
public static String controlGenerationWithJsonSchema2(
42+
String projectId, String location, String modelName)
43+
throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs
45+
// to be created once, and can be reused for multiple requests.
46+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
47+
GenerationConfig generationConfig = GenerationConfig.newBuilder()
48+
.setResponseMimeType("application/json")
49+
.setResponseSchema(Schema.newBuilder()
50+
.setType(Type.ARRAY)
51+
.setItems(Schema.newBuilder()
52+
.setType(Type.OBJECT)
53+
.putProperties("rating", Schema.newBuilder().setType(Type.INTEGER).build())
54+
.putProperties("flavor", Schema.newBuilder().setType(Type.STRING).build())
55+
.addAllRequired(Arrays.asList("rating", "flavor"))
56+
.build())
57+
.build())
58+
.build();
59+
60+
GenerativeModel model = new GenerativeModel(modelName, vertexAI)
61+
.withGenerationConfig(generationConfig);
62+
63+
GenerateContentResponse response = model.generateContent(
64+
"Reviews from our social media:\n"
65+
+ "\"Absolutely loved it! Best ice cream I've ever had.\" "
66+
+ "Rating: 4, Flavor: Strawberry Cheesecake\n"
67+
+ "\"Quite good, but a bit too sweet for my taste.\" "
68+
+ "Rating: 1, Flavor: Mango Tango"
69+
);
70+
71+
String output = ResponseHandler.getText(response);
72+
System.out.println(output);
73+
return output;
74+
}
75+
}
76+
}
77+
// [END generativeaionvertexai_gemini_controlled_generation_response_schema_2]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2024 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+
package vertexai.gemini;
18+
19+
// [START generativeaionvertexai_gemini_controlled_generation_response_schema_3]
20+
import com.google.cloud.vertexai.VertexAI;
21+
import com.google.cloud.vertexai.api.GenerateContentResponse;
22+
import com.google.cloud.vertexai.api.GenerationConfig;
23+
import com.google.cloud.vertexai.api.Schema;
24+
import com.google.cloud.vertexai.api.Type;
25+
import com.google.cloud.vertexai.generativeai.GenerativeModel;
26+
import com.google.cloud.vertexai.generativeai.ResponseHandler;
27+
import java.io.IOException;
28+
import java.util.Arrays;
29+
30+
public class ControlledGenerationSchema3 {
31+
public static void main(String[] args) throws IOException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "genai-java-demos";
34+
String location = "us-central1";
35+
String modelName = "gemini-1.5-pro-001";
36+
37+
controlGenerationWithJsonSchema3(projectId, location, modelName);
38+
}
39+
40+
// Generate responses that are always valid JSON and comply with a JSON schema
41+
public static String controlGenerationWithJsonSchema3(
42+
String projectId, String location, String modelName)
43+
throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs
45+
// to be created once, and can be reused for multiple requests.
46+
try (VertexAI vertexAI = new VertexAI(projectId, location)) {
47+
GenerationConfig generationConfig = GenerationConfig.newBuilder()
48+
.setResponseMimeType("application/json")
49+
.setResponseSchema(Schema.newBuilder()
50+
.setType(Type.OBJECT)
51+
.putProperties("forecast", Schema.newBuilder()
52+
.setType(Type.ARRAY)
53+
.setItems(Schema.newBuilder()
54+
.setType(Type.OBJECT)
55+
.putProperties("Day", Schema.newBuilder()
56+
.setType(Type.STRING)
57+
.build())
58+
.putProperties("Forecast", Schema.newBuilder()
59+
.setType(Type.STRING)
60+
.build())
61+
.putProperties("Humidity", Schema.newBuilder()
62+
.setType(Type.STRING)
63+
.build())
64+
.putProperties("Temperature", Schema.newBuilder()
65+
.setType(Type.INTEGER)
66+
.build())
67+
.putProperties("Wind Speed", Schema.newBuilder()
68+
.setType(Type.INTEGER)
69+
.build())
70+
.addAllRequired(Arrays.asList("Day", "Temperature", "Forecast"))
71+
.build())
72+
.build())
73+
)
74+
.build();
75+
76+
GenerativeModel model = new GenerativeModel(modelName, vertexAI)
77+
.withGenerationConfig(generationConfig);
78+
79+
GenerateContentResponse response = model.generateContent(
80+
"The week ahead brings a mix of weather conditions.\n"
81+
+ "Sunday is expected to be sunny with a temperature of 77°F and a humidity level "
82+
+ "of 50%. Winds will be light at around 10 km/h.\n"
83+
+ "Monday will see partly cloudy skies with a slightly cooler temperature of 72°F "
84+
+ "and humidity increasing to 55%. Winds will pick up slightly to around 15 km/h.\n"
85+
+ "Tuesday brings rain showers, with temperatures dropping to 64°F and humidity"
86+
+ "rising to 70%. Expect stronger winds at 20 km/h.\n"
87+
+ "Wednesday may see thunderstorms, with a temperature of 68°F and high humidity "
88+
+ "of 75%. Winds will be gusty at 25 km/h.\n"
89+
+ "Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. "
90+
+ "Winds will ease slightly to 18 km/h.\n"
91+
+ "Friday returns to partly cloudy conditions, with a temperature of 73°F and lower "
92+
+ "humidity at 45%. Winds will be light at 12 km/h.\n"
93+
+ "Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, "
94+
+ "and a humidity level of 40%. Winds will be gentle at 8 km/h."
95+
);
96+
97+
String output = ResponseHandler.getText(response);
98+
System.out.println(output);
99+
return output;
100+
}
101+
}
102+
}
103+
// [END generativeaionvertexai_gemini_controlled_generation_response_schema_3]

0 commit comments

Comments
 (0)
0