use crate::client::Client;
use crate::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Document {
pub id: i64,
pub name: String,
pub collection_id: i64,
pub created: i64,
#[serde(default)]
pub chunks: Option<i64>,
#[serde(default)]
pub size: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Documents {
pub data: Vec<Document>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct DocumentResponse {
pub id: i64,
}
#[derive(Debug, Clone, Default)]
pub struct CreateDocument {
pub collection_id: Option<i64>,
pub name: Option<String>,
pub disable_chunking: Option<bool>,
pub chunk_size: Option<i64>,
pub chunk_overlap: Option<i64>,
}
impl CreateDocument {
#[must_use]
pub fn new(collection_id: i64) -> Self {
Self {
collection_id: Some(collection_id),
..Self::default()
}
}
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
#[must_use]
pub fn disable_chunking(mut self, disable: bool) -> Self {
self.disable_chunking = Some(disable);
self
}
#[must_use]
pub fn chunk_size(mut self, size: i64) -> Self {
self.chunk_size = Some(size);
self
}
#[must_use]
pub fn chunk_overlap(mut self, overlap: i64) -> Self {
self.chunk_overlap = Some(overlap);
self
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct DocumentsQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub collection_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u32>,
}
impl DocumentsQuery {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn collection(mut self, collection_id: i64) -> Self {
self.collection_id = Some(collection_id);
self
}
#[must_use]
pub fn offset(mut self, offset: u32) -> Self {
self.offset = Some(offset);
self
}
#[must_use]
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
}
impl Client {
pub async fn create_document(
&self,
file_name: impl Into<String>,
file: Vec<u8>,
opts: &CreateDocument,
) -> Result<DocumentResponse, Error> {
let url = self.endpoint("/v1/documents")?;
let mut form = reqwest::multipart::Form::new().part(
"file",
reqwest::multipart::Part::bytes(file).file_name(file_name.into()),
);
if let Some(collection_id) = opts.collection_id {
form = form.text("collection_id", collection_id.to_string());
}
if let Some(name) = &opts.name {
form = form.text("name", name.clone());
}
if let Some(disable) = opts.disable_chunking {
form = form.text("disable_chunking", disable.to_string());
}
if let Some(size) = opts.chunk_size {
form = form.text("chunk_size", size.to_string());
}
if let Some(overlap) = opts.chunk_overlap {
form = form.text("chunk_overlap", overlap.to_string());
}
self.post_multipart(url, form).await
}
pub async fn documents(&self, query: &DocumentsQuery) -> Result<Documents, Error> {
let url = self.endpoint("/v1/documents")?;
self.get_json_with_query(url, query).await
}
pub async fn document(&self, document_id: impl std::fmt::Display) -> Result<Document, Error> {
let url = self.endpoint_with_segment("/v1/documents", &document_id.to_string())?;
self.get_json(url).await
}
pub async fn delete_document(&self, document_id: impl std::fmt::Display) -> Result<(), Error> {
let url = self.endpoint_with_segment("/v1/documents", &document_id.to_string())?;
self.delete_no_content(url).await
}
}