Mscc.GenerativeAI.Web
1.9.3
See the version list below for details.
dotnet add package Mscc.GenerativeAI.Web --version 1.9.3
NuGet\Install-Package Mscc.GenerativeAI.Web -Version 1.9.3
<PackageReference Include="Mscc.GenerativeAI.Web" Version="1.9.3" />
paket add Mscc.GenerativeAI.Web --version 1.9.3
#r "nuget: Mscc.GenerativeAI.Web, 1.9.3"
// Install Mscc.GenerativeAI.Web as a Cake Addin #addin nuget:?package=Mscc.GenerativeAI.Web&version=1.9.3 // Install Mscc.GenerativeAI.Web as a Cake Tool #tool nuget:?package=Mscc.GenerativeAI.Web&version=1.9.3
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="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.
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 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<TOptions> 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 result = await service.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<TOptions> 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.
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Mscc.GenerativeAI (>= 1.9.2)
- System.Text.Encodings.Web (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Mscc.GenerativeAI (>= 1.9.2)
- System.Text.Encodings.Web (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
-
net6.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Mscc.GenerativeAI (>= 1.9.2)
- System.Text.Encodings.Web (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Mscc.GenerativeAI (>= 1.9.2)
- System.Text.Encodings.Web (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
-
net9.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.0)
- Mscc.GenerativeAI (>= 1.9.2)
- System.Text.Encodings.Web (>= 9.0.0)
- System.Text.Json (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.9.5 | 42 | 11/22/2024 |
1.9.4 | 37 | 11/21/2024 |
1.9.3 | 41 | 11/20/2024 |
1.9.2 | 67 | 11/18/2024 |
1.9.1 | 76 | 11/13/2024 |
1.9.0 | 86 | 11/4/2024 |
1.8.3 | 77 | 11/1/2024 |
1.8.2 | 62 | 10/31/2024 |
1.8.1 | 74 | 10/30/2024 |
1.8.0 | 76 | 10/29/2024 |
1.7.0 | 86 | 10/14/2024 |
1.6.5 | 91 | 10/13/2024 |
1.6.4 | 159 | 10/9/2024 |
1.6.3 | 109 | 9/24/2024 |
1.6.2 | 119 | 9/19/2024 |
1.6.1 | 86 | 9/18/2024 |
1.6.0 | 109 | 8/29/2024 |
1.5.1 | 95 | 7/31/2024 |
1.5.0 | 316 | 5/15/2024 |
1.4.0 | 137 | 4/22/2024 |
1.3.0 | 119 | 4/18/2024 |
1.2.0 | 117 | 4/16/2024 |
1.1.4 | 115 | 4/15/2024 |
1.1.3 | 155 | 4/12/2024 |
1.1.2 | 109 | 4/11/2024 |
1.1.1 | 106 | 4/10/2024 |
1.1.0 | 111 | 4/9/2024 |
1.0.1 | 117 | 4/1/2024 |
1.0.0 | 114 | 3/30/2024 |
0.9.4 | 114 | 3/29/2024 |
0.9.3 | 118 | 3/28/2024 |
0.9.1 | 125 | 3/26/2024 |
0.9.0 | 106 | 3/23/2024 |
0.8.4 | 110 | 3/21/2024 |
0.8.3 | 123 | 3/20/2024 |
0.8.2 | 124 | 3/20/2024 |
0.8.1 | 129 | 3/20/2024 |
0.8.0 | 119 | 3/20/2024 |
0.7.2 | 109 | 3/18/2024 |
0.7.1 | 120 | 3/18/2024 |
0.5.1 | 145 | 3/5/2024 |
0.5.0 | 123 | 3/5/2024 |
# Changelog (Release Notes)
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Added
### Changed
### Fixed
## 1.9.3
### Changed
- bump version
## 1.9.2
### Changed
- bump version
## 1.9.1
### Changed
- update NuGet package(s)
## 1.9.0
### Changed
- bump version
## 1.8.3
### Changed
- bump version
## 1.8.2
### Changed
- bump version
## 1.8.1
### Changed
- bump version
## 1.8.0
### Changed
- bump version
## 1.7.0
### Changed
- bump version
## 1.6.5
### Changed
- bump version
## 1.6.4
### Changed
- bump version
## 1.6.3
### Changed
- bump version
## 1.6.2
### Changed
- bump version
## 1.6.1
### Changed
- bump version
## 1.6.0
### Changed
- bump version
## 1.5.1
### Changed
- Update System.Text.Json to 8.0.4
## 1.5.0
### Changed
- bump version
## 1.4.0
### Changed
- bump version
## 1.3.0
### Changed
- bump version
## 1.2.0
### Changed
- bump version
## 1.1.4
### Changed
- bump version
## 1.1.3
### Changed
- bump version
## 1.1.2
### Changed
- bump version
## 1.1.1
### Changed
- bump version
## 1.1.0
### Added
- implement generic access to GenerativeModel
### Changed
- bump version
## 1.0.1
### Changed
- bump version
## 1.0.0
### Changed
- bump version
## 0.9.4
### Changed
- bump version
## 0.9.3
### Changed
- bump version
## 0.9.2
### Changed
- bump version
## 0.9.1
### Changed
- use GoogleAI type to create model reference
- increase package reference
- bump version
## 0.9.0
### Changed
- bump version
## 0.8.4
### Changed
- bump version
## 0.8.3
### Changed
- bump version
## 0.8.2
### Changed
- bump version
## 0.8.1
### Changed
- bump version
## 0.8.0
### Changed
- bump version
## 0.7.1
### Changed
- bump version
## 0.5.1
### Changed
- Update package reference
## 0.5.0
### Added
- Initial implementation