Skip to content

Moderations

Zoltan Juhasz edited this page May 2, 2023 · 1 revision

Given a input text, outputs if the model classifies it as violating OpenAI's content policy. Also it is useful for other services, which can accept these policies.

More info: https://platform.openai.com/docs/api-reference/moderations

static async Task Main(string[] args)
{
    // This example demonstrates, how to check, if a set of text can pass the OpenAI moderation rules.
    //
    // The very first step to create an account at OpenAI: https://platform.openai.com/
    // Using the loggedIn account, navigate to https://platform.openai.com/account/api-keys
    // Here you can create apiKey(s)

    using var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((builder, services) =>
    {
        services.AddForgeOpenAI(options => {
            options.AuthenticationInfo = builder.Configuration["OpenAI:ApiKey"]!;
        });
    })
    .Build();

    IOpenAIService openAi = host.Services.GetService<IOpenAIService>()!;

    ModerationRequest request = new ModerationRequest(new string[] { "I want to kill them." });
    HttpOperationResult<ModerationResponse> response = 
        await openAi.ModerationService
            .GetAsync(request, CancellationToken.None)
                .ConfigureAwait(false);

    if (response.IsSuccess)
    {
        Console.WriteLine(response.Result!);
    }
    else
    {
        Console.WriteLine(response);
    }

}