-
Notifications
You must be signed in to change notification settings - Fork 8
Initialization
This page describes how to initialize the C# Generative AI SDK to interact with Google's Generative AI models, including Gemini. The library supports initialization using environment variables or manual initialization through constructors and platform adapters.
The simplest way to initialize the SDK is by setting environment variables. This method is particularly useful for quick setup and development. The SDK will automatically detect and use these variables. Below is a list of supported environment variables:
-
GOOGLE_AI_MODEL
: The name of the model to use. See the Models page for a list of available models. If not set, a default model may be used. -
GOOGLE_API_KEY
: The API key generated in Google AI Studio. This is required for accessing the Gemini API. See the Authentication page for details on obtaining an API key. -
GOOGLE_PROJECT_ID
: Project ID in Google Cloud to access the APIs, especially when using Vertex AI. -
GOOGLE_REGION
: Region in Google Cloud. This is relevant for Vertex AI deployments. -
GOOGLE_ACCESS_TOKEN
: The access token required to use models running in Vertex AI. -
GOOGLE_APPLICATION_CREDENTIALS
: Path to the application credentials file. This is an alternative authentication method, particularly useful for server-side applications. See the Authentication page for more information. -
GOOGLE_WEB_CREDENTIALS
: Path to a Web credentials file. This is used for web applications. See the Authentication page for more information.
Example:
// No explicit code is needed when using environment variables.
// The SDK will automatically detect and use them.
// Example of setting an environment variable (you would typically do this outside your application)
Environment.SetEnvironmentVariable("GOOGLE_API_KEY", "YOUR_API_KEY");
// Now, when you create an instance of the client, it will automatically use the API key.
var ai = new GoogleAI(); // Or VertexAI if appropriate
var model = ai.CreateGenerativeModel(GoogleAIModels.Gemini2Flash); // Create a specific model instance
// Use the 'model' object to interact with the chosen model.
For more control over the initialization process, you can manually initialize the SDK using constructors and platform adapters. This is particularly useful when you need to configure specific settings or when you are working in an environment where environment variables are not suitable.
You can directly create instances of the GoogleAI
(for Gemini API) or VertexAI
(for Vertex AI) classes, providing the necessary credentials and configuration options. You then use the CreateGenerativeModel()
method to specify the model you wish to use.
Example (Gemini API):
using Google.GenerativeAI; // Or appropriate namespace
// Using API Key
var ai = new GoogleAI("YOUR_API_KEY");
var model = ai.CreateGenerativeModel(GoogleAIModels.Gemini2Flash); // Create a specific model instance
// Using Application Default Credentials (ADC)
var ai = new GoogleAI(); // ADC will be automatically detected
var model = ai.CreateGenerativeModel(GoogleAIModels.Gemini2Flash); // Create a specific model instance
// You can also use other constructors for more specific scenarios.
Example (Vertex AI):
using Google.Cloud.AIPlatform.V1; // Or appropriate namespace
// Initialize Vertex AI client with project and location
string projectId = "your-project-id";
string location = "us-central1"; // Or your preferred region
var ai = new VertexAI(projectId, location);
var model = ai.CreateGenerativeModel(VertexAIModels.Gemini.Gemini2Flash); // Create a specific model instance
// You can also use other constructors for more specific scenarios, like setting credentials.
For more advanced scenarios, you can use platform adapters. This allows you to customize the underlying platform interaction.
Example:
using Google.GenerativeAI.Platforms; // Or appropriate namespace
// Create a Google AI Platform Adapter
IPlatformAdapter googleAIAdapter = new GoogleAIPlatformAdapter("YOUR_API_KEY");
// Create a client using the adapter
var ai = new GoogleAI(googleAIAdapter);
var model = ai.CreateGenerativeModel(GoogleAIModels.Gemini2Flash); // Create a specific model instance
// Similarly for Vertex AI
IPlatformAdapter vertexAIAdapter = new VertexAIPlatformAdapter(projectId, location);
var ai = new VertexAI(vertexAIAdapter);
var model = ai.CreateGenerativeModel(VertexAIModels.Gemini.Gemini2Flash); // Create a specific model instance
- Environment Variables: Best for quick setup, development, and simple deployments.
- Constructors: Provides more control over configuration and credentials. Suitable for most use cases.
- Platform Adapters: For advanced scenarios requiring custom platform interaction.