use serde::{Deserialize, Serialize};
use crate::client::Client;
use crate::error::Result;
#[derive(Debug, Clone, Serialize, Default)]
pub struct BatchJob {
pub model: String,
pub prompt: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<i64>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchSubmitResponse {
pub job_ids: Vec<String>,
#[serde(default)]
pub status: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchJsonlResponse {
pub job_ids: Vec<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchJobInfo {
pub job_id: String,
pub status: String,
#[serde(default)]
pub model: Option<String>,
#[serde(default)]
pub title: Option<String>,
#[serde(default)]
pub created_at: Option<String>,
#[serde(default)]
pub completed_at: Option<String>,
#[serde(default)]
pub result: Option<serde_json::Value>,
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub cost_ticks: i64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchJobsResponse {
#[serde(default)]
pub jobs: Vec<BatchJobInfo>,
}
pub type BatchJobInput = BatchJob;
#[derive(Debug, Clone, Serialize, Default)]
pub struct BatchSubmitRequest {
pub jobs: Vec<BatchJob>,
}
impl Client {
pub async fn batch_submit(&self, jobs: &[BatchJob]) -> Result<BatchSubmitResponse> {
let body = serde_json::json!({ "jobs": jobs });
let (resp, _meta) = self
.post_json::<serde_json::Value, BatchSubmitResponse>("/qai/v1/batch", &body)
.await?;
Ok(resp)
}
pub async fn batch_submit_jsonl(&self, jsonl: &str) -> Result<BatchJsonlResponse> {
let body = serde_json::json!({ "jsonl": jsonl });
let (resp, _meta) = self
.post_json::<serde_json::Value, BatchJsonlResponse>("/qai/v1/batch/jsonl", &body)
.await?;
Ok(resp)
}
pub async fn batch_jobs(&self) -> Result<BatchJobsResponse> {
let (resp, _meta) = self
.get_json::<BatchJobsResponse>("/qai/v1/batch/jobs")
.await?;
Ok(resp)
}
pub async fn batch_job(&self, id: &str) -> Result<BatchJobInfo> {
let path = format!("/qai/v1/batch/jobs/{id}");
let (resp, _meta) = self.get_json::<BatchJobInfo>(&path).await?;
Ok(resp)
}
}