Gemini Developer API client for .NET and Unity
English | 日本語
GemiNet is a Gemini Developer API client library for .NET/Unity. It is designed based on the official TypeScript SDK (js-genai), and provides an easy-to-use API compared to other libraries (Google_GenerativeAI, GeminiSharp, etc.). GemiNet also offers extension packages compatible with Microsoft.Extensions.AI abstraction layer, enabling smoother integration with your applications.
GemiNet requires .NET Standard 2.1 or later. The package is available on NuGet.
dotnet add package GemiNetInstall-Package GemiNetYou can use GemiNet in Unity by using NuGetForUnity. For details, please refer to the NuGetForUnity README.
You can call the Gemini API using GoogleGenAI.
using GemiNet;
using var ai = new GoogleGenAI
{
ApiKey = Environment.GetEnvironmentVariable("GEMINI_API_KEY"),
};
var response = await ai.Models.GenerateContentAsync(new()
{
Model = Models.Gemini2_0Flash, // models/gemini-2.0-flash
Contents = "Hello, Gemini!"
});
Console.WriteLine(response.GetText());Streaming generation is also supported.
var request = new GenerateContentRequest
{
Model = Models.Gemini2_0Flash,
Contents = "Hello, Gemini!"
};
await foreach (var response in GenerateContentStreamAsync(request))
{
Console.WriteLine(response.GetText());
}GemiNet's GoogleGenAI is modularized similar to the TypeScript SDK.
ai.Modelsallows you to generate content, generate vectors, and retrieve available models.ai.Cacheslets you create caches for specific inputs.ai.Filesenables file uploads and deletions.ai.Livesupports the Live API (bidirectional communication via WebSocket). For details, see the Live API section.
GemiNet supports the Live API. You can read messages using ReceiveAsync() with await foreach.
using GemiNet;
using var ai = new GoogleGenAI();
await using var session = await ai.Live.ConnectAsync(new()
{
Model = Models.Gemini2_0FlashLive,
Config = new()
{
ResponseModalities = [Modality.Text]
}
});
_ = Task.Run(async () =>
{
await session.SendRealtimeInputAsync(new()
{
Text = "Hello, Gemini!"
});
});
await foreach (var message in session.ReceiveAsync())
{
Console.WriteLine(message.ServerContent?.ModelTurn?.Parts[0].Text);
}To use GemiNet with Microsoft.Extensions.AI, add the GemiNet.Extensions.AI package.
dotnet add package GemiNet.Extensions.AIInstall-Package GemiNet.Extensions.AIYou can convert GoogleGenAI to Microsoft.Extensions.AI interfaces using AsChatClient() and AsEmbeddingGenerator().
using GemiNet;
using GemiNet.Extensions.AI;
using var ai = new GoogleGenAI();
var client = ai.AsChatClient(Models.Gemini2_0Flash);
var response = await client.GetResponseAsync([new(ChatRole.User, "What is AI?")]);
var embeddingGenerator = ai.AsEmbeddingGenerator(Models.Gemini2_0Flash);
var embedding = await embeddingGenerator.GenerateEmbeddingAsync("Hello, Gemini!");- This library does not currently support Vertex AI. If you want to use Vertex AI, please use Google.Cloud.AIPlatform.V1 instead.
This library is released under the MIT License.