justmotes commited on
Commit
3621168
·
1 Parent(s): 5827883

Fix: Prevent data wipe in initialize and set correct port

Browse files
Files changed (1) hide show
  1. src/vector_db.py +56 -31
src/vector_db.py CHANGED
@@ -22,47 +22,72 @@ class UnifiedQdrant:
22
  url = os.getenv("QDRANT_URL", ":memory:")
23
  api_key = os.getenv("QDRANT_API_KEY", None)
24
  print(f"Connecting to Qdrant at {url}...")
25
- self.client = QdrantClient(location=url, api_key=api_key, timeout=60)
 
 
 
 
 
 
 
 
 
 
26
 
27
  self.is_local = url == ":memory:" or not url.startswith("http")
 
28
  if self.is_local:
29
- print("WARNING: Running in local/memory mode. Custom Sharding is NOT supported. Simulating behavior.")
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
- # Check if collection exists, if so, recreate it for a clean slate (or handle gracefully)
32
- if self.client.collection_exists(self.collection_name):
33
- self.client.delete_collection(self.collection_name)
34
-
35
- # Try to create collection with full clusters
36
- try:
37
- self._create_collection_and_shards(self.num_clusters)
38
- print(f"Successfully created collection with {self.num_clusters} clusters.")
39
- except Exception as e:
40
- print(f"Failed to create {self.num_clusters} clusters: {e}")
41
- print("Attempting fallback to 8 clusters (Free Tier limit mitigation)...")
42
- # Fallback 1: 8 Clusters
43
  try:
44
- self.num_clusters = 8
45
- if self.client.collection_exists(self.collection_name):
46
- self.client.delete_collection(self.collection_name)
47
  self._create_collection_and_shards(self.num_clusters)
48
- print(f"Fallback successful: Created collection with {self.num_clusters} clusters.")
49
- except Exception as e2:
50
- print(f"Failed to create 8 clusters: {e2}")
51
- print("CRITICAL: Custom Sharding not supported. Falling back to Standard Collection (No Sharding).")
52
- # Fallback 2: Standard Collection
53
- self.num_clusters = 1 # Virtual clusters only
54
- if self.client.collection_exists(self.collection_name):
55
- self.client.delete_collection(self.collection_name)
56
-
57
- self.client.create_collection(
58
- collection_name=self.collection_name,
59
- vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE)
60
- )
61
- print("Fallback successful: Created Standard Collection.")
 
 
 
 
 
 
 
 
 
 
62
 
63
  def _create_collection_and_shards(self, n_clusters):
64
  print(f"Creating collection '{self.collection_name}' with custom sharding ({n_clusters} clusters)...")
65
 
 
 
 
 
 
66
  if self.is_local:
67
  # Local mode doesn't support sharding_method=CUSTOM
68
  self.client.create_collection(
 
22
  url = os.getenv("QDRANT_URL", ":memory:")
23
  api_key = os.getenv("QDRANT_API_KEY", None)
24
  print(f"Connecting to Qdrant at {url}...")
25
+
26
+ # Relaxed connection settings for HF Spaces
27
+ port = 443 if url.startswith("https") else 6333
28
+ self.client = QdrantClient(
29
+ location=url,
30
+ port=port,
31
+ api_key=api_key,
32
+ timeout=60,
33
+ check_compatibility=False,
34
+ verify=False # Passed to httpx
35
+ )
36
 
37
  self.is_local = url == ":memory:" or not url.startswith("http")
38
+
39
  if self.is_local:
40
+ print("Running in local/memory mode. Custom Sharding is NOT supported. Simulating behavior.")
41
+ self.num_clusters = 1
42
+ if self.client.collection_exists(collection_name=self.collection_name):
43
+ self.client.delete_collection(collection_name=self.collection_name)
44
+ self.client.create_collection(
45
+ collection_name=self.collection_name,
46
+ vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE)
47
+ )
48
+ print(f"Created standard collection '{self.collection_name}'.")
49
+ else:
50
+ # Check if exists first to avoid accidental deletion
51
+ if self.client.collection_exists(self.collection_name):
52
+ print(f"Collection '{self.collection_name}' already exists. Skipping initialization.")
53
+ return
54
 
55
+ # Try to create collection with full clusters
 
 
 
 
 
 
 
 
 
 
 
56
  try:
 
 
 
57
  self._create_collection_and_shards(self.num_clusters)
58
+ print(f"Successfully created collection with {self.num_clusters} clusters.")
59
+ except Exception as e:
60
+ print(f"Failed to create {self.num_clusters} clusters: {e}")
61
+ print("Attempting fallback to 8 clusters (Free Tier limit mitigation)...")
62
+ # Fallback 1: 8 Clusters
63
+ try:
64
+ self.num_clusters = 8
65
+ if self.client.collection_exists(self.collection_name):
66
+ self.client.delete_collection(self.collection_name)
67
+ self._create_collection_and_shards(self.num_clusters)
68
+ print(f"Fallback successful: Created collection with {self.num_clusters} clusters.")
69
+ except Exception as e2:
70
+ print(f"Failed to create 8 clusters: {e2}")
71
+ print("CRITICAL: Custom Sharding not supported. Falling back to Standard Collection (No Sharding).")
72
+ # Fallback 2: Standard Collection
73
+ self.num_clusters = 1
74
+ if self.client.collection_exists(self.collection_name):
75
+ self.client.delete_collection(self.collection_name)
76
+
77
+ self.client.create_collection(
78
+ collection_name=self.collection_name,
79
+ vectors_config=VectorParams(size=self.vector_size, distance=Distance.COSINE)
80
+ )
81
+ print("Fallback successful: Created Standard Collection.")
82
 
83
  def _create_collection_and_shards(self, n_clusters):
84
  print(f"Creating collection '{self.collection_name}' with custom sharding ({n_clusters} clusters)...")
85
 
86
+ if self.client.collection_exists(self.collection_name):
87
+ print(f"Collection '{self.collection_name}' already exists. Skipping creation.")
88
+ return
89
+
90
+
91
  if self.is_local:
92
  # Local mode doesn't support sharding_method=CUSTOM
93
  self.client.create_collection(