diff --git a/python/semantic_kernel/memory/volatile_memory_store.py b/python/semantic_kernel/memory/volatile_memory_store.py index dd1a631d0614..7a57862f1edd 100644 --- a/python/semantic_kernel/memory/volatile_memory_store.py +++ b/python/semantic_kernel/memory/volatile_memory_store.py @@ -251,6 +251,9 @@ async def get_nearest_matches( # Get all the records in the collection memory_records = list(self._store[collection_name].values()) + if not memory_records: + return [] + # Convert the collection of embeddings into a numpy array (stacked) embeddings = array([x._embedding for x in memory_records], dtype=float) embeddings = embeddings.reshape(embeddings.shape[0], -1) diff --git a/python/tests/unit/memory/test_volatile_memory_store.py b/python/tests/unit/memory/test_volatile_memory_store.py new file mode 100644 index 000000000000..3b3f6323ff22 --- /dev/null +++ b/python/tests/unit/memory/test_volatile_memory_store.py @@ -0,0 +1,39 @@ +# Copyright (c) Microsoft. All rights reserved. + +import numpy as np + +from semantic_kernel.memory.memory_record import MemoryRecord +from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore + + +async def test_get_nearest_matches_empty_collection(): + store = VolatileMemoryStore() + await store.create_collection("test") + result = await store.get_nearest_matches("test", np.array([1.0, 2.0]), limit=3) + assert result == [] + + +async def test_get_nearest_matches_missing_collection(): + store = VolatileMemoryStore() + result = await store.get_nearest_matches("missing", np.array([1.0, 2.0]), limit=3) + assert result == [] + + +async def test_get_nearest_matches_with_records(): + store = VolatileMemoryStore() + await store.create_collection("test") + await store.upsert( + "test", + MemoryRecord( + is_reference=False, + external_source_name=None, + id="first", + description="first record", + text="hello", + additional_metadata=None, + embedding=np.array([1.0, 0.0]), + ), + ) + result = await store.get_nearest_matches("test", np.array([1.0, 0.0]), limit=3) + assert len(result) == 1 + assert result[0][0].id == "first"