openai-interface 0.11.0

A low-level Rust interface for the OpenAI API
Documentation
//! Manage the files of a vector store via
//! `/vector_stores/{vector_store_id}/files`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! Submodules: [`retrieve`], [`delete`], [`list`] and [`content`]; the
//! create endpoint lives directly in this module.

pub mod content;
pub mod delete;
pub mod list;
pub mod retrieve;

use serde::Serialize;
use url::Url;

use crate::{
    errors::OapiError,
    rest::post::{Post, PostNoStream},
    vector_stores::VectorStoreFile,
};

/// Adds a file to a vector store via
/// `POST /vector_stores/{vector_store_id}/files`.
#[derive(Debug, Clone, Serialize)]
pub struct CreateVectorStoreFileRequest<'a> {
    /// The ID of the vector store, e.g. `vs_...`.
    #[serde(skip)]
    pub vector_store_id: &'a str,
    /// The ID of the [`FileObject`](crate::files::FileObject) to add.
    pub file_id: String,
    /// The chunking strategy applied to the file, as raw JSON
    /// (`{"type": "auto"}` or
    /// `{"type": "static", "max_chunk_size_tokens": ..., "chunk_overlap_tokens": ...}`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub chunking_strategy: Option<serde_json::Value>,
    /// The attributes attached to the file (filterable at search
    /// time), as raw JSON.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attributes: Option<serde_json::Value>,
    /// Additional JSON properties flattened into the request body, for
    /// fields not covered by the typed struct.
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub extra_body: Option<serde_json::Map<String, serde_json::Value>>,
}

impl Post for CreateVectorStoreFileRequest<'_> {
    fn is_streaming(&self) -> bool {
        false
    }

    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("vector_stores")
            .push(self.vector_store_id)
            .push("files");
        Ok(url.to_string())
    }
}

impl PostNoStream for CreateVectorStoreFileRequest<'_> {
    type Response = VectorStoreFile;
}