A client for ASP.NET Core designed to consume Gemini AI.
Install
| Product | Versions |
|---|---|
| .NET | |
| .NET Standard |
README
Gemini AI Client for ASP.NET Core
Integrate Gemini API into your web projects. It works both Google AI (Studio) and Google Cloud Vertex AI.
Getting started
Either create a new ASP.NET Core project or use an existing one.
> dotnet new web -o Web.Minimal.Api
Navigate to the project's root folder and install the package Mscc.GenerativeAI.Web 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.Web
Working with Visual Studio use the NuGet Package Manager to install the package Mscc.GenerativeAI.Web.
PM> Install-Package Mscc.GenerativeAI.Web
Alternatively, add the following line to your .csproj file.
<ItemGroup>
<PackageReference Include="Mscc.GenerativeAI.Web" Version="3.0.1" />
</ItemGroup>
You can then add this code to your sources whenever you need to access any Gemini API provided by Google. This package works currently for Google AI (Google AI Studio) only. Use with Google Cloud Vertex AI is provided by the underlying Mscc.GenerativeAI package but not exposed yet.
Using Google AI Gemini API
Working with Google AI in your application requires an API key. Get an API key from Google AI Studio.
Add the following configuration to the appsettings.json file.
{
// section name and location to your liking.
"Gemini": {
"Credentials": {
"ApiKey": "YOUR_API_KEY" // replace value with key from AI Studio
},
"ProjectId": "",
"Region": "us-central1",
"Model": "gemini-1.5-pro" // default value
},
// any other settings...
"Logging": {
},
}
The section name Gemini is arbitrary as well as the location of the section. Although, it needs to be referenced correctly in the Configuration builder.
Using the AddGenerativeAI service
Next, add the service AddGenerativeAI() to the ASP.NET Core web app and map the routes as needed. Following is the most minimal implementation.
using Mscc.GenerativeAI.Web;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI(builder.Configuration.GetSection("Gemini"));
var app = builder.Build();
app.MapGet("/", async (IGenerativeModelService service) =>
{
var model = service.CreateInstance();
var result = await model.GenerateContent("Write about the history of Mauritius.");
return result.Text;
});
app.Run();
There are overloads of the extension method AddGenerativeAI() according to Options pattern guidance for .NET library authors.
Following approaches are available:
- Parameterless
- IConfiguration parameter (as shown above)
- Configuration section path parameter
- Action parameter
- Options instance parameter
Hoping this provides enough flexibility for individual preferences.
Using a typed HttpClient
Alternatively to the above described service extension methods Mscc.GenerativeAI.Web also provides a typed HttpClient that can be added to your ASP.NET web application.
sing Mscc.GenerativeAI.Web;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient<GenerativeAIClient>()
{
BaseAddress = "";
};
var app = builder.Build();
app.MapGet("/", async (IGenerativeModelService service) =>
{
var model = service.CreateInstance();
var result = await model.GenerateContent("Write about the history of Mauritius.");
return result.Text;
});
app.Run();
Use global usings
Keeping the code base minimal, one should consider creating a GlobalUsings.cs file and define the using statements there.
global using Mscc.GenerativeAI.Web;
This approach renders repetitive use of using Mscc.GenerativeAI.Web; in each .cs file obsolete.
Configure the service
Find each approach documented. Surrounding source code skipped for brevity.
Parameterless๐
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI();
var app = builder.Build();
// ...
IConfiguration parameter (as shown above)๐
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI(builder.Configuration.GetSection("Gemini"));
var app = builder.Build();
// ...
Configuration section path parameter๐
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI("Gemini");
var app = builder.Build();
// ...
Action parameter๐
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI(options =>
{
// User defined option values
options.ProjectId = string.Empty;
options.Model = GenerativeAI.Types.Model.GeminiProVision;
options.Credentials.ApiKey = "YOUR_API_KEY";
});
var app = builder.Build();
// ...
Options instance parameter๐
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGenerativeAI(new GenerativeAIOptions
{
// Specify option values
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"),
Region = Environment.GetEnvironmentVariable("GOOGLE_REGION"),
Model = Environment.GetEnvironmentVariable("GOOGLE_MODEL"),
Credentials = new() { ApiKey = Environment.GetEnvironmentVariable("GOOGLE_API_KEY") }
});
var app = builder.Build();
// ...
The choice of configuring the service is yours.
More examples
The folders samples and tests contain more examples.
Troubleshooting
tba
Feedback
You can create issues at the https://github.com/mscraftsman/generative-ai repository.
Changes from Previous Version
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.