zai_rs/model/moderation/data.rs
1//! # Content Moderation API
2//!
3//! This module provides the content moderation client for analyzing text,
4//! image, audio, and video content for safety risks.
5
6use super::models::*;
7use crate::client::http::HttpClient;
8
9/// Content moderation client.
10///
11/// This client provides functionality to moderate content for safety risks,
12/// supporting text, image, audio, and video formats.
13///
14/// ## Examples
15///
16/// ```rust,ignore
17/// let api_key = "your-api-key".to_string();
18/// let moderation = Moderation::new_text("审核内容安全样例字符串。", api_key);
19/// let result = moderation.send().await?;
20/// ```
21pub struct Moderation {
22 /// API key for authentication
23 pub key: String,
24 /// Moderation request body
25 body: ModerationRequest,
26}
27
28impl Moderation {
29 /// Creates a new moderation request with text content.
30 ///
31 /// ## Arguments
32 ///
33 /// * `text` - The text content to moderate (max 2000 characters)
34 /// * `key` - API key for authentication
35 ///
36 /// ## Returns
37 ///
38 /// A new `Moderation` instance configured for text moderation.
39 pub fn new_text(text: impl Into<String>, key: String) -> Self {
40 let body = ModerationRequest::new_text(text);
41 Self { body, key }
42 }
43
44 /// Creates a new moderation request with multimedia content.
45 ///
46 /// ## Arguments
47 ///
48 /// * `content_type` - The type of multimedia content (image, audio, video)
49 /// * `url` - URL to the multimedia content
50 /// * `key` - API key for authentication
51 ///
52 /// ## Returns
53 ///
54 /// A new `Moderation` instance configured for multimedia moderation.
55 pub fn new_multimedia(content_type: MediaType, url: impl Into<String>, key: String) -> Self {
56 let body = ModerationRequest::new_multimedia(content_type, url);
57 Self { body, key }
58 }
59
60 /// Gets mutable access to the request body for further customization.
61 pub fn body_mut(&mut self) -> &mut ModerationRequest {
62 &mut self.body
63 }
64
65 pub fn validate(&self) -> crate::ZaiResult<()> {
66 self.body
67 .validate()
68 .map_err(crate::client::error::ZaiError::from)?;
69 Ok(())
70 }
71
72 /// Sends the moderation request and returns the structured response.
73 ///
74 /// This method automatically validates the request before sending.
75 ///
76 /// ## Returns
77 ///
78 /// A `ModerationResponse` containing the moderation results and usage
79 /// statistics.
80 pub async fn send(&self) -> crate::ZaiResult<ModerationResponse> {
81 self.validate()?;
82
83 let resp: reqwest::Response = self.post().await?;
84
85 let parsed = resp.json::<ModerationResponse>().await?;
86
87 Ok(parsed)
88 }
89}
90
91impl HttpClient for Moderation {
92 type Body = ModerationRequest;
93 type ApiUrl = &'static str;
94 type ApiKey = String;
95
96 /// Returns the Zhipu AI moderation API endpoint URL.
97 fn api_url(&self) -> &Self::ApiUrl {
98 &"https://open.bigmodel.cn/api/paas/v4/moderations"
99 }
100
101 fn api_key(&self) -> &Self::ApiKey {
102 &self.key
103 }
104
105 fn body(&self) -> &Self::Body {
106 &self.body
107 }
108}