Skip to main content

openai_interface/vector_stores/files/
mod.rs

1//! Manage the files of a vector store via
2//! `/vector_stores/{vector_store_id}/files`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6//!
7//! Submodules: [`retrieve`], [`delete`], [`list`] and [`content`]; the
8//! create endpoint lives directly in this module.
9
10pub mod content;
11pub mod delete;
12pub mod list;
13pub mod retrieve;
14
15use serde::Serialize;
16use url::Url;
17
18use crate::{
19    errors::OapiError,
20    rest::post::{Post, PostNoStream},
21    vector_stores::VectorStoreFile,
22};
23
24/// Adds a file to a vector store via
25/// `POST /vector_stores/{vector_store_id}/files`.
26#[derive(Debug, Clone, Serialize)]
27pub struct CreateVectorStoreFileRequest<'a> {
28    /// The ID of the vector store, e.g. `vs_...`.
29    #[serde(skip)]
30    pub vector_store_id: &'a str,
31    /// The ID of the [`FileObject`](crate::files::FileObject) to add.
32    pub file_id: String,
33    /// The chunking strategy applied to the file, as raw JSON
34    /// (`{"type": "auto"}` or
35    /// `{"type": "static", "max_chunk_size_tokens": ..., "chunk_overlap_tokens": ...}`).
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub chunking_strategy: Option<serde_json::Value>,
38    /// The attributes attached to the file (filterable at search
39    /// time), as raw JSON.
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub attributes: Option<serde_json::Value>,
42    /// Additional JSON properties flattened into the request body, for
43    /// fields not covered by the typed struct.
44    #[serde(flatten, skip_serializing_if = "Option::is_none")]
45    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
46}
47
48impl Post for CreateVectorStoreFileRequest<'_> {
49    fn is_streaming(&self) -> bool {
50        false
51    }
52
53    /// Builds the URL for the request.
54    ///
55    /// `base_url` should be like <https://api.openai.com/v1>
56    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
57        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
58        url.path_segments_mut()
59            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
60            .push("vector_stores")
61            .push(self.vector_store_id)
62            .push("files");
63        Ok(url.to_string())
64    }
65}
66
67impl PostNoStream for CreateVectorStoreFileRequest<'_> {
68    type Response = VectorStoreFile;
69}