Zonit.Extensions.Ai.Google 1.9.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Zonit.Extensions.Ai.Google --version 1.9.0
                    
NuGet\Install-Package Zonit.Extensions.Ai.Google -Version 1.9.0
                    
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="Zonit.Extensions.Ai.Google" Version="1.9.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zonit.Extensions.Ai.Google" Version="1.9.0" />
                    
Directory.Packages.props
<PackageReference Include="Zonit.Extensions.Ai.Google" />
                    
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 Zonit.Extensions.Ai.Google --version 1.9.0
                    
#r "nuget: Zonit.Extensions.Ai.Google, 1.9.0"
                    
#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 Zonit.Extensions.Ai.Google@1.9.0
                    
#: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=Zonit.Extensions.Ai.Google&version=1.9.0
                    
Install as a Cake Addin
#tool nuget:?package=Zonit.Extensions.Ai.Google&version=1.9.0
                    
Install as a Cake Tool

Zonit.Extensions.Ai

A .NET library for integrating with multiple AI providers (OpenAI, Anthropic Claude, X Grok, Google Gemini) with Scriban templating, type-safe prompts, and built-in resilience.


NuGet Packages

Package Version Downloads Description
Zonit.Extensions.Ai NuGet NuGet Core library with prompts and DI
Zonit.Extensions.Ai.Abstractions NuGet NuGet Interfaces and contracts
Zonit.Extensions.Ai.OpenAi NuGet NuGet OpenAI provider (GPT-5, O3, DALL-E)
Zonit.Extensions.Ai.Anthropic NuGet NuGet Anthropic provider (Claude 4)
Zonit.Extensions.Ai.Google NuGet NuGet Google provider (Gemini)
Zonit.Extensions.Ai.X NuGet NuGet X provider (Grok)
Zonit.Extensions.Ai.Prompts NuGet NuGet Ready-to-use example prompts
# Core library
dotnet add package Zonit.Extensions.Ai

# Add providers you need
dotnet add package Zonit.Extensions.Ai.OpenAi
dotnet add package Zonit.Extensions.Ai.Anthropic
dotnet add package Zonit.Extensions.Ai.Google
dotnet add package Zonit.Extensions.Ai.X

# Or via NuGet Package Manager
Install-Package Zonit.Extensions.Ai
Install-Package Zonit.Extensions.Ai.OpenAi

Features

  • Multi-provider - OpenAI, Anthropic, Google, X with unified API
  • Type-safe prompts - Strongly typed responses with JSON Schema
  • Scriban templating - Dynamic prompts with variables and conditions
  • Cost calculation - Estimate costs before calling API
  • Resilience - Retry, circuit breaker, timeout with Microsoft.Extensions.Http.Resilience
  • Plugin architecture - Auto-discovery of providers, safe to call AddAi multiple times

Requirements

  • .NET 8.0, 9.0, or 10.0

Quick Start

Simple Registration

// In Program.cs
services.AddAi();  // Reads from appsettings.json automatically
// appsettings.json
{
  "Ai": {
    "OpenAi": { "ApiKey": "sk-..." },
    "Anthropic": { "ApiKey": "sk-ant-..." },
    "Google": { "ApiKey": "..." },
    "X": { "ApiKey": "..." }
  }
}

Fluent Configuration

services.AddAi()
    .WithOpenAi(o => o.ApiKey = "sk-...")
    .WithAnthropic(o => o.ApiKey = "sk-ant-...")
    .WithGoogle(o => o.ApiKey = "...")
    .WithX(o => o.ApiKey = "...")
    .WithResilience(r => 
    {
        r.MaxRetryAttempts = 5;
        r.HttpClientTimeout = TimeSpan.FromMinutes(10);
    });

Combined Configuration

services.AddAi(options =>
{
    options.OpenAi.ApiKey = "sk-...";
    options.Resilience.MaxRetryAttempts = 3;
})
.WithAnthropic(o => o.ApiKey = config["Anthropic:ApiKey"]!);

Plugin Architecture

Safe to call multiple times from different plugins:

// Plugin A
services.AddAi().WithOpenAi(o => o.ApiKey = "...");

// Plugin B (does not duplicate, only adds configuration)
services.AddAi().WithAnthropic(o => o.ApiKey = "...");

Creating Prompts

public class TranslatePrompt : PromptBase<TranslateResponse>
{
    public required string Content { get; set; }
    public required string Language { get; set; }

    public override string Prompt => @"
Translate the following text into {{ language }}:
{{ content }}
";
}

[Description("Translation result")]
public class TranslateResponse
{
    [Description("Translated text")]
    public required string TranslatedText { get; set; }
    
