rusty_openai/openai_api/
vectors.rs

1use crate::{error_handling::OpenAIResult, extend_url_params, openai::OpenAI, setters};
2use serde::Serialize;
3use serde_json::Value;
4
5/// [`VectorsApi`] struct to interact with vector stores API endpoints.
6pub struct VectorsApi<'a>(pub(crate) &'a OpenAI<'a>);
7
8/// Struct representing a request for vector store creation.
9#[derive(Default, Serialize)]
10pub struct VectorStoreCreationRequest {
11    /// List of file IDs to include in the vector store
12    #[serde(skip_serializing_if = "Option::is_none")]
13    file_ids: Option<Vec<String>>,
14
15    /// Name for the vector store
16    #[serde(skip_serializing_if = "Option::is_none")]
17    name: Option<String>,
18
19    /// Expiration date for the vector store
20    #[serde(skip_serializing_if = "Option::is_none")]
21    expires_after: Option<Value>,
22
23    /// Strategy for chunking the data
24    #[serde(skip_serializing_if = "Option::is_none")]
25    chunking_strategy: Option<Value>,
26
27    /// Metadata for the vector store
28    #[serde(skip_serializing_if = "Option::is_none")]
29    metadata: Option<Value>,
30}
31
32/// Struct representing a request for vector store modification.
33#[derive(Default, Serialize)]
34pub struct VectorStoreModificationRequest {
35    /// Name for the vector store
36    #[serde(skip_serializing_if = "Option::is_none")]
37    name: Option<String>,
38
39    /// Expiration date for the vector store
40    #[serde(skip_serializing_if = "Option::is_none")]
41    expires_after: Option<Value>,
42
43    /// Metadata for the vector store
44    #[serde(skip_serializing_if = "Option::is_none")]
45    metadata: Option<Value>,
46}
47
48impl VectorStoreCreationRequest {
49    setters! {
50        /// Set file IDs for the request.
51        file_ids: Vec<String>,
52
53        /// Set name for the request.
54        name: String,
55
56        /// Set expiration date for the request.
57        expires_after: Value,
58
59        /// Set chunking strategy for the request.
60        chunking_strategy: Value,
61
62        /// Set metadata for the request.
63        metadata: Value,
64    }
65}
66
67impl<'a> VectorsApi<'a> {
68    /// Create a new vector store using the provided request parameters.
69    ///
70    /// # Arguments
71    ///
72    /// * `request` - A [`VectorStoreCreationRequest`] containing the parameters for the vector store.
73    ///
74    /// # Returns
75    ///
76    /// A Result containing the JSON response as [`serde_json::Value`] on success, or an [`OpenAIError`][crate::error_handling::OpenAIError] on failure.
77    pub async fn create_vector_store(
78        &self,
79        request: VectorStoreCreationRequest,
80    ) -> OpenAIResult<Value> {
81        // Send a POST request to the vector stores endpoint with the request body.
82        self.0.post_json("/vector_stores", &request).await
83    }
84
85    /// List vector stores with optional query parameters.
86    ///
87    /// # Arguments
88    ///
89    /// * `limit` - Maximum number of vector stores to retrieve.
90    /// * `order` - Order of the retrieved vector stores.
91    /// * `after` - Retrieve vector stores created after this ID.
92    /// * `before` - Retrieve vector stores created before this ID.
93    ///
94    /// # Returns
95    ///
96    /// A Result containing the JSON response as [`serde_json::Value`] on success, or an [`OpenAIError`][crate::error_handling::OpenAIError] on failure.
97    pub async fn list_vector_stores(
98        &self,
99        limit: Option<u64>,
100        order: Option<String>,
101        after: Option<String>,
102        before: Option<String>,
103    ) -> OpenAIResult<Value> {
104        let mut url = String::from("/vector_stores?");
105
106        extend_url_params!(url, limit, order, after, before);
107        url.pop();
108
109        self.0.get(&url).await
110    }
111
112    /// Retrieve details of a specific vector store.
113    ///
114    /// # Arguments
115    ///
116    /// * `vector_store_id` - The ID of the vector store to retrieve.
117    ///
118    /// # Returns
119    ///
120    /// A Result containing the JSON response as [`serde_json::Value`] on success, or an [`OpenAIError`][crate::error_handling::OpenAIError] on failure.
121    pub async fn retrieve_vector_store(&self, vector_store_id: &str) -> OpenAIResult<Value> {
122        let url = format!("/vector_stores/{vector_store_id}");
123
124        self.0.get(&url).await
125    }
126
127    /// Modify an existing vector store using the provided request parameters.
128    ///
129    /// # Arguments
130    ///
131    /// * `vector_store_id` - The ID of the vector store to modify.
132    /// * `request` - A [`VectorStoreModificationRequest`] containing the parameters for the vector store modification.
133    ///
134    /// # Returns
135    ///
136    /// A Result containing the JSON response as [`serde_json::Value`] on success, or an [`OpenAIError`][crate::error_handling::OpenAIError] on failure.
137    pub async fn modify_vector_store(
138        &self,
139        vector_store_id: &str,
140        request: VectorStoreModificationRequest,
141    ) -> OpenAIResult<Value> {
142        let url = format!("/vector_stores/{vector_store_id}");
143
144        self.0.post_json(&url, &request).await
145    }
146
147    /// Delete a specific vector store.
148    ///
149    /// # Arguments
150    ///
151    /// * `vector_store_id` - The ID of the vector store to delete.
152    ///
153    /// # Returns
154    ///
155    /// A Result containing the JSON response as [`serde_json::Value`] on success, or an [`OpenAIError`][crate::error_handling::OpenAIError] on failure.
156    pub async fn delete_vector_store(&self, vector_store_id: &str) -> OpenAIResult<Value> {
157        let url = format!("/vector_stores/{vector_store_id}");
158
159        self.0.delete(&url).await
160    }
161}