use std::collections::BTreeMap;
use http::Method;
use serde::{Deserialize, Serialize};
use serde_json::{Number, Value};
use super::{
DeleteResponse, JsonRequestBuilder, ListRequestBuilder, VectorStoreFileBatchesResource,
VectorStoreFilesResource, VectorStoresResource, encode_path_segment,
};
use crate::Page;
use crate::generated::endpoints;
pub type VectorStoreMetadata = BTreeMap<String, String>;
pub type VectorStoreAttributes = BTreeMap<String, VectorStoreAttributeValue>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum VectorStoreAttributeValue {
String(String),
Number(Number),
Bool(bool),
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileCounts {
pub cancelled: Option<u64>,
pub completed: Option<u64>,
pub failed: Option<u64>,
pub in_progress: Option<u64>,
pub total: Option<u64>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreExpiresAfter {
pub anchor: Option<String>,
pub days: Option<u64>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreStaticFileChunkingStrategy {
pub chunk_overlap_tokens: Option<u64>,
pub max_chunk_size_tokens: Option<u64>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum VectorStoreFileChunkingStrategy {
Static {
#[serde(rename = "static")]
configuration: VectorStoreStaticFileChunkingStrategy,
},
Other,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileLastError {
pub code: Option<String>,
pub message: Option<String>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileContent {
pub text: Option<String>,
#[serde(rename = "type")]
pub content_type: Option<String>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchContent {
pub text: Option<String>,
#[serde(rename = "type")]
pub content_type: Option<String>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchResult {
pub attributes: Option<VectorStoreAttributes>,
#[serde(default)]
pub content: Vec<VectorStoreSearchContent>,
pub file_id: Option<String>,
pub filename: Option<String>,
pub score: Option<f64>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStore {
pub id: String,
#[serde(default)]
pub object: String,
pub created_at: Option<u64>,
pub description: Option<String>,
pub name: Option<String>,
pub status: Option<String>,
pub last_active_at: Option<u64>,
pub usage_bytes: Option<u64>,
pub file_counts: Option<VectorStoreFileCounts>,
pub metadata: Option<VectorStoreMetadata>,
pub expires_after: Option<VectorStoreExpiresAfter>,
pub expires_at: Option<u64>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFile {
pub id: String,
#[serde(default)]
pub object: String,
pub created_at: Option<u64>,
pub vector_store_id: Option<String>,
pub status: Option<String>,
pub last_error: Option<VectorStoreFileLastError>,
pub usage_bytes: Option<u64>,
pub attributes: Option<VectorStoreAttributes>,
pub chunking_strategy: Option<VectorStoreFileChunkingStrategy>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreFileBatch {
pub id: String,
#[serde(default)]
pub object: String,
pub created_at: Option<u64>,
pub vector_store_id: Option<String>,
pub status: Option<String>,
pub file_counts: Option<VectorStoreFileCounts>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct VectorStoreSearchResponse {
#[serde(default)]
pub object: String,
#[serde(default)]
pub data: Vec<VectorStoreSearchResult>,
pub search_query: Option<String>,
#[serde(flatten)]
pub extra: BTreeMap<String, Value>,
}
impl VectorStoresResource {
pub fn create(&self) -> JsonRequestBuilder<VectorStore> {
let endpoint = endpoints::vector_stores::VECTOR_STORES_CREATE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.template,
)
}
pub fn retrieve(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<VectorStore> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_RETRIEVE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::GET,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn update(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<VectorStore> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_UPDATE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn list(&self) -> ListRequestBuilder<VectorStore> {
let endpoint = endpoints::vector_stores::VECTOR_STORES_LIST;
vector_store_list(self.client.clone(), endpoint.id, endpoint.template)
}
pub fn delete(&self, vector_store_id: impl Into<String>) -> JsonRequestBuilder<DeleteResponse> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_DELETE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::DELETE,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn search(
&self,
vector_store_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreSearchResponse> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_SEARCH;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn files(&self) -> VectorStoreFilesResource {
VectorStoreFilesResource::new(self.client.clone())
}
pub fn file_batches(&self) -> VectorStoreFileBatchesResource {
VectorStoreFileBatchesResource::new(self.client.clone())
}
}
impl VectorStoreFilesResource {
pub fn create(
&self,
vector_store_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFile> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_CREATE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn retrieve(
&self,
vector_store_id: impl Into<String>,
file_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFile> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let file_id = encode_path_segment(file_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_RETRIEVE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::GET,
endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
)
}
pub fn update(
&self,
vector_store_id: impl Into<String>,
file_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFile> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let file_id = encode_path_segment(file_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_UPDATE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
)
}
pub fn list(&self, vector_store_id: impl Into<String>) -> ListRequestBuilder<VectorStoreFile> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_LIST;
vector_store_list(
self.client.clone(),
endpoint.id,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn delete(
&self,
vector_store_id: impl Into<String>,
file_id: impl Into<String>,
) -> JsonRequestBuilder<DeleteResponse> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let file_id = encode_path_segment(file_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_DELETE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::DELETE,
endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
)
}
pub fn content(
&self,
vector_store_id: impl Into<String>,
file_id: impl Into<String>,
) -> JsonRequestBuilder<Page<VectorStoreFileContent>> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let file_id = encode_path_segment(file_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILES_CONTENT;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::GET,
endpoint.render(&[("vector_store_id", &vector_store_id), ("file_id", &file_id)]),
)
}
}
impl VectorStoreFileBatchesResource {
pub fn create(
&self,
vector_store_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFileBatch> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_CREATE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[("vector_store_id", &vector_store_id)]),
)
}
pub fn retrieve(
&self,
vector_store_id: impl Into<String>,
batch_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFileBatch> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let batch_id = encode_path_segment(batch_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_RETRIEVE;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::GET,
endpoint.render(&[
("vector_store_id", &vector_store_id),
("batch_id", &batch_id),
]),
)
}
pub fn cancel(
&self,
vector_store_id: impl Into<String>,
batch_id: impl Into<String>,
) -> JsonRequestBuilder<VectorStoreFileBatch> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let batch_id = encode_path_segment(batch_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_CANCEL;
vector_store_json(
self.client.clone(),
endpoint.id,
Method::POST,
endpoint.render(&[
("vector_store_id", &vector_store_id),
("batch_id", &batch_id),
]),
)
}
pub fn list_files(
&self,
vector_store_id: impl Into<String>,
batch_id: impl Into<String>,
) -> ListRequestBuilder<VectorStoreFile> {
let vector_store_id = encode_path_segment(vector_store_id.into());
let batch_id = encode_path_segment(batch_id.into());
let endpoint = endpoints::vector_stores::VECTOR_STORES_FILE_BATCHES_LIST_FILES;
vector_store_list(
self.client.clone(),
endpoint.id,
endpoint.render(&[
("vector_store_id", &vector_store_id),
("batch_id", &batch_id),
]),
)
}
}
fn vector_store_json<T>(
client: crate::Client,
endpoint_id: &'static str,
method: Method,
path: impl Into<String>,
) -> JsonRequestBuilder<T> {
JsonRequestBuilder::new(client, endpoint_id, method, path)
.extra_header("openai-beta", "assistants=v2")
}
fn vector_store_list<T>(
client: crate::Client,
endpoint_id: &'static str,
path: impl Into<String>,
) -> ListRequestBuilder<T> {
ListRequestBuilder::new(client, endpoint_id, path).extra_header("openai-beta", "assistants=v2")
}