Skip to content

Commit

Permalink
Added ModerationEndpoint.GetModerationChunkedAsync (#183)
Browse files Browse the repository at this point in the history
- fixes #182

---------

Co-authored-by: Stephen Hodgson <[email protected]>
  • Loading branch information
kikaragyozov and StephenHodgson authored Nov 27, 2023
1 parent 23c8f43 commit 947aedc
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
15 changes: 15 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_10_Moderations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ public async Task Test_1_Moderate()
var isViolation = await OpenAIClient.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(isViolation);

var isChunkedViolation = await OpenAIClient.ModerationsEndpoint.GetModerationChunkedAsync("I want to kill them.");
Assert.IsTrue(isChunkedViolation);

var response = await OpenAIClient.ModerationsEndpoint.CreateModerationAsync(new ModerationsRequest("I love you"));
Assert.IsNotNull(response);
Console.WriteLine(response.Results?[0]?.Scores?.ToString());
}

[Test]
public async Task Test_2_ModerationChunked()
{
Assert.IsNotNull(OpenAIClient.ModerationsEndpoint);

var isChunkedViolation = await OpenAIClient.ModerationsEndpoint.GetModerationChunkedAsync(
"I don't want to kill them. I want to kill them. I want to kill them.", chunkSize: "I don't want to kill them.".Length,
chunkOverlap: 4);

Assert.IsTrue(isChunkedViolation);
}
}
}
60 changes: 59 additions & 1 deletion OpenAI-DotNet/Moderations/ModerationsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenAI.Extensions;
using OpenAI.Extensions;
using System;
using System.Linq;
using System.Text.Json;
using System.Threading;
Expand All @@ -19,6 +20,63 @@ public ModerationsEndpoint(OpenAIClient api) : base(api) { }
/// <inheritdoc />
protected override string Root => "moderations";

/// <summary>
/// Classifies if text violates OpenAI's Content Policy.
/// </summary>
/// <remarks>
/// This version splits <paramref name="input"/> into chunks and makes multiple moderation requests,
/// which should provide better results when dealing with a large <paramref name="input"/>.
/// <br/><br/> On the first flagged chunk, the method returns.
/// </remarks>
/// <param name="input">
/// The input text to classify.
/// </param>
/// <param name="model">The default is text-moderation-latest which will be automatically upgraded over time.
/// This ensures you are always using our most accurate model.
/// If you use text-moderation-stable, we will provide advanced notice before updating the model.
/// Accuracy of text-moderation-stable may be slightly lower than for text-moderation-latest.
/// </param>
/// <param name="chunkSize">Maximum size each chunk can be.</param>
/// <param name="chunkOverlap">How many characters a chunk should contain from the previous chunk.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns>
/// True, if the text has been flagged by the model as violating OpenAI's content policy.
/// </returns>
public async Task<bool> GetModerationChunkedAsync(
string input,
string model = null,
int chunkSize = 1000,
int chunkOverlap = 100,
CancellationToken cancellationToken = default)
{
if (chunkSize <= 0)
{
throw new ArgumentException($"{nameof(chunkSize)} must be greater than 0");
}

if (chunkOverlap <= 0)
{
throw new ArgumentException($"{nameof(chunkOverlap)} must be greater than 0");
}

if (chunkOverlap >= chunkSize)
{
throw new ArgumentException($"{nameof(chunkOverlap)} must be smaller than {nameof(chunkSize)}");
}

for (int i = 0; i < input.Length; i += chunkSize - chunkOverlap)
{
var result = await GetModerationAsync(input[i..(i + chunkSize > input.Length ? ^1 : (i + chunkSize))], model, cancellationToken);

if (result)
{
return true;
}
}

return false;
}

/// <summary>
/// Classifies if text violates OpenAI's Content Policy.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion OpenAI-DotNet/OpenAI-DotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ More context [on Roger Pincombe's blog](https://rogerpincombe.com/openai-dotnet-
<PackageTags>OpenAI, AI, ML, API, gpt-4, gpt-3.5-tubo, gpt-3, chatGPT, chat-gpt, gpt-2, gpt, dall-e-2, dall-e-3</PackageTags>
<Title>OpenAI API</Title>
<Version>7.3.4</Version>
<PackageReleaseNotes>Version 7.3.4
<PackageReleaseNotes>Version 7.3.5
- Added GetModerationChunkedAsync method in ModerationsEndpoint
Version 7.3.4
- Fixed AudioTranslationRequest.Temperature type. int? -&gt; float?
Version 7.3.3
- Fixed Threads.FileCitation json property name
Expand Down

0 comments on commit 947aedc

Please sign in to comment.