1use serde::{Deserialize, Serialize};
23
24use crate::client::Client;
25use crate::error::Result;
26
27#[derive(Debug, Clone, Serialize, Default)]
29pub struct BatchJob {
30 pub model: String,
32
33 pub prompt: String,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub title: Option<String>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
42 pub system_prompt: Option<String>,
43
44 #[serde(skip_serializing_if = "Option::is_none")]
46 pub max_tokens: Option<i64>,
47}
48
49#[derive(Debug, Clone, Deserialize)]
51pub struct BatchSubmitResponse {
52 pub job_ids: Vec<String>,
54
55 #[serde(default)]
57 pub status: String,
58}
59
60#[derive(Debug, Clone, Deserialize)]
62pub struct BatchJsonlResponse {
63 pub job_ids: Vec<String>,
65}
66
67#[derive(Debug, Clone, Deserialize)]
69pub struct BatchJobInfo {
70 pub job_id: String,
72
73 pub status: String,
75
76 #[serde(default)]
78 pub model: Option<String>,
79
80 #[serde(default)]
82 pub title: Option<String>,
83
84 #[serde(default)]
86 pub created_at: Option<String>,
87
88 #[serde(default)]
90 pub completed_at: Option<String>,
91
92 #[serde(default)]
94 pub result: Option<serde_json::Value>,
95
96 #[serde(default)]
98 pub error: Option<String>,
99
100 #[serde(default)]
102 pub cost_ticks: i64,
103}
104
105#[derive(Debug, Clone, Deserialize)]
107pub struct BatchJobsResponse {
108 #[serde(default)]
110 pub jobs: Vec<BatchJobInfo>,
111}
112
113pub type BatchJobInput = BatchJob;
115
116#[derive(Debug, Clone, Serialize, Default)]
118pub struct BatchSubmitRequest {
119 pub jobs: Vec<BatchJob>,
121}
122
123impl Client {
124 pub async fn batch_submit(&self, jobs: &[BatchJob]) -> Result<BatchSubmitResponse> {
128 let body = serde_json::json!({ "jobs": jobs });
129 let (resp, _meta) = self
130 .post_json::<serde_json::Value, BatchSubmitResponse>("/qai/v1/batch", &body)
131 .await?;
132 Ok(resp)
133 }
134
135 pub async fn batch_submit_jsonl(&self, jsonl: &str) -> Result<BatchJsonlResponse> {
139 let body = serde_json::json!({ "jsonl": jsonl });
140 let (resp, _meta) = self
141 .post_json::<serde_json::Value, BatchJsonlResponse>("/qai/v1/batch/jsonl", &body)
142 .await?;
143 Ok(resp)
144 }
145
146 pub async fn batch_jobs(&self) -> Result<BatchJobsResponse> {
148 let (resp, _meta) = self
149 .get_json::<BatchJobsResponse>("/qai/v1/batch/jobs")
150 .await?;
151 Ok(resp)
152 }
153
154 pub async fn batch_job(&self, id: &str) -> Result<BatchJobInfo> {
156 let path = format!("/qai/v1/batch/jobs/{id}");
157 let (resp, _meta) = self.get_json::<BatchJobInfo>(&path).await?;
158 Ok(resp)
159 }
160}