async_openai_wasm/
vector_store_file_batches.rs

1use serde::Serialize;
2
3use crate::{
4    Client,
5    config::Config,
6    error::OpenAIError,
7    types::{
8        CreateVectorStoreFileBatchRequest, ListVectorStoreFilesResponse, VectorStoreFileBatchObject,
9    },
10};
11
12/// Vector store file batches represent operations to add multiple files to a vector store.
13///
14/// Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search)
15pub struct VectorStoreFileBatches<'c, C: Config> {
16    client: &'c Client<C>,
17    pub vector_store_id: String,
18}
19
20impl<'c, C: Config> VectorStoreFileBatches<'c, C> {
21    pub fn new(client: &'c Client<C>, vector_store_id: &str) -> Self {
22        Self {
23            client,
24            vector_store_id: vector_store_id.into(),
25        }
26    }
27
28    /// Create vector store file batch
29    #[crate::byot(T0 = serde::Serialize, R = serde::de::DeserializeOwned)]
30    pub async fn create(
31        &self,
32        request: CreateVectorStoreFileBatchRequest,
33    ) -> Result<VectorStoreFileBatchObject, OpenAIError> {
34        self.client
35            .post(
36                &format!("/vector_stores/{}/file_batches", &self.vector_store_id),
37                request,
38            )
39            .await
40    }
41
42    /// Retrieves a vector store file batch.
43    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
44    pub async fn retrieve(
45        &self,
46        batch_id: &str,
47    ) -> Result<VectorStoreFileBatchObject, OpenAIError> {
48        self.client
49            .get(&format!(
50                "/vector_stores/{}/file_batches/{batch_id}",
51                &self.vector_store_id
52            ))
53            .await
54    }
55
56    /// Cancel a vector store file batch. This attempts to cancel the processing of files in this batch as soon as possible.
57    #[crate::byot(T0 = std::fmt::Display, R = serde::de::DeserializeOwned)]
58    pub async fn cancel(&self, batch_id: &str) -> Result<VectorStoreFileBatchObject, OpenAIError> {
59        self.client
60            .post(
61                &format!(
62                    "/vector_stores/{}/file_batches/{batch_id}/cancel",
63                    &self.vector_store_id
64                ),
65                serde_json::json!({}),
66            )
67            .await
68    }
69
70    /// Returns a list of vector store files in a batch.
71    #[crate::byot(T0 = std::fmt::Display, T1 = serde::Serialize, R = serde::de::DeserializeOwned)]
72    pub async fn list<Q>(
73        &self,
74        batch_id: &str,
75        query: &Q,
76    ) -> Result<ListVectorStoreFilesResponse, OpenAIError>
77    where
78        Q: Serialize + ?Sized,
79    {
80        self.client
81            .get_with_query(
82                &format!(
83                    "/vector_stores/{}/file_batches/{batch_id}/files",
84                    &self.vector_store_id
85                ),
86                &query,
87            )
88            .await
89    }
90}