OpenAI provider implementation for RepletoryLib AI (GPT, DALL-E, Whisper, TTS, Moderation)
$ dotnet add package RepletoryLib.Ai.OpenAiOpenAI provider implementation for RepletoryLib AI services (GPT, DALL-E, Whisper, TTS, Moderation).
Part of the RepletoryLib ecosystem -- standalone, reusable .NET 10 libraries with zero business logic.
RepletoryLib.Ai.OpenAi implements all five AI service interfaces from RepletoryLib.Ai.Abstractions using direct HTTP calls to the OpenAI API. Each service uses a named HttpClient with Polly resilience policies (retry with Retry-After support, circuit breaker) and returns Result<T> for consistent error handling.
Result<T> with HTTP status code mappingdotnet add package RepletoryLib.Ai.OpenAi
| Package | Type |
|---|---|
RepletoryLib.Ai.Abstractions | RepletoryLib |
Microsoft.Extensions.Http.Polly | NuGet |
Microsoft.Extensions.Http | NuGet |
Microsoft.Extensions.Options.ConfigurationExtensions | NuGet |
using RepletoryLib.Ai.OpenAi;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRepletoryOpenAi(builder.Configuration);
{
"Ai": {
"OpenAi": {
"ApiKey": "sk-...",
"DefaultChatModel": "gpt-4o",
"DefaultEmbeddingModel": "text-embedding-3-small",
"TimeoutSeconds": 120,
"RetryCount": 3
}
}
}
| Property | Type | Default | Description |
|---|---|---|---|
ApiKey | string | "" | OpenAI API key |
BaseUrl | string | https://api.openai.com/v1 | API base URL |
DefaultChatModel | string | gpt-4o | Default chat model |
DefaultEmbeddingModel | string | text-embedding-3-small | Default embedding model |
DefaultImageModel | string | dall-e-3 | Default image model |
DefaultAudioModel | string | whisper-1 | Default transcription model |
DefaultTtsModel | string | tts-1 | Default TTS model |
DefaultModerationModel | string | omni-moderation-latest | Default moderation model |
MaxTokens | int | 4096 | Default max output tokens |
TimeoutSeconds | int | 120 | Request timeout |
RetryCount | int | 3 | Retry attempts with exponential backoff |
CircuitBreakerThreshold | int | 5 | Failures before circuit opens |
CircuitBreakerDurationSeconds | int | 30 | Duration circuit stays open |
Organization | string? | null | Optional OpenAI organization ID |
using RepletoryLib.Ai.Abstractions.Interfaces;
using RepletoryLib.Ai.Abstractions.Models.Chat;
public class ChatService
{
private readonly IChatCompletionService _chat;
public ChatService(IChatCompletionService chat) => _chat = chat;
public async Task<string?> AskAsync(string question)
{
var request = new ChatRequest
{
Messages = [ChatMessage.User(question)]
};
var result = await _chat.CompleteAsync(request);
return result.IsSuccess ? result.Data?.Content : null;
}
}
await foreach (var chunk in _chat.StreamAsync(request))
{
if (chunk.ContentDelta is not null)
Console.Write(chunk.ContentDelta);
if (chunk.IsFinal)
Console.WriteLine($"\nTokens: {chunk.FinalUsage?.TotalTokens}");
}
var result = await _embedding.EmbedSingleAsync("Hello, world!");
if (result.IsSuccess)
Console.WriteLine($"Dimensions: {result.Data!.Length}");
var result = await _image.GenerateAsync(new ImageGenerationRequest
{
Prompt = "A cat wearing a top hat",
Size = ImageSize.Square1024,
Quality = ImageQuality.Hd
});
| Interface | Implementation |
|---|---|
IChatCompletionService | OpenAiChatService |
IEmbeddingService | OpenAiEmbeddingService |
IImageGenerationService | OpenAiImageService |
IAudioService | OpenAiAudioService |
IModerationService | OpenAiModerationService |
IAiService | CompositeAiService |
All services are registered with TryAddScoped, allowing them to be overridden by registering your own implementation first.
| Package | Relationship |
|---|---|
RepletoryLib.Ai.Abstractions | Provider-agnostic interfaces and models |
RepletoryLib.Common | Result<T> for response wrapping |
RepletoryLib.Tracing | HTTP calls instrumented by OpenTelemetry |
RepletoryLib.Caching.Abstractions | Optional response caching |
| Issue | Solution |
|---|---|
| 401 Unauthorized | Verify ApiKey is set correctly in configuration. |
| 429 Rate Limited | Retry policy handles this automatically. Increase RetryCount or add delays. |
| Timeout errors | Increase TimeoutSeconds. Streaming requests may need longer timeouts. |
| Circuit breaker open | Wait for CircuitBreakerDurationSeconds or restart the application. |
This project is licensed under the MIT License.
Copyright (c) 2024-2026 Repletory.
For complete documentation, infrastructure setup, and configuration reference, see the RepletoryLib main repository.