async_openai_alt/
vector_store_files.rs

1use serde::Serialize;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{
7        CreateVectorStoreFileRequest, DeleteVectorStoreFileResponse, ListVectorStoreFilesResponse,
8        VectorStoreFileObject,
9    },
10    Client,
11};
12
13/// Vector store files represent files inside a vector store.
14///
15/// Related guide: [File Search](https://platform.openai.com/docs/assistants/tools/file-search)
16pub struct VectorStoreFiles<'c, C: Config> {
17    client: &'c Client<C>,
18    pub vector_store_id: String,
19}
20
21impl<'c, C: Config> VectorStoreFiles<'c, C> {
22    pub fn new(client: &'c Client<C>, vector_store_id: &str) -> Self {
23        Self {
24            client,
25            vector_store_id: vector_store_id.into(),
26        }
27    }
28
29    /// Create a vector store file by attaching a [File](https://platform.openai.com/docs/api-reference/files) to a [vector store](https://platform.openai.com/docs/api-reference/vector-stores/object).
30    pub async fn create(
31        &self,
32        request: CreateVectorStoreFileRequest,
33    ) -> Result<VectorStoreFileObject, OpenAIError> {
34        self.client
35            .post(
36                &format!("/vector_stores/{}/files", &self.vector_store_id),
37                request,
38            )
39            .await
40    }
41
42    /// Retrieves a vector store file.
43    pub async fn retrieve(&self, file_id: &str) -> Result<VectorStoreFileObject, OpenAIError> {
44        self.client
45            .get(&format!(
46                "/vector_stores/{}/files/{file_id}",
47                &self.vector_store_id
48            ))
49            .await
50    }
51
52    /// Delete a vector store file. This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the [delete file](https://platform.openai.com/docs/api-reference/files/delete) endpoint.
53    pub async fn delete(
54        &self,
55        file_id: &str,
56    ) -> Result<DeleteVectorStoreFileResponse, OpenAIError> {
57        self.client
58            .delete(&format!(
59                "/vector_stores/{}/files/{file_id}",
60                &self.vector_store_id
61            ))
62            .await
63    }
64
65    /// Returns a list of vector store files.
66    pub async fn list<Q>(&self, query: &Q) -> Result<ListVectorStoreFilesResponse, OpenAIError>
67    where
68        Q: Serialize + ?Sized,
69    {
70        self.client
71            .get_with_query(
72                &format!("/vector_stores/{}/files", &self.vector_store_id),
73                query,
74            )
75            .await
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use crate::types::{CreateFileRequest, CreateVectorStoreRequest, FileInput, FilePurpose};
82    use crate::Client;
83
84    #[tokio::test]
85    async fn vector_store_file_creation_and_deletion(
86    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
87        let client = Client::new();
88
89        // Create a file
90        let file_handle = client
91            .files()
92            .create(CreateFileRequest {
93                file: FileInput::from_vec_u8(
94                    String::from("meow.txt"),
95                    String::from(":3").into_bytes(),
96                ),
97                purpose: FilePurpose::Assistants,
98            })
99            .await?;
100
101        // Create a vector store
102        let vector_store_handle = client
103            .vector_stores()
104            .create(CreateVectorStoreRequest {
105                file_ids: Some(vec![file_handle.id.clone()]),
106                name: None,
107                expires_after: None,
108                chunking_strategy: None,
109                metadata: None,
110            })
111            .await?;
112        let vector_store_file = client
113            .vector_stores()
114            .files(&vector_store_handle.id)
115            .retrieve(&file_handle.id)
116            .await?;
117
118        assert_eq!(vector_store_file.id, file_handle.id);
119        // Delete the vector store
120        client
121            .vector_stores()
122            .delete(&vector_store_handle.id)
123            .await?;
124
125        // Delete the file
126        client.files().delete(&file_handle.id).await?;
127
128        Ok(())
129    }
130}