跳到正文
GitHub·· 7 天前AI 评分15

Soenneker.SemanticKernel.Pool.Gemini:Semantic Kernel 的 Gemini 连接器注册助手

soenneker/soenneker.semantickernel.pool.gemini

AI 导读

Soenneker.SemanticKernel.Pool.Gemini 发布,为 Semantic Kernel 的 Gemini 连接器提供注册助手,可通过 dotnet add package 安装。

正文

Gemini connector registration helpers for Soenneker.SemanticKernel.Pool.

Installation

dotnet add package Soenneker.SemanticKernel.Pool.Gemini

Add a Gemini entry

Resolve the pool and HTTP client cache from dependency injection, then register a chat or embedding entry:

using Soenneker.SemanticKernel.Enums.KernelType;
using Soenneker.SemanticKernel.Pool.Abstract;
using Soenneker.SemanticKernel.Pool.Gemini;
using Soenneker.Utils.HttpClientCache.Abstract;

await pool.AddGemini(
    poolId: "chat",
    key: "gemini-primary",
    type: KernelType.Chat,
    modelId: "gemini-model-id",
    apiKey: configuration["Gemini:ApiKey"]!,
    endpoint: "https://generativelanguage.googleapis.com",
    httpClientCache: httpClientCache,
    rps: 2,
    rpm: 60,
    rpd: 1_000,
    tokensPerDay: null,
    cancellationToken);

Use KernelType.Chat for Google AI chat completion or KernelType.Embedding for the Google AI embedding generator. Other kernel types throw NotSupportedException when the pool first constructs the kernel.

The adapter caches chat HTTP clients under gemini:{poolId}:{key} with a five-minute timeout. The endpoint argument is retained in the entry's SemanticKernelOptions, but this adapter does not pass it to the Google connector; connector endpoint behavior therefore comes from the Google Semantic Kernel package.

Pool quota values are reservations made when GetAvailable selects the entry. tokensPerDay counts one unit per acquisition; it is not populated from provider token usage.

Remove the entry

Use the matching helper so both the pool entry and its cached HTTP client are removed:

await pool.RemoveGemini(
    "chat",
    "gemini-primary",
    httpClientCache,
    cancellationToken);

Keep the API key in a protected configuration provider and avoid logging or serializing the generated SemanticKernelOptions.

来源:GitHub · github.com