GeminiAPIWrapper 1.0.3

dotnet add package GeminiAPIWrapper --version 1.0.3
                    
NuGet\Install-Package GeminiAPIWrapper -Version 1.0.3
                    
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="GeminiAPIWrapper" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GeminiAPIWrapper" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="GeminiAPIWrapper" />
                    
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 GeminiAPIWrapper --version 1.0.3
                    
#r "nuget: GeminiAPIWrapper, 1.0.3"
                    
#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 GeminiAPIWrapper@1.0.3
                    
#: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=GeminiAPIWrapper&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=GeminiAPIWrapper&version=1.0.3
                    
Install as a Cake Tool

GeminiAPIWrapper

Gemini API を簡単に利用するための .NET ラッパーライブラリです。

特徴

  • Generative Language APIをサポート
  • ストリーミング・非ストリーミング応答に対応
  • API キーヘッダーや認証ヘッダーのカスタマイズ可能
  • System.Text.Json のソース生成を活用した高速なシリアライゼーション
  • .NET 9 以降に対応

サポート対象

  • .NET バージョン: .NET 9 以降

インストール

以下のコマンドでインストールできます。

dotnet add package GeminiAPIWrapper

Quick Start

using GeminiAPIWrapper;
using GeminiAPIWrapper.Options;

// オプションを設定
var options = new GenerativeLanguageOptions
{
    EndpointBase = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash",
    ApiKey = "YOUR_API_KEY"
};

// サービスを構築
var service = GeminiServiceBuilder.Build(options);

// テキスト生成
var response = await service.GenerateAsync("こんにちは");
Console.WriteLine(response?.GetText());

重要: EndpointBase には :generateContent:streamGenerateContent などの操作名を含めないでください。 ライブラリが自動的に適切な操作名を付加します。

ストリーミング応答

await foreach (var chunk in service.StreamGenerateAsync("長文を生成してください"))
{
    Console.Write(chunk?.GetText());
}

詳細な設定を行う場合

using GeminiAPIWrapper.Configurations;

var generationConfig = new GenerationConfig
{
    Temperature = 0.7,
    MaxOutputTokens = 1024
};

var response = await service.GenerateAsync(
    userMessage: "AIについて説明してください",
    systemInstruction: "あなたは親切なアシスタントです",
    generationConfig: generationConfig
);

カスタムリクエストを使用する場合

using GeminiAPIWrapper.Configurations;

var request = new GeminiRequest
{
    Contents = 
    [
        new Content
        {
            Role = Role.User,
            Parts = [new Part { Text = "こんにちは" }]
        }
    ],
    GenerationConfig = new GenerationConfig { Temperature = 0.9 }
};

var response = await service.GenerateAsync(request);

API キーヘッダーのカスタマイズ

デフォルトでは X-Goog-Api-Key ヘッダーが使用されますが、カスタマイズ可能です。

var options = new GenerativeLanguageOptions
{
    EndpointBase = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp",
    ApiKey = "YOUR_API_KEY",
    KeyHeader = "Custom-Api-Key-Header" // カスタムヘッダー名
};

主要なクラス

  • GeminiServiceBuilder: サービスインスタンスを構築するファクトリクラス
  • GeminiService: API 呼び出しを行うメインサービスクラス
  • GeminiRequest: リクエストの詳細を構成するクラス
  • GeminiResponse: API からの応答を表すクラス
  • GenerativeLanguageOptions: Google AI Studio 用の設定

拡張メソッド

GeminiResponse には便利な拡張メソッドが用意されています。

using GeminiAPIWrapper.Extensions;

// テキストを取得
string? text = response.GetText();

// Function Call を取得
FunctionCall? functionCall = response.GetFunctionCall();

詳細

ResponseSchemaやFunctionCallの利用については以下の記事で解説しています。

https://osg.junue.net/articles/geminiapiwrapper/

ライセンス

このプロジェクトは MIT ライセンスの下で公開されています。

貢献

プルリクエストや Issue の報告を歓迎します。

作者

Name: Osg-Junue

Blog: https://osg.junue.net/

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

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
1.0.3 206 10/20/2025
1.0.2 206 10/20/2025
1.0.1 191 10/19/2025
1.0.0 195 10/19/2025

Ver.1.0.0:リリース
Ver.1.0.1:不要な参照の削除
Ver.1.0.2:nugetでのエラーを解決
Ver.1.0.3:Schemaを統一