use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
type Extra = Map<String, Value>;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Collection {
pub name: String,
#[serde(default)]
pub dimension: Option<u64>,
#[serde(default)]
pub metric: Option<String>,
#[serde(default)]
pub count: u64,
#[serde(default)]
pub size_bytes: u64,
#[serde(default)]
pub description: Option<String>,
#[serde(default)]
pub has_text_index: bool,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub created_at: Option<String>,
#[serde(default)]
pub updated_at: Option<String>,
#[serde(default)]
pub indexed_fields: Vec<Value>,
#[serde(flatten)]
pub extra: Extra,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Hit {
pub id: String,
#[serde(default)]
pub score: f64,
#[serde(default)]
pub text: String,
#[serde(default)]
pub metadata: Map<String, Value>,
#[serde(skip)]
pub citation: String,
#[serde(flatten)]
pub extra: Extra,
}
impl Hit {
pub(crate) fn with_citation(mut self) -> Hit {
let from = |key: &str| {
self.metadata
.get(key)
.and_then(Value::as_str)
.map(str::to_owned)
};
self.citation = from("_vx_citation")
.or_else(|| from("source"))
.unwrap_or_else(|| self.id.clone());
self
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Document {
pub doc_id: String,
#[serde(default)]
pub filename: Option<String>,
#[serde(default)]
pub kind: Option<String>,
#[serde(default)]
pub source: Option<Value>,
#[serde(default)]
pub version: Option<String>,
#[serde(default)]
pub extracted_at: Option<String>,
#[serde(default)]
pub chunking: Map<String, Value>,
#[serde(flatten)]
pub extra: Extra,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Added {
pub doc_id: String,
#[serde(default)]
pub chunks: u64,
#[serde(default)]
pub replaced: u64,
#[serde(default)]
pub quality: f64,
#[serde(default)]
pub low_quality: bool,
#[serde(default)]
pub citations: Vec<String>,
#[serde(default)]
pub kept: bool,
#[serde(flatten)]
pub extra: Extra,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Source {
pub id: String,
#[serde(default)]
pub address: String,
#[serde(default)]
pub kind: Option<String>,
#[serde(default)]
pub every: Option<Value>,
#[serde(flatten)]
pub extra: Extra,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Refreshed {
#[serde(default)]
pub added: u64,
#[serde(default)]
pub updated: u64,
#[serde(default)]
pub unchanged: u64,
#[serde(default)]
pub removed: u64,
#[serde(default)]
pub failed: Vec<Value>,
#[serde(flatten)]
pub extra: Extra,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Mode {
#[default]
Meaning,
Hybrid,
}
#[derive(Debug, Clone)]
pub struct SearchOptions {
pub limit: usize,
pub filter: Option<Value>,
pub rerank: bool,
pub mode: Mode,
}
impl Default for SearchOptions {
fn default() -> Self {
SearchOptions {
limit: 10,
filter: None,
rerank: false,
mode: Mode::Meaning,
}
}
}
#[derive(Debug, Clone)]
pub struct CreateOptions {
pub dimension: u32,
pub text_index: bool,
pub metric: String,
pub description: Option<String>,
}
impl Default for CreateOptions {
fn default() -> Self {
CreateOptions {
dimension: 384,
text_index: true,
metric: "cosine".to_owned(),
description: None,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct AddOptions {
pub doc_id: Option<String>,
pub metadata: Option<Value>,
pub chunk: Option<String>,
pub chunk_size: Option<u32>,
pub overlap: Option<u32>,
}
#[derive(Debug, Clone, Default)]
pub struct Text {
pub id: String,
pub text: String,
pub metadata: Option<Map<String, Value>>,
}
impl Text {
pub fn new(id: impl Into<String>, text: impl Into<String>) -> Text {
Text {
id: id.into(),
text: text.into(),
metadata: None,
}
}
pub fn metadata(mut self, metadata: Map<String, Value>) -> Text {
self.metadata = Some(metadata);
self
}
}
#[derive(Debug, Clone, Default)]
pub struct SourceOptions {
pub kind: Option<String>,
pub every: Option<String>,
}