-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
55 lines (46 loc) · 1.92 KB
/
main.py
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
import logging
from qdrant_client import QdrantClient
from qdrant_client.http import models
import random
# Configure logging for your application
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Increase log level for httpx to WARNING to suppress its INFO logs
logging.getLogger("httpx").setLevel(logging.WARNING)
client = QdrantClient("localhost", port=6333)
collection_name = "sharding_collection"
key = "tempKey"
# Deleting an existing collection if it exists
response = client.delete_collection(collection_name=f"{collection_name}")
logger.info(f"Deleted collection '{collection_name}': {response}")
# Creating a new collection with specific configuration
response = client.create_collection(
collection_name=f"{collection_name}",
shard_number=6,
sharding_method=models.ShardingMethod.CUSTOM,
vectors_config=models.VectorParams(size=768, distance=models.Distance.COSINE)
)
logger.info(f"Created collection '{collection_name}' with custom sharding: {response}")
# Creating a shard key for the collection
response = client.create_shard_key(f"{collection_name}", f"{key}")
logger.info(f"Created shard key '{key}' for collection '{collection_name}': {response}")
# Counter for generating unique point IDs
point_counter = 0
# Function to generate a random vector of 768 dimensions with up to 15 decimal points
def generate_random_vector():
return [round(random.uniform(0, 1), 15) for _ in range(768)]
# Run the loop 1000 times
for _ in range(1000):
random_vector = generate_random_vector()
point_counter += 1
response = client.upsert(
collection_name=f"{collection_name}",
points=[
models.PointStruct(
id=point_counter,
vector=random_vector,
),
],
shard_key_selector=f"{key}",
)
logger.info(f"Upserted point with ID {point_counter} into collection '{collection_name}' with shard key '{key}': {response}")