openai-interface 0.11.0

A low-level Rust interface for the OpenAI API
Documentation
//! Retrieve the parsed contents of a vector store file via
//! `GET /vector_stores/{vector_store_id}/files/{file_id}/content`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.
//!
//! The response is a JSON object whose `data` array carries the parsed
//! text chunks of the file.

use url::Url;

use crate::{
    errors::OapiError,
    rest::get::{Get, GetNoStream},
};

/// Retrieves the parsed contents of a vector store file.
#[derive(Debug, Clone)]
pub struct RetrieveVectorStoreFileContentRequest<'a> {
    /// The ID of the vector store, e.g. `vs_...`.
    pub vector_store_id: &'a str,
    /// The ID of the file association, e.g. `vsf_...`.
    pub file_id: &'a str,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl Get for RetrieveVectorStoreFileContentRequest<'_> {
    /// 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")
            .push(self.file_id)
            .push("content");

        if let Some(extra_query) = &self.extra_query {
            let mut pairs = url.query_pairs_mut();
            for (key, value) in extra_query {
                pairs.append_pair(key, value);
            }
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for RetrieveVectorStoreFileContentRequest<'_> {
    type Response = VectorStoreFileContentResponse;
}

/// A parsed text chunk of a vector store file.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct VectorStoreFileContentChunk {
    /// The chunk ID, e.g. `chunk_...`.
    #[serde(default)]
    pub id: Option<String>,
    /// The object type (`vector_store.file_content.chunk`).
    #[serde(default)]
    pub object: Option<String>,
    /// The parsed text of the chunk.
    #[serde(default)]
    pub text: Option<String>,
    /// The token count of the chunk.
    #[serde(default)]
    pub tokens: Option<u64>,
    /// The attributes of the chunk, as raw JSON.
    #[serde(default)]
    pub attributes: Option<serde_json::Value>,
}

/// The response of retrieving a vector store file's contents.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct VectorStoreFileContentResponse {
    /// The parsed text chunks of the file.
    #[serde(default)]
    pub data: Vec<VectorStoreFileContentChunk>,
    /// Whether more chunks exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
}

crate::impl_from_str!(VectorStoreFileContentResponse);