Mscc.GenerativeAI.Web 0.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mscc.GenerativeAI.Web --version 0.5.0
                    
NuGet\Install-Package Mscc.GenerativeAI.Web -Version 0.5.0
                    
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="0.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mscc.GenerativeAI.Web" Version="0.5.0" />
                    
Directory.Packages.props
<PackageReference Include="Mscc.GenerativeAI.Web" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mscc.GenerativeAI.Web --version 0.5.0
                    
#r "nuget: Mscc.GenerativeAI.Web, 0.5.0"
                    
#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.
#:package Mscc.GenerativeAI.Web@0.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mscc.GenerativeAI.Web&version=0.5.0
                    
Install as a Cake Addin
#tool nuget:?package=Mscc.GenerativeAI.Web&version=0.5.0
                    
Install as a Cake Tool

Gemini AI Client for .NET and ASP.NET Core

Client for .NET: NuGet Version NuGet Downloads GitHub GitHub last commit

Client for ASP.NET Core: NuGet Version NuGet Downloads

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.

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-token is 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.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
3.0.2 553 12/31/2025
3.0.1 93 12/29/2025
3.0.0 302 12/16/2025
2.9.8 681 12/10/2025
2.9.7 435 12/8/2025
2.9.6 408 12/8/2025
2.9.5 412 12/8/2025
2.9.4 767 12/3/2025
2.9.3 502 11/18/2025
2.9.2 391 11/17/2025
2.9.1 301 11/11/2025
2.9.0 293 11/11/2025
2.8.25 2,162 11/5/2025
2.8.24 384 11/2/2025
2.8.23 383 10/31/2025
2.8.22 198 10/30/2025
2.8.21 183 10/29/2025
2.8.20 345 10/28/2025
2.8.19 751 10/20/2025
2.8.18 227 10/17/2025
2.8.17 368 10/13/2025
2.8.16 254 10/8/2025
2.8.15 505 10/2/2025
2.8.14 262 9/30/2025
2.8.13 181 9/29/2025
2.8.12 173 9/26/2025
2.8.11 182 9/25/2025
2.8.10 240 9/22/2025
2.8.9 177 9/12/2025
2.8.8 139 9/12/2025
2.8.7 367 9/9/2025
2.8.5 874 9/9/2025
2.8.4 160 9/8/2025
2.8.3 390 8/28/2025
2.8.2 224 8/26/2025
2.8.1 232 8/26/2025
2.8.0 191 8/25/2025
2.7.1 190 8/20/2025
2.7.0 159 8/18/2025
2.6.13 121 8/16/2025
2.6.12 96 8/15/2025
2.6.11 98 8/15/2025
2.6.10 100 8/15/2025
2.6.9 213 8/8/2025
2.6.8 2,284 7/21/2025
2.6.7 191 7/16/2025
2.6.6 182 7/15/2025
2.6.5 311 7/11/2025
2.6.4 1,879 6/26/2025
2.6.3 1,042 6/5/2025
2.6.2 341 6/3/2025
2.6.1 668 5/26/2025
2.6.0 170 5/23/2025
2.5.6 353 5/16/2025
2.5.5 872 5/8/2025
2.5.4 195 5/8/2025
2.5.3 798 4/28/2025
2.5.2 821 4/19/2025
2.5.1 120 4/19/2025
2.5.0 587 4/11/2025
2.4.1 566 4/2/2025
2.4.0 558 3/25/2025
2.3.6 1,130 3/16/2025
2.3.5 196 3/16/2025
2.3.4 412 3/12/2025
2.3.3 199 3/12/2025
2.3.2 516 3/8/2025
2.3.1 172 3/8/2025
2.3.0 291 3/7/2025
2.2.11 585 2/27/2025
2.2.10 271 2/26/2025
2.2.9 608 2/24/2025
2.2.8 140 2/24/2025
2.2.7 250 2/20/2025
2.2.6 136 2/20/2025
2.2.5 313 2/18/2025
2.2.4 168 2/17/2025
2.2.3 234 2/13/2025
2.2.2 286 2/12/2025
2.2.1 237 2/11/2025
2.2.0 208 2/7/2025
2.1.8 138 2/7/2025
2.1.7 142 2/6/2025
2.1.6 159 2/3/2025
2.1.5 152 2/3/2025
2.1.4 140 2/3/2025
2.1.3 208 1/28/2025
2.1.2 151 1/27/2025
2.1.1 151 1/17/2025
2.1.0 159 1/16/2025
2.0.2 158 1/6/2025
2.0.1 454 12/11/2024
2.0.0 160 12/11/2024
1.9.7 176 12/6/2024
1.9.6 157 11/26/2024
1.9.5 156 11/22/2024
1.9.4 150 11/21/2024
1.9.3 161 11/20/2024
1.9.2 158 11/18/2024
1.9.1 170 11/13/2024
1.9.0 167 11/4/2024
1.8.3 164 11/1/2024
1.8.2 156 10/31/2024
1.8.1 161 10/30/2024
1.8.0 164 10/29/2024
1.7.0 167 10/14/2024
1.6.5 176 10/13/2024
1.6.4 239 10/9/2024
1.6.3 455 9/24/2024
1.6.2 210 9/19/2024
1.6.1 148 9/18/2024
1.6.0 171 8/29/2024
1.5.1 167 7/31/2024
1.5.0 447 5/15/2024
1.4.0 195 4/22/2024
1.3.0 196 4/18/2024
1.2.0 180 4/16/2024
1.1.4 187 4/15/2024
1.1.3 242 4/12/2024
1.1.2 176 4/11/2024
1.1.1 180 4/10/2024
1.1.0 172 4/9/2024
1.0.1 193 4/1/2024
1.0.0 193 3/30/2024
0.9.4 189 3/29/2024
0.9.3 181 3/28/2024
0.9.1 204 3/26/2024
0.9.0 177 3/23/2024
0.8.4 174 3/21/2024
0.8.3 188 3/20/2024
0.8.2 180 3/20/2024
0.8.1 200 3/20/2024
0.8.0 184 3/20/2024
0.7.2 166 3/18/2024
0.7.1 185 3/18/2024
0.5.1 209 3/5/2024
0.5.0 185 3/5/2024

- Initial implementation
- Improve GenerativeModelService with Options