|
| 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 | +package genai.controlledgeneration; |
| 18 | + |
| 19 | +// [START googlegenaisdk_ctrlgen_with_nullable_schema] |
| 20 | + |
| 21 | +import com.google.genai.Client; |
| 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.Schema; |
| 26 | +import com.google.genai.types.Type; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +public class ControlledGenerationWithNullableSchema { |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + // TODO(developer): Replace these variables before running the sample. |
| 34 | + String modelId = "gemini-2.5-flash"; |
| 35 | + |
| 36 | + String prompt = |
| 37 | + "The week ahead brings a mix of weather conditions.\n" |
| 38 | + + "Sunday is expected to be sunny with a temperature " |
| 39 | + + "of 77°F and a humidity level of 50%. " |
| 40 | + + "Winds will be light at around 10 km/h.\n" |
| 41 | + + "Monday will see partly cloudy skies with " |
| 42 | + + "a slightly cooler temperature of 72°F and the winds " |
| 43 | + + "will pick up slightly to around 15 km/h.\n" |
| 44 | + + "Tuesday brings rain showers, with temperatures dropping " |
| 45 | + + "to 64°F and humidity rising to 70%.\n" |
| 46 | + + "Wednesday may see thunderstorms, with a temperature of 68°F.\n" |
| 47 | + + "Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%.\n" |
| 48 | + + "Friday returns to partly cloudy conditions, with " |
| 49 | + + "a temperature of 73°F and the Winds will be " |
| 50 | + + "light at 12 km/h.\n" |
| 51 | + + "Finally, Saturday rounds off the week with sunny skies, a " |
| 52 | + + "temperature of 80°F, and a humidity " |
| 53 | + + "level of 40%. Winds will be gentle at 8 km/h.\n"; |
| 54 | + |
| 55 | + generateContent(modelId, prompt); |
| 56 | + } |
| 57 | + |
| 58 | + // Generates content with a nullable response schema |
| 59 | + public static String generateContent(String modelId, String contents) { |
| 60 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 61 | + // once, and can be reused for multiple requests. |
| 62 | + try (Client client = |
| 63 | + Client.builder() |
| 64 | + .location("global") |
| 65 | + .vertexAI(true) |
| 66 | + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) |
| 67 | + .build()) { |
| 68 | + |
| 69 | + // Define schema for array items (each weather entry object) |
| 70 | + Schema dayForecastSchema = |
| 71 | + Schema.builder() |
| 72 | + .type(Type.Known.OBJECT) |
| 73 | + .properties( |
| 74 | + Map.of( |
| 75 | + "Day", Schema.builder().type(Type.Known.STRING).nullable(true).build(), |
| 76 | + "Forecast", Schema.builder().type(Type.Known.STRING).nullable(true).build(), |
| 77 | + "Temperature", |
| 78 | + Schema.builder().type(Type.Known.INTEGER).nullable(true).build(), |
| 79 | + "Humidity", Schema.builder().type(Type.Known.STRING).nullable(true).build(), |
| 80 | + "Wind Speed", |
| 81 | + Schema.builder().type(Type.Known.INTEGER).nullable(true).build())) |
| 82 | + .required(List.of("Day", "Temperature", "Forecast", "Wind Speed")) |
| 83 | + .build(); |
| 84 | + |
| 85 | + // Full response schema |
| 86 | + Schema responseSchema = |
| 87 | + Schema.builder() |
| 88 | + .type(Type.Known.OBJECT) |
| 89 | + .properties( |
| 90 | + Map.of( |
| 91 | + "forecast", |
| 92 | + Schema.builder().type(Type.Known.ARRAY).items(dayForecastSchema).build())) |
| 93 | + .build(); |
| 94 | + |
| 95 | + GenerateContentConfig config = |
| 96 | + GenerateContentConfig.builder() |
| 97 | + .responseMimeType("application/json") |
| 98 | + .responseSchema(responseSchema) |
| 99 | + .build(); |
| 100 | + |
| 101 | + GenerateContentResponse response = client.models.generateContent(modelId, contents, config); |
| 102 | + |
| 103 | + System.out.println(response.text()); |
| 104 | + // Example response: |
| 105 | + // {"forecast": [{"Day": "Sunday", "Forecast": "sunny", "Temperature": 77, "Wind Speed": 10, |
| 106 | + // "Humidity": "50%"}, |
| 107 | + // {"Day": "Monday", "Forecast": "partly cloudy", "Temperature": 72, "Wind Speed": 15}, |
| 108 | + // {"Day": "Tuesday", "Forecast": "rain showers", "Temperature": 64, "Wind Speed": null, |
| 109 | + // "Humidity": "70%"}, |
| 110 | + // {"Day": "Wednesday", "Forecast": "thunderstorms", "Temperature": 68, "Wind Speed": null}, |
| 111 | + // {"Day": "Thursday", "Forecast": "cloudy", "Temperature": 66, "Wind Speed": null, |
| 112 | + // "Humidity": "60%"}, |
| 113 | + // {"Day": "Friday", "Forecast": "partly cloudy", "Temperature": 73, "Wind Speed": 12}, |
| 114 | + // {"Day": "Saturday", "Forecast": "sunny", "Temperature": 80, "Wind Speed": 8, "Humidity": |
| 115 | + // "40%"}]} |
| 116 | + return response.text(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +// [END googlegenaisdk_ctrlgen_with_nullable_schema] |
0 commit comments