Skip to content
forked from nuskey8/GemiNet

Gemini Developer API client for .NET and Unity

License

Notifications You must be signed in to change notification settings

unitycoder/GemiNet

 
 

Repository files navigation

GemiNet

Gemini Developer API client for .NET and Unity

NuGet Releases GitHub license

English | 日本語

Overview

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.

Installation

NuGet packages

GemiNet requires .NET Standard 2.1 or later. The package is available on NuGet.

.NET CLI

dotnet add package GemiNet

Package Manager

Install-Package GemiNet

Unity

You can use GemiNet in Unity by using NuGetForUnity. For details, please refer to the NuGetForUnity README.

Quick Start

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());
}

Features

GemiNet's GoogleGenAI is modularized similar to the TypeScript SDK.

  • ai.Models allows you to generate content, generate vectors, and retrieve available models.
  • ai.Caches lets you create caches for specific inputs.
  • ai.Files enables file uploads and deletions.
  • ai.Live supports the Live API (bidirectional communication via WebSocket). For details, see the Live API section.

Live API

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);
}

Microsoft.Extensions.AI

To use GemiNet with Microsoft.Extensions.AI, add the GemiNet.Extensions.AI package.

Installation

.NET CLI

dotnet add package GemiNet.Extensions.AI

Package Manager

Install-Package GemiNet.Extensions.AI

Usage

You 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!");

Limitations

License

This library is released under the MIT License.

About

Gemini Developer API client for .NET and Unity

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%