Skip to main content

outfox_openai/spec/moderations/
moderation.rs

1use derive_builder::Builder;
2use serde::{Deserialize, Serialize};
3
4use crate::error::OpenAIError;
5
6#[derive(Debug, Serialize, Clone, PartialEq, Deserialize)]
7#[serde(untagged)]
8pub enum ModerationInput {
9    /// A single string of text to classify for moderation
10    String(String),
11
12    /// An array of strings to classify for moderation
13    StringArray(Vec<String>),
14
15    /// An array of multi-modal inputs to the moderation model
16    MultiModal(Vec<ModerationContentPart>),
17}
18
19#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
20pub struct ModerationTextInput {
21    /// A string of text to classify
22    pub text: String,
23}
24
25#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
26pub struct ModerationImageURLInput {
27    /// Either a URL of the image or the base64 encoded image data.
28    pub image_url: String,
29}
30
31/// Content part for multi-modal moderation input
32#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
33#[serde(tag = "type")]
34pub enum ModerationContentPart {
35    /// An object describing text to classify
36    #[serde(rename = "text")]
37    Text(ModerationTextInput),
38
39    /// An object describing an image to classify
40    #[serde(rename = "image_url")]
41    ImageUrl(ModerationImageURLInput),
42}
43
44#[derive(Debug, Default, Clone, Serialize, Builder, PartialEq, Deserialize)]
45#[builder(name = "CreateModerationRequestArgs")]
46#[builder(pattern = "mutable")]
47#[builder(setter(into, strip_option), default)]
48#[builder(derive(Debug))]
49#[builder(build_fn(error = "OpenAIError"))]
50pub struct CreateModerationRequest {
51    /// Input (or inputs) to classify. Can be a single string, an array of strings, or
52    /// an array of multi-modal input objects similar to other models.
53    pub input: ModerationInput,
54
55    /// The content moderation model you would like to use. Learn more in
56    /// [the moderation guide](https://platform.openai.com/docs/guides/moderation), and learn about
57    /// available models [here](https://platform.openai.com/docs/models#moderation).
58    #[serde(skip_serializing_if = "Option::is_none")]
59    pub model: Option<String>,
60}
61
62#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
63pub struct Categories {
64    /// Content that expresses, incites, or promotes hate based on race, gender,
65    /// ethnicity, religion, nationality, sexual orientation, disability status, or
66    /// caste. Hateful content aimed at non-protected groups (e.g., chess players)
67    /// is harrassment.
68    pub hate: bool,
69    #[serde(rename = "hate/threatening")]
70    /// Hateful content that also includes violence or serious harm towards the
71    /// targeted group based on race, gender, ethnicity, religion, nationality,
72    /// sexual orientation, disability status, or caste.
73    pub hate_threatening: bool,
74    /// Content that expresses, incites, or promotes harassing language towards any target.
75    pub harassment: bool,
76    /// Harassment content that also includes violence or serious harm towards any target.
77    #[serde(rename = "harassment/threatening")]
78    pub harassment_threatening: bool,
79    /// Content that includes instructions or advice that facilitate the planning or execution of
80    /// wrongdoing, or that gives advice or instruction on how to commit illicit acts. For example,
81    /// "how to shoplift" would fit this category.
82    pub illicit: bool,
83    /// Content that includes instructions or advice that facilitate the planning or execution of
84    /// wrongdoing that also includes violence, or that gives advice or instruction on the
85    /// procurement of any weapon.
86    #[serde(rename = "illicit/violent")]
87    pub illicit_violent: bool,
88    /// Content that promotes, encourages, or depicts acts of self-harm, such as suicide, cutting,
89    /// and eating disorders.
90    #[serde(rename = "self-harm")]
91    pub self_harm: bool,
92    /// Content where the speaker expresses that they are engaging or intend to engage in acts of
93    /// self-harm, such as suicide, cutting, and eating disorders.
94    #[serde(rename = "self-harm/intent")]
95    pub self_harm_intent: bool,
96    /// Content that encourages performing acts of self-harm, such as suicide, cutting, and eating
97    /// disorders, or that gives instructions or advice on how to commit such acts.
98    #[serde(rename = "self-harm/instructions")]
99    pub self_harm_instructions: bool,
100    /// Content meant to arouse sexual excitement, such as the description of sexual activity, or
101    /// that promotes sexual services (excluding sex education and wellness).
102    pub sexual: bool,
103    /// Sexual content that includes an individual who is under 18 years old.
104    #[serde(rename = "sexual/minors")]
105    pub sexual_minors: bool,
106    /// Content that depicts death, violence, or physical injury.
107    pub violence: bool,
108    /// Content that depicts death, violence, or physical injury in graphic detail.
109    #[serde(rename = "violence/graphic")]
110    pub violence_graphic: bool,
111}
112
113/// A list of the categories along with their scores as predicted by model.
114#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
115pub struct CategoryScore {
116    /// The score for the category 'hate'.
117    pub hate: f32,
118    /// The score for the category 'hate/threatening'.
119    #[serde(rename = "hate/threatening")]
120    pub hate_threatening: f32,
121    /// The score for the category 'harassment'.
122    pub harassment: f32,
123    /// The score for the category 'harassment/threatening'.
124    #[serde(rename = "harassment/threatening")]
125    pub harassment_threatening: f32,
126    /// The score for the category 'illicit'.
127    pub illicit: f32,
128    /// The score for the category 'illicit/violent'.
129    #[serde(rename = "illicit/violent")]
130    pub illicit_violent: f32,
131    /// The score for the category 'self-harm'.
132    #[serde(rename = "self-harm")]
133    pub self_harm: f32,
134    /// The score for the category 'self-harm/intent'.
135    #[serde(rename = "self-harm/intent")]
136    pub self_harm_intent: f32,
137    /// The score for the category 'self-harm/instructions'.
138    #[serde(rename = "self-harm/instructions")]
139    pub self_harm_instructions: f32,
140    /// The score for the category 'sexual'.
141    pub sexual: f32,
142    /// The score for the category 'sexual/minors'.
143    #[serde(rename = "sexual/minors")]
144    pub sexual_minors: f32,
145    /// The score for the category 'violence'.
146    pub violence: f32,
147    /// The score for the category 'violence/graphic'.
148    #[serde(rename = "violence/graphic")]
149    pub violence_graphic: f32,
150}
151
152#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
153pub struct ContentModerationResult {
154    /// Whether any of the below categories are flagged.
155    pub flagged: bool,
156    /// A list of the categories, and whether they are flagged or not.
157    pub categories: Categories,
158    /// A list of the categories along with their scores as predicted by model.
159    pub category_scores: CategoryScore,
160    /// A list of the categories along with the input type(s) that the score applies to.
161    pub category_applied_input_types: CategoryAppliedInputTypes,
162}
163
164/// Represents if a given text input is potentially harmful.
165#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
166pub struct CreateModerationResponse {
167    /// The unique identifier for the moderation request.
168    pub id: String,
169    /// The model used to generate the moderation results.
170    pub model: String,
171    /// A list of moderation objects.
172    pub results: Vec<ContentModerationResult>,
173}
174
175/// A list of the categories along with the input type(s) that the score applies to.
176#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
177pub struct CategoryAppliedInputTypes {
178    /// The applied input type(s) for the category 'hate'.
179    pub hate: Vec<ModInputType>,
180
181    /// The applied input type(s) for the category 'hate/threatening'.
182    #[serde(rename = "hate/threatening")]
183    pub hate_threatening: Vec<ModInputType>,
184
185    /// The applied input type(s) for the category 'harassment'.
186    pub harassment: Vec<ModInputType>,
187
188    /// The applied input type(s) for the category 'harassment/threatening'.
189    #[serde(rename = "harassment/threatening")]
190    pub harassment_threatening: Vec<ModInputType>,
191
192    /// The applied input type(s) for the category 'illicit'.
193    pub illicit: Vec<ModInputType>,
194
195    /// The applied input type(s) for the category 'illicit/violent'.
196    #[serde(rename = "illicit/violent")]
197    pub illicit_violent: Vec<ModInputType>,
198
199    /// The applied input type(s) for the category 'self-harm'.
200    #[serde(rename = "self-harm")]
201    pub self_harm: Vec<ModInputType>,
202
203    /// The applied input type(s) for the category 'self-harm/intent'.
204    #[serde(rename = "self-harm/intent")]
205    pub self_harm_intent: Vec<ModInputType>,
206
207    /// The applied input type(s) for the category 'self-harm/instructions'.
208    #[serde(rename = "self-harm/instructions")]
209    pub self_harm_instructions: Vec<ModInputType>,
210
211    /// The applied input type(s) for the category 'sexual'.
212    pub sexual: Vec<ModInputType>,
213
214    /// The applied input type(s) for the category 'sexual/minors'.
215    #[serde(rename = "sexual/minors")]
216    pub sexual_minors: Vec<ModInputType>,
217
218    /// The applied input type(s) for the category 'violence'.
219    pub violence: Vec<ModInputType>,
220
221    /// The applied input type(s) for the category 'violence/graphic'.
222    #[serde(rename = "violence/graphic")]
223    pub violence_graphic: Vec<ModInputType>,
224}
225
226/// The type of input that was moderated
227#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
228#[serde(rename_all = "lowercase")]
229pub enum ModInputType {
230    /// Text content that was moderated
231    Text,
232    /// Image content that was moderated
233    Image,
234}