Spaces:
Runtime error
Runtime error
Download vector_store.py from its-seraj/rag-backend-api: direct link, hf CLI and curl.
- Browser
- Download file 5.91 kB
-
https://huggingface.co/spaces/its-seraj/rag-backend-api/resolve/main/vector_store.py
- Command line
-
hf download hf://spaces/its-seraj/rag-backend-api/vector_store.py
-
curl -L -o vector_store.py https://huggingface.co/spaces/its-seraj/rag-backend-api/resolve/main/vector_store.py
5.91 kB
| import os | |
| import uuid | |
| import logging | |
| from typing import List, Dict, Any, Optional | |
| from qdrant_client import QdrantClient | |
| from qdrant_client.http import models | |
| from qdrant_client.http.exceptions import UnexpectedResponse | |
| logger = logging.getLogger("rag_backend") | |
| class QdrantVectorStore: | |
| def __init__(self): | |
| self.url = os.getenv("QDRANT_URL") | |
| self.api_key = os.getenv("QDRANT_API_KEY") | |
| self.collection_name = os.getenv("QDRANT_COLLECTION", "rag_documents") | |
| self.vector_size = 384 # Default dimension for all-MiniLM-L6-v2 | |
| # Connect to Qdrant Cloud if credentials are provided, otherwise fallback to in-memory | |
| if self.url and self.api_key: | |
| url = self.url | |
| # Qdrant Cloud URLs typically require port 443 for HTTPS REST APIs. | |
| # If the user provided a cloud URL without specifying any port, default to 443. | |
| clean_url = url.replace("https://", "").replace("http://", "") | |
| if "qdrant.io" in url.lower() and ":" not in clean_url: | |
| url = f"{url.rstrip('/')}:443" | |
| logger.info(f"Detected Qdrant Cloud URL without port. Appending secure port 443: {url}") | |
| else: | |
| logger.info(f"Connecting to Qdrant Cloud at {url}...") | |
| self.client = QdrantClient( | |
| url=url, | |
| api_key=self.api_key | |
| ) | |
| self.is_cloud = True | |
| else: | |
| logger.warning("QDRANT_URL and QDRANT_API_KEY not found in environment variables. Falling back to local In-Memory Qdrant...") | |
| self.client = QdrantClient(location=":memory:") | |
| self.is_cloud = False | |
| self._ensure_collection() | |
| def _ensure_collection(self): | |
| """Ensures that the target collection exists in Qdrant.""" | |
| try: | |
| # Check if collection exists by getting its info | |
| self.client.get_collection(self.collection_name) | |
| logger.info(f"Collection '{self.collection_name}' already exists.") | |
| except (UnexpectedResponse, Exception): | |
| # Collection does not exist or error, create it | |
| logger.info(f"Collection '{self.collection_name}' not found. Creating a new one...") | |
| self.client.create_collection( | |
| collection_name=self.collection_name, | |
| vectors_config=models.VectorParams( | |
| size=self.vector_size, | |
| distance=models.Distance.COSINE | |
| ) | |
| ) | |
| logger.info(f"Collection '{self.collection_name}' created successfully.") | |
| def add_documents(self, texts: List[str], embeddings: List[List[float]], metadatas: Optional[List[Dict[str, Any]]] = None) -> int: | |
| """ | |
| Upserts document chunks and their corresponding embeddings and payloads into Qdrant. | |
| """ | |
| if not texts: | |
| return 0 | |
| if metadatas is None: | |
| metadatas = [{} for _ in range(len(texts))] | |
| points = [] | |
| for i, (text, embedding, metadata) in enumerate(zip(texts, embeddings, metadatas)): | |
| point_id = str(uuid.uuid4()) | |
| payload = { | |
| "text": text, | |
| "metadata": metadata | |
| } | |
| points.append( | |
| models.PointStruct( | |
| id=point_id, | |
| vector=embedding, | |
| payload=payload | |
| ) | |
| ) | |
| # Qdrant client upsert in batches | |
| self.client.upsert( | |
| collection_name=self.collection_name, | |
| wait=True, | |
| points=points | |
| ) | |
| return len(points) | |
| def search(self, query_embedding: List[float], top_k: int = 3) -> List[Dict[str, Any]]: | |
| """ | |
| Searches the Qdrant collection for similar embeddings. | |
| Returns a list of dicts with text, metadata, and score. | |
| """ | |
| results = self.client.query_points( | |
| collection_name=self.collection_name, | |
| query=query_embedding, | |
| limit=top_k | |
| ) | |
| search_results = [] | |
| for hit in results.points: | |
| search_results.append({ | |
| "text": hit.payload.get("text", ""), | |
| "metadata": hit.payload.get("metadata", {}), | |
| "score": hit.score | |
| }) | |
| return search_results | |
| def clear(self) -> bool: | |
| """ | |
| Deletes and recreates the collection to reset the database. | |
| """ | |
| try: | |
| self.client.delete_collection(self.collection_name) | |
| self.client.create_collection( | |
| collection_name=self.collection_name, | |
| vectors_config=models.VectorParams( | |
| size=self.vector_size, | |
| distance=models.Distance.COSINE | |
| ) | |
| ) | |
| logger.info(f"Collection '{self.collection_name}' has been reset.") | |
| return True | |
| except Exception as e: | |
| logger.error(f"Error resetting collection '{self.collection_name}': {e}") | |
| return False | |
| def get_status(self) -> Dict[str, Any]: | |
| """ | |
| Returns connection and collection stats. | |
| """ | |
| try: | |
| collection_info = self.client.get_collection(self.collection_name) | |
| vectors_count = collection_info.points_count | |
| status = collection_info.status.value if hasattr(collection_info.status, "value") else str(collection_info.status) | |
| except Exception as e: | |
| logger.error(f"Error retrieving collection info: {e}") | |
| vectors_count = 0 | |
| status = "unknown" | |
| return { | |
| "connection_type": "Cloud" if self.is_cloud else "In-Memory (Local)", | |
| "collection_name": self.collection_name, | |
| "vector_size": self.vector_size, | |
| "vectors_count": vectors_count, | |
| "status": status | |
| } | |