openai_req/moderations/
mod.rs

1
2use crate::{Input, JsonRequest};
3use serde::*;
4
5/// Obtains moderation info for provided text.
6/// More details at https://platform.openai.com/docs/api-reference/moderations
7/// # Usage example
8/// ```
9/// use openai_req::JsonRequest;
10/// use openai_req::moderations::ModerationRequest;
11/// 
12/// let req = ModerationRequest::new("I want to kill everyone".into());
13/// let res = req.run(&client).await?;
14/// ```
15#[derive(Serialize, Deserialize, Clone, Debug)]
16pub struct ModerationRequest{
17    pub input:Input,
18    pub model:ModerationModel
19}
20
21impl JsonRequest<ModerationResponse> for ModerationRequest{
22    const ENDPOINT: &'static str = "/moderations";
23}
24
25impl ModerationRequest{
26    pub fn new(input:Input) -> Self {
27        ModerationRequest{
28            input,
29            model:ModerationModel::TextModerationStable
30        }
31    }
32
33    pub fn with_model(model:ModerationModel,input:Input) -> Self {
34        ModerationRequest{
35            input,
36            model
37        }
38    }
39}
40
41#[derive(Serialize, Deserialize, Clone, Debug)]
42pub struct CategoryScores {
43    pub hate: f64,
44    #[serde(rename = "hate/threatening")]
45    pub hate_threatening: f64,
46    #[serde(rename = "self-harm")]
47    pub self_harm: f64,
48    pub sexual: f64,
49    #[serde(rename = "sexual/minors")]
50    pub sexual_minors: f64,
51    pub violence: f64,
52    #[serde(rename = "violence/graphic")]
53    pub violence_graphic: f64,
54}
55
56#[derive(Serialize, Deserialize, Clone, Debug)]
57pub struct Categories {
58    pub hate: bool,
59    #[serde(rename = "hate/threatening")]
60    pub hate_threatening: bool,
61    #[serde(rename = "self-harm")]
62    pub self_harm: bool,
63    pub sexual: bool,
64    #[serde(rename = "sexual/minors")]
65    pub sexual_minors: bool,
66    pub violence: bool,
67    #[serde(rename = "violence/graphic")]
68    pub violence_graphic: bool,
69}
70
71#[derive(Serialize, Deserialize, Clone, Debug)]
72pub  struct Struct {
73    pub categories: Categories,
74    pub category_scores: CategoryScores,
75    pub flagged: bool,
76}
77
78#[derive(Serialize, Deserialize, Clone, Debug)]
79pub struct ModerationResponse {
80    pub id: String,
81    pub model: String,
82    pub results: Vec<Struct>,
83}
84
85#[derive(Serialize, Deserialize, Clone, Debug)]
86pub enum ModerationModel{
87    #[serde(rename = "text-moderation-stable")]
88    TextModerationStable,
89    #[serde(rename = "text-moderation-latest")]
90    TextModerationLatest
91}