-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
162 lines (131 loc) · 5 KB
/
commands.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import json
import numpy as np
from embeddings import load_image_from_url, embeddings
from milvus import collections, metrics
def format_url_list(urls):
quoted_urls = [f'"{url}"' for url in urls]
return f'[{",".join(quoted_urls)}]'
def insert_images(
model_name, urls, metadatas, image_embeddings=None, replace_existing=True
):
existing_urls = []
if not replace_existing:
existing_urls = [
search_result["url"]
for search_result in collections[model_name].query(
f"url in {format_url_list(urls)}",
consistency_level="Strong", # https://milvus.io/docs/consistency.md
)
]
new_urls = [url for url in urls if url not in existing_urls]
image_embeddings = (
[
embeddings[model_name].get_image_embedding(load_image_from_url(url))
for url in new_urls
]
if image_embeddings is None
else [
embedding
for url, embedding in zip(urls, image_embeddings)
if url not in existing_urls
]
)
metadatas = [
json.dumps(metadata)
for url, metadata in zip(urls, metadatas)
if url not in existing_urls
]
if len(new_urls) > 0:
collections[model_name].insert([new_urls, image_embeddings, metadatas])
return {
"added": new_urls,
"found": existing_urls,
}
def similarity_score(distance):
# 2 is the maximum distance between normalised vectors
return 100 * (1 - distance / 2)
def search_by_embedding(model_name, embedding, limit=10):
search_results = collections[model_name].search(
data=[embedding],
anns_field="embedding",
param={
"metric_type": metrics[model_name],
"params": {
"nprobe": 64
}, # https://milvus.io/docs/v1.1.1/performance_faq.md
},
output_fields=["metadata"],
limit=limit,
expr=None,
consistency_level="Strong", # https://milvus.io/docs/consistency.md
)
return [
{
"url": hit.id,
"metadata": json.loads(hit.entity.get("metadata")),
"similarity": similarity_score(hit.distance),
}
for hit in search_results[0]
]
def search_by_url(model_name, url, limit=10):
embedding = embeddings[model_name].get_image_embedding(load_image_from_url(url))
return search_by_embedding(model_name, embedding, limit)
def search_by_text(model_name, text, limit=10):
embedding = embeddings[model_name].get_text_embedding(text)
return search_by_embedding(model_name, embedding, limit)
def compare(model_name, url_left, url_right):
# alternatively, we could first try to fetch the embeddings from milvus in
# case their computation is significantly more expensive than a query
left = embeddings[model_name].get_image_embedding(load_image_from_url(url_left))
right = embeddings[model_name].get_image_embedding(load_image_from_url(url_right))
# calc_distance() has been removed from milvus
# it's a bit overkill anyway if we don't compare with vectors from the db
if metrics[model_name] == "L2":
# _squared_ L2, to be consistent with the distances in milvus' search
return similarity_score(np.sum(np.square(np.array(left) - np.array(right))))
raise RuntimeError(
"Distance calculation has not been implemented in the API. "
"Please contact the administrator."
)
def list_images(model_name, cursor="", limit=None, output_fields=None):
def prepare(entry):
if "embedding" in entry:
entry["embedding"] = [float(x) for x in entry["embedding"]]
if "metadata" in entry:
entry["metadata"] = json.loads(entry["metadata"])
return entry
return [
search_result["url"] if output_fields is None else prepare(search_result)
for search_result in collections[model_name].query(
f'url > "{cursor}"',
consistency_level="Strong", # https://milvus.io/docs/consistency.md
limit=limit,
output_fields=output_fields,
)
]
def ping():
return "pong"
def count(model_name):
urls = list_images(model_name)
result = len(urls)
while urls:
urls = list_images(model_name, urls[-1])
result += len(urls)
return result
def remove_images(model_name, urls):
# Milvus only supports deleting entities with clearly specified primary
# keys, which can be achieved merely with the term expression in. Other
# operators can be used only in query or scalar filtering in vector search.
# See Boolean Expression Rules for more information.
# https://milvus.io/docs/v2.2.x/delete_data.md?shell#Delete-Entities
collections[model_name].delete(f"url in {format_url_list(urls)}")
commands = dict(
insert_images=insert_images,
search_by_url=search_by_url,
search_by_text=search_by_text,
compare=compare,
list_images=list_images,
count=count,
remove_images=remove_images,
ping=ping,
)