Repository navigation
Expand file tree
/
Copy pathredis.py
More file actions
139 lines (124 loc) · 4.95 KB
/
Copy pathredis.py
File metadata and controls
139 lines (124 loc) · 4.95 KB
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
# -*- coding: utf-8 -*-
from typing import List
import numpy as np
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
from redis.commands.search.query import Query
from redis.commands.search.field import TagField, VectorField, NumericField
from redis.client import Redis
from modelcache.manager.vector_data.base import VectorBase, VectorData
from modelcache.utils import import_redis
from modelcache.utils.log import modelcache_log
from modelcache.utils.index_util import get_index_name
from modelcache.utils.index_util import get_index_prefix
import_redis()
class RedisVectorStore(VectorBase):
def __init__(
self,
host: str = "localhost",
port: str = "6379",
username: str = "",
password: str = "",
dimension: int = 0,
top_k: int = 1,
namespace: str = "",
):
if dimension <= 0:
raise ValueError(
f"invalid `dim` param: {dimension} in the Redis vector store."
)
self._client = Redis(
host=host, port=int(port), username=username, password=password
)
self.top_k = top_k
self.dimension = dimension
self.namespace = namespace
self.doc_prefix = f"{self.namespace}doc:"
def _check_index_exists(self, index_name: str) -> bool:
"""Check if Redis index exists."""
try:
self._client.ft(index_name).info()
except:
modelcache_log.info("Index does not exist")
return False
modelcache_log.info("Index already exists")
return True
def create_index(self, index_name, index_prefix):
dimension = self.dimension
if self._check_index_exists(index_name):
modelcache_log.info(
"The %s already exists, and it will be used directly", index_name
)
return 'already_exists'
else:
id_field_name = "data_id"
embedding_field_name = "data_vector"
id = NumericField(name=id_field_name)
embedding = VectorField(embedding_field_name,
"HNSW", {
"TYPE": "FLOAT32",
"DIM": dimension,
"DISTANCE_METRIC": "L2",
"INITIAL_CAP": 1000,
}
)
fields = [id, embedding]
definition = IndexDefinition(prefix=[index_prefix], index_type=IndexType.HASH)
# create Index
self._client.ft(index_name).create_index(
fields=fields, definition=definition
)
return 'create_success'
def mul_add(self, datas: List[VectorData], model=None):
# pipe = self._client.pipeline()
for data in datas:
id: int = data.id
embedding = data.data.astype(np.float32).tobytes()
id_field_name = "data_id"
embedding_field_name = "data_vector"
obj = {id_field_name: id, embedding_field_name: embedding}
index_prefix = get_index_prefix(model)
self._client.hset(f"{index_prefix}{id}", mapping=obj)
def search(self, data: np.ndarray, top_k: int = -1, model=None):
index_name = get_index_name(model)
id_field_name = "data_id"
embedding_field_name = "data_vector"
base_query = f'*=>[KNN 2 @{embedding_field_name} $vector AS distance]'
query = (
Query(base_query)
.sort_by("distance")
.return_fields(id_field_name, "distance")
.dialect(2)
)
query_params = {"vector": data.astype(np.float32).tobytes()}
results = (
self._client.ft(index_name)
.search(query, query_params=query_params)
.docs
)
return [(float(result.distance), int(getattr(result, id_field_name))) for result in results]
def rebuild(self, ids=None) -> bool:
pass
def rebuild_col(self, model):
index_name_model = get_index_name(model)
if self._check_index_exists(index_name_model):
try:
self._client.ft(index_name_model).dropindex(delete_documents=True)
except Exception as e:
raise ValueError(str(e))
try:
index_prefix = get_index_prefix(model)
self.create_index(index_name_model, index_prefix)
except Exception as e:
raise ValueError(str(e))
# return 'rebuild success'
def delete(self, ids) -> None:
pipe = self._client.pipeline()
for data_id in ids:
pipe.delete(f"{self.doc_prefix}{data_id}")
pipe.execute()
def create(self, model=None):
index_name = get_index_name(model)
index_prefix = get_index_prefix(model)
return self.create_index(index_name, index_prefix)
def get_index_by_name(self, index_name):
pass