    [Description("Detected source language")]
    public string? DetectedLanguage { get; set; }
}

// Usage
var result = await aiClient.GenerateAsync(
    new GPT51(),
    new TranslatePrompt { Content = "Hello!", Language = "Polish" }
);
Console.WriteLine(result.Value.TranslatedText); // "Cześć!"
Console.WriteLine(result.PromptName); // "Translate" (auto-generated)

Cost Calculation

All results include calculated costs using the Price value object:

var result = await aiClient.GenerateAsync(new GPT51(), prompt);

// Token usage
Console.WriteLine($"Tokens: {result.Usage.InputTokens} in / {result.Usage.OutputTokens} out");
Console.WriteLine($"Total tokens: {result.Usage.TotalTokens}");
Console.WriteLine($"Cached tokens: {result.Usage.CachedTokens}");

// Cost breakdown (Price value object from Zonit.Extensions)
Console.WriteLine($"Input cost: {result.InputCost}");      // e.g. 0.01
Console.WriteLine($"Output cost: {result.OutputCost}");    // e.g. 0.03
Console.WriteLine($"Total cost: {result.TotalCost}");      // e.g. 0.04

// Duration
Console.WriteLine($"Duration: {result.Duration.TotalSeconds:F2}s");

Estimate costs before calling

Use IAiProvider methods to calculate or estimate costs:

// Calculate text generation cost
var cost = aiClient.CalculateCost(new GPT51(), inputTokens: 1000, outputTokens: 500);
Console.WriteLine($"Cost: {cost}");

// Calculate embedding cost
var embeddingCost = aiClient.CalculateCost(new TextEmbedding3Large(), inputTokens: 1000);

// Calculate image cost
var imageCost = aiClient.CalculateCost(new GPTImage1(), imageCount: 2);

// Calculate audio transcription cost (per minute)
var audioCost = aiClient.CalculateCost(new Whisper1(), durationSeconds: 180);

// Estimate cost from prompt text (estimates tokens automatically)
var estimated = aiClient.EstimateCost(new GPT51(), "Your prompt here...", estimatedOutputTokens: 500);

Simple API

The API is designed to be simple and intuitive. The same GenerateAsync method is used for all model types - the compiler resolves the correct overload based on the model interface:

// Text generation (ILlm)
var result = await aiClient.GenerateAsync(new GPT51(), "What is 2+2?");
Console.WriteLine(result.Value); // "4"

// Typed response with prompt class
var result = await aiClient.GenerateAsync(
    new GPT51(),
    new TranslatePrompt { Content = "Hello!", Language = "Polish" }
);
Console.WriteLine(result.Value.TranslatedText); // "Cześć!"

// Image generation (IImageLlm) - same method name, different model type
var image = await aiClient.GenerateAsync(new GPTImage1(), "A sunset over mountains");
await image.Value.SaveAsync("sunset.png");

// Embeddings (IEmbeddingLlm)
var embedding = await aiClient.GenerateAsync(new TextEmbedding3Large(), "Hello world");
float[] vector = embedding.Value;

// Audio transcription (IAudioLlm)  
var audio = await AiFile.CreateFromFilePathAsync("speech.mp3");
var transcription = await aiClient.GenerateAsync(new Whisper1(), audio, language: "en");
Console.WriteLine(transcription.Value);

// Streaming
await foreach (var chunk in aiClient.StreamAsync(new GPT51(), "Tell me a story"))
{
    Console.Write(chunk);
}

Interface-based Type Detection

The model interface determines the operation:

Interface Method Returns
ILlm GenerateAsync(model, string) AiResult<string>
ILlm GenerateAsync(model, IPrompt<T>) AiResult<T>
IImageLlm GenerateAsync(model, string) AiResult<AiFile>
IEmbeddingLlm GenerateAsync(model, string) AiResult<float[]>
IAudioLlm GenerateAsync(model, AiFile) AiResult<string>

Supported Models

OpenAI

Model Class Features
GPT-5, GPT-5.1, GPT-5.2 GPT5, GPT51, GPT52, GPT52Chat Text, Vision, Image output
GPT-5 Mini/Nano GPT5Mini, GPT5Nano Cost-effective
GPT-4.1 GPT41, GPT41Mini, GPT41Nano Latest GPT-4
O3, O3-Pro, O4-mini O3, O3Pro, O4Mini Reasoning models
GPT-4o Search GPT4oSearch Web search
DALL·E GPTImage1, GPTImage1Mini Image generation

Anthropic (Claude)

