Skip to main content

lc_vector_stores/
lib.rs

1// lc-vector-stores/src/lib.rs
2//! Vector store implementations.
3//!
4//! Provides document vector storage and retrieval functionality.
5
6pub mod chromadb;
7pub mod chunked_vector_store;
8pub mod document_store;
9mod file_store;
10pub mod lancedb;
11pub mod neo4j;
12mod memory;
13mod provider;
14
15#[cfg(feature = "mongodb-persistence")]
16mod mongo_document_store;
17
18#[cfg(feature = "qdrant-integration")]
19mod qdrant;
20
21#[cfg(feature = "redis-storage")]
22pub mod redis_store;
23
24#[cfg(feature = "sqlite-storage")]
25pub mod sqlite_store;
26
27#[cfg(feature = "pgvector-storage")]
28pub mod pgvector;
29
30pub mod pinecone;
31
32pub use chunked_vector_store::ChunkedVectorStore;
33pub use document_store::{
34    ChunkedDocumentStore, ChunkedDocumentStoreTrait, DocumentStore, InMemoryChunkedDocumentStore,
35    InMemoryDocumentStore,
36};
37pub use file_store::FileVectorStore;
38pub use memory::InMemoryVectorStore;
39pub use lancedb::{LanceDBConfig, LanceDBVectorStore};
40pub use neo4j::{Neo4jConfig, Neo4jVectorStore};
41pub use pinecone::PineconeStore;
42pub use provider::{VectorStoreBuilder, VectorStoreProvider, VectorStoreType};
43
44#[cfg(feature = "mongodb-persistence")]
45pub use mongo_document_store::{MongoChunkedDocumentStore, MongoStoreConfig};
46
47#[cfg(feature = "qdrant-integration")]
48pub use qdrant::{QdrantConfig, QdrantDistance, QdrantVectorStore};
49
50pub use chromadb::{ChromaDBConfig, ChromaDBVectorStore};
51
52#[cfg(feature = "redis-storage")]
53pub use redis_store::{RedisDocumentStore, RedisStoreConfig};
54
55#[cfg(feature = "sqlite-storage")]
56pub use sqlite_store::{SQLiteDocumentStore, SQLiteStoreConfig};
57
58use async_trait::async_trait;
59use std::error::Error;
60
61// Re-export shared document types from lc-shared
62pub use lc_shared::document::{ChunkDocument, Document, SearchResult, VectorDocument};
63
64// Re-export cosine_similarity from lc-core
65pub use lc_core::math::cosine_similarity;
66
67/// Vector store error types.
68#[derive(Debug)]
69pub enum VectorStoreError {
70    /// Document not found.
71    DocumentNotFound(String),
72
73    /// Embedding error.
74    EmbeddingError(String),
75
76    /// Storage error.
77    StorageError(String),
78
79    /// Connection error (for remote vector databases).
80    ConnectionError(String),
81}
82
83impl std::fmt::Display for VectorStoreError {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        match self {
86            VectorStoreError::DocumentNotFound(id) => write!(f, "Document not found: {}", id),
87            VectorStoreError::EmbeddingError(msg) => write!(f, "Embedding error: {}", msg),
88            VectorStoreError::StorageError(msg) => write!(f, "Storage error: {}", msg),
89            VectorStoreError::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
90        }
91    }
92}
93
94impl Error for VectorStoreError {}
95
96/// Vector store trait.
97#[async_trait]
98pub trait VectorStore: Send + Sync {
99    /// Adds documents.
100    ///
101    /// # Arguments
102    /// * `documents` - Document list.
103    /// * `embeddings` - Embedding vectors for documents.
104    ///
105    /// # Returns
106    /// Document ID list.
107    async fn add_documents(
108        &self,
109        documents: Vec<Document>,
110        embeddings: Vec<Vec<f32>>,
111    ) -> Result<Vec<String>, VectorStoreError>;
112
113    /// Searches similar documents.
114    ///
115    /// # Arguments
116    /// * `query_embedding` - Query vector.
117    /// * `k` - Number of documents to return.
118    ///
119    /// # Returns
120    /// Similar document list (sorted by similarity descending).
121    async fn similarity_search(
122        &self,
123        query_embedding: &[f32],
124        k: usize,
125    ) -> Result<Vec<SearchResult>, VectorStoreError>;
126
127    /// Gets document by ID.
128    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError>;
129
130    /// Gets document embedding by ID.
131    async fn get_embedding(&self, id: &str) -> Result<Option<Vec<f32>>, VectorStoreError>;
132
133    /// Deletes document.
134    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError>;
135
136    /// Returns document count.
137    async fn count(&self) -> usize;
138
139    /// Clears store.
140    async fn clear(&self) -> Result<(), VectorStoreError>;
141}
142
143/// Embedding model trait — re-exported from lc-embeddings.
144///
145/// Used by vector store implementations that need to embed documents on the fly
146/// (e.g., Pinecone's `upsert` method).
147pub use lc_embeddings::{EmbeddingError, Embeddings};
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn test_document_creation() {
155        let doc = Document::new("Hello, world!")
156            .with_metadata("source", "test")
157            .with_id("doc-1");
158
159        assert_eq!(doc.content, "Hello, world!");
160        assert_eq!(doc.metadata.get("source"), Some(&"test".to_string()));
161        assert_eq!(doc.id, Some("doc-1".to_string()));
162    }
163
164    #[test]
165    fn test_document_page_content() {
166        let doc = Document::new("Test content");
167        assert_eq!(doc.page_content(), "Test content");
168    }
169}