-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add controlled generation samples (2) #3284
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
Open
jdomingr
wants to merge
1
commit into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-controlledgeneration-samples
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.
Open
Changes from all commits
Commits
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
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/ControlledGeneration/ControlledGenWithNullableSchemaTest.cs
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,38 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ControlledGenWithNullableSchemaTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ControlledGenWithNullableSchema _sample; | ||
|
|
||
| public ControlledGenWithNullableSchemaTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ControlledGenWithNullableSchema(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestControlledGenWithNullableSchema() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/ControlledGeneration/ControlledGenWithRespSchemaTest.cs
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,38 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ControlledGenWithRespSchemaTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ControlledGenWithRespSchema _sample; | ||
|
|
||
| public ControlledGenWithRespSchemaTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ControlledGenWithRespSchema(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestControlledGenWithRespSchema() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
137 changes: 137 additions & 0 deletions
137
genai/api/GenAI.Samples/ControlledGeneration/ControlledGenWithNullableSchema.cs
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,137 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_ctrlgen_with_nullable_schema] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ControlledGenWithNullableSchema | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| string prompt = @"The week ahead brings a mix of weather conditions. | ||
| Sunday is expected to be sunny with a temperature of 77°F and a humidity level | ||
| of 50%. Winds will be light at around 10 km/h. | ||
| Monday will see partly cloudy skies with a slightly cooler temperature of 72°F | ||
| and the winds will pick up slightly to around 15 km/h. | ||
| Tuesday brings rain showers, with temperatures dropping to 64°F and humidity rising to 70%. | ||
| Wednesday may see thunderstorms, with a temperature of 68°F. | ||
| Thursday will be cloudy with a temperature of 66°F and moderate humidity at 60%. | ||
| Friday returns to partly cloudy conditions, with a temperature of 73°F and the | ||
| Winds will be light at 12 km/h. | ||
| Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F, | ||
| and a humidity level of 40%. Winds will be gentle at 8 km/h."; | ||
|
|
||
| var responseSchema = new Dictionary<string, object> | ||
| { | ||
| { "type", "object" }, | ||
| { | ||
| "properties", new Dictionary<string, object> | ||
| { | ||
| { | ||
| "forecast", new Dictionary<string, object> | ||
| { | ||
| { "type", "array" }, | ||
| { | ||
| "items", new Dictionary<string, object> | ||
| { | ||
| { "type", "object" }, | ||
| { | ||
| "properties", new Dictionary<string, object> | ||
| { | ||
| { | ||
| "Day", new Dictionary<string, object> | ||
| { | ||
| { "type", "string" }, | ||
| { "nullable", true } | ||
| } | ||
| }, | ||
| { | ||
| "Forecast", new Dictionary<string, object> | ||
| { | ||
| { "type", "string" }, | ||
| { "nullable", true } | ||
| } | ||
| }, | ||
| { | ||
| "Temperature", new Dictionary<string, object> | ||
| { | ||
| { "type", "integer" }, | ||
| { "nullable", true } | ||
| } | ||
| }, | ||
| { | ||
| "Humidity", new Dictionary<string, object> | ||
| { | ||
| { "type", "string" }, | ||
| { "nullable", true } | ||
| } | ||
| }, | ||
| { | ||
| "Wind Speed", new Dictionary<string, object> | ||
| { | ||
| { "type", "integer" }, | ||
| { "nullable", true } | ||
| } | ||
| } | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }, | ||
| { "required", new List<string> { "Day", "Temperature", "Forecast", "Wind Speed" } } | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: prompt, | ||
| config: new GenerateContentConfig | ||
| { | ||
| ResponseMimeType = "application/json", | ||
| ResponseJsonSchema = responseSchema | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // {"forecast": [{"Day": "Sunday", "Temperature": 77, "Forecast": "sunny", "Wind Speed": 10, "Humidity": "50%"}, | ||
| // {"Day": "Monday", "Temperature": 72, "Forecast": "partly cloudy", "Wind Speed": 15, "Humidity": null}, | ||
| // {"Day": "Tuesday", "Temperature": 64, "Forecast": "rain showers", "Wind Speed": null, "Humidity": "70%"}, | ||
| // {"Day": "Wednesday", "Temperature": 68, "Forecast": "thunderstorms", "Wind Speed": null, "Humidity": null}, | ||
| // {"Day": "Thursday", "Temperature": 66, "Forecast": "cloudy", "Wind Speed": null, "Humidity": "60%"}, | ||
| // {"Day": "Friday", "Temperature": 73, "Forecast": "partly cloudy", "Wind Speed": 12, "Humidity": null}, | ||
| // {"Day": "Saturday", "Temperature": 80, "Forecast": "sunny", "Wind Speed": 8, "Humidity": "40%"}]} | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_ctrlgen_with_nullable_schema] | ||
118 changes: 118 additions & 0 deletions
118
genai/api/GenAI.Samples/ControlledGeneration/ControlledGenWithRespSchema.cs
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,118 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_ctrlgen_with_resp_schema] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ControlledGenWithRespSchema | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| string prompt = "List a few popular cookie recipes."; | ||
|
|
||
| var responseSchema = new Dictionary<string, object> | ||
| { | ||
| { "type", "array" }, | ||
| { | ||
| "items", new Dictionary<string, object> | ||
| { | ||
| { "type", "object" }, | ||
| { | ||
| "properties", new Dictionary<string, object> | ||
| { | ||
| { | ||
| "recipe_name", new Dictionary<string, object> | ||
| { | ||
| { "type", "string" } | ||
| } | ||
| }, | ||
| { | ||
| "ingredients", new Dictionary<string, object> | ||
| { | ||
| { "type", "array" }, | ||
| { | ||
| "items", new Dictionary<string, object> | ||
| { | ||
| { "type", "string" }, | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| { "required", new List<string> { "recipe_name", "ingredients" } } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: prompt, | ||
| config: new GenerateContentConfig | ||
| { | ||
| ResponseMimeType = "application/json", | ||
| ResponseJsonSchema = responseSchema | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // [ | ||
| // { | ||
| // "recipe_name": "Classic Chocolate Chip Cookies", | ||
| // "ingredients": [ | ||
| // "all-purpose flour", | ||
| // "baking soda", | ||
| // "salt", | ||
| // "unsalted butter", | ||
| // "granulated sugar", | ||
| // "brown sugar", | ||
| // "eggs", | ||
| // "vanilla extract", | ||
| // "chocolate chips"] | ||
| // }, | ||
| // { | ||
| // "recipe_name": "Peanut Butter Cookies", | ||
| // "ingredients": [ | ||
| // "all-purpose flour", | ||
| // "baking soda", | ||
| // "salt", | ||
| // "unsalted butter", | ||
| // "peanut butter", | ||
| // "granulated sugar", | ||
| // "brown sugar", | ||
| // "eggs", | ||
| // "vanilla extract"] | ||
| // } | ||
| // ] | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_ctrlgen_with_resp_schema] | ||
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.
Uh oh!
There was an error while loading. Please reload this page.