Skip to main content

openai_interface/vector_stores/files/
content.rs

1//! Retrieve the parsed contents of a vector store file via
2//! `GET /vector_stores/{vector_store_id}/files/{file_id}/content`.
3//!
4//! > ![warn] This module is untested!
5//! > If you encounter any issues, please report them on the repository.
6//!
7//! The response is a JSON object whose `data` array carries the parsed
8//! text chunks of the file.
9
10use url::Url;
11
12use crate::{
13    errors::OapiError,
14    rest::get::{Get, GetNoStream},
15};
16
17/// Retrieves the parsed contents of a vector store file.
18#[derive(Debug, Clone)]
19pub struct RetrieveVectorStoreFileContentRequest<'a> {
20    /// The ID of the vector store, e.g. `vs_...`.
21    pub vector_store_id: &'a str,
22    /// The ID of the file association, e.g. `vsf_...`.
23    pub file_id: &'a str,
24    /// Additional query parameters appended verbatim to the URL.
25    pub extra_query: Option<std::collections::HashMap<String, String>>,
26}
27
28impl Get for RetrieveVectorStoreFileContentRequest<'_> {
29    /// Builds the URL for the request.
30    ///
31    /// `base_url` should be like <https://api.openai.com/v1>
32    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
33        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
34        url.path_segments_mut()
35            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
36            .push("vector_stores")
37            .push(self.vector_store_id)
38            .push("files")
39            .push(self.file_id)
40            .push("content");
41
42        if let Some(extra_query) = &self.extra_query {
43            let mut pairs = url.query_pairs_mut();
44            for (key, value) in extra_query {
45                pairs.append_pair(key, value);
46            }
47        }
48
49        Ok(url.to_string())
50    }
51}
52
53impl GetNoStream for RetrieveVectorStoreFileContentRequest<'_> {
54    type Response = VectorStoreFileContentResponse;
55}
56
57/// A parsed text chunk of a vector store file.
58#[derive(Debug, Clone, serde::Deserialize)]
59pub struct VectorStoreFileContentChunk {
60    /// The chunk ID, e.g. `chunk_...`.
61    #[serde(default)]
62    pub id: Option<String>,
63    /// The object type (`vector_store.file_content.chunk`).
64    #[serde(default)]
65    pub object: Option<String>,
66    /// The parsed text of the chunk.
67    #[serde(default)]
68    pub text: Option<String>,
69    /// The token count of the chunk.
70    #[serde(default)]
71    pub tokens: Option<u64>,
72    /// The attributes of the chunk, as raw JSON.
73    #[serde(default)]
74    pub attributes: Option<serde_json::Value>,
75}
76
77/// The response of retrieving a vector store file's contents.
78#[derive(Debug, Clone, serde::Deserialize)]
79pub struct VectorStoreFileContentResponse {
80    /// The parsed text chunks of the file.
81    #[serde(default)]
82    pub data: Vec<VectorStoreFileContentChunk>,
83    /// Whether more chunks exist after this page.
84    #[serde(default)]
85    pub has_more: Option<bool>,
86}
87
88crate::impl_from_str!(VectorStoreFileContentResponse);