dynamo_async_openai/
batches.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11use serde::Serialize;
12
13use crate::{
14    Client,
15    config::Config,
16    error::OpenAIError,
17    types::{Batch, BatchRequest, ListBatchesResponse},
18};
19
20/// Create large batches of API requests for asynchronous processing. The Batch API returns completions within 24 hours for a 50% discount.
21///
22/// Related guide: [Batch](https://platform.openai.com/docs/guides/batch)
23pub struct Batches<'c, C: Config> {
24    client: &'c Client<C>,
25}
26
27impl<'c, C: Config> Batches<'c, C> {
28    pub fn new(client: &'c Client<C>) -> Self {
29        Self { client }
30    }
31
32    /// Creates and executes a batch from an uploaded file of requests
33    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
34    pub async fn create(&self, request: BatchRequest) -> Result<Batch, OpenAIError> {
35        self.client.post("/batches", request).await
36    }
37
38    /// List your organization's batches.
39    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
40    pub async fn list<Q>(&self, query: &Q) -> Result<ListBatchesResponse, OpenAIError>
41    where
42        Q: Serialize + ?Sized,
43    {
44        self.client.get_with_query("/batches", &query).await
45    }
46
47    /// Retrieves a batch.
48    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
49    pub async fn retrieve(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
50        self.client.get(&format!("/batches/{batch_id}")).await
51    }
52
53    /// 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.
54    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
55    pub async fn cancel(&self, batch_id: &str) -> Result<Batch, OpenAIError> {
56        self.client
57            .post(
58                &format!("/batches/{batch_id}/cancel"),
59                serde_json::json!({}),
60            )
61            .await
62    }
63}