demeter
Autonomous Hydroponic Intelligencegit clone https://github.com/maydayv7/demeter
backend/server/reset-db.py (2798B)
1import sys
2import os
3from dotenv import load_dotenv
4from qdrant_client import QdrantClient
5from qdrant_client.http import models
6
7# --- CONFIGURATION ---
8COLLECTION_NAME = "Farm_Memory"
9VECTOR_SIZE = 516 # 512 (Vision) + 4 (Sensors: pH, EC, Temp, Humid)
10
11# 1. Load Environment Variables
12current_dir = os.path.dirname(os.path.abspath(__file__))
13project_root = os.path.abspath(os.path.join(current_dir, '../../'))
14env_path = os.path.join(project_root, '.env')
15load_dotenv(env_path)
16
17# 2. Connect to Qdrant (Handles Cloud or Local)
18qdrant_url = os.getenv("QDRANT_URL", "http://localhost:6333")
19qdrant_key = os.getenv("QDRANT_API_KEY", None)
20
21print(f"🔌 Connecting to Qdrant at: {qdrant_url}...")
22client = QdrantClient(url=qdrant_url, api_key=qdrant_key)
23
24def reset_db():
25 # 3. Check if Collection Exists and Delete it
26 if client.collection_exists(COLLECTION_NAME):
27 print(f"🔥 Deleting existing collection '{COLLECTION_NAME}'...")
28 client.delete_collection(COLLECTION_NAME)
29 print("✅ Deleted.")
30 else:
31 print(f"⚠️ Collection '{COLLECTION_NAME}' did not exist.")
32
33 # 4. Create the New Collection
34 print(f"🛠️ Creating collection '{COLLECTION_NAME}' with {VECTOR_SIZE} dimensions...")
35 client.create_collection(
36 collection_name=COLLECTION_NAME,
37 vectors_config=models.VectorParams(
38 size=VECTOR_SIZE,
39 distance=models.Distance.COSINE
40 )
41 )
42 print("✅ Collection created.")
43
44 # 5. Create Payload Indexes (CRITICAL STEP)
45 print("🏗️ Creating Payload Indexes...")
46
47 # A. Text Fields (Keyword)
48 text_indexes = [
49 "crop", # "Lettuce"
50 "stage", # "Vegetative"
51 "crop_id", # "Batch_A1"
52 "outcome", # "Negative"
53 "action_taken" # "Add CalMag"
54 ]
55
56 for field in text_indexes:
57 try:
58 client.create_payload_index(
59 collection_name=COLLECTION_NAME,
60 field_name=field,
61 field_schema=models.PayloadSchemaType.KEYWORD
62 )
63 print(f" 👉 Indexed (Keyword): '{field}'")
64 except Exception as e:
65 print(f" ⚠️ Error indexing '{field}': {e}")
66
67 # B. Numeric Fields (Integer)
68 # 👇 NEW: Index sequence_number so we can sort by it later
69 try:
70 client.create_payload_index(
71 collection_name=COLLECTION_NAME,
72 field_name="sequence_number",
73 field_schema=models.PayloadSchemaType.INTEGER
74 )
75 print(f" 👉 Indexed (Integer): 'sequence_number'")
76 except Exception as e:
77 print(f" ⚠️ Error indexing 'sequence_number': {e}")
78
79 print("\n🎉 Database Reset Complete! You are ready to ingest data.")
80
81if __name__ == "__main__":
82 reset_db()