-
Notifications
You must be signed in to change notification settings - Fork 2
/
8-marqo.py
104 lines (82 loc) · 2.66 KB
/
8-marqo.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
93
94
95
96
97
98
99
100
101
102
103
104
import struct
import time
import marqo
import numpy as np
from sentence_transformers import SentenceTransformer
from common import read_verses
mq = marqo.Client(url='http://localhost:8882')
collection_name = "collection_768"
mq.create_index(
index_name=collection_name,
type='structured',
model="no_model",
model_properties={
"type": "no_model",
"dimensions": 768
},
ann_parameters={
"spaceType": "prenormalized-angular",
"parameters": {
"efConstruction": 512,
"m": 16
}
},
# field types can be found here: https://docs.marqo.ai/2.7/API-Reference/Indexes/create_structured_index/#fields
all_fields=[
{
"name": "custom",
"type": "custom_vector",
"features": ["lexical_search", "filter"]
},
],
tensor_fields=["custom"])
index = mq.index(collection_name)
def marqo_inserts(chunk):
docs = []
for id, text, meta, embedding in chunk:
if isinstance(embedding, bytes):
embedding = list(struct.unpack(f'{len(embedding) // 4}f', embedding))
if isinstance(embedding, np.ndarray):
embedding = embedding.tolist()
docs.append({
"custom": {
"content": text,
"vector": embedding
},
# "meta": meta,
"_id": id
})
start_time = time.perf_counter()
res = index.add_documents(docs)
# , tensor_fields=["custom"], mappings={
# "custom": {"type": "custom_vector"},
# })
print(res)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"batch insert: {elapsed_time} sec")
return elapsed_time
def marqo_search(embedding):
if isinstance(embedding, bytes):
embedding = list(struct.unpack(f'{len(embedding) // 4}f', embedding))
if isinstance(embedding, np.ndarray):
embedding = embedding.tolist()
search_result = mq.index(collection_name).search(
q={
"customVector": {"vector": embedding}
},
)
for result in search_result['hits']:
print(f"Text: {result['custom']}; Similarity: {result['_score']}")
read_verses(marqo_inserts, max_items=1400000, minibatch_size=128)
model = SentenceTransformer('sentence-transformers/paraphrase-multilingual-mpnet-base-v2')
embeddings = model.encode("воскресил из мертвых")
start_time = time.perf_counter()
marqo_search(embeddings)
marqo_search(embeddings)
marqo_search(embeddings)
marqo_search(embeddings)
marqo_search(embeddings)
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print(f"Search time: {elapsed_time} sec")