async_openai/
batches.rs

1use crate::{
2    config::Config,
3    error::OpenAIError,
4    types::batches::{Batch, BatchRequest, ListBatchesResponse},
5    Client, RequestOptions,
6};
7
8/// Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.
9///
10/// Related guide: [Batch](https://platform.openai.com/docs/guides/batch)
11pub struct Batches<'c, C: Config> {
12    client: &'c Client<C>,
13    pub(crate) request_options: RequestOptions,
14}
15
16impl<'c, C: Config> Batches<'c, C> {
17    pub fn new(client: &'c Client<C>) -> Self {
18        Self {
19            client,
20            request_options: RequestOptions::new(),
21        }
22    }
23
24    /// Creates and executes a batch from an uploaded file of requests
25    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
26    pub async fn create(&self, request: BatchRequest) -> Result<Batch, OpenAIError> {
27        self.client
28            .post("/batches", request, &self.request_options)
29            .await
30    }
31
32    /// List your organization's batches.
33    #[crate::byot(R = serde::de::DeserializeOwned)]
34    pub async fn list(&self) -> Result<ListBatchesResponse, OpenAIError> {
35        self.client.get("/batches", &self.request_options).await
36    }
37
38    /// Retrieves a batch.
39    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
40    pub async fn retrieve(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
41        self.client
42            .get(&format!("/batches/{batch_id}"), &self.request_options)
43            .await
44    }
45
46    /// Cancels an in-progress batch. The batch will be in status `cancelling` for up to 10 minutes, before changing to `cancelled`, where it will have partial results (if any) available in the output file.
47    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
48    pub async fn cancel(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
49        self.client
50            .post(
51                &format!("/batches/{batch_id}/cancel"),
52                serde_json::json!({}),
53                &self.request_options,
54            )
55            .await
56    }
57}