-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add provisioned throughput and controlled generation samples #3278
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
base: main
Are you sure you want to change the base?
Changes from all commits
905cb44
935c8c7
f45b27a
44a492a
928d158
31149ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ControlledGenWithEnumSchemaTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ControlledGenWithEnumSchema _sample; | ||
|
|
||
| public ControlledGenWithEnumSchemaTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ControlledGenWithEnumSchema(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestControlledGenWithEnumSchema() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
| 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 ProvisionedThroughputWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ProvisionedThroughputWithTxt _sample; | ||
|
|
||
| public ProvisionedThroughputWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ProvisionedThroughputWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestProvisionedThroughputWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(projectId: _fixture.ProjectId, throughputMode: "shared"); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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_enum_schema] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ControlledGenWithEnumSchema | ||
| { | ||
| 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" }); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "What type of instrument is an oboe?", | ||
| config: new GenerateContentConfig | ||
| { | ||
| ResponseMimeType = "application/json", | ||
| ResponseJsonSchema = new Dictionary<string, object> | ||
| { | ||
| { "type", "string" }, | ||
| { "enum", new List<string> { "Percussion", "String", "Woodwind", "Brass", "Keyboard" } } | ||
| } | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
| System.Console.WriteLine(responseText); | ||
| // Example response: | ||
| // Woodwind | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_ctrlgen_with_enum_schema] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * 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_provisionedthroughput_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ProvisionedThroughputWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "us-central1", | ||
| string model = "gemini-2.5-flash", | ||
| string throughputMode = "dedicated") | ||
| { | ||
|
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. Some of the samples have a try-catch to catch the errors. I think that we should aim for that here as well just to 1. showcase some simple error handling, and 2. keep consistency with other languages that have a try-catch as well.
Contributor
Author
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. Thanks for the suggestion, but as far as I know, it is not necessary to implement exception handling in the samples, unless there are specific error handling cases that we need to show, which, in my opinion, does not apply in this case.
Contributor
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. in general, we recommend it so that if there is some kind of issue with the service that you can print that out rather than the user assuming something is broken with the sample. This reduces frustration and gives the user a path to move forward. |
||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions | ||
| { | ||
| ApiVersion = "v1", | ||
| Headers = new Dictionary<string, string> | ||
| { | ||
| // Options: | ||
| // - "dedicated": Use Provisioned Throughput | ||
| // - "shared": Use pay-as-you-go | ||
| // https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput | ||
| { "X-Vertex-AI-LLM-Request-Type", throughputMode } | ||
| } | ||
| }); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync(model: model, contents: "How does AI work?"); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // At its core, **Artificial Intelligence (AI)** works by enabling machines to learn from data, | ||
| // identify patterns, and make decisions or predictions without being explicitly programmed for | ||
| // every possible scenario... | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_provisionedthroughput_with_txt] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.