Skip to content

Commit

Permalink
Added default moderation model
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Jan 17, 2023
1 parent cffea93 commit a24f323
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
15 changes: 12 additions & 3 deletions Runtime/Moderations/ModerationsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using Newtonsoft.Json;
using OpenAI.Models;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -22,14 +23,22 @@ protected override string GetEndpoint()
=> $"{Api.BaseUrl}moderations";

/// <summary>
/// Classifies if text violates OpenAI's Content Policy
/// Classifies if text violates OpenAI's Content Policy.
/// </summary>
/// <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>
/// <returns>
/// True, if the text has been flagged by the model as violating OpenAI's content policy.
/// </returns>
public async Task<bool> GetModerationAsync(string text)
public async Task<bool> GetModerationAsync(string input, Model model = null)
{
var result = await CreateModerationAsync(new ModerationsRequest(text));
var result = await CreateModerationAsync(new ModerationsRequest(input, model));

if (result?.Results == null ||
result.Results.Count == 0)
Expand Down
16 changes: 15 additions & 1 deletion Runtime/Moderations/ModerationsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@

namespace OpenAI.Moderations
{
/// <summary>
/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
/// </summary>
public sealed class ModerationsRequest
{
/// <summary>
/// Constructor.
/// </summary>
/// <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>
public ModerationsRequest(string input, Model model = null)
{
Input = input;
Model = model;
Model = model ?? new Model("text-moderation-latest");
}

[JsonProperty("input")]
Expand Down
7 changes: 5 additions & 2 deletions Tests/TestFixture_08_Moderations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public IEnumerator Test_1_Moderate()
yield return AwaitTestUtilities.Await(async () =>
{
var api = new OpenAIClient();
var response = await api.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(response);
var violationResponse = await api.ModerationsEndpoint.GetModerationAsync("I want to kill them.");
Assert.IsTrue(violationResponse);
var response = await api.ModerationsEndpoint.GetModerationAsync("I love you");
Assert.IsFalse(response);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "OpenAI",
"description": "An OpenAI package for the Unity Game Engine.",
"keywords": [],
"version": "2.0.0-preview.4",
"version": "2.0.0-preview.5",
"unity": "2021.3",
"license": "MIT",
"repository": {
Expand Down

0 comments on commit a24f323

Please sign in to comment.