Mscc.GenerativeAI 0.7.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mscc.GenerativeAI --version 0.7.2
NuGet\Install-Package Mscc.GenerativeAI -Version 0.7.2
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" Version="0.7.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mscc.GenerativeAI --version 0.7.2
#r "nuget: Mscc.GenerativeAI, 0.7.2"
#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 as a Cake Addin
#addin nuget:?package=Mscc.GenerativeAI&version=0.7.2

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

Gemini AI Client for .NET and ASP.NET Core

GitHub GitHub last commit

Access the Gemini API in Google AI Studio and Google Cloud Vertex AI.

Client for .NET: NuGet Version NuGet Downloads

Client for ASP.NET Core: NuGet Version NuGet Downloads

Read more about Mscc.GenerativeAI.Web and how to add it to your ASP.NET web applications.

Client for .NET using Google API Client Library: NuGet Version NuGet Downloads

Read more about Mscc.GenerativeAI.Google and how to add it to your .NET 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.7.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 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.

Using Environment variables

In the cloud most settings are configured via environment variables (EnvVars). The ease of configuration, their wide spread support and the simplicity of environment variables makes them a very interesting option.

Variable Name Description
GOOGLE_AI_MODEL The name of the model to use (default is Model.Gemini10Pro)
GOOGLE_API_KEY The API key generated in Google AI Studio
GOOGLE_PROJECT_ID Project ID in Google Cloud to access the APIs
GOOGLE_REGION Region in Google Cloud (default is us-central1)
GOOGLE_ACCESS_TOKEN The access token required to use models running in Vertex AI
GOOGLE_APPLICATION_CREDENTIALS Path to the application credentials file.
GOOGLE_WEB_CREDENTIALS Path to a Web credentials file.

Using any environment variable provides a simplified access to a model.

using Mscc.GenerativeAI;

var model = new GenerativeModel();

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.Gemini10Pro);
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

Chat conversations

Gemini enables you to have freeform conversations across multiple turns. You can interact with Gemini Pro using a single-turn prompt and response or chat with it in a multi-turn, continuous conversation, even for code understanding and generation.

using Mscc.GenerativeAI;

var apiKey = "your_api_key";
var model = new GenerativeModel(apiKey: apiKey);    // using default model: gemini-pro
var chat = model.StartChat();

// Instead of discarding you could also use the response and access `response.Text`.
_ = await chat.SendMessage("Hello, fancy brainstorming about IT?");
_ = await chat.SendMessage("In one sentence, explain how a computer works to a young child.");
_ = await chat.SendMessage("Okay, how about a more detailed explanation to a high schooler?");
_ = await chat.SendMessage("Lastly, give a thorough definition for a CS graduate.");

// A chat session keeps every response in its history.
chat.History.ForEach(c => Console.WriteLine($"{c.Role}: {c.Parts[0].Text}"));

Create a tuned model

The Gemini API lets you tune models on your own data. Since it's your data and your tuned models this needs stricter access controls than API-Keys can provide.

Before you can create a tuned model, you'll need to setup OAuth for your project.

using Mscc.GenerativeAI;

var projectId = "your_google_project_id"; // the ID of a project, not its name.
var accessToken = "your_access_token";      // use `gcloud auth application-default print-access-token` to get it.
var model = new GenerativeModel(apiKey: null, model: Model.Gemini10Pro001)
{
    AccessToken = accessToken,
    ProjectId = projectId
};
var request = new CreateTunedModelRequest()
{
    BaseModel = $"models/{Model.Gemini10Pro001}",
    DisplayName = "Autogenerated Test model",
    TuningTask = new()
    {
        Hyperparameters = new() { BatchSize = 2, LearningRate = 0.001f, EpochCount = 3 },
        TrainingData = new()
        {
            Examples = new()
            {
                Examples = new()
                {
                    new TrainingDataExample() { TextInput = "1", Output = "2" },
                    new TrainingDataExample() { TextInput = "3", Output = "4" },
                    new TrainingDataExample() { TextInput = "-3", Output = "-2" },
                    new TrainingDataExample() { TextInput = "twenty two", Output = "twenty three" },
                    new TrainingDataExample() { TextInput = "two hundred", Output = "two hundred one" },
                    new TrainingDataExample() { TextInput = "ninety nine", Output = "one hundred" },
                    new TrainingDataExample() { TextInput = "8", Output = "9" },
                    new TrainingDataExample() { TextInput = "-98", Output = "-97" },
                    new TrainingDataExample() { TextInput = "1,000", Output = "1,001" },
                    new TrainingDataExample() { TextInput = "thirteen", Output = "fourteen" },
                    new TrainingDataExample() { TextInput = "seven", Output = "eight" },
                }
            }
        }
    }
};

// Act
var response = await model.CreateTunedModel(request);
Console.WriteLine($"Name: {response.Name}");
Console.WriteLine($"Model: {response.Metadata.TunedModel} (Steps: {response.Metadata.TotalSteps})");

(This is still work in progress but operational. Future release will provide types to simplify the create request.)

