litellm_rs/storage/vector/
backend.rs1use crate::config::models::file_storage::VectorDbConfig;
4use crate::utils::error::gateway_error::{GatewayError, Result};
5use tracing::info;
6
7use super::pinecone::PineconeStore;
8use super::qdrant::QdrantStore;
9use super::types::{SearchResult, VectorPoint};
10use super::weaviate::WeaviateStore;
11
12#[derive(Debug, Clone)]
14pub enum VectorStoreBackend {
15 Qdrant(QdrantStore),
17 Weaviate(WeaviateStore),
19 Pinecone(PineconeStore),
21}
22
23impl VectorStoreBackend {
24 pub async fn new(config: &VectorDbConfig) -> Result<Self> {
26 info!("Initializing vector database: {}", config.db_type);
27
28 match config.db_type.as_str() {
29 "qdrant" => Ok(VectorStoreBackend::Qdrant(QdrantStore::new(config).await?)),
30 "weaviate" | "pinecone" => Err(GatewayError::Config(format!(
31 "Vector DB type '{}' is declared but not implemented yet. Implemented types: [\"qdrant\"]",
32 config.db_type
33 ))),
34 _ => Err(GatewayError::Config(format!(
35 "Unsupported vector DB type: {}",
36 config.db_type
37 ))),
38 }
39 }
40
41 pub async fn store(
43 &self,
44 id: &str,
45 vector: &[f32],
46 metadata: Option<serde_json::Value>,
47 ) -> Result<()> {
48 match self {
49 VectorStoreBackend::Qdrant(store) => store.store(id, vector, metadata).await,
50 VectorStoreBackend::Weaviate(store) => store.store(id, vector, metadata).await,
51 VectorStoreBackend::Pinecone(store) => store.store(id, vector, metadata).await,
52 }
53 }
54
55 pub async fn search(
57 &self,
58 query_vector: &[f32],
59 limit: usize,
60 threshold: Option<f32>,
61 ) -> Result<Vec<SearchResult>> {
62 match self {
63 VectorStoreBackend::Qdrant(store) => store.search(query_vector, limit, threshold).await,
64 VectorStoreBackend::Weaviate(store) => {
65 store.search(query_vector, limit, threshold).await
66 }
67 VectorStoreBackend::Pinecone(store) => {
68 store.search(query_vector, limit, threshold).await
69 }
70 }
71 }
72
73 pub async fn delete(&self, id: &str) -> Result<()> {
75 match self {
76 VectorStoreBackend::Qdrant(store) => store.delete(id).await,
77 VectorStoreBackend::Weaviate(store) => store.delete(id).await,
78 VectorStoreBackend::Pinecone(store) => store.delete(id).await,
79 }
80 }
81
82 pub async fn get(&self, id: &str) -> Result<Option<VectorPoint>> {
84 match self {
85 VectorStoreBackend::Qdrant(store) => store.get(id).await,
86 VectorStoreBackend::Weaviate(store) => store.get(id).await,
87 VectorStoreBackend::Pinecone(store) => store.get(id).await,
88 }
89 }
90
91 pub async fn health_check(&self) -> Result<()> {
93 match self {
94 VectorStoreBackend::Qdrant(store) => store.health_check().await,
95 VectorStoreBackend::Weaviate(store) => store.health_check().await,
96 VectorStoreBackend::Pinecone(store) => store.health_check().await,
97 }
98 }
99
100 pub async fn close(&self) -> Result<()> {
102 match self {
103 VectorStoreBackend::Qdrant(_store) => Ok(()), VectorStoreBackend::Weaviate(_store) => Ok(()),
105 VectorStoreBackend::Pinecone(_store) => Ok(()),
106 }
107 }
108
109 pub async fn batch_store(&self, points: &[VectorPoint]) -> Result<()> {
111 match self {
112 VectorStoreBackend::Qdrant(store) => store.batch_store(points).await,
113 VectorStoreBackend::Weaviate(store) => store.batch_store(points).await,
114 VectorStoreBackend::Pinecone(store) => store.batch_store(points).await,
115 }
116 }
117
118 pub async fn count(&self) -> Result<u64> {
120 match self {
121 VectorStoreBackend::Qdrant(store) => store.count().await,
122 VectorStoreBackend::Weaviate(store) => store.count().await,
123 VectorStoreBackend::Pinecone(store) => store.count().await,
124 }
125 }
126}