portkey_sdk/service/
moderations.rs

1use std::future::Future;
2
3use crate::model::{CreateModerationRequest, ModerationResponse};
4use crate::{PortkeyClient, Result};
5
6/// Service for moderating content.
7///
8/// # Example
9///
10/// ```no_run
11/// use portkey_sdk::{PortkeyConfig, PortkeyClient, Result};
12/// use portkey_sdk::service::ModerationsService;
13/// use portkey_sdk::model::CreateModerationRequest;
14///
15/// # async fn example() -> Result<()> {
16/// let config = PortkeyConfig::builder()
17///     .with_api_key("your-api-key")
18///     .build()?;
19/// let client = PortkeyClient::new(config)?;
20///
21///     let response = client.create_moderation(
22///         CreateModerationRequest {
23///             input: portkey_sdk::model::ModerationInput::String("I want to hurt someone".to_string()),
24///             model: None,
25///         }
26///     ).await?;
27///
28///     for result in response.results {
29///         if result.flagged {
30///             println!("Content was flagged!");
31///         }
32///     }
33///     Ok(())
34/// # }
35/// ```
36pub trait ModerationsService {
37    /// Classifies if text is potentially harmful.
38    ///
39    /// # Example
40    ///
41    /// ```no_run
42    /// # use portkey_sdk::model::{CreateModerationRequest, ModerationInput};
43    /// # use portkey_sdk::service::ModerationsService;
44    /// # async fn example(client: &impl ModerationsService) -> portkey_sdk::Result<()> {
45    /// let response = client.create_moderation(
46    ///     CreateModerationRequest {
47    ///         input: ModerationInput::String("Sample text to moderate".to_string()),
48    ///         model: Some("text-moderation-latest".to_string()),
49    ///     }
50    /// ).await?;
51    ///
52    /// for result in response.results {
53    ///     if result.flagged {
54    ///         println!("Flagged categories:");
55    ///         if result.categories.hate {
56    ///             println!("  - Hate (score: {})", result.category_scores.hate);
57    ///         }
58    ///         if result.categories.violence {
59    ///             println!("  - Violence (score: {})", result.category_scores.violence);
60    ///         }
61    ///     }
62    /// }
63    /// # Ok(())
64    /// # }
65    /// ```
66    fn create_moderation(
67        &self,
68        request: CreateModerationRequest,
69    ) -> impl Future<Output = Result<ModerationResponse>>;
70}
71
72impl ModerationsService for PortkeyClient {
73    async fn create_moderation(
74        &self,
75        request: CreateModerationRequest,
76    ) -> Result<ModerationResponse> {
77        #[cfg(feature = "tracing")]
78        tracing::debug!(
79            target: crate::TRACING_TARGET_SERVICE,
80            "Creating moderation"
81        );
82
83        let response = self
84            .send_json(reqwest::Method::POST, "/moderations", &request)
85            .await?;
86        let response = response.error_for_status()?;
87        let moderation: ModerationResponse = response.json().await?;
88
89        #[cfg(feature = "tracing")]
90        tracing::debug!(
91            target: crate::TRACING_TARGET_SERVICE,
92            "Moderation created successfully"
93        );
94
95        Ok(moderation)
96    }
97}