Mscc.GenerativeAI
0.5.2
See the version list below for details.
dotnet add package Mscc.GenerativeAI --version 0.5.2
NuGet\Install-Package Mscc.GenerativeAI -Version 0.5.2
<PackageReference Include="Mscc.GenerativeAI" Version="0.5.2" />
paket add Mscc.GenerativeAI --version 0.5.2
#r "nuget: Mscc.GenerativeAI, 0.5.2"
// Install Mscc.GenerativeAI as a Cake Addin #addin nuget:?package=Mscc.GenerativeAI&version=0.5.2 // Install Mscc.GenerativeAI as a Cake Tool #tool nuget:?package=Mscc.GenerativeAI&version=0.5.2
Gemini AI Client for .NET and ASP.NET Core
Access the Gemini API in Google AI Studio and Google Cloud Vertex AI.
Read more about Mscc.GenerativeAI.Web and how to add it to your ASP.NET web applications.
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.5.2" />
</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.
- Google AI: Authentication with an API key
- Google AI: Authentication with OAuth
- Vertex AI: Authentication with Application Default Credentials (ADC)
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.
- Simple console application
- ASP.NET Core Minimal web application
- ASP.NET Core MVP web application (work in progress!)
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 | 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 is compatible. 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. |
.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
- Nullable (>= 1.3.1)
- System.Text.Json (>= 8.0.2)
-
.NETStandard 2.0
- Nullable (>= 1.3.1)
- System.Text.Json (>= 8.0.2)
-
net6.0
- System.Text.Json (>= 8.0.2)
-
net7.0
- System.Text.Json (>= 8.0.2)
-
net8.0
- System.Text.Json (>= 8.0.2)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Mscc.GenerativeAI:
Package | Downloads |
---|---|
Mscc.GenerativeAI.Google
Gemini AI Client for .NET |
|
Mscc.GenerativeAI.Web
A client for ASP.NET Core designed to consume Gemini AI. |
|
fsEnsemble
Package Description |
|
Mscc.GenerativeAI.Microsoft
Gemini AI Client for .NET |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Mscc.GenerativeAI:
Repository | Stars |
---|---|
JasonBock/Rocks
A mocking library based on the Compiler APIs (Roslyn + Mocks)
|
Version | Downloads | Last updated |
---|---|---|
1.9.6 | 87 | 11/26/2024 |
1.9.5 | 163 | 11/22/2024 |
1.9.4 | 89 | 11/21/2024 |
1.9.3 | 181 | 11/20/2024 |
1.9.2 | 388 | 11/18/2024 |
1.9.1 | 155 | 11/13/2024 |
1.9.0 | 369 | 11/4/2024 |
1.8.3 | 226 | 11/1/2024 |
1.8.2 | 99 | 10/31/2024 |
1.8.1 | 243 | 10/30/2024 |
1.8.0 | 180 | 10/29/2024 |
1.7.0 | 322 | 10/14/2024 |
1.6.5 | 406 | 10/13/2024 |
1.6.4 | 602 | 10/9/2024 |
1.6.3 | 669 | 9/24/2024 |
1.6.2 | 129 | 9/19/2024 |
1.6.1 | 208 | 9/18/2024 |
1.6.0 | 778 | 8/29/2024 |
1.5.1 | 367 | 7/31/2024 |
1.5.0 | 2,434 | 5/15/2024 |
1.4.0 | 370 | 4/22/2024 |
1.3.0 | 123 | 4/18/2024 |
1.2.0 | 115 | 4/16/2024 |
1.1.4 | 153 | 4/15/2024 |
1.1.3 | 115 | 4/12/2024 |
1.1.2 | 104 | 4/11/2024 |
1.1.1 | 1,859 | 4/10/2024 |
1.1.0 | 97 | 4/9/2024 |
1.0.1 | 280 | 4/1/2024 |
1.0.0 | 103 | 3/30/2024 |
0.9.4 | 286 | 3/29/2024 |
0.9.3 | 204 | 3/28/2024 |
0.9.1 | 197 | 3/26/2024 |
0.9.0 | 209 | 3/23/2024 |
0.8.4 | 195 | 3/21/2024 |
0.8.3 | 256 | 3/20/2024 |
0.8.2 | 211 | 3/20/2024 |
0.8.1 | 225 | 3/20/2024 |
0.8.0 | 220 | 3/20/2024 |
0.7.2 | 117 | 3/18/2024 |
0.7.1 | 104 | 3/18/2024 |
0.7.0 | 115 | 3/15/2024 |
0.6.1 | 450 | 3/11/2024 |
0.6.0 | 120 | 3/11/2024 |
0.5.4 | 132 | 3/7/2024 |
0.5.3 | 152 | 3/7/2024 |
0.5.2 | 119 | 3/6/2024 |
0.5.1 | 130 | 3/5/2024 |
0.5.0 | 162 | 3/5/2024 |
0.4.5 | 208 | 3/3/2024 |
0.4.4 | 127 | 3/1/2024 |
0.4.3 | 124 | 3/1/2024 |
0.4.2 | 123 | 3/1/2024 |
0.4.1 | 119 | 2/29/2024 |
0.3.2 | 116 | 2/29/2024 |
0.3.1 | 106 | 2/29/2024 |
0.2.1 | 118 | 2/29/2024 |
- Correct JSON conversion of SafetySettings
- Use of enum string values