Skip to main content

novel_openai/vectorstores/
vector_store_file_batches.rs

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