openai_oxide/resources/
moderations.rs1use crate::client::OpenAI;
4use crate::error::OpenAIError;
5use crate::types::moderation::{ModerationCreateResponse, ModerationRequest};
6
7pub struct Moderations<'a> {
12 client: &'a OpenAI,
13}
14
15impl<'a> Moderations<'a> {
16 pub(crate) fn new(client: &'a OpenAI) -> Self {
17 Self { client }
18 }
19
20 pub async fn create(
24 &self,
25 request: ModerationRequest,
26 ) -> Result<ModerationCreateResponse, OpenAIError> {
27 self.client.post("/moderations", &request).await
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use crate::OpenAI;
34 use crate::config::ClientConfig;
35 use crate::types::moderation::ModerationRequest;
36
37 #[tokio::test]
38 async fn test_moderations_create() {
39 let mut server = mockito::Server::new_async().await;
40 let mock = server
41 .mock("POST", "/moderations")
42 .match_header("authorization", "Bearer sk-test")
43 .with_status(200)
44 .with_header("content-type", "application/json")
45 .with_body(
46 r#"{
47 "id": "modr-abc123",
48 "model": "text-moderation-007",
49 "results": [{
50 "flagged": false,
51 "categories": {
52 "harassment": false,
53 "harassment/threatening": false,
54 "hate": false,
55 "hate/threatening": false,
56 "self-harm": false,
57 "self-harm/instructions": false,
58 "self-harm/intent": false,
59 "sexual": false,
60 "sexual/minors": false,
61 "violence": false,
62 "violence/graphic": false
63 },
64 "category_scores": {
65 "harassment": 0.001,
66 "harassment/threatening": 0.0001,
67 "hate": 0.0001,
68 "hate/threatening": 0.0001,
69 "self-harm": 0.0001,
70 "self-harm/instructions": 0.0001,
71 "self-harm/intent": 0.0001,
72 "sexual": 0.0001,
73 "sexual/minors": 0.0001,
74 "violence": 0.0001,
75 "violence/graphic": 0.0001
76 }
77 }]
78 }"#,
79 )
80 .create_async()
81 .await;
82
83 let client = OpenAI::with_config(ClientConfig::new("sk-test").base_url(server.url()));
84 let request = ModerationRequest::new("Hello, how are you?");
85
86 let response = client.moderations().create(request).await.unwrap();
87 assert_eq!(response.id, "modr-abc123");
88 assert!(!response.results[0].flagged);
89 mock.assert_async().await;
90 }
91}