openai_rust_sdk/models/moderations/
scores.rs1use super::constants::SCORE_MAPPINGS;
4use crate::{De, Ser};
5
6#[derive(Debug, Clone, Ser, De)]
8pub struct CategoryScores {
9 pub hate: f64,
11
12 #[serde(rename = "hate/threatening")]
14 pub hate_threatening: f64,
15
16 #[serde(rename = "self-harm")]
18 pub self_harm: f64,
19
20 #[serde(rename = "self-harm/intent")]
22 pub self_harm_intent: f64,
23
24 #[serde(rename = "self-harm/instructions")]
26 pub self_harm_instructions: f64,
27
28 pub sexual: f64,
30
31 #[serde(rename = "sexual/minors")]
33 pub sexual_minors: f64,
34
35 pub violence: f64,
37
38 #[serde(rename = "violence/graphic")]
40 pub violence_graphic: f64,
41
42 pub harassment: f64,
44
45 #[serde(rename = "harassment/threatening")]
47 pub harassment_threatening: f64,
48}
49
50impl CategoryScores {
51 #[must_use]
53 pub fn max_score(&self) -> f64 {
54 SCORE_MAPPINGS
55 .iter()
56 .map(|(_, getter)| getter(self))
57 .fold(0.0, f64::max)
58 }
59
60 #[must_use]
62 pub fn scores_above_threshold(&self, threshold: f64) -> Vec<(String, f64)> {
63 self.get_category_scores()
64 .into_iter()
65 .filter(|(_, score)| *score >= threshold)
66 .map(|(name, score)| (name.to_string(), score))
67 .collect()
68 }
69
70 fn get_category_scores(&self) -> Vec<(&'static str, f64)> {
72 SCORE_MAPPINGS
73 .iter()
74 .map(|&(name, getter)| (name, getter(self)))
75 .collect()
76 }
77}