A client for ASP.NET Core designed to consume Gemini AI.
Install
| Product | Versions |
|---|---|
| .NET Standard |
README
Gemini AI Client for .NET and ASP.NET Core
Access the Gemini API in Google AI Studio and Google Cloud Vertex AI.
Install the package
Install the package Mscc.GenerativeAI from NuGet. You can install the package from the command line using either the command line or the NuGet Package Manager Console. Or you add it directly to your .NET project.
Add the package using the dotnet command line tool in your .NET project folder.
> dotnet add package Mscc.GenerativeAI
Working with Visual Studio use the NuGet Package Manager to install the package Mscc.GenerativeAI.
PM> Install-Package Mscc.GenerativeAI
Alternatively, add the following line to your .csproj file.
<ItemGroup>
<PackageReference Include="Mscc.GenerativeAI" Version="0.4.5" />
</ItemGroup>
You can then add this code to your sources whenever you need to access any Gemini API provided by Google. This package works for Google AI (Google AI Studio) and Google Cloud Vertex AI.
Authentication use cases
The package supports the following use cases to authenticate.
- Google AI: Authentication with an API key
- Google AI: Authentication with OAuth
- Vertex AI: Authentication with Application Default Credentials (ADC)
This applies mainly to the instantiation procedure.
Getting Started
Use of Gemini API in either Google AI or Vertex AI is almost identical. The major difference is the way to instantiate the model handling your prompt.
Choose an API and authentication mode
Google AI with an API key
using Mscc.GenerativeAI;
// Google AI with an API key
var model = new GenerativeModel(apiKey: "your API key", model: Model.GeminiPro);
Google AI with OAuth. Use gcloud auth application-default print-access-token to get the access token.
using Mscc.GenerativeAI;
// Google AI with OAuth. Use `gcloud auth application-default print-access-token` to get the access token.
var model = new GenerativeModel(model: Model.GeminiPro);
model.AccessToken = accessToken;
Vertex AI with OAuth. Use gcloud auth application-default print-access-token to get the access token.
using Mscc.GenerativeAI;
// Vertex AI with OAuth. Use `gcloud auth application-default print-access-token` to get the access token.
var vertex = new VertexAI(projectId: projectId, region: region);
var model = vertex.GenerativeModel(model: Model.GeminiPro);
model.AccessToken = accessToken;
The ConfigurationFixture type in the test project implements multiple options to retrieve sensitive information, ie. API key or access token.
Using Google AI Gemini API
Working with Google AI in your application requires an API key. Get an API key from Google AI Studio.
using Mscc.GenerativeAI;
var apiKey = "your_api_key";
var prompt = "Write a story about a magic backpack.";
var model = new GenerativeModel(apiKey: apiKey, model: Model.GeminiPro);
var response = model.GenerateContent(prompt).Result;
Console.WriteLine(response.Text);
Using Vertex AI Gemini API
Use of Vertex AI requires an account on Google Cloud, a project with billing and Vertex AI API enabled.
using Mscc.GenerativeAI;
var projectId = "your_google_project_id"; // the ID of a project, not its name.
var region = "us-central1"; // see documentation for available regions.
var accessToken = "your_access_token"; // use `gcloud auth application-default print-access-token` to get it.
var prompt = "Write a story about a magic backpack.";
var vertex = new VertexAI(projectId: projectId, region: region);
var model = vertex.GenerativeModel(model: Model.GeminiPro);
model.AccessToken = accessToken;
var response = model.GenerateContent(prompt).Result;
Console.WriteLine(response.Text);
More examples
The folders samples and tests contain more examples.
Troubleshooting
tba
Using the tests
The repository contains a number of test cases for Google AI and Vertex AI. You will find them in the tests folder. They are part of the [GenerativeAI solution].
To run the tests, either enter the relevant information into the appsettings.json, create a new appsettings.user.json file with the same JSON structure in the tests folder, or define the following environment variables
- GOOGLE_API_KEY
- GOOGLE_PROJECT_ID
- GOOGLE_REGION
- GOOGLE_ACCESS_TOKEN (optional: if absent,
gcloud auth application-default print-access-tokenis executed)
The test cases should provide more insights and use cases on how to use the Mscc.GenerativeAI package in your .NET projects.
Feedback
You can create issues at the https://github.com/mscraftsman/generative-ai repository.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.