openai-interface 0.10.0

A low-level Rust interface for the OpenAI API
Documentation
//! Search a vector store via `POST /vector_stores/{vector_store_id}/search`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

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

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

/// A search result of a vector store query.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct VectorStoreSearchResult {
    /// The ID of the file the result came from.
    #[serde(default)]
    pub file_id: Option<String>,
    /// The filename of the file the result came from.
    #[serde(default)]
    pub filename: Option<String>,
    /// The relevance score of the result.
    #[serde(default)]
    pub score: Option<f64>,
    /// The matching text chunks.
    #[serde(default)]
    pub text: Option<Vec<String>>,
    /// The matching text chunks with their individual scores.
    #[serde(default)]
    pub attributes: Option<serde_json::Value>,
}

/// The response of a vector store search.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct VectorStoreSearchResponse {
    /// The object type (`vector_store.search_results.page`).
    #[serde(default)]
    pub object: Option<String>,
    /// The search query.
    #[serde(default)]
    pub search_query: Option<Vec<String>>,
    /// The matching results.
    #[serde(default)]
    pub data: Vec<VectorStoreSearchResult>,
    /// Whether more results exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The pagination token for the next page.
    #[serde(default)]
    pub next_page: Option<String>,
}

crate::impl_from_str!(VectorStoreSearchResponse);

/// Searches a vector store for matching chunks.
#[derive(Debug, Clone, Serialize)]
pub struct SearchVectorStoreRequest<'a> {
    /// The ID of the vector store to search, e.g. `vs_...`.
    #[serde(skip)]
    pub vector_store_id: &'a str,
    /// The search query.
    pub query: Vec<String>,
    /// Whether to rewrite the query with the model (REWRITE or
    /// DISABLED, as raw JSON when a string).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rewrite_query: Option<bool>,
    /// The maximum number of results to return (1-50, default 10).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_num_results: Option<u32>,
    /// The attribute filters to apply, as raw JSON.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filters: Option<serde_json::Value>,
    /// The ranking options (e.g. the reranker), as raw JSON.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ranking_options: 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 SearchVectorStoreRequest<'_> {
    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("search");
        Ok(url.to_string())
    }
}

impl PostNoStream for SearchVectorStoreRequest<'_> {
    type Response = VectorStoreSearchResponse;
}