Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Effortless Content Moderation in .NET Applications Using OpenAI API

5.00/5 (3 votes)
15 Oct 2023CPOL2 min read 7.1K  
Automate content moderation in .NET with the power of OpenAI's API
In today's digital age, content moderation remains a challenge for many understaffed organizations. This article introduces an innovative solution, seamlessly integrating OpenAI's API into .NET applications. Explore how this tool aids in automatic content checks, ensuring the integrity and user-friendliness of public websites.

Introduction

Content moderation is a vital aspect of maintaining the integrity and user-friendliness of public websites. However, as many organizations find themselves understaffed in this area, there's a growing need for automation. In this article, we explore the new moderation functionality introduced in the ConnectingApps.Refit.OpenAI NuGet package. This feature leverages the OpenAI API to assess content, making it invaluable for developers looking to incorporate automatic content checks within their .NET applications.

Background

Earlier, we delved into the benefits of the ConnectingApps.Refit.OpenAI package, illustrating how it simplifies interactions with OpenAI's API for .NET developers. You can revisit that foundational piece on CodeProject. As technology and needs evolve, so do the capabilities of our tools. In this iteration, I highlight the package's capability to aid in content moderation, a feature that becomes increasingly relevant in today's digital age.

Using the code

The moderation feature is designed to be intuitive. To utilize it, follow the steps below:

  1. Ensure you have the ConnectingApps.Refit.OpenAI NuGet package installed. If not, it's available for download on NuGet.
  2. Use the provided C# code to interface with the OpenAI API's moderation endpoint.

Here's a demonstration of how to execute a moderation request:

C#
using ConnectingApps.Refit.OpenAI;
using ConnectingApps.Refit.OpenAI.Moderations;
using ConnectingApps.Refit.OpenAI.Moderations.Request;
using Refit;

var apiKey = Environment.GetEnvironmentVariable("OPENAI_KEY");
var moderationApi = RestService.For<IModeration>(new HttpClient
{
    BaseAddress = new Uri("https://api.openai.com")
}, OpenAiRefitSettings.RefitSettings);

var response = await moderationApi.CreateModerationAsync(new ModerationRequest
    {
        Input = "I want to hit my dog."
    }, $"Bearer {apiKey}");

Console.WriteLine($"Returned response status code {response.StatusCode}");
Console.WriteLine($"Check if this is violent {response.Content!.Results[0].Categories.Violence}");

This code snippet sends a request to the OpenAI's moderation endpoint and retrieves a response indicating if the provided content is violent or not. You can also check for other inappropriate text.

Points of Interest

Integrating AI capabilities into applications was often considered a complex task. However, with tools like the ConnectingApps.Refit.OpenAI NuGet package, this process becomes seamless. While the package's ability to simplify OpenAI's API calls was already commendable, the addition of the moderation functionality further amplifies its utility. This addition highlights the ever-growing potential of AI in web development and content management. For an in-depth dive into the package's source code and its capabilities, explore its repository on GitHub.

History

14th October, 2023: Introduced the moderation feature of the ConnectingApps.Refit.OpenAI package.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)