-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_weviate.py
More file actions
96 lines (76 loc) · 3.12 KB
/
Copy pathmanager_weviate.py
File metadata and controls
96 lines (76 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from src.database.weviate import WeaviateHelper
import os
import sys
def list_collections(weaviate_helper):
"""List all collections in Weaviate."""
collections = weaviate_helper.list_collections()
if collections:
print("\nAvailable collections in Weaviate:")
for i, collection in enumerate(collections, 1):
print(f"{i}. {collection['name']} - {collection['description']}")
else:
print("No collections found in Weaviate.")
def delete_collections_except(weaviate_helper, collection_to_keep):
"""Delete all collections except the specified one."""
print(f"\nPreparing to delete all collections except '{collection_to_keep}'...")
# Get current collections
collections = weaviate_helper.list_collections()
collection_names = [c['name'] for c in collections]
if collection_to_keep not in collection_names:
print(f"Warning: Collection '{collection_to_keep}' does not exist. No collections will be kept.")
# Ask for confirmation
collections_to_delete = [name for name in collection_names if name != collection_to_keep]
if not collections_to_delete:
print("No collections to delete.")
return
print("\nThe following collections will be deleted:")
for name in collections_to_delete:
print(f"- {name}")
confirm = input("\nAre you sure you want to continue? (y/n): ")
if confirm.lower() != 'y':
print("Operation cancelled.")
return
# Perform deletion
print("\nDeleting collections...")
result = weaviate_helper.delete_all_collections_except(collection_to_keep)
# Show results
print("\nOperation completed:")
print(f"- Kept collection: {result['kept']}")
if result['deleted']:
print("- Deleted collections:")
for name in result['deleted']:
print(f" - {name}")
if result['errors']:
print("\nErrors occurred while deleting:")
for error in result['errors']:
print(f"- {error['collection']}: {error['error']}")
def main():
try:
# Initialize the Weaviate helper
weaviate_helper = WeaviateHelper(
weaviate_api_key="test_apikey_**//",
weaviate_host="127.0.0.1",
weaviate_port="8080",
weaviate_grpc_port="50051"
)
while True:
print("\n=== Weaviate Collection Manager ===")
print("1. List all collections")
print("2. Delete all collections except 'Knowledge'")
print("3. Exit")
choice = input("\nEnter your choice (1-3): ")
if choice == '1':
list_collections(weaviate_helper)
elif choice == '2':
delete_collections_except(weaviate_helper, "Knowledge")
elif choice == '3':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
except Exception as e:
print(f"\nError: {str(e)}", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
sys.exit(main())