use std::sync::Arc;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use validator::Validate;
use crate::{
ZaiResult,
client::{
endpoints::{ApiBase, EndpointConfig, paths},
http::{HttpClient, HttpClientConfig, parse_typed_response},
},
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum BatchEndpoint {
#[serde(rename = "/v4/chat/completions")]
ChatCompletions,
}
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct CreateBatchBody {
#[validate(length(min = 1))]
pub input_file_id: String,
pub endpoint: BatchEndpoint,
#[serde(skip_serializing_if = "Option::is_none")]
pub auto_delete_input_file: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Value>,
}
impl CreateBatchBody {
pub fn new(input_file_id: impl Into<String>, endpoint: BatchEndpoint) -> Self {
Self {
input_file_id: input_file_id.into(),
endpoint,
auto_delete_input_file: Some(true),
metadata: None,
}
}
pub fn with_auto_delete_input_file(mut self, v: bool) -> Self {
self.auto_delete_input_file = Some(v);
self
}
pub fn with_metadata(mut self, v: Value) -> Self {
self.metadata = Some(v);
self
}
}
pub struct CreateBatchRequest {
pub key: String,
url: String,
endpoint_config: EndpointConfig,
api_base: ApiBase,
http_config: Arc<HttpClientConfig>,
pub body: CreateBatchBody,
}
impl CreateBatchRequest {
pub fn new(key: String, input_file_id: impl Into<String>, endpoint: BatchEndpoint) -> Self {
let body = CreateBatchBody::new(input_file_id, endpoint);
let endpoint_config = EndpointConfig::default();
let api_base = ApiBase::PaasV4;
let url = endpoint_config.url(&api_base, paths::BATCHES);
Self {
key,
url,
endpoint_config,
api_base,
http_config: Arc::new(HttpClientConfig::default()),
body,
}
}
fn rebuild_url(&mut self) {
self.url = self.endpoint_config.url(&self.api_base, paths::BATCHES);
}
pub fn with_base_url(mut self, base: impl Into<String>) -> Self {
self.api_base = ApiBase::Custom(base.into());
self.rebuild_url();
self
}
pub fn with_endpoint_config(mut self, endpoint_config: EndpointConfig) -> Self {
self.endpoint_config = endpoint_config;
self.rebuild_url();
self
}
pub fn with_http_config(mut self, config: HttpClientConfig) -> Self {
self.http_config = Arc::new(config);
self
}
pub fn with_auto_delete_input_file(mut self, v: bool) -> Self {
self.body = self.body.with_auto_delete_input_file(v);
self
}
pub fn with_metadata(mut self, v: serde_json::Value) -> Self {
self.body = self.body.with_metadata(v);
self
}
pub fn validate(&self) -> ZaiResult<()> {
self.body.validate().map_err(|e| e.into())
}
pub async fn send(&self) -> ZaiResult<CreateBatchResponse> {
self.validate()?;
let resp: reqwest::Response = self.post().await?;
let parsed = parse_typed_response::<CreateBatchResponse>(resp).await?;
Ok(parsed)
}
}
impl HttpClient for CreateBatchRequest {
type Body = CreateBatchBody;
type ApiUrl = String;
type ApiKey = String;
fn api_url(&self) -> &Self::ApiUrl {
&self.url
}
fn api_key(&self) -> &Self::ApiKey {
&self.key
}
fn body(&self) -> &Self::Body {
&self.body
}
fn http_config(&self) -> Arc<HttpClientConfig> {
Arc::clone(&self.http_config)
}
}
pub type CreateBatchResponse = super::types::BatchItem;