Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace OpenAI_API.Moderations
namespace OpenAI_API.Moderation
{
/// <summary>
/// This endpoint classifies text against the OpenAI Content Policy
Expand Down Expand Up @@ -32,18 +32,18 @@ internal ModerationEndpoint(OpenAIAPI api) : base(api) { }
/// </summary>
/// <param name="input">Text to classify</param>
/// <returns>Asynchronously returns the classification result</returns>
public async Task<ModerationResult> CreateModerationAsync(string input)
public async Task<ModerationResult> CallModerationAsync(string input)
{
ModerationRequest req = new ModerationRequest(DefaultModerationRequestArgs.Model, input);
return await CreateModerationAsync(req);
return await CallModerationAsync(req);
}

/// <summary>
/// Ask the API to classify the text using a custom request.
/// </summary>
/// <param name="request">Request to send to the API</param>
/// <returns>Asynchronously returns the classification result</returns>
public async Task<ModerationResult> CreateModerationAsync(ModerationRequest request)
public async Task<ModerationResult> CallModerationAsync(ModerationRequest request)
{
return await HttpPost<ModerationResult>(postData: request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
using OpenAI_API.Models;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace OpenAI_API.Moderations
namespace OpenAI_API.Moderation
{
/// <summary>
/// Represents a request to the Moderations API.
/// </summary>
public class ModerationRequest
{
/// <summary>
/// Represents a request to the Moderations API.
/// </summary>
public class ModerationRequest
{

/// <summary>
/// Which Moderation model to use for this request
/// Which Moderation model to use for this request. Two content moderations models are available: <see cref="Model.TextModerationStable"/> and <see cref="Model.TextModerationLatest"/>. The default is <see cref="Model.TextModerationLatest"/> which will be automatically upgraded over time.This ensures you are always using our most accurate model.If you use <see cref="Model.TextModerationStable"/>, we will provide advanced notice before updating the model. Accuracy of <see cref="Model.TextModerationStable"/> may be slightly lower than for <see cref="Model.TextModerationLatest"/>.
/// </summary>
[JsonProperty("model")]
public string Model { get; set; }

/// <summary>
/// Main text to classify
/// The input text to classify
/// </summary>
[JsonProperty("input")]
public string Input { get; set; }
Expand All @@ -34,9 +36,9 @@ public ModerationRequest()
/// <summary>
/// Creates a new <see cref="ModerationRequest"/> with the specified parameters
/// </summary>
/// <param name="model">The model to use. You can use <see cref="ModelsEndpoint.GetModelsAsync()"/> to see all of your available models, or use a standard model like <see cref="Model.TextModerationLatest"/>.</param>
/// <param name="input">The prompt to classify</param>
public ModerationRequest(Model model, string input)
/// <param name="model">The model to use. You can use <see cref="ModelsEndpoint.GetModelsAsync()"/> to see all of your available models, or use a standard model like <see cref="Model.TextModerationLatest"/>.</param>
public ModerationRequest(string input, Model model)
{
Model = model;
this.Input = input;
Expand Down
197 changes: 197 additions & 0 deletions OpenAI_API/Moderation/ModerationResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OpenAI_API.Moderation
{
/// <summary>
/// Represents a moderation result returned by the Moderations API
/// </summary>
public class ModerationResult : ApiResultBase
{
/// <summary>
/// List of results returned from the Moderations API request
/// </summary>
[JsonProperty("results")]
public List<Result> Results { get; set; }

/// <summary>
/// The unique identifier associated with a moderation request
/// Consists of the prefix "modr-" followed by a randomly generated alphanumeric string
/// </summary>
[JsonProperty("id")]
public string Id { get; set; }

/// <summary>
/// Convenience function to return the highest confidence category for which the content was flagged, or null if no content flags
/// </summary>
/// <returns>the highest confidence category for which the content was flagged, or null if no content flags</returns>
public override string ToString()
{
return Results?.First()?.MainContentFlag;
}
}

/// <summary>
/// The result generated by the Moderations API request
/// </summary>
public class Result
{
/// <summary>
/// A series of categories that the content could be flagged for. Values are bool's, indicating if the txt is flagged in that category
/// </summary>
[JsonProperty("categories")]
public IDictionary<string, bool> Categories { get; set; }

/// <summary>
/// Confidence scores for the different category flags. Values are between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("category_scores")]
public IDictionary<string, double> CategoryScores { get; set; }

/// <summary>
/// True if the text was flagged in any of the categories
/// </summary>
[JsonProperty("flagged")]
public bool Flagged { get; set; }

/// <summary>
/// Returns a list of all categories for which the content was flagged, sorted from highest confidence to lowest
/// </summary>
public IList<string> FlaggedCategories
{
get
{
return Categories.Where(kv => kv.Value).OrderByDescending(kv => CategoryScores?[kv.Key]).Select(kv => kv.Key).ToList();
}
}

/// <summary>
/// Returns the highest confidence category for which the content was flagged, or null if no content flags
/// </summary>
public string MainContentFlag
{
get
{
return FlaggedCategories.FirstOrDefault();
}
}

/// <summary>
/// Returns the highest confidence flag score across all categories
/// </summary>
public double HighestFlagScore
{
get
{
return CategoryScores.OrderByDescending(kv => kv.Value).First().Value;
}
}


}

/// <summary>
/// Series of boolean values indiciating what the text is flagged for
/// </summary>
public class Categories
{
/// <summary>
/// If the text contains hate speech
/// </summary>
[JsonProperty("hate")]
public bool Hate { get; set; }

/// <summary>
/// If the text contains hate / threatening speech
/// </summary>
[JsonProperty("hate/threatening")]
public bool HateThreatening { get; set; }
/// <summary>
/// If the text contains content about self-harm
/// </summary>
[JsonProperty("self-harm")]
public bool SelfHarm { get; set; }

/// <summary>
/// If the text contains sexual content
/// </summary>
[JsonProperty("sexual")]
public bool Sexual { get; set; }

/// <summary>
/// If the text contains sexual content featuring minors
/// </summary>
[JsonProperty("sexual/minors")]
public bool SexualMinors { get; set; }

/// <summary>
/// If the text contains violent content
/// </summary>
[JsonProperty("violence")]
public bool Violence { get; set; }

/// <summary>
/// If the text contains violent and graphic content
/// </summary>
[JsonProperty("violence/graphic")]
public bool ViolenceGraphic { get; set; }
}

/// <summary>
/// Confidence scores for the different category flags
/// </summary>
public class CategoryScores
{
/// <summary>
/// Confidence score indicating "hate" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("hate")]
public double Hate { get; set; }

/// <summary>
/// Confidence score indicating "hate/threatening" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("hate/threatening")]
public double HateThreatening { get; set; }

/// <summary>
/// Confidence score indicating "self-harm" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("self-harm")]
public double SelfHarm { get; set; }

/// <summary>
/// Confidence score indicating "sexual" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("sexual")]
public double Sexual { get; set; }

/// <summary>
/// Confidence score indicating "sexual/minors" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("sexual/minors")]
public double SexualMinors { get; set; }

/// <summary>
/// Confidence score indicating "violence" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("violence")]
public double Violence { get; set; }

/// <summary>
/// Confidence score indicating "violence/graphic" content is detected in the text
/// A value between 0 and 1, where 0 indicates low confidence
/// </summary>
[JsonProperty("violence/graphic")]
public double ViolenceGraphic { get; set; }
}
}
Loading