Zonit.Extensions.Ai.Infrastructure
1.0.11
dotnet add package Zonit.Extensions.Ai.Infrastructure --version 1.0.11
NuGet\Install-Package Zonit.Extensions.Ai.Infrastructure -Version 1.0.11
<PackageReference Include="Zonit.Extensions.Ai.Infrastructure" Version="1.0.11" />
<PackageVersion Include="Zonit.Extensions.Ai.Infrastructure" Version="1.0.11" />
<PackageReference Include="Zonit.Extensions.Ai.Infrastructure" />
paket add Zonit.Extensions.Ai.Infrastructure --version 1.0.11
#r "nuget: Zonit.Extensions.Ai.Infrastructure, 1.0.11"
#addin nuget:?package=Zonit.Extensions.Ai.Infrastructure&version=1.0.11
#tool nuget:?package=Zonit.Extensions.Ai.Infrastructure&version=1.0.11
Zonit.Extensions.Ai
A .NET library for integrating with AI models (OpenAI GPT) in applications, providing a simple and flexible architecture for text and image generation.
Main Features
- OpenAI Model Support: GPT-4, GPT-4 Turbo, reasoning models (o1), DALL�E image generation
- Flexible Architecture: Interface-driven, based on Clean Architecture
- Type-safe Prompts: Strongly-typed prompts with automatic JSON Schema validation
- AI Tools: Web search, file search with configuration
- File Management: Built-in support for images and documents
- Metadata Tracking: Automatic logging of cost, token counts, and execution time
Installation
Install-Package Zonit.Extensions.Ai
Configuration
1. Register Services
services.AddAi(options =>
{
options.OpenAiKey = "your-openai-api-key";
});
2. appsettings.json
{
"Ai": {
"OpenAiKey": "your-openai-api-key"
}
}
Basic Usage
Text Generation
public class TranslatePrompt : PromptBase<TranslateResponse>
{
public string Content { get; set; }
public string Language { get; set; }
public string Culture { get; set; }
public override string Prompt =>
$"Translate the text '{Content}' into {Language} (culture: {Culture})";
}
public class TranslateResponse
{
public string TranslatedText { get; set; }
public string DetectedLanguage { get; set; }
}
// Usage:
var prompt = new TranslatePrompt
{
Content = "Hello world!",
Language = "pl",
Culture = "pl-PL"
};
var result = await aiClient.GenerateAsync(prompt, new GPT4());
Console.WriteLine(result.Value.TranslatedText);
Image Generation
public class AnimalPrompt : PromptBase<byte[]>
{
public string Animal { get; set; }
public override string Prompt =>
$"Generate an image depicting a {Animal}";
}
// Usage:
var prompt = new AnimalPrompt { Animal = "dog" };
var result = await aiClient.GenerateAsync(
prompt,
new GPTImage1
{
Quality = GPTImage1.QualityType.High,
Size = GPTImage1.SizeType.Square
}
);
// Save the image:
await File.WriteAllBytesAsync("dog.png", result.Value.Data);
AI Models
Text Models
// Basic GPT-4
var gpt4 = new GPT4();
// GPT-4 Turbo with custom settings
var gpt4Turbo = new GPT4Turbo
{
Temperature = 0.7,
TopP = 0.9,
MaxTokens = 2000
};
// Reasoning model (o1)
var reasoning = new GPT4Reasoning
{
Reason = OpenAiReasoningBase.ReasonType.High,
ReasonSummary = OpenAiReasoningBase.ReasonSummaryType.Detailed
};
Image Models
var imageModel = new GPTImage1
{
Quality = GPTImage1.QualityType.High,
Size = GPTImage1.SizeType.Landscape,
Style = GPTImage1.StyleType.Natural
};
AI Tools
Web Search
public class SearchPrompt : PromptBase<SearchResponse>
{
public string Query { get; set; }
public override string Prompt => $"Search for information about: {Query}";
public override IReadOnlyList<ITool> Tools =>
new List<ITool>
{
new WebSearchTool
{
ContextSize = WebSearchTool.ContextSizeType.High
}
};
public override ToolsType ToolChoice => ToolsType.WebSearch;
}
File Search
public override IReadOnlyList<ITool> Tools =>
new List<ITool> { new FileSearchTool() };
public override ToolsType ToolChoice => ToolsType.FileSearch;
File Management
Create a File from Data
var fileData = await File.ReadAllBytesAsync("document.pdf");
var file = new FileModel("document.pdf", "application/pdf", fileData);
Create a File from Path
var file = await FileModel.CreateFromFilePathAsync("path/to/image.jpg");
Save a File
await file.SaveToFileAsync("output/saved-file.jpg");
Supported Formats
- Images: JPG, PNG, GIF, BMP, WebP
- Documents: PDF, DOC/DOCX, XLS/XLSX, PPT/PPTX
- Text: TXT, JSON, CSV, XML
Metadata & Costs
var result = await aiClient.GenerateAsync(prompt, model);
// Cost information
Console.WriteLine($"Input cost: ${result.MetaData.PriceInput:F6}");
Console.WriteLine($"Output cost: ${result.MetaData.PriceOutput:F6}");
Console.WriteLine($"Total cost: ${result.MetaData.PriceTotal:F6}");
// Token counts
Console.WriteLine($"Input tokens: {result.MetaData.InputTokenCount}");
Console.WriteLine($"Output tokens: {result.MetaData.OutputTokenCount}");
// Execution time
Console.WriteLine($"Duration: {result.MetaData.Duration.TotalSeconds:F2}s");
Advanced Features
Logging Responses
var model = new GPT4
{
StoreLogs = true
};
Custom JSON Schema
The library automatically generates JSON Schema from response classes. You can add descriptions with attributes:
[Description("Response containing the translated text")]
public class TranslateResponse
{
[Description("The text translated into the target language")]
public string TranslatedText { get; set; }
[Description("The detected source language")]
public string DetectedLanguage { get; set; }
}
Error Handling
try
{
var result = await aiClient.GenerateAsync(prompt, model);
// Use result.Value
}
catch (JsonException ex)
{
// JSON parsing error
}
catch (InvalidOperationException ex)
{
// API communication error
}
Architecture
The library follows Clean Architecture with layered separation:
- Abstractions: Interfaces and contracts
- Domain: Domain models and business logic
- Application: Application services and configuration
- Infrastructure: Repository implementations (OpenAI)
- LLM: Language model definitions
Complete Application Example
public class AiService
{
private readonly IAiClient _aiClient;
public AiService(IAiClient aiClient)
{
_aiClient = aiClient;
}
public async Task<string> TranslateAsync(string text, string targetLanguage)
{
var prompt = new TranslatePrompt
{
Content = text,
Language = targetLanguage,
Culture = $"{targetLanguage}-{targetLanguage.ToUpper()}"
};
var result = await _aiClient.GenerateAsync(prompt, new GPT4Turbo());
return result.Value.TranslatedText;
}
public async Task<byte[]> GenerateImageAsync(string description)
{
var prompt = new ImagePrompt { Description = description };
var result = await _aiClient.GenerateAsync(
prompt,
new GPTImage1
{
Quality = GPTImage1.QualityType.High,
Size = GPTImage1.SizeType.Square
}
);
return result.Value.Data;
}
}
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.Configuration.Abstractions (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.Http (>= 10.0.0-preview.4.25258.110)
- Microsoft.Extensions.Http.Resilience (>= 9.5.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.0-preview.4.25258.110)
- OpenAI (>= 2.2.0)
- Zonit.Extensions.Ai.Application (>= 1.0.11)
-
net8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- Microsoft.Extensions.Http (>= 8.0.1)
- Microsoft.Extensions.Http.Resilience (>= 8.10.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
- OpenAI (>= 2.2.0)
- Zonit.Extensions.Ai.Application (>= 1.0.11)
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.5)
- Microsoft.Extensions.Http (>= 9.0.5)
- Microsoft.Extensions.Http.Resilience (>= 9.5.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 9.0.5)
- OpenAI (>= 2.2.0)
- Zonit.Extensions.Ai.Application (>= 1.0.11)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Zonit.Extensions.Ai.Infrastructure:
Package | Downloads |
---|---|
Zonit.Extensions.Ai
A powerful and versatile library for integrating AI models like OpenAI, DeepSeek, Claude, Grok, and Gemini. Easily build chatbots, generate text and images, and enhance your applications with cutting-edge AI capabilities. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.11 | 59 | 7/4/2025 |
1.0.10 | 63 | 7/4/2025 |
1.0.9 | 69 | 7/4/2025 |
1.0.8 | 97 | 7/4/2025 |
1.0.7 | 103 | 7/4/2025 |
1.0.6 | 105 | 7/3/2025 |
1.0.5 | 100 | 7/3/2025 |
1.0.4 | 102 | 7/3/2025 |
1.0.3 | 102 | 7/3/2025 |
1.0.2 | 101 | 7/3/2025 |
1.0.1 | 106 | 7/3/2025 |
1.0.0 | 105 | 7/3/2025 |
0.0.9 | 118 | 6/3/2025 |
0.0.8 | 118 | 6/3/2025 |
0.0.7 | 123 | 5/20/2025 |
0.0.6 | 118 | 5/20/2025 |
0.0.5 | 115 | 5/20/2025 |
0.0.4 | 122 | 5/19/2025 |
0.0.3 | 118 | 5/19/2025 |
0.0.2 | 121 | 5/19/2025 |
0.0.1 | 110 | 5/2/2025 |