Skip to content

Repository files navigation

Soenneker.OpenAI.Moderation.HttpClients

Provides a cached HttpClient configured for OpenAI moderation requests.

Install

dotnet add package Soenneker.OpenAI.Moderation.HttpClients

Registration

using Microsoft.Extensions.DependencyInjection;
using Soenneker.OpenAI.Moderation.HttpClients.Registrars;

services.AddOpenAIModerationHttpClientAsSingleton();

Use AddOpenAIModerationHttpClientAsScoped() when the wrapper must follow a consumer scope. Both registrations reuse the singleton HTTP-client cache; disposing a scoped wrapper does not destroy the shared client.

Configuration

The API key is required:

{
  "OpenAI": {
    "Moderation": {
      "ApiKey": "..."
    }
  }
}

Keep the key in a secret store or environment variable. The client uses https://api.openai.com/v1 and an Authorization: Bearer <token> header by default. Compatible endpoints can be configured with these optional keys:

{
  "OpenAI": {
    "ClientBaseUrl": "https://example.com/v1",
    "AuthHeaderName": "Authorization",
    "AuthHeaderValueTemplate": "Bearer {token}"
  }
}

{token} is replaced with OpenAI:Moderation:ApiKey.

Usage

Inject IOpenAIModerationHttpClient and retrieve the cached client:

using Soenneker.OpenAI.Moderation.HttpClients.Abstract;

HttpClient client = await moderationHttpClient.Get(cancellationToken);

using var request = new HttpRequestMessage(HttpMethod.Get, "models");
using HttpResponseMessage response = await client.SendAsync(request, cancellationToken);

response.EnsureSuccessStatusCode();

Do not dispose the returned HttpClient; its lifetime is owned by the singleton cache.

About

An HTTPClient singleton for OpenAI Moderation

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages