-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
redis_collection.py
341 lines (299 loc) · 14 KB
/
redis_collection.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import json
import logging
import sys
from collections.abc import Sequence
from copy import copy
from typing import Any, ClassVar, TypeVar
if sys.version_info >= (3, 12):
from typing import override # pragma: no cover
else:
from typing_extensions import override # pragma: no cover
import numpy as np
from pydantic import ValidationError
from redis.asyncio.client import Redis
from redis.commands.search.indexDefinition import IndexDefinition
from semantic_kernel.connectors.memory.redis.const import INDEX_TYPE_MAP, RedisCollectionTypes
from semantic_kernel.connectors.memory.redis.utils import RedisWrapper, data_model_definition_to_redis_fields
from semantic_kernel.data.record_definition import (
VectorStoreRecordDefinition,
VectorStoreRecordKeyField,
VectorStoreRecordVectorField,
)
from semantic_kernel.data.vector_storage import VectorStoreRecordCollection
from semantic_kernel.exceptions.memory_connector_exceptions import (
MemoryConnectorException,
MemoryConnectorInitializationError,
)
from semantic_kernel.utils.experimental_decorator import experimental_class
logger: logging.Logger = logging.getLogger(__name__)
TModel = TypeVar("TModel")
@experimental_class
class RedisCollection(VectorStoreRecordCollection[str, TModel]):
"""A vector store record collection implementation using Redis."""
redis_database: Redis
prefix_collection_name_to_key_names: bool
collection_type: RedisCollectionTypes
supported_key_types: ClassVar[list[str] | None] = ["str"]
supported_vector_types: ClassVar[list[str] | None] = ["float"]
def __init__(
self,
data_model_type: type[TModel],
data_model_definition: VectorStoreRecordDefinition | None = None,
collection_name: str | None = None,
redis_database: Redis | None = None,
prefix_collection_name_to_key_names: bool = True,
collection_type: RedisCollectionTypes = RedisCollectionTypes.HASHSET,
connection_string: str | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
**kwargs: Any,
) -> None:
"""RedisMemoryStore is an abstracted interface to interact with a Redis node connection.
See documentation about connections: https://redis-py.readthedocs.io/en/stable/connections.html
See documentation about vector attributes: https://redis.io/docs/stack/search/reference/vectors.
"""
if redis_database:
super().__init__(
data_model_type=data_model_type,
data_model_definition=data_model_definition,
collection_name=collection_name,
redis_database=redis_database,
prefix_collection_name_to_key_names=prefix_collection_name_to_key_names,
collection_type=collection_type,
managed_client=False,
)
return
try:
from semantic_kernel.connectors.memory.redis.redis_settings import RedisSettings
redis_settings = RedisSettings.create(
connection_string=connection_string,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise MemoryConnectorInitializationError("Failed to create Redis settings.", ex) from ex
super().__init__(
data_model_type=data_model_type,
data_model_definition=data_model_definition,
collection_name=collection_name,
redis_database=RedisWrapper.from_url(redis_settings.connection_string.get_secret_value()),
prefix_collection_name_to_key_names=prefix_collection_name_to_key_names,
collection_type=collection_type,
)
def _get_redis_key(self, key: str) -> str:
if self.prefix_collection_name_to_key_names:
return f"{self.collection_name}:{key}"
return key
def _unget_redis_key(self, key: str) -> str:
if self.prefix_collection_name_to_key_names and ":" in key:
return key[len(self.collection_name) + 1 :]
return key
@override
async def create_collection(self, **kwargs) -> None:
"""Create a new index in Redis.
Args:
**kwargs: Additional keyword arguments.
fields (list[Fields]): The fields to create the index with, when not supplied,
these are created from the data_model_definition.
index_definition (IndexDefinition): The search index to create, if this is supplied
this is used instead of a index created based on the definition.
other kwargs are passed to the create_index method.
"""
if (index_definition := kwargs.pop("index_definition", None)) and (fields := kwargs.pop("fields", None)):
if isinstance(index_definition, IndexDefinition):
await self.redis_database.ft(self.collection_name).create_index(
fields, definition=index_definition, **kwargs
)
return
raise MemoryConnectorException("Invalid index type supplied.")
fields = data_model_definition_to_redis_fields(self.data_model_definition, self.collection_type)
index_definition = IndexDefinition(
prefix=f"{self.collection_name}:", index_type=INDEX_TYPE_MAP[self.collection_type]
)
await self.redis_database.ft(self.collection_name).create_index(fields, definition=index_definition, **kwargs)
@override
async def does_collection_exist(self, **kwargs) -> bool:
try:
await self.redis_database.ft(self.collection_name).info()
return True
except Exception:
return False
@override
async def delete_collection(self, **kwargs) -> None:
exists = await self.does_collection_exist()
if exists:
await self.redis_database.ft(self.collection_name).dropindex(**kwargs)
else:
logger.debug("Collection does not exist, skipping deletion.")
@override
async def __aexit__(self, exc_type, exc_value, traceback) -> None:
"""Exit the context manager."""
if self.managed_client:
await self.redis_database.aclose()
@experimental_class
class RedisHashsetCollection(RedisCollection):
"""A vector store record collection implementation using Redis Hashsets."""
def __init__(
self,
data_model_type: type[TModel],
data_model_definition: VectorStoreRecordDefinition | None = None,
collection_name: str | None = None,
redis_database: Redis | None = None,
prefix_collection_name_to_key_names: bool = False,
connection_string: str | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
**kwargs: Any,
) -> None:
"""RedisMemoryStore is an abstracted interface to interact with a Redis node connection.
See documentation about connections: https://redis-py.readthedocs.io/en/stable/connections.html
See documentation about vector attributes: https://redis.io/docs/stack/search/reference/vectors.
"""
super().__init__(
data_model_type=data_model_type,
data_model_definition=data_model_definition,
collection_name=collection_name,
redis_database=redis_database,
prefix_collection_name_to_key_names=prefix_collection_name_to_key_names,
collection_type=RedisCollectionTypes.HASHSET,
connection_string=connection_string,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
**kwargs,
)
@override
async def _inner_upsert(self, records: Sequence[Any], **kwargs: Any) -> Sequence[str]:
return await asyncio.gather(*[self._single_upsert(record) for record in records])
async def _single_upsert(self, upsert_record: Any) -> str:
await self.redis_database.hset(**upsert_record)
return self._unget_redis_key(upsert_record["name"])
@override
async def _inner_get(self, keys: Sequence[str], **kwargs) -> Sequence[dict[bytes, bytes]] | None:
results = await asyncio.gather(*[self.redis_database.hgetall(self._get_redis_key(key)) for key in keys])
return [result for result in results if result]
@override
async def _inner_delete(self, keys: Sequence[str], **kwargs: Any) -> None:
await self.redis_database.delete(*[self._get_redis_key(key) for key in keys])
@override
def _serialize_dicts_to_store_models(
self,
records: Sequence[dict[str, Any]],
**kwargs: Any,
) -> Sequence[dict[str, Any]]:
"""Serialize the dict to a Redis store model."""
results = []
for record in records:
result = {"mapping": {}}
metadata = {}
for name, field in self.data_model_definition.fields.items():
if isinstance(field, VectorStoreRecordVectorField):
if not isinstance(record[name], np.ndarray):
record[name] = np.array(record[name])
result["mapping"][name] = record[name].tobytes()
continue
if isinstance(field, VectorStoreRecordKeyField):
result["name"] = self._get_redis_key(record[name])
continue
metadata[name] = record[field.name]
result["mapping"]["metadata"] = json.dumps(metadata)
results.append(result)
return results
@override
def _deserialize_store_models_to_dicts(
self,
records: Sequence[dict[bytes, bytes]],
keys: Sequence[str],
**kwargs: Any,
) -> Sequence[dict[str, Any]]:
results = []
for key, record in zip(keys, records):
if record:
flattened = json.loads(record[b"metadata"])
for name, field in self.data_model_definition.fields.items():
if isinstance(field, VectorStoreRecordKeyField):
flattened[name] = self._unget_redis_key(key)
if isinstance(field, VectorStoreRecordVectorField):
flattened[name] = np.frombuffer(record[name.encode()]).tolist()
results.append(flattened)
return results
@experimental_class
class RedisJsonCollection(RedisCollection):
"""A vector store record collection implementation using Redis Json."""
def __init__(
self,
data_model_type: type[TModel],
data_model_definition: VectorStoreRecordDefinition | None = None,
collection_name: str | None = None,
redis_database: Redis | None = None,
prefix_collection_name_to_key_names: bool = False,
connection_string: str | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
**kwargs: Any,
) -> None:
"""RedisMemoryStore is an abstracted interface to interact with a Redis node connection.
See documentation about connections: https://redis-py.readthedocs.io/en/stable/connections.html
See documentation about vector attributes: https://redis.io/docs/stack/search/reference/vectors.
"""
super().__init__(
data_model_type=data_model_type,
data_model_definition=data_model_definition,
collection_name=collection_name,
redis_database=redis_database,
prefix_collection_name_to_key_names=prefix_collection_name_to_key_names,
collection_type=RedisCollectionTypes.JSON,
connection_string=connection_string,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
**kwargs,
)
@override
async def _inner_upsert(self, records: Sequence[Any], **kwargs: Any) -> Sequence[str]:
return await asyncio.gather(*[self._single_upsert(record) for record in records])
async def _single_upsert(self, upsert_record: Any) -> str:
await self.redis_database.json().set(upsert_record["name"], "$", upsert_record["value"])
return self._unget_redis_key(upsert_record["name"])
@override
async def _inner_get(self, keys: Sequence[str], **kwargs) -> Sequence[dict[bytes, bytes]] | None:
kwargs_copy = copy(kwargs)
kwargs_copy.pop("include_vectors", None)
results = await self.redis_database.json().mget([self._get_redis_key(key) for key in keys], "$", **kwargs_copy)
return [result[0] for result in results if result]
@override
async def _inner_delete(self, keys: Sequence[str], **kwargs: Any) -> None:
await asyncio.gather(*[self.redis_database.json().delete(key, **kwargs) for key in keys])
@override
def _serialize_dicts_to_store_models(
self,
records: Sequence[dict[str, Any]],
**kwargs: Any,
) -> Sequence[dict[str, Any]]:
"""Serialize the dict to a Redis store model."""
results = []
for record in records:
result = {"value": {}}
for name, field in self.data_model_definition.fields.items():
if isinstance(field, VectorStoreRecordKeyField):
result["name"] = self._get_redis_key(record[name])
continue
if isinstance(field, VectorStoreRecordVectorField):
if isinstance(record[name], np.ndarray):
record[name] = record[name].tolist()
result["value"][name] = record[name]
result["value"][name] = record[name]
results.append(result)
return results
@override
def _deserialize_store_models_to_dicts(
self,
records: Sequence[dict[str, Any]],
keys: Sequence[str],
**kwargs: Any,
) -> Sequence[dict[str, Any]]:
results = []
for key, record in zip(keys, records):
record[self.data_model_definition.key_field_name] = self._unget_redis_key(key)
results.append(record)
return results