Model Class Features
Sonnet 4.5 Sonnet45 Balanced, prompt caching
Opus 4, 4.1 Opus4, Opus41 Most capable
Haiku 4.5 Haiku45 Fast, cost-effective

X (Grok)

Model Class Features
Grok-4 Grok4 Web search native
Grok-4.1 Fast Grok41Fast Advanced reasoning
Grok-3 Grok3, Grok3Fast, Grok3Mini Previous gen

Google (Gemini)

Model Class Features
Gemini 2.5 Pro Gemini25Pro Most capable
Gemini 2.5 Flash Gemini25Flash Balanced
Gemini 2.0 Flash Gemini20Flash, Gemini20FlashLite Cost-effective

Scriban Templating

Properties are automatically available as snake_case:

public class EmailPrompt : PromptBase<EmailResponse>
{
    public string RecipientName { get; set; }   // {{ recipient_name }}
    public List<string> Topics { get; set; }    // {{ topics }}
    public bool IsFormal { get; set; }          // {{ is_formal }}

    public override string Prompt => @"
Write an email to {{ recipient_name }}.

{{~ if is_formal ~}}
Use formal tone.
{{~ end ~}}

Topics:
{{~ for topic in topics ~}}
- {{ topic }}
{{~ end ~}}
";
}

Files and Images

public class AnalyzePrompt : PromptBase<AnalysisResult>
{
    public override string Prompt => "Analyze the documents";
}

var file = await AiFile.CreateFromFilePathAsync("image.jpg");
var result = await aiClient.GenerateAsync(
    new GPT51(),
    new AnalyzePrompt { Files = [file] }
);

Image Generation

var result = await aiClient.GenerateAsync(
    new GPTImage1
    {
        Quality = GPTImage1.QualityType.High,
        Size = GPTImage1.SizeType.Landscape,
        Style = GPTImage1.StyleType.Natural
    },
    "A sunset over mountains"
);
await result.Value.SaveAsync("sunset.png");

Resilience Configuration

{
  "Ai": {
    "Resilience": {
      "HttpClientTimeout": "00:05:00",
      "MaxRetryAttempts": 3,
      "RetryBaseDelay": "00:00:02",
      "RetryMaxDelay": "00:00:30",
      "UseJitter": true
    }
  }
}

Example Prompts

The Zonit.Extensions.Ai.Prompts package includes ready-to-use prompts:

// Translation
var result = await ai.GenerateAsync(new GPT51(), 
    new TranslatePrompt { Content = "Hello", Language = "Polish" });

// Sentiment analysis
var result = await ai.GenerateAsync(new GPT51(),
    new SentimentPrompt { Content = "I love this product!" });

// Summarization
var result = await ai.GenerateAsync(new GPT51(),
    new SummarizePrompt { Content = longText, MaxWords = 100 });

// Code generation
var result = await ai.GenerateAsync(new GPT51(),
    new CodePrompt { Description = "Fibonacci function", Language = "C#" });

// Classification
var result = await ai.GenerateAsync(new GPT51(),
    new ClassifyPrompt { Content = text, Categories = ["Tech", "Sports", "News"] });

Architecture

Zonit.Extensions.Ai
├── Abstractions     - Interfaces (IPrompt, IAiProvider, ILlm)
├── Core             - PromptBase, AiBuilder, JsonParsers
├── Providers        - OpenAI, Anthropic, Google, X
├── Prompts          - Ready-to-use examples
└── SourceGenerators - AOT support

AOT and Trimming

This library uses reflection for:

  • Scriban templating (property discovery)
  • JSON serialization (response parsing)
  • Provider auto-discovery (assembly scanning)

For AOT projects, use Source Generators:

  • AiJsonSerializerGenerator - Generates JsonSerializerContext
  • AiProviderRegistrationGenerator - Generates provider registration

License

MIT License

Product Compatible and additional computed target framework versions.
.NET 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.  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 is compatible.  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.

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
2.0.4 34 4/27/2026
2.0.3 30 4/27/2026
2.0.2 29 4/27/2026
2.0.1 37 4/26/2026
2.0.0 33 4/26/2026
1.50.0 34 4/26/2026
1.21.0 34 4/25/2026
1.20.0 123 1/31/2026
1.13.0 104 1/25/2026
1.12.0 113 1/22/2026
1.11.0 105 1/22/2026
1.10.0 107 1/22/2026
1.9.20 105 1/22/2026
1.9.19 106 1/21/2026
1.9.18 104 1/21/2026
1.9.17 106 1/21/2026
1.9.16 101 1/21/2026
1.9.15 101 1/21/2026
1.9.14 99 1/20/2026
1.9.0 98 1/20/2026
Loading failed