use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct Document {
pub id: String,
pub text: String,
pub metadata: HashMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_uri: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
}
impl Document {
pub fn new(id: impl Into<String>, text: impl Into<String>) -> Self {
Self {
id: id.into(),
text: text.into(),
metadata: HashMap::new(),
source_uri: None,
title: None,
}
}
#[must_use]
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
#[must_use]
pub fn with_source_uri(mut self, source_uri: impl Into<String>) -> Self {
self.source_uri = Some(source_uri.into());
self
}
#[must_use]
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[non_exhaustive]
pub struct Chunk {
pub id: String,
pub text: String,
pub embedding: Vec<f32>,
pub metadata: HashMap<String, String>,
pub document_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_uri: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
}
impl Chunk {
pub fn new(
id: impl Into<String>,
document_id: impl Into<String>,
text: impl Into<String>,
) -> Self {
Self {
id: id.into(),
text: text.into(),
embedding: Vec::new(),
metadata: HashMap::new(),
document_id: document_id.into(),
source_uri: None,
title: None,
}
}
pub fn from_document(
document: &Document,
id: impl Into<String>,
text: impl Into<String>,
) -> Self {
Self {
id: id.into(),
text: text.into(),
embedding: Vec::new(),
metadata: document.metadata.clone(),
document_id: document.id.clone(),
source_uri: document.source_uri.clone(),
title: document.title.clone(),
}
}
#[must_use]
pub fn with_embedding(mut self, embedding: Vec<f32>) -> Self {
self.embedding = embedding;
self
}
#[must_use]
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
#[must_use]
pub fn with_source_uri(mut self, source_uri: impl Into<String>) -> Self {
self.source_uri = Some(source_uri.into());
self
}
#[must_use]
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
pub chunk: Chunk,
pub score: f32,
}