Class VertextPlatformAdapter
- Namespace
- GenerativeAI
- Assembly
- GenerativeAI.dll
The VertextPlatformAdapter class provides authentication, configuration, and utility methods for interacting with Vertex AI.
public class VertextPlatformAdapter : IPlatformAdapter
- Inheritance
-
VertextPlatformAdapter
- Implements
- Inherited Members
- Extension Methods
Constructors
VertextPlatformAdapter(string, string, IGoogleAuthenticator, string?, bool, ILogger?)
Constructor for setting mandatory fields for Vertex AI operations.
public VertextPlatformAdapter(string projectId, string region, IGoogleAuthenticator authenticator, string? apiVersion = null, bool validateAccessToken = true, ILogger? logger = null)
Parameters
projectIdstringThe Google Cloud Project ID.
regionstringThe region for Vertex AI.
authenticatorIGoogleAuthenticatorThe authenticator to be used for authentication.
apiVersionstringThe API version to use. Defaults to GOOGLE_GENAI_API_VERSION if set, otherwise v1beta1.
validateAccessTokenboolBoolean flag for validating the access token.
loggerILoggerOptional logger instance for diagnostics.
VertextPlatformAdapter(string?, string?, bool, string?, string?, string?, IGoogleAuthenticator?, string?, bool, ILogger?)
Constructor for initializing the adapter using environment variables or manual parameters.
public VertextPlatformAdapter(string? projectId = null, string? region = null, bool expressMode = false, string? apiKey = null, string? accessToken = null, string? apiVersion = null, IGoogleAuthenticator? authenticator = null, string? credentialsFile = null, bool validateAccessToken = true, ILogger? logger = null)
Parameters
projectIdstringThe Google Cloud Project ID.
regionstringThe region for Vertex AI.
expressModeboolFlag to denote if Express Mode should be used.
apiKeystringOptional API key for authentication.
accessTokenstringOptional access token for authentication.
apiVersionstringThe API version to use. Defaults to GOOGLE_GENAI_API_VERSION if set, otherwise v1beta1.
authenticatorIGoogleAuthenticatorOptional authenticator instance for custom token management.
credentialsFilestringPath to the credentials file.
validateAccessTokenboolWhether access tokens should be validated before use.
loggerILoggerOptional logger instance for diagnostics.
Properties
Authenticator
The authenticator interface responsible for obtaining access tokens.
public IGoogleAuthenticator? Authenticator { get; set; }
Property Value
CredentialFile
The path to the credentials file.
public string? CredentialFile { get; set; }
Property Value
Credentials
The credentials object containing API key and access token.
public GoogleAICredentials? Credentials { get; set; }
Property Value
DefaultApiVersion
The default API version to use when making requests.
public string DefaultApiVersion { get; set; }
Property Value
ExpressMode
Indicates whether Express Mode is enabled.
public bool? ExpressMode { get; }
Property Value
- bool?
ProjectId
The Google Cloud Project ID.
public string? ProjectId { get; }
Property Value
Publisher
Publisher information, defaulting to "google".
public string Publisher { get; set; }
Property Value
Region
The region in which the Vertex AI service is hosted.
public string Region { get; }
Property Value
Methods
AddAuthorizationAsync(HttpRequestMessage, bool, CancellationToken)
Adds authorization headers to an HTTP request.
public Task AddAuthorizationAsync(HttpRequestMessage request, bool requireAccessToken, CancellationToken cancellationToken = default)
Parameters
requestHttpRequestMessageThe HTTP request message to be modified.
requireAccessTokenboolIndicates if an access token is required.
cancellationTokenCancellationTokenOptional cancellation token for task cancellation.
Returns
AuthorizeAsync(CancellationToken)
Authorizes the request by generating or refreshing the access token.
public Task AuthorizeAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenOptional cancellation token for task cancellation.
Returns
CreateUrlForModel(string, string)
Generates the URL for a specific model and task.
public string CreateUrlForModel(string modelId, string task)
Parameters
Returns
- string
The constructed URL as a string.
CreateUrlForTunedModel(string, string)
Generates the URL for a specific tuned model and task.
public string CreateUrlForTunedModel(string modelId, string task)
Parameters
Returns
- string
The constructed URL as a string.
GetAccessTokenAsync(CancellationToken)
Retrieves the access token required for making authenticated API requests.
public Task<AuthTokens?> GetAccessTokenAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe cancellation token to cancel the operation if needed.
Returns
- Task<AuthTokens>
A task that represents the asynchronous operation, containing the authentication tokens.
GetApiVersion()
Retrieves the current API version used by the platform.
public string GetApiVersion()
Returns
- string
The API version as a string.
GetApiVersionForFile()
Retrieves the API version specifically for file operations.
public string GetApiVersionForFile()
Returns
- string
The API version as an object (v1Beta by default).
GetBaseUrl(bool, bool, bool, bool)
Constructs the base URL for API requests with optional components. Pattern: https://{region}-aiplatform.googleapis.com/{version}/projects/{projectId}/locations/{region}/publishers/{publisher}
public string GetBaseUrl(bool appendVersion = true, bool appendPublisher = true, bool appendLocation = true, bool appendProject = true)
Parameters
appendVersionboolIndicates whether to append the version to the URL.
appendPublisherboolIndicates whether to append the publisher to the URL.
appendLocationboolIndicates whether to include the location/region in the URL.
appendProjectboolIndicates whether to include the project ID in the URL.
Returns
- string
The constructed base URL string.
GetBaseUrlForFile()
Gets the base URL specifically for file-related operations.
public string GetBaseUrlForFile()
Returns
- string
The constructed base URL string for file operations.
GetFullyQualifiedModelName(string, bool)
Gets the formatted model name for multi-modal live sessions.
public string? GetFullyQualifiedModelName(string modelName, bool appendLocation = true)
Parameters
modelNamestringThe base model name to format.
appendLocationboolWhether to include the location segment in the qualified name.
Returns
- string
The formatted model name for multi-modal live sessions, or null if not applicable.
GetMultiModalLiveUrl(string)
Gets the WebSocket URL for multi-modal live sessions.
public string GetMultiModalLiveUrl(string version = "v1alpha")
Parameters
versionstringThe API version to use (default: "v1alpha").
Returns
- string
The WebSocket URL for multi-modal live sessions.
SetAuthenticator(IGoogleAuthenticator)
Sets the Google authenticator responsible for managing authentication processes for Google services.
public void SetAuthenticator(IGoogleAuthenticator authenticator)
Parameters
authenticatorIGoogleAuthenticatorThe Google authenticator to be used for handling authentication operations.
ValidateCredentialsAsync(CancellationToken)
Validates the credentials and refreshes the token if necessary.
public Task ValidateCredentialsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenOptional cancellation token for task cancellation.