Skip to main content

outfox_openai/
moderation.rs

1use crate::config::Config;
2use crate::error::OpenAIError;
3use crate::spec::{CreateModerationRequest, CreateModerationResponse};
4use crate::{Client, RequestOptions};
5
6/// Given text and/or image inputs, classifies if those inputs are potentially harmful across
7/// several categories.
8///
9/// Related guide: [Moderations](https://platform.openai.com/docs/guides/moderation)
10pub struct Moderations<'c, C: Config> {
11    client: &'c Client<C>,
12    pub(crate) request_options: RequestOptions,
13}
14
15impl<'c, C: Config> Moderations<'c, C> {
16    pub fn new(client: &'c Client<C>) -> Self {
17        Self {
18            client,
19            request_options: RequestOptions::new(),
20        }
21    }
22
23    /// Classifies if text and/or image inputs are potentially harmful. Learn
24    /// more in the [moderation guide](https://platform.openai.com/docs/guides/moderation).
25    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
26    pub async fn create(
27        &self,
28        request: CreateModerationRequest,
29    ) -> Result<CreateModerationResponse, OpenAIError> {
30        self.client
31            .post("/moderations", request, &self.request_options)
32            .await
33    }
34}