Tuned models appear in your Google AI Studio library.

Tuned models are listed below My Library in Google AI Studio

More samples

The folders samples and tests contain more examples.

Troubleshooting ⚡

Sometimes you might have authentication warnings HTTP 403 (Forbidden). Especially while working with OAuth-based authentication. You can fix it by re-authenticating through ADC.

gcloud config set project "$PROJECT_ID"

gcloud auth application-default set-quota-project "$PROJECT_ID"
gcloud auth application-default login

Make sure that the required API have been enabled.

# ENABLE APIs
gcloud services enable aiplatform.googleapis.com

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 ✨

For support and feedback kindly create issues at the https://github.com/mscraftsman/generative-ai repository.

License 📜

This project is licensed under the Apache-2.0 License - see the LICENSE file for details.

Citation 📚

If you use Mscc.GenerativeAI in your research project, kindly cite as follows

@misc{Mscc.GenerativeAI,
  author = {Kirstätter, J and MSCraftsman},
  title = {Mscc.GenerativeAI - Gemini AI Client for .NET and ASP.NET Core},
  year = {2024},
  publisher = {GitHub},
  journal = {GitHub repository},
  note = {https://github.com/mscraftsman/generative-ai}
}

Created by Jochen Kirstätter.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Mscc.GenerativeAI:

Package Downloads
Mscc.GenerativeAI.Web

A client for ASP.NET Core designed to consume Gemini AI.

Mscc.GenerativeAI.Google

Gemini AI Client for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.0 56 5/15/2024
1.4.0 286 4/22/2024
1.3.0 90 4/18/2024
1.2.0 90 4/16/2024
1.1.4 90 4/15/2024
1.1.3 89 4/12/2024
1.1.2 79 4/11/2024
1.1.1 457 4/10/2024
1.1.0 82 4/9/2024
1.0.1 199 4/1/2024
1.0.0 77 3/30/2024
0.9.4 175 3/29/2024
0.9.3 147 3/28/2024
0.9.1 126 3/26/2024
0.9.0 154 3/23/2024
0.8.4 141 3/21/2024
0.8.3 204 3/20/2024
0.8.2 156 3/20/2024
0.8.1 135 3/20/2024
0.8.0 148 3/20/2024
0.7.2 88 3/18/2024
0.7.1 74 3/18/2024
0.7.0 84 3/15/2024
0.6.1 323 3/11/2024
0.6.0 87 3/11/2024
0.5.4 101 3/7/2024
0.5.3 117 3/7/2024
0.5.2 87 3/6/2024
0.5.1 98 3/5/2024
0.5.0 116 3/5/2024
0.4.5 157 3/3/2024
0.4.4 98 3/1/2024
0.4.3 91 3/1/2024
0.4.2 92 3/1/2024
0.4.1 96 2/29/2024
0.3.2 88 2/29/2024
0.3.1 87 2/29/2024
0.2.1 91 2/29/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) (SemVer).

## [Unreleased]

### Added

- provide types to simplify creation of tuned model

### Changed
### Fixed

## 0.7.2

###

- delete tuned model

### Changed

- method to list models supports both - regular and tuned - model types

## 0.7.1

### Added

- implement model tuning (works with stable models only)
 - text-bison-001
 - gemini-1.0-pro-001
- tests for model tuning
-
### Changed

- improved authentication regarding API key or OAuth/ADC
- added scope https://www.googleapis.com/auth/generative-language.tuning
- harmonized version among NuGet packages
- provide empty response on Safety stop
- merged regular and tuned ModelResponse

## 0.7.0

### Added

- use Environment Variables for default values (parameterless constructor)
- support of .env file

### Changed

- improve Function Calling
- improve Chat streaming
- improve Embeddings

## 0.6.1

### Added

- implement Function Calling

## 0.6.0

### Added

- implement streaming of content
- support of HTTP/3 protocol
- specify JSON order of properties

### Changed

- improve handling of config and settings

## 0.5.4

### Added

- implement Embeddings
- brief sanity check on model selection

### Changed

- refactor handling of parts
- ⛳ allow configuration, safety settings and tools for Chat

## 0.5.3

### Added

- Implement Chat

## 0.5.2

### Added

- Use of enumerations

### Changed

- Correct JSON conversion of SafetySettings

## 0.5.1

### Added

- Handle GenerationConfig, SafetySeetings and Tools

### Changed

- Append streamGenerateContent

## 0.5.0

### Added

- Add NuGet package Mscc.GenerativeAI.Web for use with ASP.NET Core.

### Changed

- Refactor folder structure

## 0.4.5

### Changed

- Extend methods

## 0.4.4

### Added

- Automate package build process

## 0.4.3

### Added

- Add x-goog-api-key header

## 0.4.2

### Changed

- Minor correction

## 0.4.1

### Added

- Add OAuth to Google AI

## 0.3.2

### Changed

- Improve package attributes

## 0.3.1

### Added

- Add methods ListModels and GetModel

## 0.2.1

### Added

- Initial Release

## 0.1.2

### Changed

- Update README.md