zai_rs/batches/
create.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use validator::Validate;
4
5use crate::client::http::HttpClient;
6
7/// Endpoint for batch requests
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub enum BatchEndpoint {
10    /// Chat completions endpoint
11    #[serde(rename = "/v4/chat/completions")]
12    ChatCompletions,
13}
14
15/// Request body for creating a batch task
16#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
17pub struct CreateBatchBody {
18    /// ID of the uploaded .jsonl file (purpose must be "batch")
19    #[validate(length(min = 1))]
20    pub input_file_id: String,
21
22    /// Endpoint to be used for all requests in the batch
23    pub endpoint: BatchEndpoint,
24
25    /// Whether to auto delete input file after processing (default: true)
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub auto_delete_input_file: Option<bool>,
28
29    /// Arbitrary metadata for task management and tracking (up to 16 kv pairs)
30    #[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    /// Set auto delete flag
45    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    /// Set metadata object
51    pub fn with_metadata(mut self, v: Value) -> Self {
52        self.metadata = Some(v);
53        self
54    }
55}
56
57/// Create batch request (POST /paas/v4/batches)
58pub struct CreateBatchRequest {
59    pub key: String,
60    pub body: CreateBatchBody,
61}
62
63impl CreateBatchRequest {
64    /// Build a new create-batch request with required fields
65    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    /// Set auto-delete flag (default true)
71    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    /// Set metadata object
77    pub fn with_metadata(mut self, v: serde_json::Value) -> Self {
78        self.body = self.body.with_metadata(v);
79        self
80    }
81
82    /// Validate body using `validator`
83    pub fn validate(&self) -> anyhow::Result<()> {
84        self.body.validate().map_err(|e| anyhow::anyhow!(e))
85    }
86
87    /// Send request and parse typed response
88    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
112/// Response type for creating a batch task (same as a single item)
113pub type CreateBatchResponse = super::types::BatchItem;