llm_chain/
schema.rs

1//! Schema for Documents that can be stored in vector stores.
2//!
3//! This schema is used to store documents in vector stores. It is used to store the document's content and metadata.
4
5#[derive(Debug)]
6pub struct Document<M = EmptyMetadata>
7where
8    M: serde::Serialize + serde::de::DeserializeOwned,
9{
10    pub page_content: String,
11    pub metadata: Option<M>,
12}
13
14impl<M> Document<M>
15where
16    M: serde::Serialize + serde::de::DeserializeOwned,
17{
18    pub fn new(page_content: String) -> Self {
19        Document {
20            page_content,
21            metadata: None,
22        }
23    }
24}
25
26#[derive(Debug)]
27pub struct EmptyMetadata;
28
29impl From<()> for EmptyMetadata {
30    fn from(_: ()) -> Self {
31        EmptyMetadata
32    }
33}
34
35// impl a serializer that turns it into a null
36impl serde::Serialize for EmptyMetadata {
37    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
38        serializer.serialize_none()
39    }
40}
41
42// impl a deserializer that turns it into a null
43use serde::de::{self, Deserialize, Deserializer};
44
45impl<'de> Deserialize<'de> for EmptyMetadata {
46    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
47        struct EmptyMetadataVisitor;
48
49        impl<'de> de::Visitor<'de> for EmptyMetadataVisitor {
50            type Value = EmptyMetadata;
51
52            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
53                formatter.write_str("a null value")
54            }
55
56            fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
57                Ok(EmptyMetadata)
58            }
59        }
60
61        deserializer.deserialize_unit(EmptyMetadataVisitor)
62    }
63}