-
Notifications
You must be signed in to change notification settings - Fork 8
Authentication
For Vertex AI, an access token is generally required. While some parts of Google AI, such as semantic retrieval endpoints, also necessitate an access token. The Google_Generative SDK provides several ways to handle authentication and obtain the necessary authorization access token. This page outlines the authentication methods available for both Google AI and Vertex AI.
The following authentication methods are supported:
Google AI:
-
Authentication with an API key: This is the simplest method. Developers can provide the API key as an environment variable (
GOOGLE_API_KEY
) or pass it directly to the constructor of the relevant client class (e.g.,GoogleAi
). - Authentication with OAuth: Required for accessing tuned models and semantic retrieval endpoints. This method involves obtaining an access token through the OAuth 2.0 flow.
Vertex AI:
- Authentication with Application Default Credentials (ADC): ADC automatically discovers credentials from the environment (e.g., gcloud, environment variables). This is often the recommended approach for development and local testing.
-
Authentication with OAuth: Uses the
Google_GenerativeAI.Auth
package. This allows for programmatic OAuth 2.0 flow. -
Authentication with Service Account: Uses the
Google_GenerativeAI.Auth
package. This is suitable for server-side applications where a service account's credentials are used to obtain an access token. -
Express Mode with an API key: A simplified method for using an API key with Vertex AI. This is enabled by passing the
apiKey
andexpressMode = true
options to theVertexAI
constructor.
For API key authorization, developers can set the GOOGLE_API_KEY
environment variable or provide the key directly to the constructor of the relevant client class (e.g. VertexAI
, GoogleAi
).
For access token authorization, users can use the optional authenticator
parameter in the client constructor. The Google_GenerativeAI.Auth
package provides implementations for several authenticators:
-
GoogleCloudAdcAuthenticator
: For ADC authentication. -
GoogleOAuthAuthenticator
: For OAuth authentication. -
GoogleServiceAccountAuthenticator
: For service account authentication.
Users can pass an instance of the chosen authenticator to the authenticator
named parameter of the appropriate class constructor, such as GoogleAi
, VertexAI
, or the platform adapter classes like VertexPlatformAdapter
or GoogleAIPlatformAdapter
.
API Key Authentication (Google AI):
// Setting API Key as Environment Variable
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "YOUR_API_KEY");
// Using GoogleAi client
var googleAiClient = new GoogleAi(); // API Key will be picked up from the environment variable
// Or pass it to the client constructor
var googleAiClient = new GoogleAi("YOUR_API_KEY");
ADC Authentication (Vertex AI):
// Using ADC authentication
var vertexAIClient = new VertexAI(authenticator: new GoogleCloudAdcAuthenticator());
// Or using VertexPlatformAdapter
var platformAdapter = new VertexPlatformAdapter(authenticator: new GoogleCloudAdcAuthenticator());
var vertexAIClient = new VertexAI(platformAdapter);
OAuth Authentication (Vertex AI):
// Assuming you have obtained an access token
string accessToken = "YOUR_ACCESS_TOKEN";
var vertexAIClient = new VertexAI(authenticator: new GoogleOAuthAuthenticator(accessToken));
// Or using VertexPlatformAdapter
var platformAdapter = new VertexPlatformAdapter(authenticator: new GoogleOAuthAuthenticator(accessToken));
var vertexAIClient = new VertexAI(platformAdapter);
Service Account Authentication (Vertex AI):
// Path to your service account JSON key file
string serviceAccountKeyPath = "path/to/your/service_account.json";
var vertexAIClient = new VertexAI(authenticator: new GoogleServiceAccountAuthenticator(serviceAccountKeyPath));
// Or using VertexPlatformAdapter
var platformAdapter = new VertexPlatformAdapter(authenticator: new GoogleServiceAccountAuthenticator(serviceAccountKeyPath));
var vertexAIClient = new VertexAI(platformAdapter: platformAdapter);
Express Mode Authentication (Vertex AI):
string apiKey = "YOUR_API_KEY";
var vertexAIClient = new VertexAI(apiKey: apiKey, expressMode: true);