Skip to main content

liter_llm/types/
moderation.rs

1use serde::{Deserialize, Serialize};
2
3/// Request to classify content for policy violations.
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5#[serde(deny_unknown_fields)]
6pub struct ModerationRequest {
7    pub input: ModerationInput,
8    #[serde(default, skip_serializing_if = "Option::is_none")]
9    pub model: Option<String>,
10}
11
12/// Input to the moderation endpoint — a single string or multiple strings.
13#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
14#[serde(untagged)]
15pub enum ModerationInput {
16    Single(String),
17    Multiple(Vec<String>),
18}
19
20impl Default for ModerationInput {
21    fn default() -> Self {
22        Self::Single(String::new())
23    }
24}
25
26/// Response from the moderation endpoint.
27#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct ModerationResponse {
29    pub id: String,
30    pub model: String,
31    pub results: Vec<ModerationResult>,
32}
33
34/// A single moderation classification result.
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct ModerationResult {
37    pub flagged: bool,
38    pub categories: ModerationCategories,
39    pub category_scores: ModerationCategoryScores,
40}
41
42/// Boolean flags for each moderation category.
43#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
44pub struct ModerationCategories {
45    pub sexual: bool,
46    pub hate: bool,
47    pub harassment: bool,
48    #[serde(rename = "self-harm")]
49    pub self_harm: bool,
50    #[serde(rename = "sexual/minors")]
51    pub sexual_minors: bool,
52    #[serde(rename = "hate/threatening")]
53    pub hate_threatening: bool,
54    #[serde(rename = "violence/graphic")]
55    pub violence_graphic: bool,
56    #[serde(rename = "self-harm/intent")]
57    pub self_harm_intent: bool,
58    #[serde(rename = "self-harm/instructions")]
59    pub self_harm_instructions: bool,
60    #[serde(rename = "harassment/threatening")]
61    pub harassment_threatening: bool,
62    pub violence: bool,
63}
64
65/// Confidence scores for each moderation category.
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
67pub struct ModerationCategoryScores {
68    pub sexual: f64,
69    pub hate: f64,
70    pub harassment: f64,
71    #[serde(rename = "self-harm")]
72    pub self_harm: f64,
73    #[serde(rename = "sexual/minors")]
74    pub sexual_minors: f64,
75    #[serde(rename = "hate/threatening")]
76    pub hate_threatening: f64,
77    #[serde(rename = "violence/graphic")]
78    pub violence_graphic: f64,
79    #[serde(rename = "self-harm/intent")]
80    pub self_harm_intent: f64,
81    #[serde(rename = "self-harm/instructions")]
82    pub self_harm_instructions: f64,
83    #[serde(rename = "harassment/threatening")]
84    pub harassment_threatening: f64,
85    pub violence: f64,
86}