openai_rust_sdk/models/moderations/
scores.rs

1//! Confidence scores for each category
2
3use super::constants::SCORE_MAPPINGS;
4use crate::{De, Ser};
5
6/// Confidence scores for each category (0.0 to 1.0)
7#[derive(Debug, Clone, Ser, De)]
8pub struct CategoryScores {
9    /// Hate confidence score
10    pub hate: f64,
11
12    /// Hate/threatening confidence score
13    #[serde(rename = "hate/threatening")]
14    pub hate_threatening: f64,
15
16    /// Self-harm confidence score
17    #[serde(rename = "self-harm")]
18    pub self_harm: f64,
19
20    /// Self-harm/intent confidence score
21    #[serde(rename = "self-harm/intent")]
22    pub self_harm_intent: f64,
23
24    /// Self-harm/instructions confidence score
25    #[serde(rename = "self-harm/instructions")]
26    pub self_harm_instructions: f64,
27
28    /// Sexual confidence score
29    pub sexual: f64,
30
31    /// Sexual/minors confidence score
32    #[serde(rename = "sexual/minors")]
33    pub sexual_minors: f64,
34
35    /// Violence confidence score
36    pub violence: f64,
37
38    /// Violence/graphic confidence score
39    #[serde(rename = "violence/graphic")]
40    pub violence_graphic: f64,
41
42    /// Harassment confidence score
43    pub harassment: f64,
44
45    /// Harassment/threatening confidence score
46    #[serde(rename = "harassment/threatening")]
47    pub harassment_threatening: f64,
48}
49
50impl CategoryScores {
51    /// Get the maximum score across all categories
52    #[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    /// Get scores above a certain threshold
61    #[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    /// Helper method to get all category scores with their names
71    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}