-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhello.py
executable file
·88 lines (70 loc) · 2.18 KB
/
hello.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
#!/usr/bin/env python
import numpy as np
from redis import Redis
from redis.commands.search.field import TagField, VectorField
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query
from redis.exceptions import ResponseError
def main():
r = Redis(host='localhost', port=6379, decode_responses=True)
# print(r.info()['redis_version'])
index_name = "vectors"
doc_prefix = "doc:"
try:
schema = (
TagField("genre"),
VectorField("values",
"HNSW", {
"TYPE": "FLOAT32",
"DIM": 3,
"DISTANCE_METRIC": "COSINE"
}
)
)
definition = IndexDefinition(
prefix=[doc_prefix],
index_type=IndexType.HASH
)
r.ft(index_name).create_index(fields=schema, definition=definition)
except ResponseError as e:
if str(e) != 'Index already exists':
raise e
pipe = r.ft(index_name).pipeline()
vectors = [
{
"id": 1,
"values": [0.1, 0.2, 0.3],
"metadata": {"genre": "drama"},
},
{
"id": 2,
"values": [0.2, 0.3, 0.4],
"metadata": {"genre": "action"},
},
]
for vector in vectors:
key = f"{doc_prefix}{vector['id']}"
value = {
"genre": vector["metadata"]["genre"],
"values": np.array(vector["values"]).astype(np.float32).tobytes()
}
pipe.hset(key, mapping=value)
pipe.execute()
# print(doc_prefix)
# for key in r.scan_iter(f"{doc_prefix}*"):
# print(f" {key}")
query = (
Query("(@genre:{ action })=>[KNN 2 @values $vector as score]")
.sort_by("score")
.return_fields("id", "score", "genre")
.dialect(2)
)
query_params = {
"vector": np.array([0.1, 0.2, 0.3]).astype(np.float32).tobytes()
}
results = r.ft(index_name).search(query, query_params).docs
for result in results:
print(result)
r.ft(index_name).dropindex(True)
if __name__ == "__main__":
main()