async_openai_alt/
batches.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{Batch, BatchRequest, ListBatchesResponse},
7    Client,
8};
9
10/// Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.
11///
12/// Related guide: [Batch](https://platform.openai.com/docs/guides/batch)
13pub struct Batches<'c, C: Config> {
14    client: &'c Client<C>,
15}
16
17impl<'c, C: Config> Batches<'c, C> {
18    pub fn new(client: &'c Client<C>) -> Self {
19        Self { client }
20    }
21
22    /// Creates and executes a batch from an uploaded file of requests
23    pub async fn create(&self, request: BatchRequest) -> Result<Batch, OpenAIError> {
24        self.client.post("/batches", request).await
25    }
26
27    /// List your organization's batches.
28    pub async fn list<Q>(&self, query: &Q) -> Result<ListBatchesResponse, OpenAIError>
29    where
30        Q: Serialize + ?Sized,
31    {
32        self.client.get_with_query("/batches", query).await
33    }
34
35    /// Retrieves a batch.
36    pub async fn retrieve(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
37        self.client.get(&format!("/batches/{batch_id}")).await
38    }
39
40    /// 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.
41    pub async fn cancel(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
42        self.client
43            .post(
44                &format!("/batches/{batch_id}/cancel"),
45                serde_json::json!({}),
46            )
47            .await
48    }
49}