| 
									
										
										
										
											2024-09-10 11:37:06 +08:00
										 |  |  | from pydantic import BaseModel | 
					
						
							| 
									
										
										
											
												feat: Add abstract base class for vector database integration
- Created `VectorDBBase` as an abstract base class to standardize vector database operations.
- Added required methods for common vector database operations: `has_collection`, `delete_collection`, `insert`, `upsert`, `search`, `query`, `get`, `delete`, `reset`.
- The base class can now be extended by any vector database implementation (e.g., Qdrant, Pinecone) to ensure a consistent API across different database systems.
											
										 
											2025-04-21 13:26:08 +08:00
										 |  |  | from abc import ABC, abstractmethod | 
					
						
							|  |  |  | from typing import Any, Dict, List, Optional, Union | 
					
						
							| 
									
										
										
										
											2024-09-10 11:37:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class VectorItem(BaseModel): | 
					
						
							|  |  |  |     id: str | 
					
						
							|  |  |  |     text: str | 
					
						
							|  |  |  |     vector: List[float | int] | 
					
						
							|  |  |  |     metadata: Any | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-13 13:18:20 +08:00
										 |  |  | class GetResult(BaseModel): | 
					
						
							| 
									
										
										
										
											2024-09-10 11:37:06 +08:00
										 |  |  |     ids: Optional[List[List[str]]] | 
					
						
							|  |  |  |     documents: Optional[List[List[str]]] | 
					
						
							|  |  |  |     metadatas: Optional[List[List[Any]]] | 
					
						
							| 
									
										
										
										
											2024-09-13 13:18:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SearchResult(GetResult): | 
					
						
							|  |  |  |     distances: Optional[List[List[float | int]]] | 
					
						
							| 
									
										
										
											
												feat: Add abstract base class for vector database integration
- Created `VectorDBBase` as an abstract base class to standardize vector database operations.
- Added required methods for common vector database operations: `has_collection`, `delete_collection`, `insert`, `upsert`, `search`, `query`, `get`, `delete`, `reset`.
- The base class can now be extended by any vector database implementation (e.g., Qdrant, Pinecone) to ensure a consistent API across different database systems.
											
										 
											2025-04-21 13:26:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class VectorDBBase(ABC): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Abstract base class for all vector database backends. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Implementations of this class provide methods for collection management, | 
					
						
							|  |  |  |     vector insertion, deletion, similarity search, and metadata filtering. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Any custom vector database integration must inherit from this class and | 
					
						
							|  |  |  |     implement all abstract methods. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def has_collection(self, collection_name: str) -> bool: | 
					
						
							|  |  |  |         """Check if the collection exists in the vector DB.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def delete_collection(self, collection_name: str) -> None: | 
					
						
							|  |  |  |         """Delete a collection from the vector DB.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def insert(self, collection_name: str, items: List[VectorItem]) -> None: | 
					
						
							|  |  |  |         """Insert a list of vector items into a collection.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def upsert(self, collection_name: str, items: List[VectorItem]) -> None: | 
					
						
							|  |  |  |         """Insert or update vector items in a collection.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def search( | 
					
						
							|  |  |  |         self, collection_name: str, vectors: List[List[Union[float, int]]], limit: int | 
					
						
							|  |  |  |     ) -> Optional[SearchResult]: | 
					
						
							|  |  |  |         """Search for similar vectors in a collection.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def query( | 
					
						
							|  |  |  |         self, collection_name: str, filter: Dict, limit: Optional[int] = None | 
					
						
							|  |  |  |     ) -> Optional[GetResult]: | 
					
						
							|  |  |  |         """Query vectors from a collection using metadata filter.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def get(self, collection_name: str) -> Optional[GetResult]: | 
					
						
							|  |  |  |         """Retrieve all vectors from a collection.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def delete( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         collection_name: str, | 
					
						
							|  |  |  |         ids: Optional[List[str]] = None, | 
					
						
							|  |  |  |         filter: Optional[Dict] = None, | 
					
						
							|  |  |  |     ) -> None: | 
					
						
							|  |  |  |         """Delete vectors by ID or filter from a collection.""" | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @abstractmethod | 
					
						
							|  |  |  |     def reset(self) -> None: | 
					
						
							|  |  |  |         """Reset the vector database by removing all collections or those matching a condition.""" | 
					
						
							|  |  |  |         pass |