1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use validator::Validate;
4
5use crate::client::http::HttpClient;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum BatchEndpoint {
10 #[serde(rename = "/v4/chat/completions")]
12 ChatCompletions,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
17pub struct CreateBatchBody {
18 #[validate(length(min = 1))]
20 pub input_file_id: String,
21
22 pub endpoint: BatchEndpoint,
24
25 #[serde(skip_serializing_if = "Option::is_none")]
27 pub auto_delete_input_file: Option<bool>,
28
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub metadata: Option<Value>,
32}
33
34impl CreateBatchBody {
35 pub fn new(input_file_id: impl Into<String>, endpoint: BatchEndpoint) -> Self {
36 Self {
37 input_file_id: input_file_id.into(),
38 endpoint,
39 auto_delete_input_file: Some(true),
40 metadata: None,
41 }
42 }
43
44 pub fn with_auto_delete_input_file(mut self, v: bool) -> Self {
46 self.auto_delete_input_file = Some(v);
47 self
48 }
49
50 pub fn with_metadata(mut self, v: Value) -> Self {
52 self.metadata = Some(v);
53 self
54 }
55}
56
57pub struct CreateBatchRequest {
59 pub key: String,
60 pub body: CreateBatchBody,
61}
62
63impl CreateBatchRequest {
64 pub fn new(key: String, input_file_id: impl Into<String>, endpoint: BatchEndpoint) -> Self {
66 let body = CreateBatchBody::new(input_file_id, endpoint);
67 Self { key, body }
68 }
69
70 pub fn with_auto_delete_input_file(mut self, v: bool) -> Self {
72 self.body = self.body.with_auto_delete_input_file(v);
73 self
74 }
75
76 pub fn with_metadata(mut self, v: serde_json::Value) -> Self {
78 self.body = self.body.with_metadata(v);
79 self
80 }
81
82 pub fn validate(&self) -> anyhow::Result<()> {
84 self.body.validate().map_err(|e| anyhow::anyhow!(e))
85 }
86
87 pub async fn send(&self) -> anyhow::Result<CreateBatchResponse> {
89 self.validate()?;
90 let resp: reqwest::Response = self.post().await?;
91 let parsed = resp.json::<CreateBatchResponse>().await?;
92 Ok(parsed)
93 }
94}
95
96impl HttpClient for CreateBatchRequest {
97 type Body = CreateBatchBody;
98 type ApiUrl = &'static str;
99 type ApiKey = String;
100
101 fn api_url(&self) -> &Self::ApiUrl {
102 &"https://open.bigmodel.cn/api/paas/v4/batches"
103 }
104 fn api_key(&self) -> &Self::ApiKey {
105 &self.key
106 }
107 fn body(&self) -> &Self::Body {
108 &self.body
109 }
110}
111
112pub type CreateBatchResponse = super::types::BatchItem;