Integrate Gemini API into your web projects. It works both Google AI (Studio) and Google Cloud Vertex AI.
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="1.6.3" />
</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.
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.
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 result = await service.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.
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 result = await service.GenerateContent("Write about the history of Mauritius.");
return result.Text;
});
app.Run();
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.
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.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.
The folders samples and tests contain more examples.
tba
You can create issues at the https://github.com/mscraftsman/generative-ai repository.