Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
{

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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;
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]

2 changes: 1 addition & 1 deletion genai/api/GenAI.Samples/TextGeneration/TextGenWithPdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Your task is to provide a concise executive summary of no more than 300 words.

string responseText = response.Candidates[0].Content.Parts[0].Text;
Console.WriteLine(responseText);
// Example reponse:
// Example response:
// This paper introduces the Transformer, a novel neural network architecture designed
// for processing sequences, such as those found in language translation. Traditionally,
// such tasks have relied on complex recurrent or convolutional neural networks...
Expand Down
2 changes: 1 addition & 1 deletion genai/api/GenAI.Samples/TextGeneration/TextGenWithTxt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task<string> GenerateContent(

string responseText = response.Candidates[0].Content.Parts[0].Text;
Console.WriteLine(responseText);
// Example reponse:
// Example response:
// AI, or Artificial Intelligence, at its core, is about creating machines that can perform...
// Here's a breakdown of how it generally works...
return responseText;
Expand Down