This quickstart shows you how to get started with the Gemini API in Vertex AI on Google Cloud using an SDK for .NET called Mscc.GenerativeAI.
Prerequisites
To complete this quickstart locally, ensure that your .NET development meets the following requirements:
- .NET 6+ or
- .NET Framework 4.7.2+
- Account on Google Cloud
The NuGet package also supports .NET Standard 2.0.
Get set up on Google Cloud
To get you started using Vertex AI, follow the instructions in the official guide. You need to complete the following steps:
- Set up a project
- Enable billing for the Google Cloud project
- Enable Vertex AI APIs
- Install the Google Cloud CLI
- Set up Application Default Credentials (ADC)
In conclusion, you need the project ID associated with your project, and choose a supported region.
In order to keep private, sensitive information and secrets out of your source code repositories, it is recommended to use either Environment Variables, User Secrets, or a Key/Secrets Manager to retrieve data like an API key.
Here, I'm going to create an .env
file and place it into the project folder with the following content.
GOOGLE_PROJECT_ID=<your project ID>
GOOGLE_REGION=us-central1
To access the value of a variable that is defined in a .env
file, use $dotenv
. More details are described under Environment Variables in the official documentation.
Set the file properties with a Build action of None
and copy instructions of Copy, if newer
.
<None Update=".env" Condition="Exists('.env')">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Install the SDK / NuGet package
The SDK for .NET for the Gemini API is contained in the Mscc.GenerativeAI
package. Install the dependency using dotnet CLI:
dotnet add package Mscc.GenerativeAI
See README for alternative options, like the NuGet Package Manager.
Note: This is a quickstart to Gemini on Vertex AI. If you need additional authentication options see README of Mscc.GenerativeAI and consider to add the package reference Mscc.GenerativeAI.Google which is based on the .NET Cloud Client Libraries.
Initialize the Generative Model
Before you can make any API calls, you need to add a reference to the namespace Mscc.GenerativeAI
and initialize the Generative Model.
using Mscc.GenerativeAI;
var vertexAI = new VertexAI(
projectId: Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"),
region: Environment.GetEnvironmentVariable("GOOGLE_REGION"));
var model = vertexAI.GenerativeModel(model: Model.Gemini10Pro);
The necessary access token to authenticate against Google Cloud is retrieved from the Application Default Credentials or the Google Client Library, in case that you are using the Mscc.GenerativeAI.Google package.
Generate Text
var prompt = "Write a poem about Japanese koi.";
var responseStream = model.GenerateContentStream(prompt);
// Use StringBuilder to append the streamed responses
await foreach (var response in responseStream)
{
var response = await model.GenerateContent();
// optional, check for response.Candidates[0].FinishReason
Console.WriteLine(response.Text);
}
Note: The package Mscc.GenerativeAI offers a unified developer experience across Google AI and Vertex AI provided by the interface IGenerativeAI. Meaning, the non-streaming method `GenerateContent` is available in Vertex AI and works as expected.
What's next
Explore the README of the NuGet package which has more samples documented. All unit tests are accessible in the GitHub repository:
If you're new to generative AI models, you might want to look at the concepts guide and the Gemini API overview before trying a quickstart.
What's the difference from Google AI Gemini API
The Vertex AI Gemini API and Google AI Gemini API both let you incorporate the capabilities of Gemini models into your applications. The platform that's right for you depends on your goals.
The Vertex AI Gemini API is designed for developers and enterprises for use in scaled deployments. It offers features such as enterprise security, data residency, performance, and technical support. If you're an existing Google Cloud customer or deploy medium to large scale applications, you're in the right place.
If you're a hobbyist, student, or developer who is new to Google Cloud, try the Google AI Gemini API, which is suitable for experimentation, prototyping, and small deployments. If you're looking for a way to use Gemini directly from your mobile and web apps, see the Google AI SDKs for Android, Swift, and web.
Image credit: Gemini using prompt Create an image showing the fast start of a race in motor sport.