async_openai/
moderation.rs

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