in aiplatform/api/AIPlatform.Samples/ControlledGeneration.cs [269:357]
public async Task<string> GenerateContentWithResponseSchema4(
string projectId = "your-project-id",
string location = "us-central1",
string publisher = "google",
string model = "gemini-2.0-flash-001")
{
var predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{location}-aiplatform.googleapis.com"
}.Build();
var responseSchema = new OpenApiSchema
{
Type = Type.Array,
Items = new()
{
Type = Type.Object,
Properties =
{
["to_discard"] = new() { Type = Type.Integer },
["subcategory"] = new() { Type = Type.String },
["safe_handling"] = new() { Type = Type.Integer },
["item_category"] = new()
{
Type = Type.String,
Enum =
{
"clothing",
"winter apparel",
"specialized apparel",
"furniture",
"decor",
"tableware",
"cookware",
"toys"
}
},
["for_resale"] = new() { Type = Type.Integer },
["condition"] = new()
{
Type = Type.String,
Enum =
{
"new in package",
"like new",
"gently used",
"used",
"damaged",
"soiled"
}
}
}
}
};
string prompt = @"
Item description:
The item is a long winter coat that has many tears all around the seams and is falling apart.
It has large questionable stains on it.";
var generateContentRequest = new GenerateContentRequest
{
Model = $"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}",
Contents =
{
new Content
{
Role = "USER",
Parts =
{
new Part { Text = prompt }
}
}
},
GenerationConfig = new GenerationConfig
{
ResponseMimeType = "application/json",
ResponseSchema = responseSchema
},
};
GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);
string responseText = response.Candidates[0].Content.Parts[0].Text;
Console.WriteLine(responseText);
return responseText;
}