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