Mscc.GenerativeAI.Web 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mscc.GenerativeAI.Web --version 1.1.3                
NuGet\Install-Package Mscc.GenerativeAI.Web -Version 1.1.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mscc.GenerativeAI.Web" Version="1.1.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mscc.GenerativeAI.Web --version 1.1.3                
#r "nuget: Mscc.GenerativeAI.Web, 1.1.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Mscc.GenerativeAI.Web as a Cake Addin
#addin nuget:?package=Mscc.GenerativeAI.Web&version=1.1.3

// Install Mscc.GenerativeAI.Web as a Cake Tool
#tool nuget:?package=Mscc.GenerativeAI.Web&version=1.1.3                

Gemini AI Client for ASP.NET Core

GitHub GitHub last commit

Integrate Gemini API into your web projects. It works both Google AI (Studio) and Google Cloud Vertex AI.

Client for ASP.NET Core: NuGet Version NuGet Downloads

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="0.5.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.0-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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0 75 11/4/2024
1.8.3 69 11/1/2024
1.8.2 56 10/31/2024
1.8.1 68 10/30/2024
1.8.0 72 10/29/2024
1.7.0 80 10/14/2024
1.6.5 84 10/13/2024
1.6.4 153 10/9/2024
1.6.3 105 9/24/2024
1.6.2 116 9/19/2024
1.6.1 82 9/18/2024
1.6.0 104 8/29/2024
1.5.1 92 7/31/2024
1.5.0 300 5/15/2024
1.4.0 131 4/22/2024
1.3.0 117 4/18/2024
1.2.0 115 4/16/2024
1.1.4 113 4/15/2024
1.1.3 147 4/12/2024
1.1.2 107 4/11/2024
1.1.1 104 4/10/2024
1.1.0 109 4/9/2024
1.0.1 115 4/1/2024
1.0.0 112 3/30/2024
0.9.4 112 3/29/2024
0.9.3 116 3/28/2024
0.9.1 123 3/26/2024
0.9.0 104 3/23/2024
0.8.4 106 3/21/2024
0.8.3 121 3/20/2024
0.8.2 122 3/20/2024
0.8.1 127 3/20/2024
0.8.0 117 3/20/2024
0.7.2 106 3/18/2024
0.7.1 117 3/18/2024
0.5.1 143 3/5/2024
0.5.0 121 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.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