Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions python/semantic_kernel/connectors/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,9 @@ def _deserialize_store_models_to_dicts(
storage_name = field.storage_name or field.name
if storage_name in rec:
dtype = DATATYPE_MAP_VECTOR[field.type_ or "default"]
rec[field.name] = buffer_to_array(rec[storage_name], dtype)
# Decode in place: the data model step reads the storage name key.
rec[storage_name] = buffer_to_array(rec[storage_name], dtype)
rec[field.name] = rec[storage_name]
else:
rec[field.name] = None
results.append(rec)
Expand All @@ -675,12 +677,14 @@ def _add_return_fields(self, query: TQuery, include_vectors: bool) -> TQuery:

"""
for field in self.definition.fields:
# The index attributes and the stored fields use the storage name.
storage_name = field.storage_name or field.name
match field.field_type:
case "vector":
if include_vectors:
query.return_field(field.name, decode_field=False)
query.return_field(storage_name, decode_field=False)
case _:
query.return_field(field.name)
query.return_field(storage_name)
return query


Expand Down Expand Up @@ -791,12 +795,14 @@ def _deserialize_store_models_to_dicts(
def _add_return_fields(self, query: TQuery, include_vectors: bool) -> TQuery:
"""Add the return fields to the query."""
for field in self.definition.fields:
# The index attributes and the stored fields use the storage name.
storage_name = field.storage_name or field.name
match field.field_type:
case FieldTypes.VECTOR:
if include_vectors:
query.return_field(field.name)
query.return_field(storage_name)
case _:
query.return_field(field.name)
query.return_field(storage_name)
return query


Expand Down
76 changes: 76 additions & 0 deletions python/tests/unit/connectors/memory/test_redis_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) Microsoft. All rights reserved.

from types import SimpleNamespace
from unittest.mock import AsyncMock, patch

import numpy as np
Expand All @@ -13,6 +14,7 @@
RedisJsonCollection,
RedisStore,
)
from semantic_kernel.data.vector import VectorSearchOptions, VectorStoreCollectionDefinition, VectorStoreField
from semantic_kernel.exceptions import VectorStoreInitializationException, VectorStoreOperationException

BASE_PATH = "redis.asyncio.client.Redis"
Expand Down Expand Up @@ -368,3 +370,77 @@ def test_hash_deserialization_handles_omitted_vector(collection_hash):
records = collection_hash._deserialize_store_models_to_dicts([{"id": "id1", "content": "content"}])

assert records[0]["vector"] is None


@fixture
def aliased_definition():
return VectorStoreCollectionDefinition(
fields=[
VectorStoreField("key", name="id", type="str"),
VectorStoreField("data", name="content", type="str", storage_name="doc_content", is_indexed=True),
VectorStoreField("vector", name="vector", dimensions=3, type="float", storage_name="doc_vector"),
]
)


@fixture
def aliased_collection(request, redis_unit_test_env, aliased_definition):
return request.param(
record_type=dict,
collection_name="test",
definition=aliased_definition,
env_file_path="test.env",
)


@mark.parametrize("aliased_collection", [RedisHashsetCollection, RedisJsonCollection], indirect=True)
@mark.parametrize("include_vectors", [True, False])
def test_search_returns_storage_names(aliased_collection, include_vectors):
query = aliased_collection._construct_vector_query(
[1.0, 2.0, 3.0], VectorSearchOptions(include_vectors=include_vectors)
)

expected = {"vector_distance", "id", "doc_content"} | ({"doc_vector"} if include_vectors else set())
assert set(query._return_fields) == expected


@mark.parametrize("aliased_collection", [RedisHashsetCollection], indirect=True)
async def test_hash_search_returns_aliased_fields(aliased_collection):
stored = {
"doc_content": "hello",
"doc_vector": np.array([1.0, 2.0, 3.0], dtype=np.float32).tobytes(),
"vector_distance": "0.25",
}
returned: list[str] = []

async def fake_search(query, query_params=None):
# Like RediSearch, return only the attributes named in RETURN.
args = query.get_args()
start = args.index("RETURN")
returned.extend(args[start + 2 : start + 2 + args[start + 1]])
doc = {"id": "key1"} | {name: stored[name] for name in returned if name in stored}
return SimpleNamespace(docs=[SimpleNamespace(**doc)], total=1)

async def fake_process(results, query, collection_name, redis_database, collection_type):
return [dict(doc.__dict__) for doc in results.docs]

with (
patch(f"{BASE_PATH_FT}.search", new=AsyncMock(side_effect=fake_search)),
patch.object(redis_module, "_process_search_results", new=fake_process),
):
results = await aliased_collection.search(vector=[1.0, 2.0, 3.0], top=3, include_vectors=True)
records = [result.record async for result in results.results]

assert returned == ["vector_distance", "id", "doc_content", "doc_vector"]
assert records == [{"id": "key1", "content": "hello", "vector": [1.0, 2.0, 3.0]}]


@mark.parametrize("aliased_collection", [RedisHashsetCollection], indirect=True)
def test_hash_deserialization_decodes_aliased_vector(aliased_collection):
vector = np.array([1.0, 2.0, 3.0], dtype=np.float32).tobytes()
store_record = {"id": "key1", "doc_content": "hello", "doc_vector": vector}

records = aliased_collection._deserialize_store_models_to_dicts([store_record])
record = aliased_collection._deserialize_dict_to_data_model(records[0], include_vectors=True)

assert record == {"id": "key1", "content": "hello", "vector": [1.0, 2.0, 3.0]}