-
Notifications
You must be signed in to change notification settings - Fork 2
/
2-qdrant.py
92 lines (73 loc) · 2.65 KB
/
2-qdrant.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
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
import struct
import time
from qdrant_client import QdrantClient
from qdrant_client.grpc import Filter, FieldCondition
from qdrant_client.http.models import MatchValue
from qdrant_client.models import Distance, VectorParams
from qdrant_client.models import PointStruct
from sentence_transformers import SentenceTransformer
import hashlib
md5_hash = hashlib.md5()
from common import read_verses
client = QdrantClient("0.0.0.0", grpc_port=6334, prefer_grpc=True)
collection_name = "collection_768"
if not client.collection_exists(collection_name):
client.create_collection(
collection_name,
vectors_config=VectorParams(size=768, distance=Distance.COSINE),
)
def qdrant_inserts(chunk):
points = []
for id, text, meta, embedding in chunk:
md5_hash.update(id.encode('utf-8'))
id = md5_hash.hexdigest()
# Ensure embedding is a list of floats
if isinstance(embedding, bytes):
embedding = list(struct.unpack(f'{len(embedding) // 4}f', embedding))
points.append(PointStruct(id=id, vector=embedding, payload={"text":text, "meta":meta}))
start_time = time.perf_counter()
client.upsert(
collection_name,
wait=False,
points=points,
)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"batch insert: {elapsed_time} sec")
return elapsed_time
def qdrant_search(embeddings):
search_result = client.search(
collection_name=collection_name,
query_vector=embeddings,
# query_filter=Filter(
# must=[FieldCondition(key="city", match=MatchValue(value="London"))]
# ),
with_payload=True,
limit=10,
)
# cycle through search results
for result in search_result:
print(f"Text: {result.payload['text']}; Similarity: {result.score}")
def qdrant_filter_search(embeddings):
search_result = client.search(
collection_name=collection_name,
query_vector=embeddings,
query_filter=Filter(
must=[FieldCondition(key="city", match=MatchValue(value="London"))]
),
with_payload=True,
limit=10,
)
print(search_result)
read_verses(qdrant_inserts, max_items=1400000, minibatch_size=1000)
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')
embeddings = model.encode("воскресил из мертвых")
start_time = time.perf_counter()
qdrant_search(embeddings)
qdrant_search(embeddings)
qdrant_search(embeddings)
qdrant_search(embeddings)
qdrant_search(embeddings)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Search time: {elapsed_time/5} sec")