Skip to main content

openai_types/moderation/
manual.rs

1// Hand-crafted moderation types for openai-types.
2// These types mirror the original src/types/moderation.rs but are standalone (no crate:: imports).
3
4use serde::{Deserialize, Serialize};
5
6// -- Request types --
7
8/// Input for moderations: a single string or array of strings.
9#[derive(Debug, Clone, Serialize)]
10#[serde(untagged)]
11#[non_exhaustive]
12pub enum ModerationInput {
13    String(String),
14    StringArray(Vec<String>),
15}
16
17impl From<&str> for ModerationInput {
18    fn from(s: &str) -> Self {
19        ModerationInput::String(s.to_string())
20    }
21}
22
23impl From<String> for ModerationInput {
24    fn from(s: String) -> Self {
25        ModerationInput::String(s)
26    }
27}
28
29impl From<Vec<String>> for ModerationInput {
30    fn from(v: Vec<String>) -> Self {
31        ModerationInput::StringArray(v)
32    }
33}
34
35/// Request body for `POST /moderations`.
36#[derive(Debug, Clone, Serialize)]
37pub struct ModerationRequest {
38    /// Input text to classify.
39    pub input: ModerationInput,
40
41    /// Model to use (e.g. "omni-moderation-latest").
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub model: Option<String>,
44}
45
46impl ModerationRequest {
47    pub fn new(input: impl Into<ModerationInput>) -> Self {
48        Self {
49            input: input.into(),
50            model: None,
51        }
52    }
53}
54
55// -- Response types --
56
57/// Category flags for moderation results.
58#[derive(Debug, Clone, Deserialize)]
59pub struct Categories {
60    pub harassment: bool,
61    #[serde(rename = "harassment/threatening")]
62    pub harassment_threatening: bool,
63    pub hate: bool,
64    #[serde(rename = "hate/threatening")]
65    pub hate_threatening: bool,
66    #[serde(default, rename = "illicit")]
67    pub illicit: Option<bool>,
68    #[serde(default, rename = "illicit/violent")]
69    pub illicit_violent: Option<bool>,
70    #[serde(rename = "self-harm")]
71    pub self_harm: bool,
72    #[serde(rename = "self-harm/instructions")]
73    pub self_harm_instructions: bool,
74    #[serde(rename = "self-harm/intent")]
75    pub self_harm_intent: bool,
76    pub sexual: bool,
77    #[serde(rename = "sexual/minors")]
78    pub sexual_minors: bool,
79    pub violence: bool,
80    #[serde(rename = "violence/graphic")]
81    pub violence_graphic: bool,
82}
83
84/// Category scores for moderation results.
85#[derive(Debug, Clone, Deserialize)]
86pub struct CategoryScores {
87    pub harassment: f64,
88    #[serde(rename = "harassment/threatening")]
89    pub harassment_threatening: f64,
90    pub hate: f64,
91    #[serde(rename = "hate/threatening")]
92    pub hate_threatening: f64,
93    #[serde(default, rename = "illicit")]
94    pub illicit: Option<f64>,
95    #[serde(default, rename = "illicit/violent")]
96    pub illicit_violent: Option<f64>,
97    #[serde(rename = "self-harm")]
98    pub self_harm: f64,
99    #[serde(rename = "self-harm/instructions")]
100    pub self_harm_instructions: f64,
101    #[serde(rename = "self-harm/intent")]
102    pub self_harm_intent: f64,
103    pub sexual: f64,
104    #[serde(rename = "sexual/minors")]
105    pub sexual_minors: f64,
106    pub violence: f64,
107    #[serde(rename = "violence/graphic")]
108    pub violence_graphic: f64,
109}
110
111/// A single moderation result.
112#[derive(Debug, Clone, Deserialize)]
113pub struct Moderation {
114    pub flagged: bool,
115    pub categories: Categories,
116    pub category_scores: CategoryScores,
117}
118
119/// Response from `POST /moderations`.
120#[derive(Debug, Clone, Deserialize)]
121pub struct ModerationCreateResponse {
122    pub id: String,
123    pub model: String,
124    pub results: Vec<Moderation>,
125}