This sample application is written using the .NET 8 minimal Web API to demonstrate calling the Vertex Generative AI API with the Gemini model.
Using Vertex AI Studio in the Google Cloud Consle you can easily design and test prompts. You can also quickly generate code using the <> GET CODE button in the upper right.
- This sample assumes you have a GCP project with the Vertex AI API enabled
- You have installed the gcloud cli
You can find your project id in the GCP console under the project settings. Update your appsettings.json
:
{
"projectId": "YOUR_GCP_PROJECT_ID_HERE"
}
Unfortunately, there is not a Vertex SDK for .NET yet. Thankfully all GCP APIs have a REST API which makes it easy for us to wrap with C# using the .NET HttpClient.
We are going to use the GCP Application Default Credentials which provides an easy abstraction to authenticate with GCP APIs. When running locally it will look for an environment variable named GOOGLE_APPLICATION_CREDENTIALS
which contains the path to a JSON file with your GCP credentials. When you deploy to GCP (i.e. Cloud Run, GKE, GCE, etc...) the SDK will automatically authenticate using the metadata server, so you don't have to pass in any credentials.
Use these instructions to generate your credentials file. For example on linux follow these steps:
gcloud auth application-default login
export GOOGLE_APPLICATION_CREDENTIALS=~/.config/gcloud/application_default_credentials.json
The code to get the required access token is in the GetAccessTokenAsync()
method of VertexAIModelGenerator.cs
:
using var client = new HttpClient();
// ...
var accessToken = await GetAccessTokenAsync();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
// ...
private async Task<string> GetAccessTokenAsync()
{
// Use Application Default Credentials
var credential = await GoogleCredential.GetApplicationDefaultAsync();
// Create credential scoped to GCP APIs
var scoped = credential.CreateScoped(
new[] { "https://www.googleapis.com/auth/cloud-platform" });
var accessToken = await scoped.UnderlyingCredential.GetAccessTokenForRequestAsync();
return accessToken;
}
As an added bonus you can use Duet AI assistance with Google's Cloud Code Extension for VS Code.