Zonit.Extensions.Ai.Google
1.9.0
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
<PackageReference Include="Zonit.Extensions.Ai.Google" Version="1.9.0" />
<PackageVersion Include="Zonit.Extensions.Ai.Google" Version="1.9.0" />
<PackageReference Include="Zonit.Extensions.Ai.Google" />
paket add Zonit.Extensions.Ai.Google --version 1.9.0
#r "nuget: Zonit.Extensions.Ai.Google, 1.9.0"
#:package Zonit.Extensions.Ai.Google@1.9.0
#addin nuget:?package=Zonit.Extensions.Ai.Google&version=1.9.0
#tool nuget:?package=Zonit.Extensions.Ai.Google&version=1.9.0
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 | Core library with prompts and DI | ||
| Zonit.Extensions.Ai.Abstractions | Interfaces and contracts | ||
| Zonit.Extensions.Ai.OpenAi | OpenAI provider (GPT-5, O3, DALL-E) | ||
| Zonit.Extensions.Ai.Anthropic | Anthropic provider (Claude 4) | ||
| Zonit.Extensions.Ai.Google | Google provider (Gemini) | ||
| Zonit.Extensions.Ai.X | X provider (Grok) | ||
| Zonit.Extensions.Ai.Prompts | 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 JsonSerializerContextAiProviderRegistrationGenerator- Generates provider registration
License
MIT License
| Product | Versions 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. |
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.2)
- Microsoft.Extensions.Http.Resilience (>= 10.2.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.2)
- Microsoft.Extensions.Options (>= 10.0.2)
- Zonit.Extensions.Ai (>= 1.9.0)
-
net8.0
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Http.Resilience (>= 8.10.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.3)
- Microsoft.Extensions.Options (>= 8.0.2)
- Zonit.Extensions.Ai (>= 1.9.0)
-
net9.0
- Microsoft.Extensions.Http (>= 9.0.11)
- Microsoft.Extensions.Http.Resilience (>= 9.10.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.11)
- Microsoft.Extensions.Options (>= 9.0.11)
- Zonit.Extensions.Ai (>= 1.9.0)
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 |